KiCad PCB EDA Suite
Loading...
Searching...
No Matches
fields_data_model.h
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
20#pragma once
21
22#include <sch_reference_list.h>
23#include <wx/grid.h>
24#include <widgets/wx_grid.h>
25
26
27struct BOM_FIELD;
28struct BOM_PRESET;
29struct BOM_FMT_PRESET;
30class SCH_SYMBOL;
31
32
34{
37 wxString reference;
38 wxString caseFoldedKey;
39 std::vector<std::pair<wxString, wxString>> variants;
40};
41
42
43std::vector<FIELD_CASE_CONFLICT> DetectFieldCaseConflicts( const SCH_REFERENCE_LIST& aSymbols );
44
45
46// Columns for the View Fields grid
47#define DISPLAY_NAME_COLUMN 0 // The field name in the data model (translated)
48#define LABEL_COLUMN 1 // The field name's label for exporting (CSV, etc.)
49#define SHOW_FIELD_COLUMN 2
50#define GROUP_BY_COLUMN 3
51#define VIEW_FIELDS_COL_COUNT 4
52
53
54// Data model for the list of fields to view (and to group-by) for the Symbol Fields Table
56{
57public:
59 m_forBOM( aForBOM )
60 {}
61
62 ~VIEW_CONTROLS_GRID_DATA_MODEL() override = default;
63
64 int GetNumberRows() override { return (int) m_fields.size(); }
65 int GetNumberCols() override { return VIEW_FIELDS_COL_COUNT; }
66
67 wxString GetColLabelValue( int aCol ) override;
68
69 bool IsEmptyCell( int aRow, int aCol ) override
70 {
71 return false; // don't allow adjacent cell overflow, even if we are actually empty
72 }
73
74 bool CanGetValueAs( int aRow, int aCol, const wxString& aTypeName ) override
75 {
76 switch( aCol )
77 {
79 case LABEL_COLUMN: return aTypeName == wxGRID_VALUE_STRING;
80
82 case GROUP_BY_COLUMN: return aTypeName == wxGRID_VALUE_BOOL;
83
84 default: wxFAIL; return false;
85 }
86 }
87
88 bool CanSetValueAs( int aRow, int aCol, const wxString& aTypeName ) override
89 {
90 return CanGetValueAs( aRow, aCol, aTypeName );
91 }
92
93 wxString GetValue( int aRow, int aCol ) override;
94 bool GetValueAsBool( int aRow, int aCol ) override;
95
96 void SetValue( int aRow, int aCol, const wxString& aValue ) override;
97 void SetValueAsBool( int aRow, int aCol, bool aValue ) override;
98
99 void AppendRow( const wxString& aFieldName, const wxString& aBOMName, bool aShow, bool aGroupBy );
100 void DeleteRow( int aRow );
101
102 wxString GetCanonicalFieldName( int aRow );
103 void SetCanonicalFieldName( int aRow, const wxString& aName );
104
105protected:
107 std::vector<BOM_FIELD> m_fields;
108};
109
110
112{
113 DATA_MODEL_ROW( const SCH_REFERENCE& aFirstReference, GROUP_TYPE aType )
114 {
115 m_ItemNumber = 0;
116 m_Refs.push_back( aFirstReference );
117 m_Flag = aType;
118 }
119
122 std::vector<SCH_REFERENCE> m_Refs;
123};
124
125
127{
128 wxString m_fieldName;
129 wxString m_label;
131 bool m_show;
133};
134
135
137{
138public:
145
146 FIELDS_EDITOR_GRID_DATA_MODEL( const SCH_REFERENCE_LIST& aSymbolsList, wxGridCellAttr* aURLEditor ) :
147 m_symbolsList( aSymbolsList ),
148 m_edited( false ),
149 m_sortColumn( 0 ),
150 m_sortAscending( false ),
152 m_groupingEnabled( false ),
153 m_excludeDNP( false ),
154 m_includeExcluded( false ),
155 m_rebuildsEnabled( true ),
156 m_urlEditor( aURLEditor ),
157 m_textVarRenderer( nullptr )
158 {
159 m_symbolsList.SplitReferences();
160 }
161
163 {
164 wxSafeDecRef( m_urlEditor );
165 wxSafeDecRef( m_textVarRenderer );
166 }
167
168 static const wxString QUANTITY_VARIABLE;
169 static const wxString ITEM_NUMBER_VARIABLE;
170
171 void AddColumn( const wxString& aFieldName, const wxString& aLabel, bool aAddedByUser,
172 const wxString& aVariantName );
173 void RemoveColumn( int aCol );
174 void RenameColumn( int aCol, const wxString& newName );
175
176 void MoveColumn( int aCol, int aNewPos )
177 {
178 wxCHECK_RET( aCol >= 0 && aCol < static_cast<int>( m_cols.size() ), "Invalid Column Number" );
179
180 if( aCol == aNewPos )
181 {
182 return;
183 }
184 else if( aCol < aNewPos )
185 {
186 std::rotate( std::begin( m_cols ) + aCol, std::begin( m_cols ) + aCol + 1,
187 std::begin( m_cols ) + aNewPos + 1 );
188 }
189 else
190 {
191 std::rotate( std::begin( m_cols ) + aNewPos, std::begin( m_cols ) + aCol,
192 std::begin( m_cols ) + aCol + 1 );
193 }
194 }
195
196 int GetNumberRows() override { return (int) m_rows.size(); }
197 int GetNumberCols() override { return (int) m_cols.size(); }
198
199 void SetColLabelValue( int aCol, const wxString& aLabel ) override
200 {
201 wxCHECK_RET( aCol >= 0 && aCol < static_cast<int>( m_cols.size() ), "Invalid Column Number" );
202 m_cols[aCol].m_label = aLabel;
203 }
204
205 wxString GetColLabelValue( int aCol ) override
206 {
207 wxCHECK( aCol >= 0 && aCol < static_cast<int>( m_cols.size() ), wxString() );
208 return m_cols[aCol].m_label;
209 }
210
211 wxString GetColFieldName( int aCol )
212 {
213 wxCHECK( aCol >= 0 && aCol < static_cast<int>( m_cols.size() ), wxString() );
214 return m_cols[aCol].m_fieldName;
215 }
216
217 int GetFieldNameCol( const wxString& aFieldName ) const;
218
219 std::vector<BOM_FIELD> GetFieldsOrdered();
220 void SetFieldsOrder( const std::vector<wxString>& aNewOrder );
221
222 bool IsEmptyCell( int aRow, int aCol ) override
223 {
224 return false; // don't allow adjacent cell overflow, even if we are actually empty
225 }
226
227 wxString GetValue( int aRow, int aCol ) override;
228 wxString GetResolvedValue( int aRow, int aCol );
229 wxGridCellAttr* GetAttr( int aRow, int aCol, wxGridCellAttr::wxAttrKind aKind ) override;
230
231 wxString GetValue( const DATA_MODEL_ROW& group, int aCol,
232 const wxString& refDelimiter = wxT( ", " ),
233 const wxString& refRangDelimiter = wxT( "-" ),
234 bool resolveVars = false,
235 bool listMixedValues = false );
236
237 wxString GetExportValue( int aRow, int aCol, const wxString& refDelimiter,
238 const wxString& refRangeDelimiter )
239 {
240 return GetValue( m_rows[aRow], aCol, refDelimiter, refRangeDelimiter, true, true );
241 }
242
243 void SetValue( int aRow, int aCol, const wxString& aValue ) override;
244
245 GROUP_TYPE GetRowFlags( int aRow ) { return m_rows[aRow].m_Flag; }
246
247 std::vector<SCH_REFERENCE> GetRowReferences( int aRow ) const
248 {
249 wxCHECK( aRow >= 0 && aRow < (int) m_rows.size(), std::vector<SCH_REFERENCE>() );
250 return m_rows[aRow].m_Refs;
251 }
252
253 bool ColIsReference( int aCol );
254 bool ColIsValue( int aCol );
255 bool ColIsQuantity( int aCol );
256 bool ColIsItemNumber( int aCol );
257 bool ColIsAttribute( int aCol );
258
259 bool IsExpanderColumn( int aCol ) const override;
260 GROUP_TYPE GetGroupType( int aRow ) const override
261 {
262 return m_rows[aRow].m_Flag;
263 }
264
265 void SetSorting( int aCol, bool ascending )
266 {
267 wxCHECK_RET( aCol >= 0 && aCol < (int) m_cols.size(), "Invalid Column Number" );
268 m_sortColumn = aCol;
269 m_sortAscending = ascending;
270 }
271
272 int GetSortCol() { return m_sortColumn; }
273 bool GetSortAsc() { return m_sortAscending; }
274
275 // These are used to disable the RebuildRows functionality while we're generating
276 // lots of events in the UI, e.g. applying a BOM preset, that would thrash the grid.
277 void EnableRebuilds();
278 void DisableRebuilds();
279 void RebuildRows();
280
281 void ExpandRow( int aRow );
282 void CollapseRow( int aRow );
283 void ExpandCollapseRow( int aRow );
284 void CollapseForSort();
285 void ExpandAfterSort();
286
287 void ApplyData( SCH_COMMIT& aCommit, TEMPLATES& aTemplateFieldnames, const wxString& aVariantName );
288
289 bool IsEdited() { return m_edited; }
290
291 int GetDataWidth( int aCol );
292
293 void SetFilter( const wxString& aFilter ) { m_filter = aFilter; }
294 const wxString& GetFilter() { return m_filter; }
295
296 void SetScope( SCOPE aScope ) { m_scope = aScope; }
297 SCOPE GetScope() { return m_scope; }
298
299 void SetPath( const SCH_SHEET_PATH& aPath ) { m_path = aPath; }
300 const SCH_SHEET_PATH& GetPath() { return m_path; }
301
304
305 /* These contradictorily named functions force including symbols that
306 * have the Exclude from BOM check box ticked. This is needed so we can view
307 * these parts in the symbol fields table dialog, while also excluding from the
308 * BOM export */
309 void SetIncludeExcludedFromBOM( bool include ) { m_includeExcluded = include; }
311
312 void SetExcludeDNP( bool exclude ) { m_excludeDNP = exclude; }
313 bool GetExcludeDNP() { return m_excludeDNP; }
314
315 void SetGroupColumn( int aCol, bool group )
316 {
317 wxCHECK_RET( aCol >= 0 && aCol < (int) m_cols.size(), "Invalid Column Number" );
318 m_cols[aCol].m_group = group;
319 }
320
321 bool GetGroupColumn( int aCol )
322 {
323 wxCHECK_MSG( aCol >= 0 && aCol < (int) m_cols.size(), false, "Invalid Column Number" );
324 return m_cols[aCol].m_group;
325 }
326
327 void SetShowColumn( int aCol, bool show )
328 {
329 wxCHECK_RET( aCol >= 0 && aCol < (int) m_cols.size(), "Invalid Column Number" );
330 m_cols[aCol].m_show = show;
331 }
332
333 bool GetShowColumn( int aCol )
334 {
335 wxCHECK_MSG( aCol >= 0 && aCol < (int) m_cols.size(), false, "Invalid Column Number" );
336 return m_cols[aCol].m_show;
337 }
338
339 void ApplyBomPreset( const BOM_PRESET& preset, const wxString& aVariantName );
341 wxString Export( const BOM_FMT_PRESET& settings );
342
343 void AddReferences( const SCH_REFERENCE_LIST& aRefs );
344 void RemoveReferences( const SCH_REFERENCE_LIST& aRefs );
345 void RemoveSymbol( const SCH_SYMBOL& aSymbol );
346 void UpdateReferences( const SCH_REFERENCE_LIST& aRefs, const wxString& aVariantName );
347
348 bool DeleteRows( size_t aPosition = 0, size_t aNumRows = 1 ) override;
349
351
360 void SetCurrentVariant( const wxString& aVariantName ) { m_currentVariant = aVariantName; }
361 const wxString& GetCurrentVariant() const { return m_currentVariant; }
362
363 void SetVariantNames( const std::vector<wxString>& aVariantNames ) { m_variantNames = aVariantNames; }
364 const std::vector<wxString>& GetVariantNames() const { return m_variantNames; }
365
366private:
367 static bool cmp( const DATA_MODEL_ROW& lhGroup, const DATA_MODEL_ROW& rhGroup,
368 FIELDS_EDITOR_GRID_DATA_MODEL* dataModel, int sortCol, bool ascending );
369
370 bool unitMatch( const SCH_REFERENCE& lhRef, const SCH_REFERENCE& rhRef );
371 bool groupMatch( const SCH_REFERENCE& lhRef, const SCH_REFERENCE& rhRef );
372
373 // Helper functions to deal with translating wxGrid values to and from
374 // named field values like ${DNP}
375 bool isAttribute( const wxString& aFieldName );
376 wxString getAttributeValue( const SCH_REFERENCE& aRef, const wxString& aAttributeName,
377 const wxString& aVariantNames );
378
388 wxString getDefaultFieldValue( const SCH_REFERENCE& aRef, const wxString& aFieldName );
389
400 bool setAttributeValue( SCH_REFERENCE& aRef, const wxString& aAttributeName, const wxString& aValue,
401 const wxString& aVariantName = wxEmptyString );
402
403 /* Helper function to get the resolved field value.
404 * Handles symbols that are missing fields that would have a variable
405 * in their value because their name is the same as a variable.
406 * Example: BOM template provides ${DNP} as a field, but they symbol doesn't have the field. */
407 wxString getFieldShownText( const SCH_REFERENCE& aRef, const wxString& aFieldName );
408
409 void Sort();
410
411 void updateDataStoreSymbolField( const SCH_REFERENCE& aSymbolRef, const wxString& aFieldName,
412 const wxString& aVariantName );
413
414protected:
425 wxString m_filter;
432 wxGridCellAttr* m_urlEditor;
433 wxGridCellRenderer* m_textVarRenderer;
435 std::vector<wxString> m_variantNames;
436
437 std::vector<DATA_MODEL_COL> m_cols;
438 std::vector<DATA_MODEL_ROW> m_rows;
439
440 // Data store
441 // The data model is fundamentally m_componentRefs X m_fieldNames.
442 // A map of compID : fieldSet, where fieldSet is a map of fieldName : fieldValue
443 // The compID is now the full KIID_PATH (sheet path + symbol UUID) as a string
444 std::map<KIID_PATH, std::map<wxString, wxString>> m_dataStore;
445};
int GetFieldNameCol(const wxString &aFieldName) const
void AddColumn(const wxString &aFieldName, const wxString &aLabel, bool aAddedByUser, const wxString &aVariantName)
std::vector< DATA_MODEL_ROW > m_rows
void SetFieldsOrder(const std::vector< wxString > &aNewOrder)
std::vector< SCH_REFERENCE > GetRowReferences(int aRow) const
SCH_REFERENCE_LIST m_symbolsList
The flattened by hierarchy list of symbols.
wxGridCellRenderer * m_textVarRenderer
Renderer for cells with text variable references.
void UpdateReferences(const SCH_REFERENCE_LIST &aRefs, const wxString &aVariantName)
wxString m_currentVariant
Current variant name for highlighting.
bool DeleteRows(size_t aPosition=0, size_t aNumRows=1) override
bool groupMatch(const SCH_REFERENCE &lhRef, const SCH_REFERENCE &rhRef)
const SCH_SHEET_PATH & GetPath()
wxString GetColLabelValue(int aCol) override
void updateDataStoreSymbolField(const SCH_REFERENCE &aSymbolRef, const wxString &aFieldName, const wxString &aVariantName)
bool unitMatch(const SCH_REFERENCE &lhRef, const SCH_REFERENCE &rhRef)
wxString getAttributeValue(const SCH_REFERENCE &aRef, const wxString &aAttributeName, const wxString &aVariantNames)
wxString GetExportValue(int aRow, int aCol, const wxString &refDelimiter, const wxString &refRangeDelimiter)
wxString getDefaultFieldValue(const SCH_REFERENCE &aRef, const wxString &aFieldName)
Get the default (non-variant) value for a field.
void SetPath(const SCH_SHEET_PATH &aPath)
wxString getFieldShownText(const SCH_REFERENCE &aRef, const wxString &aFieldName)
bool IsEmptyCell(int aRow, int aCol) override
wxString GetResolvedValue(int aRow, int aCol)
GROUP_TYPE GetGroupType(int aRow) const override
void RenameColumn(int aCol, const wxString &newName)
FIELDS_EDITOR_GRID_DATA_MODEL(const SCH_REFERENCE_LIST &aSymbolsList, wxGridCellAttr *aURLEditor)
bool IsExpanderColumn(int aCol) const override
wxString Export(const BOM_FMT_PRESET &settings)
std::vector< wxString > m_variantNames
Variant names for multi-variant DNP filtering.
std::vector< DATA_MODEL_COL > m_cols
void SetSorting(int aCol, bool ascending)
wxGridCellAttr * GetAttr(int aRow, int aCol, wxGridCellAttr::wxAttrKind aKind) override
void SetFilter(const wxString &aFilter)
bool setAttributeValue(SCH_REFERENCE &aRef, const wxString &aAttributeName, const wxString &aValue, const wxString &aVariantName=wxEmptyString)
Set the attribute value.
const wxString & GetCurrentVariant() const
static bool cmp(const DATA_MODEL_ROW &lhGroup, const DATA_MODEL_ROW &rhGroup, FIELDS_EDITOR_GRID_DATA_MODEL *dataModel, int sortCol, bool ascending)
void ApplyBomPreset(const BOM_PRESET &preset, const wxString &aVariantName)
static const wxString ITEM_NUMBER_VARIABLE
void SetIncludeExcludedFromBOM(bool include)
bool isAttribute(const wxString &aFieldName)
wxString GetValue(int aRow, int aCol) override
const SCH_REFERENCE_LIST & GetReferenceList() const
void SetVariantNames(const std::vector< wxString > &aVariantNames)
const std::vector< wxString > & GetVariantNames() const
static const wxString QUANTITY_VARIABLE
void SetGroupColumn(int aCol, bool group)
void RemoveSymbol(const SCH_SYMBOL &aSymbol)
std::vector< BOM_FIELD > GetFieldsOrdered()
void SetValue(int aRow, int aCol, const wxString &aValue) override
std::map< KIID_PATH, std::map< wxString, wxString > > m_dataStore
void ApplyData(SCH_COMMIT &aCommit, TEMPLATES &aTemplateFieldnames, const wxString &aVariantName)
void SetColLabelValue(int aCol, const wxString &aLabel) override
void SetCurrentVariant(const wxString &aVariantName)
Set the current variant name for highlighting purposes.
void MoveColumn(int aCol, int aNewPos)
void RemoveReferences(const SCH_REFERENCE_LIST &aRefs)
void SetShowColumn(int aCol, bool show)
void AddReferences(const SCH_REFERENCE_LIST &aRefs)
Container to create a flattened list of symbols because in a complex hierarchy, a symbol can be used ...
A helper to define a symbol's reference designator in a schematic.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Schematic symbol object.
Definition sch_symbol.h:76
bool IsEmptyCell(int aRow, int aCol) override
void SetValueAsBool(int aRow, int aCol, bool aValue) override
~VIEW_CONTROLS_GRID_DATA_MODEL() override=default
wxString GetColLabelValue(int aCol) override
std::vector< BOM_FIELD > m_fields
void SetValue(int aRow, int aCol, const wxString &aValue) override
void AppendRow(const wxString &aFieldName, const wxString &aBOMName, bool aShow, bool aGroupBy)
bool CanSetValueAs(int aRow, int aCol, const wxString &aTypeName) override
bool GetValueAsBool(int aRow, int aCol) override
bool CanGetValueAs(int aRow, int aCol, const wxString &aTypeName) override
void SetCanonicalFieldName(int aRow, const wxString &aName)
wxString GetValue(int aRow, int aCol) override
#define LABEL_COLUMN
#define DISPLAY_NAME_COLUMN
std::vector< FIELD_CASE_CONFLICT > DetectFieldCaseConflicts(const SCH_REFERENCE_LIST &aSymbols)
#define GROUP_BY_COLUMN
#define SHOW_FIELD_COLUMN
#define VIEW_FIELDS_COL_COUNT
std::vector< SCH_REFERENCE > m_Refs
DATA_MODEL_ROW(const SCH_REFERENCE &aFirstReference, GROUP_TYPE aType)
SCH_SHEET_PATH sheetPath
std::vector< std::pair< wxString, wxString > > variants
enum KICOMMON_API GROUP_TYPE
Definition wx_grid.h:44