37#include <wx/validate.h>
38#include <wx/propgrid/property.h>
45#include <unordered_map>
71#define TYPE_HASH( x ) typeid( x ).hash_code()
72#define TYPE_NAME( x ) typeid( x ).name()
75template<
typename Owner,
typename T>
84template<
typename Owner,
typename T,
typename FuncType>
96 return ( aOwner->*
m_func )();
103template<
typename Owner,
typename T>
112template<
typename Owner,
typename T,
typename FuncType>
124 ( aOwner->*
m_func )( aValue );
131#if defined( _MSC_VER )
132#pragma warning( push )
133#pragma warning( disable : 5266 )
136template<
typename Owner,
typename T,
typename Base = Owner>
188#if defined( _MSC_VER )
189#pragma warning( pop )
208 m_writeableFunc( [](
INSPECTABLE*)->bool {
return true; } ),
209 m_validator( NullValidator )
217 const wxString&
Name()
const {
return m_name; }
225 static wxPGChoices
empty;
251 return m_availFunc( aObject );
259 m_availFunc = std::move( aFunc );
265 return m_writeableFunc( aObject );
270 m_writeableFunc = std::move( aFunc );
302 m_hideFromPropertiesManager = aHide;
309 m_hideFromRulesEditor = aHide;
316 m_hideFromLibraryEditors = aIsHidden;
323 m_hideFromDesignEditors = aIsHidden;
327 wxString
Group()
const {
return m_group; }
332 m_validator = aValidator;
338 return m_validator( std::move( aValue ), aItem );
348 void set(
void* aObject, T aValue )
355 if( std::is_same<T, wxVariant>::value )
357 wxVariant var =
static_cast<wxVariant
>( aValue );
358 wxAny pv = getter( aObject );
360 if( pv.CheckType<
unsigned>() )
362 a =
static_cast<unsigned>( var.GetLong() );
364 else if( pv.CheckType<std::optional<int>>() )
369 else if( pv.CheckType<std::optional<double>>() )
386 setter( aObject, a );
390 T
get(
const void* aObject )
const
392 wxAny a = getter( aObject );
395 if( a.CheckType<
bool>() )
396 a = a.RawAs<
bool>() ? 1 : 0;
398 if ( !( std::is_enum<T>::value && a.CheckType<
int>() ) && !a.CheckType<T>() )
399 throw std::invalid_argument(
"Invalid requested type" );
401 return wxANY_AS( a, T );
405 virtual void setter(
void* aObject, wxAny& aValue ) = 0;
406 virtual wxAny
getter(
const void* aObject )
const = 0;
442template<
typename Owner,
typename T,
typename Base = Owner>
448 template<
typename SetType,
typename GetType>
450 void ( Base::*aSetter )( SetType ), GetType( Base::*aGetter )(),
454 METHOD<Owner, T, Base>::Wrap( aGetter ), aDisplay, aCoordType )
458 template<
typename SetType,
typename GetType>
460 void ( Base::*aSetter )( SetType ), GetType( Base::*aGetter )()
const,
464 METHOD<Owner, T, Base>::Wrap( aGetter ), aDisplay, aCoordType )
499 virtual void setter(
void* obj, wxAny& v )
override
503 if( !v.CheckType<T>() )
504 throw std::invalid_argument(
"Invalid type requested" );
506 Owner* o =
reinterpret_cast<Owner*
>( obj );
508 (*m_setter)( o, value );
511 virtual wxAny
getter(
const void* obj )
const override
513 const Owner* o =
reinterpret_cast<const Owner*
>( obj );
514 wxAny
res = (*m_getter)( o );
535template<
typename Owner,
typename T,
typename Base = Owner>
539 template<
typename SetType,
typename GetType>
541 void ( Base::*aSetter )( SetType ), GetType( Base::*aGetter )(),
543 :
PROPERTY<Owner, T, Base>( aName,
METHOD<Owner, T, Base>::Wrap( aSetter ),
544 METHOD<Owner, T, Base>::Wrap( aGetter ), aDisplay )
546 if ( std::is_enum<T>::value )
549 wxASSERT_MSG(
m_choices.GetCount() > 0, wxT(
"No enum choices defined" ) );
553 template<
typename SetType,
typename GetType>
555 void ( Base::*aSetter )( SetType ), GetType( Base::*aGetter )()
const,
558 :
PROPERTY<Owner, T, Base>( aName,
METHOD<Owner, T, Base>::Wrap( aSetter ),
559 METHOD<Owner, T, Base>::Wrap( aGetter ), aDisplay, aCoordType )
561 if ( std::is_enum<T>::value )
564 wxASSERT_MSG(
m_choices.GetCount() > 0, wxT(
"No enum choices defined" ) );
568 void setter(
void* obj, wxAny& v )
override
571 Owner* o =
reinterpret_cast<Owner*
>( obj );
573 if( v.CheckType<T>() )
575 T value = wxANY_AS(v, T);
578 else if (v.CheckType<
int>() )
580 int value = wxANY_AS(v,
int);
585 throw std::invalid_argument(
"Invalid type requested" );
589 wxAny
getter(
const void* obj )
const override
591 const Owner* o =
reinterpret_cast<const Owner*
>( obj );
608 return Choices().GetCount() > 0;
621 virtual const void*
operator()(
const void* aPointer )
const = 0;
627template<
typename Base,
typename Derived>
637 Base* base =
reinterpret_cast<Base*
>( aPointer );
638 return static_cast<Derived*
>( base );
641 const void*
operator()(
const void* aPointer )
const override
643 const Base* base =
reinterpret_cast<const Base*
>( aPointer );
644 return static_cast<const Derived*
>( base );
671 m_choices.Add( aName,
static_cast<int>( aValue ) );
684 static const wxString s_undef =
"UNDEFINED";
686 int idx =
m_choices.Index(
static_cast<int>( value ) );
688 if( idx >= 0 && idx < (
int)
m_choices.GetCount() )
689 return m_choices.GetLabel(
static_cast<int>( idx ) );
696 int idx =
m_choices.Index(
static_cast<int>( value ) );
698 if( idx >= 0 && idx < (
int)
m_choices.GetCount() )
729#define DECLARE_ENUM_TO_WXANY( type ) \
731 class wxAnyValueTypeImpl<type> : public wxAnyValueTypeImplBase<type> \
733 WX_DECLARE_ANY_VALUE_TYPE( wxAnyValueTypeImpl<type> ) \
735 wxAnyValueTypeImpl() : wxAnyValueTypeImplBase<type>() {} \
736 virtual ~wxAnyValueTypeImpl() {} \
737 virtual bool ConvertValue( const wxAnyValueBuffer& src, wxAnyValueType* dstType, \
738 wxAnyValueBuffer& dst ) const override \
740 type value = GetValue( src ); \
741 ENUM_MAP<type>& conv = ENUM_MAP<type>::Instance(); \
742 if( ! conv.IsValueDefined( value ) ) \
746 if( dstType->CheckType<wxString>() ) \
748 wxAnyValueTypeImpl<wxString>::SetValue( conv.ToString( value ), dst ); \
751 if( dstType->CheckType<int>() ) \
753 wxAnyValueTypeImpl<int>::SetValue( static_cast<int>( value ), dst ); \
763#define IMPLEMENT_ENUM_TO_WXANY( type ) WX_IMPLEMENT_ANY_VALUE_TYPE( wxAnyValueTypeImpl<type> )
765#define ENUM_TO_WXANY( type ) \
766 DECLARE_ENUM_TO_WXANY( type ) \
767 IMPLEMENT_ENUM_TO_WXANY( type )
770#define NO_SETTER( owner, type ) ( ( void ( owner::* )( type ) ) nullptr )
const KIGFX::COLOR4D & Color()
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 &))
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)
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)
PROPERTY_DISPLAY Display() 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
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)
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.
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)
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.