Compare commits
3 Commits
7fa8612313
...
6408eaa10a
| Author | SHA1 | Date | |
|---|---|---|---|
| 6408eaa10a | |||
| 24192df5d6 | |||
| c7b0e4f3e5 |
@ -36,6 +36,7 @@ if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
|
||||
views/itemdetailmapper.h views/itemdetailmapper.cpp
|
||||
widgets/controls/comboboxdelegate.h widgets/controls/comboboxdelegate.cpp
|
||||
widgethelper.h widgethelper.cpp
|
||||
widgets/controls/spinboxdelegate.h widgets/controls/spinboxdelegate.cpp
|
||||
)
|
||||
# Define target properties for Android with Qt 6 as:
|
||||
# set_property(TARGET ${TARGET_APP} APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
|
||||
|
||||
@ -18,6 +18,7 @@
|
||||
#include "model/metadata.h"
|
||||
#include "model/tablemodel.h"
|
||||
#include "widgets/controls/comboboxdelegate.h"
|
||||
#include "widgets/controls/spinboxdelegate.h"
|
||||
|
||||
static QStandardPaths::StandardLocation standardLocation = QStandardPaths::HomeLocation;
|
||||
static QString updateTextClean = "Do you want to update the application now?";
|
||||
@ -40,7 +41,6 @@ MainWindow::MainWindow(QWidget* parent)
|
||||
transform.rotate(180);
|
||||
QPixmap rotated = pixmap.transformed(transform);
|
||||
QIcon appIcon = QIcon(rotated);
|
||||
qWarning() << "appIcon.availableSizes():" << appIcon.availableSizes();
|
||||
setWindowIcon(QIcon(rotated));
|
||||
#else
|
||||
setWindowIcon(QIcon(iconString));
|
||||
@ -252,7 +252,6 @@ void MainWindow::importCSV() {
|
||||
tr("CSV Files (*.csv)"));
|
||||
if (QFileInfo::exists(csvFilePath)) {
|
||||
m_core->importCSVFile(csvFilePath);
|
||||
showStatusMessage(tr("Imported CSV file."));
|
||||
} else {
|
||||
qWarning() << "Selected CSV file path doesn't exist. Doing nothing...";
|
||||
showStatusMessage(tr("Could't find CSV file!"));
|
||||
@ -345,6 +344,16 @@ void MainWindow::setupModelViews() {
|
||||
// ui->tableView->setModel(m_tableModel.get());
|
||||
m_proxyModel = m_core->getSortFilterModel();
|
||||
|
||||
// TODO iterate over INT_ROLES and DOUBLE_ROLES to set spinbox delegate
|
||||
/// setting number delegates to combo boxes
|
||||
SpinboxDelegate* spinboxDelegate = new SpinboxDelegate(this);
|
||||
const int amountColumn = GET_COLUMN_FOR_ROLE(AmountRole);
|
||||
ui->tableView->setItemDelegateForColumn(amountColumn, spinboxDelegate);
|
||||
const int factorColumn = GET_COLUMN_FOR_ROLE(FactorRole);
|
||||
ui->tableView->setItemDelegateForColumn(factorColumn, spinboxDelegate);
|
||||
|
||||
// TODO iterate over TYPE_ROLES to set combobox delegate
|
||||
/// setting type delegates to combo boxes
|
||||
const int typeColumn = GET_COLUMN_FOR_ROLE(TypeRole);
|
||||
ComboboxDelegate* shareTypeDelegate = new ComboboxDelegate(TYPES, this);
|
||||
ui->tableView->setItemDelegateForColumn(typeColumn, shareTypeDelegate);
|
||||
|
||||
@ -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() {}
|
||||
|
||||
@ -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
|
||||
|
||||
69
widgets/controls/spinboxdelegate.cpp
Normal file
69
widgets/controls/spinboxdelegate.cpp
Normal file
@ -0,0 +1,69 @@
|
||||
#include "spinboxdelegate.h"
|
||||
|
||||
#include <QSpinBox>
|
||||
|
||||
#include "model/metadata.h"
|
||||
|
||||
SpinboxDelegate::SpinboxDelegate(QObject* parent)
|
||||
: QStyledItemDelegate(parent) {}
|
||||
|
||||
void SpinboxDelegate::paint(QPainter* painter,
|
||||
const QStyleOptionViewItem& option,
|
||||
const QModelIndex& index) const {
|
||||
QStyledItemDelegate::paint(painter, option, index);
|
||||
}
|
||||
|
||||
QSize SpinboxDelegate::sizeHint(const QStyleOptionViewItem& option,
|
||||
const QModelIndex& index) const {
|
||||
return QStyledItemDelegate::sizeHint(option, index);
|
||||
}
|
||||
|
||||
QWidget* SpinboxDelegate::createEditor(QWidget* parent,
|
||||
const QStyleOptionViewItem& /*option*/,
|
||||
const QModelIndex& index) const {
|
||||
const QAbstractItemModel* localModel = index.model();
|
||||
QString headerText = localModel->headerData(index.column(), Qt::Horizontal).toString();
|
||||
|
||||
const UserRoles role = GET_ROLE_FOR_COLUMN(index.column());
|
||||
const bool isInt = INT_ROLES.contains(role);
|
||||
if (isInt) {
|
||||
QSpinBox* editor = new QSpinBox(parent);
|
||||
editor->setMinimum(0);
|
||||
editor->setMaximum(23000);
|
||||
return editor;
|
||||
} else {
|
||||
QDoubleSpinBox* editor = new QDoubleSpinBox(parent);
|
||||
editor->setMinimum(0);
|
||||
editor->setMaximum(23000);
|
||||
return editor;
|
||||
}
|
||||
// return QStyledItemDelegate::createEditor(parent, option, index);
|
||||
}
|
||||
|
||||
void SpinboxDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const {
|
||||
// Get the value via index of the Model
|
||||
const QAbstractItemModel* localModel = index.model();
|
||||
QString headerText = localModel->headerData(index.column(), Qt::Horizontal).toString();
|
||||
|
||||
const UserRoles role = GET_ROLE_FOR_COLUMN(index.column());
|
||||
const bool isInt = INT_ROLES.contains(role);
|
||||
if (isInt) {
|
||||
int value = index.model()->data(index, Qt::EditRole).toInt();
|
||||
// Put the value into the SpinBox
|
||||
QSpinBox* spinbox = static_cast<QSpinBox*>(editor);
|
||||
spinbox->setValue(value);
|
||||
} else {
|
||||
// Put the value into the SpinBox
|
||||
qreal value = index.model()->data(index, Qt::EditRole).toReal();
|
||||
QDoubleSpinBox* spinbox = static_cast<QDoubleSpinBox*>(editor);
|
||||
spinbox->setValue(value);
|
||||
}
|
||||
|
||||
// QStyledItemDelegate::setEditorData(editor, index);
|
||||
}
|
||||
|
||||
void SpinboxDelegate::setModelData(QWidget* editor,
|
||||
QAbstractItemModel* model,
|
||||
const QModelIndex& index) const {
|
||||
QStyledItemDelegate::setModelData(editor, model, index);
|
||||
}
|
||||
27
widgets/controls/spinboxdelegate.h
Normal file
27
widgets/controls/spinboxdelegate.h
Normal file
@ -0,0 +1,27 @@
|
||||
#ifndef SPINBOXDELEGATE_H
|
||||
#define SPINBOXDELEGATE_H
|
||||
|
||||
#include <QStyledItemDelegate>
|
||||
|
||||
class SpinboxDelegate : public QStyledItemDelegate {
|
||||
// TODO move source code files into subfolder "widgets/delegate"
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit SpinboxDelegate(QObject* parent = nullptr);
|
||||
|
||||
/// QAbstractItemDelegate interface
|
||||
public:
|
||||
void paint(QPainter* painter,
|
||||
const QStyleOptionViewItem& option,
|
||||
const QModelIndex& index) const override;
|
||||
QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const override;
|
||||
QWidget* createEditor(QWidget* parent,
|
||||
const QStyleOptionViewItem& option,
|
||||
const QModelIndex& index) const override;
|
||||
void setEditorData(QWidget* editor, const QModelIndex& index) const override;
|
||||
void setModelData(QWidget* editor,
|
||||
QAbstractItemModel* model,
|
||||
const QModelIndex& index) const override;
|
||||
};
|
||||
|
||||
#endif // SPINBOXDELEGATE_H
|
||||
Reference in New Issue
Block a user