2020-02-02 18:29:33 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2020 CERN
|
|
|
|
* @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
|
|
|
|
* @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/>.
|
|
|
|
*/
|
|
|
|
|
2022-11-03 02:50:49 +00:00
|
|
|
#include <properties/property_mgr.h>
|
|
|
|
#include <properties/property.h>
|
2020-02-02 18:29:33 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <utility>
|
2022-11-25 21:29:56 +00:00
|
|
|
#include <wx/wx.h>
|
2020-02-02 18:29:33 +00:00
|
|
|
|
|
|
|
static wxString EMPTY_STRING( wxEmptyString );
|
|
|
|
|
|
|
|
|
|
|
|
void PROPERTY_MANAGER::RegisterType( TYPE_ID aType, const wxString& aName )
|
|
|
|
{
|
|
|
|
wxASSERT( m_classNames.count( aType ) == 0 );
|
|
|
|
m_classNames.emplace( aType, aName );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const wxString& PROPERTY_MANAGER::ResolveType( TYPE_ID aType ) const
|
|
|
|
{
|
|
|
|
auto it = m_classNames.find( aType );
|
|
|
|
return it != m_classNames.end() ? it->second : EMPTY_STRING;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PROPERTY_BASE* PROPERTY_MANAGER::GetProperty( TYPE_ID aType, const wxString& aProperty ) const
|
|
|
|
{
|
2020-07-19 21:22:49 +00:00
|
|
|
if( m_dirty )
|
|
|
|
const_cast<PROPERTY_MANAGER*>( this )->Rebuild();
|
2020-02-05 14:59:45 +00:00
|
|
|
|
2020-02-02 18:29:33 +00:00
|
|
|
auto it = m_classes.find( aType );
|
|
|
|
|
|
|
|
if( it == m_classes.end() )
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
const CLASS_DESC& classDesc = it->second;
|
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
for( PROPERTY_BASE* property : classDesc.m_allProperties )
|
2020-02-02 18:29:33 +00:00
|
|
|
{
|
2020-06-04 22:22:59 +00:00
|
|
|
if( !aProperty.CmpNoCase( property->Name() ) )
|
2020-02-02 18:29:33 +00:00
|
|
|
return property;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const PROPERTY_LIST& PROPERTY_MANAGER::GetProperties( TYPE_ID aType ) const
|
|
|
|
{
|
2020-07-19 21:22:49 +00:00
|
|
|
if( m_dirty )
|
|
|
|
const_cast<PROPERTY_MANAGER*>( this )->Rebuild();
|
2020-02-02 18:29:33 +00:00
|
|
|
|
|
|
|
static const PROPERTY_LIST empty;
|
|
|
|
auto it = m_classes.find( aType );
|
|
|
|
|
|
|
|
if( it == m_classes.end() )
|
|
|
|
return empty;
|
|
|
|
|
|
|
|
return it->second.m_allProperties;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-28 03:40:14 +00:00
|
|
|
const PROPERTY_DISPLAY_ORDER& PROPERTY_MANAGER::GetDisplayOrder( TYPE_ID aType ) const
|
|
|
|
{
|
|
|
|
if( m_dirty )
|
|
|
|
const_cast<PROPERTY_MANAGER*>( this )->Rebuild();
|
|
|
|
|
|
|
|
static const PROPERTY_DISPLAY_ORDER empty;
|
|
|
|
auto it = m_classes.find( aType );
|
|
|
|
|
|
|
|
if( it == m_classes.end() )
|
|
|
|
return empty;
|
|
|
|
|
|
|
|
return it->second.m_displayOrder;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const std::vector<wxString>& PROPERTY_MANAGER::GetGroupDisplayOrder( TYPE_ID aType ) const
|
|
|
|
{
|
|
|
|
if( m_dirty )
|
|
|
|
const_cast<PROPERTY_MANAGER*>( this )->Rebuild();
|
|
|
|
|
|
|
|
static const std::vector<wxString> empty;
|
|
|
|
auto it = m_classes.find( aType );
|
|
|
|
|
|
|
|
if( it == m_classes.end() )
|
|
|
|
return empty;
|
|
|
|
|
|
|
|
return it->second.m_groupDisplayOrder;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-02 18:29:33 +00:00
|
|
|
const void* PROPERTY_MANAGER::TypeCast( const void* aSource, TYPE_ID aBase, TYPE_ID aTarget ) const
|
|
|
|
{
|
|
|
|
if( aBase == aTarget )
|
|
|
|
return aSource;
|
|
|
|
|
|
|
|
auto classDesc = m_classes.find( aBase );
|
|
|
|
|
|
|
|
if( classDesc == m_classes.end() )
|
|
|
|
return aSource;
|
|
|
|
|
|
|
|
auto& converters = classDesc->second.m_typeCasts;
|
|
|
|
auto converter = converters.find( aTarget );
|
|
|
|
|
|
|
|
if( converter == converters.end() ) // explicit type cast not found
|
|
|
|
return IsOfType( aBase, aTarget ) ? aSource : nullptr;
|
|
|
|
|
|
|
|
return (*converter->second)( aSource );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-28 03:40:14 +00:00
|
|
|
void PROPERTY_MANAGER::AddProperty( PROPERTY_BASE* aProperty, const wxString& aGroup )
|
2020-02-02 18:29:33 +00:00
|
|
|
{
|
|
|
|
const wxString& name = aProperty->Name();
|
|
|
|
TYPE_ID hash = aProperty->OwnerHash();
|
|
|
|
CLASS_DESC& classDesc = getClass( hash );
|
|
|
|
classDesc.m_ownProperties.emplace( name, aProperty );
|
2022-12-04 22:20:34 +00:00
|
|
|
classDesc.m_ownDisplayOrder.emplace_back( aProperty );
|
2022-11-25 21:29:56 +00:00
|
|
|
|
2022-11-28 03:40:14 +00:00
|
|
|
aProperty->SetGroup( aGroup );
|
|
|
|
|
|
|
|
if( !classDesc.m_groups.count( aGroup ) )
|
|
|
|
{
|
|
|
|
classDesc.m_groupDisplayOrder.emplace_back( aGroup );
|
|
|
|
classDesc.m_groups.insert( aGroup );
|
|
|
|
}
|
2022-11-25 21:29:56 +00:00
|
|
|
|
2020-02-02 18:29:33 +00:00
|
|
|
m_dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-28 03:40:14 +00:00
|
|
|
void PROPERTY_MANAGER::ReplaceProperty( size_t aBase, const wxString& aName, PROPERTY_BASE* aNew,
|
|
|
|
const wxString& aGroup )
|
2020-02-05 14:59:45 +00:00
|
|
|
{
|
|
|
|
CLASS_DESC& classDesc = getClass( aNew->OwnerHash() );
|
|
|
|
classDesc.m_replaced.insert( std::make_pair( aBase, aName ) );
|
2022-11-28 03:40:14 +00:00
|
|
|
AddProperty( aNew, aGroup );
|
2022-11-25 21:29:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-02 18:29:33 +00:00
|
|
|
void PROPERTY_MANAGER::AddTypeCast( TYPE_CAST_BASE* aCast )
|
|
|
|
{
|
|
|
|
TYPE_ID derivedHash = aCast->DerivedHash();
|
|
|
|
CLASS_DESC& classDesc = getClass( aCast->BaseHash() );
|
|
|
|
auto& typeCasts = classDesc.m_typeCasts;
|
|
|
|
wxASSERT_MSG( typeCasts.count( derivedHash ) == 0, "Such converter already exists" );
|
|
|
|
typeCasts.emplace( derivedHash, aCast );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PROPERTY_MANAGER::InheritsAfter( TYPE_ID aDerived, TYPE_ID aBase )
|
|
|
|
{
|
|
|
|
wxASSERT_MSG( aDerived != aBase, "Class cannot inherit from itself" );
|
|
|
|
|
|
|
|
CLASS_DESC& derived = getClass( aDerived );
|
|
|
|
CLASS_DESC& base = getClass( aBase );
|
|
|
|
derived.m_bases.push_back( base );
|
|
|
|
m_dirty = true;
|
|
|
|
|
|
|
|
wxASSERT_MSG( derived.m_bases.size() == 1 || derived.m_typeCasts.count( aBase ) == 1,
|
2020-07-19 21:22:49 +00:00
|
|
|
"You need to add a TYPE_CAST for classes inheriting from multiple bases" );
|
2020-02-02 18:29:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-02 02:54:12 +00:00
|
|
|
void PROPERTY_MANAGER::Mask( TYPE_ID aDerived, TYPE_ID aBase, const wxString& aName )
|
|
|
|
{
|
|
|
|
wxASSERT_MSG( aDerived != aBase, "Class cannot mask from itself" );
|
|
|
|
|
|
|
|
CLASS_DESC& derived = getClass( aDerived );
|
|
|
|
derived.m_maskedBaseProperties.insert( std::make_pair( aBase, aName ) );
|
|
|
|
m_dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-08 05:09:39 +00:00
|
|
|
void PROPERTY_MANAGER::OverrideAvailability( TYPE_ID aDerived, TYPE_ID aBase,
|
|
|
|
const wxString& aName,
|
|
|
|
std::function<bool( INSPECTABLE* )> aFunc )
|
|
|
|
{
|
|
|
|
wxASSERT_MSG( aDerived != aBase, "Class cannot override from itself" );
|
|
|
|
|
|
|
|
CLASS_DESC& derived = getClass( aDerived );
|
|
|
|
derived.m_availabilityOverrides[std::make_pair( aBase, aName )] = aFunc;
|
|
|
|
m_dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool PROPERTY_MANAGER::IsAvailableFor( TYPE_ID aItemClass, PROPERTY_BASE* aProp,
|
|
|
|
INSPECTABLE* aItem )
|
|
|
|
{
|
|
|
|
if( !aProp->Available( aItem ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
CLASS_DESC& derived = getClass( aItemClass );
|
|
|
|
|
|
|
|
auto it = derived.m_availabilityOverrides.find( std::make_pair( aProp->BaseHash(),
|
|
|
|
aProp->Name() ) );
|
|
|
|
|
|
|
|
if( it != derived.m_availabilityOverrides.end() )
|
|
|
|
return it->second( aItem );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-02 18:29:33 +00:00
|
|
|
bool PROPERTY_MANAGER::IsOfType( TYPE_ID aDerived, TYPE_ID aBase ) const
|
|
|
|
{
|
|
|
|
if( aDerived == aBase )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
auto derived = m_classes.find( aDerived );
|
|
|
|
wxCHECK( derived != m_classes.end(), false ); // missing class description
|
|
|
|
|
|
|
|
// traverse the hierarchy seeking for the base class
|
|
|
|
for( auto& base : derived->second.m_bases )
|
|
|
|
{
|
|
|
|
if( IsOfType( base.get().m_id, aBase ) )
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PROPERTY_MANAGER::Rebuild()
|
|
|
|
{
|
2020-07-19 21:22:49 +00:00
|
|
|
for( std::pair<const TYPE_ID, CLASS_DESC>& classEntry : m_classes )
|
2020-02-02 18:29:33 +00:00
|
|
|
classEntry.second.rebuild();
|
|
|
|
|
|
|
|
m_dirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PROPERTY_MANAGER::CLASS_DESC& PROPERTY_MANAGER::getClass( TYPE_ID aTypeId )
|
|
|
|
{
|
|
|
|
auto it = m_classes.find( aTypeId );
|
|
|
|
|
|
|
|
if( it == m_classes.end() )
|
2020-02-05 14:59:45 +00:00
|
|
|
tie( it, std::ignore ) = m_classes.emplace( aTypeId, CLASS_DESC( aTypeId ) );
|
2020-02-02 18:29:33 +00:00
|
|
|
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PROPERTY_MANAGER::CLASS_DESC::rebuild()
|
|
|
|
{
|
2020-02-05 14:59:45 +00:00
|
|
|
PROPERTY_SET replaced( m_replaced );
|
2020-02-02 18:29:33 +00:00
|
|
|
m_allProperties.clear();
|
2022-12-02 02:54:12 +00:00
|
|
|
collectPropsRecur( m_allProperties, replaced, m_displayOrder, m_maskedBaseProperties );
|
2020-02-02 18:29:33 +00:00
|
|
|
// We need to keep properties sorted to be able to use std::set_* functions
|
|
|
|
sort( m_allProperties.begin(), m_allProperties.end() );
|
2022-12-02 04:18:42 +00:00
|
|
|
|
|
|
|
std::vector<wxString> displayOrder;
|
|
|
|
std::set<wxString> groups;
|
|
|
|
|
|
|
|
auto collectGroups =
|
|
|
|
[&]( std::set<wxString>& aSet, std::vector<wxString>& aResult )
|
|
|
|
{
|
|
|
|
auto collectGroupsRecursive =
|
|
|
|
[]( auto& aSelf, std::set<wxString>& aSetR, std::vector<wxString>& aResultR,
|
|
|
|
const CLASS_DESC& aClassR ) -> void
|
|
|
|
{
|
2022-12-22 22:43:40 +00:00
|
|
|
for( const wxString& group : aClassR.m_groupDisplayOrder )
|
2022-12-02 04:18:42 +00:00
|
|
|
{
|
|
|
|
if( !aSetR.count( group ) )
|
|
|
|
{
|
|
|
|
aSetR.insert( group );
|
|
|
|
aResultR.emplace_back( group );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for( const CLASS_DESC& base : aClassR.m_bases )
|
|
|
|
aSelf( aSelf, aSetR, aResultR, base );
|
|
|
|
};
|
|
|
|
|
|
|
|
collectGroupsRecursive( collectGroupsRecursive, aSet, aResult, *this );
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO(JE): This currently relies on rebuild() happening after all properties are added
|
|
|
|
// separate out own groups vs. all groups to fix
|
|
|
|
collectGroups( groups, displayOrder );
|
|
|
|
m_groupDisplayOrder = displayOrder;
|
2020-02-02 18:29:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
void PROPERTY_MANAGER::CLASS_DESC::collectPropsRecur( PROPERTY_LIST& aResult,
|
2022-11-28 03:40:14 +00:00
|
|
|
PROPERTY_SET& aReplaced,
|
2022-12-02 02:54:12 +00:00
|
|
|
PROPERTY_DISPLAY_ORDER& aDisplayOrder,
|
|
|
|
const PROPERTY_SET& aMasked ) const
|
2020-02-02 18:29:33 +00:00
|
|
|
{
|
2020-07-19 21:22:49 +00:00
|
|
|
for( const std::pair<size_t, wxString>& replacedEntry : m_replaced )
|
2020-02-05 14:59:45 +00:00
|
|
|
aReplaced.emplace( replacedEntry );
|
2020-02-02 18:29:33 +00:00
|
|
|
|
2022-11-28 03:40:14 +00:00
|
|
|
/*
|
|
|
|
* We want to insert our own properties in forward order, but earlier than anything already in
|
|
|
|
* the list (which will have been added by a subclass of us)
|
|
|
|
*/
|
2022-12-03 03:17:08 +00:00
|
|
|
int displayOrderStart = 0;
|
|
|
|
|
|
|
|
if( !aDisplayOrder.empty() )
|
|
|
|
{
|
|
|
|
int firstSoFar = std::min_element( aDisplayOrder.begin(), aDisplayOrder.end(),
|
|
|
|
[]( const std::pair<PROPERTY_BASE*, int>& aFirst,
|
|
|
|
const std::pair<PROPERTY_BASE*, int>& aSecond )
|
|
|
|
{
|
|
|
|
return aFirst.second < aSecond.second;
|
|
|
|
} )->second;
|
|
|
|
|
|
|
|
displayOrderStart = firstSoFar - m_ownProperties.size();
|
|
|
|
}
|
|
|
|
|
2022-11-28 03:40:14 +00:00
|
|
|
int idx = 0;
|
|
|
|
|
2022-12-04 22:20:34 +00:00
|
|
|
for( PROPERTY_BASE* property : m_ownDisplayOrder )
|
2020-02-05 14:59:45 +00:00
|
|
|
{
|
2022-12-02 02:54:12 +00:00
|
|
|
PROPERTY_SET::key_type propertyKey = std::make_pair( property->OwnerHash(),
|
|
|
|
property->Name() );
|
2020-02-05 14:59:45 +00:00
|
|
|
// Do not store replaced properties
|
2022-12-02 02:54:12 +00:00
|
|
|
if( aReplaced.count( propertyKey ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Do not store masked properties
|
|
|
|
if( aMasked.count( propertyKey ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
aDisplayOrder[property] = displayOrderStart + idx++;
|
|
|
|
aResult.push_back( property );
|
2020-02-05 14:59:45 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
for( const std::reference_wrapper<CLASS_DESC>& base : m_bases )
|
2022-12-02 02:54:12 +00:00
|
|
|
base.get().collectPropsRecur( aResult, aReplaced, aDisplayOrder, aMasked );
|
2020-02-02 18:29:33 +00:00
|
|
|
}
|
2020-06-07 21:51:07 +00:00
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
|
2020-06-07 21:51:07 +00:00
|
|
|
std::vector<TYPE_ID> PROPERTY_MANAGER::GetMatchingClasses( PROPERTY_BASE* aProperty )
|
|
|
|
{
|
|
|
|
std::vector<TYPE_ID> ids;
|
2020-07-19 21:22:49 +00:00
|
|
|
|
|
|
|
/*
|
2020-06-07 21:51:07 +00:00
|
|
|
for( auto& cls : m_classes )
|
|
|
|
{
|
|
|
|
CLASS_INFO info;
|
|
|
|
|
|
|
|
for( auto prop : cls.second.m_allProperties )
|
|
|
|
info.properties.push_back(prop);
|
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
|
2020-06-07 21:51:07 +00:00
|
|
|
}
|
2020-07-19 21:22:49 +00:00
|
|
|
*/
|
2020-06-07 21:51:07 +00:00
|
|
|
|
|
|
|
return ids;
|
|
|
|
}
|
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
|
2020-06-07 21:51:07 +00:00
|
|
|
PROPERTY_MANAGER::CLASSES_INFO PROPERTY_MANAGER::GetAllClasses()
|
|
|
|
{
|
|
|
|
CLASSES_INFO rv;
|
2020-07-19 21:22:49 +00:00
|
|
|
|
|
|
|
for( std::pair<const TYPE_ID, CLASS_DESC>& classEntry : m_classes )
|
2020-06-07 21:51:07 +00:00
|
|
|
{
|
|
|
|
CLASS_INFO info;
|
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
info.type = classEntry.first;
|
|
|
|
info.name = m_classNames[classEntry.first];
|
2020-06-07 21:51:07 +00:00
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
for( PROPERTY_BASE* prop : classEntry.second.m_allProperties )
|
|
|
|
info.properties.push_back( prop );
|
2020-06-07 21:51:07 +00:00
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
rv.push_back( info );
|
2020-06-07 21:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|