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 The 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_FIELD_COUNT; ++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( SCH_FIELD* libField : libFields )
92 {
93 if( !libField->IsMandatory() )
94 fieldNames.insert( libField->GetName() );
95 }
96
97 libFields.clear(); // flattenedPart is about to go out of scope...
98
99 // Load non-mandatory fields from the editor symbol
100 m_symbol->GetFields( libFields );
101
102 for( SCH_FIELD* libField : libFields )
103 {
104 if( !libField->IsMandatory() )
105 fieldNames.insert( libField->GetName() );
106 }
107
108 libFields.clear();
109
110 // Update the listbox widget
111 for( unsigned i = m_fieldsBox->GetCount() - 1; i >= MANDATORY_FIELD_COUNT; --i )
112 m_fieldsBox->Delete( i );
113
114 for( const wxString& fieldName : fieldNames )
115 m_fieldsBox->Append( fieldName );
116
117 for( unsigned i = MANDATORY_FIELD_COUNT; i < m_fieldsBox->GetCount(); ++i )
118 m_fieldsBox->Check( i, true );
119}
120
121
123{
124 for( unsigned i = 0; i < m_fieldsBox->GetCount(); ++i )
125 m_fieldsBox->Check( i, aCheck );
126}
127
128
130{
131 wxBusyCursor dummy;
132 SCH_COMMIT commit( m_editFrame );
133
134 commit.Modify( m_symbol );
135
136 // Create the set of fields to be updated
137 m_updateFields.clear();
138
139 for( unsigned i = 0; i < m_fieldsBox->GetCount(); ++i )
140 {
141 if( m_fieldsBox->IsChecked( i ) )
142 m_updateFields.insert( m_fieldsBox->GetString( i ) );
143 }
144
145 std::unique_ptr<LIB_SYMBOL> flattenedParent = m_symbol->GetParent().lock()->Flatten();
146
147 bool removeExtras = m_removeExtraBox->GetValue();
148 bool resetVis = m_resetFieldVisibilities->GetValue();
149 bool resetEffects = m_resetFieldEffects->GetValue();
150 bool resetPositions = m_resetFieldPositions->GetValue();
151
152 std::vector<SCH_FIELD> fields;
153 std::vector<SCH_FIELD> result;
154 m_symbol->CopyFields( fields );
155
156 for( SCH_FIELD& field : fields )
157 {
158 bool copy = true;
159 SCH_FIELD* parentField = nullptr;
160
161 if( alg::contains( m_updateFields, field.GetName() ) )
162 {
163 parentField = flattenedParent->FindField( field.GetName() );
164
165 if( parentField )
166 {
167 bool resetText = parentField->GetText().IsEmpty() ? m_resetEmptyFields->GetValue()
168 : m_resetFieldText->GetValue();
169
170 if( resetText )
171 field.SetText( parentField->GetText() );
172
173 if( resetVis )
174 {
175 field.SetVisible( parentField->IsVisible() );
176 field.SetNameShown( parentField->IsNameShown() );
177 }
178
179 if( resetEffects )
180 {
181 // Careful: the visible bit and position are also set by SetAttributes()
182 bool visible = field.IsVisible();
183 VECTOR2I pos = field.GetPosition();
184
185 field.SetAttributes( *parentField );
186
187 field.SetVisible( visible );
188 field.SetPosition( pos );
189 }
190
191 if( resetPositions )
192 field.SetTextPos( parentField->GetTextPos() );
193 }
194 else if( removeExtras )
195 {
196 copy = false;
197 }
198 }
199
200 if( copy )
201 result.emplace_back( std::move( field ) );
202 }
203
204 std::vector<SCH_FIELD*> parentFields;
205 int idx = result.size();
206
207 flattenedParent->GetFields( parentFields );
208
209 for( SCH_FIELD* parentField : parentFields )
210 {
211 if( !alg::contains( m_updateFields, parentField->GetName() ) )
212 continue;
213
214 if( !m_symbol->FindField( parentField->GetName() ) )
215 {
216 result.emplace_back( m_symbol, idx++ );
217 SCH_FIELD* newField = &result.back();
218
219 newField->SetName( parentField->GetCanonicalName() );
220 newField->SetText( parentField->GetText() );
221 newField->SetAttributes( *parentField ); // Includes visible bit and position
222 }
223 }
224
225 m_symbol->SetFields( result );
226
227 commit.Push( _( "Update Symbol Fields" ) );
229
230 wxPostEvent( this, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK ) );
231}
232
233
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Modify a given item in the model.
Definition: commit.h:108
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 checkAll(bool aCheck)
Select or deselect all fields in the listbox widget.
void onOkButtonClicked(wxCommandEvent &aEvent) override
DIALOG_UPDATE_SYMBOL_FIELDS(SYMBOL_EDIT_FRAME *aParent, LIB_SYMBOL *aSymbol)
std::set< wxString > m_updateFields
Set of field names that should have values updated.
const VECTOR2I & GetTextPos() const
Definition: eda_text.h:260
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:98
virtual bool IsVisible() const
Definition: eda_text.h:174
void SetAttributes(const EDA_TEXT &aSrc, bool aSetPosition=true)
Set the text attributes from another instance.
Definition: eda_text.cpp:426
Define a library symbol object.
Definition: lib_symbol.h:84
void SetFields(const std::vector< SCH_FIELD > &aFieldsList)
Overwrite all the existing fields in this symbol with fields supplied in aFieldsList.
void GetFields(std::vector< SCH_FIELD * > &aList, bool aVisibleOnly=false) override
Return a list of fields within this symbol.
void CopyFields(std::vector< SCH_FIELD > &aList)
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:117
virtual void Push(const wxString &aMessage=wxT("A commit"), int aCommitFlags=0) override
Execute the changes.
Definition: sch_commit.cpp:433
Instances are attached to a symbol or sheet and provide a place for the symbol's value,...
Definition: sch_field.h:53
bool IsNameShown() const
Definition: sch_field.h:219
void SetName(const wxString &aName)
Definition: sch_field.cpp:1204
void SetText(const wxString &aText) override
Definition: sch_field.cpp:1214
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)
wxString GetDefaultFieldName(int aFieldNdx, bool aTranslateForHI)
Return a default symbol field name for field aFieldNdx for all components.
#define DO_TRANSLATE
@ MANDATORY_FIELD_COUNT
The first 5 are mandatory, and must be instantiated in SCH_COMPONENT, LIB_PART, and FOOTPRINT constru...