KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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>
35#include <pad.h>
37#include <string_utils.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
80
81
83{
85}
86
87
89{
91 const SELECTION& selection = selectionTool->GetSelection();
92
93 // TODO perhaps it could be called less often? use PROPERTIES_TOOL and catch MODEL_RELOAD?
94 updateLists( static_cast<PCB_EDIT_FRAME*>( m_frame )->GetBoard() );
95
96 // Will actually just be updatePropertyValues() if selection hasn't changed
97 rebuildProperties( selection );
98}
99
100
102{
104 const SELECTION& selection = selectionTool->GetSelection();
105
106 rebuildProperties( selection );
107
108 CallAfter( [&]()
109 {
110 static_cast<PCB_EDIT_FRAME*>( m_frame )->GetCanvas()->SetFocus();
111 } );
112}
113
114
115wxPGProperty* PCB_PROPERTIES_PANEL::createPGProperty( const PROPERTY_BASE* aProperty ) const
116{
117 if( aProperty->TypeHash() == TYPE_HASH( PCB_LAYER_ID ) )
118 {
119 wxASSERT( aProperty->HasChoices() );
120
121 const wxPGChoices& canonicalLayers = aProperty->Choices();
122 wxArrayString boardLayerNames;
123 wxArrayInt boardLayerIDs;
124
125 for( int ii = 0; ii < (int) aProperty->Choices().GetCount(); ++ii )
126 {
127 int layer = canonicalLayers.GetValue( ii );
128
129 boardLayerNames.push_back( m_frame->GetBoard()->GetLayerName( ToLAYER_ID( layer ) ) );
130 boardLayerIDs.push_back( canonicalLayers.GetValue( ii ) );
131 }
132
133 auto ret = new PGPROPERTY_COLORENUM( wxPG_LABEL, wxPG_LABEL,
134 new wxPGChoices( boardLayerNames, boardLayerIDs ) );
135
136 ret->SetColorFunc(
137 [&]( int aValue ) -> wxColour
138 {
139 return m_frame->GetColorSettings()->GetColor( ToLAYER_ID( aValue ) ).ToColour();
140 } );
141
142 ret->SetLabel( wxGetTranslation( aProperty->Name() ) );
143 ret->SetName( aProperty->Name() );
144 ret->SetHelpString( wxGetTranslation( aProperty->Name() ) );
145 ret->SetClientData( const_cast<PROPERTY_BASE*>( aProperty ) );
146
147 return ret;
148 }
149
150 return PGPropertyFactory( aProperty, m_frame );
151}
152
153
154PROPERTY_BASE* PCB_PROPERTIES_PANEL::getPropertyFromEvent( const wxPropertyGridEvent& aEvent ) const
155{
157 const SELECTION& selection = selectionTool->GetSelection();
158 BOARD_ITEM* firstItem = static_cast<BOARD_ITEM*>( selection.Front() );
159
160 wxCHECK_MSG( firstItem, nullptr,
161 wxT( "getPropertyFromEvent for a property with nothing selected!") );
162
163 PROPERTY_BASE* property = m_propMgr.GetProperty( TYPE_HASH( *firstItem ),
164 aEvent.GetPropertyName() );
165 wxCHECK_MSG( property, nullptr,
166 wxT( "getPropertyFromEvent for a property not found on the selected item!" ) );
167
168 return property;
169}
170
171
172void PCB_PROPERTIES_PANEL::valueChanging( wxPropertyGridEvent& aEvent )
173{
175 const SELECTION& selection = selectionTool->GetSelection();
176 EDA_ITEM* item = selection.Front();
177
178 PROPERTY_BASE* property = getPropertyFromEvent( aEvent );
179 wxCHECK( property, /* void */ );
180 wxCHECK( item, /* void */ );
181
182 wxVariant newValue = aEvent.GetPropertyValue();
183
184 if( VALIDATOR_RESULT validationFailure = property->Validate( newValue.GetAny(), item ) )
185 {
186 wxString errorMsg = wxString::Format( wxS( "%s: %s" ), wxGetTranslation( property->Name() ),
187 validationFailure->get()->Format( m_frame ) );
188 m_frame->ShowInfoBarError( errorMsg );
189 aEvent.Veto();
190 return;
191 }
192}
193
194
195void PCB_PROPERTIES_PANEL::valueChanged( wxPropertyGridEvent& aEvent )
196{
198 const SELECTION& selection = selectionTool->GetSelection();
199
200 PROPERTY_BASE* property = getPropertyFromEvent( aEvent );
201 wxCHECK( property, /* void */ );
202
203 wxVariant newValue = aEvent.GetPropertyValue();
204 BOARD_COMMIT changes( m_frame );
205
206 for( EDA_ITEM* edaItem : selection )
207 {
208 BOARD_ITEM* item = static_cast<BOARD_ITEM*>( edaItem );
209 changes.Modify( item );
210 item->Set( property, newValue );
211 }
212
213 changes.Push( _( "Change property" ) );
214 m_frame->Refresh();
215
216 // Perform grid updates as necessary based on value change
217 AfterCommit();
218}
219
220
222{
223 wxPGChoices layersAll, layersCu, nets;
224
225 // Regenerate all layers
226 for( LSEQ seq = aBoard->GetEnabledLayers().UIOrder(); seq; ++seq )
227 layersAll.Add( LSET::Name( *seq ), *seq );
228
229 for( LSEQ seq = LSET( aBoard->GetEnabledLayers() & LSET::AllCuMask() ).UIOrder(); seq; ++seq )
230 layersCu.Add( LSET::Name( *seq ), *seq );
231
232 m_propMgr.GetProperty( TYPE_HASH( BOARD_ITEM ), _HKI( "Layer" ) )->SetChoices( layersAll );
233 m_propMgr.GetProperty( TYPE_HASH( PCB_SHAPE ), _HKI( "Layer" ) )->SetChoices( layersAll );
234
235 // Copper only properties
237 _HKI( "Layer" ) )->SetChoices( layersCu );
238 m_propMgr.GetProperty( TYPE_HASH( PCB_VIA ), _HKI( "Layer Top" ) )->SetChoices( layersCu );
239 m_propMgr.GetProperty( TYPE_HASH( PCB_VIA ), _HKI( "Layer Bottom" ) )->SetChoices( layersCu );
240
241 // Regenerate nets
242 for( const auto& [ netCode, netInfo ] : aBoard->GetNetInfo().NetsByNetcode() )
243 nets.Add( UnescapeString( netInfo->GetNetname() ), netCode );
244
245 auto netProperty = m_propMgr.GetProperty( TYPE_HASH( BOARD_CONNECTED_ITEM ), _HKI( "Net" ) );
246 netProperty->SetChoices( nets );
247}
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:77
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:271
const NETINFO_LIST & GetNetInfo() const
Definition: board.h:803
LSET GetEnabledLayers() const
A proxy function that calls the corresponding function in m_BoardSettings.
Definition: board.cpp:614
const wxString GetLayerName(PCB_LAYER_ID aLayer) const
Return the name of a aLayer.
Definition: board.cpp:501
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
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:513
LSET is a set of PCB_LAYER_IDs.
Definition: layer_ids.h:552
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
Regenerates caches storing layer and net names.
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)
void valueChanging(wxPropertyGridEvent &aEvent) override
The selection tool: currently supports:
PCB_SELECTION & GetSelection()
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: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 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:233
const wxString & Name() const
Definition: property.h:209
virtual const wxPGChoices & Choices() const
Return a limited set of possible values (e.g.
Definition: property.h:215
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.
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:55
#define _HKI(x)
#define _(s)
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:60
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition: lset.cpp:932
BOARD * GetBoard()
wxPGProperty * PGPropertyFactory(const PROPERTY_BASE *aProperty, EDA_DRAW_FRAME *aFrame)
Customized abstract wxPGProperty class to handle coordinate/size units.
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.
wxString UnescapeString(const wxString &aSource)