Skip to content

Commit

Permalink
iio-widgets/IIOWidget: Add methods for last operation timestamp and s…
Browse files Browse the repository at this point in the history
…tate

Signed-off-by: Andrei-Fabian-Pop <[email protected]>
  • Loading branch information
Andrei-Fabian-Pop committed Apr 8, 2024
1 parent b070032 commit 70836cc
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 8 deletions.
41 changes: 34 additions & 7 deletions iio-widgets/include/iio-widgets/iiowidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,25 @@ class SCOPY_IIO_WIDGETS_EXPORT IIOWidget : public QWidget
Q_OBJECT
QWIDGET_PAINT_EVENT_HELPER
public:
typedef enum
{
Busy,
Correct,
Error
} State;

IIOWidget(AttrUiStrategyInterface *uiStrategy, DataStrategyInterface *dataStrategy, QWidget *parent = nullptr);

/**
* @brief Returns the UI of the IIOWidget
* @return AttrUiStrategyInterface
* */
AttrUiStrategyInterface *getUiStrategy();

/**
* @brief Returns the data save/load strategy
* @return DataStretegyInterface
* */
DataStrategyInterface *getDataStrategy();

/**
Expand All @@ -59,16 +75,22 @@ class SCOPY_IIO_WIDGETS_EXPORT IIOWidget : public QWidget
* */
void setRecipe(IIOWidgetFactoryRecipe recipe);

typedef enum
{
Busy,
Correct,
Error
} State;
/**
* @brief Returns the timestamp for the last operation
* @return QDateTime if there exists a last operation or nullptr if no operation was performed on this IIOWidget
* */
QDateTime *lastOperationTimestamp();

/**
* @brief Returns the state of the last operation
* @return IIOWidget::State if there exists a last operation or nullptr if no operation was performed on this IIOWidget
* */
IIOWidget::State *lastOperationState();

Q_SIGNALS:
/**
* @brief 0 - busy, 1 - correct, 2 error
* @brief Emits the current state of the IIOWidget system and a string containing a more
* elaborate explanation of the current state
* */
void currentStateChanged(State currentState, QString explanation = "");

Expand All @@ -79,12 +101,17 @@ protected Q_SLOTS:
void startTimer(QString data);

protected:
void setLastOperationTimestamp(QDateTime timestamp);
void setLastOperationState(IIOWidget::State state);

AttrUiStrategyInterface *m_uiStrategy;
DataStrategyInterface *m_dataStrategy;
IIOWidgetFactoryRecipe m_recipe;

QString m_lastData;
SmallProgressBar *m_progressBar;
QDateTime *m_lastOpTimestamp;
IIOWidget::State *m_lastOpState;
};
} // namespace scopy

Expand Down
37 changes: 36 additions & 1 deletion iio-widgets/src/iiowidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ IIOWidget::IIOWidget(AttrUiStrategyInterface *uiStrategy, DataStrategyInterface
, m_uiStrategy(uiStrategy)
, m_dataStrategy(dataStrategy)
, m_progressBar(new SmallProgressBar(this))
, m_lastOpTimestamp(nullptr)
, m_lastOpState(nullptr)
{
setLayout(new QVBoxLayout(this));
layout()->setContentsMargins(0, 0, 0, 0);
Expand All @@ -46,8 +48,11 @@ IIOWidget::IIOWidget(AttrUiStrategyInterface *uiStrategy, DataStrategyInterface
connect(dynamic_cast<QWidget *>(m_uiStrategy), SIGNAL(emitData(QString)), this, SLOT(startTimer(QString)));
connect(dynamic_cast<QWidget *>(m_dataStrategy), SIGNAL(emitStatus(int)), this, SLOT(emitDataStatus(int)));

// forward data request from ui strategy to data strategy
connect(dynamic_cast<QWidget *>(m_uiStrategy), SIGNAL(requestData()), dynamic_cast<QWidget *>(m_dataStrategy),
SLOT(requestData()));

// forward data from data strategy to ui strategy
connect(dynamic_cast<QWidget *>(m_dataStrategy), SIGNAL(sendData(QString, QString)),
dynamic_cast<QWidget *>(m_uiStrategy), SLOT(receiveData(QString, QString)));

Expand All @@ -56,6 +61,7 @@ IIOWidget::IIOWidget(AttrUiStrategyInterface *uiStrategy, DataStrategyInterface

void IIOWidget::saveData(QString data)
{
setLastOperationState(IIOWidget::Busy);
m_progressBar->setBarColor(StyleHelper::getColor("ProgressBarBusy"));
setToolTip("Operation in progress.");

Expand All @@ -65,17 +71,20 @@ void IIOWidget::saveData(QString data)

void IIOWidget::emitDataStatus(int status)
{
QString timestamp = QDateTime::currentDateTime().toString("hh:mm:ss");
setLastOperationTimestamp(QDateTime::currentDateTime());
QString timestamp = m_lastOpTimestamp->toString("hh:mm:ss");
if(status < 0) {
m_progressBar->setBarColor(StyleHelper::getColor("ProgressBarError"));
QString statusString = "Tried to write \"" + m_lastData +
"\", but failed.\nError: " + QString(strerror(-status)) + " (" + QString::number(status) + ").";
setToolTip("[" + timestamp + "] " + statusString);
setLastOperationState(IIOWidget::Error);
qDebug(CAT_IIOWIDGET) << statusString;
} else {
m_progressBar->setBarColor(StyleHelper::getColor("ProgressBarSuccess"));
QString statusString = "Operation finished successfully.";
setToolTip("[" + timestamp + "] " + statusString);
setLastOperationState(IIOWidget::Correct);
qDebug(CAT_IIOWIDGET) << statusString << ". Wrote " + m_lastData + ".";
}
auto *timer = new QTimer();
Expand All @@ -96,11 +105,37 @@ IIOWidgetFactoryRecipe IIOWidget::getRecipe() { return m_recipe; }

void IIOWidget::setRecipe(IIOWidgetFactoryRecipe recipe) { m_recipe = recipe; }

QDateTime *IIOWidget::lastOperationTimestamp()
{
return m_lastOpTimestamp;
}

IIOWidget::State *IIOWidget::lastOperationState()
{
return m_lastOpState;
}

void IIOWidget::startTimer(QString data)
{
m_lastData = data;
m_progressBar->setBarColor(StyleHelper::getColor("ScopyBlue"));
m_progressBar->startProgress();
}

void IIOWidget::setLastOperationTimestamp(QDateTime timestamp)
{
if (m_lastOpTimestamp == nullptr) {
m_lastOpTimestamp = new QDateTime();
}
*m_lastOpTimestamp = timestamp;
}

void IIOWidget::setLastOperationState(State state)
{
if (m_lastOpState == nullptr) {
m_lastOpState = new IIOWidget::State;
}
*m_lastOpState = state;
}

#include "moc_iiowidget.cpp"

0 comments on commit 70836cc

Please sign in to comment.