summaryrefslogtreecommitdiffstats
path: root/desktop-widgets/modeldelegates.cpp
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2020-02-28 20:38:04 +0100
committerGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2020-04-07 00:13:35 +0200
commit1dcc885bb257d6c8c64074f8788b468397c34aaa (patch)
tree39c987e36b7f0f103dbdf412c582da1b1907a0e1 /desktop-widgets/modeldelegates.cpp
parent1aa06e680230351024c05152aeca9a189c5a8d4f (diff)
downloadsubsurface-1dcc885bb257d6c8c64074f8788b468397c34aaa.tar.gz
undo/cylinders: Implement editing of the type
This one is tricky, as when browsing through the types-combobox, the user is presented with presets without actually changing the dive. We do not want an undo-command for every change-event in the combo-box. Therefore, implement a scheme analoguous to the weight-editing: A temporary row can be set / committed or reset. Sadly, the code is more complex because we have to consider the planner, which is not included in the undo system. Firstly, the planner uses a different model, therefore all interactions are channeled through setData() with special roles. Secondly, in the planner we shouldn't place an undo command, but simply overwrite the dive. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'desktop-widgets/modeldelegates.cpp')
-rw-r--r--desktop-widgets/modeldelegates.cpp34
1 files changed, 11 insertions, 23 deletions
diff --git a/desktop-widgets/modeldelegates.cpp b/desktop-widgets/modeldelegates.cpp
index ed7278b4c..65e8f377e 100644
--- a/desktop-widgets/modeldelegates.cpp
+++ b/desktop-widgets/modeldelegates.cpp
@@ -230,12 +230,6 @@ void ComboBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionV
editor->setGeometry(defaultRect);
}
-static struct RevertCylinderData {
- QString type;
- int pressure;
- int size;
-} currCylinderData;
-
void TankInfoDelegate::setModelData(QWidget*, QAbstractItemModel*, const QModelIndex&) const
{
QAbstractItemModel *mymodel = currCombo.model;
@@ -254,9 +248,9 @@ void TankInfoDelegate::setModelData(QWidget*, QAbstractItemModel*, const QModelI
int tankSize = tanks->data(tanks->index(row, TankInfoModel::ML)).toInt();
int tankPressure = tanks->data(tanks->index(row, TankInfoModel::BAR)).toInt();
- mymodel->setData(IDX(CylindersModel::TYPE), cylinderName, Qt::EditRole);
- mymodel->setData(IDX(CylindersModel::WORKINGPRESS), tankPressure, CylindersModel::PASS_IN_ROLE);
- mymodel->setData(IDX(CylindersModel::SIZE), tankSize, CylindersModel::PASS_IN_ROLE);
+ mymodel->setData(IDX(CylindersModel::TYPE), cylinderName, CylindersModel::TEMP_ROLE);
+ mymodel->setData(IDX(CylindersModel::WORKINGPRESS), tankPressure, CylindersModel::TEMP_ROLE);
+ mymodel->setData(IDX(CylindersModel::SIZE), tankSize, CylindersModel::TEMP_ROLE);
}
TankInfoDelegate::TankInfoDelegate(QObject *parent) : ComboBoxDelegate(TankInfoModel::instance(), parent, true)
@@ -275,25 +269,19 @@ void TankInfoDelegate::reenableReplot(QWidget*, QAbstractItemDelegate::EndEditHi
void TankInfoDelegate::editorClosed(QWidget*, QAbstractItemDelegate::EndEditHint hint)
{
- if (hint == QAbstractItemDelegate::NoHint ||
- hint == QAbstractItemDelegate::RevertModelCache) {
- QAbstractItemModel *mymodel = currCombo.model;
- mymodel->setData(IDX(CylindersModel::TYPE), currCylinderData.type, Qt::EditRole);
- mymodel->setData(IDX(CylindersModel::WORKINGPRESS), currCylinderData.pressure, CylindersModel::PASS_IN_ROLE);
- mymodel->setData(IDX(CylindersModel::SIZE), currCylinderData.size, CylindersModel::PASS_IN_ROLE);
- }
+ QAbstractItemModel *mymodel = currCombo.model;
+ // Ugly hack: We misuse setData() with COMMIT_ROLE or REVERT_ROLE to commit or
+ // revert the current row. We send in the type, because we may get multiple
+ // end events and thus can prevent multiple commits.
+ if (hint == QAbstractItemDelegate::RevertModelCache)
+ mymodel->setData(IDX(CylindersModel::TYPE), currCombo.activeText, CylindersModel::REVERT_ROLE);
+ else
+ mymodel->setData(IDX(CylindersModel::TYPE), currCombo.activeText, CylindersModel::COMMIT_ROLE);
}
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);
- QAbstractItemModel *model = currCombo.model;
- int row = index.row();
- currCylinderData.type = model->data(model->index(row, CylindersModel::TYPE)).value<QString>();
- currCylinderData.pressure = model->data(model->index(row, CylindersModel::WORKINGPRESS_INT)).value<int>();
- currCylinderData.size = model->data(model->index(row, CylindersModel::SIZE_INT)).value<int>();
MainWindow::instance()->graphics->setReplot(false);
return delegate;
}