Commit 6aecdf7c authored by Don Gagne's avatar Don Gagne

Merge pull request #3211 from DonLakeFlyer/ValidateDecimalPlaces

Validate/Set decimal places against increment value
parents db59e0bb 12db07d7
...@@ -56,7 +56,7 @@ const FactMetaData::AppSettingsTranslation_s FactMetaData::_rgAppSettingsTransla ...@@ -56,7 +56,7 @@ const FactMetaData::AppSettingsTranslation_s FactMetaData::_rgAppSettingsTransla
FactMetaData::FactMetaData(QObject* parent) FactMetaData::FactMetaData(QObject* parent)
: QObject(parent) : QObject(parent)
, _type(valueTypeInt32) , _type(valueTypeInt32)
, _decimalPlaces(defaultDecimalPlaces) , _decimalPlaces(unknownDecimalPlaces)
, _rawDefaultValue(0) , _rawDefaultValue(0)
, _defaultValueAvailable(false) , _defaultValueAvailable(false)
, _group("*Default Group") , _group("*Default Group")
...@@ -75,7 +75,7 @@ FactMetaData::FactMetaData(QObject* parent) ...@@ -75,7 +75,7 @@ FactMetaData::FactMetaData(QObject* parent)
FactMetaData::FactMetaData(ValueType_t type, QObject* parent) FactMetaData::FactMetaData(ValueType_t type, QObject* parent)
: QObject(parent) : QObject(parent)
, _type(type) , _type(type)
, _decimalPlaces(defaultDecimalPlaces) , _decimalPlaces(unknownDecimalPlaces)
, _rawDefaultValue(0) , _rawDefaultValue(0)
, _defaultValueAvailable(false) , _defaultValueAvailable(false)
, _group("*Default Group") , _group("*Default Group")
...@@ -578,3 +578,33 @@ QString FactMetaData::appSettingsDistanceUnitsString(void) ...@@ -578,3 +578,33 @@ QString FactMetaData::appSettingsDistanceUnitsString(void)
return QStringLiteral("m"); return QStringLiteral("m");
} }
} }
int FactMetaData::decimalPlaces(void) const
{
int actualDecimalPlaces = defaultDecimalPlaces;
int incrementDecimalPlaces = unknownDecimalPlaces;
// First determine decimal places from increment
double increment = this->increment();
if (!qIsNaN(increment)) {
double integralPart;
// Get the fractional part only
increment = fabs(modf(increment, &integralPart));
if (increment == 0.0) {
// No fractional part, so no decimal places
incrementDecimalPlaces = 0;
} else {
incrementDecimalPlaces = -ceil(log10(increment));
}
}
// Correct decimal places is the larger of the two, increment or meta data value
if (incrementDecimalPlaces != unknownDecimalPlaces && _decimalPlaces == unknownDecimalPlaces) {
actualDecimalPlaces = incrementDecimalPlaces;
} else {
actualDecimalPlaces = qMax(_decimalPlaces, incrementDecimalPlaces);
}
return actualDecimalPlaces;
}
...@@ -70,7 +70,7 @@ public: ...@@ -70,7 +70,7 @@ public:
/// Returns the string for distance units which has configued by user /// Returns the string for distance units which has configued by user
static QString appSettingsDistanceUnitsString(void); static QString appSettingsDistanceUnitsString(void);
int decimalPlaces (void) const { return _decimalPlaces; } int decimalPlaces (void) const;
QVariant rawDefaultValue (void) const; QVariant rawDefaultValue (void) const;
QVariant cookedDefaultValue (void) const { return _rawTranslator(rawDefaultValue()); } QVariant cookedDefaultValue (void) const { return _rawTranslator(rawDefaultValue()); }
bool defaultValueAvailable (void) const { return _defaultValueAvailable; } bool defaultValueAvailable (void) const { return _defaultValueAvailable; }
...@@ -139,7 +139,8 @@ public: ...@@ -139,7 +139,8 @@ public:
/// Same as convertAndValidateRaw except for cookedValue input /// Same as convertAndValidateRaw except for cookedValue input
bool convertAndValidateCooked(const QVariant& cookedValue, bool convertOnly, QVariant& typedValue, QString& errorString); bool convertAndValidateCooked(const QVariant& cookedValue, bool convertOnly, QVariant& typedValue, QString& errorString);
static const int defaultDecimalPlaces = 3; static const int defaultDecimalPlaces = 3; ///< Default value for decimal places if not specified/known
static const int unknownDecimalPlaces = -1; ///< Number of decimal places to specify is not known
static ValueType_t stringToType(const QString& typeString, bool& unknownType); static ValueType_t stringToType(const QString& typeString, bool& unknownType);
static size_t typeToSize(ValueType_t type); static size_t typeToSize(ValueType_t type);
......
...@@ -187,7 +187,7 @@ void MissionCommandList::_loadMavCmdInfoJson(const QString& jsonFilename) ...@@ -187,7 +187,7 @@ void MissionCommandList::_loadMavCmdInfoJson(const QString& jsonFilename)
paramInfo->_label = paramObject.value(_labelJsonKey).toString(); paramInfo->_label = paramObject.value(_labelJsonKey).toString();
paramInfo->_defaultValue = paramObject.value(_defaultJsonKey).toDouble(0.0); paramInfo->_defaultValue = paramObject.value(_defaultJsonKey).toDouble(0.0);
paramInfo->_decimalPlaces = paramObject.value(_decimalPlacesJsonKey).toInt(FactMetaData::defaultDecimalPlaces); paramInfo->_decimalPlaces = paramObject.value(_decimalPlacesJsonKey).toInt(FactMetaData::unknownDecimalPlaces);
paramInfo->_enumStrings = paramObject.value(_enumStringsJsonKey).toString().split(",", QString::SkipEmptyParts); paramInfo->_enumStrings = paramObject.value(_enumStringsJsonKey).toString().split(",", QString::SkipEmptyParts);
paramInfo->_param = i; paramInfo->_param = i;
paramInfo->_units = paramObject.value(_unitsJsonKey).toString(); paramInfo->_units = paramObject.value(_unitsJsonKey).toString();
......
...@@ -322,16 +322,16 @@ QString SimpleMissionItem::abbreviation() const ...@@ -322,16 +322,16 @@ QString SimpleMissionItem::abbreviation() const
void SimpleMissionItem::_clearParamMetaData(void) void SimpleMissionItem::_clearParamMetaData(void)
{ {
_param1MetaData.setRawUnits(""); _param1MetaData.setRawUnits("");
_param1MetaData.setDecimalPlaces(FactMetaData::defaultDecimalPlaces); _param1MetaData.setDecimalPlaces(FactMetaData::unknownDecimalPlaces);
_param1MetaData.setBuiltInTranslator(); _param1MetaData.setBuiltInTranslator();
_param2MetaData.setRawUnits(""); _param2MetaData.setRawUnits("");
_param2MetaData.setDecimalPlaces(FactMetaData::defaultDecimalPlaces); _param2MetaData.setDecimalPlaces(FactMetaData::unknownDecimalPlaces);
_param2MetaData.setBuiltInTranslator(); _param2MetaData.setBuiltInTranslator();
_param3MetaData.setRawUnits(""); _param3MetaData.setRawUnits("");
_param3MetaData.setDecimalPlaces(FactMetaData::defaultDecimalPlaces); _param3MetaData.setDecimalPlaces(FactMetaData::unknownDecimalPlaces);
_param3MetaData.setBuiltInTranslator(); _param3MetaData.setBuiltInTranslator();
_param4MetaData.setRawUnits(""); _param4MetaData.setRawUnits("");
_param4MetaData.setDecimalPlaces(FactMetaData::defaultDecimalPlaces); _param4MetaData.setDecimalPlaces(FactMetaData::unknownDecimalPlaces);
_param4MetaData.setBuiltInTranslator(); _param4MetaData.setBuiltInTranslator();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment