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