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

View File

@ -8,10 +8,13 @@ class QWidget;
class WidgetHelper {
public:
static QWidget* createControlWidget(const UserRoles role, QWidget* parent);
static QWidget* createComboBox(const UserRoles role, QWidget* parent);
private:
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