diff --git a/widgethelper.cpp b/widgethelper.cpp index 2a8e790..bf1460b 100644 --- a/widgethelper.cpp +++ b/widgethelper.cpp @@ -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() {} diff --git a/widgethelper.h b/widgethelper.h index e86b286..f20f729 100644 --- a/widgethelper.h +++ b/widgethelper.h @@ -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