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
30{
31public:
33
34 wxString GetStringValue() const override { return m_selectedSymbol; }
35
36 void SetSelectedSymbol( const wxString& aSymbolName )
37 {
38 m_selectedSymbol = aSymbolName;
39 GetComboCtrl()->SetValue( m_selectedSymbol );
40 }
41
42 void Accept() override
43 {
44 wxString selectedSymbol = getSelectedValue().value_or( wxEmptyString );
45
46 Dismiss();
47
48 // No update on empty
49 if( !selectedSymbol.IsEmpty() && selectedSymbol != m_selectedSymbol )
50 {
51 m_selectedSymbol = selectedSymbol;
52 GetComboCtrl()->SetValue( m_selectedSymbol );
53
54 wxCommandEvent changeEvent( FILTERED_ITEM_SELECTED );
55 wxPostEvent( GetComboCtrl(), changeEvent );
56 }
57 }
58
59 void SetSymbolList( const wxArrayString& aSymbolList )
60 {
61 m_symbolList = aSymbolList;
62 m_symbolList.Sort();
64 }
65
66private:
67 void getListContent( wxArrayString& aListContent ) override
68 {
69 const wxString filterString = getFilterValue();
70
71 // Simple substring, case-insensitive search
72 for( const wxString& symbol : m_symbolList )
73 {
74 if( filterString.IsEmpty() || symbol.Lower().Contains( filterString.Lower() ) )
75 aListContent.push_back( symbol );
76 }
77 }
78
80 wxArrayString m_symbolList;
81};
82
83
84SYMBOL_FILTER_COMBOBOX::SYMBOL_FILTER_COMBOBOX( wxWindow* parent, wxWindowID id, const wxPoint& pos,
85 const wxSize& size, long style ) :
86 FILTER_COMBOBOX( parent, id, pos, size, style )
87{
88 std::unique_ptr<SYMBOL_FILTER_COMBOPOPUP> popup = std::make_unique<SYMBOL_FILTER_COMBOPOPUP>();
89 m_selectorPopup = popup.get();
90 setFilterPopup( std::move( popup ) );
91}
92
93
94void SYMBOL_FILTER_COMBOBOX::SetSymbolList( const wxArrayString& aSymbolList )
95{
96 m_selectorPopup->SetSymbolList( aSymbolList );
97}
98
99
100void SYMBOL_FILTER_COMBOBOX::SetSelectedSymbol( const wxString& aSymbolName )
101{
102 m_selectorPopup->SetSelectedSymbol( aSymbolName );
103}
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)
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