Properties: introduce method chaining for initialization

(cherry picked from commit 254168c788)
This commit is contained in:
Jon Evans 2023-02-09 20:45:23 -05:00
parent eed7ede376
commit d403e92315
5 changed files with 68 additions and 56 deletions

View File

@ -392,10 +392,9 @@ static struct EDA_ITEM_DESC
PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
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;

View File

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

View File

@ -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
@ -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.
*/
void SetAvailableFunc( std::function<bool(INSPECTABLE*)> aFunc )
PROPERTY_BASE& SetAvailableFunc( std::function<bool(INSPECTABLE*)> aFunc )
{
m_availFunc = aFunc;
return *this;
}
virtual bool Writeable( INSPECTABLE* aObject ) const
@ -248,9 +246,10 @@ PROPERTY_BASE( const wxString& aName, PROPERTY_DISPLAY aDisplay = PT_DEFAULT,
return m_writeableFunc( aObject );
}
void SetWriteableFunc( std::function<bool(INSPECTABLE*)> aFunc )
PROPERTY_BASE& SetWriteableFunc( std::function<bool(INSPECTABLE*)> 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;
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; }
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; }
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; }
PROPERTY_BASE& SetIsDeprecated( bool aIsDeprecated = true )
{
m_isDeprecated = aIsDeprecated;
return *this;
}
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:
template<typename T>
@ -329,8 +339,8 @@ private:
private:
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
bool m_isInternal;

View File

@ -146,7 +146,7 @@ public:
* @param aProperty is the property to register.
* @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.
@ -159,8 +159,8 @@ public:
* @param aNew is the property replacing the inherited one.
* @param aGroup is the group to set for the replaced property.
*/
void ReplaceProperty( size_t aBase, const wxString& aName, PROPERTY_BASE* aNew,
const wxString& aGroup = wxEmptyString );
PROPERTY_BASE& ReplaceProperty( size_t aBase, const wxString& aName, PROPERTY_BASE* aNew,
const wxString& aGroup = wxEmptyString );
/**
* Register a type converter. Required prior TypeCast() usage.

View File

@ -1431,55 +1431,57 @@ static struct ZONE_DESC
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 ),
_HKI( "Net" ), isCopperZone );
propMgr.OverrideAvailability( TYPE_HASH( ZONE ), TYPE_HASH( BOARD_CONNECTED_ITEM ),
_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" ),
&ZONE::SetZoneName, &ZONE::GetZoneName ) );
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.
// see https://gitlab.com/kicad/code/kicad/-/issues/13811
fillMode->SetIsInternal();
propMgr.AddProperty( fillMode, groupFill );
propMgr.AddProperty( new PROPERTY_ENUM<ZONE, ZONE_FILL_MODE>(
_HKI( "Fill Mode" ), &ZONE::SetFillMode, &ZONE::GetFillMode ),
groupFill )
.SetIsInternal();
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<ZONE, EDA_ANGLE>(
_HKI( "Orientation" ), &ZONE::SetHatchOrientation,
&ZONE::GetHatchOrientation, PROPERTY_DISPLAY::PT_DEGREE ),
groupFill )
.SetWriteableFunc( isHatchedFill )
.SetIsInternal();
//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
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 amount (double)