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 (C) 2023 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 ), m_edited( false ), m_sortColumn( 0 ),
86 m_excludeDNP( false ), m_includeExcluded( false ), m_rebuildsEnabled( true )
87 {
89 }
90
91 static const wxString QUANTITY_VARIABLE;
92 static const wxString ITEM_NUMBER_VARIABLE;
93
94 void AddColumn( const wxString& aFieldName, const wxString& aLabel, bool aAddedByUser );
95 void RemoveColumn( int aCol );
96 void RenameColumn( int aCol, const wxString& newName );
97
98 void MoveColumn( int aCol, int aNewPos )
99 {
100 wxCHECK_RET( aCol >= 0 && aCol < (int) m_cols.size(), "Invalid Column Number" );
101
102 if( aCol == aNewPos )
103 return;
104 else if( aCol < aNewPos )
105 std::rotate( std::begin( m_cols ) + aCol, std::begin( m_cols ) + aCol + 1,
106 std::begin( m_cols ) + aNewPos + 1 );
107 else
108 std::rotate( std::begin( m_cols ) + aNewPos, std::begin( m_cols ) + aCol,
109 std::begin( m_cols ) + aCol + 1 );
110 }
111
112 int GetNumberRows() override { return (int) m_rows.size(); }
113 int GetNumberCols() override { return (int) m_cols.size(); }
114
115 void SetColLabelValue( int aCol, const wxString& aLabel ) override
116 {
117 wxCHECK_RET( aCol >= 0 && aCol < (int) m_cols.size(), "Invalid Column Number" );
118 m_cols[aCol].m_label = aLabel;
119 }
120
121
122 wxString GetColLabelValue( int aCol ) override
123 {
124 wxCHECK( aCol >= 0 && aCol < (int) m_cols.size(), wxString() );
125 return m_cols[aCol].m_label;
126 }
127
128 wxString GetColFieldName( int aCol )
129 {
130 wxCHECK( aCol >= 0 && aCol < (int) m_cols.size(), wxString() );
131 return m_cols[aCol].m_fieldName;
132 }
133
134 int GetFieldNameCol( wxString aFieldName );
135
136 const std::vector<BOM_FIELD> GetFieldsOrdered();
137 void SetFieldsOrder( const std::vector<wxString>& aNewOrder );
138
139 bool IsEmptyCell( int aRow, int aCol ) override
140 {
141 return false; // don't allow adjacent cell overflow, even if we are actually empty
142 }
143
144 wxString GetValue( int aRow, int aCol ) override;
145 wxString GetValue( const DATA_MODEL_ROW& group, int aCol,
146 const wxString& refDelimiter = wxT( ", " ),
147 const wxString& refRangDelimiter = wxT( "-" ),
148 bool resolveVars = false );
149
150 wxString GetExportValue( int aRow, int aCol, const wxString& refDelimiter,
151 const wxString& refRangeDelimiter )
152 {
153 return GetValue( m_rows[aRow], aCol, refDelimiter, refRangeDelimiter, true );
154 }
155
156 void SetValue( int aRow, int aCol, const wxString& aValue ) override;
157
158 GROUP_TYPE GetRowFlags( int aRow ) { return m_rows[aRow].m_Flag; }
159
160 std::vector<SCH_REFERENCE> GetRowReferences( int aRow ) const
161 {
162 wxCHECK( aRow >= 0 && aRow < (int) m_rows.size(), std::vector<SCH_REFERENCE>() );
163 return m_rows[aRow].m_Refs;
164 }
165
166 bool ColIsReference( int aCol );
167 bool ColIsValue( int aCol );
168 bool ColIsQuantity( int aCol );
169 bool ColIsItemNumber( int aCol );
170 bool ColIsAttribute( int aCol );
171
172 void SetSorting( int aCol, bool ascending )
173 {
174 wxCHECK_RET( aCol >= 0 && aCol < (int) m_cols.size(), "Invalid Column Number" );
175 m_sortColumn = aCol;
176 m_sortAscending = ascending;
177 }
178
179 int GetSortCol() { return m_sortColumn; }
180 bool GetSortAsc() { return m_sortAscending; }
181
182 // These are used to disable the RebuildRows functionality while we're generating
183 // lots of events in the UI, e.g. applying a BOM preset, that would thrash the grid.
184 void EnableRebuilds();
185 void DisableRebuilds();
186 void RebuildRows();
187
188 void ExpandRow( int aRow );
189 void CollapseRow( int aRow );
190 void ExpandCollapseRow( int aRow );
191 void CollapseForSort();
192 void ExpandAfterSort();
193
194 void ApplyData( std::function<void( SCH_SYMBOL&, SCH_SHEET_PATH& )> symbolChangeHandler );
195
196 bool IsEdited() { return m_edited; }
197
198 int GetDataWidth( int aCol );
199
200 void SetFilter( const wxString& aFilter ) { m_filter = aFilter; }
201 const wxString& GetFilter() { return m_filter; }
202
203 void SetScope( SCOPE aScope ) { m_scope = aScope; }
204 SCOPE GetScope() { return m_scope; }
205
206 void SetPath( const SCH_SHEET_PATH& aPath ) { m_path = aPath; }
207 const SCH_SHEET_PATH& GetPath() { return m_path; }
208
211
212 /* These contradictorily named functions force including symbols that
213 * have the Exclude from BOM check box ticked. This is needed so we can view
214 * these parts in the symbol fields table dialog, while also excluding from the
215 * BOM export */
216 void SetIncludeExcludedFromBOM( bool include ) { m_includeExcluded = include; }
218
219 void SetExcludeDNP( bool exclude ) { m_excludeDNP = exclude; }
220 bool GetExcludeDNP() { return m_excludeDNP; }
221
222 void SetGroupColumn( int aCol, bool group )
223 {
224 wxCHECK_RET( aCol >= 0 && aCol < (int) m_cols.size(), "Invalid Column Number" );
225 m_cols[aCol].m_group = group;
226 }
227
228 bool GetGroupColumn( int aCol )
229 {
230 wxCHECK_MSG( aCol >= 0 && aCol < (int) m_cols.size(), false, "Invalid Column Number" );
231 return m_cols[aCol].m_group;
232 }
233
234 void SetShowColumn( int aCol, bool show )
235 {
236 wxCHECK_RET( aCol >= 0 && aCol < (int) m_cols.size(), "Invalid Column Number" );
237 m_cols[aCol].m_show = show;
238 }
239
240 bool GetShowColumn( int aCol )
241 {
242 wxCHECK_MSG( aCol >= 0 && aCol < (int) m_cols.size(), false, "Invalid Column Number" );
243 return m_cols[aCol].m_show;
244 }
245
246 void ApplyBomPreset( const BOM_PRESET& preset );
248 wxString Export( const BOM_FMT_PRESET& settings );
249
250 void AddReferences( const SCH_REFERENCE_LIST& aRefs );
251 void RemoveReferences( const SCH_REFERENCE_LIST& aRefs );
252 void RemoveSymbol( const SCH_SYMBOL& aSymbol );
253 void UpdateReferences( const SCH_REFERENCE_LIST& aRefs );
254
255
256private:
257 static bool cmp( const DATA_MODEL_ROW& lhGroup, const DATA_MODEL_ROW& rhGroup,
258 FIELDS_EDITOR_GRID_DATA_MODEL* dataModel, int sortCol, bool ascending );
259 bool unitMatch( const SCH_REFERENCE& lhRef, const SCH_REFERENCE& rhRef );
260 bool groupMatch( const SCH_REFERENCE& lhRef, const SCH_REFERENCE& rhRef );
261
262 // Helper functions to deal with translating wxGrid values to and from
263 // named field values like ${DNP}
264 bool isAttribute( const wxString& aFieldName );
265 wxString getAttributeValue( const SCH_SYMBOL&, const wxString& aAttributeName );
266 void setAttributeValue( SCH_SYMBOL& aSymbol, const wxString& aAttributeName,
267 const wxString& aValue );
268
269 /* Helper function to get the resolved field value.
270 * Handles symbols that are missing fields that would have a variable
271 * in their value because their name is the same as a variable.
272 * Example: BOM template provides ${DNP} as a field, but they symbol doesn't have the field. */
273 wxString getFieldShownText( const SCH_REFERENCE& aRef, const wxString& aFieldName );
274
275 void Sort();
276
279 void updateDataStoreSymbolField( const SCH_SYMBOL& aSymbol, const wxString& aFieldName );
280
281protected:
286 wxString m_filter;
293
294 std::vector<DATA_MODEL_COL> m_cols;
295 std::vector<DATA_MODEL_ROW> m_rows;
296
297 // Data store
298 // The data model is fundamentally m_componentRefs X m_fieldNames.
299 // A map of compID : fieldSet, where fieldSet is a map of fieldName : fieldValue
300 std::map<KIID, std::map<wxString, wxString>> m_dataStore;
301};
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)