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 <symbol_edit_frame.h>
34#include <symbol_viewer_frame.h>
35#include <schematic.h>
37#include <string_utils.h>
38#include <tool/tool_manager.h>
40
41
43 PROPERTIES_PANEL( aParent, aFrame ),
44 m_frame( aFrame ),
45 m_propMgr( PROPERTY_MANAGER::Instance() )
46{
48 bool found = false;
49
50 wxASSERT( wxPGGlobalVars );
51
52 wxString editorKey = PG_UNIT_EDITOR::BuildEditorName( m_frame );
53
54 auto it = wxPGGlobalVars->m_mapEditorClasses.find( editorKey );
55
56 if( it != wxPGGlobalVars->m_mapEditorClasses.end() )
57 {
58 m_unitEditorInstance = static_cast<PG_UNIT_EDITOR*>( it->second );
60 found = true;
61 }
62
63 if( !found )
64 {
65 PG_UNIT_EDITOR* new_editor = new PG_UNIT_EDITOR( m_frame );
66 m_unitEditorInstance = static_cast<PG_UNIT_EDITOR*>( wxPropertyGrid::RegisterEditorClass( new_editor ) );
67 }
68
69 it = wxPGGlobalVars->m_mapEditorClasses.find( PG_CHECKBOX_EDITOR::EDITOR_NAME );
70
71 if( it == wxPGGlobalVars->m_mapEditorClasses.end() )
72 {
73 PG_CHECKBOX_EDITOR* cbEditor = new PG_CHECKBOX_EDITOR();
74 m_checkboxEditorInstance = static_cast<PG_CHECKBOX_EDITOR*>( wxPropertyGrid::RegisterEditorClass( cbEditor ) );
75 }
76 else
77 {
78 m_checkboxEditorInstance = static_cast<PG_CHECKBOX_EDITOR*>( it->second );
79 }
80
81 it = wxPGGlobalVars->m_mapEditorClasses.find( PG_COLOR_EDITOR::EDITOR_NAME );
82
83 if( it == wxPGGlobalVars->m_mapEditorClasses.end() )
84 {
85 PG_COLOR_EDITOR* colorEditor = new PG_COLOR_EDITOR();
86 m_colorEditorInstance = static_cast<PG_COLOR_EDITOR*>( wxPropertyGrid::RegisterEditorClass( colorEditor ) );
87 }
88 else
89 {
90 m_colorEditorInstance = static_cast<PG_COLOR_EDITOR*>( it->second );
91 }
92
94}
95
96
97
99{
101}
102
103
105{
107 const SELECTION& selection = selectionTool->GetSelection();
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
122
123wxPGProperty* SCH_PROPERTIES_PANEL::createPGProperty( const PROPERTY_BASE* aProperty ) const
124{
125 wxPGProperty* prop = PGPropertyFactory( aProperty, m_frame );
126
127 if( auto colorProp = dynamic_cast<PGPROPERTY_COLOR4D*>( prop ) )
128 {
130 colorProp->SetBackgroundColor( bg );
131 }
132
133 return prop;
134}
135
136
137PROPERTY_BASE* SCH_PROPERTIES_PANEL::getPropertyFromEvent( const wxPropertyGridEvent& aEvent ) const
138{
140 const SELECTION& selection = selectionTool->GetSelection();
141 SCH_ITEM* firstItem = static_cast<SCH_ITEM*>( selection.Front() );
142
143 wxCHECK_MSG( firstItem, nullptr,
144 wxT( "getPropertyFromEvent for a property with nothing selected!") );
145
146 PROPERTY_BASE* property = m_propMgr.GetProperty( TYPE_HASH( *firstItem ),
147 aEvent.GetPropertyName() );
148 wxCHECK_MSG( property, nullptr,
149 wxT( "getPropertyFromEvent for a property not found on the selected item!" ) );
150
151 return property;
152}
153
154
155void SCH_PROPERTIES_PANEL::valueChanging( wxPropertyGridEvent& aEvent )
156{
158 const SELECTION& selection = selectionTool->GetSelection();
159 EDA_ITEM* item = selection.Front();
160
161 PROPERTY_BASE* property = getPropertyFromEvent( aEvent );
162 wxCHECK( property, /* void */ );
163 wxCHECK( item, /* void */ );
164
165 wxVariant newValue = aEvent.GetPropertyValue();
166
167 if( VALIDATOR_RESULT validationFailure = property->Validate( newValue.GetAny(), item ) )
168 {
169 wxString errorMsg = wxString::Format( wxS( "%s: %s" ), wxGetTranslation( property->Name() ),
170 validationFailure->get()->Format( m_frame ) );
171 m_frame->ShowInfoBarError( errorMsg );
172 aEvent.Veto();
173 return;
174 }
175
176 aEvent.Skip();
177}
178
179
180void SCH_PROPERTIES_PANEL::valueChanged( wxPropertyGridEvent& aEvent )
181{
183 const SELECTION& selection = selectionTool->GetSelection();
184
185 PROPERTY_BASE* property = getPropertyFromEvent( aEvent );
186 wxCHECK( property, /* void */ );
187
188 wxVariant newValue = aEvent.GetPropertyValue();
189 SCH_COMMIT changes( m_frame );
190 SCH_SCREEN* screen = m_frame->GetScreen();
191
192 PROPERTY_COMMIT_HANDLER handler( &changes );
193
194 for( EDA_ITEM* edaItem : selection )
195 {
196 SCH_ITEM* item = static_cast<SCH_ITEM*>( edaItem );
197 changes.Modify( item, screen );
198 item->Set( property, newValue );
199
200 if( SCH_SYMBOL* symbol = dynamic_cast<SCH_SYMBOL*>( item ) )
201 symbol->SyncOtherUnits( symbol->Schematic()->CurrentSheet(), changes, property );
202 }
203
204 changes.Push( _( "Edit Properties" ) );
205 m_frame->Refresh();
206
207 // Perform grid updates as necessary based on value change
208 AfterCommit();
209
210 aEvent.Skip();
211}
212
213
214void SCH_PROPERTIES_PANEL::OnLanguageChanged( wxCommandEvent& aEvent )
215{
218
219 aEvent.Skip();
220}
221
222
224{
225 wxPGChoices fonts;
226 const std::vector<wxString>* fontFiles = nullptr;
227
229 {
231 }
233 {
234 SYMBOL_EDIT_FRAME* symbolFrame = static_cast<SYMBOL_EDIT_FRAME*>( m_frame );
235
236 if( symbolFrame->GetCurSymbol() )
237 fontFiles = symbolFrame->GetCurSymbol()->GetEmbeddedFiles()->UpdateFontFiles();
238 }
239 else if( m_frame->GetFrameType() == FRAME_SCH_VIEWER )
240 {
241 SYMBOL_VIEWER_FRAME* symbolFrame = static_cast<SYMBOL_VIEWER_FRAME*>( m_frame );
242
243 if( symbolFrame->GetSelectedSymbol() )
244 fontFiles = symbolFrame->GetSelectedSymbol()->GetEmbeddedFiles()->UpdateFontFiles();
245 }
246
247 // Regnerate font names
248 std::vector<std::string> fontNames;
249 Fontconfig()->ListFonts( fontNames, std::string( Pgm().GetLanguageTag().utf8_str() ),
250 fontFiles );
251
252 fonts.Add( _( "Default Font" ), -1 );
253 fonts.Add( KICAD_FONT_NAME, -2 );
254
255 for( int ii = 0; ii < (int) fontNames.size(); ++ii )
256 fonts.Add( wxString( fontNames[ii] ), ii );
257
258 auto fontProperty = m_propMgr.GetProperty( TYPE_HASH( EDA_TEXT ), _HKI( "Font" ) );
259 fontProperty->SetChoices( fonts );
260}
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
FRAME_T GetFrameType() const
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:89
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:79
EE_SELECTION & GetSelection()
const std::vector< wxString > * UpdateFontFiles()
Helper function to get a list of fonts for fontconfig to add to the library.
const std::vector< wxString > * GetFontFiles() const
If we just need the cached version of the font files, we can use this function which is const and wil...
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
EMBEDDED_FILES * GetEmbeddedFiles() override
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.
EMBEDDED_FILES * GetEmbeddedFiles() override
Definition: schematic.cpp:860
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:432
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:166
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.
SCHEMATIC * Schematic() const
Definition: sch_screen.cpp:100
Schematic symbol object.
Definition: sch_symbol.h:104
The symbol library editor main window.
LIB_SYMBOL * GetCurSymbol() const
Return the current symbol being edited or NULL if none selected.
Symbol library viewer main window.
LIB_SYMBOL * GetSelectedSymbol() const
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:55
#define _HKI(x)
#define _(s)
FONTCONFIG * Fontconfig()
Definition: fontconfig.cpp:100
@ FRAME_SCH_SYMBOL_EDITOR
Definition: frame_type.h:35
@ FRAME_SCH_VIEWER
Definition: frame_type.h:36
@ FRAME_SCH
Definition: frame_type.h:34
#define KICAD_FONT_NAME
@ LAYER_SCHEMATIC_BACKGROUND
Definition: layer_ids.h:391
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:1060
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.