KiCad PCB EDA Suite
Loading...
Searching...
No Matches
file_checklist_ctrl.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 The 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 3
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, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <memory>
21
22#include <wx/dataview.h>
23#include <wx/filename.h>
24
27
28
29void FILE_CHECKLIST_DATA::SetEntries( const std::vector<FILE_CHECKLIST_ENTRY>& aEntries )
30{
31 m_sections.clear();
32 m_rowsBySection.clear();
33
34 for( const FILE_CHECKLIST_ENTRY& entry : aEntries )
35 {
36 if( m_rowsBySection.find( entry.section ) == m_rowsBySection.end() )
37 m_sections.push_back( entry.section );
38
39 m_rowsBySection[entry.section].push_back( entry );
40 }
41}
42
43
44const std::vector<FILE_CHECKLIST_ENTRY>& FILE_CHECKLIST_DATA::Rows( const wxString& aSection ) const
45{
46 static const std::vector<FILE_CHECKLIST_ENTRY> empty;
47
48 auto it = m_rowsBySection.find( aSection );
49 return it == m_rowsBySection.end() ? empty : it->second;
50}
51
52
53void FILE_CHECKLIST_DATA::SetChecked( const wxString& aPath, bool aChecked )
54{
55 for( auto& [section, rows] : m_rowsBySection )
56 {
57 for( FILE_CHECKLIST_ENTRY& row : rows )
58 {
59 if( row.path == aPath )
60 {
61 row.checked = aChecked;
62 return;
63 }
64 }
65 }
66}
67
68
69void FILE_CHECKLIST_DATA::SetSectionChecked( const wxString& aSection, bool aChecked )
70{
71 auto it = m_rowsBySection.find( aSection );
72
73 if( it == m_rowsBySection.end() )
74 return;
75
76 for( FILE_CHECKLIST_ENTRY& row : it->second )
77 row.checked = aChecked;
78}
79
80
81bool FILE_CHECKLIST_DATA::IsSectionChecked( const wxString& aSection ) const
82{
83 auto it = m_rowsBySection.find( aSection );
84
85 if( it == m_rowsBySection.end() || it->second.empty() )
86 return false;
87
88 for( const FILE_CHECKLIST_ENTRY& row : it->second )
89 {
90 if( !row.checked )
91 return false;
92 }
93
94 return true;
95}
96
97
98std::vector<wxString> FILE_CHECKLIST_DATA::CheckedPaths() const
99{
100 std::vector<wxString> paths;
101
102 for( const wxString& section : m_sections )
103 {
104 for( const FILE_CHECKLIST_ENTRY& row : m_rowsBySection.at( section ) )
105 {
106 if( row.checked )
107 paths.push_back( row.path );
108 }
109 }
110
111 return paths;
112}
113
114
115// Addressing node for the data view. The row itself lives in FILE_CHECKLIST_DATA, so a
116// node only says which section it belongs to and, for a file row, its index within that
117// section. Indices stay valid because the nodes are rebuilt whenever the rows are.
119{
120 bool isSection = false;
121 wxString section;
122 size_t index = 0;
124 std::vector<std::unique_ptr<CHECKLIST_NODE>> children;
125};
126
127
128class FILE_CHECKLIST_MODEL : public wxDataViewModel
129{
130public:
138
139 void SetEntries( const std::vector<FILE_CHECKLIST_ENTRY>& aEntries )
140 {
141 m_data.SetEntries( aEntries );
142 rebuildNodes();
143 Cleared();
144 }
145
146 void SetAllChecked( bool aChecked )
147 {
148 // Update every row before notifying listeners.
149 for( const std::unique_ptr<CHECKLIST_NODE>& section : m_sectionNodes )
150 m_data.SetSectionChecked( section->section, aChecked );
151
152 for( const std::unique_ptr<CHECKLIST_NODE>& section : m_sectionNodes )
153 {
154 for( const std::unique_ptr<CHECKLIST_NODE>& child : section->children )
155 ValueChanged( wxDataViewItem( child.get() ), COL_CHECK );
156
157 ValueChanged( wxDataViewItem( section.get() ), COL_CHECK );
158 }
159 }
160
161 std::vector<wxString> CheckedPaths() const { return m_data.CheckedPaths(); }
162
163 wxString PathFor( const wxDataViewItem& aItem ) const
164 {
165 CHECKLIST_NODE* node = toNode( aItem );
166
167 if( !node || node->isSection )
168 return wxEmptyString;
169
170 return m_data.Rows( node->section )[node->index].path;
171 }
172
173 void GetValue( wxVariant& aVariant, const wxDataViewItem& aItem, unsigned int aCol ) const override
174 {
175 CHECKLIST_NODE* node = toNode( aItem );
176
177 if( !node )
178 return;
179
180 if( node->isSection )
181 {
182 switch( aCol )
183 {
184 case COL_CHECK: aVariant = m_data.IsSectionChecked( node->section ); break;
185 case COL_NAME:
186 aVariant << wxDataViewIconText(
187 wxString::Format( wxT( "%s (%zu)" ), node->section, m_data.Rows( node->section ).size() ) );
188 break;
189 default: aVariant = wxString(); break;
190 }
191 }
192 else
193 {
194 const FILE_CHECKLIST_ENTRY& row = m_data.Rows( node->section )[node->index];
195
196 switch( aCol )
197 {
198 case COL_CHECK: aVariant = row.checked; break;
199 case COL_NAME: aVariant << wxDataViewIconText( row.path, KiBitmapBundle( row.icon ) ); break;
200 default: aVariant = row.status; break;
201 }
202 }
203 }
204
205 bool GetAttr( const wxDataViewItem& aItem, unsigned int aCol, wxDataViewItemAttr& aAttr ) const override
206 {
207 CHECKLIST_NODE* node = toNode( aItem );
208
209 if( !node || node->isSection || aCol != COL_STATUS )
210 return false;
211
212 const FILE_CHECKLIST_ENTRY& row = m_data.Rows( node->section )[node->index];
213
214 if( !row.statusColour.IsOk() )
215 return false;
216
217 aAttr.SetColour( row.statusColour );
218 return true;
219 }
220
221 bool SetValue( const wxVariant& aVariant, const wxDataViewItem& aItem, unsigned int aCol ) override
222 {
223 CHECKLIST_NODE* node = toNode( aItem );
224
225 if( !node || aCol != COL_CHECK )
226 return false;
227
228 bool checked = aVariant.GetBool();
229
230 if( node->isSection )
231 {
232 m_data.SetSectionChecked( node->section, checked );
233
234 for( const std::unique_ptr<CHECKLIST_NODE>& child : node->children )
235 ValueChanged( wxDataViewItem( child.get() ), COL_CHECK );
236 }
237 else
238 {
239 m_data.SetChecked( m_data.Rows( node->section )[node->index].path, checked );
240
241 if( node->parent )
242 ValueChanged( wxDataViewItem( node->parent ), COL_CHECK );
243 }
244
245 return true;
246 }
247
248 wxDataViewItem GetParent( const wxDataViewItem& aItem ) const override
249 {
250 CHECKLIST_NODE* node = toNode( aItem );
251 return ( node && node->parent ) ? wxDataViewItem( node->parent ) : wxDataViewItem();
252 }
253
254 bool IsContainer( const wxDataViewItem& aItem ) const override
255 {
256 CHECKLIST_NODE* node = toNode( aItem );
257 return !node || node->isSection;
258 }
259
260 // Section rows carry a name and a count in the file column. Without this the base class
261 // reports that a container has a value only in column 0, and the section label is never
262 // drawn, leaving the header as a bare checkbox.
263 bool HasContainerColumns( const wxDataViewItem& aItem ) const override { return true; }
264
265 unsigned int GetChildren( const wxDataViewItem& aItem, wxDataViewItemArray& aChildren ) const override
266 {
267 if( !aItem.IsOk() )
268 {
269 for( const std::unique_ptr<CHECKLIST_NODE>& section : m_sectionNodes )
270 aChildren.Add( wxDataViewItem( section.get() ) );
271
272 return aChildren.GetCount();
273 }
274
275 CHECKLIST_NODE* node = toNode( aItem );
276
277 if( node && node->isSection )
278 {
279 for( const std::unique_ptr<CHECKLIST_NODE>& child : node->children )
280 aChildren.Add( wxDataViewItem( child.get() ) );
281 }
282
283 return aChildren.GetCount();
284 }
285
286private:
287 CHECKLIST_NODE* toNode( const wxDataViewItem& aItem ) const
288 {
289 return static_cast<CHECKLIST_NODE*>( aItem.GetID() );
290 }
291
293 {
294 m_sectionNodes.clear();
295
296 for( const wxString& section : m_data.Sections() )
297 {
298 auto sectionNode = std::make_unique<CHECKLIST_NODE>();
299 sectionNode->isSection = true;
300 sectionNode->section = section;
301
302 for( size_t i = 0; i < m_data.Rows( section ).size(); ++i )
303 {
304 auto fileNode = std::make_unique<CHECKLIST_NODE>();
305 fileNode->section = section;
306 fileNode->index = i;
307 fileNode->parent = sectionNode.get();
308 sectionNode->children.push_back( std::move( fileNode ) );
309 }
310
311 m_sectionNodes.push_back( std::move( sectionNode ) );
312 }
313 }
314
316 std::vector<std::unique_ptr<CHECKLIST_NODE>> m_sectionNodes;
317};
318
319
320FILE_CHECKLIST_CTRL::FILE_CHECKLIST_CTRL( wxWindow* aParent, wxWindowID aId ) :
321 WX_DATAVIEWCTRL( aParent, aId, wxDefaultPosition, wxDefaultSize, wxDV_ROW_LINES ),
323{
324 AssociateModel( m_model.get() );
325
326 AppendToggleColumn( wxEmptyString, FILE_CHECKLIST_MODEL::COL_CHECK, wxDATAVIEW_CELL_ACTIVATABLE,
327 wxCOL_WIDTH_AUTOSIZE );
328 AppendIconTextColumn( _( "File" ), FILE_CHECKLIST_MODEL::COL_NAME, wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE );
329 AppendTextColumn( _( "Status" ), FILE_CHECKLIST_MODEL::COL_STATUS, wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE );
330}
331
332
333// Out of line because wxObjectDataPtr needs the complete model type to release it, and the
334// header only forward declares it.
338
339
340void FILE_CHECKLIST_CTRL::SetEntries( const std::vector<FILE_CHECKLIST_ENTRY>& aEntries )
341{
342 m_model->SetEntries( aEntries );
343 ExpandAll();
344}
345
346
348{
349 m_model->SetAllChecked( aChecked );
350}
351
352
353std::vector<wxString> FILE_CHECKLIST_CTRL::GetCheckedPaths() const
354{
355 return m_model->CheckedPaths();
356}
357
358
359wxString FILE_CHECKLIST_CTRL::GetItemPath( const wxDataViewItem& aItem ) const
360{
361 return m_model->PathFor( aItem );
362}
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:106
FILE_CHECKLIST_CTRL(wxWindow *aParent, wxWindowID aId=wxID_ANY)
void SetAllChecked(bool aChecked)
std::vector< wxString > GetCheckedPaths() const
wxString GetItemPath(const wxDataViewItem &aItem) const
Path of the file row for aItem, or empty for a section header or invalid item.
wxObjectDataPtr< FILE_CHECKLIST_MODEL > m_model
void SetEntries(const std::vector< FILE_CHECKLIST_ENTRY > &aEntries)
The rows and tick states behind FILE_CHECKLIST_CTRL.
std::vector< wxString > CheckedPaths() const
Paths of every ticked row, in the order they were added.
std::vector< wxString > m_sections
void SetEntries(const std::vector< FILE_CHECKLIST_ENTRY > &aEntries)
void SetSectionChecked(const wxString &aSection, bool aChecked)
std::map< wxString, std::vector< FILE_CHECKLIST_ENTRY > > m_rowsBySection
void SetChecked(const wxString &aPath, bool aChecked)
bool IsSectionChecked(const wxString &aSection) const
True when the section has rows and every one of them is ticked.
const std::vector< FILE_CHECKLIST_ENTRY > & Rows(const wxString &aSection) const
Rows under a section, in the order they were added. Empty for an unknown section.
bool IsContainer(const wxDataViewItem &aItem) const override
wxDataViewItem GetParent(const wxDataViewItem &aItem) const override
wxString PathFor(const wxDataViewItem &aItem) const
bool SetValue(const wxVariant &aVariant, const wxDataViewItem &aItem, unsigned int aCol) override
FILE_CHECKLIST_DATA m_data
std::vector< std::unique_ptr< CHECKLIST_NODE > > m_sectionNodes
std::vector< wxString > CheckedPaths() const
bool GetAttr(const wxDataViewItem &aItem, unsigned int aCol, wxDataViewItemAttr &aAttr) const override
void SetAllChecked(bool aChecked)
void SetEntries(const std::vector< FILE_CHECKLIST_ENTRY > &aEntries)
CHECKLIST_NODE * toNode(const wxDataViewItem &aItem) const
bool HasContainerColumns(const wxDataViewItem &aItem) const override
unsigned int GetChildren(const wxDataViewItem &aItem, wxDataViewItemArray &aChildren) const override
void GetValue(wxVariant &aVariant, const wxDataViewItem &aItem, unsigned int aCol) const override
Extension of the wxDataViewCtrl to include some helper functions for working with items.
bool AssociateModel(wxDataViewModel *aModel) override
Install a WX_ENSURE_VISIBLE_CANCELLER on aModel so deferred scrolls are dropped automatically when it...
static bool empty(const wxTextEntryBase *aCtrl)
#define _(s)
std::vector< std::unique_ptr< CHECKLIST_NODE > > children
CHECKLIST_NODE * parent
One file row in a FILE_CHECKLIST_CTRL.
BITMAPS icon
wxString path
Row identity, and what the row shows.
wxColour statusColour
Text colour for the status, or the default when not set.
wxString status
Free text, e.g. "Modified".
bool checked
std::vector< DATABASE_CONNECTION::ROW > rows