diff --git a/qt-ui/modeldelegates.cpp b/qt-ui/modeldelegates.cpp index 58a7e141b..16140bb8d 100644 --- a/qt-ui/modeldelegates.cpp +++ b/qt-ui/modeldelegates.cpp @@ -14,6 +14,11 @@ #include #include #include +#include + +// Gets the index of the model in the currentRow and column. +// currCombo is defined below. +#define IDX( XX ) mymodel->index(currCombo.currRow, XX) StarWidgetsDelegate::StarWidgetsDelegate(QWidget* parent): QStyledItemDelegate(parent), @@ -133,6 +138,12 @@ void ComboBoxDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionV editor->setGeometry(defaultRect); } +struct RevertCylinderData{ + QString type; + int pressure; + int size; +} currCylinderData; + void TankInfoDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& thisindex) const { CylindersModel *mymodel = qobject_cast(currCombo.model); @@ -154,13 +165,39 @@ void TankInfoDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, if ( mymodel->data(thisindex, CylindersModel::TYPE).toString() == currCombo.activeText){ return; } - mymodel->setData(model->index(currCombo.currRow, CylindersModel::TYPE), currCombo.activeText, Qt::EditRole); - mymodel->passInData(model->index(currCombo.currRow, CylindersModel::WORKINGPRESS), tankPressure); - mymodel->passInData(model->index(currCombo.currRow, CylindersModel::SIZE), tankSize); + + mymodel->setData(IDX(CylindersModel::TYPE), currCombo.activeText, Qt::EditRole); + mymodel->passInData(IDX(CylindersModel::WORKINGPRESS), tankPressure); + mymodel->passInData(IDX(CylindersModel::SIZE), tankSize); } TankInfoDelegate::TankInfoDelegate(QObject* parent): ComboBoxDelegate(TankInfoModel::instance(), parent) { + connect(this, SIGNAL(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)), + this, SLOT(revertModelData(QWidget*, QAbstractItemDelegate::EndEditHint))); +} + +void TankInfoDelegate::revertModelData(QWidget* widget, QAbstractItemDelegate::EndEditHint hint) +{ + if (hint == QAbstractItemDelegate::NoHint || hint == QAbstractItemDelegate::RevertModelCache){ + CylindersModel *mymodel = qobject_cast(currCombo.model); + mymodel->setData(IDX(CylindersModel::TYPE), currCylinderData.type, Qt::EditRole); + mymodel->passInData(IDX(CylindersModel::WORKINGPRESS), currCylinderData.pressure); + mymodel->passInData(IDX(CylindersModel::SIZE), currCylinderData.size); + } +} + +QWidget* TankInfoDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const +{ + // ncreate editor needs to be called before because it will populate a few + // things in the currCombo global var. + QWidget *delegate = ComboBoxDelegate::createEditor(parent, option, index); + CylindersModel *mymodel = qobject_cast(currCombo.model); + cylinder_t *cyl = mymodel->cylinderAt(index); + currCylinderData.type = cyl->type.description; + currCylinderData.pressure = cyl->type.workingpressure.mbar; + currCylinderData.size = cyl->type.size.mliter; + return delegate; } void WSInfoDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& thisindex) const @@ -184,11 +221,16 @@ void WSInfoDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, co if (mymodel->data(thisindex, WeightModel::TYPE).toString() == currCombo.activeText){ return; } - mymodel->setData(model->index(currCombo.currRow, WeightModel::TYPE), v, Qt::EditRole); - mymodel->passInData(model->index(currCombo.currRow, WeightModel::WEIGHT), grams); + mymodel->setData(IDX(WeightModel::TYPE), v, Qt::EditRole); + mymodel->passInData(IDX(WeightModel::WEIGHT), grams); qDebug() << "Fixme, every weigth is 0.0 grams. see:" << grams; } WSInfoDelegate::WSInfoDelegate(QObject* parent): ComboBoxDelegate(WSInfoModel::instance(), parent) { } + +QWidget* WSInfoDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const +{ + return ComboBoxDelegate::createEditor(parent, option, index); +} diff --git a/qt-ui/modeldelegates.h b/qt-ui/modeldelegates.h index 7de676c6c..4ddf53df3 100644 --- a/qt-ui/modeldelegates.h +++ b/qt-ui/modeldelegates.h @@ -33,6 +33,9 @@ class TankInfoDelegate : public ComboBoxDelegate{ public: explicit TankInfoDelegate(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; +public slots: + void revertModelData(QWidget* widget, QAbstractItemDelegate::EndEditHint hint); }; class WSInfoDelegate : public ComboBoxDelegate{ @@ -40,6 +43,7 @@ class WSInfoDelegate : public ComboBoxDelegate{ public: explicit WSInfoDelegate(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; }; #endif diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 0991ddb5d..fe15a98c3 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -147,9 +147,15 @@ QVariant CylindersModel::data(const QModelIndex& index, int role) const ret = QIcon(":trash"); break; } + return ret; } +cylinder_t* CylindersModel::cylinderAt(const QModelIndex& index) +{ + return ¤t->cylinder[index.row()]; +} + // this is our magic 'pass data in' function that allows the delegate to get // the data here without silly unit conversions; // so we only implement the two columns we care about diff --git a/qt-ui/models.h b/qt-ui/models.h index bd1e60c0f..b7a72b1d2 100644 --- a/qt-ui/models.h +++ b/qt-ui/models.h @@ -84,6 +84,8 @@ public: void clear(); void update(); void setDive(struct dive *d); + cylinder_t *cylinderAt(const QModelIndex& index); + public slots: void remove(const QModelIndex& index);