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 (C) 2021-2022 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>
26#include <pgm_base.h>
27#include <connection_graph.h>
31#include <sch_commit.h>
32#include <sch_edit_frame.h>
33#include <schematic.h>
35#include <string_utils.h>
36#include <tool/tool_manager.h>
38
39
41 PROPERTIES_PANEL( aParent, aFrame ),
42 m_frame( aFrame ),
43 m_propMgr( PROPERTY_MANAGER::Instance() )
44{
46 bool found = false;
47
48 wxASSERT( wxPGGlobalVars );
49
50 wxString editorKey = PG_UNIT_EDITOR::BuildEditorName( m_frame );
51
52 auto it = wxPGGlobalVars->m_mapEditorClasses.find( editorKey );
53
54 if( it != wxPGGlobalVars->m_mapEditorClasses.end() )
55 {
56 m_unitEditorInstance = static_cast<PG_UNIT_EDITOR*>( it->second );
58 found = true;
59 }
60
61 if( !found )
62 {
63 PG_UNIT_EDITOR* new_editor = new PG_UNIT_EDITOR( m_frame );
64 m_unitEditorInstance = static_cast<PG_UNIT_EDITOR*>( wxPropertyGrid::RegisterEditorClass( new_editor ) );
65 }
66
67 it = wxPGGlobalVars->m_mapEditorClasses.find( PG_CHECKBOX_EDITOR::EDITOR_NAME );
68
69 if( it == wxPGGlobalVars->m_mapEditorClasses.end() )
70 {
71 PG_CHECKBOX_EDITOR* cbEditor = new PG_CHECKBOX_EDITOR();
72 m_checkboxEditorInstance = static_cast<PG_CHECKBOX_EDITOR*>( wxPropertyGrid::RegisterEditorClass( cbEditor ) );
73 }
74 else
75 {
76 m_checkboxEditorInstance = static_cast<PG_CHECKBOX_EDITOR*>( it->second );
77 }
78
79 it = wxPGGlobalVars->m_mapEditorClasses.find( PG_COLOR_EDITOR::EDITOR_NAME );
80
81 if( it == wxPGGlobalVars->m_mapEditorClasses.end() )
82 {
83 PG_COLOR_EDITOR* colorEditor = new PG_COLOR_EDITOR();
84 m_colorEditorInstance = static_cast<PG_COLOR_EDITOR*>( wxPropertyGrid::RegisterEditorClass( colorEditor ) );
85 }
86 else
87 {
88 m_colorEditorInstance = static_cast<PG_COLOR_EDITOR*>( it->second );
89 }
90}
91
92
93
95{
97}
98
99
101{
103 const SELECTION& selection = selectionTool->GetSelection();
104
105 // TODO perhaps it could be called less often? use PROPERTIES_TOOL and catch MODEL_RELOAD?
106 if( SCH_EDIT_FRAME* schFrame = dynamic_cast<SCH_EDIT_FRAME*>( m_frame ) )
107 updateLists( schFrame->Schematic() );
108
109 // Will actually just be updatePropertyValues() if selection hasn't changed
110 rebuildProperties( selection );
111}
112
113
115{
117 const SELECTION& selection = selectionTool->GetSelection();
118
119 rebuildProperties( selection );
120
121 CallAfter( [&]()
122 {
123 m_frame->GetCanvas()->SetFocus();
124 } );
125}
126
127
128wxPGProperty* SCH_PROPERTIES_PANEL::createPGProperty( const PROPERTY_BASE* aProperty ) const
129{
130 wxPGProperty* prop = PGPropertyFactory( aProperty, m_frame );
131
132 if( auto colorProp = dynamic_cast<PGPROPERTY_COLOR4D*>( prop ) )
133 {
135 colorProp->SetBackgroundColor( bg );
136 }
137
138 return prop;
139}
140
141
142PROPERTY_BASE* SCH_PROPERTIES_PANEL::getPropertyFromEvent( const wxPropertyGridEvent& aEvent ) const
143{
145 const SELECTION& selection = selectionTool->GetSelection();
146 SCH_ITEM* firstItem = static_cast<SCH_ITEM*>( selection.Front() );
147
148 wxCHECK_MSG( firstItem, nullptr,
149 wxT( "getPropertyFromEvent for a property with nothing selected!") );
150
151 PROPERTY_BASE* property = m_propMgr.GetProperty( TYPE_HASH( *firstItem ),
152 aEvent.GetPropertyName() );
153 wxCHECK_MSG( property, nullptr,
154 wxT( "getPropertyFromEvent for a property not found on the selected item!" ) );
155
156 return property;
157}
158
159
160void SCH_PROPERTIES_PANEL::valueChanging( wxPropertyGridEvent& aEvent )
161{
163 const SELECTION& selection = selectionTool->GetSelection();
164 EDA_ITEM* item = selection.Front();
165
166 PROPERTY_BASE* property = getPropertyFromEvent( aEvent );
167 wxCHECK( property, /* void */ );
168 wxCHECK( item, /* void */ );
169
170 wxVariant newValue = aEvent.GetPropertyValue();
171
172 if( VALIDATOR_RESULT validationFailure = property->Validate( newValue.GetAny(), item ) )
173 {
174 wxString errorMsg = wxString::Format( wxS( "%s: %s" ), wxGetTranslation( property->Name() ),
175 validationFailure->get()->Format( m_frame ) );
176 m_frame->ShowInfoBarError( errorMsg );
177 aEvent.Veto();
178 return;
179 }
180}
181
182
183void SCH_PROPERTIES_PANEL::valueChanged( wxPropertyGridEvent& aEvent )
184{
186 const SELECTION& selection = selectionTool->GetSelection();
187
188 PROPERTY_BASE* property = getPropertyFromEvent( aEvent );
189 wxCHECK( property, /* void */ );
190
191 wxVariant newValue = aEvent.GetPropertyValue();
192 SCH_COMMIT changes( m_frame );
193 SCH_SCREEN* screen = m_frame->GetScreen();
194
195 for( EDA_ITEM* edaItem : selection )
196 {
197 SCH_ITEM* item = static_cast<SCH_ITEM*>( edaItem );
198 changes.Modify( item, screen );
199 item->Set( property, newValue );
200 }
201
202 changes.Push( _( "Change property" ) );
203 m_frame->Refresh();
204
205 // Perform grid updates as necessary based on value change
206 AfterCommit();
207}
208
209
211{
212 wxPGChoices fonts;
213
214 // Regnerate font names
215 std::vector<std::string> fontNames;
216 Fontconfig()->ListFonts( fontNames, std::string( Pgm().GetLanguageTag().utf8_str() ) );
217
218 fonts.Add( _( "Default Font" ), -1 );
219 fonts.Add( KICAD_FONT_NAME, -2 );
220
221 for( int ii = 0; ii < (int) fontNames.size(); ++ii )
222 fonts.Add( wxString( fontNames[ii] ), ii );
223
224 auto fontProperty = m_propMgr.GetProperty( TYPE_HASH( EDA_TEXT ), _HKI( "Font" ) );
225 fontProperty->SetChoices( fonts );
226}
COLOR4D GetColor(int aLayer) const
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Create an undo entry for an item that has been already modified.
Definition: commit.h:103
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:85
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:80
EE_SELECTION & GetSelection()
bool Set(PROPERTY_BASE *aProperty, wxAny &aValue)
Definition: inspectable.h:42
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:57
static wxString BuildEditorName(EDA_DRAW_FRAME *aFrame)
Definition: pg_editors.cpp:51
virtual void rebuildProperties(const SELECTION &aSelection)
Generates the property grid for a given selection of items.
virtual void SetChoices(const wxPGChoices &aChoices)
Set the possible values for for the property.
Definition: property.h:224
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.
Holds all the data relating to one schematic.
Definition: schematic.h:75
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.
SCH_DRAW_PANEL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
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
Revert the commit by restoring the modified items state.
Definition: sch_commit.cpp:353
Schematic editor (Eeschema) main window.
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:151
void updateLists(const SCHEMATIC &aSchematic)
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
Regenerates caches of list properties.
SCH_PROPERTIES_PANEL(wxWindow *aParent, SCH_BASE_FRAME *aFrame)
PG_COLOR_EDITOR * m_colorEditorInstance
PROPERTY_BASE * getPropertyFromEvent(const wxPropertyGridEvent &aEvent) const
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:55
#define _HKI(x)
#define _(s)
FONTCONFIG * Fontconfig()
Definition: fontconfig.cpp:54
#define KICAD_FONT_NAME
@ LAYER_SCHEMATIC_BACKGROUND
Definition: layer_ids.h:382
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:64
std::optional< std::unique_ptr< VALIDATION_ERROR > > VALIDATOR_RESULT
Null optional means validation succeeded.
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:119