KiCad PCB EDA Suite
Loading...
Searching...
No Matches
search_pane_tab.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) 2022 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
21#include <widgets/search_pane.h>
22#include <kiway.h>
23#include <vector>
24#include <string_utils.h>
25
27 wxWindowID winid, const wxPoint& pos,
28 const wxSize& size ) :
29 wxListView( parent, winid, pos, size, wxLC_REPORT | wxLC_VIRTUAL ),
30 m_handler( handler ),
31 m_sortCol( -1 ),
32 m_sortAscending( true )
33{
34 SetItemCount( 0 );
35
37
38 Bind( wxEVT_LIST_ITEM_SELECTED, &SEARCH_PANE_LISTVIEW::OnItemSelected, this );
39 Bind( wxEVT_LIST_ITEM_ACTIVATED, &SEARCH_PANE_LISTVIEW::OnItemActivated, this );
40 Bind( wxEVT_LIST_ITEM_FOCUSED, &SEARCH_PANE_LISTVIEW::OnItemSelected, this );
41 Bind( wxEVT_LIST_ITEM_DESELECTED, &SEARCH_PANE_LISTVIEW::OnItemDeselected, this );
42 Bind( wxEVT_LIST_COL_CLICK, &SEARCH_PANE_LISTVIEW::OnColClicked, this );
43}
44
45
47{
48 Unbind( wxEVT_LIST_ITEM_SELECTED, &SEARCH_PANE_LISTVIEW::OnItemSelected, this );
49 Unbind( wxEVT_LIST_ITEM_ACTIVATED, &SEARCH_PANE_LISTVIEW::OnItemActivated, this );
50 Unbind( wxEVT_LIST_ITEM_FOCUSED, &SEARCH_PANE_LISTVIEW::OnItemSelected, this );
51 Unbind( wxEVT_LIST_ITEM_DESELECTED, &SEARCH_PANE_LISTVIEW::OnItemDeselected, this );
52}
53
54
55void SEARCH_PANE_LISTVIEW::GetSelectRowsList( std::vector<long>& aSelectedList )
56{
57 long idx = GetFirstSelected();
58
59 if( idx < 0 ) // Nothing selected
60 return;
61
62 aSelectedList.emplace_back( idx );
63
64 idx = GetNextSelected( idx );
65
66 while( idx >= 0 )
67 {
68 aSelectedList.emplace_back( idx );
69 idx = GetNextSelected( idx );
70 }
71}
72
73
74void SEARCH_PANE_LISTVIEW::OnItemActivated( wxListEvent& aEvent )
75{
76 CallAfter(
77 [=]()
78 {
79 m_handler->ActivateItem( aEvent.GetIndex() );
80
81 // Reset our selection to match the selected list items
82 std::vector<long> list;
83 GetSelectRowsList( list );
84 m_handler->SelectItems( list );
85 } );
86
87 aEvent.Skip();
88}
89
90
91void SEARCH_PANE_LISTVIEW::OnItemSelected( wxListEvent& aEvent )
92{
93 CallAfter(
94 [=]()
95 {
96 std::vector<long> list;
97 GetSelectRowsList( list );
98 m_handler->SelectItems( list );
99 } );
100
101 aEvent.Skip();
102}
103
104
105void SEARCH_PANE_LISTVIEW::OnItemDeselected( wxListEvent& aEvent )
106{
107 CallAfter(
108 [=]()
109 {
110 std::vector<long> list;
111 GetSelectRowsList( list );
112 m_handler->SelectItems( list );
113 } );
114
115 aEvent.Skip();
116}
117
118
119void SEARCH_PANE_LISTVIEW::OnColClicked( wxListEvent& aEvent )
120{
121 if( aEvent.GetColumn() == m_sortCol )
122 {
124 }
125 else
126 {
127 m_sortAscending = true;
128 m_sortCol = aEvent.GetColumn();
129 }
130
131 ShowSortIndicator( m_sortCol, m_sortAscending );
132
133 Sort();
134 Refresh();
135}
136
137
139{
141}
142
143
145{
146 Freeze();
147 DeleteAllColumns();
148
149 std::vector<std::pair<wxString, int>> columns = m_handler->GetColumns();
150
151 for( auto& [ columnName, colProportion ] : columns )
152 AppendColumn( wxGetTranslation( columnName ) );
153
154 int widthUnit = GetClientSize().GetWidth() / 4;
155
156 for( int ii = 0; ii < (int) columns.size(); ++ii )
157 SetColumnWidth( ii, widthUnit * columns[ ii ].second );
158
159 Thaw();
160}
161
162
163wxString SEARCH_PANE_LISTVIEW::OnGetItemText( long item, long column ) const
164{
165 return m_handler->GetResultCell( (int) item, (int) column );
166}
167
168
169SEARCH_PANE_TAB::SEARCH_PANE_TAB( SEARCH_HANDLER* handler, wxWindow* parent, wxWindowID aId,
170 const wxPoint& aLocation, const wxSize& aSize ) :
171 wxPanel( parent, aId, aLocation, aSize ),
172 m_handler( handler )
173{
174 wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
175
176 m_listView = new SEARCH_PANE_LISTVIEW( handler, this );
177 sizer->Add( m_listView, 5, wxRIGHT | wxBOTTOM | wxEXPAND, 1 );
178
179 SetSizer( sizer );
180
181 Layout();
182 sizer->Fit( this );
183}
184
185
186void SEARCH_PANE_TAB::Search( wxString& query )
187{
188 int results = m_handler->Search( query );
189 m_listView->SetItemCount( results );
190 m_listView->Sort();
191 m_listView->Refresh();
192}
193
194
196{
197 m_listView->SetItemCount( 0 );
198 m_listView->Refresh();
199}
200
201
203{
205}
virtual void Sort(int aCol, bool aAscending)=0
virtual wxString GetResultCell(int row, int col)=0
std::vector< std::pair< wxString, int > > GetColumns() const
Definition: search_pane.h:38
virtual void SelectItems(std::vector< long > &aItemRows)
Definition: search_pane.h:44
virtual void ActivateItem(long aItemRow)
Definition: search_pane.h:45
virtual int Search(const wxString &string)=0
void OnItemDeselected(wxListEvent &aEvent)
SEARCH_PANE_LISTVIEW(SEARCH_HANDLER *handler, wxWindow *parent, wxWindowID winid=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize)
void GetSelectRowsList(std::vector< long > &aSelectedList)
void OnColClicked(wxListEvent &aEvent)
void OnItemSelected(wxListEvent &aEvent)
SEARCH_HANDLER * m_handler
void OnItemActivated(wxListEvent &aEvent)
wxString OnGetItemText(long item, long column) const override
SEARCH_HANDLER * m_handler
void Search(wxString &query)
SEARCH_PANE_LISTVIEW * m_listView
SEARCH_PANE_TAB(SEARCH_HANDLER *handler, wxWindow *parent, wxWindowID aId=wxID_ANY, const wxPoint &aLocation=wxDefaultPosition, const wxSize &aSize=wxDefaultSize)
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...