1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
|
/*
* This program source code file is part of KICAD, a free EDA CAD application.
*
* Copyright (C) 2020-2023 CERN
* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
* @author Maciej Suminski <maciej.suminski@cern.ch>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "properties_panel.h"
#include <tool/selection.h>
#include <eda_base_frame.h>
#include <eda_item.h>
#include <import_export.h>
#include <pgm_base.h>
#include <properties/pg_cell_renderer.h>
#include <algorithm>
#include <set>
#include <wx/settings.h>
#include <wx/stattext.h>
#include <wx/propgrid/advprops.h>
// This is provided by wx >3.3.0
#if !wxCHECK_VERSION( 3, 3, 0 )
extern APIIMPORT wxPGGlobalVarsClass* wxPGGlobalVars;
#endif
PROPERTIES_PANEL::PROPERTIES_PANEL( wxWindow* aParent, EDA_BASE_FRAME* aFrame ) :
wxPanel( aParent ),
m_SuppressGridChangeEvents( 0 ),
m_frame( aFrame ),
m_splitter_key_proportion( -1 )
{
wxBoxSizer* mainSizer = new wxBoxSizer( wxVERTICAL );
// on some platforms wxPGGlobalVars is initialized automatically,
// but others need an explicit init
if( !wxPGGlobalVars )
wxPGInitResourceModule();
// See https://gitlab.com/kicad/code/kicad/-/issues/12297
// and https://github.com/wxWidgets/wxWidgets/issues/11787
if( wxPGGlobalVars->m_mapEditorClasses.empty() )
{
wxPGEditor_TextCtrl = nullptr;
wxPGEditor_Choice = nullptr;
wxPGEditor_ComboBox = nullptr;
wxPGEditor_TextCtrlAndButton = nullptr;
wxPGEditor_CheckBox = nullptr;
wxPGEditor_ChoiceAndButton = nullptr;
wxPGEditor_SpinCtrl = nullptr;
wxPGEditor_DatePickerCtrl = nullptr;
}
if( !Pgm().m_PropertyGridInitialized )
{
delete wxPGGlobalVars->m_defaultRenderer;
wxPGGlobalVars->m_defaultRenderer = new PG_CELL_RENDERER();
Pgm().m_PropertyGridInitialized = true;
}
m_caption = new wxStaticText( this, wxID_ANY, _( "No objects selected" ) );
mainSizer->Add( m_caption, 0, wxALL | wxEXPAND, 5 );
m_grid = new wxPropertyGrid( this );
m_grid->SetUnspecifiedValueAppearance( wxPGCell( wxT( "<...>" ) ) );
m_grid->SetExtraStyle( wxPG_EX_HELP_AS_TOOLTIPS );
#if wxCHECK_VERSION( 3, 3, 0 )
m_grid->SetValidationFailureBehavior( wxPGVFBFlags::MarkCell );
#else
m_grid->SetValidationFailureBehavior( wxPG_VFB_MARK_CELL );
#endif
#if wxCHECK_VERSION( 3, 3, 0 )
m_grid->AddActionTrigger( wxPGKeyboardAction::NextProperty, WXK_RETURN );
m_grid->AddActionTrigger( wxPGKeyboardAction::NextProperty, WXK_NUMPAD_ENTER );
m_grid->AddActionTrigger( wxPGKeyboardAction::NextProperty, WXK_DOWN );
m_grid->AddActionTrigger( wxPGKeyboardAction::PrevProperty, WXK_UP );
m_grid->AddActionTrigger( wxPGKeyboardAction::Edit, WXK_SPACE );
#else
m_grid->AddActionTrigger( wxPG_ACTION_NEXT_PROPERTY, WXK_RETURN );
m_grid->AddActionTrigger( wxPG_ACTION_NEXT_PROPERTY, WXK_NUMPAD_ENTER );
m_grid->AddActionTrigger( wxPG_ACTION_NEXT_PROPERTY, WXK_DOWN );
m_grid->AddActionTrigger( wxPG_ACTION_PREV_PROPERTY, WXK_UP );
m_grid->AddActionTrigger( wxPG_ACTION_EDIT, WXK_SPACE );
#endif
m_grid->DedicateKey( WXK_RETURN );
m_grid->DedicateKey( WXK_NUMPAD_ENTER );
m_grid->DedicateKey( WXK_DOWN );
m_grid->DedicateKey( WXK_UP );
mainSizer->Add( m_grid, 1, wxEXPAND, 5 );
m_grid->SetCellDisabledTextColour( wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT ) );
#ifdef __WXGTK__
// Needed for dark mode, on wx 3.0 at least.
m_grid->SetCaptionTextColour( wxSystemSettings::GetColour( wxSYS_COLOUR_CAPTIONTEXT ) );
#endif
SetFont( KIUI::GetDockedPaneFont( this ) );
SetSizer( mainSizer );
Layout();
m_grid->CenterSplitter();
Connect( wxEVT_CHAR_HOOK, wxKeyEventHandler( PROPERTIES_PANEL::onCharHook ), nullptr, this );
Connect( wxEVT_PG_CHANGED, wxPropertyGridEventHandler( PROPERTIES_PANEL::valueChanged ),
nullptr, this );
Connect( wxEVT_PG_CHANGING, wxPropertyGridEventHandler( PROPERTIES_PANEL::valueChanging ),
nullptr, this );
Connect( wxEVT_SHOW, wxShowEventHandler( PROPERTIES_PANEL::onShow ), nullptr, this );
Bind( wxEVT_PG_COL_END_DRAG,
[&]( wxPropertyGridEvent& )
{
m_splitter_key_proportion =
static_cast<float>( m_grid->GetSplitterPosition() ) / m_grid->GetSize().x;
} );
Bind( wxEVT_SIZE,
[&]( wxSizeEvent& aEvent )
{
CallAfter( [this]()
{
RecalculateSplitterPos();
} );
aEvent.Skip();
} );
m_frame->Bind( EDA_LANG_CHANGED, &PROPERTIES_PANEL::OnLanguageChanged, this );
}
PROPERTIES_PANEL::~PROPERTIES_PANEL()
{
m_frame->Unbind( EDA_LANG_CHANGED, &PROPERTIES_PANEL::OnLanguageChanged, this );
}
void PROPERTIES_PANEL::OnLanguageChanged( wxCommandEvent& aEvent )
{
if( m_grid->IsEditorFocused() )
m_grid->CommitChangesFromEditor();
m_grid->Clear();
m_displayed.clear();
UpdateData();
aEvent.Skip();
}
class SUPPRESS_GRID_CHANGED_EVENTS
{
public:
SUPPRESS_GRID_CHANGED_EVENTS( PROPERTIES_PANEL* aPanel ) :
m_panel( aPanel )
{
m_panel->m_SuppressGridChangeEvents++;
}
~SUPPRESS_GRID_CHANGED_EVENTS()
{
m_panel->m_SuppressGridChangeEvents--;
}
private:
PROPERTIES_PANEL* m_panel;
};
void PROPERTIES_PANEL::rebuildProperties( const SELECTION& aSelection )
{
SUPPRESS_GRID_CHANGED_EVENTS raii( this );
auto reset =
[&]()
{
if( m_grid->IsEditorFocused() )
m_grid->CommitChangesFromEditor();
m_grid->Clear();
m_displayed.clear();
};
if( aSelection.Empty() )
{
m_caption->SetLabel( _( "No objects selected" ) );
reset();
return;
}
else if( aSelection.Size() == 1 )
{
m_caption->SetLabel( aSelection.Front()->GetFriendlyName() );
}
else
{
m_caption->SetLabel( wxString::Format( _( "%d objects selected" ), aSelection.Size() ) );
}
// Get all the selected types
std::set<TYPE_ID> types;
for( EDA_ITEM* item : aSelection )
types.insert( TYPE_HASH( *item ) );
wxCHECK( !types.empty(), /* void */ ); // already guarded above, but Coverity doesn't know that
PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
std::set<PROPERTY_BASE*> commonProps;
const PROPERTY_LIST& allProperties = propMgr.GetProperties( *types.begin() );
copy( allProperties.begin(), allProperties.end(),
inserter( commonProps, commonProps.begin() ) );
PROPERTY_DISPLAY_ORDER displayOrder = propMgr.GetDisplayOrder( *types.begin() );
std::vector<wxString> groupDisplayOrder = propMgr.GetGroupDisplayOrder( *types.begin() );
std::set<wxString> groups( groupDisplayOrder.begin(), groupDisplayOrder.end() );
std::set<PROPERTY_BASE*> availableProps;
// Get all possible properties
for( const TYPE_ID& type : types )
{
const PROPERTY_LIST& itemProps = propMgr.GetProperties( type );
const PROPERTY_DISPLAY_ORDER& itemDisplayOrder = propMgr.GetDisplayOrder( type );
copy( itemDisplayOrder.begin(), itemDisplayOrder.end(),
inserter( displayOrder, displayOrder.begin() ) );
const std::vector<wxString>& itemGroups = propMgr.GetGroupDisplayOrder( type );
for( const wxString& group : itemGroups )
{
if( !groups.count( group ) )
{
groupDisplayOrder.emplace_back( group );
groups.insert( group );
}
}
for( auto it = commonProps.begin(); it != commonProps.end(); /* ++it in the loop */ )
{
if( !binary_search( itemProps.begin(), itemProps.end(), *it ) )
it = commonProps.erase( it );
else
++it;
}
}
EDA_ITEM* firstItem = aSelection.Front();
bool isLibraryEditor = m_frame->IsType( FRAME_FOOTPRINT_EDITOR )
|| m_frame->IsType( FRAME_SCH_SYMBOL_EDITOR );
bool isDesignEditor = m_frame->IsType( FRAME_PCB_EDITOR )
|| m_frame->IsType( FRAME_SCH );
// Find a set of properties that is common to all selected items
for( PROPERTY_BASE* property : commonProps )
{
if( property->IsHiddenFromPropertiesManager() )
continue;
if( isLibraryEditor && property->IsHiddenFromLibraryEditors() )
continue;
if( isDesignEditor && property->IsHiddenFromDesignEditors() )
continue;
if( propMgr.IsAvailableFor( TYPE_HASH( *firstItem ), property, firstItem ) )
availableProps.insert( property );
}
bool writeable = true;
std::set<PROPERTY_BASE*> existingProps;
for( wxPropertyGridIterator it = m_grid->GetIterator(); !it.AtEnd(); it.Next() )
{
wxPGProperty* pgProp = it.GetProperty();
PROPERTY_BASE* property = propMgr.GetProperty( TYPE_HASH( *firstItem ), pgProp->GetName() );
// Switching item types? Property may no longer be valid
if( !property )
continue;
wxVariant commonVal;
extractValueAndWritability( aSelection, property, commonVal, writeable );
pgProp->SetValue( commonVal );
pgProp->Enable( writeable );
existingProps.insert( property );
}
if( !existingProps.empty() && existingProps == availableProps )
return;
// Some difference exists: start from scratch
reset();
std::map<wxPGProperty*, int> pgPropOrders;
std::map<wxString, std::vector<wxPGProperty*>> pgPropGroups;
for( PROPERTY_BASE* property : availableProps )
{
wxPGProperty* pgProp = createPGProperty( property );
wxVariant commonVal;
if( !extractValueAndWritability( aSelection, property, commonVal, writeable ) )
continue;
if( pgProp )
{
pgProp->SetValue( commonVal );
pgProp->Enable( writeable );
m_displayed.push_back( property );
wxASSERT( displayOrder.count( property ) );
pgPropOrders[pgProp] = displayOrder[property];
pgPropGroups[property->Group()].emplace_back( pgProp );
}
}
const wxString unspecifiedGroupCaption = _( "Basic Properties" );
for( const wxString& groupName : groupDisplayOrder )
{
if( !pgPropGroups.count( groupName ) )
continue;
std::vector<wxPGProperty*>& properties = pgPropGroups[groupName];
wxString groupCaption = wxGetTranslation( groupName );
auto groupItem = new wxPropertyCategory( groupName.IsEmpty() ? unspecifiedGroupCaption
: groupCaption );
m_grid->Append( groupItem );
std::sort( properties.begin(), properties.end(),
[&]( wxPGProperty*& aFirst, wxPGProperty*& aSecond )
{
return pgPropOrders[aFirst] < pgPropOrders[aSecond];
} );
for( wxPGProperty* property : properties )
m_grid->Append( property );
}
RecalculateSplitterPos();
}
bool PROPERTIES_PANEL::getItemValue( EDA_ITEM* aItem, PROPERTY_BASE* aProperty, wxVariant& aValue )
{
const wxAny& any = aItem->Get( aProperty );
bool converted = false;
if( aProperty->HasChoices() )
{
// handle enums as ints, since there are no default conversion functions for wxAny
int tmp;
converted = any.GetAs<int>( &tmp );
if( converted )
aValue = wxVariant( tmp );
}
if( !converted ) // all other types
converted = any.GetAs( &aValue );
if( !converted )
{
wxString propName = aProperty->Name();
propName.Replace( ' ', '_' );
wxFAIL_MSG( wxString::Format( wxS( "Could not convert wxAny to wxVariant for %s::%s" ),
aItem->GetClass(),
propName ) );
}
return converted;
}
bool PROPERTIES_PANEL::extractValueAndWritability( const SELECTION& aSelection,
PROPERTY_BASE* aProperty,
wxVariant& aValue, bool& aWritable )
{
PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
bool different = false;
wxVariant commonVal;
aWritable = true;
for( EDA_ITEM* item : aSelection )
{
if( !propMgr.IsAvailableFor( TYPE_HASH( *item ), aProperty, item ) )
return false;
if( aProperty->IsHiddenFromPropertiesManager() )
return false;
// If read-only for any of the selection, read-only for the whole selection.
if( !propMgr.IsWriteableFor( TYPE_HASH( *item ), aProperty, item ) )
aWritable = false;
wxVariant value;
if( getItemValue( item, aProperty, value ) )
{
// Null value indicates different property values between items
if( !different && !aValue.IsNull() && value != aValue )
{
different = true;
aValue.MakeNull();
}
else if( !different )
{
aValue = value;
}
}
else
{
// getItemValue returned false -- not available for this item
return false;
}
}
return true;
}
void PROPERTIES_PANEL::onShow( wxShowEvent& aEvent )
{
if( aEvent.IsShown() )
UpdateData();
aEvent.Skip();
}
void PROPERTIES_PANEL::onCharHook( wxKeyEvent& aEvent )
{
// m_grid->IsAnyModified() doesn't work for the first modification
if( aEvent.GetKeyCode() == WXK_TAB && !aEvent.ShiftDown() )
{
wxVariant oldValue = m_grid->GetSelectedProperty()->GetValue();
m_grid->CommitChangesFromEditor();
// If there was no change, treat it as a navigation key
if( oldValue == m_grid->GetSelectedProperty()->GetValue() )
aEvent.Skip();
return;
}
if( aEvent.GetKeyCode() == WXK_SPACE )
{
if( wxPGProperty* prop = m_grid->GetSelectedProperty() )
{
if( prop->GetValueType() == wxT( "bool" ) )
{
m_grid->SetPropertyValue( prop, !prop->GetValue().GetBool() );
return;
}
}
}
if( aEvent.GetKeyCode() == WXK_RETURN || aEvent.GetKeyCode() == WXK_NUMPAD_ENTER )
{
m_grid->CommitChangesFromEditor();
/* don't skip this one; if we're not the last property we'll also go to the next row */
}
aEvent.Skip();
}
void PROPERTIES_PANEL::RecalculateSplitterPos()
{
if( m_splitter_key_proportion < 0 )
m_grid->CenterSplitter();
else
m_grid->SetSplitterPosition( m_splitter_key_proportion * m_grid->GetSize().x );
}
void PROPERTIES_PANEL::SetSplitterProportion( float aProportion )
{
m_splitter_key_proportion = aProportion;
RecalculateSplitterPos();
}
|