Skip to content

Commit

Permalink
Moving display decimals for scaled int values to be provided by the p…
Browse files Browse the repository at this point in the history
…rotocol defintion.
  • Loading branch information
arobenko committed Oct 22, 2024
1 parent dad0fe3 commit bc665b9
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 78 deletions.
2 changes: 1 addition & 1 deletion app/cc_view/src/widget/field/IntValueFieldWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void IntValueFieldWidget::updatePropertiesImpl(const QVariantMap& props)
assert(m_fieldPtr);
assert(!m_childWidget);
do {
if (property::field::IntValue(props).hasScaledDecimals()) {
if (m_fieldPtr->hasScaledDecimals()) {
m_childWidget.reset(new ScaledIntValueFieldWidget(std::move(m_fieldPtr)));
break;
}
Expand Down
25 changes: 5 additions & 20 deletions app/cc_view/src/widget/field/ScaledIntValueFieldWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@
namespace cc_tools_qt
{

namespace
{

const int DefaultInitialDecimals = 6;

} // namespace

ScaledIntValueFieldWidget::ScaledIntValueFieldWidget(
FieldPtr fieldPtr,
QWidget* parentObj)
Expand All @@ -54,7 +47,11 @@ ScaledIntValueFieldWidget::ScaledIntValueFieldWidget(
m_ui.m_valueSpinBox->setRange(
m_fieldPtr->scaleValue(m_fieldPtr->minValue()),
m_fieldPtr->scaleValue(m_fieldPtr->maxValue()));
m_ui.m_valueSpinBox->setDecimals(DefaultInitialDecimals);

auto decimals = m_fieldPtr->scaledDecimals();
if (0 < decimals) {
m_ui.m_valueSpinBox->setDecimals(decimals);
}

refresh();

Expand Down Expand Up @@ -106,18 +103,6 @@ void ScaledIntValueFieldWidget::editEnabledUpdatedImpl()
void ScaledIntValueFieldWidget::updatePropertiesImpl(const QVariantMap& props)
{
property::field::IntValue actProps(props);

auto decimals = property::field::IntValue(props).scaledDecimals();

if (0 < decimals) {
m_ui.m_valueSpinBox->setDecimals(decimals);
}
else {
[[maybe_unused]] static constexpr bool Should_not_happen = false;
assert(Should_not_happen);
m_ui.m_valueSpinBox->setDecimals(0);
}

refresh();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ UnsignedLongLongIntValueFieldWidget::UnsignedLongLongIntValueFieldWidget(
FieldPtr fieldPtr,
QWidget* parentObj)
: Base(parentObj),
m_fieldPtr(std::move(fieldPtr))
m_fieldPtr(std::move(fieldPtr)),
m_decimals(m_fieldPtr->scaledDecimals())
{
m_ui.setupUi(this);
setNameLabelWidget(m_ui.m_nameLabel);
Expand Down Expand Up @@ -97,7 +98,6 @@ void UnsignedLongLongIntValueFieldWidget::updatePropertiesImpl(const QVariantMap
{
property::field::IntValue parsedProps(props);
m_offset = parsedProps.displayOffset();
m_decimals = parsedProps.scaledDecimals();
refresh();
}

Expand Down
10 changes: 2 additions & 8 deletions demo/cc_plugin/message/FloatValues.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,8 @@ QVariantList createFieldsProperties()
QVariantList props;
props.append(cc::property::field::ForField<ProtMsg::Field_field1>().asMap());
props.append(cc::property::field::ForField<ProtMsg::Field_field2>().asMap());
props.append(
cc::property::field::ForField<ProtMsg::Field_field3>()
.scaledDecimals(2)
.asMap());
props.append(
cc::property::field::ForField<ProtMsg::Field_field4>()
.scaledDecimals(11)
.asMap());
props.append(cc::property::field::ForField<ProtMsg::Field_field3>().asMap());
props.append(cc::property::field::ForField<ProtMsg::Field_field4>().asMap());

assert(props.size() == ProtMsg::FieldIdx_numOfValues);
return props;
Expand Down
16 changes: 13 additions & 3 deletions demo/protocol/include/demo/message/FloatValues.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,12 @@ struct FloatValuesFields
static const std::size_t MapSize = std::extent<decltype(Map)>::value;

return std::make_pair(&Map[0], MapSize);
}
}

static constexpr unsigned displayDecimals()
{
return 2U;
}
};

/// @brief Floating point value serialised as 5 byte integer with (1e-11) scaling ratio.
Expand All @@ -185,8 +190,13 @@ struct FloatValuesFields

static const char* name()
{
return "field1";
}
return "field4";
}

static constexpr unsigned displayDecimals()
{
return 11U;
}
};

/// @brief All the fields bundled in std::tuple.
Expand Down
34 changes: 28 additions & 6 deletions lib/include/cc_tools_qt/details/ToolsIntFieldImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,29 +106,51 @@ class ToolsIntFieldImpl : public ToolsNumericFieldImpl<cc_tools_qt::field::Tools
using Tag =
std::conditional_t<
Field::hasSpecials(),
HasSpecialsTag,
NoSpecialstag
HasFeatureTag,
NoFeatureTag
>;

return specialsInternal(Tag());
}

virtual int scaledDecimalsImpl() const override
{
using Tag =
std::conditional_t<
Field::hasScaling(),
HasFeatureTag,
NoFeatureTag
>;

return scaledDecimalsInternal(Tag());
}

private:
struct HasSpecialsTag{};
struct NoSpecialstag{};
struct HasFeatureTag{};
struct NoFeatureTag{};

static const SpecialsList& specialsInternal(HasSpecialsTag)
static const SpecialsList& specialsInternal(HasFeatureTag)
{
static const SpecialsList List = createSpecialsList();
return List;
}

static const SpecialsList& specialsInternal(NoSpecialstag)
static const SpecialsList& specialsInternal(NoFeatureTag)
{
static const SpecialsList List;
return List;
}

static int scaledDecimalsInternal(HasFeatureTag)
{
return Field::displayDecimals();
}

static int scaledDecimalsInternal(NoFeatureTag)
{
return 0;
}

static SpecialsList createSpecialsList()
{
SpecialsList result;
Expand Down
34 changes: 28 additions & 6 deletions lib/include/cc_tools_qt/details/ToolsUnsignedLongFieldImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,29 +105,51 @@ class ToolsUnsignedLongFieldImpl : public ToolsNumericFieldImpl<cc_tools_qt::fie
using Tag =
std::conditional_t<
Field::hasSpecials(),
HasSpecialsTag,
NoSpecialstag
HasFeatureTag,
NoFeatureTag
>;

return specialsInternal(Tag());
}

virtual int scaledDecimalsImpl() const override
{
using Tag =
std::conditional_t<
Field::hasScaling(),
HasFeatureTag,
NoFeatureTag
>;

return scaledDecimalsInternal(Tag());
}

private:
struct HasSpecialsTag{};
struct NoSpecialstag{};
struct HasFeatureTag{};
struct NoFeatureTag{};

static const SpecialsList& specialsInternal(HasSpecialsTag)
static const SpecialsList& specialsInternal(HasFeatureTag)
{
static const SpecialsList List = createSpecialsList();
return List;
}

static const SpecialsList& specialsInternal(NoSpecialstag)
static const SpecialsList& specialsInternal(NoFeatureTag)
{
static const SpecialsList List;
return List;
}

static int scaledDecimalsInternal(HasFeatureTag)
{
return Field::displayDecimals();
}

static int scaledDecimalsInternal(NoFeatureTag)
{
return 0;
}

static SpecialsList createSpecialsList()
{
SpecialsList result;
Expand Down
4 changes: 4 additions & 0 deletions lib/include/cc_tools_qt/field/ToolsIntField.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ class CC_API ToolsIntField : public details::ToolsNumericFieldBase<long long int

const SpecialsList& specials() const;

bool hasScaledDecimals() const;
int scaledDecimals() const;

protected:
virtual UnderlyingType minValueImpl() const = 0;
virtual UnderlyingType maxValueImpl() const = 0;
Expand All @@ -74,6 +77,7 @@ class CC_API ToolsIntField : public details::ToolsNumericFieldBase<long long int
virtual std::size_t valueTypeSizeImpl() const = 0;
virtual ActPtr cloneImpl() = 0;
virtual const SpecialsList& specialsImpl() const = 0;
virtual int scaledDecimalsImpl() const = 0;

virtual void dispatchImpl(ToolsFieldHandler& handler) override;
};
Expand Down
4 changes: 4 additions & 0 deletions lib/include/cc_tools_qt/field/ToolsUnsignedLongField.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ class CC_API ToolsUnsignedLongField : public cc_tools_qt::details::ToolsNumericF

const SpecialsList& specials() const;

bool hasScaledDecimals() const;
int scaledDecimals() const;

protected:
virtual UnderlyingType minValueImpl() const = 0;
virtual UnderlyingType maxValueImpl() const = 0;
Expand All @@ -76,6 +79,7 @@ class CC_API ToolsUnsignedLongField : public cc_tools_qt::details::ToolsNumericF
virtual std::size_t valueTypeSizeImpl() const = 0;
virtual ActPtr cloneImpl() = 0;
virtual const SpecialsList& specialsImpl() const = 0;
virtual int scaledDecimalsImpl() const = 0;

virtual void dispatchImpl(ToolsFieldHandler& handler) override;
};
Expand Down
12 changes: 0 additions & 12 deletions lib/include/cc_tools_qt/property/field.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,25 +240,13 @@ class CC_API IntValue : public CommonBase<IntValue>
/// @brief Set numeric offset for displayed field
IntValue& displayOffset(long long value);

/// @brief Check whether property of having scaled decimals being set
bool hasScaledDecimals() const;

/// @brief Get number of digits after decimal point when displaying
/// scaled value.
int scaledDecimals() const;

/// @brief Set number of digits after decimal point when displaying scaled
/// value.
IntValue& scaledDecimals(int value);

/// @brief Retrieve all properties as map.
QVariantMap asMap() const;

private:
void getFrom(const QVariantMap& props);

long long m_displayOffset = 0;
int m_scaledDecimals = 0;
};

/// @brief Class to contain all the properties relevant to
Expand Down
10 changes: 10 additions & 0 deletions lib/src/field/ToolsIntField.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ const ToolsIntField::SpecialsList& ToolsIntField::specials() const
return specialsImpl();
}

bool ToolsIntField::hasScaledDecimals() const
{
return 0 < scaledDecimals();
}

int ToolsIntField::scaledDecimals() const
{
return scaledDecimalsImpl();
}

void ToolsIntField::dispatchImpl(ToolsFieldHandler& handler)
{
handler.handle(*this);
Expand Down
10 changes: 10 additions & 0 deletions lib/src/field/ToolsUnsignedLongField.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ const ToolsUnsignedLongField::SpecialsList& ToolsUnsignedLongField::specials() c
return specialsImpl();
}

bool ToolsUnsignedLongField::hasScaledDecimals() const
{
return 0 < scaledDecimals();
}

int ToolsUnsignedLongField::scaledDecimals() const
{
return scaledDecimalsImpl();
}

void ToolsUnsignedLongField::dispatchImpl(ToolsFieldHandler& handler)
{
handler.handle(*this);
Expand Down
20 changes: 0 additions & 20 deletions lib/src/property/field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,38 +215,18 @@ IntValue& IntValue::displayOffset(long long value)
return *this;
}

bool IntValue::hasScaledDecimals() const
{
return 0 < scaledDecimals();
}

int IntValue::scaledDecimals() const
{
return m_scaledDecimals;
}

IntValue& IntValue::scaledDecimals(int value)
{
m_scaledDecimals = value;
return *this;
}

QVariantMap IntValue::asMap() const
{
QVariantMap props;
Base::setTo(props);
Base::setElemTo(m_displayOffset, numValueDisplayOffsetKey(), props);
Base::setElemTo(m_scaledDecimals, floatDecimalsKey(), props);
return props;
}

void IntValue::getFrom(const QVariantMap& props)
{
m_displayOffset =
getElemFrom<decltype(m_displayOffset)>(props, numValueDisplayOffsetKey());

m_scaledDecimals =
getElemFrom<decltype(m_scaledDecimals)>(props, floatDecimalsKey());
}

EnumValue::EnumValue() = default;
Expand Down

0 comments on commit bc665b9

Please sign in to comment.