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_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 ), m_frame( aFrame ), m_propMgr( PROPERTY_MANAGER::Instance() )
41{
43 bool found = false;
44
45 if( wxPGGlobalVars )
46 {
47 auto it = wxPGGlobalVars->m_mapEditorClasses.find( PG_UNIT_EDITOR::EDITOR_NAME );
48
49 if( it != wxPGGlobalVars->m_mapEditorClasses.end() )
50 {
51 m_editor = static_cast<PG_UNIT_EDITOR*>( it->second );
53 found = true;
54 }
55 }
56
57 if( !found )
58 {
59 PG_UNIT_EDITOR* new_editor = new PG_UNIT_EDITOR( m_frame );
60 m_editor = static_cast<PG_UNIT_EDITOR*>( wxPropertyGrid::RegisterEditorClass( new_editor ) );
61 }
62}
63
64
65
67{
68 m_editor->UpdateFrame( nullptr );
69}
70
71
73{
75 const SELECTION& selection = selectionTool->GetSelection();
76
77 // TODO perhaps it could be called less often? use PROPERTIES_TOOL and catch MODEL_RELOAD?
78 updateLists( static_cast<PCB_EDIT_FRAME*>( m_frame )->GetBoard() );
79 update( selection );
80}
81
82
83wxPGProperty* PCB_PROPERTIES_PANEL::createPGProperty( const PROPERTY_BASE* aProperty ) const
84{
85 if( aProperty->TypeHash() == TYPE_HASH( PCB_LAYER_ID ) )
86 {
87 wxASSERT( aProperty->HasChoices() );
88
89 auto ret = new PGPROPERTY_COLORENUM( wxPG_LABEL, wxPG_LABEL,
90 const_cast<wxPGChoices&>( aProperty->Choices() ) );
91
92 ret->SetColorFunc(
93 [&]( const wxString& aChoice ) -> wxColour
94 {
95 PCB_LAYER_ID l = ENUM_MAP<PCB_LAYER_ID>::Instance().ToEnum( aChoice );
96 wxASSERT( IsPcbLayer( l ) );
97 return m_frame->GetColorSettings()->GetColor( l ).ToColour();
98 } );
99
100 ret->SetLabel( aProperty->Name() );
101 ret->SetName( aProperty->Name() );
102 ret->Enable( !aProperty->IsReadOnly() );
103 ret->SetClientData( const_cast<PROPERTY_BASE*>( aProperty ) );
104
105 return ret;
106 }
107
108 return PGPropertyFactory( aProperty );
109}
110
111
112void PCB_PROPERTIES_PANEL::valueChanged( wxPropertyGridEvent& aEvent )
113{
115 const SELECTION& selection = selectionTool->GetSelection();
116 BOARD_ITEM* firstItem = static_cast<BOARD_ITEM*>( selection.Front() );
117 PROPERTY_BASE* property = m_propMgr.GetProperty( TYPE_HASH( *firstItem ),
118 aEvent.GetPropertyName() );
119 wxVariant newValue = aEvent.GetPropertyValue();
120 BOARD_COMMIT changes( m_frame );
121
122 for( EDA_ITEM* edaItem : selection )
123 {
124 BOARD_ITEM* item = static_cast<BOARD_ITEM*>( edaItem );
125 changes.Modify( item );
126 item->Set( property, newValue );
127 }
128
129 // Pushing the commit will result in a SelectedItemsModified event, which we want to skip
130 m_skipNextUpdate = true;
131
132 changes.Push( _( "Change property" ) );
133 m_frame->Refresh();
134}
135
136
138{
139 wxPGChoices layersAll, layersCu, nets;
140
141 // Regenerate all layers
142 for( LSEQ seq = aBoard->GetEnabledLayers().UIOrder(); seq; ++seq )
143 layersAll.Add( LSET::Name( *seq ), *seq );
144
145 for( LSEQ seq = LSET( aBoard->GetEnabledLayers() & LSET::AllCuMask() ).UIOrder(); seq; ++seq )
146 layersCu.Add( LSET::Name( *seq ), *seq );
147
148 m_propMgr.GetProperty( TYPE_HASH( BOARD_ITEM ), _HKI( "Layer" ) )->SetChoices( layersAll );
149 m_propMgr.GetProperty( TYPE_HASH( PCB_SHAPE ), _HKI( "Layer" ) )->SetChoices( layersAll );
150
151 // Copper only properties
153 _HKI( "Layer" ) )->SetChoices( layersCu );
154 m_propMgr.GetProperty( TYPE_HASH( PCB_VIA ), _HKI( "Layer Top" ) )->SetChoices( layersCu );
155 m_propMgr.GetProperty( TYPE_HASH( PCB_VIA ), _HKI( "Layer Bottom" ) )->SetChoices( layersCu );
156
157 // Regenerate nets
158 for( const auto& [ netCode, netInfo ] : aBoard->GetNetInfo().NetsByNetcode() )
159 nets.Add( UnescapeString( netInfo->GetNetname() ), netCode );
160
161 auto netProperty = m_propMgr.GetProperty( TYPE_HASH( BOARD_CONNECTED_ITEM ), _HKI( "Net" ) );
162 netProperty->SetChoices( nets );
163}
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:58
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:265
const NETINFO_LIST & GetNetInfo() const
Definition: board.h:765
LSET GetEnabledLayers() const
A proxy function that calls the corresponding function in m_BoardSettings.
Definition: board.cpp:526
COLOR4D GetColor(int aLayer) const
COMMIT & Modify(EDA_ITEM *aItem)
Create an undo entry for an item that has been already modified.
Definition: commit.h:103
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:85
static ENUM_MAP< T > & Instance()
Definition: property.h:546
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:491
LSET is a set of PCB_LAYER_IDs.
Definition: layer_ids.h:530
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:369
COLOR_SETTINGS * GetColorSettings(bool aForceRefresh=false) const override
Helper to retrieve the current color settings.
The main frame for Pcbnew.
void valueChanged(wxPropertyGridEvent &aEvent) override
wxPGProperty * createPGProperty(const PROPERTY_BASE *aProperty) const override
PCB_PROPERTIES_PANEL(wxWindow *aParent, PCB_EDIT_FRAME *aFrame)
PROPERTY_MANAGER & m_propMgr
PG_UNIT_EDITOR * m_editor
PCB_EDIT_FRAME * m_frame
void updateLists(const BOARD *aBoard)
‍Regenerates caches storing layer and net names
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:34
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:45
bool m_skipNextUpdate
Skips one call to update()
virtual void update(const SELECTION &aSelection)
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:222
virtual bool IsReadOnly() const =0
const wxString & Name() const
Definition: property.h:195
virtual const wxPGChoices & Choices() const
Return a limited set of possible values (e.g.
Definition: property.h:204
virtual void SetChoices(const wxPGChoices &aChoices)
Set the possible values for for the property.
Definition: property.h:213
Provide class metadata.Helper macro to map type hashes to names.
Definition: property_mgr.h:67
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)
bool IsPcbLayer(int aLayer)
Test whether a layer is a valid layer for Pcbnew.
Definition: layer_ids.h:814
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:59
BOARD * GetBoard()
wxPGProperty * PGPropertyFactory(const PROPERTY_BASE *aProperty)
APIIMPORT wxPGGlobalVarsClass * wxPGGlobalVars
#define TYPE_HASH(x)
Definition: property.h:61
wxString UnescapeString(const wxString &aSource)