37#include <wx/validate.h>
38#include <wx/propgrid/property.h>
41#include <unordered_map>
64#define TYPE_HASH( x ) typeid( x ).hash_code()
65#define TYPE_NAME( x ) typeid( x ).name()
68template<
typename Owner,
typename T>
77template<
typename Owner,
typename T,
typename FuncType>
89 return ( aOwner->*
m_func )();
96template<
typename Owner,
typename T>
105template<
typename Owner,
typename T,
typename FuncType>
117 ( aOwner->*
m_func )( aValue );
125template<
typename Owner,
typename T,
typename Base = Owner>
193 m_writeableFunc( [](
INSPECTABLE*)->bool {
return true; } ),
194 m_validator( NullValidator )
202 const wxString&
Name()
const {
return m_name; }
210 static wxPGChoices
empty;
236 return m_availFunc( aObject );
250 return m_writeableFunc( aObject );
255 m_writeableFunc = aFunc;
287 m_hideFromPropertiesManager = aHide;
294 m_hideFromRulesEditor = aHide;
301 m_hideFromLibraryEditors = aIsHidden;
305 wxString
Group()
const {
return m_group; }
310 m_validator = aValidator;
316 return m_validator( std::move( aValue ), aItem );
326 void set(
void* aObject, T aValue )
333 if( std::is_same<T, wxVariant>::value )
335 wxVariant var =
static_cast<wxVariant
>( aValue );
336 wxAny pv = getter( aObject );
338 if( pv.CheckType<
unsigned>() )
340 a =
static_cast<unsigned>( var.GetLong() );
354 setter( aObject, a );
358 T
get(
const void* aObject )
const
360 wxAny a = getter( aObject );
363 if( a.CheckType<
bool>() )
364 a = a.RawAs<
bool>() ? 1 : 0;
366 if ( !( std::is_enum<T>::value && a.CheckType<
int>() ) && !a.CheckType<T>() )
367 throw std::invalid_argument(
"Invalid requested type" );
369 return wxANY_AS( a, T );
373 virtual void setter(
void* aObject, wxAny& aValue ) = 0;
374 virtual wxAny
getter(
const void* aObject )
const = 0;
412template<
typename Owner,
typename T,
typename Base = Owner>
418 template<
typename SetType,
typename GetType>
420 void ( Base::*aSetter )( SetType ), GetType( Base::*aGetter )(),
424 METHOD<Owner, T, Base>::Wrap( aGetter ), aDisplay, aCoordType )
428 template<
typename SetType,
typename GetType>
430 void ( Base::*aSetter )( SetType ), GetType( Base::*aGetter )()
const,
434 METHOD<Owner, T, Base>::Wrap( aGetter ), aDisplay, aCoordType )
469 virtual void setter(
void* obj, wxAny& v )
override
473 if( !v.CheckType<T>() )
474 throw std::invalid_argument(
"Invalid type requested" );
476 Owner* o =
reinterpret_cast<Owner*
>( obj );
478 (*m_setter)( o, value );
481 virtual wxAny
getter(
const void* obj )
const override
483 const Owner* o =
reinterpret_cast<const Owner*
>( obj );
484 wxAny
res = (*m_getter)( o );
505template<
typename Owner,
typename T,
typename Base = Owner>
509 template<
typename SetType,
typename GetType>
511 void ( Base::*aSetter )( SetType ), GetType( Base::*aGetter )(),
513 :
PROPERTY<Owner, T, Base>( aName,
METHOD<Owner, T, Base>::Wrap( aSetter ),
514 METHOD<Owner, T, Base>::Wrap( aGetter ), aDisplay )
516 if ( std::is_enum<T>::value )
519 wxASSERT_MSG(
m_choices.GetCount() > 0, wxT(
"No enum choices defined" ) );
523 template<
typename SetType,
typename GetType>
525 void ( Base::*aSetter )( SetType ), GetType( Base::*aGetter )()
const,
528 :
PROPERTY<Owner, T, Base>( aName,
METHOD<Owner, T, Base>::Wrap( aSetter ),
529 METHOD<Owner, T, Base>::Wrap( aGetter ), aDisplay, aCoordType )
531 if ( std::is_enum<T>::value )
534 wxASSERT_MSG(
m_choices.GetCount() > 0, wxT(
"No enum choices defined" ) );
538 void setter(
void* obj, wxAny& v )
override
541 Owner* o =
reinterpret_cast<Owner*
>( obj );
543 if( v.CheckType<T>() )
545 T value = wxANY_AS(v, T);
548 else if (v.CheckType<
int>() )
550 int value = wxANY_AS(v,
int);
555 throw std::invalid_argument(
"Invalid type requested" );
559 wxAny
getter(
const void* obj )
const override
561 const Owner* o =
reinterpret_cast<const Owner*
>( obj );
578 return Choices().GetCount() > 0;
591 virtual const void*
operator()(
const void* aPointer )
const = 0;
597template<
typename Base,
typename Derived>
607 Base* base =
reinterpret_cast<Base*
>( aPointer );
608 return static_cast<Derived*
>( base );
611 const void*
operator()(
const void* aPointer )
const override
613 const Base* base =
reinterpret_cast<const Base*
>( aPointer );
614 return static_cast<const Derived*
>( base );
641 m_choices.Add( aName,
static_cast<int>( aValue ) );
654 static const wxString s_undef =
"UNDEFINED";
656 int idx =
m_choices.Index(
static_cast<int>( value ) );
658 if( idx >= 0 && idx < (
int)
m_choices.GetCount() )
659 return m_choices.GetLabel(
static_cast<int>( idx ) );
666 int idx =
m_choices.Index(
static_cast<int>( value ) );
668 if( idx >= 0 && idx < (
int)
m_choices.GetCount() )
699#define DECLARE_ENUM_TO_WXANY( type ) \
701 class wxAnyValueTypeImpl<type> : public wxAnyValueTypeImplBase<type> \
703 WX_DECLARE_ANY_VALUE_TYPE( wxAnyValueTypeImpl<type> ) \
705 wxAnyValueTypeImpl() : wxAnyValueTypeImplBase<type>() {} \
706 virtual ~wxAnyValueTypeImpl() {} \
707 virtual bool ConvertValue( const wxAnyValueBuffer& src, wxAnyValueType* dstType, \
708 wxAnyValueBuffer& dst ) const override \
710 type value = GetValue( src ); \
711 ENUM_MAP<type>& conv = ENUM_MAP<type>::Instance(); \
712 if( ! conv.IsValueDefined( value ) ) \
716 if( dstType->CheckType<wxString>() ) \
718 wxAnyValueTypeImpl<wxString>::SetValue( conv.ToString( value ), dst ); \
721 if( dstType->CheckType<int>() ) \
723 wxAnyValueTypeImpl<int>::SetValue( static_cast<int>( value ), dst ); \
733#define IMPLEMENT_ENUM_TO_WXANY( type ) WX_IMPLEMENT_ANY_VALUE_TYPE( wxAnyValueTypeImpl<type> )
735#define ENUM_TO_WXANY( type ) \
736 DECLARE_ENUM_TO_WXANY( type ) \
737 IMPLEMENT_ENUM_TO_WXANY( type )
740#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
This property should only be shown in the design editor, not the library editor.
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
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
Some properties should not be shown in the Properties Manager GUI.
PROPERTY_BASE & SetIsHiddenFromPropertiesManager(bool aHide=true)
bool m_hideFromRulesEditor
Some properties should not be shown in the Custom Rules editor autocomplete.
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
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_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.