Clamp ratio properties

Fixes https://gitlab.com/kicad/code/kicad/-/issues/16153
This commit is contained in:
Jon Evans 2023-11-25 18:39:46 -05:00
parent c307e284e0
commit 2b27337778
3 changed files with 29 additions and 3 deletions

View File

@ -38,7 +38,8 @@ enum class EDA_DATA_TYPE
{
DISTANCE = 0,
AREA = 1,
VOLUME = 2
VOLUME = 2,
UNITLESS = 3
};
enum class EDA_UNITS

View File

@ -47,8 +47,9 @@ public:
wxString Format( UNITS_PROVIDER* aUnits ) const override
{
bool addUnit = DataType != EDA_DATA_TYPE::UNITLESS;
return wxString::Format( wxS( "Value must be less than or equal to %s" ),
aUnits->StringFromValue( Maximum, true ) );
aUnits->StringFromValue( Maximum, addUnit ) );
}
};
@ -70,8 +71,9 @@ public:
wxString Format( UNITS_PROVIDER* aUnits ) const override
{
bool addUnit = DataType != EDA_DATA_TYPE::UNITLESS;
return wxString::Format( wxS( "Value must be greater than or equal to %s" ),
aUnits->StringFromValue( Minimum, true ) );
aUnits->StringFromValue( Minimum, addUnit ) );
}
};
@ -127,6 +129,26 @@ public:
return std::nullopt;
}
static VALIDATOR_RESULT PositiveRatioValidator( const wxAny&& aValue, EDA_ITEM* aItem )
{
wxASSERT_MSG( aValue.CheckType<double>(), "Expecting double-containing value" );
double val = aValue.As<double>();
if( val > 1.0 )
{
return std::make_unique<VALIDATION_ERROR_TOO_LARGE<double>>( val, 1.0,
EDA_DATA_TYPE::UNITLESS );
}
else if( val < 0.0 )
{
return std::make_unique<VALIDATION_ERROR_TOO_SMALL<double>>( val, 0.0,
EDA_DATA_TYPE::UNITLESS );
}
return std::nullopt;
}
};
#endif //KICAD_PROPERTY_VALIDATORS_H

View File

@ -27,6 +27,7 @@
#include <board_connected_item.h>
#include <board_design_settings.h>
#include <connectivity/connectivity_data.h>
#include <properties/property_validators.h>
#include <string_utils.h>
#include <i18n_utility.h>
#include <netinfo.h>
@ -243,6 +244,7 @@ static struct BOARD_CONNECTED_ITEM_DESC
&BOARD_CONNECTED_ITEM::SetTeardropBestLengthRatio,
&BOARD_CONNECTED_ITEM::GetTeardropBestLengthRatio );
bestLength->SetAvailableFunc( supportsTeardrops );
bestLength->SetValidator( PROPERTY_VALIDATORS::PositiveRatioValidator );
propMgr.AddProperty( bestLength, groupTeardrops );
auto maxLength = new PROPERTY<BOARD_CONNECTED_ITEM, int>( _HKI( "Max Length" ),
@ -255,6 +257,7 @@ static struct BOARD_CONNECTED_ITEM_DESC
&BOARD_CONNECTED_ITEM::SetTeardropBestWidthRatio,
&BOARD_CONNECTED_ITEM::GetTeardropBestWidthRatio );
bestWidth->SetAvailableFunc( supportsTeardrops );
bestWidth->SetValidator( PROPERTY_VALIDATORS::PositiveRatioValidator );
propMgr.AddProperty( bestWidth, groupTeardrops );
auto maxWidth = new PROPERTY<BOARD_CONNECTED_ITEM, int>( _HKI( "Max Width" ),