KiCad PCB EDA Suite
Loading...
Searching...
No Matches
lib_table_grid_data_model.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 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 <string_utils.h>
23#include <widgets/grid_button.h>
24#include <bitmaps.h>
26
27
29 const LIBRARY_TABLE& aTableToEdit,
31 const wxArrayString& aPluginChoices,
32 wxString* aMRUDirectory, const wxString& aProjectPath ) :
33 m_table( aTableToEdit ),
34 m_adapter( aAdapter )
35{
36 m_uriEditor = new wxGridCellAttr;
37 m_uriEditor->SetEditor( new GRID_CELL_PATH_EDITOR( aDialog, aGrid, aMRUDirectory, !aProjectPath.IsEmpty(),
38 aProjectPath,
39 [this]( WX_GRID* aCurrGrid, int aRow ) -> wxString
40 {
41 return getFileTypes( aCurrGrid, aRow );
42 } ) );
43
44 m_typesEditor = new wxGridCellAttr;
45 m_typesEditor->SetEditor( new wxGridCellChoiceEditor( aPluginChoices ) );
46
47 m_boolAttr = new wxGridCellAttr;
48 m_boolAttr->SetRenderer( new wxGridCellBoolRenderer() );
49 m_boolAttr->SetReadOnly(); // not really; we delegate interactivity to GRID_TRICKS
50 m_boolAttr->SetAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
51
52 m_warningAttr = new wxGridCellAttr;
54 m_warningAttr->SetReadOnly();
55 m_warningAttr->SetAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
56
57 m_noStatusAttr = new wxGridCellAttr;
58 m_noStatusAttr->SetReadOnly();
59 m_noStatusAttr->SetAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
60
61 m_editSettingsAttr = new wxGridCellAttr;
63 m_editSettingsAttr->SetReadOnly(); // not really; we delegate interactivity to GRID_TRICKS
64 m_editSettingsAttr->SetAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
65
66 m_openTableAttr = new wxGridCellAttr;
68 m_openTableAttr->SetReadOnly(); // not really; we delegate interactivity to GRID_TRICKS
69 m_openTableAttr->SetAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
70}
71
72
74{
75 m_uriEditor->DecRef();
76 m_typesEditor->DecRef();
77 m_boolAttr->DecRef();
78 m_warningAttr->DecRef();
79 m_noStatusAttr->DecRef();
80 m_editSettingsAttr->DecRef();
81 m_openTableAttr->DecRef();
82}
83
84
85bool LIB_TABLE_GRID_DATA_MODEL::badCoords( int aRow, int aCol )
86{
87 if( aRow < 0 || aRow >= (int) size() )
88 return true;
89
90 if( aCol < 0 || aCol >= GetNumberCols() )
91 return true;
92
93 return false;
94}
95
96
97wxString LIB_TABLE_GRID_DATA_MODEL::GetValue( int aRow, int aCol )
98{
99 if( badCoords( aRow, aCol ) )
100 return wxEmptyString;
101
102 const LIBRARY_TABLE_ROW& r = at( aRow );
103
104 switch( aCol )
105 {
106 case COL_NICKNAME: return UnescapeString( r.Nickname() );
107 case COL_URI: return r.URI();
108 case COL_TYPE: return r.Type();
109 case COL_OPTIONS: return r.Options();
110 case COL_DESCR: return r.Description();
111 case COL_ENABLED: return r.Disabled() ? wxT( "0" ) : wxT( "1" );
112 case COL_VISIBLE: return r.Hidden() ? wxT( "0" ) : wxT( "1" );
113
114 case COL_STATUS:
115 if( !r.IsOk() )
116 return r.ErrorDescription();
117
118 if( m_adapter->SupportsConfigurationDialog( r.Nickname() ) )
119 return _( "Edit settings" );
121 return _( "Open library table" );
122
123 return wxEmptyString;
124
125 default:
126 return wxEmptyString;
127 }
128}
129
130
131wxGridCellAttr* LIB_TABLE_GRID_DATA_MODEL::GetAttr( int aRow, int aCol, wxGridCellAttr::wxAttrKind aKind )
132{
133 if( badCoords( aRow, aCol ) )
134 return enhanceAttr( nullptr, aRow, aCol, aKind );
135
136 LIBRARY_TABLE_ROW& tableRow = at( aRow );
137
138 switch( aCol )
139 {
140 case COL_URI:
141 m_uriEditor->IncRef();
142 return enhanceAttr( m_uriEditor, aRow, aCol, aKind );
143
144 case COL_TYPE:
145 m_typesEditor->IncRef();
146 return enhanceAttr( m_typesEditor, aRow, aCol, aKind );
147
148 case COL_ENABLED:
149 case COL_VISIBLE:
150 m_boolAttr->IncRef();
151 return enhanceAttr( m_boolAttr, aRow, aCol, aKind );
152
153 case COL_STATUS:
154 if( !tableRow.IsOk() )
155 {
156 m_warningAttr->IncRef();
157 return enhanceAttr( m_warningAttr, aRow, aCol, aKind );
158 }
159
160 if( m_adapter->SupportsConfigurationDialog( tableRow.Nickname() ) )
161 {
162 m_editSettingsAttr->IncRef();
163 return enhanceAttr( m_editSettingsAttr, aRow, aCol, aKind );
164 }
165 else if( tableRow.Type() == LIBRARY_TABLE_ROW::TABLE_TYPE_NAME )
166 {
167 m_openTableAttr->IncRef();
168 return enhanceAttr( m_openTableAttr, aRow, aCol, aKind );
169 }
170
171 m_noStatusAttr->IncRef();
172 return enhanceAttr( m_noStatusAttr, aRow, aCol, aKind );
173
174 case COL_NICKNAME:
175 case COL_OPTIONS:
176 case COL_DESCR:
177 default:
178 return enhanceAttr( nullptr, aRow, aCol, aKind );
179 }
180}
181
182
183bool LIB_TABLE_GRID_DATA_MODEL::CanGetValueAs( int aRow, int aCol, const wxString& aTypeName )
184{
185 if( badCoords( aRow, aCol ) )
186 return false;
187
188 switch( aCol )
189 {
190 case COL_ENABLED:
191 case COL_VISIBLE:
192 return aTypeName == wxGRID_VALUE_BOOL;
193
194 default:
195 return aTypeName == wxGRID_VALUE_STRING;
196 }
197}
198
199
201{
202 if( badCoords( aRow, aCol ) )
203 return false;
204
205 if( aCol == COL_ENABLED )
206 return !at( aRow ).Disabled();
207 else if( aCol == COL_VISIBLE )
208 return !at( aRow ).Hidden();
209 else
210 return false;
211}
212
213
214void LIB_TABLE_GRID_DATA_MODEL::SetValue( int aRow, int aCol, const wxString& aValue )
215{
216 if( badCoords( aRow, aCol ) )
217 return;
218
219 LIBRARY_TABLE_ROW& lrow = at( aRow );
220
221 switch( aCol )
222 {
223 case COL_NICKNAME: lrow.SetNickname( EscapeString( aValue, CTX_LIBID ) ); break;
224 case COL_URI: lrow.SetURI( aValue ); break;
225 case COL_TYPE: lrow.SetType( aValue ); break;
226 case COL_OPTIONS: lrow.SetOptions( aValue ); break;
227 case COL_DESCR: lrow.SetDescription( aValue ); break;
228 case COL_ENABLED: lrow.SetDisabled( aValue == wxT( "0" ) ); break;
229 case COL_VISIBLE: lrow.SetHidden( aValue == wxT( "0" ) ); break;
230 case COL_STATUS: break;
231 }
232
233 if( aCol == COL_URI || aCol == COL_TYPE || aCol == COL_OPTIONS )
234 {
235 GetView()->CallAfter(
236 [this, aRow, aCol]()
237 {
238 if( badCoords( aRow, aCol ) )
239 return;
240
241 LIBRARY_TABLE_ROW& r = at( aRow );
242
243 m_adapter->CheckTableRow( r );
244
245 GetView()->RefreshBlock( aRow, COL_STATUS, aRow, COL_STATUS );
246 } );
247 }
248}
249
250
251void LIB_TABLE_GRID_DATA_MODEL::SetValueAsBool( int aRow, int aCol, bool aValue )
252{
253 if( badCoords( aRow, aCol ) )
254 return;
255
256 if( aCol == COL_ENABLED )
257 at( aRow ).SetDisabled( !aValue );
258 else if( aCol == COL_VISIBLE )
259 at( aRow ).SetHidden( !aValue );
260}
261
262
263bool LIB_TABLE_GRID_DATA_MODEL::InsertRows( size_t aPos, size_t aNumRows )
264{
265 if( aPos < size() )
266 {
267 for( size_t i = 0; i < aNumRows; i++ )
268 insert( begin() + i, makeNewRow() );
269
270 // use the (wxGridStringTable) source Luke.
271 if( GetView() )
272 {
273 wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_INSERTED, aPos, aNumRows );
274 GetView()->ProcessTableMessage( msg );
275 }
276
277 return true;
278 }
279
280 return false;
281}
282
283
285{
286 // do not modify aNumRows, original value needed for wxGridTableMessage below
287 for( int i = aNumRows; i; --i )
289
290 if( GetView() )
291 {
292 wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, aNumRows );
293 GetView()->ProcessTableMessage( msg );
294 }
295
296 return true;
297}
298
299
300bool LIB_TABLE_GRID_DATA_MODEL::DeleteRows( size_t aPos, size_t aNumRows )
301{
302 // aPos may be a large positive, e.g. size_t(-1), and the sum of
303 // aPos+aNumRows may wrap here, so both ends of the range are tested.
304 if( aPos < size() && aPos + aNumRows <= size() )
305 {
306 LIBRARY_TABLE_ROWS_ITER start = begin() + aPos;
307 erase( start, start + aNumRows );
308
309 if( GetView() )
310 {
311 wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, aPos, aNumRows );
312 GetView()->ProcessTableMessage( msg );
313 }
314
315 return true;
316 }
317
318 return false;
319}
320
321
323{
324 switch( aCol )
325 {
326 case COL_NICKNAME: return _( "Nickname" );
327 case COL_URI: return _( "Library Path" );
328
329 // keep this "Library Format" text fairly long so column is sized wide enough
330 case COL_TYPE: return _( "Library Format" );
331 case COL_OPTIONS: return _( "Options" );
332 case COL_DESCR: return _( "Description" );
333 case COL_ENABLED: return _( "Enable" );
334 case COL_VISIBLE: return _( "Show" );
335 case COL_STATUS: return wxEmptyString;
336
337 default: return wxEmptyString;
338 }
339}
340
341
342bool LIB_TABLE_GRID_DATA_MODEL::ContainsNickname( const wxString& aNickname )
343{
344 for( size_t i = 0; i < size(); ++i )
345 {
346 LIBRARY_TABLE_ROW& row = at( i );
347
348 if( row.Nickname() == aNickname )
349 return true;
350 }
351 return false;
352}
353
354
356{
357 if( !m_adapter )
358 return;
359
360 for( size_t row = 0; row < size(); ++row )
361 m_adapter->CheckTableRow( at( row ) );
362
363 if( GetView() && GetNumberRows() > 0 )
364 GetView()->RefreshBlock( 0, COL_STATUS, GetNumberRows() - 1, COL_STATUS );
365}
366
367
369{
370 return m_table.Rows().at( aIndex );
371}
372
373
375{
376 return m_table.Rows().size();
377}
378
379
384
385
390
391
393 const LIBRARY_TABLE_ROW& aRow )
394{
395 return m_table.Rows().insert( aIterator, aRow );
396}
397
398
400{
401 m_table.Rows().push_back( aRow );
402}
403
404
407{
408 return m_table.Rows().erase( aFirst, aLast );
409}
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:110
@ small_new_window
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
Definition dialog_shim.h:68
Editor for wxGrid cells that adds a file/folder browser to the grid input field.
The interface used by the classes that actually can load IO plugins for the different parts of KiCad ...
void SetOptions(const wxString &aOptions)
void SetNickname(const wxString &aNickname)
void SetType(const wxString &aType)
bool Disabled() const
const wxString & ErrorDescription() const
void SetDescription(const wxString &aDescription)
const wxString & Type() const
static const wxString TABLE_TYPE_NAME
void SetURI(const wxString &aUri)
const wxString & Description() const
void SetDisabled(bool aDisabled=true)
bool Hidden() const
void SetHidden(bool aHidden=true)
bool IsOk() const
const wxString & URI() const
const wxString & Nickname() const
const wxString & Options() const
virtual LIBRARY_TABLE_ROWS_ITER begin()
bool AppendRows(size_t aNumRows=1) override
virtual LIBRARY_TABLE_ROW & at(size_t aIndex)
bool ContainsNickname(const wxString &aNickname)
virtual LIBRARY_TABLE_ROWS_ITER erase(LIBRARY_TABLE_ROWS_ITER aFirst, LIBRARY_TABLE_ROWS_ITER aLast)
void SetValueAsBool(int aRow, int aCol, bool aValue) override
virtual LIBRARY_TABLE_ROWS_ITER insert(LIBRARY_TABLE_ROWS_ITER aIterator, const LIBRARY_TABLE_ROW &aRow)
wxString GetValue(int aRow, int aCol) override
wxString GetColLabelValue(int aCol) override
wxGridCellAttr * GetAttr(int aRow, int aCol, wxGridCellAttr::wxAttrKind aKind) override
bool CanGetValueAs(int aRow, int aCol, const wxString &aTypeName) override
bool GetValueAsBool(int aRow, int aCol) override
bool DeleteRows(size_t aPos, size_t aNumRows) override
virtual void push_back(const LIBRARY_TABLE_ROW &aRow)
bool InsertRows(size_t aPos=0, size_t aNumRows=1) override
LIBRARY_MANAGER_ADAPTER * m_adapter
Handle to the adapter for the type of table this grid represents (may be null)
void SetValue(int aRow, int aCol, const wxString &aValue) override
LIBRARY_TABLE m_table
Working copy of a table.
LIB_TABLE_GRID_DATA_MODEL(DIALOG_SHIM *aParent, WX_GRID *aGrid, const LIBRARY_TABLE &aTableToEdit, LIBRARY_MANAGER_ADAPTER *aAdapter, const wxArrayString &aPluginChoices, wxString *aMRUDirectory, const wxString &aProjectPath)
virtual LIBRARY_TABLE_ROW makeNewRow()
wxGridCellAttr * enhanceAttr(wxGridCellAttr *aInputAttr, int aRow, int aCol, wxGridCellAttr::wxAttrKind aKind)
Definition wx_grid.cpp:47
#define _(s)
std::vector< LIBRARY_TABLE_ROW >::iterator LIBRARY_TABLE_ROWS_ITER
wxString UnescapeString(const wxString &aSource)
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
@ CTX_LIBID