From a4aff37dd23c312e17d10bc49dbf7553b9ab0dac Mon Sep 17 00:00:00 2001 From: Andrei-Fabian-Pop Date: Tue, 27 Aug 2024 17:24:00 +0300 Subject: [PATCH] iio-widgets: Add optional custom conversion functions Signed-off-by: Andrei-Fabian-Pop --- iio-widgets/include/iio-widgets/iiowidget.h | 11 +++++++++++ iio-widgets/src/iiowidget.cpp | 22 +++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/iio-widgets/include/iio-widgets/iiowidget.h b/iio-widgets/include/iio-widgets/iiowidget.h index 0fafe8bbee..87c0d8d30d 100644 --- a/iio-widgets/include/iio-widgets/iiowidget.h +++ b/iio-widgets/include/iio-widgets/iiowidget.h @@ -31,6 +31,7 @@ #include "datastrategy/datastrategyinterface.h" #include "scopy-iio-widgets_export.h" #include +#include namespace scopy { class GuiStrategyInterface; @@ -125,6 +126,9 @@ class SCOPY_IIO_WIDGETS_EXPORT IIOWidget : public QWidget */ int lastReturnCode(); + void setUItoDataConversion(std::function func); + void setDataToUIConversion(std::function func); + Q_SIGNALS: /** * @brief Emits the current state of the IIOWidget system and a string containing a more @@ -139,6 +143,9 @@ protected Q_SLOTS: void startTimer(QString data); void storeReadInfo(QString data, QString optionalData); + void convertUItoDS(QString data); + void convertDStoUI(QString data, QString optionalData); + protected: void initialize(); @@ -154,6 +161,10 @@ protected Q_SLOTS: QDateTime *m_lastOpTimestamp; int m_lastReturnCode; IIOWidget::State *m_lastOpState; + + /* Conversion functions */ + std::function m_UItoDS; + std::function m_DStoUI; }; } // namespace scopy diff --git a/iio-widgets/src/iiowidget.cpp b/iio-widgets/src/iiowidget.cpp index 42b8ab3fd0..5b2e78b7dd 100644 --- a/iio-widgets/src/iiowidget.cpp +++ b/iio-widgets/src/iiowidget.cpp @@ -34,6 +34,8 @@ IIOWidget::IIOWidget(GuiStrategyInterface *uiStrategy, DataStrategyInterface *da , m_lastOpTimestamp(nullptr) , m_lastOpState(nullptr) , m_lastReturnCode(0) + , m_UItoDS(nullptr) + , m_DStoUI(nullptr) { setLayout(new QVBoxLayout(this)); layout()->setContentsMargins(0, 0, 0, 0); @@ -145,6 +147,10 @@ IIOWidget::State *IIOWidget::lastOperationState() { return m_lastOpState; } int IIOWidget::lastReturnCode() { return m_lastReturnCode; } +void IIOWidget::setUItoDataConversion(std::function func) { m_UItoDS = func; } + +void IIOWidget::setDataToUIConversion(std::function func) { m_DStoUI = func; } + void IIOWidget::startTimer(QString data) { m_lastData = data; @@ -160,6 +166,22 @@ void IIOWidget::storeReadInfo(QString data, QString optionalData) setLastOperationTimestamp(QDateTime::currentDateTime()); } +void IIOWidget::convertUItoDS(QString data) +{ + if(m_UItoDS) { + data = m_UItoDS(data); + } + this->saveData(data); +} + +void IIOWidget::convertDStoUI(QString data, QString optionalData) +{ + if(m_DStoUI) { // only the data should be converted + data = m_DStoUI(data); + } + m_uiStrategy->receiveData(data, optionalData); +} + void IIOWidget::initialize() { m_dataStrategy->readAsync(); } void IIOWidget::setLastOperationTimestamp(QDateTime timestamp)