Files
GenericQtClientWidgets/widgethelper.cpp

67 lines
2.0 KiB
C++

#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 = createLineEdit(role, parent);
} else if (TYPE_ROLES.contains(role)) {
control = createComboBox(role, parent);
} 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 = 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);
} 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;
}