Add the ability to modify the cylinder use in the UI

Thanks to Tomaz for writing a first draft of the delegate.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2014-11-17 14:03:37 +00:00
parent 1739042f34
commit 0c3f13d128
5 changed files with 53 additions and 2 deletions

View file

@ -103,6 +103,7 @@ MainTab::MainTab(QWidget *parent) : QTabWidget(parent),
connect(ui.weights->view(), SIGNAL(clicked(QModelIndex)), this, SLOT(editWeightWidget(QModelIndex)));
ui.cylinders->view()->setItemDelegateForColumn(CylindersModel::TYPE, new TankInfoDelegate(this));
ui.cylinders->view()->setItemDelegateForColumn(CylindersModel::USE, new TankUseDelegate(this));
ui.weights->view()->setItemDelegateForColumn(WeightModel::TYPE, new WSInfoDelegate(this));
ui.cylinders->view()->setColumnHidden(CylindersModel::DEPTH, true);
completers.buddy = new QCompleter(&buddyModel, ui.buddy);
@ -612,6 +613,10 @@ void MainTab::updateDiveInfo(bool clear)
}
editMode = NONE;
ui.cylinders->view()->hideColumn(CylindersModel::DEPTH);
if (get_dive_dc(&displayed_dive, dc_number)->dctype == CCR)
ui.cylinders->view()->showColumn(CylindersModel::USE);
else
ui.cylinders->view()->hideColumn(CylindersModel::USE);
}
void MainTab::addCylinder_clicked()

View file

@ -277,6 +277,32 @@ QWidget *TankInfoDelegate::createEditor(QWidget *parent, const QStyleOptionViewI
return delegate;
}
TankUseDelegate::TankUseDelegate(QObject *parent)
{
}
QWidget *TankUseDelegate::createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const
{
QComboBox *comboBox = new QComboBox(parent);
for (int i = 0; i < NUM_GAS_USE; i++)
comboBox->addItem(QString(cylinderuse_text[i]));
return comboBox;
}
void TankUseDelegate::setEditorData(QWidget * editor, const QModelIndex & index) const
{
QComboBox *comboBox = qobject_cast<QComboBox*>(editor);
QString indexString = index.data().toString();
comboBox->setCurrentIndex(cylinderuse_from_text(indexString.toUtf8().data()));
}
void TankUseDelegate::setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const
{
QComboBox *comboBox = qobject_cast<QComboBox*>(editor);
model->setData(index, comboBox->currentIndex());
}
struct RevertWeightData {
QString type;
int weight;

View file

@ -2,7 +2,7 @@
#define MODELDELEGATES_H
#include <QStyledItemDelegate>
class QComboBox;
#include <QComboBox>
class QPainter;
class DiveListDelegate : public QStyledItemDelegate {
@ -59,6 +59,15 @@ slots:
void revertModelData(QWidget *widget, QAbstractItemDelegate::EndEditHint hint);
};
class TankUseDelegate : public QStyledItemDelegate {
Q_OBJECT
public:
explicit TankUseDelegate(QObject *parent = 0);
virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
virtual void setEditorData(QWidget * editor, const QModelIndex & index) const;
};
class WSInfoDelegate : public ComboBoxDelegate {
Q_OBJECT
public:

View file

@ -76,7 +76,7 @@ CylindersModel::CylindersModel(QObject *parent) : changed(false),
{
// enum {REMOVE, TYPE, SIZE, WORKINGPRESS, START, END, O2, HE, DEPTH};
setHeaderDataStrings(QStringList() << "" << tr("Type") << tr("Size") << tr("Work press.") << tr("Start press.") << tr("End press.") << trUtf8("O" UTF8_SUBSCRIPT_2 "%") << tr("He%")
<< tr("Switch at"));
<< tr("Switch at") << tr("Use"));
initTrashIcon();
}
@ -172,6 +172,9 @@ QVariant CylindersModel::data(const QModelIndex &index, int role) const
case DEPTH:
ret = get_depth_string(cyl->depth, true);
break;
case USE:
ret = QString(cylinderuse_text[cyl->cylinder_use]);
break;
}
break;
case Qt::DecorationRole:
@ -297,6 +300,13 @@ bool CylindersModel::setData(const QModelIndex &index, const QVariant &value, in
cyl->depth = string_to_depth(vString.toUtf8().data());
changed = true;
}
break;
case USE:
if (CHANGED()) {
cyl->cylinder_use = (enum cylinderuse)vString.toInt();
changed = true;
}
break;
}
if (addDiveMode)
DivePlannerPointsModel::instance()->tanksUpdated();

View file

@ -107,6 +107,7 @@ public:
O2,
HE,
DEPTH,
USE,
COLUMNS
};