[PATCH 2/2] Create a delegate for depth and gas components

Anton Lundin glance at acc.umu.se
Mon Oct 27 14:12:46 PDT 2014


This creates a delegate to simplify the handling of gas components and
the change depth.

Signed-off-by: Anton Lundin <glance at acc.umu.se>
---
 qt-ui/configuredivecomputerdialog.cpp | 44 +++++++++++++++++++++++++++++++++++
 qt-ui/configuredivecomputerdialog.h   | 20 ++++++++++++++++
 2 files changed, 64 insertions(+)

diff --git a/qt-ui/configuredivecomputerdialog.cpp b/qt-ui/configuredivecomputerdialog.cpp
index c2da244..4b66499 100644
--- a/qt-ui/configuredivecomputerdialog.cpp
+++ b/qt-ui/configuredivecomputerdialog.cpp
@@ -28,6 +28,42 @@ struct mydescriptor {
 	unsigned int model;
 };
 
+GasSpinBoxItemDelegate::GasSpinBoxItemDelegate(QObject *parent, column_type type) : QStyledItemDelegate(parent), type(type) { }
+GasSpinBoxItemDelegate::~GasSpinBoxItemDelegate() { }
+
+QWidget* GasSpinBoxItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
+{
+	// Create the spinbox and give it it's settings
+	QSpinBox *sb = new QSpinBox(parent);
+	if (type == PERCENT) {
+		sb->setMinimum(0);
+		sb->setMaximum(100);
+		sb->setSuffix("%");
+	} else if (type == DEPTH) {
+		sb->setMinimum(0);
+		sb->setMaximum(255);
+		sb->setSuffix("m");
+	}
+	return sb;
+}
+
+void GasSpinBoxItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
+{
+	if(QSpinBox *sb = qobject_cast<QSpinBox *>(editor))
+		sb->setValue(index.data(Qt::EditRole).toInt());
+	else
+		QStyledItemDelegate::setEditorData(editor, index);
+}
+
+
+void GasSpinBoxItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
+{
+	if(QSpinBox *sb = qobject_cast<QSpinBox *>(editor))
+		model->setData(index, sb->value(), Qt::EditRole);
+	else
+		QStyledItemDelegate::setModelData(editor, model, index);
+}
+
 GasTypeComboBoxItemDelegate::GasTypeComboBoxItemDelegate(QObject *parent, computer_type type) : QStyledItemDelegate(parent), type(type) { }
 GasTypeComboBoxItemDelegate::~GasTypeComboBoxItemDelegate() { }
 
@@ -89,10 +125,18 @@ ConfigureDiveComputerDialog::ConfigureDiveComputerDialog(QWidget *parent) :
 	ui.DiveComputerList->setCurrentRow(0);
 	on_DiveComputerList_currentRowChanged(0);
 
+	ui.ostc3GasTable->setItemDelegateForColumn(1, new GasSpinBoxItemDelegate(this, GasSpinBoxItemDelegate::PERCENT));
+	ui.ostc3GasTable->setItemDelegateForColumn(2, new GasSpinBoxItemDelegate(this, GasSpinBoxItemDelegate::PERCENT));
 	ui.ostc3GasTable->setItemDelegateForColumn(3, new GasTypeComboBoxItemDelegate(this, GasTypeComboBoxItemDelegate::OSTC3));
+	ui.ostc3GasTable->setItemDelegateForColumn(4, new GasSpinBoxItemDelegate(this, GasSpinBoxItemDelegate::DEPTH));
 	ui.ostc3DilTable->setItemDelegateForColumn(3, new GasTypeComboBoxItemDelegate(this, GasTypeComboBoxItemDelegate::OSTC3));
+	ui.ostc3DilTable->setItemDelegateForColumn(4, new GasSpinBoxItemDelegate(this, GasSpinBoxItemDelegate::DEPTH));
+	ui.ostcGasTable->setItemDelegateForColumn(1, new GasSpinBoxItemDelegate(this, GasSpinBoxItemDelegate::PERCENT));
+	ui.ostcGasTable->setItemDelegateForColumn(2, new GasSpinBoxItemDelegate(this, GasSpinBoxItemDelegate::PERCENT));
 	ui.ostcGasTable->setItemDelegateForColumn(3, new GasTypeComboBoxItemDelegate(this, GasTypeComboBoxItemDelegate::OSTC));
+	ui.ostcGasTable->setItemDelegateForColumn(4, new GasSpinBoxItemDelegate(this, GasSpinBoxItemDelegate::DEPTH));
 	ui.ostcDilTable->setItemDelegateForColumn(3, new GasTypeComboBoxItemDelegate(this, GasTypeComboBoxItemDelegate::OSTC));
+	ui.ostcDilTable->setItemDelegateForColumn(4, new GasSpinBoxItemDelegate(this, GasSpinBoxItemDelegate::DEPTH));
 
 	QSettings settings;
 	settings.beginGroup("ConfigureDiveComputerDialog");
diff --git a/qt-ui/configuredivecomputerdialog.h b/qt-ui/configuredivecomputerdialog.h
index 84c2540..89efee6 100644
--- a/qt-ui/configuredivecomputerdialog.h
+++ b/qt-ui/configuredivecomputerdialog.h
@@ -8,6 +8,26 @@
 #include "configuredivecomputer.h"
 #include <QStyledItemDelegate>
 
+class GasSpinBoxItemDelegate : public QStyledItemDelegate
+{
+	Q_OBJECT
+
+public:
+	enum column_type {
+		PERCENT,
+		DEPTH,
+	};
+
+	GasSpinBoxItemDelegate(QObject *parent = 0, column_type type = PERCENT);
+	~GasSpinBoxItemDelegate();
+
+	virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
+	virtual void setEditorData(QWidget *editor, const QModelIndex &index) const;
+	virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
+private:
+	column_type type;
+};
+
 class GasTypeComboBoxItemDelegate : public QStyledItemDelegate
 {
 	Q_OBJECT
-- 
1.9.1



More information about the subsurface mailing list