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
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
22#include <lib_symbol.h>
23#include <symbol_edit_frame.h>
24#include <sch_commit.h>
25#include <template_fieldnames.h>
26#include <string_utils.h>
27
28
31 m_editFrame( aParent ),
32 m_symbol( aSymbol)
33{
34 wxASSERT( aParent );
35 wxASSERT( aSymbol );
36
37 if( std::shared_ptr<LIB_SYMBOL> parent = m_symbol->GetParent().lock() )
38 m_parentSymbolReadOnly->SetValue( UnescapeString( parent->GetName() ) );
39
40 for( FIELD_T fieldId : MANDATORY_FIELDS )
41 {
42 m_mandatoryFieldListIndexes[fieldId] = m_fieldsBox->GetCount();
43 m_fieldsBox->Append( GetDefaultFieldName( fieldId, DO_TRANSLATE ) );
44 m_fieldsBox->Check( m_fieldsBox->GetCount() - 1, true );
45 }
46
48
50
51 // Now all widgets have the size fixed, call FinishDialogSettings
53}
54
55
57{
58 // Load non-mandatory fields from the parent part
59 std::vector<SCH_FIELD*> libFields;
60 std::set<wxString> fieldNames;
61 std::unique_ptr<LIB_SYMBOL> flattenedParent = m_symbol->GetParent().lock()->Flatten();
62
63 flattenedParent->GetFields( libFields );
64
65 for( SCH_FIELD* libField : libFields )
66 {
67 if( !libField->IsMandatory() )
68 fieldNames.insert( libField->GetName() );
69 }
70
71 libFields.clear(); // flattenedPart is about to go out of scope...
72
73 // Load non-mandatory fields from the editor symbol
74 m_symbol->GetFields( libFields );
75
76 for( SCH_FIELD* libField : libFields )
77 {
78 if( !libField->IsMandatory() )
79 fieldNames.insert( libField->GetName() );
80 }
81
82 auto isMandatoryField =
83 [&]( int listbox_idx )
84 {
85 for( FIELD_T fieldId : MANDATORY_FIELDS )
86 {
87 if( m_mandatoryFieldListIndexes[fieldId] == listbox_idx )
88 return true;
89 }
90
91 return false;
92 };
93
94 libFields.clear();
95
96 // Update the listbox widget
97 for( int i = (int) m_fieldsBox->GetCount() - 1; i >= 0; --i )
98 {
99 if( !isMandatoryField( i ) )
100 m_fieldsBox->Delete( i );
101 }
102
103 for( const wxString& fieldName : fieldNames )
104 m_fieldsBox->Append( fieldName );
105
106 for( int i = 0; i < (int) m_fieldsBox->GetCount(); ++i )
107 {
108 if( !isMandatoryField( i ) )
109 m_fieldsBox->Check( i, true );
110 }
111}
112
113
115{
116 for( unsigned i = 0; i < m_fieldsBox->GetCount(); ++i )
117 m_fieldsBox->Check( i, aCheck );
118}
119
120
122{
123 wxBusyCursor dummy;
124 SCH_COMMIT commit( m_editFrame );
125
126 commit.Modify( m_symbol, m_editFrame->GetScreen() );
127
129
130 // The dialog drives the field set explicitly; an empty selection updates nothing.
131 options.m_updateAllFields = false;
132
133 for( unsigned i = 0; i < m_fieldsBox->GetCount(); ++i )
134 {
135 if( m_fieldsBox->IsChecked( i ) )
136 options.m_updateFields.insert( m_fieldsBox->GetString( i ) );
137 }
138
139 // Mandatory rows show translated names, but SyncFieldsFromParent() matches on canonical
140 // names, so add those too or a non-English UI would never update mandatory fields.
141 for( FIELD_T fieldId : MANDATORY_FIELDS )
142 {
143 if( m_fieldsBox->IsChecked( m_mandatoryFieldListIndexes[fieldId] ) )
144 options.m_updateFields.insert( GetCanonicalFieldName( fieldId ) );
145 }
146
147 options.m_removeExtraFields = m_removeExtraBox->GetValue();
148 options.m_resetVisibility = m_resetFieldVisibilities->GetValue();
149 options.m_resetEffects = m_resetFieldEffects->GetValue();
150 options.m_resetPositions = m_resetFieldPositions->GetValue();
151 options.m_resetText = m_resetFieldText->GetValue();
152 options.m_resetEmptyText = m_resetEmptyFields->GetValue();
153
154 m_symbol->SyncFieldsFromParent( options );
155
156 commit.Push( _( "Update Symbol Fields" ) );
157 m_editFrame->RebuildView();
158
159 wxPostEvent( this, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK ) );
160}
161
162
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr, RECURSE_MODE aRecurse=RECURSE_MODE::NO_RECURSE)
Modify a given item in the model.
Definition commit.h:102
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...
DIALOG_UPDATE_SYMBOL_FIELDS_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Update Symbol Fields"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
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::map< FIELD_T, int > m_mandatoryFieldListIndexes
Index in the list control for each MANDATORY_FIELD type.
Define a library symbol object.
Definition lib_symbol.h:114
virtual void Push(const wxString &aMessage=wxT("A commit"), int aCommitFlags=0) override
Execute the changes.
The symbol library editor main window.
#define _(s)
std::vector< FAB_LAYER_COLOR > dummy
wxString UnescapeString(const wxString &aSource)
Options controlling how a derived symbol's fields are reconciled with its parent.
Definition lib_symbol.h:80
bool m_removeExtraFields
Drop fields not present in the parent.
Definition lib_symbol.h:88
bool m_resetPositions
Copy parent field positions.
Definition lib_symbol.h:97
bool m_resetVisibility
Copy parent visibility and name-shown flags.
Definition lib_symbol.h:91
bool m_updateAllFields
Reconcile every field. When false only the names in m_updateFields are touched.
Definition lib_symbol.h:82
std::set< wxString > m_updateFields
Field names to reconcile when m_updateAllFields is false.
Definition lib_symbol.h:85
bool m_resetEffects
Copy parent text effects (font, justification, etc.) but keep local visibility/position.
Definition lib_symbol.h:94
bool m_resetEmptyText
Copy parent text even when the parent value is empty.
Definition lib_symbol.h:103
bool m_resetText
Copy parent text when the parent value is non-empty.
Definition lib_symbol.h:100
wxString GetDefaultFieldName(FIELD_T aFieldId, bool aTranslateForHI)
Return a default symbol field name for a mandatory field type.
#define DO_TRANSLATE
#define MANDATORY_FIELDS
FIELD_T
The set of all field indices assuming an array like sequence that a SCH_COMPONENT or LIB_PART can hol...
wxString GetCanonicalFieldName(FIELD_T aFieldType)