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 (C) 2023 <author>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20#include <sch_reference_list.h>
21#include <wx/grid.h>
22
23// The field name in the data model (translated)
24#define DISPLAY_NAME_COLUMN 0
25
26// The field name's label for exporting (CSV, etc.)
27#define LABEL_COLUMN 1
28#define SHOW_FIELD_COLUMN 2
29#define GROUP_BY_COLUMN 3
30
31// The internal field name (untranslated)
32#define FIELD_NAME_COLUMN 4
33
34struct BOM_FIELD;
35struct BOM_PRESET;
36struct BOM_FMT_PRESET;
37
39{
45};
46
47
49{
50 DATA_MODEL_ROW( const SCH_REFERENCE& aFirstReference, GROUP_TYPE aType )
51 {
52 m_ItemNumber = 0;
53 m_Refs.push_back( aFirstReference );
54 m_Flag = aType;
55 }
56
59 std::vector<SCH_REFERENCE> m_Refs;
60};
61
62
64{
65 wxString m_fieldName;
66 wxString m_label;
68 bool m_show;
69 bool m_group;
70};
71
72
73class FIELDS_EDITOR_GRID_DATA_MODEL : public wxGridTableBase
74{
75public:
76 enum SCOPE : int
77 {
81 };
82
84 m_symbolsList( aSymbolsList ),
85 m_edited( false ),
86 m_sortColumn( 0 ),
87 m_sortAscending( false ),
89 m_groupingEnabled( false ),
90 m_excludeDNP( false ),
91 m_includeExcluded( false ),
92 m_rebuildsEnabled( true )
93 {
95 }
96
97 static const wxString QUANTITY_VARIABLE;
98 static const wxString ITEM_NUMBER_VARIABLE;
99
100 void AddColumn( const wxString& aFieldName, const wxString& aLabel, bool aAddedByUser );
101 void RemoveColumn( int aCol );
102 void RenameColumn( int aCol, const wxString& newName );
103
104 void MoveColumn( int aCol, int aNewPos )
105 {
106 wxCHECK_RET( aCol >= 0 && aCol < (int) m_cols.size(), "Invalid Column Number" );
107
108 if( aCol == aNewPos )
109 {
110 return;
111 }
112 else if( aCol < aNewPos )
113 {
114 std::rotate( std::begin( m_cols ) + aCol, std::begin( m_cols ) + aCol + 1,
115 std::begin( m_cols ) + aNewPos + 1 );
116 }
117 else
118 {
119 std::rotate( std::begin( m_cols ) + aNewPos, std::begin( m_cols ) + aCol,
120 std::begin( m_cols ) + aCol + 1 );
121 }
122 }
123
124 int GetNumberRows() override { return (int) m_rows.size(); }
125 int GetNumberCols() override { return (int) m_cols.size(); }
126
127 void SetColLabelValue( int aCol, const wxString& aLabel ) override
128 {
129 wxCHECK_RET( aCol >= 0 && aCol < (int) m_cols.size(), "Invalid Column Number" );
130 m_cols[aCol].m_label = aLabel;
131 }
132
133
134 wxString GetColLabelValue( int aCol ) override
135 {
136 wxCHECK( aCol >= 0 && aCol < (int) m_cols.size(), wxString() );
137 return m_cols[aCol].m_label;
138 }
139
140 wxString GetColFieldName( int aCol )
141 {
142 wxCHECK( aCol >= 0 && aCol < (int) m_cols.size(), wxString() );
143 return m_cols[aCol].m_fieldName;
144 }
145
146 int GetFieldNameCol( wxString aFieldName );
147
148 const std::vector<BOM_FIELD> GetFieldsOrdered();
149 void SetFieldsOrder( const std::vector<wxString>& aNewOrder );
150
151 bool IsEmptyCell( int aRow, int aCol ) override
152 {
153 return false; // don't allow adjacent cell overflow, even if we are actually empty
154 }
155
156 wxString GetValue( int aRow, int aCol ) override;
157 wxString GetValue( const DATA_MODEL_ROW& group, int aCol,
158 const wxString& refDelimiter = wxT( ", " ),
159 const wxString& refRangDelimiter = wxT( "-" ),
160 bool resolveVars = false,
161 bool listMixedValues = false );
162
163 wxString GetExportValue( int aRow, int aCol, const wxString& refDelimiter,
164 const wxString& refRangeDelimiter )
165 {
166 return GetValue( m_rows[aRow], aCol, refDelimiter, refRangeDelimiter, true, true );
167 }
168
169 void SetValue( int aRow, int aCol, const wxString& aValue ) override;
170
171 GROUP_TYPE GetRowFlags( int aRow ) { return m_rows[aRow].m_Flag; }
172
173 std::vector<SCH_REFERENCE> GetRowReferences( int aRow ) const
174 {
175 wxCHECK( aRow >= 0 && aRow < (int) m_rows.size(), std::vector<SCH_REFERENCE>() );
176 return m_rows[aRow].m_Refs;
177 }
178
179 bool ColIsReference( int aCol );
180 bool ColIsValue( int aCol );
181 bool ColIsQuantity( int aCol );
182 bool ColIsItemNumber( int aCol );
183 bool ColIsAttribute( int aCol );
184
185 void SetSorting( int aCol, bool ascending )
186 {
187 wxCHECK_RET( aCol >= 0 && aCol < (int) m_cols.size(), "Invalid Column Number" );
188 m_sortColumn = aCol;
189 m_sortAscending = ascending;
190 }
191
192 int GetSortCol() { return m_sortColumn; }
193 bool GetSortAsc() { return m_sortAscending; }
194
195 // These are used to disable the RebuildRows functionality while we're generating
196 // lots of events in the UI, e.g. applying a BOM preset, that would thrash the grid.
197 void EnableRebuilds();
198 void DisableRebuilds();
199 void RebuildRows();
200
201 void ExpandRow( int aRow );
202 void CollapseRow( int aRow );
203 void ExpandCollapseRow( int aRow );
204 void CollapseForSort();
205 void ExpandAfterSort();
206
207 void ApplyData( std::function<void( SCH_SYMBOL&, SCH_SHEET_PATH& )> symbolChangeHandler );
208
209 bool IsEdited() { return m_edited; }
210
211 int GetDataWidth( int aCol );
212
213 void SetFilter( const wxString& aFilter ) { m_filter = aFilter; }
214 const wxString& GetFilter() { return m_filter; }
215
216 void SetScope( SCOPE aScope ) { m_scope = aScope; }
217 SCOPE GetScope() { return m_scope; }
218
219 void SetPath( const SCH_SHEET_PATH& aPath ) { m_path = aPath; }
220 const SCH_SHEET_PATH& GetPath() { return m_path; }
221
224
225 /* These contradictorily named functions force including symbols that
226 * have the Exclude from BOM check box ticked. This is needed so we can view
227 * these parts in the symbol fields table dialog, while also excluding from the
228 * BOM export */
229 void SetIncludeExcludedFromBOM( bool include ) { m_includeExcluded = include; }
231
232 void SetExcludeDNP( bool exclude ) { m_excludeDNP = exclude; }
233 bool GetExcludeDNP() { return m_excludeDNP; }
234
235 void SetGroupColumn( int aCol, bool group )
236 {
237 wxCHECK_RET( aCol >= 0 && aCol < (int) m_cols.size(), "Invalid Column Number" );
238 m_cols[aCol].m_group = group;
239 }
240
241 bool GetGroupColumn( int aCol )
242 {
243 wxCHECK_MSG( aCol >= 0 && aCol < (int) m_cols.size(), false, "Invalid Column Number" );
244 return m_cols[aCol].m_group;
245 }
246
247 void SetShowColumn( int aCol, bool show )
248 {
249 wxCHECK_RET( aCol >= 0 && aCol < (int) m_cols.size(), "Invalid Column Number" );
250 m_cols[aCol].m_show = show;
251 }
252
253 bool GetShowColumn( int aCol )
254 {
255 wxCHECK_MSG( aCol >= 0 && aCol < (int) m_cols.size(), false, "Invalid Column Number" );
256 return m_cols[aCol].m_show;
257 }
258
259 void ApplyBomPreset( const BOM_PRESET& preset );
261 wxString Export( const BOM_FMT_PRESET& settings );
262
263 void AddReferences( const SCH_REFERENCE_LIST& aRefs );
264 void RemoveReferences( const SCH_REFERENCE_LIST& aRefs );
265 void RemoveSymbol( const SCH_SYMBOL& aSymbol );
266 void UpdateReferences( const SCH_REFERENCE_LIST& aRefs );
267
268
269private:
270 static bool cmp( const DATA_MODEL_ROW& lhGroup, const DATA_MODEL_ROW& rhGroup,
271 FIELDS_EDITOR_GRID_DATA_MODEL* dataModel, int sortCol, bool ascending );
272 bool unitMatch( const SCH_REFERENCE& lhRef, const SCH_REFERENCE& rhRef );
273 bool groupMatch( const SCH_REFERENCE& lhRef, const SCH_REFERENCE& rhRef );
274
275 // Helper functions to deal with translating wxGrid values to and from
276 // named field values like ${DNP}
277 bool isAttribute( const wxString& aFieldName );
278 wxString getAttributeValue( const SCH_SYMBOL&, const wxString& aAttributeName );
279 void setAttributeValue( SCH_SYMBOL& aSymbol, const wxString& aAttributeName,
280 const wxString& aValue );
281
282 /* Helper function to get the resolved field value.
283 * Handles symbols that are missing fields that would have a variable
284 * in their value because their name is the same as a variable.
285 * Example: BOM template provides ${DNP} as a field, but they symbol doesn't have the field. */
286 wxString getFieldShownText( const SCH_REFERENCE& aRef, const wxString& aFieldName );
287
288 void Sort();
289
292 void updateDataStoreSymbolField( const SCH_SYMBOL& aSymbol, const wxString& aFieldName );
293
294protected:
299 wxString m_filter;
306
307 std::vector<DATA_MODEL_COL> m_cols;
308 std::vector<DATA_MODEL_ROW> m_rows;
309
310 // Data store
311 // The data model is fundamentally m_componentRefs X m_fieldNames.
312 // A map of compID : fieldSet, where fieldSet is a map of fieldName : fieldValue
313 std::map<KIID, std::map<wxString, wxString>> m_dataStore;
314};
wxString GetColFieldName(int aCol)
std::vector< DATA_MODEL_ROW > m_rows
int GetFieldNameCol(wxString aFieldName)
void ApplyBomPreset(const BOM_PRESET &preset)
void SetFieldsOrder(const std::vector< wxString > &aNewOrder)
std::vector< SCH_REFERENCE > GetRowReferences(int aRow) const
SCH_REFERENCE_LIST getSymbolReferences(SCH_SYMBOL *aSymbol)
wxString getAttributeValue(const SCH_SYMBOL &, const wxString &aAttributeName)
void updateDataStoreSymbolField(const SCH_SYMBOL &aSymbol, const wxString &aFieldName)
bool groupMatch(const SCH_REFERENCE &lhRef, const SCH_REFERENCE &rhRef)
const SCH_SHEET_PATH & GetPath()
wxString GetColLabelValue(int aCol) override
bool unitMatch(const SCH_REFERENCE &lhRef, const SCH_REFERENCE &rhRef)
wxString GetExportValue(int aRow, int aCol, const wxString &refDelimiter, const wxString &refRangeDelimiter)
void SetPath(const SCH_SHEET_PATH &aPath)
wxString getFieldShownText(const SCH_REFERENCE &aRef, const wxString &aFieldName)
bool IsEmptyCell(int aRow, int aCol) override
void RenameColumn(int aCol, const wxString &newName)
wxString Export(const BOM_FMT_PRESET &settings)
void AddColumn(const wxString &aFieldName, const wxString &aLabel, bool aAddedByUser)
std::vector< DATA_MODEL_COL > m_cols
void SetSorting(int aCol, bool ascending)
void SetFilter(const wxString &aFilter)
static bool cmp(const DATA_MODEL_ROW &lhGroup, const DATA_MODEL_ROW &rhGroup, FIELDS_EDITOR_GRID_DATA_MODEL *dataModel, int sortCol, bool ascending)
FIELDS_EDITOR_GRID_DATA_MODEL(SCH_REFERENCE_LIST &aSymbolsList)
static const wxString ITEM_NUMBER_VARIABLE
void SetIncludeExcludedFromBOM(bool include)
bool isAttribute(const wxString &aFieldName)
wxString GetValue(int aRow, int aCol) override
void UpdateReferences(const SCH_REFERENCE_LIST &aRefs)
void ApplyData(std::function< void(SCH_SYMBOL &, SCH_SHEET_PATH &)> symbolChangeHandler)
GROUP_TYPE GetRowFlags(int aRow)
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)
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 storeReferenceFields(SCH_REFERENCE &aRef)
void setAttributeValue(SCH_SYMBOL &aSymbol, const wxString &aAttributeName, const wxString &aValue)
void AddReferences(const SCH_REFERENCE_LIST &aRefs)
const std::vector< BOM_FIELD > GetFieldsOrdered()
Container to create a flattened list of symbols because in a complex hierarchy, a symbol can be used ...
void SplitReferences()
Attempt to split all reference designators into a name (U) and number (1).
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:77
GROUP_TYPE
@ GROUP_COLLAPSED_DURING_SORT
@ GROUP_EXPANDED
@ GROUP_COLLAPSED
@ GROUP_SINGLETON
@ CHILD_ITEM
std::vector< SCH_REFERENCE > m_Refs
DATA_MODEL_ROW(const SCH_REFERENCE &aFirstReference, GROUP_TYPE aType)