From 9b0a44196c1f619b37450ef77d00aedfdad106ae Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Thu, 18 Jul 2013 10:24:02 -0300 Subject: [PATCH 1/3] 'Cancel' action Cancels editing Cylinders. Made the default 'Cancel' action correctly cancel the cylinder edition. This is needed only because we bypassed the default behavior on Qt that took care of this, because we wanted to have more control on how the view would update the items accordingly with wich one of the cylinders were selected on the edition pane - the pressure and size of the cylinders needed to have it's data set, but the Qt Model/View system *thinks* that cancel-edition is simply 'do not commit the edition data, then.' wich would not work with us, because we passed the strange data already. So, I created a backup data that serves us very well. When the user cancels, this backup data is added back on the cylinder, making everything as it was before. Signed-off-by: Tomaz Canabrava --- qt-ui/modeldelegates.cpp | 52 ++++++++++++++++++++++++++++++++++++---- qt-ui/modeldelegates.h | 4 ++++ qt-ui/models.cpp | 6 +++++ qt-ui/models.h | 2 ++ 4 files changed, 59 insertions(+), 5 deletions(-) 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); From edb1a7adb37639d50b2dc1672adde20d544211c1 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Thu, 18 Jul 2013 11:53:47 -0300 Subject: [PATCH 2/3] Added the code to revert Weigths when user cancels edition This is a follow up commit to the previous one that enabled cancel for cylinders, everything in the commit log for the cylinders also applyes here. Signed-off-by: Tomaz Canabrava --- qt-ui/modeldelegates.cpp | 26 +++++++++++++++++++++++--- qt-ui/modeldelegates.h | 3 +++ qt-ui/models.cpp | 5 +++++ qt-ui/models.h | 2 ++ 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/qt-ui/modeldelegates.cpp b/qt-ui/modeldelegates.cpp index 16140bb8d..9ccf6608f 100644 --- a/qt-ui/modeldelegates.cpp +++ b/qt-ui/modeldelegates.cpp @@ -59,6 +59,8 @@ QSize StarWidgetsDelegate::sizeHint(const QStyleOptionViewItem& option, const QM ComboBoxDelegate::ComboBoxDelegate(QAbstractItemModel *model, QObject* parent): QStyledItemDelegate(parent), model(model) { + connect(this, SIGNAL(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)), + this, SLOT(revertModelData(QWidget*, QAbstractItemDelegate::EndEditHint))); } void ComboBoxDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const @@ -173,8 +175,6 @@ void TankInfoDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, 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) @@ -200,6 +200,20 @@ QWidget* TankInfoDelegate::createEditor(QWidget* parent, const QStyleOptionViewI return delegate; } +struct RevertWeigthData { + QString type; + int weigth; +} currWeigth; + +void WSInfoDelegate::revertModelData(QWidget* widget, QAbstractItemDelegate::EndEditHint hint) +{ + if (hint == QAbstractItemDelegate::NoHint || hint == QAbstractItemDelegate::RevertModelCache){ + WeightModel *mymodel = qobject_cast(currCombo.model); + mymodel->setData(IDX(WeightModel::TYPE), currWeigth.type, Qt::EditRole); + mymodel->passInData(IDX(WeightModel::WEIGHT), currWeigth.weigth); + } +} + void WSInfoDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& thisindex) const { WeightModel *mymodel = qobject_cast(currCombo.model); @@ -232,5 +246,11 @@ WSInfoDelegate::WSInfoDelegate(QObject* parent): ComboBoxDelegate(WSInfoModel::i QWidget* WSInfoDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const { - return ComboBoxDelegate::createEditor(parent, option, index); + /* First, call the combobox-create editor, it will setup our globals. */ + QWidget *editor = ComboBoxDelegate::createEditor(parent, option, index); + WeightModel *mymodel = qobject_cast(currCombo.model); + weightsystem_t *ws = mymodel->weightSystemAt(index); + currWeigth.type = ws->description; + currWeigth.weigth = ws->weight.grams; + return editor; } diff --git a/qt-ui/modeldelegates.h b/qt-ui/modeldelegates.h index 4ddf53df3..9603d5dce 100644 --- a/qt-ui/modeldelegates.h +++ b/qt-ui/modeldelegates.h @@ -24,6 +24,7 @@ public: virtual bool eventFilter(QObject* object, QEvent* event); public slots: void testActivation(const QString& s); + virtual void revertModelData(QWidget* widget, QAbstractItemDelegate::EndEditHint hint) = 0; protected: QAbstractItemModel *model; }; @@ -44,6 +45,8 @@ 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; +public slots: + void revertModelData(QWidget* widget, QAbstractItemDelegate::EndEditHint hint); }; #endif diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index fe15a98c3..347301424 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -348,6 +348,11 @@ WeightModel::WeightModel(QObject* parent): QAbstractTableModel(parent), current( { } +weightsystem_t* WeightModel::weightSystemAt(const QModelIndex& index) +{ + return ¤t->weightsystem[index.row()]; +} + void WeightModel::remove(const QModelIndex& index) { if (index.column() != REMOVE) { diff --git a/qt-ui/models.h b/qt-ui/models.h index b7a72b1d2..e8a7daaf3 100644 --- a/qt-ui/models.h +++ b/qt-ui/models.h @@ -114,6 +114,8 @@ public: void clear(); void update(); void setDive(struct dive *d); + weightsystem_t *weightSystemAt(const QModelIndex& index); + public slots: void remove(const QModelIndex& index); From 430a77b8a75668f2fac21cdced3fac2d05073089 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Thu, 18 Jul 2013 12:28:28 -0300 Subject: [PATCH 3/3] Fixes incorrect editing policies. The Working Press didn't correctly updated when there was a 'bar' or 'psi' in the string ( and that was defalt behaviour ); The o2 didn't correctly updated when there was a '%' on the string (and that was default behaviour ), The He didn't correctly updated when there was a '%' on the string, and that was also default behavior. Now all of them correctly updates. Signed-off-by: Tomaz Canabrava --- qt-ui/models.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 347301424..0af5c7019 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -180,7 +180,7 @@ void CylindersModel::passInData(const QModelIndex& index, const QVariant& value) } } -#define CHANGED(_t,_u1,_u2) value._t() != data(index, role).toString().replace(_u1,"").replace(_u2,"")._t() +#define CHANGED(_t,_u1,_u2) value._t() != data(index, role).toString().remove(_u1).remove(_u2)._t() bool CylindersModel::setData(const QModelIndex& index, const QVariant& value, int role) { @@ -223,13 +223,15 @@ bool CylindersModel::setData(const QModelIndex& index, const QVariant& value, in break; case WORKINGPRESS: if (CHANGED(toDouble, "psi", "bar")) { - if (value.toDouble() != 0.0) { + QString vString = value.toString(); + vString.remove("psi").remove("bar"); + if (vString.toDouble() != 0.0) { TankInfoModel *tanks = TankInfoModel::instance(); QModelIndexList matches = tanks->match(tanks->index(0,0), Qt::DisplayRole, cyl->type.description); if (prefs.units.pressure == prefs.units.PSI) - cyl->type.workingpressure.mbar = psi_to_mbar(value.toDouble()); + cyl->type.workingpressure.mbar = psi_to_mbar(vString.toDouble()); else - cyl->type.workingpressure.mbar = value.toDouble() * 1000; + cyl->type.workingpressure.mbar = vString.toDouble() * 1000; if (!matches.isEmpty()) tanks->setData(tanks->index(matches.first().row(), TankInfoModel::BAR), cyl->type.workingpressure.mbar / 1000.0); mark_divelist_changed(TRUE); @@ -259,13 +261,13 @@ bool CylindersModel::setData(const QModelIndex& index, const QVariant& value, in break; case O2: if (CHANGED(toDouble, "%", "%")) { - cyl->gasmix.o2.permille = value.toDouble() * 10 + 0.5; + cyl->gasmix.o2.permille = value.toString().remove('%').toDouble() * 10 + 0.5; mark_divelist_changed(TRUE); } break; case HE: if (CHANGED(toDouble, "%", "%")) { - cyl->gasmix.he.permille = value.toDouble() * 10 + 0.5; + cyl->gasmix.he.permille = value.toString().remove('%').toDouble() * 10 + 0.5; mark_divelist_changed(TRUE); } break;