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_KEYWORDS;
53 static const wxString SYMBOL_IS_POWER;
54 static const wxString SYMBOL_IS_LOCAL_POWER;
55
56 void CreateDerivedSymbolImmediate( int aRow, int aCol, wxString& aNewSymbolName );
57
58 wxString GetTypeName( int row, int col ) override;
59 void SetValue( int aRow, int aCol, const wxString& aValue ) override;
60
61 wxGridCellAttr* GetAttr( int row, int col, wxGridCellAttr::wxAttrKind kind ) override;
62
63 bool ColIsItemIdentifier( int aCol ) const override;
64
65 const LIB_SYMBOL* GetSymbolForRow( int aRow )
66 {
67 wxCHECK( aRow >= 0 && aRow < (int) m_rows.size(), nullptr );
68 return m_rows[aRow].m_items[0];
69 }
70
71 void GetSymbolNames( wxArrayString& aList, SYMBOL_NAME_FILTER aFilter )
72 {
73 aList.Clear();
74
75 for( const LIB_SYMBOL* symbol : m_symbolsList )
76 {
77 if( ( symbol->IsDerived() && ( aFilter == SYMBOL_NAME_FILTER::ROOT_ONLY ) )
78 || ( symbol->IsRoot() && ( aFilter == SYMBOL_NAME_FILTER::DERIVED_ONLY ) ) )
79 {
80 continue;
81 }
82
83 aList.Add( UnescapeString( symbol->GetName() ) );
84 }
85 }
86
87 void RebuildRows() override;
88
89 void SetScope( SCOPE aScope ) { m_scope = aScope; }
90 SCOPE GetScope() { return m_scope; }
91
92 void SetRelatedSymbolRoot( const wxString& aRootSymbolName ) { m_relatedSymbolRoot = aRootSymbolName; }
93
94 void ApplyData( std::function<void( LIB_SYMBOL* )> symbolChangeHandler,
95 std::function<void()> postApplyHandler = nullptr );
96
97 bool ValidateReferences( wxString& aSymbolName, wxString& aErrorMessage ) const;
98
100 std::vector<std::pair<LIB_SYMBOL*, wxString>> GetAndClearCreatedDerivedSymbols()
101 {
102 auto result = std::move( m_createdDerivedSymbols );
104 return result;
105 }
106
107 bool IsRowSingleSymbol( int aRow )
108 {
109 wxCHECK_MSG( aRow >= 0 && aRow < (int) m_rows.size(), false, "Invalid Row Number" );
110 return m_rows[aRow].m_state == ROW_STATE::NON_EXPANDABLE
111 || m_rows[aRow].m_state == ROW_STATE::EXPANDED_CHILD;
112 }
113
114 bool IsCellReadOnly( int aRow, int aCol ) override;
115
116private:
117 bool unitMatch( LIB_SYMBOL* const& lhItem, LIB_SYMBOL* const& rhItem ) override;
118
119 bool fieldIsAttribute( const wxString& aFieldName ) const override;
120 bool fieldIsItemProperty( const wxString& aFieldName ) const override;
121
122 wxString getAttributeValue( const LIB_SYMBOL*, const wxString& aAttributeName );
123 void setAttributeValue( LIB_SYMBOL* aSymbol, const wxString& aAttributeName, const wxString& aValue );
124 wxString getAttributeResolvedValue( const wxString& aFieldName, bool aValue ) const override;
125
126 bool isPowerSymbolControlledField( const wxString& aFieldName ) const;
127 bool getStoredPowerSymbolValue( LIB_SYMBOL* aSymbol ) const;
128 void setPowerSymbolDefaults( LIB_SYMBOL* aSymbol );
129
130 wxString getFieldResolvedLiveValue( LIB_SYMBOL* const& aSymbol, const wxString& aFieldName ) override;
131 wxString resolveTextVars( LIB_SYMBOL* const& aSymbol, const wxString& aText ) override;
132
133 void createActualDerivedSymbol( const LIB_SYMBOL* aParentSymbol, const wxString& aNewSymbolName,
134 const KIID& aNewSymbolUuid );
135
136 bool getLiveFieldValue( LIB_SYMBOL* const& aSymbol, const wxString& aFieldName, wxString& aValue ) override;
137 std::vector<LIB_SYMBOL*> getAllItems() const override;
138
139 KIID_PATH getDataStoreKey( LIB_SYMBOL* const& aItem ) const override;
140 wxString getItemIdentifier( LIB_SYMBOL* const& aItem ) const override;
141
142protected:
143 std::vector<LIB_SYMBOL*> m_symbolsList;
146
147 // Track newly created derived symbols for library manager integration
148 std::vector<std::pair<LIB_SYMBOL*, wxString>> m_createdDerivedSymbols; // symbol, library name
149};
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)
bool ValidateReferences(wxString &aSymbolName, wxString &aErrorMessage) const
Define a library symbol object.
Definition lib_symbol.h:119
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.