Browse Source

Properties: introduce method chaining for initialization

newinvert
Jon Evans 3 years ago
parent
commit
254168c788
  1. 7
      common/eda_item.cpp
  2. 7
      common/properties/property_mgr.cpp
  3. 40
      include/properties/property.h
  4. 4
      include/properties/property_mgr.h
  5. 64
      pcbnew/zone.cpp

7
common/eda_item.cpp

@ -392,10 +392,9 @@ static struct EDA_ITEM_DESC
PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance(); PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
REGISTER_TYPE( EDA_ITEM ); REGISTER_TYPE( EDA_ITEM );
auto typeProp = new PROPERTY_ENUM<EDA_ITEM, KICAD_T>( wxS( "Type" ),
NO_SETTER( EDA_ITEM, KICAD_T ), &EDA_ITEM::Type );
typeProp->SetIsInternal( true );
propMgr.AddProperty( typeProp );
propMgr.AddProperty( new PROPERTY_ENUM<EDA_ITEM, KICAD_T>( wxS( "Type" ),
NO_SETTER( EDA_ITEM, KICAD_T ), &EDA_ITEM::Type ) )
.SetIsInternal();
} }
} _EDA_ITEM_DESC; } _EDA_ITEM_DESC;

7
common/properties/property_mgr.cpp

@ -130,7 +130,7 @@ const void* PROPERTY_MANAGER::TypeCast( const void* aSource, TYPE_ID aBase, TYPE
} }
void PROPERTY_MANAGER::AddProperty( PROPERTY_BASE* aProperty, const wxString& aGroup )
PROPERTY_BASE& PROPERTY_MANAGER::AddProperty( PROPERTY_BASE* aProperty, const wxString& aGroup )
{ {
const wxString& name = aProperty->Name(); const wxString& name = aProperty->Name();
TYPE_ID hash = aProperty->OwnerHash(); TYPE_ID hash = aProperty->OwnerHash();
@ -147,15 +147,16 @@ void PROPERTY_MANAGER::AddProperty( PROPERTY_BASE* aProperty, const wxString& aG
} }
m_dirty = true; m_dirty = true;
return *aProperty;
} }
void PROPERTY_MANAGER::ReplaceProperty( size_t aBase, const wxString& aName, PROPERTY_BASE* aNew,
PROPERTY_BASE& PROPERTY_MANAGER::ReplaceProperty( size_t aBase, const wxString& aName, PROPERTY_BASE* aNew,
const wxString& aGroup ) const wxString& aGroup )
{ {
CLASS_DESC& classDesc = getClass( aNew->OwnerHash() ); CLASS_DESC& classDesc = getClass( aNew->OwnerHash() );
classDesc.m_replaced.insert( std::make_pair( aBase, aName ) ); classDesc.m_replaced.insert( std::make_pair( aBase, aName ) );
AddProperty( aNew, aGroup );
return AddProperty( aNew, aGroup );
} }

40
include/properties/property.h

@ -195,10 +195,7 @@ PROPERTY_BASE( const wxString& aName, PROPERTY_DISPLAY aDisplay = PT_DEFAULT,
{ {
} }
const wxString& Name() const
{
return m_name;
}
const wxString& Name() const { return m_name; }
/** /**
* Return a limited set of possible values (e.g. enum). Check with HasChoices() if a particular * Return a limited set of possible values (e.g. enum). Check with HasChoices() if a particular
@ -238,9 +235,10 @@ PROPERTY_BASE( const wxString& aName, PROPERTY_DISPLAY aDisplay = PT_DEFAULT,
/** /**
* Set a callback function to determine whether an object provides this property. * Set a callback function to determine whether an object provides this property.
*/ */
void SetAvailableFunc( std::function<bool(INSPECTABLE*)> aFunc )
PROPERTY_BASE& SetAvailableFunc( std::function<bool(INSPECTABLE*)> aFunc )
{ {
m_availFunc = aFunc; m_availFunc = aFunc;
return *this;
} }
virtual bool Writeable( INSPECTABLE* aObject ) const virtual bool Writeable( INSPECTABLE* aObject ) const
@ -248,9 +246,10 @@ PROPERTY_BASE( const wxString& aName, PROPERTY_DISPLAY aDisplay = PT_DEFAULT,
return m_writeableFunc( aObject ); return m_writeableFunc( aObject );
} }
void SetWriteableFunc( std::function<bool(INSPECTABLE*)> aFunc )
PROPERTY_BASE& SetWriteableFunc( std::function<bool(INSPECTABLE*)> aFunc )
{ {
m_writeableFunc = aFunc; m_writeableFunc = aFunc;
return *this;
} }
/** /**
@ -268,21 +267,32 @@ PROPERTY_BASE( const wxString& aName, PROPERTY_DISPLAY aDisplay = PT_DEFAULT,
*/ */
virtual size_t TypeHash() const = 0; virtual size_t TypeHash() const = 0;
PROPERTY_DISPLAY Display() const
{
return m_display;
}
PROPERTY_DISPLAY Display() const { return m_display; }
PROPERTY_BASE& SetDisplay( PROPERTY_DISPLAY aDisplay ) { m_display = aDisplay; return *this; }
ORIGIN_TRANSFORMS::COORD_TYPES_T CoordType() const { return m_coordType; } ORIGIN_TRANSFORMS::COORD_TYPES_T CoordType() const { return m_coordType; }
PROPERTY_BASE& SetCoordType( ORIGIN_TRANSFORMS::COORD_TYPES_T aType )
{
m_coordType = aType;
return *this;
}
void SetIsInternal( bool aIsInternal = true ) { m_isInternal = aIsInternal; }
bool IsInternal() const { return m_isInternal; } bool IsInternal() const { return m_isInternal; }
PROPERTY_BASE& SetIsInternal( bool aIsInternal = true )
{
m_isInternal = aIsInternal;
return *this;
}
void SetIsDeprecated( bool aIsDeprecated = true ) { m_isDeprecated = aIsDeprecated; }
bool IsDeprecated() const { return m_isDeprecated; } bool IsDeprecated() const { return m_isDeprecated; }
PROPERTY_BASE& SetIsDeprecated( bool aIsDeprecated = true )
{
m_isDeprecated = aIsDeprecated;
return *this;
}
wxString Group() const { return m_group; } wxString Group() const { return m_group; }
void SetGroup( const wxString& aGroup ) { m_group = aGroup; }
PROPERTY_BASE& SetGroup( const wxString& aGroup ) { m_group = aGroup; return *this; }
protected: protected:
template<typename T> template<typename T>
@ -329,8 +339,8 @@ private:
private: private:
const wxString m_name; const wxString m_name;
const PROPERTY_DISPLAY m_display;
const ORIGIN_TRANSFORMS::COORD_TYPES_T m_coordType;
PROPERTY_DISPLAY m_display;
ORIGIN_TRANSFORMS::COORD_TYPES_T m_coordType;
/// Internal properties are hidden from the GUI but not from the rules editor autocomplete /// Internal properties are hidden from the GUI but not from the rules editor autocomplete
bool m_isInternal; bool m_isInternal;

4
include/properties/property_mgr.h

@ -146,7 +146,7 @@ public:
* @param aProperty is the property to register. * @param aProperty is the property to register.
* @param aGroup is an optional grouping key for the property * @param aGroup is an optional grouping key for the property
*/ */
void AddProperty( PROPERTY_BASE* aProperty, const wxString& aGroup = wxEmptyString );
PROPERTY_BASE& AddProperty( PROPERTY_BASE* aProperty, const wxString& aGroup = wxEmptyString );
/** /**
* Replace an existing property for a specific type. * Replace an existing property for a specific type.
@ -159,7 +159,7 @@ public:
* @param aNew is the property replacing the inherited one. * @param aNew is the property replacing the inherited one.
* @param aGroup is the group to set for the replaced property. * @param aGroup is the group to set for the replaced property.
*/ */
void ReplaceProperty( size_t aBase, const wxString& aName, PROPERTY_BASE* aNew,
PROPERTY_BASE& ReplaceProperty( size_t aBase, const wxString& aName, PROPERTY_BASE* aNew,
const wxString& aGroup = wxEmptyString ); const wxString& aGroup = wxEmptyString );
/** /**

64
pcbnew/zone.cpp

@ -1431,55 +1431,57 @@ static struct ZONE_DESC
return false; return false;
}; };
auto layer = new PROPERTY_ENUM<ZONE, PCB_LAYER_ID>( _HKI( "Layer" ),
&ZONE::SetLayer, &ZONE::GetLayer );
layer->SetIsInternal( true );
propMgr.ReplaceProperty( TYPE_HASH( BOARD_CONNECTED_ITEM ), _HKI( "Layer" ), layer );
propMgr.ReplaceProperty( TYPE_HASH( BOARD_CONNECTED_ITEM ), _HKI( "Layer" ),
new PROPERTY_ENUM<ZONE, PCB_LAYER_ID>( _HKI( "Layer" ),
&ZONE::SetLayer,
&ZONE::GetLayer ) )
.SetIsInternal();
propMgr.OverrideAvailability( TYPE_HASH( ZONE ), TYPE_HASH( BOARD_CONNECTED_ITEM ), propMgr.OverrideAvailability( TYPE_HASH( ZONE ), TYPE_HASH( BOARD_CONNECTED_ITEM ),
_HKI( "Net" ), isCopperZone ); _HKI( "Net" ), isCopperZone );
propMgr.OverrideAvailability( TYPE_HASH( ZONE ), TYPE_HASH( BOARD_CONNECTED_ITEM ), propMgr.OverrideAvailability( TYPE_HASH( ZONE ), TYPE_HASH( BOARD_CONNECTED_ITEM ),
_HKI( "Net Class" ), isCopperZone ); _HKI( "Net Class" ), isCopperZone );
auto priority = new PROPERTY<ZONE, unsigned>( _HKI( "Priority" ),
&ZONE::SetAssignedPriority, &ZONE::GetAssignedPriority );
priority->SetAvailableFunc( isCopperZone );
propMgr.AddProperty( priority );
propMgr.AddProperty( new PROPERTY<ZONE, unsigned>( _HKI( "Priority" ),
&ZONE::SetAssignedPriority,
&ZONE::GetAssignedPriority ) )
.SetAvailableFunc( isCopperZone );
propMgr.AddProperty( new PROPERTY<ZONE, wxString>( _HKI( "Name" ), propMgr.AddProperty( new PROPERTY<ZONE, wxString>( _HKI( "Name" ),
&ZONE::SetZoneName, &ZONE::GetZoneName ) ); &ZONE::SetZoneName, &ZONE::GetZoneName ) );
const wxString groupFill = _HKI( "Fill Style" ); const wxString groupFill = _HKI( "Fill Style" );
auto fillMode = new PROPERTY_ENUM<ZONE, ZONE_FILL_MODE>( _HKI( "Fill Mode" ),
&ZONE::SetFillMode, &ZONE::GetFillMode );
// Fill mode can't be exposed to the UI until validation is moved to the ZONE class. // Fill mode can't be exposed to the UI until validation is moved to the ZONE class.
// see https://gitlab.com/kicad/code/kicad/-/issues/13811 // see https://gitlab.com/kicad/code/kicad/-/issues/13811
fillMode->SetIsInternal();
propMgr.AddProperty( fillMode, groupFill );
auto hatchOrientation = new PROPERTY<ZONE, EDA_ANGLE>( _HKI( "Orientation" ),
&ZONE::SetHatchOrientation, &ZONE::GetHatchOrientation,
PROPERTY_DISPLAY::PT_DEGREE );
hatchOrientation->SetWriteableFunc( isHatchedFill );
hatchOrientation->SetIsInternal();
propMgr.AddProperty( hatchOrientation, groupFill );
propMgr.AddProperty( new PROPERTY_ENUM<ZONE, ZONE_FILL_MODE>(
_HKI( "Fill Mode" ), &ZONE::SetFillMode, &ZONE::GetFillMode ),
groupFill )
.SetIsInternal();
propMgr.AddProperty( new PROPERTY<ZONE, EDA_ANGLE>(
_HKI( "Orientation" ), &ZONE::SetHatchOrientation,
&ZONE::GetHatchOrientation, PROPERTY_DISPLAY::PT_DEGREE ),
groupFill )
.SetWriteableFunc( isHatchedFill )
.SetIsInternal();
//TODO: Switch to translated //TODO: Switch to translated
auto hatchWidth = new PROPERTY<ZONE, int>( wxT( "Hatch Width" ),
&ZONE::SetHatchThickness, &ZONE::GetHatchThickness,
PROPERTY_DISPLAY::PT_SIZE );
hatchWidth->SetWriteableFunc( isHatchedFill );
hatchWidth->SetIsInternal();
propMgr.AddProperty( hatchWidth, groupFill );
propMgr.AddProperty( new PROPERTY<ZONE, int>( wxT( "Hatch Width" ),
&ZONE::SetHatchThickness,
&ZONE::GetHatchThickness,
PROPERTY_DISPLAY::PT_SIZE ),
groupFill )
.SetWriteableFunc( isHatchedFill )
.SetIsInternal();
//TODO: Switch to translated //TODO: Switch to translated
auto hatchGap = new PROPERTY<ZONE, int>( wxT( "Hatch Gap" ),
&ZONE::SetHatchGap, &ZONE::GetHatchGap,
PROPERTY_DISPLAY::PT_SIZE );
hatchGap->SetWriteableFunc( isHatchedFill );
hatchGap->SetIsInternal();
propMgr.AddProperty( hatchGap, groupFill );
propMgr.AddProperty( new PROPERTY<ZONE, int>( wxT( "Hatch Gap" ), &ZONE::SetHatchGap,
&ZONE::GetHatchGap,
PROPERTY_DISPLAY::PT_SIZE ),
groupFill )
.SetWriteableFunc( isHatchedFill )
.SetIsInternal();
// TODO: Smoothing effort needs to change to enum (in dialog too) // TODO: Smoothing effort needs to change to enum (in dialog too)
// TODO: Smoothing amount (double) // TODO: Smoothing amount (double)

Loading…
Cancel
Save