KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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-2023 CERN
5 * Copyright (C) 2020-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
22#include "properties_panel.h"
23#include <tool/selection.h>
24#include <eda_base_frame.h>
25#include <eda_item.h>
26#include <import_export.h>
27#include <pgm_base.h>
29
30#include <algorithm>
31#include <set>
32
33#include <wx/settings.h>
34#include <wx/stattext.h>
35#include <wx/propgrid/advprops.h>
36
37
38// This is provided by wx >3.3.0
39#if !wxCHECK_VERSION( 3, 3, 0 )
40extern APIIMPORT wxPGGlobalVarsClass* wxPGGlobalVars;
41#endif
42
44 wxPanel( aParent ),
45 m_frame( aFrame ),
46 m_splitter_key_proportion( -1 )
47{
48 wxBoxSizer* mainSizer = new wxBoxSizer( wxVERTICAL );
49
50 // on some platforms wxPGGlobalVars is initialized automatically,
51 // but others need an explicit init
52 if( !wxPGGlobalVars )
53 wxPGInitResourceModule();
54
55 // See https://gitlab.com/kicad/code/kicad/-/issues/12297
56 // and https://github.com/wxWidgets/wxWidgets/issues/11787
57 if( wxPGGlobalVars->m_mapEditorClasses.empty() )
58 {
59 wxPGEditor_TextCtrl = nullptr;
60 wxPGEditor_Choice = nullptr;
61 wxPGEditor_ComboBox = nullptr;
62 wxPGEditor_TextCtrlAndButton = nullptr;
63 wxPGEditor_CheckBox = nullptr;
64 wxPGEditor_ChoiceAndButton = nullptr;
65 wxPGEditor_SpinCtrl = nullptr;
66 wxPGEditor_DatePickerCtrl = nullptr;
67 }
68
69 if( !Pgm().m_PropertyGridInitialized )
70 {
71 delete wxPGGlobalVars->m_defaultRenderer;
72 wxPGGlobalVars->m_defaultRenderer = new PG_CELL_RENDERER();
74 }
75
76 m_caption = new wxStaticText( this, wxID_ANY, _( "No objects selected" ) );
77 mainSizer->Add( m_caption, 0, wxALL | wxEXPAND, 5 );
78
79 m_grid = new wxPropertyGrid( this );
80 m_grid->SetUnspecifiedValueAppearance( wxPGCell( wxT( "<...>" ) ) );
81 m_grid->SetExtraStyle( wxPG_EX_HELP_AS_TOOLTIPS );
82
83#if wxCHECK_VERSION( 3, 3, 0 )
84 m_grid->SetValidationFailureBehavior( wxPGVFBFlags::MarkCell );
85#else
86 m_grid->SetValidationFailureBehavior( wxPG_VFB_MARK_CELL );
87#endif
88
89#if wxCHECK_VERSION( 3, 3, 0 )
90 m_grid->AddActionTrigger( wxPGKeyboardAction::NextProperty, WXK_RETURN );
91 m_grid->AddActionTrigger( wxPGKeyboardAction::NextProperty, WXK_NUMPAD_ENTER );
92 m_grid->AddActionTrigger( wxPGKeyboardAction::NextProperty, WXK_DOWN );
93 m_grid->AddActionTrigger( wxPGKeyboardAction::PrevProperty, WXK_UP );
94 m_grid->AddActionTrigger( wxPGKeyboardAction::Edit, WXK_SPACE );
95#else
96 m_grid->AddActionTrigger( wxPG_ACTION_NEXT_PROPERTY, WXK_RETURN );
97 m_grid->AddActionTrigger( wxPG_ACTION_NEXT_PROPERTY, WXK_NUMPAD_ENTER );
98 m_grid->AddActionTrigger( wxPG_ACTION_NEXT_PROPERTY, WXK_DOWN );
99 m_grid->AddActionTrigger( wxPG_ACTION_PREV_PROPERTY, WXK_UP );
100 m_grid->AddActionTrigger( wxPG_ACTION_EDIT, WXK_SPACE );
101#endif
102
103 m_grid->DedicateKey( WXK_RETURN );
104 m_grid->DedicateKey( WXK_NUMPAD_ENTER );
105 m_grid->DedicateKey( WXK_DOWN );
106 m_grid->DedicateKey( WXK_UP );
107 mainSizer->Add( m_grid, 1, wxEXPAND, 5 );
108
109 m_grid->SetCellDisabledTextColour( wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT ) );
110
111#ifdef __WXGTK__
112 // Needed for dark mode, on wx 3.0 at least.
113 m_grid->SetCaptionTextColour( wxSystemSettings::GetColour( wxSYS_COLOUR_CAPTIONTEXT ) );
114#endif
115
116 SetFont( KIUI::GetDockedPaneFont( this ) );
117
118 SetSizer( mainSizer );
119 Layout();
120
121 m_grid->CenterSplitter();
122
123 Connect( wxEVT_CHAR_HOOK, wxKeyEventHandler( PROPERTIES_PANEL::onCharHook ), nullptr, this );
124 Connect( wxEVT_PG_CHANGED, wxPropertyGridEventHandler( PROPERTIES_PANEL::valueChanged ), nullptr, this );
125 Connect( wxEVT_PG_CHANGING, wxPropertyGridEventHandler( PROPERTIES_PANEL::valueChanging ), nullptr, this );
126 Connect( wxEVT_SHOW, wxShowEventHandler( PROPERTIES_PANEL::onShow ), nullptr, this );
127
128 Bind( wxEVT_PG_COL_END_DRAG,
129 [&]( wxPropertyGridEvent& )
130 {
132 static_cast<float>( m_grid->GetSplitterPosition() ) / m_grid->GetSize().x;
133 } );
134
135 Bind( wxEVT_SIZE,
136 [&]( wxSizeEvent& aEvent )
137 {
138 CallAfter( [this]()
139 {
141 } );
142 aEvent.Skip();
143 } );
144
145 m_frame->Bind( EDA_LANG_CHANGED, &PROPERTIES_PANEL::OnLanguageChanged, this );
146}
147
148
150{
151 m_frame->Unbind( EDA_LANG_CHANGED, &PROPERTIES_PANEL::OnLanguageChanged, this );
152}
153
154
155void PROPERTIES_PANEL::OnLanguageChanged( wxCommandEvent& aEvent )
156{
157 if( m_grid->IsEditorFocused() )
158 m_grid->CommitChangesFromEditor();
159
160 m_grid->Clear();
161 m_displayed.clear();
162
163 UpdateData();
164
165 aEvent.Skip();
166}
167
168
170{
171 auto reset =
172 [&]()
173 {
174 if( m_grid->IsEditorFocused() )
175 m_grid->CommitChangesFromEditor();
176
177 m_grid->Clear();
178 m_displayed.clear();
179 };
180
181 if( aSelection.Empty() )
182 {
183 m_caption->SetLabel( _( "No objects selected" ) );
184 reset();
185 return;
186 }
187 else if( aSelection.Size() == 1 )
188 {
189 m_caption->SetLabel( aSelection.Front()->GetFriendlyName() );
190 }
191 else
192 {
193 m_caption->SetLabel( wxString::Format( _( "%d objects selected" ), aSelection.Size() ) );
194 }
195
196 // Get all the selected types
197 std::set<TYPE_ID> types;
198
199 for( EDA_ITEM* item : aSelection )
200 types.insert( TYPE_HASH( *item ) );
201
202 wxCHECK( !types.empty(), /* void */ );
203
205 std::set<PROPERTY_BASE*> commonProps;
206 const PROPERTY_LIST& allProperties = propMgr.GetProperties( *types.begin() );
207
208 copy( allProperties.begin(), allProperties.end(), inserter( commonProps, commonProps.begin() ) );
209
210 PROPERTY_DISPLAY_ORDER displayOrder = propMgr.GetDisplayOrder( *types.begin() );
211
212 std::vector<wxString> groupDisplayOrder = propMgr.GetGroupDisplayOrder( *types.begin() );
213 std::set<wxString> groups( groupDisplayOrder.begin(), groupDisplayOrder.end() );
214
215 std::set<PROPERTY_BASE*> availableProps;
216
217 // Get all possible properties
218 for( const TYPE_ID& type : types )
219 {
220 const PROPERTY_LIST& itemProps = propMgr.GetProperties( type );
221
222 const PROPERTY_DISPLAY_ORDER& itemDisplayOrder = propMgr.GetDisplayOrder( type );
223
224 copy( itemDisplayOrder.begin(), itemDisplayOrder.end(),
225 inserter( displayOrder, displayOrder.begin() ) );
226
227 const std::vector<wxString>& itemGroups = propMgr.GetGroupDisplayOrder( type );
228
229 for( const wxString& group : itemGroups )
230 {
231 if( !groups.count( group ) )
232 {
233 groupDisplayOrder.emplace_back( group );
234 groups.insert( group );
235 }
236 }
237
238 for( auto it = commonProps.begin(); it != commonProps.end(); /* ++it in the loop */ )
239 {
240 if( !binary_search( itemProps.begin(), itemProps.end(), *it ) )
241 it = commonProps.erase( it );
242 else
243 ++it;
244 }
245 }
246
247 EDA_ITEM* firstItem = aSelection.Front();
248
249 bool isLibraryEditor = m_frame->IsType( FRAME_FOOTPRINT_EDITOR )
251
252 bool isDesignEditor = m_frame->IsType( FRAME_PCB_EDITOR )
253 || m_frame->IsType( FRAME_SCH );
254
255 // Find a set of properties that is common to all selected items
256 for( PROPERTY_BASE* property : commonProps )
257 {
258 if( property->IsHiddenFromPropertiesManager() )
259 continue;
260
261 if( isLibraryEditor && property->IsHiddenFromLibraryEditors() )
262 continue;
263
264 if( isDesignEditor && property->IsHiddenFromDesignEditors() )
265 continue;
266
267 if( propMgr.IsAvailableFor( TYPE_HASH( *firstItem ), property, firstItem ) )
268 availableProps.insert( property );
269 }
270
271 bool writeable = true;
272 std::set<PROPERTY_BASE*> existingProps;
273
274 for( wxPropertyGridIterator it = m_grid->GetIterator(); !it.AtEnd(); it.Next() )
275 {
276 wxPGProperty* pgProp = it.GetProperty();
277 PROPERTY_BASE* property = propMgr.GetProperty( TYPE_HASH( *firstItem ), pgProp->GetName() );
278
279 // Switching item types? Property may no longer be valid
280 if( !property )
281 continue;
282
283 wxVariant commonVal;
284
285 extractValueAndWritability( aSelection, property, commonVal, writeable );
286 pgProp->SetValue( commonVal );
287 pgProp->Enable( writeable );
288
289 existingProps.insert( property );
290 }
291
292 if( !existingProps.empty() && existingProps == availableProps )
293 return;
294
295 // Some difference exists: start from scratch
296 reset();
297
298 std::map<wxPGProperty*, int> pgPropOrders;
299 std::map<wxString, std::vector<wxPGProperty*>> pgPropGroups;
300
301 for( PROPERTY_BASE* property : availableProps )
302 {
303 wxPGProperty* pgProp = createPGProperty( property );
304 wxVariant commonVal;
305
306 if( !extractValueAndWritability( aSelection, property, commonVal, writeable ) )
307 continue;
308
309 if( pgProp )
310 {
311 pgProp->SetValue( commonVal );
312 pgProp->Enable( writeable );
313 m_displayed.push_back( property );
314
315 wxASSERT( displayOrder.count( property ) );
316 pgPropOrders[pgProp] = displayOrder[property];
317 pgPropGroups[property->Group()].emplace_back( pgProp );
318 }
319 }
320
321 const wxString unspecifiedGroupCaption = _( "Basic Properties" );
322
323 for( const wxString& groupName : groupDisplayOrder )
324 {
325 if( !pgPropGroups.count( groupName ) )
326 continue;
327
328 std::vector<wxPGProperty*>& properties = pgPropGroups[groupName];
329 wxString groupCaption = wxGetTranslation( groupName );
330
331 auto groupItem = new wxPropertyCategory( groupName.IsEmpty() ? unspecifiedGroupCaption
332 : groupCaption );
333
334 m_grid->Append( groupItem );
335
336 std::sort( properties.begin(), properties.end(),
337 [&]( wxPGProperty*& aFirst, wxPGProperty*& aSecond )
338 {
339 return pgPropOrders[aFirst] < pgPropOrders[aSecond];
340 } );
341
342 for( wxPGProperty* property : properties )
343 m_grid->Append( property );
344 }
345
347}
348
349
350bool PROPERTIES_PANEL::getItemValue( EDA_ITEM* aItem, PROPERTY_BASE* aProperty, wxVariant& aValue )
351{
352 const wxAny& any = aItem->Get( aProperty );
353 bool converted = false;
354
355 if( aProperty->HasChoices() )
356 {
357 // handle enums as ints, since there are no default conversion functions for wxAny
358 int tmp;
359 converted = any.GetAs<int>( &tmp );
360
361 if( converted )
362 aValue = wxVariant( tmp );
363 }
364
365 if( !converted ) // all other types
366 converted = any.GetAs( &aValue );
367
368 if( !converted )
369 wxFAIL_MSG( wxS( "Could not convert wxAny to wxVariant" ) );
370
371 return converted;
372}
373
374
376 PROPERTY_BASE* aProperty,
377 wxVariant& aValue, bool& aWritable )
378{
380 bool different = false;
381 wxVariant commonVal;
382
383 aWritable = true;
384
385 for( EDA_ITEM* item : aSelection )
386 {
387 if( !propMgr.IsAvailableFor( TYPE_HASH( *item ), aProperty, item ) )
388 return false;
389
390 if( aProperty->IsHiddenFromPropertiesManager() )
391 return false;
392
393 // If read-only for any of the selection, read-only for the whole selection.
394 if( !propMgr.IsWriteableFor( TYPE_HASH( *item ), aProperty, item ) )
395 aWritable = false;
396
397 wxVariant value;
398
399 if( getItemValue( item, aProperty, value ) )
400 {
401 // Null value indicates different property values between items
402 if( !different && !aValue.IsNull() && value != aValue )
403 {
404 different = true;
405 aValue.MakeNull();
406 }
407 else if( !different )
408 {
409 aValue = value;
410 }
411 }
412 else
413 {
414 // getItemValue returned false -- not available for this item
415 return false;
416 }
417 }
418
419 return true;
420}
421
422
423void PROPERTIES_PANEL::onShow( wxShowEvent& aEvent )
424{
425 if( aEvent.IsShown() )
426 UpdateData();
427
428 aEvent.Skip();
429}
430
431
432void PROPERTIES_PANEL::onCharHook( wxKeyEvent& aEvent )
433{
434 if( aEvent.GetKeyCode() == WXK_TAB && !aEvent.ShiftDown() && m_grid->IsAnyModified() )
435 {
436 m_grid->CommitChangesFromEditor();
437
438 // Pass the tab key on so the default property grid tab behavior is honored.
439 aEvent.Skip();
440 return;
441 }
442
443 if( aEvent.GetKeyCode() == WXK_SPACE )
444 {
445 if( wxPGProperty* prop = m_grid->GetSelectedProperty() )
446 {
447 if( prop->GetValueType() == wxT( "bool" ) )
448 {
449 m_grid->SetPropertyValue( prop, !prop->GetValue().GetBool() );
450 return;
451 }
452 }
453 }
454
455 if( aEvent.GetKeyCode() == WXK_RETURN || aEvent.GetKeyCode() == WXK_NUMPAD_ENTER )
456 {
457 m_grid->CommitChangesFromEditor();
458 /* don't skip this one; if we're not the last property we'll also go to the next row */
459 }
460
461 aEvent.Skip();
462}
463
464
466{
468 m_grid->CenterSplitter();
469 else
470 m_grid->SetSplitterPosition( m_splitter_key_proportion * m_grid->GetSize().x );
471}
472
473
475{
476 m_splitter_key_proportion = aProportion;
478}
The base frame for deriving all KiCad main window classes.
bool IsType(FRAME_T aType) const
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:88
virtual wxString GetFriendlyName() const
Definition: eda_item.cpp:329
wxAny Get(PROPERTY_BASE *aProperty) const
Definition: inspectable.h:99
bool m_PropertyGridInitialized
Definition: pgm_base.h:392
Enhanced renderer to work around some limitations in wxWidgets 3.0 capabilities.
float m_splitter_key_proportion
Proportion of the grid column splitter that is used for the key column (0.0 - 1.0)
PROPERTIES_PANEL(wxWindow *aParent, EDA_BASE_FRAME *aFrame)
virtual void valueChanging(wxPropertyGridEvent &aEvent)
bool extractValueAndWritability(const SELECTION &aSelection, PROPERTY_BASE *aProperty, wxVariant &aValue, bool &aWritable)
Processes a selection and determines whether the given property should be available or not and what t...
wxPropertyGrid * m_grid
wxStaticText * m_caption
void SetSplitterProportion(float aProportion)
std::vector< PROPERTY_BASE * > m_displayed
virtual void OnLanguageChanged(wxCommandEvent &aEvent)
virtual void UpdateData()=0
virtual void valueChanged(wxPropertyGridEvent &aEvent)
bool getItemValue(EDA_ITEM *aItem, PROPERTY_BASE *aProperty, wxVariant &aValue)
Utility to fetch a property value and convert to wxVariant Precondition: aItem is known to have prope...
virtual void rebuildProperties(const SELECTION &aSelection)
Generates the property grid for a given selection of items.
void onCharHook(wxKeyEvent &aEvent)
virtual wxPGProperty * createPGProperty(const PROPERTY_BASE *aProperty) const =0
void onShow(wxShowEvent &aEvent)
EDA_BASE_FRAME * m_frame
bool IsHiddenFromPropertiesManager() const
Definition: property.h:299
virtual bool HasChoices() const
Return true if this PROPERTY has a limited set of possible values.
Definition: property.h:241
Provide class metadata.Helper macro to map type hashes to names.
Definition: property_mgr.h:85
const PROPERTY_LIST & GetProperties(TYPE_ID aType) const
Return all properties for a specific type.
const PROPERTY_DISPLAY_ORDER & GetDisplayOrder(TYPE_ID aType) const
bool IsWriteableFor(TYPE_ID aItemClass, PROPERTY_BASE *aProp, INSPECTABLE *aItem)
Checks overriden availability and original availability of a property, returns false if the property ...
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:87
PROPERTY_BASE * GetProperty(TYPE_ID aType, const wxString &aProperty) const
Return a property for a specific type.
bool IsAvailableFor(TYPE_ID aItemClass, PROPERTY_BASE *aProp, INSPECTABLE *aItem)
Checks overriden availability and original availability of a property, returns false if the property ...
const std::vector< wxString > & GetGroupDisplayOrder(TYPE_ID aType) const
EDA_ITEM * Front() const
Definition: selection.h:172
int Size() const
Returns the number of selected parts.
Definition: selection.h:116
bool Empty() const
Checks if there is anything selected.
Definition: selection.h:110
#define _(s)
Base window classes and related definitions.
@ FRAME_PCB_EDITOR
Definition: frame_type.h:42
@ FRAME_SCH_SYMBOL_EDITOR
Definition: frame_type.h:35
@ FRAME_SCH
Definition: frame_type.h:34
@ FRAME_FOOTPRINT_EDITOR
Definition: frame_type.h:43
#define APIIMPORT
Definition: import_export.h:43
KICOMMON_API wxFont GetDockedPaneFont(wxWindow *aWindow)
Definition: ui_common.cpp:142
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::vector< PROPERTY_BASE * > PROPERTY_LIST
Definition: property_mgr.h:49
std::map< PROPERTY_BASE *, int > PROPERTY_DISPLAY_ORDER
Definition: property_mgr.h:58
size_t TYPE_ID
Unique type identifier.
Definition: property_mgr.h:47