This adds some delegates to fixup the editor SpinBox'es in the
DivePlannerPointsModel, to prevent user from entering negative values.

Fixes #609

Signed-off-by: Anton Lundin <[email protected]>
---
 qt-ui/diveplanner.cpp    |  6 +++++
 qt-ui/modeldelegates.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++++++++
 qt-ui/modeldelegates.h   | 22 +++++++++++++++++
 3 files changed, 92 insertions(+)

diff --git a/qt-ui/diveplanner.cpp b/qt-ui/diveplanner.cpp
index 5e07dcb..db02865 100644
--- a/qt-ui/diveplanner.cpp
+++ b/qt-ui/diveplanner.cpp
@@ -253,6 +253,12 @@ DivePlannerWidget::DivePlannerWidget(QWidget *parent, 
Qt::WindowFlags f) : QWidg
        ui.tableWidget->setModel(DivePlannerPointsModel::instance());
        DivePlannerPointsModel::instance()->setRecalc(true);
        
ui.tableWidget->view()->setItemDelegateForColumn(DivePlannerPointsModel::GAS, 
new AirTypesDelegate(this));
+       // This makes shure the spinbox gets a setMinimum(0) on it so we can't 
have negative time or depth.
+       
ui.tableWidget->view()->setItemDelegateForColumn(DivePlannerPointsModel::DEPTH, 
new SpinBoxDelegate(this));
+       
ui.tableWidget->view()->setItemDelegateForColumn(DivePlannerPointsModel::RUNTIME,
 new SpinBoxDelegate(this));
+       
ui.tableWidget->view()->setItemDelegateForColumn(DivePlannerPointsModel::DURATION,
 new SpinBoxDelegate(this));
+       // This one is a Double, so its a bit special.
+       
ui.tableWidget->view()->setItemDelegateForColumn(DivePlannerPointsModel::CCSETPOINT,
 new DoubleSpinBoxDelegate(this));
        ui.cylinderTableWidget->setTitle(tr("Available gases"));
        ui.cylinderTableWidget->setModel(CylindersModel::instance());
        QTableView *view = ui.cylinderTableWidget->view();
diff --git a/qt-ui/modeldelegates.cpp b/qt-ui/modeldelegates.cpp
index dc6af56..c2335d5 100644
--- a/qt-ui/modeldelegates.cpp
+++ b/qt-ui/modeldelegates.cpp
@@ -364,3 +364,67 @@ void ProfilePrintDelegate::paint(QPainter *painter, const 
QStyleOptionViewItem &
        }
        QStyledItemDelegate::paint(painter, option, index);
 }
+
+SpinBoxDelegate::SpinBoxDelegate(QObject *parent) : 
QStyledItemDelegate(parent) {}
+
+QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
+               const QStyleOptionViewItem &/* option */,
+               const QModelIndex &/* index */) const
+{
+       QSpinBox *editor = new QSpinBox(parent);
+       editor->setMinimum(0);
+
+       return editor;
+}
+
+void SpinBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) 
const
+{
+       QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
+       spinBox->setValue(index.model()->data(index, Qt::EditRole).toInt());
+}
+
+void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, 
const QModelIndex &index) const
+{
+       QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
+       spinBox->interpretText();
+
+       model->setData(index, spinBox->value(), Qt::EditRole);
+}
+
+void SpinBoxDelegate::updateEditorGeometry(QWidget *editor, const 
QStyleOptionViewItem &option, const QModelIndex &/* index */) const
+{
+       editor->setGeometry(option.rect);
+}
+
+DoubleSpinBoxDelegate::DoubleSpinBoxDelegate(QObject *parent) : 
QStyledItemDelegate(parent) {}
+
+QWidget *DoubleSpinBoxDelegate::createEditor(QWidget *parent,
+               const QStyleOptionViewItem &/* option */,
+               const QModelIndex &/* index */) const
+{
+       QDoubleSpinBox *editor = new QDoubleSpinBox(parent);
+       editor->setMinimum(0);
+       editor->setSingleStep(0.1);
+
+       return editor;
+}
+
+void DoubleSpinBoxDelegate::setEditorData(QWidget *editor, const QModelIndex 
&index) const
+{
+       QDoubleSpinBox *spinBox = static_cast<QDoubleSpinBox*>(editor);
+
+       spinBox->setValue(index.model()->data(index, Qt::EditRole).toDouble());
+}
+
+void DoubleSpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel 
*model, const QModelIndex &index) const
+{
+       QDoubleSpinBox *spinBox = static_cast<QDoubleSpinBox*>(editor);
+       spinBox->interpretText();
+
+       model->setData(index, spinBox->value(), Qt::EditRole);
+}
+
+void DoubleSpinBoxDelegate::updateEditorGeometry(QWidget *editor, const 
QStyleOptionViewItem &option, const QModelIndex &/* index */) const
+{
+       editor->setGeometry(option.rect);
+}
diff --git a/qt-ui/modeldelegates.h b/qt-ui/modeldelegates.h
index e2b705e..b286347 100644
--- a/qt-ui/modeldelegates.h
+++ b/qt-ui/modeldelegates.h
@@ -88,4 +88,26 @@ public:
        void paint(QPainter *painter, const QStyleOptionViewItem &option, const 
QModelIndex &index) const;
 };
 
+class SpinBoxDelegate : public QStyledItemDelegate {
+       Q_OBJECT
+public:
+       explicit SpinBoxDelegate(QObject *parent = 0);
+
+       QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem 
&option, const QModelIndex &index) const;
+       void setEditorData(QWidget *editor, const QModelIndex &index) const;
+       void setModelData(QWidget *editor, QAbstractItemModel *model, const 
QModelIndex &index) const;
+       void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem 
&option, const QModelIndex &index) const;
+};
+
+class DoubleSpinBoxDelegate : public QStyledItemDelegate {
+       Q_OBJECT
+public:
+       explicit DoubleSpinBoxDelegate(QObject *parent = 0);
+
+       QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem 
&option, const QModelIndex &index) const;
+       void setEditorData(QWidget *editor, const QModelIndex &index) const;
+       void setModelData(QWidget *editor, QAbstractItemModel *model, const 
QModelIndex &index) const;
+       void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem 
&option, const QModelIndex &index) const;
+};
+
 #endif // MODELDELEGATES_H
-- 
1.9.1

_______________________________________________
subsurface mailing list
[email protected]
http://lists.hohndel.org/cgi-bin/mailman/listinfo/subsurface

Reply via email to