Refactored the creation of control widgets.

This commit is contained in:
2026-04-02 09:51:04 +02:00
parent 24192df5d6
commit 6408eaa10a
2 changed files with 37 additions and 13 deletions

View File

@ -8,26 +8,49 @@
QWidget* WidgetHelper::createControlWidget(const UserRoles role, QWidget* parent) { QWidget* WidgetHelper::createControlWidget(const UserRoles role, QWidget* parent) {
QWidget* control; QWidget* control;
if (STRING_ROLES.contains(role)) { if (STRING_ROLES.contains(role)) {
control = new QLineEdit(); control = createLineEdit(role, parent);
} else if (TYPE_ROLES.contains(role)) { } else if (TYPE_ROLES.contains(role)) {
control = createComboBox(role, parent); control = createComboBox(role, parent);
} else if (INT_ROLES.contains(role)) { } else if (NUMBER_ROLES.contains(role)) {
QSpinBox* spinBox = new QSpinBox(); control = createSpinBox(role, parent);
spinBox->setMaximum(1000);
control = spinBox;
} else if (DOUBLE_ROLES.contains(role)) {
QDoubleSpinBox* spinBox = new QDoubleSpinBox();
spinBox->setMaximum(1000);
control = spinBox;
} else { } else {
qCritical() << QString("Unsupported role %1!!!").arg(role); qCritical() << QString("Unsupported role %1!!!").arg(role);
qDebug() << "Using line edit as well and pretend it's a string role..."; qDebug() << "Using line edit as well and pretend it's a string role...";
control = new QLineEdit(); control = createLineEdit(role, parent);
} }
return control; return control;
} }
WidgetHelper::WidgetHelper() {}
QWidget* WidgetHelper::createLineEdit(const UserRoles role, QWidget* /*parent*/) {
QLineEdit* lineEdit = new QLineEdit();
if (READ_ONLY_ROLES.contains(role)) {
lineEdit->setReadOnly(true);
}
return lineEdit;
}
QWidget* WidgetHelper::createSpinBox(const UserRoles role, QWidget* /*parent*/) {
QAbstractSpinBox* abstractSpinBox;
if (DOUBLE_ROLES.contains(role)) {
QDoubleSpinBox* spinBox = new QDoubleSpinBox();
spinBox->setMaximum(1000);
abstractSpinBox = spinBox;
} else {
QSpinBox* spinBox = new QSpinBox();
spinBox->setMaximum(1000);
abstractSpinBox = spinBox;
}
if (READ_ONLY_ROLES.contains(role)) {
abstractSpinBox->setReadOnly(true);
}
return abstractSpinBox;
}
QWidget* WidgetHelper::createComboBox(const UserRoles role, QWidget* parent) { QWidget* WidgetHelper::createComboBox(const UserRoles role, QWidget* parent) {
// TODO add support for read only type roles?
QStringListModel* typeModel; QStringListModel* typeModel;
if (role == TypeRole) { if (role == TypeRole) {
typeModel = new QStringListModel(TYPES, parent); typeModel = new QStringListModel(TYPES, parent);
@ -41,5 +64,3 @@ QWidget* WidgetHelper::createComboBox(const UserRoles role, QWidget* parent) {
comboBox->setCurrentText(""); comboBox->setCurrentText("");
return comboBox; return comboBox;
} }
WidgetHelper::WidgetHelper() {}

View File

@ -8,10 +8,13 @@ class QWidget;
class WidgetHelper { class WidgetHelper {
public: public:
static QWidget* createControlWidget(const UserRoles role, QWidget* parent); static QWidget* createControlWidget(const UserRoles role, QWidget* parent);
static QWidget* createComboBox(const UserRoles role, QWidget* parent);
private: private:
explicit WidgetHelper(); explicit WidgetHelper();
static QWidget* createLineEdit(const UserRoles role, QWidget* parent);
static QWidget* createSpinBox(const UserRoles role, QWidget* parent);
static QWidget* createComboBox(const UserRoles role, QWidget* parent);
}; };
#endif // WIDGETHELPER_H #endif // WIDGETHELPER_H