This is something to show what we need to do for the
DivePlannerPointsModel, to fix entering of negative values there.
---
 qt-ui/diveplanner.cpp    |  6 ++++++
 qt-ui/modeldelegates.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++
 qt-ui/modeldelegates.h   | 16 ++++++++++++++++
 3 files changed, 65 insertions(+)

diff --git a/qt-ui/diveplanner.cpp b/qt-ui/diveplanner.cpp
index 631d605..a03b84d 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));
+       // Nope, thats a QDoubleSpinBox.
+       
//ui.tableWidget->view()->setItemDelegateForColumn(DivePlannerPointsModel::CCSETPOINT,
 new SpinBoxDelegate(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..4b58452 100644
--- a/qt-ui/modeldelegates.cpp
+++ b/qt-ui/modeldelegates.cpp
@@ -364,3 +364,46 @@ void ProfilePrintDelegate::paint(QPainter *painter, const 
QStyleOptionViewItem &
        }
        QStyledItemDelegate::paint(painter, option, index);
 }
+
+/*
+void SpinBoxDelegate::revertModelData(QWidget *widget, 
QAbstractItemDelegate::EndEditHint hint)
+{
+}
+*/
+
+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);
+       //editor->setMaximum(100);
+
+       return editor;
+}
+
+void SpinBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) 
const
+{
+       int value = index.model()->data(index, Qt::EditRole).toInt();
+
+       QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
+       spinBox->setValue(value);
+}
+
+void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, 
const QModelIndex &index) const
+{
+       QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
+       spinBox->interpretText();
+       int value = spinBox->value();
+
+       model->setData(index, value, Qt::EditRole);
+}
+
+void SpinBoxDelegate::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..b4959b2 100644
--- a/qt-ui/modeldelegates.h
+++ b/qt-ui/modeldelegates.h
@@ -88,4 +88,20 @@ 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;
+       /*
+public
+slots:
+       void revertModelData(QWidget *widget, 
QAbstractItemDelegate::EndEditHint hint);
+       */
+};
+
 #endif // MODELDELEGATES_H
-- 
1.9.1

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

Reply via email to