NewItemDialog uses dynamically generated control widgets for model data as well. Added a WidgetHelper class to consolidate identical source code for ItemDetailMapper and NewItemDialog.

This commit is contained in:
2026-03-02 19:25:27 +01:00
parent 2fb7560e6e
commit 2d8c97b5f0
7 changed files with 146 additions and 97 deletions

45
widgethelper.cpp Normal file
View File

@ -0,0 +1,45 @@
#include "widgethelper.h"
#include <QComboBox>
#include <QLineEdit>
#include <QSpinBox>
#include <QStringListModel>
QWidget* WidgetHelper::createControlWidget(const UserRoles role, QWidget* parent) {
QWidget* control;
if (STRING_ROLES.contains(role)) {
control = new QLineEdit();
} 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 {
qCritical() << QString("Unsupported role %1!!!").arg(role);
qDebug() << "Using line edit as well and pretend it's a string role...";
control = new QLineEdit();
}
return control;
}
QWidget* WidgetHelper::createComboBox(const UserRoles role, QWidget* parent) {
QStringListModel* typeModel;
if (role == TypeRole) {
typeModel = new QStringListModel(TYPES, parent);
} else {
qCritical() << "Unsupported type with role:" << role
<< "- Using string list model with only one empty string!";
typeModel = new QStringListModel({""}, parent);
}
QComboBox* comboBox = new QComboBox();
comboBox->setModel(typeModel);
comboBox->setCurrentText("");
return comboBox;
}
WidgetHelper::WidgetHelper() {}