[PATCH 1/2] Create a delegate for gas type columns

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


This creates a delegate for the type column to choose the type value for
gases that is less confusing then the raw value.

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

diff --git a/qt-ui/configuredivecomputerdialog.cpp b/qt-ui/configuredivecomputerdialog.cpp
index a97d1db..c2da244 100644
--- a/qt-ui/configuredivecomputerdialog.cpp
+++ b/qt-ui/configuredivecomputerdialog.cpp
@@ -28,6 +28,42 @@ struct mydescriptor {
 	unsigned int model;
 };
 
+GasTypeComboBoxItemDelegate::GasTypeComboBoxItemDelegate(QObject *parent, computer_type type) : QStyledItemDelegate(parent), type(type) { }
+GasTypeComboBoxItemDelegate::~GasTypeComboBoxItemDelegate() { }
+
+QWidget* GasTypeComboBoxItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
+{
+	// Create the combobox and populate it
+	QComboBox *cb = new QComboBox(parent);
+	cb->addItem(QString("Disabled"));
+	if (type == OSTC3) {
+		cb->addItem(QString("Fist"));
+		cb->addItem(QString("Travel"));
+		cb->addItem(QString("Deco"));
+	} else if (type == OSTC) {
+		cb->addItem(QString("Active"));
+		cb->addItem(QString("Fist"));
+	}
+	return cb;
+}
+
+void GasTypeComboBoxItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
+{
+	if(QComboBox *cb = qobject_cast<QComboBox *>(editor))
+		cb->setCurrentIndex(index.data(Qt::EditRole).toInt());
+	else
+		QStyledItemDelegate::setEditorData(editor, index);
+}
+
+
+void GasTypeComboBoxItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
+{
+	if(QComboBox *cb = qobject_cast<QComboBox *>(editor))
+		model->setData(index, cb->currentIndex(), Qt::EditRole);
+	else
+		QStyledItemDelegate::setModelData(editor, model, index);
+}
+
 ConfigureDiveComputerDialog::ConfigureDiveComputerDialog(QWidget *parent) :
 	QDialog(parent),
 	config(0),
@@ -53,6 +89,11 @@ ConfigureDiveComputerDialog::ConfigureDiveComputerDialog(QWidget *parent) :
 	ui.DiveComputerList->setCurrentRow(0);
 	on_DiveComputerList_currentRowChanged(0);
 
+	ui.ostc3GasTable->setItemDelegateForColumn(3, new GasTypeComboBoxItemDelegate(this, GasTypeComboBoxItemDelegate::OSTC3));
+	ui.ostc3DilTable->setItemDelegateForColumn(3, new GasTypeComboBoxItemDelegate(this, GasTypeComboBoxItemDelegate::OSTC3));
+	ui.ostcGasTable->setItemDelegateForColumn(3, new GasTypeComboBoxItemDelegate(this, GasTypeComboBoxItemDelegate::OSTC));
+	ui.ostcDilTable->setItemDelegateForColumn(3, new GasTypeComboBoxItemDelegate(this, GasTypeComboBoxItemDelegate::OSTC));
+
 	QSettings settings;
 	settings.beginGroup("ConfigureDiveComputerDialog");
 	settings.beginGroup("ostc3GasTable");
diff --git a/qt-ui/configuredivecomputerdialog.h b/qt-ui/configuredivecomputerdialog.h
index 941feaa..84c2540 100644
--- a/qt-ui/configuredivecomputerdialog.h
+++ b/qt-ui/configuredivecomputerdialog.h
@@ -6,6 +6,27 @@
 #include "ui_configuredivecomputerdialog.h"
 #include "../libdivecomputer.h"
 #include "configuredivecomputer.h"
+#include <QStyledItemDelegate>
+
+class GasTypeComboBoxItemDelegate : public QStyledItemDelegate
+{
+	Q_OBJECT
+
+public:
+	enum computer_type {
+		OSTC3,
+		OSTC,
+	};
+
+	GasTypeComboBoxItemDelegate(QObject *parent = 0, computer_type type = OSTC3);
+	~GasTypeComboBoxItemDelegate();
+
+	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:
+	computer_type type;
+};
 
 class ConfigureDiveComputerDialog : public QDialog
 {
-- 
1.9.1



More information about the subsurface mailing list