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