KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_properties_panel.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 * @author Maciej Suminski <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 3
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
23
24#include <font/fontconfig.h>
25#include <pgm_base.h>
26#include <connection_graph.h>
30#include <sch_commit.h>
31#include <sch_edit_frame.h>
32#include <symbol_edit_frame.h>
33#include <symbol_viewer_frame.h>
34#include <schematic.h>
36#include <string_utils.h>
37#include <tool/tool_manager.h>
39
40
42 PROPERTIES_PANEL( aParent, aFrame ),
43 m_frame( aFrame ),
44 m_propMgr( PROPERTY_MANAGER::Instance() )
45{
47 bool found = false;
48
49 wxASSERT( wxPGGlobalVars );
50
51 wxString editorKey = PG_UNIT_EDITOR::BuildEditorName( m_frame );
52
53 auto it = wxPGGlobalVars->m_mapEditorClasses.find( editorKey );
54
55 if( it != wxPGGlobalVars->m_mapEditorClasses.end() )
56 {
57 m_unitEditorInstance = static_cast<PG_UNIT_EDITOR*>( it->second );
59 found = true;
60 }
61
62 if( !found )
63 {
64 PG_UNIT_EDITOR* new_editor = new PG_UNIT_EDITOR( m_frame );
65 m_unitEditorInstance = static_cast<PG_UNIT_EDITOR*>( wxPropertyGrid::RegisterEditorClass( new_editor ) );
66 }
67
68 it = wxPGGlobalVars->m_mapEditorClasses.find( PG_CHECKBOX_EDITOR::EDITOR_NAME );
69
70 if( it == wxPGGlobalVars->m_mapEditorClasses.end() )
71 {
72 PG_CHECKBOX_EDITOR* cbEditor = new PG_CHECKBOX_EDITOR();
73 m_checkboxEditorInstance = static_cast<PG_CHECKBOX_EDITOR*>( wxPropertyGrid::RegisterEditorClass( cbEditor ) );
74 }
75 else
76 {
77 m_checkboxEditorInstance = static_cast<PG_CHECKBOX_EDITOR*>( it->second );
78 }
79
80 it = wxPGGlobalVars->m_mapEditorClasses.find( PG_COLOR_EDITOR::EDITOR_NAME );
81
82 if( it == wxPGGlobalVars->m_mapEditorClasses.end() )
83 {
84 PG_COLOR_EDITOR* colorEditor = new PG_COLOR_EDITOR();
85 m_colorEditorInstance = static_cast<PG_COLOR_EDITOR*>( wxPropertyGrid::RegisterEditorClass( colorEditor ) );
86 }
87 else
88 {
89 m_colorEditorInstance = static_cast<PG_COLOR_EDITOR*>( it->second );
90 }
91}
92
93
94
96{
98}
99
100
102{
104 const SELECTION& selection = selectionTool->GetSelection();
105
106 // Will actually just be updatePropertyValues() if selection hasn't changed
107 rebuildProperties( selection );
108}
109
110
112{
114 const SELECTION& selection = selectionTool->GetSelection();
115
116 rebuildProperties( selection );
117}
118
119
120wxPGProperty* SCH_PROPERTIES_PANEL::createPGProperty( const PROPERTY_BASE* aProperty ) const
121{
122 wxPGProperty* prop = PGPropertyFactory( aProperty, m_frame );
123
124 if( auto colorProp = dynamic_cast<PGPROPERTY_COLOR4D*>( prop ) )
125 {
127 colorProp->SetBackgroundColor( bg );
128 }
129
130 return prop;
131}
132
133
134PROPERTY_BASE* SCH_PROPERTIES_PANEL::getPropertyFromEvent( const wxPropertyGridEvent& aEvent ) const
135{
137 const SELECTION& selection = selectionTool->GetSelection();
138 SCH_ITEM* firstItem = static_cast<SCH_ITEM*>( selection.Front() );
139
140 wxCHECK_MSG( firstItem, nullptr,
141 wxT( "getPropertyFromEvent for a property with nothing selected!") );
142
143 PROPERTY_BASE* property = m_propMgr.GetProperty( TYPE_HASH( *firstItem ),
144 aEvent.GetPropertyName() );
145 wxCHECK_MSG( property, nullptr,
146 wxT( "getPropertyFromEvent for a property not found on the selected item!" ) );
147
148 return property;
149}
150
151
152void SCH_PROPERTIES_PANEL::valueChanging( wxPropertyGridEvent& aEvent )
153{
155 return;
156
158 const SELECTION& selection = selectionTool->GetSelection();
159 EDA_ITEM* frontItem = selection.Front();
160
161 if( !frontItem )
162 return;
163
164 if( PROPERTY_BASE* property = getPropertyFromEvent( aEvent ) )
165 {
166 wxVariant newValue = aEvent.GetPropertyValue();
167
168 if( VALIDATOR_RESULT validationFailure = property->Validate( newValue.GetAny(), frontItem ) )
169 {
170 wxString errorMsg = wxString::Format( wxS( "%s: %s" ), wxGetTranslation( property->Name() ),
171 validationFailure->get()->Format( m_frame ) );
172 m_frame->ShowInfoBarError( errorMsg );
173 aEvent.Veto();
174 return;
175 }
176
177 aEvent.Skip();
178 }
179}
180
181
182void SCH_PROPERTIES_PANEL::valueChanged( wxPropertyGridEvent& aEvent )
183{
185 return;
186
188 const SELECTION& selection = selectionTool->GetSelection();
189 PROPERTY_BASE* property = getPropertyFromEvent( aEvent );
190 wxCHECK( property, /* void */ );
191
192 wxVariant newValue = aEvent.GetPropertyValue();
193 SCH_COMMIT changes( m_frame );
194 SCH_SCREEN* screen = m_frame->GetScreen();
195
196 PROPERTY_COMMIT_HANDLER handler( &changes );
197
198 for( EDA_ITEM* edaItem : selection )
199 {
200 if( !edaItem->IsSCH_ITEM() )
201 continue;
202
203 SCH_ITEM* item = static_cast<SCH_ITEM*>( edaItem );
204
205 if( item->Type() == SCH_TABLECELL_T )
206 changes.Modify( item->GetParent(), screen, RECURSE_MODE::NO_RECURSE );
207 else
208 changes.Modify( item, screen, RECURSE_MODE::NO_RECURSE );
209
210 item->Set( property, newValue );
211
212 if( SCH_SYMBOL* symbol = dynamic_cast<SCH_SYMBOL*>( item ) )
213 symbol->SyncOtherUnits( symbol->Schematic()->CurrentSheet(), changes, property );
214 }
215
216 changes.Push( _( "Edit Properties" ) );
217 m_frame->Refresh();
218
219 // Perform grid updates as necessary based on value change
220 AfterCommit();
221
222 aEvent.Skip();
223}
224
225
226void SCH_PROPERTIES_PANEL::OnLanguageChanged( wxCommandEvent& aEvent )
227{
229
230 aEvent.Skip();
231}
232
233
COLOR4D GetColor(int aLayer) const
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:107
void ShowInfoBarError(const wxString &aErrorMsg, bool aShowCloseButton=false, WX_INFOBAR::MESSAGE_TYPE aType=WX_INFOBAR::MESSAGE_TYPE::GENERIC)
Show the WX_INFOBAR displayed on the top of the canvas with a message and an error icon on the left o...
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:98
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:110
EDA_ITEM * GetParent() const
Definition: eda_item.h:112
bool Set(PROPERTY_BASE *aProperty, wxAny &aValue, bool aNotify=true)
Definition: inspectable.h:43
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
static const wxString EDITOR_NAME
Definition: pg_editors.h:75
static const wxString EDITOR_NAME
Definition: pg_editors.h:91
void UpdateFrame(EDA_DRAW_FRAME *aFrame)
When restarting an editor, the instance of PG_UNIT_EDITOR may be the same but the referenced frame is...
Definition: pg_editors.cpp:58
static wxString BuildEditorName(EDA_DRAW_FRAME *aFrame)
Definition: pg_editors.cpp:52
virtual void OnLanguageChanged(wxCommandEvent &aEvent)
virtual void rebuildProperties(const SELECTION &aSelection)
Generates the property grid for a given selection of items.
Provide class metadata.Helper macro to map type hashes to names.
Definition: property_mgr.h:74
void Rebuild()
Rebuild the list of all registered properties.
PROPERTY_BASE * GetProperty(TYPE_ID aType, const wxString &aProperty) const
Return a property for a specific type.
A shim class between EDA_DRAW_FRAME and several derived classes: SYMBOL_EDIT_FRAME,...
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
COLOR_SETTINGS * GetColorSettings(bool aForceRefresh=false) const override
Returns a pointer to the active color theme settings.
virtual void Push(const wxString &aMessage=wxT("A commit"), int aCommitFlags=0) override
Execute the changes.
Definition: sch_commit.cpp:489
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:168
wxPGProperty * createPGProperty(const PROPERTY_BASE *aProperty) const override
PROPERTY_MANAGER & m_propMgr
PG_CHECKBOX_EDITOR * m_checkboxEditorInstance
void valueChanging(wxPropertyGridEvent &aEvent) override
PG_UNIT_EDITOR * m_unitEditorInstance
SCH_BASE_FRAME * m_frame
void valueChanged(wxPropertyGridEvent &aEvent) override
SCH_PROPERTIES_PANEL(wxWindow *aParent, SCH_BASE_FRAME *aFrame)
PG_COLOR_EDITOR * m_colorEditorInstance
PROPERTY_BASE * getPropertyFromEvent(const wxPropertyGridEvent &aEvent) const
void OnLanguageChanged(wxCommandEvent &aEvent) override
SCH_SELECTION & GetSelection()
Schematic symbol object.
Definition: sch_symbol.h:75
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:55
#define _(s)
@ LAYER_SCHEMATIC_BACKGROUND
Definition: layer_ids.h:478
wxPGProperty * PGPropertyFactory(const PROPERTY_BASE *aProperty, EDA_DRAW_FRAME *aFrame)
Customized abstract wxPGProperty class to handle coordinate/size units.
see class PGM_BASE
APIIMPORT wxPGGlobalVarsClass * wxPGGlobalVars
#define TYPE_HASH(x)
Definition: property.h:72
std::optional< std::unique_ptr< VALIDATION_ERROR > > VALIDATOR_RESULT
Null optional means validation succeeded.
@ SCH_TABLECELL_T
Definition: typeinfo.h:167