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;
30
31
32// Columns for the View Fields grid
33#define DISPLAY_NAME_COLUMN 0 // The field name in the data model (translated)
34#define LABEL_COLUMN 1 // The field name's label for exporting (CSV, etc.)
35#define SHOW_FIELD_COLUMN 2
36#define GROUP_BY_COLUMN 3
37#define VIEW_FIELDS_COL_COUNT 4
38
39
40// Data model for the list of fields to view (and to group-by) for the Symbol Fields Table
42{
43public:
45 m_forBOM( aForBOM )
46 {}
47
48 ~VIEW_CONTROLS_GRID_DATA_MODEL() override = default;
49
50 int GetNumberRows() override { return (int) m_fields.size(); }
51 int GetNumberCols() override { return VIEW_FIELDS_COL_COUNT; }
52
53 wxString GetColLabelValue( int aCol ) override;
54
55 bool IsEmptyCell( int aRow, int aCol ) override
56 {
57 return false; // don't allow adjacent cell overflow, even if we are actually empty
58 }
59
60 bool CanGetValueAs( int aRow, int aCol, const wxString& aTypeName ) override
61 {
62 switch( aCol )
63 {
65 case LABEL_COLUMN: return aTypeName == wxGRID_VALUE_STRING;
66
68 case GROUP_BY_COLUMN: return aTypeName == wxGRID_VALUE_BOOL;
69
70 default: wxFAIL; return false;
71 }
72 }
73
74 bool CanSetValueAs( int aRow, int aCol, const wxString& aTypeName ) override
75 {
76 return CanGetValueAs( aRow, aCol, aTypeName );
77 }
78
79 wxString GetValue( int aRow, int aCol ) override;
80 bool GetValueAsBool( int aRow, int aCol ) override;
81
82 void SetValue( int aRow, int aCol, const wxString& aValue ) override;
83 void SetValueAsBool( int aRow, int aCol, bool aValue ) override;
84
85 void AppendRow( const wxString& aFieldName, const wxString& aBOMName, bool aShow, bool aGroupBy );
86 void DeleteRow( int aRow );
87
88 wxString GetCanonicalFieldName( int aRow );
89 void SetCanonicalFieldName( int aRow, const wxString& aName );
90
91protected:
93 std::vector<BOM_FIELD> m_fields;
94};
95
96
98{
99 DATA_MODEL_ROW( const SCH_REFERENCE& aFirstReference, GROUP_TYPE aType )
100 {
101 m_ItemNumber = 0;
102 m_Refs.push_back( aFirstReference );
103 m_Flag = aType;
104 }
105
108 std::vector<SCH_REFERENCE> m_Refs;
109};
110
111
113{
114 wxString m_fieldName;
115 wxString m_label;
117 bool m_show;
119};
120
121
123{
124public:
131
132 FIELDS_EDITOR_GRID_DATA_MODEL( const SCH_REFERENCE_LIST& aSymbolsList, wxGridCellAttr* aURLEditor ) :
133 m_symbolsList( aSymbolsList ),
134 m_edited( false ),
135 m_sortColumn( 0 ),
136 m_sortAscending( false ),
138 m_groupingEnabled( false ),
139 m_excludeDNP( false ),
140 m_includeExcluded( false ),
141 m_rebuildsEnabled( true ),
142 m_urlEditor( aURLEditor ),
143 m_textVarRenderer( nullptr )
144 {
145 m_symbolsList.SplitReferences();
146 }
147
149 {
150 wxSafeDecRef( m_urlEditor );
151 wxSafeDecRef( m_textVarRenderer );
152 }
153
154 static const wxString QUANTITY_VARIABLE;
155 static const wxString ITEM_NUMBER_VARIABLE;
156
157 void AddColumn( const wxString& aFieldName, const wxString& aLabel, bool aAddedByUser,
158 const wxString& aVariantName );
159 void RemoveColumn( int aCol );
160 void RenameColumn( int aCol, const wxString& newName );
161
162 void MoveColumn( int aCol, int aNewPos )
163 {
164 wxCHECK_RET( aCol >= 0 && aCol < static_cast<int>( m_cols.size() ), "Invalid Column Number" );
165
166 if( aCol == aNewPos )
167 {
168 return;
169 }
170 else if( aCol < aNewPos )
171 {
172 std::rotate( std::begin( m_cols ) + aCol, std::begin( m_cols ) + aCol + 1,
173 std::begin( m_cols ) + aNewPos + 1 );
174 }
175 else
176 {
177 std::rotate( std::begin( m_cols ) + aNewPos, std::begin( m_cols ) + aCol,
178 std::begin( m_cols ) + aCol + 1 );
179 }
180 }
181
182 int GetNumberRows() override { return (int) m_rows.size(); }
183 int GetNumberCols() override { return (int) m_cols.size(); }
184
185 void SetColLabelValue( int aCol, const wxString& aLabel ) override
186 {
187 wxCHECK_RET( aCol >= 0 && aCol < static_cast<int>( m_cols.size() ), "Invalid Column Number" );
188 m_cols[aCol].m_label = aLabel;
189 }
190
191 wxString GetColLabelValue( int aCol ) override
192 {
193 wxCHECK( aCol >= 0 && aCol < static_cast<int>( m_cols.size() ), wxString() );
194 return m_cols[aCol].m_label;
195 }
196
197 wxString GetColFieldName( int aCol )
198 {
199 wxCHECK( aCol >= 0 && aCol < static_cast<int>( m_cols.size() ), wxString() );
200 return m_cols[aCol].m_fieldName;
201 }
202
203 int GetFieldNameCol( const wxString& aFieldName ) const;
204
205 std::vector<BOM_FIELD> GetFieldsOrdered();
206 void SetFieldsOrder( const std::vector<wxString>& aNewOrder );
207
208 bool IsEmptyCell( int aRow, int aCol ) override
209 {
210 return false; // don't allow adjacent cell overflow, even if we are actually empty
211 }
212
213 wxString GetValue( int aRow, int aCol ) override;
214 wxString GetResolvedValue( int aRow, int aCol );
215 wxGridCellAttr* GetAttr( int aRow, int aCol, wxGridCellAttr::wxAttrKind aKind ) override;
216
217 wxString GetValue( const DATA_MODEL_ROW& group, int aCol,
218 const wxString& refDelimiter = wxT( ", " ),
219 const wxString& refRangDelimiter = wxT( "-" ),
220 bool resolveVars = false,
221 bool listMixedValues = false );
222
223 wxString GetExportValue( int aRow, int aCol, const wxString& refDelimiter,
224 const wxString& refRangeDelimiter )
225 {
226 return GetValue( m_rows[aRow], aCol, refDelimiter, refRangeDelimiter, true, true );
227 }
228
229 void SetValue( int aRow, int aCol, const wxString& aValue ) override;
230
231 GROUP_TYPE GetRowFlags( int aRow ) { return m_rows[aRow].m_Flag; }
232
233 std::vector<SCH_REFERENCE> GetRowReferences( int aRow ) const
234 {
235 wxCHECK( aRow >= 0 && aRow < (int) m_rows.size(), std::vector<SCH_REFERENCE>() );
236 return m_rows[aRow].m_Refs;
237 }
238
239 bool ColIsReference( int aCol );
240 bool ColIsValue( int aCol );
241 bool ColIsQuantity( int aCol );
242 bool ColIsItemNumber( int aCol );
243 bool ColIsAttribute( int aCol );
244
245 bool IsExpanderColumn( int aCol ) const override;
246 GROUP_TYPE GetGroupType( int aRow ) const override
247 {
248 return m_rows[aRow].m_Flag;
249 }
250
251 void SetSorting( int aCol, bool ascending )
252 {
253 wxCHECK_RET( aCol >= 0 && aCol < (int) m_cols.size(), "Invalid Column Number" );
254 m_sortColumn = aCol;
255 m_sortAscending = ascending;
256 }
257
258 int GetSortCol() { return m_sortColumn; }
259 bool GetSortAsc() { return m_sortAscending; }
260
261 // These are used to disable the RebuildRows functionality while we're generating
262 // lots of events in the UI, e.g. applying a BOM preset, that would thrash the grid.
263 void EnableRebuilds();
264 void DisableRebuilds();
265 void RebuildRows();
266
267 void ExpandRow( int aRow );
268 void CollapseRow( int aRow );
269 void ExpandCollapseRow( int aRow );
270 void CollapseForSort();
271 void ExpandAfterSort();
272
273 void ApplyData( SCH_COMMIT& aCommit, TEMPLATES& aTemplateFieldnames, const wxString& aVariantName );
274
275 bool IsEdited() { return m_edited; }
276
277 int GetDataWidth( int aCol );
278
279 void SetFilter( const wxString& aFilter ) { m_filter = aFilter; }
280 const wxString& GetFilter() { return m_filter; }
281
282 void SetScope( SCOPE aScope ) { m_scope = aScope; }
283 SCOPE GetScope() { return m_scope; }
284
285 void SetPath( const SCH_SHEET_PATH& aPath ) { m_path = aPath; }
286 const SCH_SHEET_PATH& GetPath() { return m_path; }
287
290
291 /* These contradictorily named functions force including symbols that
292 * have the Exclude from BOM check box ticked. This is needed so we can view
293 * these parts in the symbol fields table dialog, while also excluding from the
294 * BOM export */
295 void SetIncludeExcludedFromBOM( bool include ) { m_includeExcluded = include; }
297
298 void SetExcludeDNP( bool exclude ) { m_excludeDNP = exclude; }
299 bool GetExcludeDNP() { return m_excludeDNP; }
300
301 void SetGroupColumn( int aCol, bool group )
302 {
303 wxCHECK_RET( aCol >= 0 && aCol < (int) m_cols.size(), "Invalid Column Number" );
304 m_cols[aCol].m_group = group;
305 }
306
307 bool GetGroupColumn( int aCol )
308 {
309 wxCHECK_MSG( aCol >= 0 && aCol < (int) m_cols.size(), false, "Invalid Column Number" );
310 return m_cols[aCol].m_group;
311 }
312
313 void SetShowColumn( int aCol, bool show )
314 {
315 wxCHECK_RET( aCol >= 0 && aCol < (int) m_cols.size(), "Invalid Column Number" );
316 m_cols[aCol].m_show = show;
317 }
318
319 bool GetShowColumn( int aCol )
320 {
321 wxCHECK_MSG( aCol >= 0 && aCol < (int) m_cols.size(), false, "Invalid Column Number" );
322 return m_cols[aCol].m_show;
323 }
324
325 void ApplyBomPreset( const BOM_PRESET& preset, const wxString& aVariantName );
327 wxString Export( const BOM_FMT_PRESET& settings );
328
329 void AddReferences( const SCH_REFERENCE_LIST& aRefs );
330 void RemoveReferences( const SCH_REFERENCE_LIST& aRefs );
331 void RemoveSymbol( const SCH_SYMBOL& aSymbol );
332 void UpdateReferences( const SCH_REFERENCE_LIST& aRefs, const wxString& aVariantName );
333
334 bool DeleteRows( size_t aPosition = 0, size_t aNumRows = 1 ) override;
335
337
346 void SetCurrentVariant( const wxString& aVariantName ) { m_currentVariant = aVariantName; }
347 const wxString& GetCurrentVariant() const { return m_currentVariant; }
348
349 void SetVariantNames( const std::vector<wxString>& aVariantNames ) { m_variantNames = aVariantNames; }
350 const std::vector<wxString>& GetVariantNames() const { return m_variantNames; }
351
352private:
353 static bool cmp( const DATA_MODEL_ROW& lhGroup, const DATA_MODEL_ROW& rhGroup,
354 FIELDS_EDITOR_GRID_DATA_MODEL* dataModel, int sortCol, bool ascending );
355
356 bool unitMatch( const SCH_REFERENCE& lhRef, const SCH_REFERENCE& rhRef );
357 bool groupMatch( const SCH_REFERENCE& lhRef, const SCH_REFERENCE& rhRef );
358
359 // Helper functions to deal with translating wxGrid values to and from
360 // named field values like ${DNP}
361 bool isAttribute( const wxString& aFieldName );
362 wxString getAttributeValue( const SCH_REFERENCE& aRef, const wxString& aAttributeName,
363 const wxString& aVariantNames );
364
374 wxString getDefaultFieldValue( const SCH_REFERENCE& aRef, const wxString& aFieldName );
375
386 bool setAttributeValue( SCH_REFERENCE& aRef, const wxString& aAttributeName, const wxString& aValue,
387 const wxString& aVariantName = wxEmptyString );
388
389 /* Helper function to get the resolved field value.
390 * Handles symbols that are missing fields that would have a variable
391 * in their value because their name is the same as a variable.
392 * Example: BOM template provides ${DNP} as a field, but they symbol doesn't have the field. */
393 wxString getFieldShownText( const SCH_REFERENCE& aRef, const wxString& aFieldName );
394
395 void Sort();
396
397 void updateDataStoreSymbolField( const SCH_REFERENCE& aSymbolRef, const wxString& aFieldName,
398 const wxString& aVariantName );
399
400protected:
411 wxString m_filter;
418 wxGridCellAttr* m_urlEditor;
419 wxGridCellRenderer* m_textVarRenderer;
421 std::vector<wxString> m_variantNames;
422
423 std::vector<DATA_MODEL_COL> m_cols;
424 std::vector<DATA_MODEL_ROW> m_rows;
425
426 // Data store
427 // The data model is fundamentally m_componentRefs X m_fieldNames.
428 // A map of compID : fieldSet, where fieldSet is a map of fieldName : fieldValue
429 // The compID is now the full KIID_PATH (sheet path + symbol UUID) as a string
430 std::map<KIID_PATH, std::map<wxString, wxString>> m_dataStore;
431};
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
#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)
enum KICOMMON_API GROUP_TYPE
Definition wx_grid.h:44