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
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22#include "properties_panel.h"
23#include <bitmaps.h>
24#include <tool/selection.h>
25#include <eda_base_frame.h>
26#include <eda_item.h>
27#include <i18n_utility.h>
28#include <import_export.h>
29#include <pgm_base.h>
31#include <properties/property.h>
34
35#include <algorithm>
36#include <iterator>
37#include <set>
38
39#include <wx/clipbrd.h>
40#include <wx/dataobj.h>
41#include <wx/settings.h>
42#include <wx/stattext.h>
43#include <wx/propgrid/advprops.h>
44#include <wx/menu.h>
45#include <wx/utils.h>
46
47
48// This is provided by wx >3.3.0
49#if !wxCHECK_VERSION( 3, 3, 0 )
50extern APIIMPORT wxPGGlobalVarsClass* wxPGGlobalVars;
51#endif
52
53
54class PROPERTIES_PANEL_GRID : public wxPropertyGrid
55{
56public:
57 PROPERTIES_PANEL_GRID( wxWindow* aParent ) :
58 wxPropertyGrid( aParent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxPG_DEFAULT_STYLE | wxPG_TOOLTIPS )
59 {
60 }
61
62 void ScrollWindow( int aDx, int aDy, const wxRect* aRect = nullptr ) override
63 {
64 wxPropertyGrid::ScrollWindow( aDx, aDy, aRect );
65
66 if( PROPERTIES_PANEL* panel = static_cast<PROPERTIES_PANEL*>( GetParent() ) )
67 panel->positionCategoryButtons();
68 }
69
71 bool IsProcessingWxPGEvent() const { return m_processedEvent != nullptr; }
72
73#if wxUSE_STATUSBAR
74 wxStatusBar* GetStatusBar() override { return nullptr; }
75#endif
76};
77
78
80 wxPanel( aParent ),
82 m_frame( aFrame ),
85{
86 wxBoxSizer* mainSizer = new wxBoxSizer( wxVERTICAL );
87
88#if !wxCHECK_VERSION( 3, 3, 0 )
89 // on some platforms wxPGGlobalVars is initialized automatically,
90 // but others need an explicit init
91 if( !wxPGGlobalVars )
92 wxPGInitResourceModule();
93#endif
94
95 // See https://gitlab.com/kicad/code/kicad/-/issues/12297
96 // and https://github.com/wxWidgets/wxWidgets/issues/11787
97 if( wxPGGlobalVars->m_mapEditorClasses.empty() )
98 {
99 wxPGEditor_TextCtrl = nullptr;
100 wxPGEditor_Choice = nullptr;
101 wxPGEditor_ComboBox = nullptr;
102 wxPGEditor_TextCtrlAndButton = nullptr;
103 wxPGEditor_CheckBox = nullptr;
104 wxPGEditor_ChoiceAndButton = nullptr;
105 wxPGEditor_SpinCtrl = nullptr;
106 wxPGEditor_DatePickerCtrl = nullptr;
107 }
108
109 if( !Pgm().m_PropertyGridInitialized )
110 {
111 delete wxPGGlobalVars->m_defaultRenderer;
112 wxPGGlobalVars->m_defaultRenderer = new PG_CELL_RENDERER();
114 }
115
116 m_caption = new wxStaticText( this, wxID_ANY, _( "No objects selected" ) );
117 mainSizer->Add( m_caption, 0, wxALL | wxEXPAND, 5 );
118
119 m_grid = new PROPERTIES_PANEL_GRID( this );
120 m_grid->SetUnspecifiedValueAppearance( wxPGCell( wxT( "<...>" ) ) );
121
122#if wxCHECK_VERSION( 3, 3, 0 )
123 m_grid->SetValidationFailureBehavior( wxPGVFBFlags::MarkCell );
124#else
125 m_grid->SetValidationFailureBehavior( wxPG_VFB_MARK_CELL );
126#endif
127
128#if wxCHECK_VERSION( 3, 3, 0 )
129 m_grid->AddActionTrigger( wxPGKeyboardAction::NextProperty, WXK_RETURN );
130 m_grid->AddActionTrigger( wxPGKeyboardAction::NextProperty, WXK_NUMPAD_ENTER );
131 m_grid->AddActionTrigger( wxPGKeyboardAction::NextProperty, WXK_DOWN );
132 m_grid->AddActionTrigger( wxPGKeyboardAction::PrevProperty, WXK_UP );
133 m_grid->AddActionTrigger( wxPGKeyboardAction::Edit, WXK_SPACE );
134#else
135 m_grid->AddActionTrigger( wxPG_ACTION_NEXT_PROPERTY, WXK_RETURN );
136 m_grid->AddActionTrigger( wxPG_ACTION_NEXT_PROPERTY, WXK_NUMPAD_ENTER );
137 m_grid->AddActionTrigger( wxPG_ACTION_NEXT_PROPERTY, WXK_DOWN );
138 m_grid->AddActionTrigger( wxPG_ACTION_PREV_PROPERTY, WXK_UP );
139 m_grid->AddActionTrigger( wxPG_ACTION_EDIT, WXK_SPACE );
140#endif
141
142 m_grid->DedicateKey( WXK_RETURN );
143 m_grid->DedicateKey( WXK_NUMPAD_ENTER );
144 m_grid->DedicateKey( WXK_DOWN );
145 m_grid->DedicateKey( WXK_UP );
146 mainSizer->Add( m_grid, 1, wxEXPAND, 5 );
147
148 m_grid->SetCellDisabledTextColour( wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT ) );
149
150#ifdef __WXGTK__
151 // Needed for dark mode, on wx 3.0 at least.
152 m_grid->SetCaptionTextColour( wxSystemSettings::GetColour( wxSYS_COLOUR_CAPTIONTEXT ) );
153#endif
154
155 SetFont( KIUI::GetDockedPaneFont( this ) );
156
157 SetSizer( mainSizer );
158 Layout();
159
160 m_grid->CenterSplitter();
161
162 // Actual edits are allowed or vetoed per-property based on whether or not it's
163 // something where the label/key should be editable (user fields, custom properties, ...)
164 m_grid->MakeColumnEditable( 0 );
165
166 Bind( wxEVT_PG_ITEM_EXPANDED,
167 [&]( wxPropertyGridEvent& )
168 {
170 } );
171
172 Bind( wxEVT_PG_ITEM_COLLAPSED,
173 [&]( wxPropertyGridEvent& )
174 {
176 } );
177
178 Bind( wxEVT_PG_LABEL_EDIT_BEGIN, &PROPERTIES_PANEL::onLabelEditBegin, this );
179 Bind( wxEVT_PG_LABEL_EDIT_ENDING, &PROPERTIES_PANEL::onLabelEditEnding, this );
180 Bind( wxEVT_PG_RIGHT_CLICK, &PROPERTIES_PANEL::onRightClick, this );
181
182 Connect( wxEVT_CHAR_HOOK, wxKeyEventHandler( PROPERTIES_PANEL::onCharHook ), nullptr, this );
183 Connect( wxEVT_PG_CHANGED, wxPropertyGridEventHandler( PROPERTIES_PANEL::valueChanged ), nullptr, this );
184 Connect( wxEVT_PG_CHANGING, wxPropertyGridEventHandler( PROPERTIES_PANEL::valueChanging ), nullptr, this );
185 Connect( wxEVT_SHOW, wxShowEventHandler( PROPERTIES_PANEL::onShow ), nullptr, this );
186
187 Bind( wxEVT_PG_COL_END_DRAG,
188 [&]( wxPropertyGridEvent& )
189 {
190 m_splitter_key_proportion = static_cast<float>( m_grid->GetSplitterPosition() ) / m_grid->GetSize().x;
192 } );
193
194 Bind( wxEVT_SIZE,
195 [&]( wxSizeEvent& aEvent )
196 {
197 CallAfter( [this]()
198 {
201 } );
202 aEvent.Skip();
203 } );
204
205 m_frame->Bind( EDA_LANG_CHANGED, &PROPERTIES_PANEL::OnLanguageChanged, this );
206
207 Bind( wxEVT_TIMER,
208 [this]( wxTimerEvent& )
209 {
211 },
212 m_deferredRebuildTimer.GetId() );
213}
214
215
217{
218 m_frame->Unbind( EDA_LANG_CHANGED, &PROPERTIES_PANEL::OnLanguageChanged, this );
219}
220
221
222void PROPERTIES_PANEL::OnLanguageChanged( wxCommandEvent& aEvent )
223{
224 if( m_grid->IsEditorFocused() )
225 m_grid->CommitChangesFromEditor();
226
227 m_grid->Clear();
228 m_displayed.clear(); // no ownership of pointers
229
230 UpdateData();
231
232 aEvent.Skip();
233}
234
235
237 m_panel( aPanel )
238{
239 m_panel->m_SuppressGridChangeEvents++;
240}
241
242
244{
245 m_panel->m_SuppressGridChangeEvents--;
246}
247
248
250{
251 SUPPRESS_GRID_CHANGED_EVENTS raii( this );
252
253 // wxPG defers property deletion while one of its events is being processed
254 // (m_processedEvent != nullptr), and wxPropertyGridPageState::DoClear() then
255 // leaves the old rows in place; re-appending the new set on top of them
256 // duplicates every row. This happens when e.g. the context menu (opened from
257 // a grid right-click) removes a property. Re-run once the event has unwound.
258 if( static_cast<PROPERTIES_PANEL_GRID*>( m_grid )->IsProcessingWxPGEvent() )
259 {
261 m_deferredSelection = aSelection;
262 m_deferredRebuildTimer.StartOnce( 1 );
263 return;
264 }
265
266 auto reset =
267 [&]()
268 {
269 if( m_grid->IsEditorFocused() )
270 m_grid->CommitChangesFromEditor();
271
272 m_grid->Clear();
273 m_displayed.clear();
274 };
275
276 if( aSelection.Empty() )
277 {
278 m_caption->SetLabel( _( "No objects selected" ) );
280 reset();
281 return;
282 }
283 else if( aSelection.Size() == 1 )
284 {
285 m_caption->SetLabel( aSelection.Front()->GetFriendlyName() );
286 }
287 else
288 {
289 m_caption->SetLabel( wxString::Format( _( "%d objects selected" ), aSelection.Size() ) );
290 }
291
292 // Get all the selected types
293 std::set<TYPE_ID> types;
294
295 for( EDA_ITEM* item : aSelection )
296 types.insert( TYPE_HASH( *item ) );
297
298 wxCHECK( !types.empty(), /* void */ ); // already guarded above, but Coverity doesn't know that
299
301 std::map<wxString, PROPERTY_BASE*> commonProps;
302 const std::vector<PROPERTY_BASE*>& allProperties = propMgr.GetProperties( *types.begin() );
303
304 for( PROPERTY_BASE* property : allProperties )
305 commonProps.emplace( property->Name(), property );
306
307 std::map<wxString, int> displayOrder;
308 for( const auto& entry : propMgr.GetDisplayOrder( *types.begin() ) )
309 displayOrder.emplace( entry.first->Name(), entry.second );
310
311 std::vector<wxString> groupDisplayOrder = propMgr.GetGroupDisplayOrder( *types.begin() );
312 std::set<wxString> groups( groupDisplayOrder.begin(), groupDisplayOrder.end() );
313
314 // Get all possible properties
315 for( auto itType = std::next( types.begin() ); itType != types.end(); ++itType )
316 {
317 TYPE_ID type = *itType;
318
319 for( const wxString& group : propMgr.GetGroupDisplayOrder( type ) )
320 {
321 if( !groups.count( group ) )
322 {
323 groupDisplayOrder.emplace_back( group );
324 groups.insert( group );
325 }
326 }
327
328 for( auto it = commonProps.begin(); it != commonProps.end(); )
329 {
330 if( PROPERTY_BASE* prop = propMgr.GetProperty( type, it->first ) )
331 {
332 // A dummy property won't have the enum values, etc., so replace them with a "real" property
333 if( it->second->IgnoreValue() )
334 it->second = prop;
335
336 ++it;
337 }
338 else
339 {
340 it = commonProps.erase( it );
341 }
342 }
343 }
344
345 for( const EDA_ITEM* item : aSelection )
346 {
347 for( PROPERTY_BASE* prop : item->GetDynamicProperties() )
348 {
349 bool commonToAll = true;
350
351 for( const EDA_ITEM* other : aSelection )
352 {
353 std::vector<PROPERTY_BASE*> otherProps = other->GetDynamicProperties();
354
355 if( std::ranges::none_of( otherProps,
356 [&]( PROPERTY_BASE* p )
357 {
358 return p->Name() == prop->Name();
359 } ) )
360 {
361 commonToAll = false;
362 break;
363 }
364 }
365
366 if( !commonToAll || commonProps.contains( prop->Name() ) )
367 continue;
368
369 commonProps.emplace( prop->Name(), prop );
370
371 auto maxOrderIt = std::ranges::max_element( displayOrder,
372 []( const auto& aL, const auto& aR )
373 {
374 return aL.second < aR.second;
375 } );
376 int nextOrder = maxOrderIt == displayOrder.end() ? 0 : maxOrderIt->second + 1;
377 displayOrder.emplace( prop->Name(), nextOrder );
378
379 if( const wxString& dynGroup = prop->Group(); !dynGroup.IsEmpty() && !groups.contains( dynGroup ) )
380 {
381 groupDisplayOrder.emplace_back( dynGroup );
382 groups.insert( dynGroup );
383 }
384 }
385 }
386
387 // Always show the Custom Properties group, even when it has no members, because it has the
388 // clearest path to add a custom property (the custom "+" button)
389 if( !groups.contains( _HKI( "Custom Properties" ) ) )
390 {
391 groupDisplayOrder.emplace_back( _HKI( "Custom Properties" ) );
392 groups.insert( _HKI( "Custom Properties" ) );
393 }
394
395 // Show category groups that have an action button if they are forced
396 // (e.g. we show "add custom property" even when no custom properties exist)
397 for( const CATEGORY_BUTTON& entry : m_categoryButtons )
398 {
399 if( !entry.forceCategory || groups.contains( entry.groupKey ) )
400 continue;
401
402 if( entry.enableFunc && !entry.enableFunc() )
403 continue;
404
405 groupDisplayOrder.emplace_back( entry.groupKey );
406 groups.insert( entry.groupKey );
407 }
408
409 bool isLibraryEditor = m_frame->IsType( FRAME_FOOTPRINT_EDITOR )
410 || m_frame->IsType( FRAME_SCH_SYMBOL_EDITOR );
411
412 bool isDesignEditor = m_frame->IsType( FRAME_PCB_EDITOR )
413 || m_frame->IsType( FRAME_SCH );
414
415 std::set<wxString> availableProps;
416
417 // Find a set of properties that is common to all selected items
418 for( auto& [name, property] : commonProps )
419 {
420 if( property->IsHiddenFromPropertiesManager() )
421 continue;
422
423 if( isLibraryEditor && property->IsHiddenFromLibraryEditors() )
424 continue;
425
426 if( isDesignEditor && property->IsHiddenFromDesignEditors() )
427 continue;
428
429 wxVariant dummy;
430 wxPGChoices choices;
431 bool writable;
432
433 if( extractValueAndWritability( aSelection, name, dummy, writable, choices ) )
434 availableProps.insert( name );
435 }
436
437 bool writeable = true;
438 std::set<wxString> existingProps;
439
440 for( wxPropertyGridIterator it = m_grid->GetIterator(); !it.AtEnd(); it.Next() )
441 {
442 wxPGProperty* pgProp = it.GetProperty();
443 wxString name = pgProp->GetName();
444
445 // Store the existing name before checking available properties so we can
446 // remove the properties when they are no longer available
447 existingProps.insert( name );
448
449 if( !availableProps.count( name ) )
450 continue;
451
452 wxVariant commonVal;
453 wxPGChoices choices;
454
455 extractValueAndWritability( aSelection, name, commonVal, writeable, choices );
456
457 if( choices.GetCount() > 0 )
458 pgProp->SetChoices( choices );
459
460 pgProp->SetValue( commonVal );
461 pgProp->ChangeFlag( wxPG_PROP_READONLY, !writeable );
462 }
463
464 if( !existingProps.empty() && existingProps == availableProps )
465 return;
466
467 // Some difference exists: start from scratch
468 reset();
469
470 std::map<wxPGProperty*, int> pgPropOrders;
471 std::map<wxString, std::vector<wxPGProperty*>> pgPropGroups;
472
473 for( const wxString& name : availableProps )
474 {
475 PROPERTY_BASE* property = commonProps[name];
476 wxPGProperty* pgProp = createPGProperty( property );
477 wxVariant commonVal;
478 wxPGChoices choices;
479
480 if( !extractValueAndWritability( aSelection, name, commonVal, writeable, choices ) )
481 continue;
482
483 if( pgProp )
484 {
485 if( choices.GetCount() )
486 pgProp->SetChoices( choices );
487
488 pgProp->SetValue( commonVal );
489 pgProp->ChangeFlag( wxPG_PROP_READONLY, !writeable );
490 m_displayed.push_back( property );
491
492 wxASSERT( displayOrder.count( name ) );
493 pgPropOrders[pgProp] = displayOrder[name];
494 pgPropGroups[property->Group()].emplace_back( pgProp );
495 }
496 }
497
498 const wxString unspecifiedGroupCaption = _( "Basic Properties" );
499
500 for( const wxString& groupName : groupDisplayOrder )
501 {
502 if( groupName != _HKI( "Custom Properties" ) && !pgPropGroups.contains( groupName ) )
503 continue;
504
505 std::vector<wxPGProperty*> properties;
506
507 if( pgPropGroups.contains( groupName ) )
508 properties = pgPropGroups[groupName];
509
510 wxString groupCaption = wxGetTranslation( groupName );
511
512 auto groupItem = new wxPropertyCategory( groupName.IsEmpty() ? unspecifiedGroupCaption
513 : groupCaption );
514
515 m_grid->Append( groupItem );
516
517 std::sort( properties.begin(), properties.end(),
518 [&]( wxPGProperty*& aFirst, wxPGProperty*& aSecond )
519 {
520 return pgPropOrders[aFirst] < pgPropOrders[aSecond];
521 } );
522
523 for( wxPGProperty* property : properties )
524 m_grid->Append( property );
525 }
526
529}
530
531
532bool PROPERTIES_PANEL::getItemValue( EDA_ITEM* aItem, PROPERTY_BASE* aProperty, wxVariant& aValue )
533{
534 const wxAny& any = aItem->Get( aProperty );
535 bool converted = false;
536
537 if( aProperty->HasChoices() )
538 {
539 // handle enums as ints, since there are no default conversion functions for wxAny
540 int tmp;
541 converted = any.GetAs<int>( &tmp );
542
543 if( converted )
544 aValue = wxVariant( tmp );
545 }
546
547 if( !converted ) // all other types
548 converted = any.GetAs( &aValue );
549
550 if( !converted )
551 {
552 wxString propName = aProperty->Name();
553 propName.Replace( ' ', '_' );
554 wxFAIL_MSG( wxString::Format( wxS( "Could not convert wxAny to wxVariant for %s::%s" ),
555 aItem->GetClass(),
556 propName ) );
557 }
558
559 return converted;
560}
561
562
563bool PROPERTIES_PANEL::extractValueAndWritability( const SELECTION& aSelection, const wxString& aPropName,
564 wxVariant& aValue, bool& aWritable, wxPGChoices& aChoices )
565{
567 bool different = false;
568 bool first = true;
569
570 aWritable = true;
571
572 for( EDA_ITEM* item : aSelection )
573 {
574 PROPERTY_BASE* property = propMgr.GetProperty( item, aPropName );
575
576 if( !property )
577 return false;
578
579 if( !propMgr.IsAvailableFor( TYPE_HASH( *item ), property, item ) )
580 return false;
581
582 if( property->IsHiddenFromPropertiesManager() )
583 return false;
584
585 if( property->IgnoreValue() )
586 continue;
587
588 wxPGChoices choices = property->GetChoices( item );
589
590 if( first )
591 {
592 aChoices = choices;
593 first = false;
594 }
595 else
596 {
597 wxArrayString labels = choices.GetLabels();
598 wxArrayInt values = choices.GetValuesForStrings( labels );
599
600 if( labels != aChoices.GetLabels() || values != aChoices.GetValuesForStrings( labels ) )
601 return false;
602 }
603
604 // If read-only for any of the selection, read-only for the whole selection.
605 if( !propMgr.IsWriteableFor( TYPE_HASH( *item ), property, item ) )
606 aWritable = false;
607
608 wxVariant value;
609
610 if( getItemValue( item, property, value ) )
611 {
612 // Null value indicates different property values between items
613 if( !different && !aValue.IsNull() && value != aValue )
614 {
615 different = true;
616 aValue.MakeNull();
617 }
618 else if( !different )
619 {
620 aValue = value;
621 }
622 }
623 else
624 {
625 // getItemValue returned false -- not available for this item
626 return false;
627 }
628 }
629
630 return true;
631}
632
633
634void PROPERTIES_PANEL::onShow( wxShowEvent& aEvent )
635{
636 if( aEvent.IsShown() )
637 UpdateData();
638
639 aEvent.Skip();
640}
641
642
643void PROPERTIES_PANEL::onLabelEditBegin( wxPropertyGridEvent& aEvent )
644{
645 wxPGProperty* pgProp = aEvent.GetProperty();
646
647 if( !pgProp || !isKeyEditable( pgProp ) )
648 {
649 aEvent.Veto();
650 return;
651 }
652
653 // Remember the original label so an invalid rename can be undone.
654 m_editingOriginalLabel = pgProp->GetLabel();
655}
656
657
658void PROPERTIES_PANEL::onLabelEditEnding( wxPropertyGridEvent& aEvent )
659{
660 wxPGProperty* pgProp = aEvent.GetProperty();
661
662 if( !pgProp || !isKeyEditable( pgProp ) )
663 return;
664
665 const wxString oldName = pgProp->GetBaseName();
666
667 wxTextCtrl* labelEditor = m_grid->GetLabelEditor();
668 wxString newName;
669
670 if( labelEditor )
671 newName = labelEditor->GetValue();
672
673 if( !m_pendingNewKey.IsEmpty() && oldName == m_pendingNewKey )
674 {
675 const wxString pendingKey = m_pendingNewKey;
676
677 m_pendingNewKey.Clear();
678
680 {
681 if( newName.IsEmpty() || isKeyNameInUse( newName ) )
682 onNewItemLeftBlank( pendingKey );
683 else
684 onKeyRenamed( oldName, newName );
685 }
686 else if( newName.IsEmpty() || isKeyNameInUse( newName ) )
687 {
688 CallAfter(
689 [this, pendingKey]()
690 {
691 onNewItemLeftBlank( pendingKey );
692 } );
693 }
694 else
695 {
696 CallAfter(
697 [this, oldName, newName]()
698 {
699 onKeyRenamed( oldName, newName );
700 } );
701 }
702
703 return;
704 }
705
706 if( newName == oldName )
707 return;
708
709 if( newName.IsEmpty() || isKeyNameInUse( newName ) )
710 {
711 const wxString originalLabel = m_editingOriginalLabel;
712
713 CallAfter(
714 [this, oldName, originalLabel, newName]()
715 {
716 for( wxPropertyGridIterator it = m_grid->GetIterator(); !it.AtEnd(); it.Next() )
717 {
718 wxPGProperty* p = it.GetProperty();
719
720 if( p->GetBaseName() == oldName && p->GetLabel() == newName )
721 {
722 p->SetLabel( originalLabel );
723 m_grid->Refresh();
724 break;
725 }
726 }
727 } );
728
729 return;
730 }
731
732 CallAfter(
733 [this, oldName, newName]()
734 {
735 onKeyRenamed( oldName, newName );
736 } );
737}
738
739
740void PROPERTIES_PANEL::onRightClick( wxPropertyGridEvent& aEvent )
741{
742 wxPGProperty* pgProp = aEvent.GetProperty();
743
744 if( !pgProp )
745 return;
746
747 m_contextMenuPropertyName = pgProp->GetBaseName();
748
749 wxMenu* menu = new wxMenu;
750
751 if( !buildContextMenu( *menu, pgProp ) )
752 {
753 delete menu;
754 return;
755 }
756
757 // Defer showing the menu until the property event has finished
758 const wxPoint pos = ScreenToClient( wxGetMousePosition() );
759 CallAfter( [this, menu, pos]()
760 {
761 PopupMenu( menu, pos );
762 delete menu;
763 } );
764}
765
766
768{
769 if( !m_grid->GetLabelEditor() )
770 {
771 m_pendingNewKey.Clear();
772 return;
773 }
774
775 const wxString newName = m_grid->GetLabelEditor()->GetValue();
776 const wxString pendingKey = m_pendingNewKey;
777
779 m_grid->EndLabelEdit( true );
780 m_resolvingPendingKey = false;
781
782 if( pendingKey.IsEmpty() )
783 return;
784
785 if( newName.IsEmpty() || isKeyNameInUse( newName ) )
786 onNewItemLeftBlank( pendingKey );
787 else if( newName != pendingKey )
788 onKeyRenamed( pendingKey, newName );
789}
790
791
792void PROPERTIES_PANEL::beginLabelEdit( const wxString& aKey, bool aStartBlank )
793{
794 for( wxPropertyGridIterator it = m_grid->GetIterator(); !it.AtEnd(); it.Next() )
795 {
796 wxPGProperty* p = it.GetProperty();
797
798 if( !p->IsCategory() && p->GetBaseName() == aKey )
799 {
800 m_grid->SelectProperty( p );
801 m_grid->BeginLabelEdit( 0 );
802
803 if( aStartBlank && m_grid->GetLabelEditor() )
804 m_grid->GetLabelEditor()->SetValue( wxEmptyString );
805
806 break;
807 }
808 }
809}
810
811
812
813
814void PROPERTIES_PANEL::onCharHook( wxKeyEvent& aEvent )
815{
816 if( aEvent.GetKeyCode() == WXK_TAB
817 && ( aEvent.GetModifiers() == wxMOD_NONE || aEvent.GetModifiers() == wxMOD_SHIFT ) )
818 {
819 // wxPropertyGrid hard-codes Tab to focus the current editor and then navigate out of
820 // the grid, so it never steps between properties. Intercept Tab here to commit any
821 // pending edit and move selection to the next (or previous, with Shift) editable
822 // property, wrapping around at the ends.
823 m_grid->CommitChangesFromEditor();
824
825 const bool forward = !aEvent.ShiftDown();
826
827 auto isTabStop =
828 []( wxPGProperty* aProp )
829 {
830 return aProp && !aProp->IsCategory() && aProp->IsVisible() && aProp->IsEnabled()
831 && !aProp->HasFlag( wxPG_PROP_READONLY ) && aProp->GetEditorClass();
832 };
833
834 auto findTarget =
835 [&]( wxPropertyGridIterator aIt )
836 {
837 while( !aIt.AtEnd() )
838 {
839 if( isTabStop( aIt.GetProperty() ) )
840 return aIt.GetProperty();
841
842 if( forward )
843 aIt.Next();
844 else
845 aIt.Prev();
846 }
847
848 return static_cast<wxPGProperty*>( nullptr );
849 };
850
851 wxPGProperty* current = m_grid->GetSelectedProperty();
852 wxPGProperty* target = nullptr;
853
854 if( current )
855 {
856 wxPropertyGridIterator it = m_grid->GetIterator( wxPG_ITERATE_VISIBLE, current );
857
858 if( forward )
859 it.Next();
860 else
861 it.Prev();
862
863 target = findTarget( it );
864 }
865
866 // Wrap around (or pick a starting property if nothing is selected)
867 if( !target )
868 {
869 wxPropertyGridIterator it = m_grid->GetIterator( wxPG_ITERATE_VISIBLE,
870 forward ? wxTOP : wxBOTTOM );
871 target = findTarget( it );
872 }
873
874 if( target )
875 m_grid->SelectProperty( target, true );
876 else
877 aEvent.Skip();
878
879 return;
880 }
881
882 if( aEvent.GetKeyCode() == 'C' && aEvent.GetModifiers() == wxMOD_CONTROL )
883 {
884 if( wxPGProperty* prop = m_grid->GetSelectedProperty() )
885 {
886 if( wxTheClipboard->Open() )
887 {
888 wxTheClipboard->SetData( new wxTextDataObject( prop->GetValueAsString() ) );
889 wxTheClipboard->Close();
890 return;
891 }
892 }
893 }
894
895 if( aEvent.GetKeyCode() == WXK_SPACE )
896 {
897 if( wxPGProperty* prop = m_grid->GetSelectedProperty() )
898 {
899 if( prop->GetValueType() == wxT( "bool" ) )
900 {
901 m_grid->SetPropertyValue( prop, !prop->GetValue().GetBool() );
902 return;
903 }
904 }
905 }
906
907 if( aEvent.GetKeyCode() == WXK_RETURN || aEvent.GetKeyCode() == WXK_NUMPAD_ENTER
908 || aEvent.GetKeyCode() == WXK_DOWN || aEvent.GetKeyCode() == WXK_UP )
909 {
910 m_grid->CommitChangesFromEditor();
911
912 CallAfter( [this]()
913 {
914 m_grid->SelectProperty( m_grid->GetSelectedProperty(), true );
915 } );
916 }
917
918 aEvent.Skip();
919}
920
921
923{
925 m_grid->CenterSplitter();
926 else
927 m_grid->SetSplitterPosition( m_splitter_key_proportion * m_grid->GetSize().x );
928}
929
930
932{
933 m_splitter_key_proportion = aProportion;
935}
936
937
938void PROPERTIES_PANEL::addCategoryButton( const wxString& aGroupKey, const wxString& aTooltip, BITMAPS aBitmap,
939 std::function<void()> aAction, std::function<bool()> aEnableFunc,
940 bool aForceCategory )
941{
942 BITMAP_BUTTON* button = new BITMAP_BUTTON( m_grid, wxID_ANY );
943 button->SetBitmap( KiBitmapBundle( aBitmap ) );
944 button->SetToolTip( aTooltip );
945 button->Hide();
946
947 button->Bind( wxEVT_BUTTON,
948 [this, button]( wxCommandEvent& )
949 {
950 for( CATEGORY_BUTTON& entry : m_categoryButtons )
951 {
952 if( entry.button == button )
953 {
954 entry.action();
955 return;
956 }
957 }
958 } );
959
960 m_categoryButtons.emplace_back( CATEGORY_BUTTON{ .button = button,
961 .groupKey = aGroupKey,
962 .action = std::move( aAction ),
963 .enableFunc = std::move( aEnableFunc ),
964 .forceCategory = aForceCategory } );
965}
966
967
968wxPGProperty* PROPERTIES_PANEL::categoryForGroup( const wxString& aGroupKey ) const
969{
970 const wxString caption = wxGetTranslation( aGroupKey );
971
972 for( wxPropertyGridIterator it = m_grid->GetIterator( wxPG_ITERATE_VISIBLE ); !it.AtEnd(); it.Next() )
973 {
974 if( wxPGProperty* pgProp = it.GetProperty(); pgProp->IsCategory() && pgProp->GetLabel() == caption )
975 return pgProp;
976 }
977
978 return nullptr;
979}
980
981
986
987
989{
990 for( CATEGORY_BUTTON& entry : m_categoryButtons )
991 entry.button->Hide();
992}
993
994
996{
997 const int rightEdgeBase = m_grid->GetClientSize().x - m_grid->FromDIP( 4 );
998 int nextRightEdge = rightEdgeBase;
999 wxString currentRow;
1000 bool haveCurrentRow = false;
1001
1002 for( CATEGORY_BUTTON& entry : m_categoryButtons )
1003 {
1004 if( bool sameRow = haveCurrentRow && currentRow == entry.groupKey; !sameRow )
1005 {
1006 currentRow = entry.groupKey;
1007 haveCurrentRow = true;
1008 nextRightEdge = rightEdgeBase;
1009 }
1010
1011 wxPGProperty* category = categoryForGroup( entry.groupKey );
1012 bool visible = false;
1013 int rowY = 0;
1014 int rowHeight = m_grid->GetRowHeight();
1015
1016 if( category && ( !entry.enableFunc || entry.enableFunc() ) )
1017 {
1018 rowY = m_grid->CalcScrolledPosition( wxPoint( 0, category->GetY() ) ).y;
1019 visible = ( rowY + rowHeight > 0 ) && ( rowY < m_grid->GetClientSize().y );
1020 }
1021
1022 if( !visible )
1023 {
1024 entry.button->Hide();
1025 continue;
1026 }
1027
1028 const wxSize btnSize = entry.button->GetSize();
1029 const int btnY = rowY + ( rowHeight - btnSize.y ) / 2;
1030 const int btnX = nextRightEdge - btnSize.x;
1031
1032 if( !entry.button->IsShown() )
1033 entry.button->Show();
1034
1035 entry.button->SetPosition( wxPoint( std::max( 0, btnX ), std::max( 0, btnY ) ) );
1036 entry.button->Raise();
1037
1038 nextRightEdge = btnX - m_grid->FromDIP( 2 );
1039 }
1040}
const char * name
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:106
BITMAPS
A list of all bitmap identifiers.
A bitmap button widget that behaves like an AUI toolbar item's button when it is drawn.
void SetBitmap(const wxBitmapBundle &aBmp)
Set the bitmap shown when the button is enabled.
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:565
wxAny Get(PROPERTY_BASE *aProperty) const
virtual wxString GetClass() const =0
Return the class name.
bool m_PropertyGridInitialized
Definition pgm_base.h:392
Enhanced renderer to work around some limitations in wxWidgets 3.0 capabilities.
bool IsProcessingWxPGEvent() const
True while a wxPropertyGrid event (e.g. right-click) is being processed.
void ScrollWindow(int aDx, int aDy, const wxRect *aRect=nullptr) override
PROPERTIES_PANEL_GRID(wxWindow *aParent)
wxString m_contextMenuPropertyName
float m_splitter_key_proportion
Proportion of the grid column splitter that is used for the key column (0.0 - 1.0)
virtual bool isKeyEditable(const wxPGProperty *aPGProp) const
virtual void onKeyRenamed(const wxString &aOldName, const wxString &aNewName)
PROPERTIES_PANEL(wxWindow *aParent, EDA_BASE_FRAME *aFrame)
virtual void valueChanging(wxPropertyGridEvent &aEvent)
wxPropertyGrid * m_grid
wxString m_editingOriginalLabel
virtual void onLabelEditEnding(wxPropertyGridEvent &aEvent)
virtual bool buildContextMenu(wxMenu &aMenu, wxPGProperty *aPGProp)
void beginLabelEdit(const wxString &aKey, bool aStartBlank=false)
virtual bool isKeyNameInUse(const wxString &aName) const
bool m_resolvingPendingKey
True while settling a pending label edit synchronously (see settlePendingLabelEdit).
wxStaticText * m_caption
void SetSplitterProportion(float aProportion)
void onRightClick(wxPropertyGridEvent &aEvent)
void settlePendingLabelEdit()
Synchronously ends any in-progress label edit; potentially canceling a newly added row.
std::vector< PROPERTY_BASE * > m_displayed
virtual void OnLanguageChanged(wxCommandEvent &aEvent)
virtual void UpdateData()=0
friend class PROPERTIES_PANEL_GRID
virtual void onLabelEditBegin(wxPropertyGridEvent &aEvent)
virtual void valueChanged(wxPropertyGridEvent &aEvent)
virtual void onNewItemLeftBlank(const wxString &aKey)
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...
wxTimer m_deferredRebuildTimer
Not CallAfter(): on macOS the pending-event queue is drained from inside tool events,...
SELECTION m_deferredSelection
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...
wxPGProperty * categoryForGroup(const wxString &aGroupKey) const
Find the caption row for the given untranslated group name, if present.
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 addCategoryButton(const wxString &aGroupKey, const wxString &aTooltip, BITMAPS aBitmap, std::function< void()> aAction, std::function< bool()> aEnableFunc=nullptr, bool aForceCategory=false)
Registers an overlay button on a category caption row.
void onShow(wxShowEvent &aEvent)
void hideCategoryButtons()
Hides all overlay buttons (used when no selection or a rebuild is deferred).
std::vector< CATEGORY_BUTTON > m_categoryButtons
void updateCategoryButtons()
Updates overlay button visibility/positions; called after rebuilds and on scroll.
EDA_BASE_FRAME * m_frame
wxString m_pendingNewKey
Key of a freshly-added blank field/custom property awaiting a name from the user.
virtual bool HasChoices() const
Return true if this PROPERTY has a limited set of possible values.
Definition property.h:247
wxPGChoices GetChoices(INSPECTABLE *aObject) const
Definition property.h:269
const wxString & Name() const
Definition property.h:221
Provide class metadata.
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:176
int Size() const
Returns the number of selected parts.
Definition selection.h:120
bool Empty() const
Checks if there is anything selected.
Definition selection.h:114
SUPPRESS_GRID_CHANGED_EVENTS(PROPERTIES_PANEL *aPanel)
A type-safe container of any type.
Definition ki_any.h:92
#define _(s)
Base window classes and related definitions.
@ FRAME_PCB_EDITOR
Definition frame_type.h:38
@ FRAME_SCH_SYMBOL_EDITOR
Definition frame_type.h:31
@ FRAME_SCH
Definition frame_type.h:30
@ FRAME_FOOTPRINT_EDITOR
Definition frame_type.h:39
Some functions to handle hotkeys in KiCad.
#define APIIMPORT
KICOMMON_API wxFont GetDockedPaneFont(wxWindow *aWindow)
#define _HKI(x)
Definition page_info.cpp:40
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
APIIMPORT wxPGGlobalVarsClass * wxPGGlobalVars
#define TYPE_HASH(x)
Macro to generate unique identifier for a type.
Definition property.h:74
size_t TYPE_ID
Unique type identifier.
std::vector< FAB_LAYER_COLOR > dummy