You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

293 lines
10 KiB

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