KiCad PCB EDA Suite
Loading...
Searching...
No Matches
symbol_filter_combobox.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2018-2024 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
25
26
27#include <iostream>
28
29static const wxString kNoParentSymbol( "<do not derive>" );
30
32{
33public:
35
36 wxString GetStringValue() const override { return m_selectedSymbol; }
37
38 void SetSelectedSymbol( const wxString& aSymbolName )
39 {
40 m_selectedSymbol = aSymbolName;
41 GetComboCtrl()->SetValue( m_selectedSymbol );
42 }
43
44 void Accept() override
45 {
46 wxString selectedSymbol = getSelectedValue().value_or( wxEmptyString );
47
48 Dismiss();
49
50 // No update on empty
51 if( !selectedSymbol.IsEmpty() && selectedSymbol != m_selectedSymbol )
52 {
53 m_selectedSymbol = selectedSymbol;
54 GetComboCtrl()->SetValue( m_selectedSymbol );
55
56 wxCommandEvent changeEvent( FILTERED_ITEM_SELECTED );
57 wxPostEvent( GetComboCtrl(), changeEvent );
58 }
59 }
60
61 void SetSymbolList( const wxArrayString& aSymbolList )
62 {
63 m_symbolList = aSymbolList;
64 m_symbolList.Sort();
66 }
67
68private:
69 void getListContent( wxArrayString& aListContent ) override
70 {
71 const wxString filterString = getFilterValue();
72
73 // Special handling for <no net>
74 if( filterString.IsEmpty() || kNoParentSymbol.Lower().Matches( filterString ) )
75 aListContent.insert( aListContent.begin(), kNoParentSymbol );
76
77 // Simple substring, case-insensitive search
78 for( const wxString& symbol : m_symbolList )
79 {
80 if( filterString.IsEmpty() || symbol.Lower().Contains( filterString.Lower() ) )
81 aListContent.push_back( symbol );
82 }
83 }
84
86 wxArrayString m_symbolList;
87};
88
89
90SYMBOL_FILTER_COMBOBOX::SYMBOL_FILTER_COMBOBOX( wxWindow* parent, wxWindowID id, const wxPoint& pos,
91 const wxSize& size, long style ) :
92 FILTER_COMBOBOX( parent, id, pos, size, style )
93{
94 std::unique_ptr<SYMBOL_FILTER_COMBOPOPUP> popup = std::make_unique<SYMBOL_FILTER_COMBOPOPUP>();
95 m_selectorPopup = popup.get();
96 setFilterPopup( std::move( popup ) );
97}
98
99
100void SYMBOL_FILTER_COMBOBOX::SetSymbolList( const wxArrayString& aSymbolList )
101{
102 m_selectorPopup->SetSymbolList( aSymbolList );
103}
104
105
106void SYMBOL_FILTER_COMBOBOX::SetSelectedSymbol( const wxString& aSymbolName )
107{
108 m_selectorPopup->SetSelectedSymbol( aSymbolName );
109}
110
111
113{
114 wxString value = m_selectorPopup->GetStringValue();
115
116 if( value == kNoParentSymbol )
117 return wxEmptyString;
118
119 return value;
120}
A combobox that has a filterable popup.
void setFilterPopup(std::unique_ptr< FILTER_COMBOPOPUP > aPopup)
void rebuildList()
Call this to rebuild the list from the getListContent() method.
wxString getFilterValue() const
Get the current value of the filter control.
std::optional< wxString > getSelectedValue() const
Get the currently selected value in the list, or std::nullopt.
SYMBOL_FILTER_COMBOPOPUP * m_selectorPopup
void SetSelectedSymbol(const wxString &aSymbolName)
void SetSymbolList(const wxArrayString &aSymbolList)
SYMBOL_FILTER_COMBOBOX(wxWindow *parent, wxWindowID id, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=0)
wxString GetValue() const override
void SetSymbolList(const wxArrayString &aSymbolList)
void getListContent(wxArrayString &aListContent) override
Implement this to fill in the given list.
void SetSelectedSymbol(const wxString &aSymbolName)
wxString GetStringValue() const override
static const wxString kNoParentSymbol("<do not derive>")