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

View File

@ -11,6 +11,8 @@
#include <QSpinBox>
#include <QTableView>
#include "../widgethelper.h"
ItemDetailMapper::ItemDetailMapper(QTableView* tableView, QWidget* parent)
: QWidget{parent} {
/// model mapping
@ -127,9 +129,11 @@ void ItemDetailMapper::setupWidgets() {
}
void ItemDetailMapper::setupWidgetPairForColumn(const int column) {
// REFACTOR consolidate ItemDetailMapper::setupWidgetPairForColumn(...)
// with NewItemDialog::setupWidgetPairForColumn(...)
const UserRoles role = GET_ROLE_FOR_COLUMN(column);
QWidget* control = createControlWidget(role);
QWidget* control = WidgetHelper::createControlWidget(role, this);
const QString string = QString("&%1").arg(GET_HEADER_FOR_COLUMN(column));
QLabel* label = new QLabel(string);
@ -146,42 +150,6 @@ void ItemDetailMapper::setupWidgetPairForColumn(const int column) {
}
}
QWidget* ItemDetailMapper::createControlWidget(const UserRoles role) {
QWidget* control;
if (STRING_ROLES.contains(role)) {
control = new QLineEdit();
} else if (TYPE_ROLES.contains(role)) {
control = createComboBox(role);
} 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* ItemDetailMapper::createComboBox(const UserRoles role) {
QStringListModel* typeModel;
if (role == TypeRole) {
typeModel = new QStringListModel(TYPES, this);
} else {
qCritical() << "Unsupported type with role:" << role
<< "- Using string list model with only one empty string!";
typeModel = new QStringListModel({""}, this);
}
QComboBox* comboBox = new QComboBox();
comboBox->setModel(typeModel);
return comboBox;
}
void ItemDetailMapper::clearControlWidgets() {
setEnabled(false);