KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_update_symbol_fields.cpp
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) 2020 CERN
5 * Copyright (C) 2021 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
21#include <core/kicad_algo.h>
23#include <lib_symbol.h>
24#include <symbol_edit_frame.h>
25#include <sch_commit.h>
26#include <template_fieldnames.h>
27#include <string_utils.h>
28
29
36
37
39 LIB_SYMBOL* aSymbol ) :
41 m_editFrame( aParent ),
42 m_symbol( aSymbol)
43{
44 wxASSERT( aParent );
45 wxASSERT( aSymbol );
46
47 m_parentSymbolReadOnly->SetValue( UnescapeString( m_symbol->GetParent().lock()->GetName() ) );
48
49 for( int i = 0; i < MANDATORY_FIELDS; ++i )
50 {
52 m_fieldsBox->Check( i, true );
53 }
54
56
63
65
66 // Now all widgets have the size fixed, call FinishDialogSettings
68}
69
70
72{
79}
80
81
83{
84 // Load non-mandatory fields from the parent part
85 std::vector<SCH_FIELD*> libFields;
86 std::set<wxString> fieldNames;
87 std::unique_ptr<LIB_SYMBOL> flattenedParent = m_symbol->GetParent().lock()->Flatten();
88
89 flattenedParent->GetFields( libFields );
90
91 for( unsigned i = MANDATORY_FIELDS; i < libFields.size(); ++i )
92 fieldNames.insert( libFields[i]->GetName() );
93
94 libFields.clear(); // flattenedPart is about to go out of scope...
95
96 // Load non-mandatory fields from the editor symbol
97 m_symbol->GetFields( libFields );
98
99 for( unsigned i = MANDATORY_FIELDS; i < libFields.size(); ++i )
100 fieldNames.insert( libFields[i]->GetName() );
101
102 libFields.clear();
103
104 // Update the listbox widget
105 for( unsigned i = m_fieldsBox->GetCount() - 1; i >= MANDATORY_FIELDS; --i )
106 m_fieldsBox->Delete( i );
107
108 for( const wxString& fieldName : fieldNames )
109 m_fieldsBox->Append( fieldName );
110
111 for( unsigned i = MANDATORY_FIELDS; i < m_fieldsBox->GetCount(); ++i )
112 m_fieldsBox->Check( i, true );
113}
114
115
117{
118 for( unsigned i = 0; i < m_fieldsBox->GetCount(); ++i )
119 m_fieldsBox->Check( i, aCheck );
120}
121
122
124{
125 wxBusyCursor dummy;
126 SCH_COMMIT commit( m_editFrame );
127
128 commit.Modify( m_symbol );
129
130 // Create the set of fields to be updated
131 m_updateFields.clear();
132
133 for( unsigned i = 0; i < m_fieldsBox->GetCount(); ++i )
134 {
135 if( m_fieldsBox->IsChecked( i ) )
136 m_updateFields.insert( m_fieldsBox->GetString( i ) );
137 }
138
139 std::unique_ptr<LIB_SYMBOL> flattenedParent = m_symbol->GetParent().lock()->Flatten();
140
141 bool removeExtras = m_removeExtraBox->GetValue();
142 bool resetVis = m_resetFieldVisibilities->GetValue();
143 bool resetEffects = m_resetFieldEffects->GetValue();
144 bool resetPositions = m_resetFieldPositions->GetValue();
145
146 std::vector<SCH_FIELD> fields;
147 std::vector<SCH_FIELD> result;
148 m_symbol->GetFields( fields );
149
150 for( SCH_FIELD& field : fields )
151 {
152 bool copy = true;
153 SCH_FIELD* parentField = nullptr;
154
155 if( alg::contains( m_updateFields, field.GetName() ) )
156 {
157 parentField = flattenedParent->FindField( field.GetName() );
158
159 if( parentField )
160 {
161 bool resetText = parentField->GetText().IsEmpty() ? m_resetEmptyFields->GetValue()
162 : m_resetFieldText->GetValue();
163
164 if( resetText )
165 field.SetText( parentField->GetText() );
166
167 if( resetVis )
168 {
169 field.SetVisible( parentField->IsVisible() );
170 field.SetNameShown( parentField->IsNameShown() );
171 }
172
173 if( resetEffects )
174 {
175 // Careful: the visible bit and position are also set by SetAttributes()
176 bool visible = field.IsVisible();
177 VECTOR2I pos = field.GetPosition();
178
179 field.SetAttributes( *parentField );
180
181 field.SetVisible( visible );
182 field.SetPosition( pos );
183 }
184
185 if( resetPositions )
186 field.SetTextPos( parentField->GetTextPos() );
187 }
188 else if( removeExtras )
189 {
190 copy = false;
191 }
192 }
193
194 if( copy )
195 result.emplace_back( std::move( field ) );
196 }
197
198 std::vector<SCH_FIELD*> parentFields;
199 int idx = result.size();
200
201 flattenedParent->GetFields( parentFields );
202
203 for( SCH_FIELD* parentField : parentFields )
204 {
205 if( !alg::contains( m_updateFields, parentField->GetName() ) )
206 continue;
207
208 if( !m_symbol->FindField( parentField->GetName() ) )
209 {
210 result.emplace_back( m_symbol, idx++ );
211 SCH_FIELD* newField = &result.back();
212
213 newField->SetName( parentField->GetCanonicalName() );
214 newField->SetText( parentField->GetText() );
215 newField->SetAttributes( *parentField ); // Includes visible bit and position
216 }
217 }
218
219 m_symbol->SetFields( result );
220
221 commit.Push( _( "Update Symbol Fields" ) );
223
224 wxPostEvent( this, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK ) );
225}
226
227
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Create an undo entry for an item that has been already modified.
Definition: commit.h:105
void SetupStandardButtons(std::map< int, wxString > aLabels={})
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
Class DIALOG_UPDATE_SYMBOL_FIELDS_BASE.
void onOkButtonClicked(wxCommandEvent &aEvent) override
DIALOG_UPDATE_SYMBOL_FIELDS(SYMBOL_EDIT_FRAME *aParent, LIB_SYMBOL *aSymbol)
LIB_SYMBOL * m_symbol
Set of field names that should have values updated.
const VECTOR2I & GetTextPos() const
Definition: eda_text.h:230
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:94
virtual bool IsVisible() const
Definition: eda_text.h:147
void SetAttributes(const EDA_TEXT &aSrc, bool aSetPosition=true)
Set the text attributes from another instance.
Definition: eda_text.cpp:291
Define a library symbol object.
Definition: lib_symbol.h:77
void GetFields(std::vector< SCH_FIELD * > &aList)
Return a list of fields within this symbol.
void SetFields(const std::vector< SCH_FIELD > &aFieldsList)
Overwrite all the existing fields in this symbol with fields supplied in aFieldsList.
SCH_FIELD * FindField(const wxString &aFieldName, bool aCaseInsensitive=false)
Find a field within this symbol matching aFieldName and returns it or NULL if not found.
LIB_SYMBOL_REF & GetParent()
Definition: lib_symbol.h:105
virtual void Push(const wxString &aMessage=wxT("A commit"), int aCommitFlags=0) override
Revert the commit by restoring the modified items state.
Definition: sch_commit.cpp:406
Instances are attached to a symbol or sheet and provide a place for the symbol's value,...
Definition: sch_field.h:51
bool IsNameShown() const
Definition: sch_field.h:208
void SetName(const wxString &aName)
Definition: sch_field.cpp:1179
void SetText(const wxString &aText) override
Definition: sch_field.cpp:1189
The symbol library editor main window.
bool g_removeExtraLibFields
bool g_resetLibFieldEffects
bool g_resetLibFieldVisibilities
bool g_resetLibFieldText
bool g_resetLibFieldPositions
bool g_resetEmptyLibFields
#define _(s)
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition: kicad_algo.h:100
std::vector< FAB_LAYER_COLOR > dummy
wxString UnescapeString(const wxString &aSource)
static const wxString GetDefaultFieldName(int aFieldNdx, bool aTranslateForHI=false)
Return a default symbol field name for field aFieldNdx for all components.
#define DO_TRANSLATE
@ MANDATORY_FIELDS
The first 5 are mandatory, and must be instantiated in SCH_COMPONENT and LIB_PART constructors.