summaryrefslogtreecommitdiffstats
path: root/desktop-widgets/modeldelegates.cpp
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2019-11-04 20:20:32 +0100
committerGravatar Dirk Hohndel <dirk@hohndel.org>2019-12-05 10:14:25 -0800
commit029c9ccf020fdb73c148da489e0e7b1acd3ca149 (patch)
tree28e92d6662d6e6ece3755369b30164d316d76f53 /desktop-widgets/modeldelegates.cpp
parentab99ca85f12b92ed55219c82ec2220aebfe1dda5 (diff)
downloadsubsurface-029c9ccf020fdb73c148da489e0e7b1acd3ca149.tar.gz
Desktop: refactor WSInfoDelegate logic
The WSInfoDelegate (weight-system-info delegate) is used to display a combo box of known weightsystem-types and auto-fills the weight if the weightsystem-type is changed. This would overwrite the weight data of the displayed dive when the user hovers over the different entries. Moreover, it saves the original weight in case the user cancels the editing action. This is not viable when implementing undo of weightsystem changes, because hovering over entries should not produce individual undo commands. Instead, implement a special "temporary" row in the weightsystem model. On canceling of the edit actions, simply reload the weightsystem from the unmodified dive. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'desktop-widgets/modeldelegates.cpp')
-rw-r--r--desktop-widgets/modeldelegates.cpp45
1 files changed, 12 insertions, 33 deletions
diff --git a/desktop-widgets/modeldelegates.cpp b/desktop-widgets/modeldelegates.cpp
index 6d5d9f9a4..715150386 100644
--- a/desktop-widgets/modeldelegates.cpp
+++ b/desktop-widgets/modeldelegates.cpp
@@ -85,10 +85,8 @@ const QSize& StarWidgetsDelegate::starSize() const
ComboBoxDelegate::ComboBoxDelegate(QAbstractItemModel *model, QObject *parent, bool allowEdit) : QStyledItemDelegate(parent), model(model)
{
editable = allowEdit;
- connect(this, SIGNAL(closeEditor(QWidget *, QAbstractItemDelegate::EndEditHint)),
- this, SLOT(revertModelData(QWidget *, QAbstractItemDelegate::EndEditHint)));
- connect(this, SIGNAL(closeEditor(QWidget *, QAbstractItemDelegate::EndEditHint)),
- this, SLOT(fixTabBehavior()));
+ connect(this, &ComboBoxDelegate::closeEditor, this, &ComboBoxDelegate::editorClosed);
+ connect(this, &ComboBoxDelegate::closeEditor, this, &ComboBoxDelegate::fixTabBehavior);
}
void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
@@ -276,7 +274,7 @@ void TankInfoDelegate::reenableReplot(QWidget*, QAbstractItemDelegate::EndEditHi
// MainWindow::instance()->graphics->replot();
}
-void TankInfoDelegate::revertModelData(QWidget*, QAbstractItemDelegate::EndEditHint hint)
+void TankInfoDelegate::editorClosed(QWidget*, QAbstractItemDelegate::EndEditHint hint)
{
if (hint == QAbstractItemDelegate::NoHint ||
hint == QAbstractItemDelegate::RevertModelCache) {
@@ -326,19 +324,13 @@ void TankUseDelegate::setModelData(QWidget * editor, QAbstractItemModel * model,
model->setData(index, comboBox->currentIndex());
}
-static struct RevertWeightData {
- QString type;
- int weight;
-} currWeight;
-
-void WSInfoDelegate::revertModelData(QWidget*, QAbstractItemDelegate::EndEditHint hint)
+void WSInfoDelegate::editorClosed(QWidget*, QAbstractItemDelegate::EndEditHint hint)
{
- if (hint == QAbstractItemDelegate::NoHint ||
- hint == QAbstractItemDelegate::RevertModelCache) {
- WeightModel *mymodel = qobject_cast<WeightModel *>(currCombo.model);
- mymodel->setData(IDX(WeightModel::TYPE), currWeight.type, Qt::EditRole);
- mymodel->passInData(IDX(WeightModel::WEIGHT), currWeight.weight);
- }
+ WeightModel *mymodel = qobject_cast<WeightModel *>(currCombo.model);
+ if (hint == QAbstractItemDelegate::RevertModelCache)
+ mymodel->clearTempWS();
+ else
+ mymodel->commitTempWS();
}
void WSInfoDelegate::setModelData(QWidget*, QAbstractItemModel*, const QModelIndex&) const
@@ -356,28 +348,15 @@ void WSInfoDelegate::setModelData(QWidget*, QAbstractItemModel*, const QModelInd
row = matches.first().row();
}
int grams = wsim->data(wsim->index(row, WSInfoModel::GR)).toInt();
- QVariant v = QString(currCombo.activeText);
- mymodel->setData(IDX(WeightModel::TYPE), v, Qt::EditRole);
- mymodel->passInData(IDX(WeightModel::WEIGHT), grams);
+ mymodel->setTempWS(currCombo.currRow, weightsystem_t{ { grams }, copy_qstring(currCombo.activeText) });
}
WSInfoDelegate::WSInfoDelegate(QObject *parent) : ComboBoxDelegate(WSInfoModel::instance(), parent, true)
{
}
-QWidget *WSInfoDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
-{
- /* First, call the combobox-create editor, it will setup our globals. */
- QWidget *editor = ComboBoxDelegate::createEditor(parent, option, index);
- WeightModel *mymodel = qobject_cast<WeightModel *>(currCombo.model);
- weightsystem_t ws = mymodel->weightSystemAt(index);
- currWeight.type = ws.description;
- currWeight.weight = ws.weight.grams;
- return editor;
-}
-
-void AirTypesDelegate::revertModelData(QWidget*, QAbstractItemDelegate::EndEditHint)
+void AirTypesDelegate::editorClosed(QWidget*, QAbstractItemDelegate::EndEditHint)
{
}
@@ -393,7 +372,7 @@ AirTypesDelegate::AirTypesDelegate(QObject *parent) : ComboBoxDelegate(GasSelect
{
}
-void DiveTypesDelegate::revertModelData(QWidget*, QAbstractItemDelegate::EndEditHint)
+void DiveTypesDelegate::editorClosed(QWidget*, QAbstractItemDelegate::EndEditHint)
{
}