Skip to content

Commit

Permalink
generic: add preferences edit line with validator function
Browse files Browse the repository at this point in the history
Signed-off-by: IonutMuthi <ionut.muthi@analog.com>
  • Loading branch information
IonutMuthi committed Feb 20, 2025
1 parent 3221134 commit 9377329
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
8 changes: 7 additions & 1 deletion gui/include/gui/preferenceshelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@
#define PREFERENCE_CHECK_BOX(p, id, title, description, parent) \
PreferencesHelper::addPreferenceCheckBox(p, id, title, description, parent)

#define PREFERENCE_EDIT(p, id, title, description, parent) \
#define PREFERENCE_EDIT(p, id, title, description, parent) \
PreferencesHelper::addPreferenceEdit(p, id, title, description, parent)

#define PREFERENCE_EDIT_VALIDATION(p, id, title, description, validator, parent) \
PreferencesHelper::addPreferenceEditValidation(p, id, title, description, validator, parent)

#define PREFERENCE_COMBO(p, id, title, description, options, parent) \
PreferencesHelper::addPreferenceCombo(p, id, title, description, options, parent)

Expand All @@ -56,6 +59,9 @@ class SCOPY_GUI_EXPORT PreferencesHelper
QObject *parent = nullptr);
static QWidget *addPreferenceEdit(Preferences *p, QString id, QString title, QString description,
QObject *parent = nullptr);
static QWidget *addPreferenceEditValidation(Preferences *p, QString id, QString title, QString description,
std::function<bool(const QString &)> validator,
QObject *parent = nullptr);
static QWidget *addPreferenceCombo(Preferences *p, QString id, QString title, QString description,
QStringList options, QObject *parent = nullptr);
static QWidget *addPreferenceComboList(Preferences *p, QString id, QString title, QString description,
Expand Down
36 changes: 36 additions & 0 deletions gui/src/preferenceshelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,42 @@ QWidget *PreferencesHelper::addPreferenceEdit(Preferences *p, QString id, QStrin
return widget;
}

QWidget *PreferencesHelper::addPreferenceEditValidation(Preferences *p, QString id, QString title, QString description,
std::function<bool(const QString &)> validator, QObject *parent)
{
QWidget *widget = new QWidget();
QHBoxLayout *layout = new QHBoxLayout();
layout->setMargin(0);
layout->setSpacing(0);
layout->setMargin(0);
widget->setLayout(layout);

QWidget *infoBtn = setupDescriptionButton(id, description, parent);

QString pref1Val = p->get(id).toString();
QLineEdit *pref = new QLineEdit();
pref->setText(pref1Val);
parent->connect(pref, &QLineEdit::returnPressed, parent, [p, id, validator, pref]() {
QString prefText = pref->text();
if(validator && !validator(prefText)) {
pref->setText(p->get(id).toString());
return;
}
p->set(id, prefText);
});

QLabel *label = new QLabel(title);
p->initDescription(id, title, description);

QSpacerItem *space = new QSpacerItem(20, 20, QSizePolicy::Preferred, QSizePolicy::Preferred);

layout->addWidget(infoBtn);
layout->addWidget(label, 1);
layout->addSpacerItem(space);
layout->addWidget(pref, 1);
return widget;
}

QWidget *PreferencesHelper::addPreferenceComboList(Preferences *p, QString id, QString title, QString description,
QList<QPair<QString, QVariant>> options, QObject *parent)
{
Expand Down
11 changes: 9 additions & 2 deletions plugins/datalogger/src/dataloggerplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,16 @@ bool DataLoggerPlugin::loadPreferencesPage()
"Select the maximum data storage size for each monitor in the datalogger.",
storage_options, generalSection));

generalSection->contentLayout()->addWidget(PREFERENCE_EDIT(
generalSection->contentLayout()->addWidget(PREFERENCE_EDIT_VALIDATION(
p, "dataloggerplugin_read_interval", "Read interval (seconds) ",
"Select the time interval, in seconds, for data polling in the instrument.", generalSection));
"Select the time interval, in seconds, for data polling in the instrument.",
[](const QString &text) {
// check if input is an positive integer
bool ok;
auto value = text.toInt(&ok);
return ok && value >= 0;
},
generalSection));

generalSection->contentLayout()->addWidget(PREFERENCE_EDIT(
p, "dataloggerplugin_date_time_format", "DateTime format :",
Expand Down

0 comments on commit 9377329

Please sign in to comment.