'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 <tcanabrava@kde.org>
This commit is contained in:
Tomaz Canabrava 2013-07-18 10:24:02 -03:00
parent 63c33e152e
commit 9b0a44196c
4 changed files with 59 additions and 5 deletions

View file

@ -14,6 +14,11 @@
#include <QLineEdit> #include <QLineEdit>
#include <QKeyEvent> #include <QKeyEvent>
#include <QAbstractItemView> #include <QAbstractItemView>
#include <boost/concept_check.hpp>
// 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): StarWidgetsDelegate::StarWidgetsDelegate(QWidget* parent):
QStyledItemDelegate(parent), QStyledItemDelegate(parent),
@ -133,6 +138,12 @@ void ComboBoxDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionV
editor->setGeometry(defaultRect); editor->setGeometry(defaultRect);
} }
struct RevertCylinderData{
QString type;
int pressure;
int size;
} currCylinderData;
void TankInfoDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& thisindex) const void TankInfoDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& thisindex) const
{ {
CylindersModel *mymodel = qobject_cast<CylindersModel *>(currCombo.model); CylindersModel *mymodel = qobject_cast<CylindersModel *>(currCombo.model);
@ -154,13 +165,39 @@ void TankInfoDelegate::setModelData(QWidget* editor, QAbstractItemModel* model,
if ( mymodel->data(thisindex, CylindersModel::TYPE).toString() == currCombo.activeText){ if ( mymodel->data(thisindex, CylindersModel::TYPE).toString() == currCombo.activeText){
return; return;
} }
mymodel->setData(model->index(currCombo.currRow, CylindersModel::TYPE), currCombo.activeText, Qt::EditRole);
mymodel->passInData(model->index(currCombo.currRow, CylindersModel::WORKINGPRESS), tankPressure); mymodel->setData(IDX(CylindersModel::TYPE), currCombo.activeText, Qt::EditRole);
mymodel->passInData(model->index(currCombo.currRow, CylindersModel::SIZE), tankSize); mymodel->passInData(IDX(CylindersModel::WORKINGPRESS), tankPressure);
mymodel->passInData(IDX(CylindersModel::SIZE), tankSize);
} }
TankInfoDelegate::TankInfoDelegate(QObject* parent): ComboBoxDelegate(TankInfoModel::instance(), parent) 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<CylindersModel *>(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<CylindersModel *>(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 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){ if (mymodel->data(thisindex, WeightModel::TYPE).toString() == currCombo.activeText){
return; return;
} }
mymodel->setData(model->index(currCombo.currRow, WeightModel::TYPE), v, Qt::EditRole); mymodel->setData(IDX(WeightModel::TYPE), v, Qt::EditRole);
mymodel->passInData(model->index(currCombo.currRow, WeightModel::WEIGHT), grams); mymodel->passInData(IDX(WeightModel::WEIGHT), grams);
qDebug() << "Fixme, every weigth is 0.0 grams. see:" << grams; qDebug() << "Fixme, every weigth is 0.0 grams. see:" << grams;
} }
WSInfoDelegate::WSInfoDelegate(QObject* parent): ComboBoxDelegate(WSInfoModel::instance(), parent) 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);
}

View file

@ -33,6 +33,9 @@ class TankInfoDelegate : public ComboBoxDelegate{
public: public:
explicit TankInfoDelegate(QObject* parent = 0); explicit TankInfoDelegate(QObject* parent = 0);
virtual void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const; 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{ class WSInfoDelegate : public ComboBoxDelegate{
@ -40,6 +43,7 @@ class WSInfoDelegate : public ComboBoxDelegate{
public: public:
explicit WSInfoDelegate(QObject* parent = 0); explicit WSInfoDelegate(QObject* parent = 0);
virtual void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const; virtual void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const;
virtual QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const;
}; };
#endif #endif

View file

@ -147,9 +147,15 @@ QVariant CylindersModel::data(const QModelIndex& index, int role) const
ret = QIcon(":trash"); ret = QIcon(":trash");
break; break;
} }
return ret; return ret;
} }
cylinder_t* CylindersModel::cylinderAt(const QModelIndex& index)
{
return &current->cylinder[index.row()];
}
// this is our magic 'pass data in' function that allows the delegate to get // this is our magic 'pass data in' function that allows the delegate to get
// the data here without silly unit conversions; // the data here without silly unit conversions;
// so we only implement the two columns we care about // so we only implement the two columns we care about

View file

@ -84,6 +84,8 @@ public:
void clear(); void clear();
void update(); void update();
void setDive(struct dive *d); void setDive(struct dive *d);
cylinder_t *cylinderAt(const QModelIndex& index);
public slots: public slots:
void remove(const QModelIndex& index); void remove(const QModelIndex& index);