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 if( !availableProps.count( name ) )
300 continue;
301
302 wxVariant commonVal;
303 wxPGChoices choices;
304
305 extractValueAndWritability( aSelection, name, commonVal, writeable, choices );
306 pgProp->SetValue( commonVal );
307 pgProp->Enable( writeable );
308
309 existingProps.insert( name );
310 }
311
312 if( !existingProps.empty() && existingProps == availableProps )
313 return;
314
315 // Some difference exists: start from scratch
316 reset();
317
318 std::map<wxPGProperty*, int> pgPropOrders;
319 std::map<wxString, std::vector<wxPGProperty*>> pgPropGroups;
320
321 for( const wxString& name : availableProps )
322 {
323 PROPERTY_BASE* property = commonProps[name];
324 wxPGProperty* pgProp = createPGProperty( property );
325 wxVariant commonVal;
326 wxPGChoices choices;
327
328 if( !extractValueAndWritability( aSelection, name, commonVal, writeable, choices ) )
329 continue;
330
331 if( pgProp )
332 {
333 if( choices.GetCount() )
334 pgProp->SetChoices( choices );
335
336 pgProp->SetValue( commonVal );
337 pgProp->Enable( writeable );
338 m_displayed.push_back( property );
339
340 wxASSERT( displayOrder.count( name ) );
341 pgPropOrders[pgProp] = displayOrder[name];
342 pgPropGroups[property->Group()].emplace_back( pgProp );
343 }
344 }
345
346 const wxString unspecifiedGroupCaption = _( "Basic Properties" );
347
348 for( const wxString& groupName : groupDisplayOrder )
349 {
350 if( !pgPropGroups.count( groupName ) )
351 continue;
352
353 std::vector<wxPGProperty*>& properties = pgPropGroups[groupName];
354 wxString groupCaption = wxGetTranslation( groupName );
355
356 auto groupItem = new wxPropertyCategory( groupName.IsEmpty() ? unspecifiedGroupCaption
357 : groupCaption );
358
359 m_grid->Append( groupItem );
360
361 std::sort( properties.begin(), properties.end(),
362 [&]( wxPGProperty*& aFirst, wxPGProperty*& aSecond )
363 {
364 return pgPropOrders[aFirst] < pgPropOrders[aSecond];
365 } );
366
367 for( wxPGProperty* property : properties )
368 m_grid->Append( property );
369 }
370
372}
373
374
375bool PROPERTIES_PANEL::getItemValue( EDA_ITEM* aItem, PROPERTY_BASE* aProperty, wxVariant& aValue )
376{
377 const wxAny& any = aItem->Get( aProperty );
378 bool converted = false;
379
380 if( aProperty->HasChoices() )
381 {
382 // handle enums as ints, since there are no default conversion functions for wxAny
383 int tmp;
384 converted = any.GetAs<int>( &tmp );
385
386 if( converted )
387 aValue = wxVariant( tmp );
388 }
389
390 if( !converted ) // all other types
391 converted = any.GetAs( &aValue );
392
393 if( !converted )
394 {
395 wxString propName = aProperty->Name();
396 propName.Replace( ' ', '_' );
397 wxFAIL_MSG( wxString::Format( wxS( "Could not convert wxAny to wxVariant for %s::%s" ),
398 aItem->GetClass(),
399 propName ) );
400 }
401
402 return converted;
403}
404
405
406bool PROPERTIES_PANEL::extractValueAndWritability( const SELECTION& aSelection, const wxString& aPropName,
407 wxVariant& aValue, bool& aWritable, wxPGChoices& aChoices )
408{
410 bool different = false;
411 bool first = true;
412
413 aWritable = true;
414
415 for( EDA_ITEM* item : aSelection )
416 {
417 PROPERTY_BASE* property = propMgr.GetProperty( TYPE_HASH( *item ), aPropName );
418
419 if( !property )
420 return false;
421
422 if( !propMgr.IsAvailableFor( TYPE_HASH( *item ), property, item ) )
423 return false;
424
425 if( property->IsHiddenFromPropertiesManager() )
426 return false;
427
428 wxPGChoices choices = property->GetChoices( item );
429
430 if( first )
431 {
432 aChoices = choices;
433 first = false;
434 }
435 else
436 {
437 wxArrayString labels = choices.GetLabels();
438 wxArrayInt values = choices.GetValuesForStrings( labels );
439
440 if( labels != aChoices.GetLabels() || values != aChoices.GetValuesForStrings( labels ) )
441 return false;
442 }
443
444 // If read-only for any of the selection, read-only for the whole selection.
445 if( !propMgr.IsWriteableFor( TYPE_HASH( *item ), property, item ) )
446 aWritable = false;
447
448 wxVariant value;
449
450 if( getItemValue( item, property, value ) )
451 {
452 // Null value indicates different property values between items
453 if( !different && !aValue.IsNull() && value != aValue )
454 {
455 different = true;
456 aValue.MakeNull();
457 }
458 else if( !different )
459 {
460 aValue = value;
461 }
462 }
463 else
464 {
465 // getItemValue returned false -- not available for this item
466 return false;
467 }
468 }
469
470 return true;
471}
472
473
474void PROPERTIES_PANEL::onShow( wxShowEvent& aEvent )
475{
476 if( aEvent.IsShown() )
477 UpdateData();
478
479 aEvent.Skip();
480}
481
482
483void PROPERTIES_PANEL::onCharHook( wxKeyEvent& aEvent )
484{
485 // m_grid->IsAnyModified() doesn't work for the first modification
486 if( aEvent.GetKeyCode() == WXK_TAB && !aEvent.ShiftDown() )
487 {
488 wxVariant oldValue;
489
490 if( wxPGProperty* prop = m_grid->GetSelectedProperty() )
491 oldValue = prop->GetValue();
492
493 m_grid->CommitChangesFromEditor();
494
495 // If there was no change, treat it as a navigation key
496 if( wxPGProperty* prop = m_grid->GetSelectedProperty() )
497 {
498 if( prop->GetValue() == oldValue )
499 aEvent.Skip();
500 }
501
502 return;
503 }
504
505 if( aEvent.GetKeyCode() == WXK_SPACE )
506 {
507 if( wxPGProperty* prop = m_grid->GetSelectedProperty() )
508 {
509 if( prop->GetValueType() == wxT( "bool" ) )
510 {
511 m_grid->SetPropertyValue( prop, !prop->GetValue().GetBool() );
512 return;
513 }
514 }
515 }
516
517 if( aEvent.GetKeyCode() == WXK_RETURN || aEvent.GetKeyCode() == WXK_NUMPAD_ENTER
518 || aEvent.GetKeyCode() == WXK_DOWN || aEvent.GetKeyCode() == WXK_UP )
519 {
520 m_grid->CommitChangesFromEditor();
521
522 CallAfter( [this]()
523 {
524 m_grid->SelectProperty( m_grid->GetSelectedProperty(), true );
525 } );
526 }
527
528 aEvent.Skip();
529}
530
531
533{
535 m_grid->CenterSplitter();
536 else
537 m_grid->SetSplitterPosition( m_splitter_key_proportion * m_grid->GetSize().x );
538}
539
540
542{
543 m_splitter_key_proportion = aProportion;
545}
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:243
wxPGChoices GetChoices(INSPECTABLE *aObject) const
Definition property.h:265
const wxString & Name() const
Definition property.h:219
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:73
size_t TYPE_ID
Unique type identifier.
std::vector< FAB_LAYER_COLOR > dummy