KiCad PCB EDA Suite
Loading...
Searching...
No Matches
property_mgr.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 CERN
5 * @author Tomasz Wlostowski <[email protected]>
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 <inspectable.h>
24#include <properties/property.h>
25
26#include <algorithm>
27#include <utility>
28#include <wx/wx.h>
29
30static wxString EMPTY_STRING( wxEmptyString );
31
32
33void PROPERTY_MANAGER::RegisterType( TYPE_ID aType, const wxString& aName )
34{
35 wxASSERT( m_classNames.count( aType ) == 0 );
36 m_classNames.emplace( aType, aName );
37}
38
39
40const wxString& PROPERTY_MANAGER::ResolveType( TYPE_ID aType ) const
41{
42 auto it = m_classNames.find( aType );
43 return it != m_classNames.end() ? it->second : EMPTY_STRING;
44}
45
46
47PROPERTY_BASE* PROPERTY_MANAGER::GetProperty( TYPE_ID aType, const wxString& aProperty ) const
48{
49 if( m_dirty )
50 const_cast<PROPERTY_MANAGER*>( this )->Rebuild();
51
52 auto it = m_classes.find( aType );
53
54 if( it == m_classes.end() )
55 return nullptr;
56
57 const CLASS_DESC& classDesc = it->second;
58
59 for( PROPERTY_BASE* property : classDesc.m_allProperties )
60 {
61 if( !aProperty.CmpNoCase( property->Name() ) )
62 return property;
63 }
64
65 return nullptr;
66}
67
68
70{
71 if( m_dirty )
72 const_cast<PROPERTY_MANAGER*>( this )->Rebuild();
73
74 static const PROPERTY_LIST empty;
75 auto it = m_classes.find( aType );
76
77 if( it == m_classes.end() )
78 return empty;
79
80 return it->second.m_allProperties;
81}
82
83
85{
86 if( m_dirty )
87 const_cast<PROPERTY_MANAGER*>( this )->Rebuild();
88
89 static const PROPERTY_DISPLAY_ORDER empty;
90 auto it = m_classes.find( aType );
91
92 if( it == m_classes.end() )
93 return empty;
94
95 return it->second.m_displayOrder;
96}
97
98
99const std::vector<wxString>& PROPERTY_MANAGER::GetGroupDisplayOrder( TYPE_ID aType ) const
100{
101 if( m_dirty )
102 const_cast<PROPERTY_MANAGER*>( this )->Rebuild();
103
104 static const std::vector<wxString> empty;
105 auto it = m_classes.find( aType );
106
107 if( it == m_classes.end() )
108 return empty;
109
110 return it->second.m_groupDisplayOrder;
111}
112
113
114const void* PROPERTY_MANAGER::TypeCast( const void* aSource, TYPE_ID aBase, TYPE_ID aTarget ) const
115{
116 if( aBase == aTarget )
117 return aSource;
118
119 auto classDesc = m_classes.find( aBase );
120
121 if( classDesc == m_classes.end() )
122 return aSource;
123
124 auto& converters = classDesc->second.m_typeCasts;
125 auto converter = converters.find( aTarget );
126
127 if( converter == converters.end() ) // explicit type cast not found
128 return IsOfType( aBase, aTarget ) ? aSource : nullptr;
129
130 return (*converter->second)( aSource );
131}
132
133
134PROPERTY_BASE& PROPERTY_MANAGER::AddProperty( PROPERTY_BASE* aProperty, const wxString& aGroup )
135{
136 const wxString& name = aProperty->Name();
137 TYPE_ID hash = aProperty->OwnerHash();
138 CLASS_DESC& classDesc = getClass( hash );
139 classDesc.m_ownProperties.emplace( name, aProperty );
140 classDesc.m_ownDisplayOrder.emplace_back( aProperty );
141
142 aProperty->SetGroup( aGroup );
143
144 if( !classDesc.m_groups.count( aGroup ) )
145 {
146 classDesc.m_groupDisplayOrder.emplace_back( aGroup );
147 classDesc.m_groups.insert( aGroup );
148 }
149
150 m_dirty = true;
151 return *aProperty;
152}
153
154
155PROPERTY_BASE& PROPERTY_MANAGER::ReplaceProperty( size_t aBase, const wxString& aName,
156 PROPERTY_BASE* aNew, const wxString& aGroup )
157{
158 CLASS_DESC& classDesc = getClass( aNew->OwnerHash() );
159 classDesc.m_replaced.insert( std::make_pair( aBase, aName ) );
160 return AddProperty( aNew, aGroup );
161}
162
163
165{
166 TYPE_ID derivedHash = aCast->DerivedHash();
167 CLASS_DESC& classDesc = getClass( aCast->BaseHash() );
168 auto& typeCasts = classDesc.m_typeCasts;
169 wxASSERT_MSG( typeCasts.count( derivedHash ) == 0, "Such converter already exists" );
170 typeCasts.emplace( derivedHash, aCast );
171}
172
173
175{
176 wxASSERT_MSG( aDerived != aBase, "Class cannot inherit from itself" );
177
178 CLASS_DESC& derived = getClass( aDerived );
179 CLASS_DESC& base = getClass( aBase );
180 derived.m_bases.push_back( base );
181 m_dirty = true;
182
183 wxASSERT_MSG( derived.m_bases.size() == 1 || derived.m_typeCasts.count( aBase ) == 1,
184 "You need to add a TYPE_CAST for classes inheriting from multiple bases" );
185}
186
187
188void PROPERTY_MANAGER::Mask( TYPE_ID aDerived, TYPE_ID aBase, const wxString& aName )
189{
190 wxASSERT_MSG( aDerived != aBase, "Class cannot mask from itself" );
191
192 CLASS_DESC& derived = getClass( aDerived );
193 derived.m_maskedBaseProperties.insert( std::make_pair( aBase, aName ) );
194 m_dirty = true;
195}
196
197
199 const wxString& aName,
200 std::function<bool( INSPECTABLE* )> aFunc )
201{
202 wxASSERT_MSG( aDerived != aBase, "Class cannot override from itself" );
203
204 CLASS_DESC& derived = getClass( aDerived );
205 derived.m_availabilityOverrides[std::make_pair( aBase, aName )] = std::move( aFunc );
206 m_dirty = true;
207}
208
209
211 const wxString& aName,
212 std::function<bool( INSPECTABLE* )> aFunc )
213{
214 wxASSERT_MSG( aDerived != aBase, "Class cannot override from itself" );
215
216 CLASS_DESC& derived = getClass( aDerived );
217 derived.m_writeabilityOverrides[std::make_pair( aBase, aName )] = std::move( aFunc );
218 m_dirty = true;
219}
220
221
223 INSPECTABLE* aItem )
224{
225 if( !aProp->Available( aItem ) )
226 return false;
227
228 CLASS_DESC& derived = getClass( aItemClass );
229
230 auto it = derived.m_availabilityOverrides.find( std::make_pair( aProp->BaseHash(),
231 aProp->Name() ) );
232
233 if( it != derived.m_availabilityOverrides.end() )
234 return it->second( aItem );
235
236 return true;
237}
238
239
241 INSPECTABLE* aItem )
242{
243 if( !aProp->Writeable( aItem ) )
244 return false;
245
246 CLASS_DESC& derived = getClass( aItemClass );
247
248 auto it = derived.m_writeabilityOverrides.find( std::make_pair( aProp->BaseHash(),
249 aProp->Name() ) );
250
251 if( it != derived.m_writeabilityOverrides.end() )
252 return it->second( aItem );
253
254 return true;
255}
256
257
258bool PROPERTY_MANAGER::IsOfType( TYPE_ID aDerived, TYPE_ID aBase ) const
259{
260 if( aDerived == aBase )
261 return true;
262
263 auto derived = m_classes.find( aDerived );
264 wxCHECK( derived != m_classes.end(), false ); // missing class description
265
266 // traverse the hierarchy seeking for the base class
267 for( auto& base : derived->second.m_bases )
268 {
269 if( IsOfType( base.get().m_id, aBase ) )
270 return true;
271 }
272
273 return false;
274}
275
276
278{
279 for( std::pair<const TYPE_ID, CLASS_DESC>& classEntry : m_classes )
280 classEntry.second.rebuild();
281
282 m_dirty = false;
283}
284
285
287{
288 auto it = m_classes.find( aTypeId );
289
290 if( it == m_classes.end() )
291 tie( it, std::ignore ) = m_classes.emplace( aTypeId, CLASS_DESC( aTypeId ) );
292
293 return it->second;
294}
295
296
298{
299 PROPERTY_SET replaced;
300 PROPERTY_SET masked;
301 m_allProperties.clear();
303 // We need to keep properties sorted to be able to use std::set_* functions
304 sort( m_allProperties.begin(), m_allProperties.end() );
305
306 std::vector<wxString> displayOrder;
307 std::set<wxString> groups;
308
309 auto collectGroups =
310 [&]( std::set<wxString>& aSet, std::vector<wxString>& aResult )
311 {
312 auto collectGroupsRecursive =
313 []( auto& aSelf, std::set<wxString>& aSetR, std::vector<wxString>& aResultR,
314 const CLASS_DESC& aClassR ) -> void
315 {
316 for( const wxString& group : aClassR.m_groupDisplayOrder )
317 {
318 if( !aSetR.count( group ) )
319 {
320 aSetR.insert( group );
321 aResultR.emplace_back( group );
322 }
323 }
324
325 for( const CLASS_DESC& base : aClassR.m_bases )
326 aSelf( aSelf, aSetR, aResultR, base );
327 };
328
329 collectGroupsRecursive( collectGroupsRecursive, aSet, aResult, *this );
330 };
331
332 // TODO(JE): This currently relies on rebuild() happening after all properties are added
333 // separate out own groups vs. all groups to fix
334 collectGroups( groups, displayOrder );
335 m_groupDisplayOrder = displayOrder;
336}
337
338
340 PROPERTY_SET& aReplaced,
341 PROPERTY_DISPLAY_ORDER& aDisplayOrder,
342 PROPERTY_SET& aMasked ) const
343{
344 for( const std::pair<size_t, wxString>& replacedEntry : m_replaced )
345 aReplaced.emplace( replacedEntry );
346
347 for( const std::pair<size_t, wxString>& maskedEntry : m_maskedBaseProperties )
348 aMasked.emplace( maskedEntry );
349
350 /*
351 * We want to insert our own properties in forward order, but earlier than anything already in
352 * the list (which will have been added by a subclass of us)
353 */
354 int displayOrderStart = 0;
355
356 if( !aDisplayOrder.empty() )
357 {
358 int firstSoFar = std::min_element( aDisplayOrder.begin(), aDisplayOrder.end(),
359 []( const std::pair<PROPERTY_BASE*, int>& aFirst,
360 const std::pair<PROPERTY_BASE*, int>& aSecond )
361 {
362 return aFirst.second < aSecond.second;
363 } )->second;
364
365 displayOrderStart = firstSoFar - m_ownProperties.size();
366 }
367
368 int idx = 0;
369
370 for( PROPERTY_BASE* property : m_ownDisplayOrder )
371 {
372 PROPERTY_SET::key_type propertyKey = std::make_pair( property->OwnerHash(),
373 property->Name() );
374 // Do not store replaced properties
375 if( aReplaced.count( propertyKey ) )
376 continue;
377
378 // Do not store masked properties
379 if( aMasked.count( propertyKey ) )
380 continue;
381
382 aDisplayOrder[property] = displayOrderStart + idx++;
383 aResult.push_back( property );
384 }
385
386 // Iterate backwards so that replaced properties appear before base properties
387 for( auto it = m_bases.rbegin(); it != m_bases.rend(); ++it )
388 it->get().collectPropsRecur( aResult, aReplaced, aDisplayOrder, aMasked );
389}
390
391
392std::vector<TYPE_ID> PROPERTY_MANAGER::GetMatchingClasses( PROPERTY_BASE* aProperty )
393{
394 std::vector<TYPE_ID> ids;
395
396/*
397 for( auto& cls : m_classes )
398 {
399 CLASS_INFO info;
400
401 for( auto prop : cls.second.m_allProperties )
402 info.properties.push_back(prop);
403
404
405 }
406 */
407
408 return ids;
409}
410
411
413{
414 CLASSES_INFO rv;
415
416 for( std::pair<const TYPE_ID, CLASS_DESC>& classEntry : m_classes )
417 {
419
420 info.type = classEntry.first;
421 info.name = m_classNames[classEntry.first];
422
423 for( PROPERTY_BASE* prop : classEntry.second.m_allProperties )
424 info.properties.push_back( prop );
425
426 rv.push_back( info );
427 }
428
429 return rv;
430}
431
432
434{
435 auto callListeners =
436 [&]( TYPE_ID typeId )
437 {
438 auto listeners = m_listeners.find( typeId );
439
440 if( listeners != m_listeners.end() )
441 {
442 for( const PROPERTY_LISTENER& listener : listeners->second )
443 listener( aObject, aProperty, m_managedCommit );
444 }
445 };
446
447 CLASS_DESC& objectClass = getClass( TYPE_HASH( *aObject ) );
448
449 callListeners( objectClass.m_id );
450
451 for( CLASS_DESC& superClass : objectClass.m_bases )
452 callListeners( superClass.m_id );
453}
454
455
457{
458 wxCHECK2_MSG( PROPERTY_MANAGER::Instance().m_managedCommit == nullptr,
459 return, "Can't have more than one managed commit at a time!" );
460
462}
463
464
466{
467 wxASSERT_MSG( PROPERTY_MANAGER::Instance().m_managedCommit != nullptr,
468 "Something went wrong: m_managedCommit already null!" );
469
471}
const char * name
Definition: DXF_plotter.cpp:57
Represent a set of changes (additions, deletions or modifications) of a data model (e....
Definition: commit.h:74
Class that other classes need to inherit from, in order to be inspectable.
Definition: inspectable.h:36
PROPERTY_BASE & SetGroup(const wxString &aGroup)
Definition: property.h:328
virtual size_t BaseHash() const =0
Return type-id of the Base class.
virtual bool Writeable(INSPECTABLE *aObject) const
Definition: property.h:263
bool Available(INSPECTABLE *aObject) const
Return true if aObject offers this PROPERTY.
Definition: property.h:249
const wxString & Name() const
Definition: property.h:217
virtual size_t OwnerHash() const =0
Return type-id of the Owner class.
PROPERTY_COMMIT_HANDLER(COMMIT *aCommit)
Provide class metadata.Helper macro to map type hashes to names.
Definition: property_mgr.h:85
bool m_dirty
Flag indicating that the list of properties needs to be rebuild (RebuildProperties())
Definition: property_mgr.h:362
CLASSES_INFO GetAllClasses()
std::vector< CLASS_INFO > CLASSES_INFO
Definition: property_mgr.h:257
const wxString & ResolveType(TYPE_ID aType) const
Return name of a type.
const PROPERTY_LIST & GetProperties(TYPE_ID aType) const
Return all properties for a specific type.
void InheritsAfter(TYPE_ID aDerived, TYPE_ID aBase)
Declare an inheritance relationship between types.
const PROPERTY_DISPLAY_ORDER & GetDisplayOrder(TYPE_ID aType) const
bool IsWriteableFor(TYPE_ID aItemClass, PROPERTY_BASE *aProp, INSPECTABLE *aItem)
Checks overriden availability and original availability of a property, returns false if the property ...
void PropertyChanged(INSPECTABLE *aObject, PROPERTY_BASE *aProperty)
Callback to alert the notification system that a property has changed.
COMMIT * m_managedCommit
Definition: property_mgr.h:366
void Mask(TYPE_ID aDerived, TYPE_ID aBase, const wxString &aName)
Sets a base class property as masked in a derived class.
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:87
PROPERTY_BASE & AddProperty(PROPERTY_BASE *aProperty, const wxString &aGroup=wxEmptyString)
Register a property.
void Rebuild()
Rebuild the list of all registered properties.
PROPERTY_BASE * GetProperty(TYPE_ID aType, const wxString &aProperty) const
Return a property for a specific type.
std::unordered_map< TYPE_ID, CLASS_DESC > m_classes
Definition: property_mgr.h:359
const void * TypeCast(const void *aSource, TYPE_ID aBase, TYPE_ID aTarget) const
Cast a type to another type.
CLASS_DESC & getClass(TYPE_ID aTypeId)
void RegisterType(TYPE_ID aType, const wxString &aName)
Associate a name with a 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 ...
std::vector< TYPE_ID > GetMatchingClasses(PROPERTY_BASE *aProperty)
std::map< TYPE_ID, std::vector< PROPERTY_LISTENER > > m_listeners
Definition: property_mgr.h:364
void OverrideAvailability(TYPE_ID aDerived, TYPE_ID aBase, const wxString &aName, std::function< bool(INSPECTABLE *)> aFunc)
Sets an override availability functor for a base class property of a given derived class.
bool IsOfType(TYPE_ID aDerived, TYPE_ID aBase) const
Return true if aDerived is inherited from aBase.
PROPERTY_BASE & ReplaceProperty(size_t aBase, const wxString &aName, PROPERTY_BASE *aNew, const wxString &aGroup=wxEmptyString)
Replace an existing property for a specific type.
void OverrideWriteability(TYPE_ID aDerived, TYPE_ID aBase, const wxString &aName, std::function< bool(INSPECTABLE *)> aFunc)
Sets an override writeability functor for a base class property of a given derived class.
std::unordered_map< TYPE_ID, wxString > m_classNames
Map of all available types.
Definition: property_mgr.h:356
const std::vector< wxString > & GetGroupDisplayOrder(TYPE_ID aType) const
void AddTypeCast(TYPE_CAST_BASE *aCast)
Register a type converter.
virtual size_t BaseHash() const =0
virtual size_t DerivedHash() const =0
static bool empty(const wxTextEntryBase *aCtrl)
#define TYPE_HASH(x)
Definition: property.h:71
static wxString EMPTY_STRING(wxEmptyString)
std::function< void(INSPECTABLE *, PROPERTY_BASE *, COMMIT *)> PROPERTY_LISTENER
Definition: property_mgr.h:60
std::vector< PROPERTY_BASE * > PROPERTY_LIST
Definition: property_mgr.h:49
std::map< PROPERTY_BASE *, int > PROPERTY_DISPLAY_ORDER
Definition: property_mgr.h:58
std::set< std::pair< size_t, wxString > > PROPERTY_SET
Definition: property_mgr.h:51
size_t TYPE_ID
Unique type identifier.
Definition: property_mgr.h:47
Returns metadata for a specific type.
Definition: property_mgr.h:296
std::vector< std::reference_wrapper< CLASS_DESC > > m_bases
Properties unique to this type (i.e. not inherited)
Definition: property_mgr.h:308
PROPERTY_SET m_replaced
Recreates the list of properties.
Definition: property_mgr.h:341
PROPERTY_SET m_maskedBaseProperties
Overrides for base class property availabilities.
Definition: property_mgr.h:317
std::vector< wxString > m_groupDisplayOrder
Non-owning list of classes's direct properties in display order.
Definition: property_mgr.h:332
std::map< wxString, std::unique_ptr< PROPERTY_BASE > > m_ownProperties
Type converters available for this type.
Definition: property_mgr.h:311
std::vector< PROPERTY_BASE * > m_ownDisplayOrder
The property groups provided by this class.
Definition: property_mgr.h:335
std::vector< PROPERTY_BASE * > m_allProperties
Compiled display order for all properties.
Definition: property_mgr.h:326
PROPERTY_DISPLAY_ORDER m_displayOrder
List of property groups provided by this class in display order.
Definition: property_mgr.h:329
PROPERTY_FUNCTOR_MAP m_writeabilityOverrides
All properties (both unique to the type and inherited)
Definition: property_mgr.h:323
std::map< TYPE_ID, std::unique_ptr< TYPE_CAST_BASE > > m_typeCasts
Properties from bases that should be masked (hidden) on this subclass.
Definition: property_mgr.h:314
void rebuild()
Traverses the class inheritance hierarchy bottom-to-top, gathering all properties available to a type...
void collectPropsRecur(PROPERTY_LIST &aResult, PROPERTY_SET &aReplaced, PROPERTY_DISPLAY_ORDER &aDisplayOrder, PROPERTY_SET &aMasked) const
PROPERTY_FUNCTOR_MAP m_availabilityOverrides
Overrides for base class property writeable status.
Definition: property_mgr.h:320
const TYPE_ID m_id
Types after which this type inherits.
Definition: property_mgr.h:305
std::set< wxString > m_groups
Replaced properties (TYPE_ID / name)
Definition: property_mgr.h:338