Skip to content

Commit

Permalink
Prepared ToolsBitmaskField.
Browse files Browse the repository at this point in the history
  • Loading branch information
arobenko committed Oct 16, 2024
1 parent 523301b commit 32e1b4d
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 85 deletions.
4 changes: 2 additions & 2 deletions app/cc_view/src/DefaultMessageDisplayHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ class WidgetCreator : public field_wrapper::FieldWrapperHandler
m_widget.reset(new UnsignedLongLongIntValueFieldWidget(field.clone(), m_parent));
}

virtual void handle(field_wrapper::BitmaskValueWrapper& wrapper) override
virtual void handle(field::ToolsBitmaskField& field) override
{
m_widget.reset(new BitmaskValueFieldWidget(wrapper.clone(), m_parent));
m_widget.reset(new BitmaskValueFieldWidget(field.clone(), m_parent));
}

virtual void handle(field::ToolsEnumField& field) override
Expand Down
36 changes: 18 additions & 18 deletions app/cc_view/src/widget/field/BitmaskValueFieldWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ namespace cc_tools_qt
{

BitmaskValueFieldWidget::BitmaskValueFieldWidget(
WrapperPtr&& wrapper,
FieldPtr&& fieldPtr,
QWidget* parentObj)
: Base(parentObj),
m_wrapper(std::move(wrapper)),
m_checkboxes(m_wrapper->bitIdxLimit())
m_fieldPtr(std::move(fieldPtr)),
m_checkboxes(m_fieldPtr->bitIdxLimit())
{
m_ui.setupUi(this);
setNameLabelWidget(m_ui.m_nameLabel);
Expand All @@ -42,7 +42,7 @@ BitmaskValueFieldWidget::BitmaskValueFieldWidget(
setSerialisedValueWidget(m_ui.m_serValueWidget);

assert(m_ui.m_serValueLineEdit != nullptr);
setSerialisedInputMask(*m_ui.m_serValueLineEdit, m_wrapper->width());
setSerialisedInputMask(*m_ui.m_serValueLineEdit, m_fieldPtr->width());

connect(m_ui.m_serValueLineEdit, SIGNAL(textEdited(const QString&)),
this, SLOT(serialisedValueUpdated(const QString&)));
Expand All @@ -52,17 +52,17 @@ BitmaskValueFieldWidget::~BitmaskValueFieldWidget() noexcept = default;

ToolsField& BitmaskValueFieldWidget::fieldImpl()
{
assert(m_wrapper);
return *m_wrapper;
assert(m_fieldPtr);
return *m_fieldPtr;
}

void BitmaskValueFieldWidget::refreshImpl()
{
assert(m_wrapper->canWrite());
assert(m_fieldPtr->canWrite());
assert(m_ui.m_serValueLineEdit != nullptr);
updateValue(*m_ui.m_serValueLineEdit, m_wrapper->getSerialisedString());
updateValue(*m_ui.m_serValueLineEdit, m_fieldPtr->getSerialisedString());

auto bitIdxLimit = m_wrapper->bitIdxLimit();
auto bitIdxLimit = m_fieldPtr->bitIdxLimit();
assert(bitIdxLimit == m_checkboxes.size());
for (auto idx = 0U; idx < bitIdxLimit; ++idx) {
auto* checkbox = m_checkboxes[idx];
Expand All @@ -71,7 +71,7 @@ void BitmaskValueFieldWidget::refreshImpl()
}

bool showedBitValue = checkbox->checkState() != 0;
bool actualBitValue = m_wrapper->bitValue(idx);
bool actualBitValue = m_fieldPtr->bitValue(idx);
if (showedBitValue != actualBitValue) {
Qt::CheckState state = Qt::Unchecked;
if (actualBitValue) {
Expand All @@ -81,7 +81,7 @@ void BitmaskValueFieldWidget::refreshImpl()
}
}

bool valid = m_wrapper->valid();
bool valid = m_fieldPtr->valid();
setValidityStyleSheet(*m_ui.m_serFrontLabel, valid);
setValidityStyleSheet(*m_ui.m_serValueLineEdit, valid);
setValidityStyleSheet(*m_ui.m_serBackLabel, valid);
Expand All @@ -105,8 +105,8 @@ void BitmaskValueFieldWidget::updatePropertiesImpl(const QVariantMap& props)

m_checkboxes.clear();

auto count = std::min(static_cast<unsigned>(bitNamesList.size()), m_wrapper->bitIdxLimit());
m_checkboxes.resize(m_wrapper->bitIdxLimit());
auto count = std::min(static_cast<unsigned>(bitNamesList.size()), m_fieldPtr->bitIdxLimit());
m_checkboxes.resize(m_fieldPtr->bitIdxLimit());

for (unsigned idx = 0; idx < count; ++idx) {
auto& nameVar = bitNamesList[static_cast<int>(idx)];
Expand All @@ -127,7 +127,7 @@ void BitmaskValueFieldWidget::updatePropertiesImpl(const QVariantMap& props)

void BitmaskValueFieldWidget::serialisedValueUpdated(const QString& value)
{
handleNumericSerialisedValueUpdate(value, *m_wrapper);
handleNumericSerialisedValueUpdate(value, *m_fieldPtr);
}

void BitmaskValueFieldWidget::checkBoxUpdated(int value)
Expand All @@ -142,11 +142,11 @@ void BitmaskValueFieldWidget::checkBoxUpdated(int value)
return;
}
auto idx = static_cast<unsigned>(std::distance(m_checkboxes.begin(), iter));
m_wrapper->setBitValue(idx, value != 0);
m_fieldPtr->setBitValue(idx, value != 0);
updated = true;
if (!m_wrapper->canWrite()) {
m_wrapper->reset();
assert(m_wrapper->canWrite());
if (!m_fieldPtr->canWrite()) {
m_fieldPtr->reset();
assert(m_fieldPtr->canWrite());
}
}

Expand Down
10 changes: 5 additions & 5 deletions app/cc_view/src/widget/field/BitmaskValueFieldWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#include <vector>

#include "cc_tools_qt/field_wrapper/BitmaskValueWrapper.h"
#include "cc_tools_qt/field/ToolsBitmaskField.h"
#include "FieldWidget.h"

#include "ui_BitmaskValueFieldWidget.h"
Expand All @@ -34,10 +34,10 @@ class BitmaskValueFieldWidget : public FieldWidget
Q_OBJECT
typedef FieldWidget Base;
public:
using WrapperPtr = field_wrapper::BitmaskValueWrapperPtr;
using FieldPtr = field::ToolsBitmaskFieldPtr;

explicit BitmaskValueFieldWidget(
WrapperPtr&& wrapper,
FieldPtr&& fieldPtr,
QWidget* parentObj = nullptr);

~BitmaskValueFieldWidget() noexcept;
Expand All @@ -53,11 +53,11 @@ private slots:
void checkBoxUpdated(int value);

private:
using WrapperType = WrapperPtr::element_type;
using WrapperType = FieldPtr::element_type;
using UnderlyingType = WrapperType::UnderlyingType;

Ui::BitmaskValueFieldWidget m_ui;
WrapperPtr m_wrapper;
FieldPtr m_fieldPtr;
std::vector<QCheckBox*> m_checkboxes;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,48 +18,26 @@

#pragma once

#include "cc_tools_qt/details/ToolsNumericFieldImpl.h"
#include "cc_tools_qt/field/ToolsBitmaskField.h"

#include "comms/field/BitmaskValue.h"

#include <cstdint>
#include <cassert>
#include <memory>
#include <limits>

#include "comms/field/BitmaskValue.h"
#include "NumericValueWrapper.h"

namespace cc_tools_qt
{

namespace field_wrapper
namespace details
{

class CC_API BitmaskValueWrapper : public NumericValueWrapper<unsigned long long>
{
public:
typedef std::unique_ptr<BitmaskValueWrapper> ActPtr;

virtual ~BitmaskValueWrapper() noexcept;

bool bitValue(unsigned idx) const;

void setBitValue(unsigned idx, bool value);

unsigned bitIdxLimit() const;

ActPtr clone();

protected:
virtual bool bitValueImpl(unsigned idx) const = 0;
virtual void setBitValueImpl(unsigned idx, bool value) = 0;
virtual unsigned bitIdxLimitImpl() const = 0;
virtual ActPtr cloneImpl() = 0;

void dispatchImpl(FieldWrapperHandler& handler);
};

template <typename TField>
class BitmaskValueWrapperT : public NumericValueWrapperT<BitmaskValueWrapper, TField>
class ToolsBitmaskFieldImpl : public ToolsNumericFieldImpl<cc_tools_qt::field::ToolsBitmaskField, TField>
{
using Base = NumericValueWrapperT<BitmaskValueWrapper, TField>;
using Base = ToolsNumericFieldImpl<cc_tools_qt::field::ToolsBitmaskField, TField>;
using Field = TField;
static_assert(comms::field::isBitmaskValue<Field>(), "Must be of BitmaskValueField type");

Expand All @@ -68,18 +46,18 @@ class BitmaskValueWrapperT : public NumericValueWrapperT<BitmaskValueWrapper, TF
static_assert(sizeof(ValueType) <= sizeof(MaskType), "This wrapper cannot handle provided field.");

public:
typedef typename Base::ActPtr ActPtr;
using ActPtr = typename Base::ActPtr;

explicit BitmaskValueWrapperT(Field& fieldRef)
explicit ToolsBitmaskFieldImpl(Field& fieldRef)
: Base(fieldRef)
{
}

BitmaskValueWrapperT(const BitmaskValueWrapperT&) = default;
BitmaskValueWrapperT(BitmaskValueWrapperT&&) = default;
virtual ~BitmaskValueWrapperT() noexcept = default;
ToolsBitmaskFieldImpl(const ToolsBitmaskFieldImpl&) = default;
ToolsBitmaskFieldImpl(ToolsBitmaskFieldImpl&&) = default;
virtual ~ToolsBitmaskFieldImpl() noexcept = default;

BitmaskValueWrapperT& operator=(const BitmaskValueWrapperT&) = delete;
ToolsBitmaskFieldImpl& operator=(const ToolsBitmaskFieldImpl&) = delete;

protected:
virtual bool bitValueImpl(unsigned idx) const override
Expand All @@ -99,21 +77,15 @@ class BitmaskValueWrapperT : public NumericValueWrapperT<BitmaskValueWrapper, TF

virtual ActPtr cloneImpl() override
{
return ActPtr(new BitmaskValueWrapperT<TField>(Base::field()));
return ActPtr(new ToolsBitmaskFieldImpl<TField>(Base::field()));
}
};

using BitmaskValueWrapperPtr = BitmaskValueWrapper::ActPtr;

template <typename TField>
BitmaskValueWrapperPtr
makeBitmaskValueWrapper(TField& field)
cc_tools_qt::field::ToolsBitmaskFieldPtr makeBitmaskField(TField& field)
{
return
BitmaskValueWrapperPtr(
new BitmaskValueWrapperT<TField>(field));
return std::make_unique<ToolsBitmaskFieldImpl<TField>>(field);
}

} // namespace field_wrapper
} // namespace details

} // namespace cc_tools_qt
4 changes: 2 additions & 2 deletions lib/include/cc_tools_qt/details/ToolsFieldCreator.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@

#include "comms/comms.h"

#include "cc_tools_qt/details/ToolsBitmaskFieldImpl.h"
#include "cc_tools_qt/details/ToolsEnumFieldImpl.h"
#include "cc_tools_qt/details/ToolsIntFieldImpl.h"
#include "cc_tools_qt/details/ToolsUnsignedLongFieldImpl.h"

#include "cc_tools_qt/field_wrapper/BitmaskValueWrapper.h"
#include "cc_tools_qt/field_wrapper/StringWrapper.h"
#include "cc_tools_qt/field_wrapper/BitfieldWrapper.h"
#include "cc_tools_qt/field_wrapper/OptionalWrapper.h"
Expand Down Expand Up @@ -144,7 +144,7 @@ class ToolsFieldCreator
template <typename TField>
static ToolsFieldPtr createFieldInternal(TField& field, BitmaskValueTag)
{
return field_wrapper::makeBitmaskValueWrapper(field);
return makeBitmaskField(field);
}

template <typename TField>
Expand Down
59 changes: 59 additions & 0 deletions lib/include/cc_tools_qt/field/ToolsBitmaskField.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// Copyright 2014 - 2024 (C). Alex Robenko. All rights reserved.
//

// This file is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.


#pragma once

#include "cc_tools_qt/details/ToolsNumericFieldBase.h"

#include <memory>

namespace cc_tools_qt
{

namespace field
{

class CC_API ToolsBitmaskField : public details::ToolsNumericFieldBase<unsigned long long>
{
public:
typedef std::unique_ptr<ToolsBitmaskField> ActPtr;

virtual ~ToolsBitmaskField() noexcept;

bool bitValue(unsigned idx) const;

void setBitValue(unsigned idx, bool value);

unsigned bitIdxLimit() const;

ActPtr clone();

protected:
virtual bool bitValueImpl(unsigned idx) const = 0;
virtual void setBitValueImpl(unsigned idx, bool value) = 0;
virtual unsigned bitIdxLimitImpl() const = 0;
virtual ActPtr cloneImpl() = 0;

virtual void dispatchImpl(field_wrapper::FieldWrapperHandler& handler) override;
};

using ToolsBitmaskFieldPtr = ToolsBitmaskField::ActPtr;

} // namespace field

} // namespace cc_tools_qt
4 changes: 2 additions & 2 deletions lib/include/cc_tools_qt/field_wrapper/AllWrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

#pragma once

#include "cc_tools_qt/field/ToolsBitmaskField.h"
#include "cc_tools_qt/field/ToolsEnumField.h"
#include "cc_tools_qt/field/ToolsIntField.h"
#include "cc_tools_qt/field/ToolsUnsignedLongField.h"

#include "BitmaskValueWrapper.h"
#include "StringWrapper.h"
#include "BitfieldWrapper.h"
#include "OptionalWrapper.h"
Expand All @@ -44,7 +44,7 @@ namespace field_wrapper
typedef std::tuple<
cc_tools_qt::field::ToolsIntField,
cc_tools_qt::field::ToolsUnsignedLongField,
BitmaskValueWrapper,
cc_tools_qt::field::ToolsBitmaskField,
cc_tools_qt::field::ToolsEnumField,
StringWrapper,
BitfieldWrapper,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ function (lib_tools)
set (name "${PROJECT_NAME}")

set (src
field/ToolsBitmaskField.cpp
field/ToolsEnumField.cpp
field/ToolsIntField.cpp
field/ToolsUnsignedLongField.cpp
Expand All @@ -25,7 +26,6 @@ function (lib_tools)
ToolsFrame.cpp
ToolsMsgFactory.cpp
ToolsProtocol.cpp
field_wrapper/BitmaskValueWrapper.cpp
field_wrapper/StringWrapper.cpp
field_wrapper/BitfieldWrapper.cpp
field_wrapper/OptionalWrapper.cpp
Expand Down
Loading

0 comments on commit 32e1b4d

Please sign in to comment.