KiCad PCB EDA Suite
Loading...
Searching...
No Matches
fields_data_model.h
Go to the documentation of this file.
2#include <wx/grid.h>
3
4// The field name in the data model (translated)
5#define DISPLAY_NAME_COLUMN 0
6// The field name's label for exporting (CSV, etc.)
7#define LABEL_COLUMN 1
8#define SHOW_FIELD_COLUMN 2
9#define GROUP_BY_COLUMN 3
10// The internal field name (untranslated)
11#define FIELD_NAME_COLUMN 4
12
13struct BOM_FIELD;
14struct BOM_PRESET;
15struct BOM_FMT_PRESET;
16
18{
24};
25
26
28{
29 DATA_MODEL_ROW( const SCH_REFERENCE& aFirstReference, GROUP_TYPE aType )
30 {
31 m_ItemNumber = 0;
32 m_Refs.push_back( aFirstReference );
33 m_Flag = aType;
34 }
35
38 std::vector<SCH_REFERENCE> m_Refs;
39};
40
41
43{
44 wxString m_fieldName;
45 wxString m_label;
47 bool m_show;
48 bool m_group;
49};
50
51
52class FIELDS_EDITOR_GRID_DATA_MODEL : public wxGridTableBase
53{
54public:
55 enum SCOPE : int
56 {
60 };
61
63 m_symbolsList( aSymbolsList ), m_edited( false ), m_sortColumn( 0 ),
65 m_excludeDNP( false ), m_includeExcluded( false ), m_rebuildsEnabled( true )
66 {
68 }
69
70 static const wxString QUANTITY_VARIABLE;
71 static const wxString ITEM_NUMBER_VARIABLE;
72
73 void AddColumn( const wxString& aFieldName, const wxString& aLabel, bool aAddedByUser );
74 void RemoveColumn( int aCol );
75 void RenameColumn( int aCol, const wxString& newName );
76 void MoveColumn( int aCol, int aNewPos )
77 {
78 wxCHECK_RET( aCol >= 0 && aCol < (int) m_cols.size(), "Invalid Column Number" );
79 std::swap( m_cols[aCol], m_cols[aNewPos] );
80 }
81
82 int GetNumberRows() override { return (int) m_rows.size(); }
83 int GetNumberCols() override { return (int) m_cols.size(); }
84
85 void SetColLabelValue( int aCol, const wxString& aLabel ) override
86 {
87 wxCHECK_RET( aCol >= 0 && aCol < (int) m_cols.size(), "Invalid Column Number" );
88 m_cols[aCol].m_label = aLabel;
89 }
90
91
92 wxString GetColLabelValue( int aCol ) override
93 {
94 wxCHECK( aCol >= 0 && aCol < (int) m_cols.size(), wxString() );
95 return m_cols[aCol].m_label;
96 }
97
98 wxString GetColFieldName( int aCol )
99 {
100 wxCHECK( aCol >= 0 && aCol < (int) m_cols.size(), wxString() );
101 return m_cols[aCol].m_fieldName;
102 }
103
104 int GetFieldNameCol( wxString aFieldName );
105
106 const std::vector<BOM_FIELD> GetFieldsOrdered();
107 void SetFieldsOrder( const std::vector<wxString>& aNewOrder );
108
109 bool IsEmptyCell( int aRow, int aCol ) override
110 {
111 return false; // don't allow adjacent cell overflow, even if we are actually empty
112 }
113
114 wxString GetValue( int aRow, int aCol ) override;
115 wxString GetValue( const DATA_MODEL_ROW& group, int aCol,
116 const wxString& refDelimiter = wxT( ", " ),
117 const wxString& refRangDelimiter = wxT( "-" ),
118 bool resolveVars = false );
119 wxString GetExportValue( int aRow, int aCol, const wxString& refDelimiter,
120 const wxString& refRangeDelimiter )
121 {
122 return GetValue( m_rows[aRow], aCol, refDelimiter, refRangeDelimiter, true );
123 }
124 void SetValue( int aRow, int aCol, const wxString& aValue ) override;
125
126 GROUP_TYPE GetRowFlags( int aRow ) { return m_rows[aRow].m_Flag; }
127
128 std::vector<SCH_REFERENCE> GetRowReferences( int aRow ) const
129 {
130 wxCHECK( aRow >= 0 && aRow < (int) m_rows.size(), std::vector<SCH_REFERENCE>() );
131 return m_rows[aRow].m_Refs;
132 }
133
134 bool ColIsReference( int aCol );
135 bool ColIsValue( int aCol );
136 bool ColIsQuantity( int aCol );
137 bool ColIsItemNumber( int aCol );
138 bool ColIsAttribute( int aCol );
139
140 void SetSorting( int aCol, bool ascending )
141 {
142 wxCHECK_RET( aCol >= 0 && aCol < (int) m_cols.size(), "Invalid Column Number" );
143 m_sortColumn = aCol;
144 m_sortAscending = ascending;
145 }
146 int GetSortCol() { return m_sortColumn; }
147 bool GetSortAsc() { return m_sortAscending; }
148
149 // These are used to disable the RebuildRows functionality while we're generating
150 // lots of events in the UI, e.g. applying a BOM preset, that would thrash the grid.
151 void EnableRebuilds();
152 void DisableRebuilds();
153 void RebuildRows();
154
155 void ExpandRow( int aRow );
156 void CollapseRow( int aRow );
157 void ExpandCollapseRow( int aRow );
158 void CollapseForSort();
159 void ExpandAfterSort();
160
161 void ApplyData( std::function<void( SCH_SYMBOL&, SCH_SHEET_PATH& )> symbolChangeHandler );
162
163 bool IsEdited() { return m_edited; }
164
165 int GetDataWidth( int aCol );
166
167 void SetFilter( const wxString& aFilter ) { m_filter = aFilter; }
168 const wxString& GetFilter() { return m_filter; }
169
170 void SetScope( SCOPE aScope ) { m_scope = aScope; }
171 SCOPE GetScope() { return m_scope; }
172
173 void SetPath( const SCH_SHEET_PATH& aPath ) { m_path = aPath; }
174 const SCH_SHEET_PATH& GetPath() { return m_path; }
175
178
179 /* These contradictorily named functions force including symbols that
180 * have the Exclude from BOM check box ticked. This is needed so we can view
181 * these parts in the symbol fields table dialog, while also excluding from the
182 * BOM export */
183 void SetIncludeExcludedFromBOM( bool include ) { m_includeExcluded = include; }
185
186 void SetExcludeDNP( bool exclude ) { m_excludeDNP = exclude; }
187 bool GetExcludeDNP() { return m_excludeDNP; }
188
189 void SetGroupColumn( int aCol, bool group )
190 {
191 wxCHECK_RET( aCol >= 0 && aCol < (int) m_cols.size(), "Invalid Column Number" );
192 m_cols[aCol].m_group = group;
193 }
194 bool GetGroupColumn( int aCol )
195 {
196 wxCHECK_MSG( aCol >= 0 && aCol < (int) m_cols.size(), false, "Invalid Column Number" );
197 return m_cols[aCol].m_group;
198 }
199
200 void SetShowColumn( int aCol, bool show )
201 {
202 wxCHECK_RET( aCol >= 0 && aCol < (int) m_cols.size(), "Invalid Column Number" );
203 m_cols[aCol].m_show = show;
204 }
205 bool GetShowColumn( int aCol )
206 {
207 wxCHECK_MSG( aCol >= 0 && aCol < (int) m_cols.size(), false, "Invalid Column Number" );
208 return m_cols[aCol].m_show;
209 }
210
211 void ApplyBomPreset( const BOM_PRESET& preset );
213 wxString Export( const BOM_FMT_PRESET& settings );
214
215 void AddReferences( const SCH_REFERENCE_LIST& aRefs );
216 void RemoveReferences( const SCH_REFERENCE_LIST& aRefs );
217 void RemoveSymbol( const SCH_SYMBOL& aSymbol );
218 void UpdateReferences( const SCH_REFERENCE_LIST& aRefs );
219
220
221private:
222 static bool cmp( const DATA_MODEL_ROW& lhGroup, const DATA_MODEL_ROW& rhGroup,
223 FIELDS_EDITOR_GRID_DATA_MODEL* dataModel, int sortCol, bool ascending );
224 bool unitMatch( const SCH_REFERENCE& lhRef, const SCH_REFERENCE& rhRef );
225 bool groupMatch( const SCH_REFERENCE& lhRef, const SCH_REFERENCE& rhRef );
226
227 // Helper functions to deal with translating wxGrid values to and from
228 // named field values like ${DNP}
229 bool isAttribute( const wxString& aFieldName );
230 wxString getAttributeValue( const SCH_SYMBOL&, const wxString& aAttributeName );
231 void setAttributeValue( SCH_SYMBOL& aSymbol, const wxString& aAttributeName,
232 const wxString& aValue );
233
234 /* Helper function to get the resolved field value.
235 * Handles symbols that are missing fields that would have a variable
236 * in their value because their name is the same as a variable.
237 * Example: BOM template provides ${DNP} as a field, but they symbol doesn't have the field. */
238 wxString getFieldShownText( const SCH_REFERENCE& aRef, const wxString& aFieldName );
239
240 void Sort();
241
244 void updateDataStoreSymbolField( const SCH_SYMBOL& aSymbol, const wxString& aFieldName );
245
246
247protected:
252 wxString m_filter;
259
260 std::vector<DATA_MODEL_COL> m_cols;
261 std::vector<DATA_MODEL_ROW> m_rows;
262
263 // Data store
264 // The data model is fundamentally m_componentRefs X m_fieldNames.
265 // A map of compID : fieldSet, where fieldSet is a map of fieldName : fieldValue
266 std::map<KIID, std::map<wxString, wxString>> m_dataStore;
267};
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:81
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)