37#include <wx/validate.h>
38#include <wx/propgrid/property.h>
45#include <unordered_map>
49#include <wx/translation.h>
74#define TYPE_HASH( x ) typeid( x ).hash_code()
75#define TYPE_NAME( x ) typeid( x ).name()
78template<
typename Owner,
typename T>
87template<
typename Owner,
typename T,
typename FuncType>
99 return ( aOwner->*
m_func )();
106template<
typename Owner,
typename T>
115template<
typename Owner,
typename T,
typename FuncType>
127 ( aOwner->*
m_func )( aValue );
134#if defined( _MSC_VER )
135#pragma warning( push )
136#pragma warning( disable : 5266 )
139template<
typename Owner,
typename T,
typename Base = Owner>
191#if defined( _MSC_VER )
192#pragma warning( pop )
212 m_writeableFunc( [](
INSPECTABLE*)->
bool {
return true; } ),
213 m_validator( NullValidator )
229 static wxPGChoices
empty;
386 void set(
void* aObject,
T aValue )
393 if( std::is_same<T, wxVariant>::value )
395 wxVariant var =
static_cast<wxVariant
>( aValue );
396 wxAny pv =
getter( aObject );
398 if( pv.CheckType<
unsigned>() )
400 a =
static_cast<unsigned>( var.GetLong() );
402 else if( pv.CheckType<std::optional<int>>() )
407 else if( pv.CheckType<std::optional<double>>() )
428 T get(
const void* aObject )
const
430 wxAny a =
getter( aObject );
433 if( a.CheckType<
bool>() )
434 a = a.RawAs<
bool>() ? 1 : 0;
436 if ( !( std::is_enum<T>::value && a.CheckType<
int>() ) && !a.CheckType<
T>() )
437 throw std::invalid_argument(
"Invalid requested type" );
439 return wxANY_AS( a,
T );
443 virtual void setter(
void* aObject, wxAny& aValue ) = 0;
444 virtual wxAny
getter(
const void* aObject )
const = 0;
483template<
typename Owner,
typename T,
typename Base = Owner>
489 template<
typename SetType,
typename GetType>
491 void ( Base::*aSetter )( SetType ),
492 GetType( Base::*aGetter )(),
496 METHOD<Owner,
T, Base>::Wrap( aSetter ),
497 METHOD<Owner,
T, Base>::Wrap( aGetter ),
498 aDisplay, aCoordType )
502 template<
typename SetType,
typename GetType>
504 void ( Base::*aSetter )( SetType ),
505 GetType( Base::*aGetter )()
const,
509 METHOD<Owner,
T, Base>::Wrap( aSetter ),
510 METHOD<Owner,
T, Base>::Wrap( aGetter ),
511 aDisplay, aCoordType )
551 virtual void setter(
void* obj, wxAny& v )
override
557 if( !v.GetAs( &value ) )
558 throw std::invalid_argument(
"Invalid type requested" );
560 Owner* o =
reinterpret_cast<Owner*
>( obj );
561 (*m_setter)( o, value );
564 virtual wxAny
getter(
const void* obj )
const override
566 const Owner* o =
reinterpret_cast<const Owner*
>( obj );
567 wxAny
res = (*m_getter)( o );
588template<
typename Owner,
typename T,
typename Base = Owner>
606 void setter(
void* obj, wxAny& v )
override
610 wxAny
getter(
const void* obj )
const override
617template<
typename Owner,
typename T,
typename Base = Owner>
621 template<
typename SetType,
typename GetType>
623 void ( Base::*aSetter )( SetType ),
624 GetType( Base::*aGetter )(),
627 METHOD<Owner,
T, Base>::Wrap( aSetter ),
628 METHOD<Owner,
T, Base>::Wrap( aGetter ),
632 if ( std::is_enum<T>::value )
636 wxASSERT_MSG(
m_choices.GetCount() > 0, wxT(
"No enum choices defined" ) );
640 template<
typename SetType,
typename GetType>
642 void ( Base::*aSetter )( SetType ),
643 GetType( Base::*aGetter )()
const,
647 METHOD<Owner,
T, Base>::Wrap( aSetter ),
648 METHOD<Owner,
T, Base>::Wrap( aGetter ),
649 aDisplay, aCoordType ),
652 if ( std::is_enum<T>::value )
656 wxASSERT_MSG(
m_choices.GetCount() > 0, wxT(
"No enum choices defined" ) );
660 void setter(
void* obj, wxAny& v )
override
663 Owner* o =
reinterpret_cast<Owner*
>( obj );
665 if( v.CheckType<
T>() )
667 T value = wxANY_AS(v,
T);
670 else if (v.CheckType<
int>() )
672 int value = wxANY_AS(v,
int);
677 throw std::invalid_argument(
"Invalid type requested" );
681 wxAny
getter(
const void* obj )
const override
683 const Owner* o =
reinterpret_cast<const Owner*
>( obj );
701 for(
unsigned ii = 0; ii < choices.GetCount(); ++ii )
702 m_choices.Add( wxGetTranslation( choices.GetLabel( ii ) ), choices.GetValue( ii ) );
714 return Choices().GetCount() > 0;
728 virtual const void*
operator()(
const void* aPointer )
const = 0;
734template<
typename Base,
typename Derived>
744 Base* base =
reinterpret_cast<Base*
>( aPointer );
745 return static_cast<Derived*
>( base );
748 const void*
operator()(
const void* aPointer )
const override
750 const Base* base =
reinterpret_cast<const Base*
>( aPointer );
751 return static_cast<const Derived*
>( base );
778 m_choices.Add( aName,
static_cast<int>( aValue ) );
793 static const wxString s_undef =
"UNDEFINED";
795 int idx =
m_choices.Index(
static_cast<int>( value ) );
797 if( idx >= 0 && idx < (
int)
m_choices.GetCount() )
798 return m_choices.GetLabel(
static_cast<int>( idx ) );
805 int idx =
m_choices.Index(
static_cast<int>( value ) );
807 if( idx >= 0 && idx < (
int)
m_choices.GetCount() )
838#define DECLARE_ENUM_TO_WXANY( type ) \
840 class wxAnyValueTypeImpl<type> : public wxAnyValueTypeImplBase<type> \
842 WX_DECLARE_ANY_VALUE_TYPE( wxAnyValueTypeImpl<type> ) \
844 wxAnyValueTypeImpl() : \
845 wxAnyValueTypeImplBase<type>() \
848 virtual ~wxAnyValueTypeImpl() {} \
849 virtual bool ConvertValue( const wxAnyValueBuffer& src, wxAnyValueType* dstType, \
850 wxAnyValueBuffer& dst ) const override \
852 type value = GetValue( src ); \
853 ENUM_MAP<type>& conv = ENUM_MAP<type>::Instance(); \
854 if( !conv.IsValueDefined( value ) && value != conv.GetUndefined() ) \
858 if( dstType->CheckType<wxString>() ) \
860 wxAnyValueTypeImpl<wxString>::SetValue( conv.ToString( value ), dst ); \
863 if( dstType->CheckType<int>() ) \
865 wxAnyValueTypeImpl<int>::SetValue( static_cast<int>( value ), dst ); \
875#define IMPLEMENT_ENUM_TO_WXANY( type ) WX_IMPLEMENT_ANY_VALUE_TYPE( wxAnyValueTypeImpl<type> )
877#define ENUM_TO_WXANY( type ) \
878 DECLARE_ENUM_TO_WXANY( type ) \
879 IMPLEMENT_ENUM_TO_WXANY( type )
882#define NO_SETTER( owner, type ) ( ( void ( owner::* )( type ) ) nullptr )
const KIGFX::COLOR4D & Color()
size_t BaseHash() const override
Return type-id of the Base class.
size_t TypeHash() const override
Return type-id of the property type.
typename std::decay< T >::type BASE_TYPE
void setter(void *obj, wxAny &v) override
bool IgnoreValue() const override
DUMMY_PROPERTY(const wxString &aName)
size_t OwnerHash() const override
Return type-id of the Owner class.
wxAny getter(const void *obj) const override
const EDA_ANGLE & Angle()
A base class for most all the KiCad significant classes used in schematics and boards.
T ToEnum(const wxString value)
ENUM_MAP & Map(T aValue, const wxString &aName)
std::unordered_map< wxString, T > m_reverseMap
static ENUM_MAP< T > & Instance()
const wxString & ToString(T value) const
bool IsValueDefined(T value) const
ENUM_MAP & Undefined(T aValue)
virtual T operator()(const Owner *aOwner) const =0
T operator()(const Owner *aOwner) const override
Class that other classes need to inherit from, in order to be inspectable.
A color representation with 4 components: red, green, blue, alpha.
static constexpr GETTER_BASE< Owner, T > * Wrap(const T(Base::*aFunc)())
static GETTER_BASE< Owner, T > * Wrap(T(Base::*aFunc)())
static constexpr GETTER_BASE< Owner, T > * Wrap(const T(Base::*aFunc)() const)
static constexpr GETTER_BASE< Owner, T > * Wrap(T(Base::*aFunc)() const)
static constexpr SETTER_BASE< Owner, T > * Wrap(void(Base::*aFunc)(const T &))
static constexpr GETTER_BASE< Owner, T > * Wrap(const T &(Base::*aFunc)())
static constexpr GETTER_BASE< Owner, T > * Wrap(const T &(Base::*aFunc)() const)
static constexpr SETTER_BASE< Owner, T > * Wrap(void(Base::*aFunc)(T))
static constexpr SETTER_BASE< Owner, T > * Wrap(void(Base::*aFunc)(T &))
PROPERTY_BASE & SetChoicesFunc(std::function< wxPGChoices(INSPECTABLE *)> aFunc)
bool IsHiddenFromPropertiesManager() const
virtual size_t TypeHash() const =0
Return type-id of the property type.
bool m_hideFromLibraryEditors
const wxString m_name
Permanent identifier for this property.
VALIDATOR_RESULT Validate(const wxAny &&aValue, EDA_ITEM *aItem)
virtual void TranslateChoices()
std::function< bool(INSPECTABLE *)> m_writeableFunc
Eval to determine if prop is read-only.
std::function< bool(INSPECTABLE *)> m_availFunc
Eval to determine if prop is available.
PROPERTY_VALIDATOR_FN m_validator
bool IsHiddenFromDesignEditors() const
PROPERTY_BASE & SetAvailableFunc(std::function< bool(INSPECTABLE *)> aFunc)
Set a callback function to determine whether an object provides this property.
PROPERTY_BASE & SetDisplay(PROPERTY_DISPLAY aDisplay)
std::function< wxPGChoices(INSPECTABLE *)> m_choicesFunc
PROPERTY_DISPLAY Display() const
virtual bool IgnoreValue() const
PROPERTY_BASE(const wxString &aName, PROPERTY_DISPLAY aDisplay=PT_DEFAULT, ORIGIN_TRANSFORMS::COORD_TYPES_T aCoordType=ORIGIN_TRANSFORMS::NOT_A_COORD)
< Used to generate unique IDs. Must come up front so it's initialized before ctor.
static VALIDATOR_RESULT NullValidator(const wxAny &&aValue, EDA_ITEM *aItem)
PROPERTY_BASE & SetWriteableFunc(std::function< bool(INSPECTABLE *)> aFunc)
PROPERTY_BASE & SetGroup(const wxString &aGroup)
ORIGIN_TRANSFORMS::COORD_TYPES_T CoordType() const
virtual wxAny getter(const void *aObject) const =0
virtual size_t BaseHash() const =0
Return type-id of the Base class.
PROPERTY_BASE & SetCoordType(ORIGIN_TRANSFORMS::COORD_TYPES_T aType)
PROPERTY_DISPLAY m_display
The display style controls how properties are edited in the properties manager GUI.
virtual bool HasChoices() const
Return true if this PROPERTY has a limited set of possible values.
ORIGIN_TRANSFORMS::COORD_TYPES_T m_coordType
The coordinate type controls how distances are mapped to the user coordinate system.
virtual bool Writeable(INSPECTABLE *aObject) const
bool IsCopyable() const
True for a property that says how an item looks, not what or where it is.
wxPGChoices GetChoices(INSPECTABLE *aObject) const
virtual void setter(void *aObject, wxAny &aValue)=0
wxString m_group
Optional group identifier.
bool Available(INSPECTABLE *aObject) const
Return true if aObject offers this PROPERTY.
PROPERTY_BASE & SetValidator(PROPERTY_VALIDATOR_FN &&aValidator)
PROPERTY_BASE & SetIsCopyable(bool aIsCopyable=true)
bool m_hideFromPropertiesManager
bool m_hideFromDesignEditors
PROPERTY_BASE & SetIsHiddenFromDesignEditors(bool aIsHidden=true)
PROPERTY_BASE & SetIsHiddenFromPropertiesManager(bool aHide=true)
bool m_hideFromRulesEditor
void set(void *aObject, T aValue)
const wxString & Name() const
bool IsHiddenFromRulesEditor() const
virtual const wxPGChoices & Choices() const
Return a limited set of possible values (e.g.
virtual void SetChoices(const wxPGChoices &aChoices)
Set the possible values for for the property.
virtual size_t OwnerHash() const =0
Return type-id of the Owner class.
PROPERTY_BASE & SetIsHiddenFromRulesEditor(bool aHide=true)
bool IsHiddenFromLibraryEditors() const
T get(const void *aObject) const
PROPERTY_BASE & SetIsHiddenFromLibraryEditors(bool aIsHidden=true)
const wxPGChoices & Choices() const override
Return a limited set of possible values (e.g.
bool HasChoices() const override
Return true if this PROPERTY has a limited set of possible values.
PROPERTY_ENUM(const wxString &aName, void(Base::*aSetter)(SetType), GetType(Base::*aGetter)() const, PROPERTY_DISPLAY aDisplay=PT_DEFAULT, ORIGIN_TRANSFORMS::COORD_TYPES_T aCoordType=ORIGIN_TRANSFORMS::NOT_A_COORD)
void setter(void *obj, wxAny &v) override
PROPERTY_ENUM(const wxString &aName, void(Base::*aSetter)(SetType), GetType(Base::*aGetter)(), PROPERTY_DISPLAY aDisplay=PT_DEFAULT)
wxAny getter(const void *obj) const override
Set method.
void SetChoices(const wxPGChoices &aChoices) override
Set the possible values for for the property.
void TranslateChoices() override
bool m_choicesFromENUM_MAP
std::unique_ptr< SETTER_BASE< Owner, T > > m_setter
Get method.
const size_t m_baseHash
Property value type-id.
size_t TypeHash() const override
Return type-id of the property type.
typename std::decay< T >::type BASE_TYPE
virtual void setter(void *obj, wxAny &v) override
size_t BaseHash() const override
Return type-id of the Base class.
bool Writeable(INSPECTABLE *aObject) const override
virtual wxAny getter(const void *obj) const override
Set method.
PROPERTY(const wxString &aName, void(Base::*aSetter)(SetType), GetType(Base::*aGetter)() const, PROPERTY_DISPLAY aDisplay=PT_DEFAULT, ORIGIN_TRANSFORMS::COORD_TYPES_T aCoordType=ORIGIN_TRANSFORMS::NOT_A_COORD)
std::unique_ptr< GETTER_BASE< Owner, T > > m_getter
Owner class type-id.
PROPERTY(const wxString &aName, void(Base::*aSetter)(SetType), GetType(Base::*aGetter)(), PROPERTY_DISPLAY aDisplay=PT_DEFAULT, ORIGIN_TRANSFORMS::COORD_TYPES_T aCoordType=ORIGIN_TRANSFORMS::NOT_A_COORD)
PROPERTY(const wxString &aName, SETTER_BASE< Owner, T > *s, GETTER_BASE< Owner, T > *g, PROPERTY_DISPLAY aDisplay, ORIGIN_TRANSFORMS::COORD_TYPES_T aCoordType)
size_t OwnerHash() const override
Return type-id of the Owner class.
const size_t m_ownerHash
Base class type-id.
virtual void operator()(Owner *aOwner, T aValue)=0
void operator()(Owner *aOwner, T aValue) override
std::optional< double > Value() const
std::optional< int > Value() const
virtual const void * operator()(const void *aPointer) const =0
virtual size_t BaseHash() const =0
virtual void * operator()(void *aPointer) const =0
virtual ~TYPE_CAST_BASE()
virtual size_t DerivedHash() const =0
size_t DerivedHash() const override
const void * operator()(const void *aPointer) const override
size_t BaseHash() const override
void * operator()(void *aPointer) const override
static bool empty(const wxTextEntryBase *aCtrl)
PROPERTY_DISPLAY
Common property types.
@ PT_DEGREE
Angle expressed in degrees.
@ PT_COORD
Coordinate expressed in distance units (mm/inch)
@ PT_DECIDEGREE
Angle expressed in decidegrees.
@ PT_AREA
Area expressed in distance units-squared (mm/inch)
@ PT_DEFAULT
Default property for a given type.
@ PT_SIZE
Size expressed in distance units (mm/inch)
@ PT_NET
Net selection property.
@ PT_TIME
Time expressed in ps.
std::function< VALIDATOR_RESULT(const wxAny &&, EDA_ITEM *aItem)> PROPERTY_VALIDATOR_FN
A property validator function takes in the data type of the owning property, and returns a VALIDATOR_...
std::optional< std::unique_ptr< VALIDATION_ERROR > > VALIDATOR_RESULT
Null optional means validation succeeded.