KiCad PCB EDA Suite
pcb_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 <pcb_base_edit_frame.h>
25#include <tool/tool_manager.h>
29#include <board_commit.h>
32#include <pcb_shape.h>
33#include <pcb_text.h>
34#include <pcb_track.h>
36#include <string_utils.h>
37
38
40 PROPERTIES_PANEL( aParent, aFrame ),
41 m_frame( aFrame ),
42 m_propMgr( PROPERTY_MANAGER::Instance() )
43{
45 bool found = false;
46
47 wxASSERT( wxPGGlobalVars );
48
49 wxString editorKey = PG_UNIT_EDITOR::BuildEditorName( m_frame );
50
51 auto it = wxPGGlobalVars->m_mapEditorClasses.find( editorKey );
52
53 if( it != wxPGGlobalVars->m_mapEditorClasses.end() )
54 {
55 m_unitEditorInstance = static_cast<PG_UNIT_EDITOR*>( it->second );
57 found = true;
58 }
59
60 if( !found )
61 {
62 PG_UNIT_EDITOR* new_editor = new PG_UNIT_EDITOR( m_frame );
63 m_unitEditorInstance = static_cast<PG_UNIT_EDITOR*>( wxPropertyGrid::RegisterEditorClass( new_editor ) );
64 }
65
66 it = wxPGGlobalVars->m_mapEditorClasses.find( PG_CHECKBOX_EDITOR::EDITOR_NAME );
67
68 if( it == wxPGGlobalVars->m_mapEditorClasses.end() )
69 {
70 PG_CHECKBOX_EDITOR* cbEditor = new PG_CHECKBOX_EDITOR();
71 m_checkboxEditorInstance = static_cast<PG_CHECKBOX_EDITOR*>( wxPropertyGrid::RegisterEditorClass( cbEditor ) );
72 }
73 else
74 {
75 m_checkboxEditorInstance = static_cast<PG_CHECKBOX_EDITOR*>( it->second );
76 }
77}
78
79
80
82{
84}
85
86
88{
90 const SELECTION& selection = selectionTool->GetSelection();
91
92 // TODO perhaps it could be called less often? use PROPERTIES_TOOL and catch MODEL_RELOAD?
93 updateLists( static_cast<PCB_EDIT_FRAME*>( m_frame )->GetBoard() );
94
95 // Will actually just be updatePropertyValues() if selection hasn't changed
96 rebuildProperties( selection );
97}
98
99
101{
103 const SELECTION& selection = selectionTool->GetSelection();
104
105 rebuildProperties( selection );
106
107 CallAfter( [&]()
108 {
109 static_cast<PCB_EDIT_FRAME*>( m_frame )->GetCanvas()->SetFocus();
110 } );
111}
112
113
114wxPGProperty* PCB_PROPERTIES_PANEL::createPGProperty( const PROPERTY_BASE* aProperty ) const
115{
116 if( aProperty->TypeHash() == TYPE_HASH( PCB_LAYER_ID ) )
117 {
118 wxASSERT( aProperty->HasChoices() );
119
120 const wxPGChoices& canonicalLayers = aProperty->Choices();
121 wxArrayString boardLayerNames;
122 wxArrayInt boardLayerIDs;
123
124 for( int ii = 0; ii < (int) aProperty->Choices().GetCount(); ++ii )
125 {
126 int layer = canonicalLayers.GetValue( ii );
127
128 boardLayerNames.push_back( m_frame->GetBoard()->GetLayerName( ToLAYER_ID( layer ) ) );
129 boardLayerIDs.push_back( canonicalLayers.GetValue( ii ) );
130 }
131
132 auto ret = new PGPROPERTY_COLORENUM( wxPG_LABEL, wxPG_LABEL,
133 new wxPGChoices( boardLayerNames, boardLayerIDs ) );
134
135 ret->SetColorFunc(
136 [&]( int aValue ) -> wxColour
137 {
138 return m_frame->GetColorSettings()->GetColor( ToLAYER_ID( aValue ) ).ToColour();
139 } );
140
141 ret->SetLabel( wxGetTranslation( aProperty->Name() ) );
142 ret->SetName( aProperty->Name() );
143 ret->SetHelpString( wxGetTranslation( aProperty->Name() ) );
144 ret->SetClientData( const_cast<PROPERTY_BASE*>( aProperty ) );
145
146 return ret;
147 }
148
149 return PGPropertyFactory( aProperty, m_frame );
150}
151
152
153PROPERTY_BASE* PCB_PROPERTIES_PANEL::getPropertyFromEvent( const wxPropertyGridEvent& aEvent ) const
154{
156 const SELECTION& selection = selectionTool->GetSelection();
157 BOARD_ITEM* firstItem = static_cast<BOARD_ITEM*>( selection.Front() );
158
159 wxCHECK_MSG( firstItem, nullptr,
160 wxT( "getPropertyFromEvent for a property with nothing selected!") );
161
162 PROPERTY_BASE* property = m_propMgr.GetProperty( TYPE_HASH( *firstItem ),
163 aEvent.GetPropertyName() );
164 wxCHECK_MSG( property, nullptr,
165 wxT( "getPropertyFromEvent for a property not found on the selected item!" ) );
166
167 return property;
168}
169
170
171void PCB_PROPERTIES_PANEL::valueChanging( wxPropertyGridEvent& aEvent )
172{
174 const SELECTION& selection = selectionTool->GetSelection();
175 EDA_ITEM* item = selection.Front();
176
177 PROPERTY_BASE* property = getPropertyFromEvent( aEvent );
178 wxCHECK( property, /* void */ );
179 wxCHECK( item, /* void */ );
180
181 wxVariant newValue = aEvent.GetPropertyValue();
182
183 if( VALIDATOR_RESULT validationFailure = property->Validate( newValue.GetAny(), item ) )
184 {
185 wxString errorMsg = wxString::Format( wxS( "%s: %s" ), wxGetTranslation( property->Name() ),
186 validationFailure->get()->Format( m_frame ) );
187 m_frame->ShowInfoBarError( errorMsg );
188 aEvent.Veto();
189 return;
190 }
191}
192
193
194void PCB_PROPERTIES_PANEL::valueChanged( wxPropertyGridEvent& aEvent )
195{
197 const SELECTION& selection = selectionTool->GetSelection();
198
199 PROPERTY_BASE* property = getPropertyFromEvent( aEvent );
200 wxCHECK( property, /* void */ );
201
202 wxVariant newValue = aEvent.GetPropertyValue();
203 BOARD_COMMIT changes( m_frame );
204
205 for( EDA_ITEM* edaItem : selection )
206 {
207 BOARD_ITEM* item = static_cast<BOARD_ITEM*>( edaItem );
208 changes.Modify( item );
209 item->Set( property, newValue );
210 }
211
212 changes.Push( _( "Change property" ) );
213 m_frame->Refresh();
214
215 // Perform grid updates as necessary based on value change
216 AfterCommit();
217}
218
219
221{
222 wxPGChoices layersAll, layersCu, nets;
223
224 // Regenerate all layers
225 for( LSEQ seq = aBoard->GetEnabledLayers().UIOrder(); seq; ++seq )
226 layersAll.Add( LSET::Name( *seq ), *seq );
227
228 for( LSEQ seq = LSET( aBoard->GetEnabledLayers() & LSET::AllCuMask() ).UIOrder(); seq; ++seq )
229 layersCu.Add( LSET::Name( *seq ), *seq );
230
231 m_propMgr.GetProperty( TYPE_HASH( BOARD_ITEM ), _HKI( "Layer" ) )->SetChoices( layersAll );
232 m_propMgr.GetProperty( TYPE_HASH( PCB_SHAPE ), _HKI( "Layer" ) )->SetChoices( layersAll );
233
234 // Copper only properties
236 _HKI( "Layer" ) )->SetChoices( layersCu );
237 m_propMgr.GetProperty( TYPE_HASH( PCB_VIA ), _HKI( "Layer Top" ) )->SetChoices( layersCu );
238 m_propMgr.GetProperty( TYPE_HASH( PCB_VIA ), _HKI( "Layer Bottom" ) )->SetChoices( layersCu );
239
240 // Regenerate nets
241 for( const auto& [ netCode, netInfo ] : aBoard->GetNetInfo().NetsByNetcode() )
242 nets.Add( UnescapeString( netInfo->GetNetname() ), netCode );
243
244 auto netProperty = m_propMgr.GetProperty( TYPE_HASH( BOARD_CONNECTED_ITEM ), _HKI( "Net" ) );
245 netProperty->SetChoices( nets );
246}
virtual void Push(const wxString &aMessage=wxT("A commit"), int aCommitFlags=0) override
Revert the commit by restoring the modified items state.
A base class derived from BOARD_ITEM for items that can be connected and have a net,...
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:70
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:269
const NETINFO_LIST & GetNetInfo() const
Definition: board.h:784
LSET GetEnabledLayers() const
A proxy function that calls the corresponding function in m_BoardSettings.
Definition: board.cpp:587
const wxString GetLayerName(PCB_LAYER_ID aLayer) const
Return the name of a aLayer.
Definition: board.cpp:474
COMMIT & Modify(EDA_ITEM *aItem)
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
bool Set(PROPERTY_BASE *aProperty, wxAny &aValue)
Definition: inspectable.h:42
LSEQ is a sequence (and therefore also a set) of PCB_LAYER_IDs.
Definition: layer_ids.h:493
LSET is a set of PCB_LAYER_IDs.
Definition: layer_ids.h:532
LSEQ UIOrder() const
Definition: lset.cpp:922
static LSET AllCuMask(int aCuLayerCount=MAX_CU_LAYERS)
Return a mask holding the requested number of Cu PCB_LAYER_IDs.
Definition: lset.cpp:773
static const wxChar * Name(PCB_LAYER_ID aLayerId)
Return the fixed name association with aLayerId.
Definition: lset.cpp:82
const NETCODES_MAP & NetsByNetcode() const
Return the netcode map, at least for python.
Definition: netinfo.h:383
Common, abstract interface for edit frames.
COLOR_SETTINGS * GetColorSettings(bool aForceRefresh=false) const override
Helper to retrieve the current color settings.
BOARD * GetBoard() const
The main frame for Pcbnew.
PCB_BASE_EDIT_FRAME * m_frame
void valueChanged(wxPropertyGridEvent &aEvent) override
PG_UNIT_EDITOR * m_unitEditorInstance
wxPGProperty * createPGProperty(const PROPERTY_BASE *aProperty) const override
PCB_PROPERTIES_PANEL(wxWindow *aParent, PCB_BASE_EDIT_FRAME *aFrame)
PROPERTY_BASE * getPropertyFromEvent(const wxPropertyGridEvent &aEvent) const
PROPERTY_MANAGER & m_propMgr
PG_CHECKBOX_EDITOR * m_checkboxEditorInstance
void updateLists(const BOARD *aBoard)
‍Regenerates caches storing layer and net names
void valueChanging(wxPropertyGridEvent &aEvent) override
The selection tool: currently supports:
PCB_SELECTION & GetSelection()
‍A wxEnumProperty that displays a color next to the enum value
static const wxString EDITOR_NAME
Definition: pg_editors.h:75
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:54
static wxString BuildEditorName(EDA_DRAW_FRAME *aFrame)
Definition: pg_editors.cpp:48
virtual void rebuildProperties(const SELECTION &aSelection)
Generates the property grid for a given selection of items.
virtual size_t TypeHash() const =0
Return type-id of the property type.
virtual bool HasChoices() const
Return true if this PROPERTY has a limited set of possible values.
Definition: property.h:225
const wxString & Name() const
Definition: property.h:201
virtual const wxPGChoices & Choices() const
Return a limited set of possible values (e.g.
Definition: property.h:207
virtual void SetChoices(const wxPGChoices &aChoices)
Set the possible values for for the property.
Definition: property.h:216
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.
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:54
#define _HKI(x)
#define _(s)
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:59
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition: lset.cpp:932
BOARD * GetBoard()
wxPGProperty * PGPropertyFactory(const PROPERTY_BASE *aProperty, EDA_DRAW_FRAME *aFrame)
APIIMPORT wxPGGlobalVarsClass * wxPGGlobalVars
#define TYPE_HASH(x)
Definition: property.h:63
std::optional< std::unique_ptr< VALIDATION_ERROR > > VALIDATOR_RESULT
Null optional means validation succeeded.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
wxString UnescapeString(const wxString &aSource)