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 The 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 <iterator>
32#include <set>
33
34#include <wx/settings.h>
35#include <wx/stattext.h>
36#include <wx/propgrid/advprops.h>
37
38
39// This is provided by wx >3.3.0
40#if !wxCHECK_VERSION( 3, 3, 0 )
41extern APIIMPORT wxPGGlobalVarsClass* wxPGGlobalVars;
42#endif
43
45 wxPanel( aParent ),
47 m_frame( aFrame ),
49{
50 wxBoxSizer* mainSizer = new wxBoxSizer( wxVERTICAL );
51
52#if !wxCHECK_VERSION( 3, 3, 0 )
53 // on some platforms wxPGGlobalVars is initialized automatically,
54 // but others need an explicit init
55 if( !wxPGGlobalVars )
56 wxPGInitResourceModule();
57#endif
58
59 // See https://gitlab.com/kicad/code/kicad/-/issues/12297
60 // and https://github.com/wxWidgets/wxWidgets/issues/11787
61 if( wxPGGlobalVars->m_mapEditorClasses.empty() )
62 {
63 wxPGEditor_TextCtrl = nullptr;
64 wxPGEditor_Choice = nullptr;
65 wxPGEditor_ComboBox = nullptr;
66 wxPGEditor_TextCtrlAndButton = nullptr;
67 wxPGEditor_CheckBox = nullptr;
68 wxPGEditor_ChoiceAndButton = nullptr;
69 wxPGEditor_SpinCtrl = nullptr;
70 wxPGEditor_DatePickerCtrl = nullptr;
71 }
72
73 if( !Pgm().m_PropertyGridInitialized )
74 {
75 delete wxPGGlobalVars->m_defaultRenderer;
76 wxPGGlobalVars->m_defaultRenderer = new PG_CELL_RENDERER();
78 }
79
80 m_caption = new wxStaticText( this, wxID_ANY, _( "No objects selected" ) );
81 mainSizer->Add( m_caption, 0, wxALL | wxEXPAND, 5 );
82
83 m_grid = new wxPropertyGrid( this );
84 m_grid->SetUnspecifiedValueAppearance( wxPGCell( wxT( "<...>" ) ) );
85 m_grid->SetExtraStyle( wxPG_EX_HELP_AS_TOOLTIPS );
86
87#if wxCHECK_VERSION( 3, 3, 0 )
88 m_grid->SetValidationFailureBehavior( wxPGVFBFlags::MarkCell );
89#else
90 m_grid->SetValidationFailureBehavior( wxPG_VFB_MARK_CELL );
91#endif
92
93#if wxCHECK_VERSION( 3, 3, 0 )
94 m_grid->AddActionTrigger( wxPGKeyboardAction::NextProperty, WXK_RETURN );
95 m_grid->AddActionTrigger( wxPGKeyboardAction::NextProperty, WXK_NUMPAD_ENTER );
96 m_grid->AddActionTrigger( wxPGKeyboardAction::NextProperty, WXK_DOWN );
97 m_grid->AddActionTrigger( wxPGKeyboardAction::PrevProperty, WXK_UP );
98 m_grid->AddActionTrigger( wxPGKeyboardAction::Edit, WXK_SPACE );
99#else
100 m_grid->AddActionTrigger( wxPG_ACTION_NEXT_PROPERTY, WXK_RETURN );
101 m_grid->AddActionTrigger( wxPG_ACTION_NEXT_PROPERTY, WXK_NUMPAD_ENTER );
102 m_grid->AddActionTrigger( wxPG_ACTION_NEXT_PROPERTY, WXK_DOWN );
103 m_grid->AddActionTrigger( wxPG_ACTION_PREV_PROPERTY, WXK_UP );
104 m_grid->AddActionTrigger( wxPG_ACTION_EDIT, WXK_SPACE );
105#endif
106
107 m_grid->DedicateKey( WXK_RETURN );
108 m_grid->DedicateKey( WXK_NUMPAD_ENTER );
109 m_grid->DedicateKey( WXK_DOWN );
110 m_grid->DedicateKey( WXK_UP );
111 mainSizer->Add( m_grid, 1, wxEXPAND, 5 );
112
113 m_grid->SetCellDisabledTextColour( wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT ) );
114
115#ifdef __WXGTK__
116 // Needed for dark mode, on wx 3.0 at least.
117 m_grid->SetCaptionTextColour( wxSystemSettings::GetColour( wxSYS_COLOUR_CAPTIONTEXT ) );
118#endif
119
120 SetFont( KIUI::GetDockedPaneFont( this ) );
121
122 SetSizer( mainSizer );
123 Layout();
124
125 m_grid->CenterSplitter();
126
127 Connect( wxEVT_CHAR_HOOK, wxKeyEventHandler( PROPERTIES_PANEL::onCharHook ), nullptr, this );
128 Connect( wxEVT_PG_CHANGED, wxPropertyGridEventHandler( PROPERTIES_PANEL::valueChanged ), nullptr, this );
129 Connect( wxEVT_PG_CHANGING, wxPropertyGridEventHandler( PROPERTIES_PANEL::valueChanging ), nullptr, this );
130 Connect( wxEVT_SHOW, wxShowEventHandler( PROPERTIES_PANEL::onShow ), nullptr, this );
131
132 Bind( wxEVT_PG_COL_END_DRAG,
133 [&]( wxPropertyGridEvent& )
134 {
135 m_splitter_key_proportion = static_cast<float>( m_grid->GetSplitterPosition() ) / m_grid->GetSize().x;
136 } );
137
138 Bind( wxEVT_SIZE,
139 [&]( wxSizeEvent& aEvent )
140 {
141 CallAfter( [this]()
142 {
144 } );
145 aEvent.Skip();
146 } );
147
148 m_frame->Bind( EDA_LANG_CHANGED, &PROPERTIES_PANEL::OnLanguageChanged, this );
149}
150
151
153{
154 m_frame->Unbind( EDA_LANG_CHANGED, &PROPERTIES_PANEL::OnLanguageChanged, this );
155}
156
157
158void PROPERTIES_PANEL::OnLanguageChanged( wxCommandEvent& aEvent )
159{
160 if( m_grid->IsEditorFocused() )
161 m_grid->CommitChangesFromEditor();
162
163 m_grid->Clear();
164 m_displayed.clear(); // no ownership of pointers
165
166 UpdateData();
167
168 aEvent.Skip();
169}
170
171
173{
174public:
176 m_panel( aPanel )
177 {
178 m_panel->m_SuppressGridChangeEvents++;
179 }
180
182 {
183 m_panel->m_SuppressGridChangeEvents--;
184 }
185
186private:
188};
189
190
192{
193 SUPPRESS_GRID_CHANGED_EVENTS raii( this );
194
195 auto reset =
196 [&]()
197 {
198 if( m_grid->IsEditorFocused() )
199 m_grid->CommitChangesFromEditor();
200
201 m_grid->Clear();
202 m_displayed.clear();
203 };
204
205 if( aSelection.Empty() )
206 {
207 m_caption->SetLabel( _( "No objects selected" ) );
208 reset();
209 return;
210 }
211 else if( aSelection.Size() == 1 )
212 {
213 m_caption->SetLabel( aSelection.Front()->GetFriendlyName() );
214 }
215 else
216 {
217 m_caption->SetLabel( wxString::Format( _( "%d objects selected" ), aSelection.Size() ) );
218 }
219
220 // Get all the selected types
221 std::set<TYPE_ID> types;
222
223 for( EDA_ITEM* item : aSelection )
224 types.insert( TYPE_HASH( *item ) );
225
226 wxCHECK( !types.empty(), /* void */ ); // already guarded above, but Coverity doesn't know that
227
229 std::map<wxString, PROPERTY_BASE*> commonProps;
230 const std::vector<PROPERTY_BASE*>& allProperties = propMgr.GetProperties( *types.begin() );
231
232 for( PROPERTY_BASE* property : allProperties )
233 commonProps.emplace( property->Name(), property );
234
235 std::map<wxString, int> displayOrder;
236 for( const auto& entry : propMgr.GetDisplayOrder( *types.begin() ) )
237 displayOrder.emplace( entry.first->Name(), entry.second );
238
239 std::vector<wxString> groupDisplayOrder = propMgr.GetGroupDisplayOrder( *types.begin() );
240 std::set<wxString> groups( groupDisplayOrder.begin(), groupDisplayOrder.end() );
241
242 // Get all possible properties
243 for( auto itType = std::next( types.begin() ); itType != types.end(); ++itType )
244 {
245 TYPE_ID type = *itType;
246
247 for( const wxString& group : propMgr.GetGroupDisplayOrder( type ) )
248 {
249 if( !groups.count( group ) )
250 {
251 groupDisplayOrder.emplace_back( group );
252 groups.insert( group );
253 }
254 }
255
256 for( auto it = commonProps.begin(); it != commonProps.end(); )
257 {
258 if( !propMgr.GetProperty( type, it->first ) )
259 it = commonProps.erase( it );
260 else
261 ++it;
262 }
263 }
264
265 bool isLibraryEditor = m_frame->IsType( FRAME_FOOTPRINT_EDITOR )
266 || m_frame->IsType( FRAME_SCH_SYMBOL_EDITOR );
267
268 bool isDesignEditor = m_frame->IsType( FRAME_PCB_EDITOR )
269 || m_frame->IsType( FRAME_SCH );
270
271 std::set<wxString> availableProps;
272
273 // Find a set of properties that is common to all selected items
274 for( auto& [name, property] : commonProps )
275 {
276 if( property->IsHiddenFromPropertiesManager() )
277 continue;
278
279 if( isLibraryEditor && property->IsHiddenFromLibraryEditors() )
280 continue;
281
282 if( isDesignEditor && property->IsHiddenFromDesignEditors() )
283 continue;
284
285 wxVariant dummy;
286 wxPGChoices choices;
287 bool writable;
288
289 if( extractValueAndWritability( aSelection, name, dummy, writable, choices ) )
290 availableProps.insert( name );
291 }
292
293 bool writeable = true;
294 std::set<wxString> existingProps;
295
296 for( wxPropertyGridIterator it = m_grid->GetIterator(); !it.AtEnd(); it.Next() )
297 {
298 wxPGProperty* pgProp = it.GetProperty();
299 wxString name = pgProp->GetName();
300
301 // Store the existing name before checking available properties so we can
302 // remove the properties when they are no longer available
303 existingProps.insert( name );
304
305 if( !availableProps.count( name ) )
306 continue;
307
308 wxVariant commonVal;
309 wxPGChoices choices;
310
311 extractValueAndWritability( aSelection, name, commonVal, writeable, choices );
312
313 if( choices.GetCount() > 0 )
314 pgProp->SetChoices( choices );
315
316 pgProp->SetValue( commonVal );
317 pgProp->Enable( writeable );
318 }
319
320 if( !existingProps.empty() && existingProps == availableProps )
321 return;
322
323 // Some difference exists: start from scratch
324 reset();
325
326 std::map<wxPGProperty*, int> pgPropOrders;
327 std::map<wxString, std::vector<wxPGProperty*>> pgPropGroups;
328
329 for( const wxString& name : availableProps )
330 {
331 PROPERTY_BASE* property = commonProps[name];
332 wxPGProperty* pgProp = createPGProperty( property );
333 wxVariant commonVal;
334 wxPGChoices choices;
335
336 if( !extractValueAndWritability( aSelection, name, commonVal, writeable, choices ) )
337 continue;
338
339 if( pgProp )
340 {
341 if( choices.GetCount() )
342 pgProp->SetChoices( choices );
343
344 pgProp->SetValue( commonVal );
345 pgProp->Enable( writeable );
346 m_displayed.push_back( property );
347
348 wxASSERT( displayOrder.count( name ) );
349 pgPropOrders[pgProp] = displayOrder[name];
350 pgPropGroups[property->Group()].emplace_back( pgProp );
351 }
352 }
353
354 const wxString unspecifiedGroupCaption = _( "Basic Properties" );
355
356 for( const wxString& groupName : groupDisplayOrder )
357 {
358 if( !pgPropGroups.count( groupName ) )
359 continue;
360
361 std::vector<wxPGProperty*>& properties = pgPropGroups[groupName];
362 wxString groupCaption = wxGetTranslation( groupName );
363
364 auto groupItem = new wxPropertyCategory( groupName.IsEmpty() ? unspecifiedGroupCaption
365 : groupCaption );
366
367 m_grid->Append( groupItem );
368
369 std::sort( properties.begin(), properties.end(),
370 [&]( wxPGProperty*& aFirst, wxPGProperty*& aSecond )
371 {
372 return pgPropOrders[aFirst] < pgPropOrders[aSecond];
373 } );
374
375 for( wxPGProperty* property : properties )
376 m_grid->Append( property );
377 }
378
380}
381
382
383bool PROPERTIES_PANEL::getItemValue( EDA_ITEM* aItem, PROPERTY_BASE* aProperty, wxVariant& aValue )
384{
385 const wxAny& any = aItem->Get( aProperty );
386 bool converted = false;
387
388 if( aProperty->HasChoices() )
389 {
390 // handle enums as ints, since there are no default conversion functions for wxAny
391 int tmp;
392 converted = any.GetAs<int>( &tmp );
393
394 if( converted )
395 aValue = wxVariant( tmp );
396 }
397
398 if( !converted ) // all other types
399 converted = any.GetAs( &aValue );
400
401 if( !converted )
402 {
403 wxString propName = aProperty->Name();
404 propName.Replace( ' ', '_' );
405 wxFAIL_MSG( wxString::Format( wxS( "Could not convert wxAny to wxVariant for %s::%s" ),
406 aItem->GetClass(),
407 propName ) );
408 }
409
410 return converted;
411}
412
413
414bool PROPERTIES_PANEL::extractValueAndWritability( const SELECTION& aSelection, const wxString& aPropName,
415 wxVariant& aValue, bool& aWritable, wxPGChoices& aChoices )
416{
418 bool different = false;
419 bool first = true;
420
421 aWritable = true;
422
423 for( EDA_ITEM* item : aSelection )
424 {
425 PROPERTY_BASE* property = propMgr.GetProperty( TYPE_HASH( *item ), aPropName );
426
427 if( !property )
428 return false;
429
430 if( !propMgr.IsAvailableFor( TYPE_HASH( *item ), property, item ) )
431 return false;
432
433 if( property->IsHiddenFromPropertiesManager() )
434 return false;
435
436 wxPGChoices choices = property->GetChoices( item );
437
438 if( first )
439 {
440 aChoices = choices;
441 first = false;
442 }
443 else
444 {
445 wxArrayString labels = choices.GetLabels();
446 wxArrayInt values = choices.GetValuesForStrings( labels );
447
448 if( labels != aChoices.GetLabels() || values != aChoices.GetValuesForStrings( labels ) )
449 return false;
450 }
451
452 // If read-only for any of the selection, read-only for the whole selection.
453 if( !propMgr.IsWriteableFor( TYPE_HASH( *item ), property, item ) )
454 aWritable = false;
455
456 wxVariant value;
457
458 if( getItemValue( item, property, value ) )
459 {
460 // Null value indicates different property values between items
461 if( !different && !aValue.IsNull() && value != aValue )
462 {
463 different = true;
464 aValue.MakeNull();
465 }
466 else if( !different )
467 {
468 aValue = value;
469 }
470 }
471 else
472 {
473 // getItemValue returned false -- not available for this item
474 return false;
475 }
476 }
477
478 return true;
479}
480
481
482void PROPERTIES_PANEL::onShow( wxShowEvent& aEvent )
483{
484 if( aEvent.IsShown() )
485 UpdateData();
486
487 aEvent.Skip();
488}
489
490
491void PROPERTIES_PANEL::onCharHook( wxKeyEvent& aEvent )
492{
493 // m_grid->IsAnyModified() doesn't work for the first modification
494 if( aEvent.GetKeyCode() == WXK_TAB && !aEvent.ShiftDown() )
495 {
496 wxVariant oldValue;
497
498 if( wxPGProperty* prop = m_grid->GetSelectedProperty() )
499 oldValue = prop->GetValue();
500
501 m_grid->CommitChangesFromEditor();
502
503 // If there was no change, treat it as a navigation key
504 if( wxPGProperty* prop = m_grid->GetSelectedProperty() )
505 {
506 if( prop->GetValue() == oldValue )
507 aEvent.Skip();
508 }
509
510 return;
511 }
512
513 if( aEvent.GetKeyCode() == WXK_SPACE )
514 {
515 if( wxPGProperty* prop = m_grid->GetSelectedProperty() )
516 {
517 if( prop->GetValueType() == wxT( "bool" ) )
518 {
519 m_grid->SetPropertyValue( prop, !prop->GetValue().GetBool() );
520 return;
521 }
522 }
523 }
524
525 if( aEvent.GetKeyCode() == WXK_RETURN || aEvent.GetKeyCode() == WXK_NUMPAD_ENTER
526 || aEvent.GetKeyCode() == WXK_DOWN || aEvent.GetKeyCode() == WXK_UP )
527 {
528 m_grid->CommitChangesFromEditor();
529
530 CallAfter( [this]()
531 {
532 m_grid->SelectProperty( m_grid->GetSelectedProperty(), true );
533 } );
534 }
535
536 aEvent.Skip();
537}
538
539
541{
543 m_grid->CenterSplitter();
544 else
545 m_grid->SetSplitterPosition( m_splitter_key_proportion * m_grid->GetSize().x );
546}
547
548
550{
551 m_splitter_key_proportion = aProportion;
553}
const char * name
The base frame for deriving all KiCad main window classes.
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
virtual wxString GetFriendlyName() const
Definition eda_item.cpp:401
wxAny Get(PROPERTY_BASE *aProperty) const
virtual wxString GetClass() const =0
Return the class name.
bool m_PropertyGridInitialized
Definition pgm_base.h:397
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)
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 extractValueAndWritability(const SELECTION &aSelection, const wxString &aPropName, wxVariant &aValue, bool &aWritable, wxPGChoices &aChoices)
Processes a selection and determines whether the given property should be available or not and what t...
virtual 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
virtual bool HasChoices() const
Return true if this PROPERTY has a limited set of possible values.
Definition property.h:246
wxPGChoices GetChoices(INSPECTABLE *aObject) const
Definition property.h:268
const wxString & Name() const
Definition property.h:220
Provide class metadata.Helper macro to map type hashes to names.
const std::vector< PROPERTY_BASE * > & GetProperties(TYPE_ID aType) const
Return all properties for a specific type.
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()
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::map< PROPERTY_BASE *, int > & GetDisplayOrder(TYPE_ID aType) const
const std::vector< wxString > & GetGroupDisplayOrder(TYPE_ID aType) const
EDA_ITEM * Front() const
Definition selection.h:177
int Size() const
Returns the number of selected parts.
Definition selection.h:121
bool Empty() const
Checks if there is anything selected.
Definition selection.h:115
SUPPRESS_GRID_CHANGED_EVENTS(PROPERTIES_PANEL *aPanel)
A type-safe container of any type.
Definition ki_any.h:93
#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
KICOMMON_API wxFont GetDockedPaneFont(wxWindow *aWindow)
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
APIIMPORT wxPGGlobalVarsClass * wxPGGlobalVars
#define TYPE_HASH(x)
Definition property.h:74
size_t TYPE_ID
Unique type identifier.
std::vector< FAB_LAYER_COLOR > dummy