#pragma once #include "FractoriumPch.h" #include "DoubleSpinBox.h" /// /// DoubleSpinBoxTableItemDelegate class. /// /// /// Used for showing a DoubleSpinBox on the cell of a QTableView when the user enters the cell to edit it. /// class DoubleSpinBoxTableItemDelegate : public QItemDelegate { Q_OBJECT public: /// /// Constructor that assigns a DoubleSpinBox. /// /// The DoubleSpinBox to use throughought the life of the object /// The parent widget. Default: nullptr. explicit DoubleSpinBoxTableItemDelegate(DoubleSpinBox* spinBox, QObject* parent = nullptr) : QItemDelegate(parent), m_SpinBox(spinBox) { } /// /// Re-parent and return the DoubleSpinBox to display when the user clicks on a cell and it enters edit mode. /// The re-parenting is done so that the DoubleSpinBox appears directly on top of the cell. /// /// The parent cell /// Ignored /// Ignored /// The DoubleSpinBox member QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const override { m_SpinBox->setParent(parent); return m_SpinBox; } /// /// Prevent DoubleSpinBox control from being destroyed when the cell loses focus. /// /// Ignored /// Ignored void destroyEditor(QWidget* editor, const QModelIndex& index) const override { } /// /// Set the value of the DoubleSpinBox as well as its tableindex property. /// /// Ignored /// Ignored void setEditorData(QWidget* editor, const QModelIndex& index) const override { const QPoint p(index.row(), index.column()); const auto value = index.model()->data(index, Qt::EditRole).toDouble(); m_SpinBox->setProperty("tableindex", p); m_SpinBox->setValue(value); } /// /// Set the cell in the model to the value of the DoubleSpinBox. /// /// Ignored /// The model whose value will be set /// The cell index of the model void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const override { model->setData(index, m_SpinBox->value(), Qt::EditRole); } /// /// Set the geometry of the DoubleSpinBox to match the cell being edited. /// /// The DoubleSpinBox member /// Contains the rectangle to be used for the geometry of the DoubleSpinBox /// Ignored void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const override { editor->setGeometry(option.rect); } private: DoubleSpinBox* m_SpinBox; };