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 {
144 m_symbolsList.SplitReferences();
145 }
146
148 {
149 wxSafeDecRef( m_urlEditor );
150 }
151
152 static const wxString QUANTITY_VARIABLE;
153 static const wxString ITEM_NUMBER_VARIABLE;
154
155 void AddColumn( const wxString& aFieldName, const wxString& aLabel, bool aAddedByUser,
156 const std::set<wxString>& aVariantNames );
157 void RemoveColumn( int aCol );
158 void RenameColumn( int aCol, const wxString& newName );
159
160 void MoveColumn( int aCol, int aNewPos )
161 {
162 wxCHECK_RET( aCol >= 0 && aCol < static_cast<int>( m_cols.size() ), "Invalid Column Number" );
163
164 if( aCol == aNewPos )
165 {
166 return;
167 }
168 else if( aCol < aNewPos )
169 {
170 std::rotate( std::begin( m_cols ) + aCol, std::begin( m_cols ) + aCol + 1,
171 std::begin( m_cols ) + aNewPos + 1 );
172 }
173 else
174 {
175 std::rotate( std::begin( m_cols ) + aNewPos, std::begin( m_cols ) + aCol,
176 std::begin( m_cols ) + aCol + 1 );
177 }
178 }
179
180 int GetNumberRows() override { return (int) m_rows.size(); }
181 int GetNumberCols() override { return (int) m_cols.size(); }
182
183 void SetColLabelValue( int aCol, const wxString& aLabel ) override
184 {
185 wxCHECK_RET( aCol >= 0 && aCol < static_cast<int>( m_cols.size() ), "Invalid Column Number" );
186 m_cols[aCol].m_label = aLabel;
187 }
188
189 wxString GetColLabelValue( int aCol ) override
190 {
191 wxCHECK( aCol >= 0 && aCol < static_cast<int>( m_cols.size() ), wxString() );
192 return m_cols[aCol].m_label;
193 }
194
195 wxString GetColFieldName( int aCol )
196 {
197 wxCHECK( aCol >= 0 && aCol < static_cast<int>( m_cols.size() ), wxString() );
198 return m_cols[aCol].m_fieldName;
199 }
200
201 int GetFieldNameCol( const wxString& aFieldName ) const;
202
203 std::vector<BOM_FIELD> GetFieldsOrdered();
204 void SetFieldsOrder( const std::vector<wxString>& aNewOrder );
205
206 bool IsEmptyCell( int aRow, int aCol ) override
207 {
208 return false; // don't allow adjacent cell overflow, even if we are actually empty
209 }
210
211 wxString GetValue( int aRow, int aCol ) override;
212 wxGridCellAttr* GetAttr( int aRow, int aCol, wxGridCellAttr::wxAttrKind aKind ) override;
213
214 wxString GetValue( const DATA_MODEL_ROW& group, int aCol,
215 const wxString& refDelimiter = wxT( ", " ),
216 const wxString& refRangDelimiter = wxT( "-" ),
217 bool resolveVars = false,
218 bool listMixedValues = false );
219
220 wxString GetExportValue( int aRow, int aCol, const wxString& refDelimiter,
221 const wxString& refRangeDelimiter )
222 {
223 return GetValue( m_rows[aRow], aCol, refDelimiter, refRangeDelimiter, true, true );
224 }
225
226 void SetValue( int aRow, int aCol, const wxString& aValue ) override;
227
228 GROUP_TYPE GetRowFlags( int aRow ) { return m_rows[aRow].m_Flag; }
229
230 std::vector<SCH_REFERENCE> GetRowReferences( int aRow ) const
231 {
232 wxCHECK( aRow >= 0 && aRow < (int) m_rows.size(), std::vector<SCH_REFERENCE>() );
233 return m_rows[aRow].m_Refs;
234 }
235
236 bool ColIsReference( int aCol );
237 bool ColIsValue( int aCol );
238 bool ColIsQuantity( int aCol );
239 bool ColIsItemNumber( int aCol );
240 bool ColIsAttribute( int aCol );
241
242 bool IsExpanderColumn( int aCol ) const override;
243 GROUP_TYPE GetGroupType( int aRow ) const override
244 {
245 return m_rows[aRow].m_Flag;
246 }
247
248 void SetSorting( int aCol, bool ascending )
249 {
250 wxCHECK_RET( aCol >= 0 && aCol < (int) m_cols.size(), "Invalid Column Number" );
251 m_sortColumn = aCol;
252 m_sortAscending = ascending;
253 }
254
255 int GetSortCol() { return m_sortColumn; }
256 bool GetSortAsc() { return m_sortAscending; }
257
258 // These are used to disable the RebuildRows functionality while we're generating
259 // lots of events in the UI, e.g. applying a BOM preset, that would thrash the grid.
260 void EnableRebuilds();
261 void DisableRebuilds();
262 void RebuildRows();
263
264 void ExpandRow( int aRow );
265 void CollapseRow( int aRow );
266 void ExpandCollapseRow( int aRow );
267 void CollapseForSort();
268 void ExpandAfterSort();
269
270 void ApplyData( SCH_COMMIT& aCommit, TEMPLATES& aTemplateFieldnames, std::set<wxString>& aVariantNames );
271
272 bool IsEdited() { return m_edited; }
273
274 int GetDataWidth( int aCol );
275
276 void SetFilter( const wxString& aFilter ) { m_filter = aFilter; }
277 const wxString& GetFilter() { return m_filter; }
278
279 void SetScope( SCOPE aScope ) { m_scope = aScope; }
280 SCOPE GetScope() { return m_scope; }
281
282 void SetPath( const SCH_SHEET_PATH& aPath ) { m_path = aPath; }
283 const SCH_SHEET_PATH& GetPath() { return m_path; }
284
287
288 /* These contradictorily named functions force including symbols that
289 * have the Exclude from BOM check box ticked. This is needed so we can view
290 * these parts in the symbol fields table dialog, while also excluding from the
291 * BOM export */
292 void SetIncludeExcludedFromBOM( bool include ) { m_includeExcluded = include; }
294
295 void SetExcludeDNP( bool exclude ) { m_excludeDNP = exclude; }
296 bool GetExcludeDNP() { return m_excludeDNP; }
297
298 void SetGroupColumn( int aCol, bool group )
299 {
300 wxCHECK_RET( aCol >= 0 && aCol < (int) m_cols.size(), "Invalid Column Number" );
301 m_cols[aCol].m_group = group;
302 }
303
304 bool GetGroupColumn( int aCol )
305 {
306 wxCHECK_MSG( aCol >= 0 && aCol < (int) m_cols.size(), false, "Invalid Column Number" );
307 return m_cols[aCol].m_group;
308 }
309
310 void SetShowColumn( int aCol, bool show )
311 {
312 wxCHECK_RET( aCol >= 0 && aCol < (int) m_cols.size(), "Invalid Column Number" );
313 m_cols[aCol].m_show = show;
314 }
315
316 bool GetShowColumn( int aCol )
317 {
318 wxCHECK_MSG( aCol >= 0 && aCol < (int) m_cols.size(), false, "Invalid Column Number" );
319 return m_cols[aCol].m_show;
320 }
321
322 void ApplyBomPreset( const BOM_PRESET& preset, const std::set<wxString>& aVariantNames );
324 wxString Export( const BOM_FMT_PRESET& settings );
325
326 void AddReferences( const SCH_REFERENCE_LIST& aRefs );
327 void RemoveReferences( const SCH_REFERENCE_LIST& aRefs );
328 void RemoveSymbol( const SCH_SYMBOL& aSymbol );
329 void UpdateReferences( const SCH_REFERENCE_LIST& aRefs, const std::set<wxString>& aVariantNames );
330
331private:
332 static bool cmp( const DATA_MODEL_ROW& lhGroup, const DATA_MODEL_ROW& rhGroup,
333 FIELDS_EDITOR_GRID_DATA_MODEL* dataModel, int sortCol, bool ascending );
334
335 bool unitMatch( const SCH_REFERENCE& lhRef, const SCH_REFERENCE& rhRef );
336 bool groupMatch( const SCH_REFERENCE& lhRef, const SCH_REFERENCE& rhRef );
337
338 // Helper functions to deal with translating wxGrid values to and from
339 // named field values like ${DNP}
340 bool isAttribute( const wxString& aFieldName );
341 wxString getAttributeValue( const SCH_SYMBOL&, const wxString& aAttributeName,
342 const std::set<wxString>& aVariantNames );
343
354 bool setAttributeValue( SCH_SYMBOL& aSymbol, const wxString& aAttributeName, const wxString& aValue,
355 const wxString& aVariantName = wxEmptyString );
356
357 /* Helper function to get the resolved field value.
358 * Handles symbols that are missing fields that would have a variable
359 * in their value because their name is the same as a variable.
360 * Example: BOM template provides ${DNP} as a field, but they symbol doesn't have the field. */
361 wxString getFieldShownText( const SCH_REFERENCE& aRef, const wxString& aFieldName );
362
363 void Sort();
364
365 void updateDataStoreSymbolField( const SCH_REFERENCE& aSymbolRef, const wxString& aFieldName,
366 const std::set<wxString>& aVariantNames );
367
368protected:
379 wxString m_filter;
386 wxGridCellAttr* m_urlEditor;
387
388 std::vector<DATA_MODEL_COL> m_cols;
389 std::vector<DATA_MODEL_ROW> m_rows;
390
391 // Data store
392 // The data model is fundamentally m_componentRefs X m_fieldNames.
393 // A map of compID : fieldSet, where fieldSet is a map of fieldName : fieldValue
394 std::map<KIID, std::map<wxString, wxString>> m_dataStore;
395};
int GetFieldNameCol(const wxString &aFieldName) const
void ApplyBomPreset(const BOM_PRESET &preset, const std::set< wxString > &aVariantNames)
std::vector< DATA_MODEL_ROW > m_rows
wxString getAttributeValue(const SCH_SYMBOL &, const wxString &aAttributeName, const std::set< wxString > &aVariantNames)
void SetFieldsOrder(const std::vector< wxString > &aNewOrder)
std::vector< SCH_REFERENCE > GetRowReferences(int aRow) const
void UpdateReferences(const SCH_REFERENCE_LIST &aRefs, const std::set< wxString > &aVariantNames)
SCH_REFERENCE_LIST m_symbolsList
The flattened by hierarchy list of symbols.
bool groupMatch(const SCH_REFERENCE &lhRef, const SCH_REFERENCE &rhRef)
const SCH_SHEET_PATH & GetPath()
wxString GetColLabelValue(int aCol) override
void ApplyData(SCH_COMMIT &aCommit, TEMPLATES &aTemplateFieldnames, std::set< wxString > &aVariantNames)
bool unitMatch(const SCH_REFERENCE &lhRef, const SCH_REFERENCE &rhRef)
wxString GetExportValue(int aRow, int aCol, const wxString &refDelimiter, const wxString &refRangeDelimiter)
void AddColumn(const wxString &aFieldName, const wxString &aLabel, bool aAddedByUser, const std::set< wxString > &aVariantNames)
void SetPath(const SCH_SHEET_PATH &aPath)
wxString getFieldShownText(const SCH_REFERENCE &aRef, const wxString &aFieldName)
bool IsEmptyCell(int aRow, int aCol) override
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< 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_SYMBOL &aSymbol, const wxString &aAttributeName, const wxString &aValue, const wxString &aVariantName=wxEmptyString)
Set the attribute value.
static bool cmp(const DATA_MODEL_ROW &lhGroup, const DATA_MODEL_ROW &rhGroup, FIELDS_EDITOR_GRID_DATA_MODEL *dataModel, int sortCol, bool ascending)
static const wxString ITEM_NUMBER_VARIABLE
void SetIncludeExcludedFromBOM(bool include)
bool isAttribute(const wxString &aFieldName)
wxString GetValue(int aRow, int aCol) override
void updateDataStoreSymbolField(const SCH_REFERENCE &aSymbolRef, const wxString &aFieldName, const std::set< wxString > &aVariantNames)
std::map< KIID, std::map< wxString, wxString > > m_dataStore
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
void SetColLabelValue(int aCol, const wxString &aLabel) override
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:75
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)
GROUP_TYPE
Definition wx_grid.h:43