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_editSettingsAttr = new wxGridCellAttr;
59 m_editSettingsAttr->SetReadOnly(); // not really; we delegate interactivity to GRID_TRICKS
60 m_editSettingsAttr->SetAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
61
62 m_openTableAttr = new wxGridCellAttr;
64 m_openTableAttr->SetReadOnly(); // not really; we delegate interactivity to GRID_TRICKS
65 m_openTableAttr->SetAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
66}
67
68
70{
71 m_uriEditor->DecRef();
72 m_typesEditor->DecRef();
73 m_boolAttr->DecRef();
74 m_warningAttr->DecRef();
75 m_editSettingsAttr->DecRef();
76 m_openTableAttr->DecRef();
77}
78
79
80wxString LIB_TABLE_GRID_DATA_MODEL::GetValue( int aRow, int aCol )
81{
82 wxCHECK( aRow >= 0, wxEmptyString );
83 size_t row = static_cast<size_t>( aRow );
84
85 if( row < size() )
86 {
87 const LIBRARY_TABLE_ROW& r = at( row );
88
89 switch( aCol )
90 {
91 case COL_NICKNAME: return UnescapeString( r.Nickname() );
92 case COL_URI: return r.URI();
93 case COL_TYPE: return r.Type();
94 case COL_OPTIONS: return r.Options();
95 case COL_DESCR: return r.Description();
96 case COL_ENABLED: return r.Disabled() ? wxT( "0" ) : wxT( "1" );
97 case COL_VISIBLE: return r.Hidden() ? wxT( "0" ) : wxT( "1" );
98
99 case COL_STATUS:
100 if( !r.IsOk() )
101 return r.ErrorDescription();
102
103 if( std::optional<LIBRARY_ERROR> error = m_adapter->LibraryError( r.Nickname() ) )
104 return error->message;
105
106 if( m_adapter->SupportsConfigurationDialog( r.Nickname() ) )
107 return _( "Edit settings" );
109 return _( "Open library table" );
110
111 return wxEmptyString;
112
113 default:
114 return wxEmptyString;
115 }
116 }
117
118 return wxEmptyString;
119}
120
121
122wxGridCellAttr* LIB_TABLE_GRID_DATA_MODEL::GetAttr( int aRow, int aCol, wxGridCellAttr::wxAttrKind aKind )
123{
124 LIBRARY_TABLE_ROW& tableRow = at( aRow );
125
126 switch( aCol )
127 {
128 case COL_URI:
129 m_uriEditor->IncRef();
130 return enhanceAttr( m_uriEditor, aRow, aCol, aKind );
131
132 case COL_TYPE:
133 m_typesEditor->IncRef();
134 return enhanceAttr( m_typesEditor, aRow, aCol, aKind );
135
136 case COL_ENABLED:
137 case COL_VISIBLE:
138 m_boolAttr->IncRef();
139 return enhanceAttr( m_boolAttr, aRow, aCol, aKind );
140
141 case COL_STATUS:
142 if( !tableRow.IsOk() )
143 {
144 m_warningAttr->IncRef();
145 return enhanceAttr( m_warningAttr, aRow, aCol, aKind );
146 }
147
148 if( std::optional<LIBRARY_ERROR> error = m_adapter->LibraryError( tableRow.Nickname() ) )
149 {
150 m_warningAttr->IncRef();
151 return enhanceAttr( m_warningAttr, aRow, aCol, aKind );
152 }
153
154 if( m_adapter->SupportsConfigurationDialog( tableRow.Nickname() ) )
155 {
156 m_editSettingsAttr->IncRef();
157 return enhanceAttr( m_editSettingsAttr, aRow, aCol, aKind );
158 }
159 else if( tableRow.Type() == LIBRARY_TABLE_ROW::TABLE_TYPE_NAME )
160 {
161 m_openTableAttr->IncRef();
162 return enhanceAttr( m_openTableAttr, aRow, aCol, aKind );
163 }
164
165 return enhanceAttr( nullptr, aRow, aCol, aKind );
166
167 case COL_NICKNAME:
168 case COL_OPTIONS:
169 case COL_DESCR:
170 default:
171 return enhanceAttr( nullptr, aRow, aCol, aKind );
172 }
173}
174
175
176bool LIB_TABLE_GRID_DATA_MODEL::CanGetValueAs( int aRow, int aCol, const wxString& aTypeName )
177{
178 if( aRow < static_cast<int>( size() ) )
179 {
180 switch( aCol )
181 {
182 case COL_ENABLED:
183 case COL_VISIBLE:
184 return aTypeName == wxGRID_VALUE_BOOL;
185
186 default:
187 return aTypeName == wxGRID_VALUE_STRING;
188 }
189 }
190
191 return false;
192}
193
194
196{
197 wxCHECK( aRow >= 0, false );
198 size_t row = static_cast<size_t>( aRow );
199
200 if( row < size() && aCol == COL_ENABLED )
201 return !at( row ).Disabled();
202 else if( row < size() && aCol == COL_VISIBLE )
203 return !at( row ).Hidden();
204 else
205 return false;
206}
207
208
209void LIB_TABLE_GRID_DATA_MODEL::SetValue( int aRow, int aCol, const wxString& aValue )
210{
211 wxCHECK( aRow >= 0, /* void */ );
212 size_t row = static_cast<size_t>( aRow );
213
214 if( row < size() )
215 {
216 LIBRARY_TABLE_ROW& r = at( row );
217
218 switch( aCol )
219 {
220 case COL_NICKNAME: r.SetNickname( EscapeString( aValue, CTX_LIBID ) ); break;
221 case COL_URI: r.SetURI( aValue ); break;
222 case COL_TYPE: r.SetType( aValue ); break;
223 case COL_OPTIONS: r.SetOptions( aValue ); break;
224 case COL_DESCR: r.SetDescription( aValue ); break;
225 case COL_ENABLED: r.SetDisabled( aValue == wxT( "0" ) ); break;
226 case COL_VISIBLE: r.SetHidden( aValue == wxT( "0" ) ); break;
227 case COL_STATUS: break;
228 }
229 }
230}
231
232void LIB_TABLE_GRID_DATA_MODEL::SetValueAsBool( int aRow, int aCol, bool aValue )
233{
234 wxCHECK( aRow >= 0, /* void */ );
235 size_t row = static_cast<size_t>( aRow );
236
237 if( row < size() && aCol == COL_ENABLED )
238 at( row ).SetDisabled( !aValue );
239 else if( row < size() && aCol == COL_VISIBLE )
240 at( row ).SetHidden( !aValue );
241}
242
243
244bool LIB_TABLE_GRID_DATA_MODEL::InsertRows( size_t aPos, size_t aNumRows )
245{
246 if( aPos < size() )
247 {
248 for( size_t i = 0; i < aNumRows; i++ )
249 insert( begin() + i, makeNewRow() );
250
251 // use the (wxGridStringTable) source Luke.
252 if( GetView() )
253 {
254 wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_INSERTED, aPos, aNumRows );
255 GetView()->ProcessTableMessage( msg );
256 }
257
258 return true;
259 }
260
261 return false;
262}
263
264
266{
267 // do not modify aNumRows, original value needed for wxGridTableMessage below
268 for( int i = aNumRows; i; --i )
270
271 if( GetView() )
272 {
273 wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, aNumRows );
274 GetView()->ProcessTableMessage( msg );
275 }
276
277 return true;
278}
279
280
281bool LIB_TABLE_GRID_DATA_MODEL::DeleteRows( size_t aPos, size_t aNumRows )
282{
283 // aPos may be a large positive, e.g. size_t(-1), and the sum of
284 // aPos+aNumRows may wrap here, so both ends of the range are tested.
285 if( aPos < size() && aPos + aNumRows <= size() )
286 {
287 LIBRARY_TABLE_ROWS_ITER start = begin() + aPos;
288 erase( start, start + aNumRows );
289
290 if( GetView() )
291 {
292 wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, aPos, aNumRows );
293 GetView()->ProcessTableMessage( msg );
294 }
295
296 return true;
297 }
298
299 return false;
300}
301
302
304{
305 switch( aCol )
306 {
307 case COL_NICKNAME: return _( "Nickname" );
308 case COL_URI: return _( "Library Path" );
309
310 // keep this "Library Format" text fairly long so column is sized wide enough
311 case COL_TYPE: return _( "Library Format" );
312 case COL_OPTIONS: return _( "Options" );
313 case COL_DESCR: return _( "Description" );
314 case COL_ENABLED: return _( "Enable" );
315 case COL_VISIBLE: return _( "Show" );
316 case COL_STATUS: return wxEmptyString;
317
318 default: return wxEmptyString;
319 }
320}
321
322
323bool LIB_TABLE_GRID_DATA_MODEL::ContainsNickname( const wxString& aNickname )
324{
325 for( size_t i = 0; i < size(); ++i )
326 {
327 LIBRARY_TABLE_ROW& row = at( i );
328
329 if( row.Nickname() == aNickname )
330 return true;
331 }
332 return false;
333}
334
335
337{
338 return m_table.Rows().at( aIndex );
339}
340
341
343{
344 return m_table.Rows().size();
345}
346
347
352
353
358
359
361{
362 return m_table.Rows().insert( aIterator, aRow );
363}
364
365
367{
368 m_table.Rows().push_back( aRow );
369}
370
371
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:46
#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