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
92}
93
94
95
97{
99}
100
101
103{
105 const SELECTION& selection = selectionTool->GetSelection();
106
107 // Will actually just be updatePropertyValues() if selection hasn't changed
108 rebuildProperties( selection );
109}
110
111
113{
115 const SELECTION& selection = selectionTool->GetSelection();
116
117 rebuildProperties( selection );
118}
119
120
121wxPGProperty* SCH_PROPERTIES_PANEL::createPGProperty( const PROPERTY_BASE* aProperty ) const
122{
123 wxPGProperty* prop = PGPropertyFactory( aProperty, m_frame );
124
125 if( auto colorProp = dynamic_cast<PGPROPERTY_COLOR4D*>( prop ) )
126 {
128 colorProp->SetBackgroundColor( bg );
129 }
130
131 return prop;
132}
133
134
135PROPERTY_BASE* SCH_PROPERTIES_PANEL::getPropertyFromEvent( const wxPropertyGridEvent& aEvent ) const
136{
138 const SELECTION& selection = selectionTool->GetSelection();
139 SCH_ITEM* firstItem = static_cast<SCH_ITEM*>( selection.Front() );
140
141 wxCHECK_MSG( firstItem, nullptr,
142 wxT( "getPropertyFromEvent for a property with nothing selected!") );
143
144 PROPERTY_BASE* property = m_propMgr.GetProperty( TYPE_HASH( *firstItem ),
145 aEvent.GetPropertyName() );
146 wxCHECK_MSG( property, nullptr,
147 wxT( "getPropertyFromEvent for a property not found on the selected item!" ) );
148
149 return property;
150}
151
152
153void SCH_PROPERTIES_PANEL::valueChanging( wxPropertyGridEvent& aEvent )
154{
156 const SELECTION& selection = selectionTool->GetSelection();
157 EDA_ITEM* item = selection.Front();
158
159 PROPERTY_BASE* property = getPropertyFromEvent( aEvent );
160 wxCHECK( property, /* void */ );
161 wxCHECK( item, /* void */ );
162
163 wxVariant newValue = aEvent.GetPropertyValue();
164
165 if( VALIDATOR_RESULT validationFailure = property->Validate( newValue.GetAny(), item ) )
166 {
167 wxString errorMsg = wxString::Format( wxS( "%s: %s" ), wxGetTranslation( property->Name() ),
168 validationFailure->get()->Format( m_frame ) );
169 m_frame->ShowInfoBarError( errorMsg );
170 aEvent.Veto();
171 return;
172 }
173
174 aEvent.Skip();
175}
176
177
178void SCH_PROPERTIES_PANEL::valueChanged( wxPropertyGridEvent& aEvent )
179{
181 const SELECTION& selection = selectionTool->GetSelection();
182
183 PROPERTY_BASE* property = getPropertyFromEvent( aEvent );
184 wxCHECK( property, /* void */ );
185
186 wxVariant newValue = aEvent.GetPropertyValue();
187 SCH_COMMIT changes( m_frame );
188 SCH_SCREEN* screen = m_frame->GetScreen();
189
190 PROPERTY_COMMIT_HANDLER handler( &changes );
191
192 for( EDA_ITEM* edaItem : selection )
193 {
194 SCH_ITEM* item = static_cast<SCH_ITEM*>( edaItem );
195 changes.Modify( item, screen );
196 item->Set( property, newValue );
197 }
198
199 changes.Push( _( "Edit Properties" ) );
200 m_frame->Refresh();
201
202 // Perform grid updates as necessary based on value change
203 AfterCommit();
204
205 aEvent.Skip();
206}
207
208
209void SCH_PROPERTIES_PANEL::OnLanguageChanged( wxCommandEvent& aEvent )
210{
213
214 aEvent.Skip();
215}
216
217
219{
220 wxPGChoices fonts;
221
222 // Regnerate font names
223 std::vector<std::string> fontNames;
224 Fontconfig()->ListFonts( fontNames, std::string( Pgm().GetLanguageTag().utf8_str() ) );
225
226 fonts.Add( _( "Default Font" ), -1 );
227 fonts.Add( KICAD_FONT_NAME, -2 );
228
229 for( int ii = 0; ii < (int) fontNames.size(); ++ii )
230 fonts.Add( wxString( fontNames[ii] ), ii );
231
232 auto fontProperty = m_propMgr.GetProperty( TYPE_HASH( EDA_TEXT ), _HKI( "Font" ) );
233 fontProperty->SetChoices( fonts );
234}
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:105
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:88
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:83
EE_SELECTION & GetSelection()
bool Set(PROPERTY_BASE *aProperty, wxAny &aValue, bool aNotify=true)
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: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.
virtual void SetChoices(const wxPGChoices &aChoices)
Set the possible values for for the property.
Definition: property.h:232
Provide class metadata.Helper macro to map type hashes to names.
Definition: property_mgr.h:85
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
Revert the commit by restoring the modified items state.
Definition: sch_commit.cpp:405
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:174
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
Regenerates caches of font list property.
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:55
#define _HKI(x)
#define _(s)
FONTCONFIG * Fontconfig()
Definition: fontconfig.cpp:90
#define KICAD_FONT_NAME
@ LAYER_SCHEMATIC_BACKGROUND
Definition: layer_ids.h:390
wxPGProperty * PGPropertyFactory(const PROPERTY_BASE *aProperty, EDA_DRAW_FRAME *aFrame)
Customized abstract wxPGProperty class to handle coordinate/size units.
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: pgm_base.cpp:1059
see class PGM_BASE
APIIMPORT wxPGGlobalVarsClass * wxPGGlobalVars
#define TYPE_HASH(x)
Definition: property.h:71
std::optional< std::unique_ptr< VALIDATION_ERROR > > VALIDATOR_RESULT
Null optional means validation succeeded.