diff --git a/UIs/BeetRoundWidgets/CMakeLists.txt b/UIs/BeetRoundWidgets/CMakeLists.txt index b4e2770..8c97104 100644 --- a/UIs/BeetRoundWidgets/CMakeLists.txt +++ b/UIs/BeetRoundWidgets/CMakeLists.txt @@ -35,6 +35,7 @@ if(${QT_VERSION_MAJOR} GREATER_EQUAL 6) dialogs/settingsdialog.h dialogs/settingsdialog.cpp views/itemdetailmapper.h views/itemdetailmapper.cpp widgets/comboboxdelegate.h widgets/comboboxdelegate.cpp + widgets/spinboxdelegate.h widgets/spinboxdelegate.cpp ) # Define target properties for Android with Qt 6 as: # set_property(TARGET ${TARGET_APP} APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR diff --git a/UIs/BeetRoundWidgets/mainwindow.cpp b/UIs/BeetRoundWidgets/mainwindow.cpp index 0af97cb..7137be6 100644 --- a/UIs/BeetRoundWidgets/mainwindow.cpp +++ b/UIs/BeetRoundWidgets/mainwindow.cpp @@ -18,6 +18,7 @@ #include "model/metadata.h" #include "model/tablemodel.h" #include "widgets/comboboxdelegate.h" +#include "widgets/spinboxdelegate.h" static QStandardPaths::StandardLocation standardLocation = QStandardPaths::HomeLocation; static QString updateTextClean = "Do you want to update the application now?"; @@ -346,6 +347,15 @@ void MainWindow::setupModelViews() { // ui->tableView->setModel(m_tableModel.get()); m_proxyModel = m_core->getSortFilterModel(); + /// setting number delegates to combo boxes + SpinboxDelegate* spinboxDelegate = new SpinboxDelegate(this); + ui->tableView->setItemDelegateForColumn(0, spinboxDelegate); + ui->tableView->setItemDelegateForColumn(4, spinboxDelegate); + ui->tableView->setItemDelegateForColumn(6, spinboxDelegate); + ui->tableView->setItemDelegateForColumn(7, spinboxDelegate); + ui->tableView->setItemDelegateForColumn(8, spinboxDelegate); + + /// setting type delegates to combo boxes ComboboxDelegate* shareTypeDelegate = new ComboboxDelegate(SHARE_TYPES, this); ComboboxDelegate* biddingTypeDelegate = new ComboboxDelegate(BIDDING_TYPES, this); ui->tableView->setItemDelegateForColumn(3, shareTypeDelegate); diff --git a/UIs/BeetRoundWidgets/widgets/comboboxdelegate.cpp b/UIs/BeetRoundWidgets/widgets/comboboxdelegate.cpp index a6e5485..64753f6 100644 --- a/UIs/BeetRoundWidgets/widgets/comboboxdelegate.cpp +++ b/UIs/BeetRoundWidgets/widgets/comboboxdelegate.cpp @@ -31,9 +31,9 @@ QWidget* ComboboxDelegate::createEditor(QWidget* parent, void ComboboxDelegate::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 QString headerText = localModel->headerData(index.column(), Qt::Horizontal).toString(); - UserRoles role = GET_ROLE_FOR_COLUMN(index.column()); + const UserRoles role = GET_ROLE_FOR_COLUMN(index.column()); const bool isShareType = SHARE_TYPE_ROLES.contains(role); const bool isBiddingType = BIDDING_TYPE_ROLES.contains(role); /// Put the value into the SpinBox diff --git a/UIs/BeetRoundWidgets/widgets/spinboxdelegate.cpp b/UIs/BeetRoundWidgets/widgets/spinboxdelegate.cpp new file mode 100644 index 0000000..c408ed8 --- /dev/null +++ b/UIs/BeetRoundWidgets/widgets/spinboxdelegate.cpp @@ -0,0 +1,69 @@ +#include "spinboxdelegate.h" + +#include + +#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(editor); + spinbox->setValue(value); + } else { + // Put the value into the SpinBox + qreal value = index.model()->data(index, Qt::EditRole).toReal(); + QDoubleSpinBox* spinbox = static_cast(editor); + spinbox->setValue(value); + } + + // QStyledItemDelegate::setEditorData(editor, index); +} + +void SpinboxDelegate::setModelData(QWidget* editor, + QAbstractItemModel* model, + const QModelIndex& index) const { + QStyledItemDelegate::setModelData(editor, model, index); +} diff --git a/UIs/BeetRoundWidgets/widgets/spinboxdelegate.h b/UIs/BeetRoundWidgets/widgets/spinboxdelegate.h new file mode 100644 index 0000000..1016640 --- /dev/null +++ b/UIs/BeetRoundWidgets/widgets/spinboxdelegate.h @@ -0,0 +1,26 @@ +#ifndef SPINBOXDELEGATE_H +#define SPINBOXDELEGATE_H + +#include + +class SpinboxDelegate : public QStyledItemDelegate { + 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 diff --git a/libs/BeetRoundCore/model/modelitem.cpp b/libs/BeetRoundCore/model/modelitem.cpp index 97a6024..1a0dc5a 100644 --- a/libs/BeetRoundCore/model/modelitem.cpp +++ b/libs/BeetRoundCore/model/modelitem.cpp @@ -19,6 +19,13 @@ QVariant ModelItem::data(int role) const { case JsonObjectRole: return toJsonObject(); break; + case MembershipNumberRole: + case ShareAmountRole: + case Bidding1Role: + case Bidding2Role: + case Bidding3Role: + return getValueButReplaceZeroValueWithEmptyString(role); + break; default: return m_values.value(role); } @@ -95,3 +102,12 @@ QJsonObject ModelItem::toJsonObject() const { } return itemObject; } + +QVariant ModelItem::getValueButReplaceZeroValueWithEmptyString(const int role) const { + QVariant localValue = m_values.value(role, QVariant()); + if (localValue == 0) { + return QVariant(); + } else { + return localValue; + } +} diff --git a/libs/BeetRoundCore/model/modelitem.h b/libs/BeetRoundCore/model/modelitem.h index b6a8ce1..83c4990 100644 --- a/libs/BeetRoundCore/model/modelitem.h +++ b/libs/BeetRoundCore/model/modelitem.h @@ -20,6 +20,8 @@ class ModelItem { private: ModelItemValues m_values; + + QVariant getValueButReplaceZeroValueWithEmptyString(const int role) const; }; #endif // MODELITEM_H