diff options
author | Anton Lundin <glance@acc.umu.se> | 2014-10-27 22:12:46 +0100 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2014-10-27 15:41:55 -0700 |
commit | ee7c86f206f5465df0f0ade08b13da3b8062e67f (patch) | |
tree | 05d6319263f40e2e48940fcd1e51d463b28f06c3 | |
parent | 0e3a9328bc10b59a4e4843819bad0b3a4d75c456 (diff) | |
download | subsurface-ee7c86f206f5465df0f0ade08b13da3b8062e67f.tar.gz |
Create a delegate for depth and gas components
This creates a delegate to simplify the handling of gas components and
the change depth.
Signed-off-by: Anton Lundin <glance@acc.umu.se>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
-rw-r--r-- | qt-ui/configuredivecomputerdialog.cpp | 44 | ||||
-rw-r--r-- | qt-ui/configuredivecomputerdialog.h | 20 |
2 files changed, 64 insertions, 0 deletions
diff --git a/qt-ui/configuredivecomputerdialog.cpp b/qt-ui/configuredivecomputerdialog.cpp index c2da24485..4b6649959 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 84c25406f..89efee622 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 |