Skip to content

Commit

Permalink
iio-widgets: Add optional custom conversion functions
Browse files Browse the repository at this point in the history
Signed-off-by: Andrei-Fabian-Pop <[email protected]>
  • Loading branch information
Andrei-Fabian-Pop authored and AlexandraTrifan committed Sep 3, 2024
1 parent f6345ce commit a4aff37
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
11 changes: 11 additions & 0 deletions iio-widgets/include/iio-widgets/iiowidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "datastrategy/datastrategyinterface.h"
#include "scopy-iio-widgets_export.h"
#include <pluginbase/lazyloadwidget.h>
#include <functional>

namespace scopy {
class GuiStrategyInterface;
Expand Down Expand Up @@ -125,6 +126,9 @@ class SCOPY_IIO_WIDGETS_EXPORT IIOWidget : public QWidget
*/
int lastReturnCode();

void setUItoDataConversion(std::function<QString(QString)> func);
void setDataToUIConversion(std::function<QString(QString)> func);

Q_SIGNALS:
/**
* @brief Emits the current state of the IIOWidget system and a string containing a more
Expand All @@ -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();

Expand All @@ -154,6 +161,10 @@ protected Q_SLOTS:
QDateTime *m_lastOpTimestamp;
int m_lastReturnCode;
IIOWidget::State *m_lastOpState;

/* Conversion functions */
std::function<QString(QString)> m_UItoDS;
std::function<QString(QString)> m_DStoUI;
};
} // namespace scopy

Expand Down
22 changes: 22 additions & 0 deletions iio-widgets/src/iiowidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -145,6 +147,10 @@ IIOWidget::State *IIOWidget::lastOperationState() { return m_lastOpState; }

int IIOWidget::lastReturnCode() { return m_lastReturnCode; }

void IIOWidget::setUItoDataConversion(std::function<QString(QString)> func) { m_UItoDS = func; }

void IIOWidget::setDataToUIConversion(std::function<QString(QString)> func) { m_DStoUI = func; }

void IIOWidget::startTimer(QString data)
{
m_lastData = data;
Expand All @@ -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)
Expand Down

0 comments on commit a4aff37

Please sign in to comment.