KiCad PCB EDA Suite
Loading...
Searching...
No Matches
lib_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) 2024 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
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include <sch_reference_list.h>
23#include <wx/grid.h>
24#include <wx/arrstr.h>
25#include <widgets/wx_grid.h>
26
28
30
31
33
34
36{
37public:
43
44 LIB_FIELDS_EDITOR_GRID_DATA_MODEL( const std::vector<LIB_SYMBOL*>& aSymbolsList ) :
45 m_symbolsList( aSymbolsList ),
47 {
48 m_includeExcluded = true;
49 }
50
51 static const wxString SYMBOL_NAME;
52 static const wxString SYMBOL_PARENT;
53 static const wxString SYMBOL_ROOT;
54 static const wxString SYMBOL_KEYWORDS;
55 static const wxString SYMBOL_IS_POWER;
56 static const wxString SYMBOL_IS_LOCAL_POWER;
57
58 void CreateDerivedSymbolImmediate( int aRow, int aCol, wxString& aNewSymbolName );
59
60 wxString GetTypeName( int row, int col ) override;
61 void SetValue( int aRow, int aCol, const wxString& aValue ) override;
62
63 bool CanUseParentValue( int aRow, int aCol );
64 void UseParentValue( int aRow, int aCol );
65
66 wxGridCellAttr* GetAttr( int row, int col, wxGridCellAttr::wxAttrKind kind ) override;
67
68 bool ColIsItemIdentifier( int aCol ) const override;
69
70 const LIB_SYMBOL* GetSymbolForRow( int aRow )
71 {
72 wxCHECK( aRow >= 0 && aRow < (int) m_rows.size(), nullptr );
73 return m_rows[aRow].m_items[0];
74 }
75
76 void GetSymbolNames( wxArrayString& aList, SYMBOL_NAME_FILTER aFilter )
77 {
78 aList.Clear();
79
80 for( const LIB_SYMBOL* symbol : m_symbolsList )
81 {
82 if( ( symbol->IsDerived() && ( aFilter == SYMBOL_NAME_FILTER::ROOT_ONLY ) )
83 || ( symbol->IsRoot() && ( aFilter == SYMBOL_NAME_FILTER::DERIVED_ONLY ) ) )
84 {
85 continue;
86 }
87
88 aList.Add( UnescapeString( symbol->GetName() ) );
89 }
90 }
91
92 void RebuildRows() override;
93
94 void SetScope( SCOPE aScope ) { m_scope = aScope; }
95 SCOPE GetScope() { return m_scope; }
96
97 void SetRelatedSymbolRoot( const wxString& aRootSymbolName ) { m_relatedSymbolRoot = aRootSymbolName; }
98
99 void ApplyData( std::function<void( LIB_SYMBOL* )> symbolChangeHandler,
100 std::function<void()> postApplyHandler = nullptr );
101
102 bool ValidateReferences( wxString& aSymbolName, wxString& aErrorMessage ) const;
103
105 std::vector<std::pair<LIB_SYMBOL*, wxString>> GetAndClearCreatedDerivedSymbols()
106 {
107 auto result = std::move( m_createdDerivedSymbols );
109 return result;
110 }
111
112 bool IsRowSingleSymbol( int aRow )
113 {
114 wxCHECK_MSG( aRow >= 0 && aRow < (int) m_rows.size(), false, "Invalid Row Number" );
115 return m_rows[aRow].GetCellItems().size() == 1;
116 }
117
118 bool IsCellReadOnly( int aRow, int aCol ) override;
119
120private:
121 bool unitMatch( LIB_SYMBOL* const& lhItem, LIB_SYMBOL* const& rhItem ) override;
122
123 bool fieldIsAttribute( const wxString& aFieldName ) const override;
124 bool fieldIsItemProperty( const wxString& aFieldName ) const override;
125
126 wxString getAttributeValue( const LIB_SYMBOL*, const wxString& aAttributeName );
127 void setAttributeValue( LIB_SYMBOL* aSymbol, const wxString& aAttributeName, const wxString& aValue );
128 wxString getAttributeResolvedValue( const wxString& aFieldName, bool aValue ) const override;
129
130 bool isPowerSymbolControlledField( const wxString& aFieldName ) const;
131 bool getStoredPowerSymbolValue( LIB_SYMBOL* aSymbol ) const;
132 void setPowerSymbolDefaults( LIB_SYMBOL* aSymbol );
133
134 wxString getFieldResolvedLiveValue( LIB_SYMBOL* const& aSymbol, const wxString& aFieldName ) override;
135 wxString resolveTextVars( LIB_SYMBOL* const& aSymbol, const wxString& aText ) override;
136
137 void createActualDerivedSymbol( const LIB_SYMBOL* aParentSymbol, const wxString& aNewSymbolName,
138 const KIID& aNewSymbolUuid );
139
140 bool getLiveFieldValue( LIB_SYMBOL* const& aSymbol, const wxString& aFieldName, wxString& aValue ) override;
141 void getEffectiveFieldValue( LIB_SYMBOL* const& aSymbol, const wxString& aFieldName,
142 wxString& aValue ) const override;
143 std::vector<LIB_SYMBOL*> getAllItems() const override;
144
145 KIID_PATH getDataStoreKey( LIB_SYMBOL* const& aItem ) const override;
146 wxString getItemIdentifier( LIB_SYMBOL* const& aItem ) const override;
147
148protected:
149 std::vector<LIB_SYMBOL*> m_symbolsList;
152
153 // Track newly created derived symbols for library manager integration
154 std::vector<std::pair<LIB_SYMBOL*, wxString>> m_createdDerivedSymbols; // symbol, library name
155};
The rest of the generic fields data model stuff, primarily around row functionality.
std::vector< DATA_MODEL_ROW< LIB_SYMBOL * > > m_rows
Definition kiid.h:46
bool fieldIsItemProperty(const wxString &aFieldName) const override
void SetValue(int aRow, int aCol, const wxString &aValue) override
wxString getAttributeResolvedValue(const wxString &aFieldName, bool aValue) const override
wxString getAttributeValue(const LIB_SYMBOL *, const wxString &aAttributeName)
std::vector< std::pair< LIB_SYMBOL *, wxString > > m_createdDerivedSymbols
std::vector< LIB_SYMBOL * > getAllItems() const override
bool IsCellReadOnly(int aRow, int aCol) override
KIID_PATH getDataStoreKey(LIB_SYMBOL *const &aItem) const override
wxGridCellAttr * GetAttr(int row, int col, wxGridCellAttr::wxAttrKind kind) override
void SetRelatedSymbolRoot(const wxString &aRootSymbolName)
LIB_FIELDS_EDITOR_GRID_DATA_MODEL(const std::vector< LIB_SYMBOL * > &aSymbolsList)
void GetSymbolNames(wxArrayString &aList, SYMBOL_NAME_FILTER aFilter)
bool ColIsItemIdentifier(int aCol) const override
Reference for symbol/fields tables, lib_id for lib tables.
void CreateDerivedSymbolImmediate(int aRow, int aCol, wxString &aNewSymbolName)
bool getStoredPowerSymbolValue(LIB_SYMBOL *aSymbol) const
wxString getFieldResolvedLiveValue(LIB_SYMBOL *const &aSymbol, const wxString &aFieldName) override
Explicitly bypasses the data store's field values and retries them from the item's current field valu...
void createActualDerivedSymbol(const LIB_SYMBOL *aParentSymbol, const wxString &aNewSymbolName, const KIID &aNewSymbolUuid)
bool fieldIsAttribute(const wxString &aFieldName) const override
void ApplyData(std::function< void(LIB_SYMBOL *)> symbolChangeHandler, std::function< void()> postApplyHandler=nullptr)
std::vector< LIB_SYMBOL * > m_symbolsList
void setAttributeValue(LIB_SYMBOL *aSymbol, const wxString &aAttributeName, const wxString &aValue)
bool unitMatch(LIB_SYMBOL *const &lhItem, LIB_SYMBOL *const &rhItem) override
bool getLiveFieldValue(LIB_SYMBOL *const &aSymbol, const wxString &aFieldName, wxString &aValue) override
Gets the current value of the field from the live item, e.g.
wxString getItemIdentifier(LIB_SYMBOL *const &aItem) const override
std::vector< std::pair< LIB_SYMBOL *, wxString > > GetAndClearCreatedDerivedSymbols()
Get and clear the list of newly created derived symbols for library manager processing.
wxString GetTypeName(int row, int col) override
bool isPowerSymbolControlledField(const wxString &aFieldName) const
const LIB_SYMBOL * GetSymbolForRow(int aRow)
wxString resolveTextVars(LIB_SYMBOL *const &aSymbol, const wxString &aText) override
void setPowerSymbolDefaults(LIB_SYMBOL *aSymbol)
void getEffectiveFieldValue(LIB_SYMBOL *const &aSymbol, const wxString &aFieldName, wxString &aValue) const override
bool ValidateReferences(wxString &aSymbolName, wxString &aErrorMessage) const
Define a library symbol object.
Definition lib_symbol.h:114
DATA_MODEL_ROW< LIB_SYMBOL * > LIB_FIELDS_TABLE_DATA_MODEL_ROW
wxString UnescapeString(const wxString &aSource)
wxString result
Test unit parsing edge cases and error handling.