Skip to content

Commit

Permalink
iio-widgets/UIStrategy: Add compact mode to UI Strategies
Browse files Browse the repository at this point in the history
Compact mode reduces the size of the IIOWidgets to a single line. It can
be toggled on/off by the compactMode() function in the builder.

Signed-off-by: Andrei-Fabian-Pop <[email protected]>
  • Loading branch information
Andrei-Fabian-Pop committed Jun 26, 2024
1 parent 13ccef8 commit 5b14537
Show file tree
Hide file tree
Showing 14 changed files with 193 additions and 90 deletions.
1 change: 1 addition & 0 deletions gui/include/gui/stylehelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class SCOPY_GUI_EXPORT StyleHelper : public QObject
static void ActiveStoredLabel(QLabel *w, QString objectName = "");
static void FaultsFrame(QFrame *w, QString objectName = "");
static void FaultsExplanation(QWidget *w, QString objectName = "");
static void IIOCompactLabel(QLabel *label, QString objectName = "");

private:
QMap<QString, QString> colorMap;
Expand Down
3 changes: 2 additions & 1 deletion gui/include/gui/widgets/titlespinbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class SCOPY_GUI_EXPORT TitleSpinBox : public QWidget
Q_OBJECT

public:
explicit TitleSpinBox(QString title, QWidget *parent = nullptr);
explicit TitleSpinBox(QString title, bool isCompact = false, QWidget *parent = nullptr);
~TitleSpinBox();

void setTitle(QString title);
Expand Down Expand Up @@ -45,6 +45,7 @@ class SCOPY_GUI_EXPORT TitleSpinBox : public QWidget
* @return
*/
static QString truncValue(double value);
void connectSignalsAndSlots();

QPushButton *m_spinBoxUpButton;
QPushButton *m_spinBoxDownButton;
Expand Down
21 changes: 21 additions & 0 deletions gui/src/stylehelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1491,4 +1491,25 @@ void StyleHelper::FaultsExplanation(QWidget *w, QString objectName)
w->setStyleSheet(style);
}

void StyleHelper::IIOCompactLabel(QLabel *w, QString objectName)
{
if(!objectName.isEmpty())
w->setObjectName(objectName);
w->setText(w->text().toUpper());
w->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
QString style = R"css(
QLabel {
color: white;
background-color: rgba(255,255,255,0);
font-weight: 500;
font-family: Open Sans;
font-size: 12px;
font-style: normal;
}
QLabel:disabled {
color: grey;
})css";
w->setStyleSheet(style);
}

#include "moc_stylehelper.cpp"
141 changes: 83 additions & 58 deletions gui/src/widgets/titlespinbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using namespace scopy;
Q_LOGGING_CATEGORY(CAT_TITLESPINBOX, "TitleSpinBox")

TitleSpinBox::TitleSpinBox(QString title, QWidget *parent)
TitleSpinBox::TitleSpinBox(QString title, bool isCompact, QWidget *parent)
: QWidget(parent)
, m_titleLabel(new QLabel(title, this))
, m_lineedit(new QLineEdit(this))
Expand All @@ -27,18 +27,8 @@ TitleSpinBox::TitleSpinBox(QString title, QWidget *parent)
m_lineedit->setMaximumHeight(25);

QWidget *spinboxWidget = new QWidget(this);
QVBoxLayout *spinboxWidgetLayout = new QVBoxLayout(spinboxWidget);
spinboxWidgetLayout->setSpacing(0);
spinboxWidgetLayout->setMargin(0);

spinboxWidgetLayout->addWidget(m_titleLabel);
spinboxWidgetLayout->addWidget(m_lineedit);

QWidget *buttonWidget = new QWidget(this);
buttonWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
QVBoxLayout *buttonWidgetLayout = new QVBoxLayout(buttonWidget);
buttonWidgetLayout->setSpacing(1);
buttonWidgetLayout->setContentsMargins(0, 0, 0, 1);

m_spinBoxUpButton->setAutoRepeat(true); // so the user can hold down the button and it will react
StyleHelper::SpinBoxUpButton(m_spinBoxUpButton, "SpinBoxUpButton");
Expand All @@ -50,56 +40,47 @@ TitleSpinBox::TitleSpinBox(QString title, QWidget *parent)
m_spinBoxDownButton->setIconSize(QSize(20, 20));
m_spinBoxDownButton->setFixedSize(20, 20);

buttonWidgetLayout->addWidget(m_spinBoxUpButton);
buttonWidgetLayout->addWidget(m_spinBoxDownButton);

// here we preffer the pressed signal rather than the clicked one to speed up the change of values
connect(m_spinBoxUpButton, &QPushButton::pressed, m_lineedit, [this] {
bool ok = true;
QString text = m_lineedit->text();
double value = text.toDouble(&ok);
if(!ok) {
// If the cast fails that means that there is an issue with the text and the
// min/max/step values are useless here. The signal will just be skipped and
// a debug message will de displayed.
qDebug(CAT_TITLESPINBOX) << "Cannot increase the value:" << text;
return;
}

double newValue = value + m_step;
if(newValue > m_max) {
newValue = value;
}

m_lineedit->setText(truncValue(newValue));
});

connect(m_spinBoxDownButton, &QPushButton::pressed, m_lineedit, [this] {
bool ok = true;
QString text = m_lineedit->text();
double value = text.toDouble(&ok);
if(!ok) {
// If the cast fails that means that there is an issue with the text and the
// min/max/step values are useless here. The signal will just be skipped and
// a debug message will de displayed.
qDebug(CAT_TITLESPINBOX) << "Cannot decrease the value:" << text;
return;
}

double newValue = value - m_step;
if(newValue < m_min) {
newValue = value;
}
QLayout *buttonWidgetLayout;
QLayout *spinboxWidgetLayout;

if(isCompact) {
m_titleLabel->setText(m_titleLabel->text().toUpper());
m_titleLabel->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
StyleHelper::IIOCompactLabel(m_titleLabel, "IIOTitleLabel");
m_lineedit->setAlignment(Qt::AlignRight);

buttonWidgetLayout = new QHBoxLayout(buttonWidget);
spinboxWidgetLayout = new QHBoxLayout(spinboxWidget);

buttonWidgetLayout->addWidget(m_spinBoxDownButton);
buttonWidgetLayout->addWidget(m_spinBoxUpButton);
spinboxWidgetLayout->addWidget(m_titleLabel);
spinboxWidgetLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Preferred));
spinboxWidgetLayout->addWidget(m_lineedit);

mainLayout->addWidget(spinboxWidget);
mainLayout->addWidget(buttonWidget);
} else {
buttonWidgetLayout = new QVBoxLayout(buttonWidget);
spinboxWidgetLayout = new QVBoxLayout(spinboxWidget);

buttonWidgetLayout->addWidget(m_spinBoxUpButton);
buttonWidgetLayout->addWidget(m_spinBoxDownButton);
spinboxWidgetLayout->addWidget(m_titleLabel);
spinboxWidgetLayout->addWidget(m_lineedit);

mainLayout->addWidget(spinboxWidget);
mainLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Preferred));
mainLayout->addWidget(buttonWidget);
}

m_lineedit->setText(truncValue(newValue));
});
buttonWidgetLayout->setSpacing(1);
buttonWidgetLayout->setContentsMargins(0, 0, 0, 1);

spinboxWidgetLayout->addWidget(m_titleLabel);
spinboxWidgetLayout->addWidget(m_lineedit);
spinboxWidgetLayout->setSpacing(0);
spinboxWidgetLayout->setMargin(0);

mainLayout->addWidget(spinboxWidget);
mainLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Preferred));
mainLayout->addWidget(buttonWidget);
connectSignalsAndSlots();
}

TitleSpinBox::~TitleSpinBox() {}
Expand Down Expand Up @@ -150,4 +131,48 @@ QString TitleSpinBox::truncValue(double value)
return sReturn;
}

void TitleSpinBox::connectSignalsAndSlots()
{
// here we preffer the pressed signal rather than the clicked one to speed up the change of values
connect(m_spinBoxUpButton, &QPushButton::pressed, m_lineedit, [this] {
bool ok = true;
QString text = m_lineedit->text();
double value = text.toDouble(&ok);
if(!ok) {
// If the cast fails that means that there is an issue with the text and the
// min/max/step values are useless here. The signal will just be skipped and
// a debug message will de displayed.
qDebug(CAT_TITLESPINBOX) << "Cannot increase the value:" << text;
return;
}

double newValue = value + m_step;
if(newValue > m_max) {
newValue = value;
}

m_lineedit->setText(truncValue(newValue));
});

connect(m_spinBoxDownButton, &QPushButton::pressed, m_lineedit, [this] {
bool ok = true;
QString text = m_lineedit->text();
double value = text.toDouble(&ok);
if(!ok) {
// If the cast fails that means that there is an issue with the text and the
// min/max/step values are useless here. The signal will just be skipped and
// a debug message will de displayed.
qDebug(CAT_TITLESPINBOX) << "Cannot decrease the value:" << text;
return;
}

double newValue = value - m_step;
if(newValue < m_min) {
newValue = value;
}

m_lineedit->setText(truncValue(newValue));
});
}

#include "moc_titlespinbox.cpp"
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class SCOPY_IIO_WIDGETS_EXPORT ComboAttrUi : public QWidget, public GuiStrategyI
/**
* @brief This contain a MenuComboWidget that takes the options for the combo from recipe->linkedAttributeValue.
* */
explicit ComboAttrUi(IIOWidgetFactoryRecipe recipe, QWidget *parent = nullptr);
explicit ComboAttrUi(IIOWidgetFactoryRecipe recipe, bool isCompact = false, QWidget *parent = nullptr);
~ComboAttrUi();

/**
Expand All @@ -57,7 +57,8 @@ public Q_SLOTS:

private:
QWidget *m_ui;
MenuCombo *m_comboWidget;
QComboBox *m_comboWidget;
bool m_isCompact;
};
} // namespace scopy

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class SCOPY_IIO_WIDGETS_EXPORT EditableGuiStrategy : public QWidget, public GuiS
/**
* @brief This contain a MenuLineEdit with no validation on what the text can or cannot be set.
* */
explicit EditableGuiStrategy(IIOWidgetFactoryRecipe recipe, QWidget *parent = nullptr);
explicit EditableGuiStrategy(IIOWidgetFactoryRecipe recipe, bool isCompact = false, QWidget *parent = nullptr);
~EditableGuiStrategy();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class SCOPY_IIO_WIDGETS_EXPORT RangeAttrUi : public QWidget, public GuiStrategyI
* string from recipe->linkedAttributeValue should look like "[begin step end]" where "begin", "step" and "end"
* will be converted to double.
* */
explicit RangeAttrUi(IIOWidgetFactoryRecipe recipe, QWidget *parent = nullptr);
explicit RangeAttrUi(IIOWidgetFactoryRecipe recipe, bool isCompact = false, QWidget *parent = nullptr);
~RangeAttrUi();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class SCOPY_IIO_WIDGETS_EXPORT SwitchAttrUi : public QWidget, public GuiStrategy
* @brief This contain a CustomSwitch capable of holding no more than 2 values, the ones specified in
* recipe->linkedAttributeValue.
* */
explicit SwitchAttrUi(IIOWidgetFactoryRecipe recipe, QWidget *parent = nullptr);
explicit SwitchAttrUi(IIOWidgetFactoryRecipe recipe, bool isCompact = false, QWidget *parent = nullptr);
~SwitchAttrUi();

/**
Expand Down
10 changes: 10 additions & 0 deletions iio-widgets/include/iio-widgets/iiowidgetbuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ class SCOPY_IIO_WIDGETS_EXPORT IIOWidgetBuilder : public QObject
*/
IIOWidgetBuilder &connection(scopy::Connection *connection);

/**
* @brief Sets the UI of the widgets to compact mode, this way, the widget
* sizes will be more compact.
* @param isCompact If this is set to true, the widget will be in compact
* mode. If false, the widget will be in normal mode.
* @return
*/
IIOWidgetBuilder &compactMode(bool isCompact);

/**
* @brief Sets the context that will be used, if no iio_device or iio_channel
* is set, the build functions will work with the context.
Expand Down Expand Up @@ -151,6 +160,7 @@ class SCOPY_IIO_WIDGETS_EXPORT IIOWidgetBuilder : public QObject
GuiStrategyInterface *createUIS();

Connection *m_connection;
bool m_isCompact;
struct iio_context *m_context;
struct iio_device *m_device;
struct iio_channel *m_channel;
Expand Down
55 changes: 42 additions & 13 deletions iio-widgets/src/guistrategy/comboguistrategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,44 @@

using namespace scopy;

ComboAttrUi::ComboAttrUi(IIOWidgetFactoryRecipe recipe, QWidget *parent)
ComboAttrUi::ComboAttrUi(IIOWidgetFactoryRecipe recipe, bool isCompact, QWidget *parent)
: m_ui(new QWidget(nullptr))
, m_isCompact(isCompact)
{
m_recipe = recipe;
m_ui->setLayout(new QVBoxLayout(m_ui));
m_ui->layout()->setContentsMargins(0, 0, 0, 0);

m_comboWidget = new MenuCombo(recipe.data, m_ui);
StyleHelper::IIOComboBox(m_comboWidget->combo(), "IIOComboBox");
if(m_isCompact) {
m_ui->setLayout(new QHBoxLayout(m_ui));
m_ui->layout()->setContentsMargins(0, 0, 0, 0);

m_ui->layout()->addWidget(m_comboWidget);
Q_EMIT requestData();
auto label = new QLabel(recipe.data, m_ui);
StyleHelper::IIOCompactLabel(label, "IIOTitleLabel");
m_comboWidget = new QComboBox(m_ui);
m_comboWidget->setSizeAdjustPolicy(QComboBox::SizeAdjustPolicy::AdjustToContents);

StyleHelper::IIOComboBox(m_comboWidget, "IIOComboBox");
m_comboWidget->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);

m_ui->layout()->addWidget(label);
m_ui->layout()->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Preferred));
m_ui->layout()->addWidget(m_comboWidget);
} else {
m_ui->setLayout(new QVBoxLayout(m_ui));
m_ui->layout()->setContentsMargins(0, 0, 0, 0);

auto comboMenuWidget = new MenuCombo(recipe.data, m_ui);
m_comboWidget = comboMenuWidget->combo();
StyleHelper::IIOComboBox(m_comboWidget, "IIOComboBox");

m_ui->layout()->addWidget(comboMenuWidget);
}

connect(m_comboWidget->combo(), QOverload<int>::of(&QComboBox::currentIndexChanged), this, [this](int index) {
QString currentData = m_comboWidget->combo()->currentText();
connect(m_comboWidget, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [this](int index) {
QString currentData = m_comboWidget->currentText();
Q_EMIT emitData(currentData);
});

Q_EMIT requestData();
}

ComboAttrUi::~ComboAttrUi() { m_ui->deleteLater(); }
Expand All @@ -56,14 +77,22 @@ bool ComboAttrUi::isValid()

void ComboAttrUi::receiveData(QString currentData, QString optionalData)
{
QSignalBlocker blocker(m_comboWidget->combo());
m_comboWidget->combo()->clear();
QSignalBlocker blocker(m_comboWidget);
m_comboWidget->clear();
QStringList optionsList = QString(optionalData).split(" ", Qt::SkipEmptyParts);
for(const QString &item : optionsList) {
m_comboWidget->combo()->addItem(item);
m_comboWidget->addItem(item);
}

if(m_isCompact) {
// hackish: in compact mode, the value is pushed to the right and the width is
// at the minimum. Because of this, the viewport is smaller than the size and some
// values might not be seen. The temporary solution is to add this line (until the
// new gui system and the new designs).
m_comboWidget->view()->setMinimumWidth(m_comboWidget->view()->sizeHintForColumn(0));
}

m_comboWidget->combo()->setCurrentText(currentData);
m_comboWidget->setCurrentText(currentData);
Q_EMIT displayedNewData(currentData, optionalData);
}

Expand Down
Loading

0 comments on commit 5b14537

Please sign in to comment.