From 19baae449d7706a948c9944996f2a4be37e2d2c4 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sat, 17 Sep 2022 16:55:44 +0200 Subject: [PATCH] tab-widgets: pass current dive computer to delegates Don't access the global current_dc, but pass it to the sensor and tank-use delegates, when the current dive or dive computer changes. The same pattern is already realized for the tank and weight models. Signed-off-by: Berthold Stoeger --- desktop-widgets/diveplanner.cpp | 25 +++++++------- desktop-widgets/diveplanner.h | 5 +-- desktop-widgets/mainwindow.cpp | 7 ++-- desktop-widgets/modeldelegates.cpp | 34 +++++++++++-------- desktop-widgets/modeldelegates.h | 6 ++++ .../tab-widgets/TabDiveEquipment.cpp | 4 +++ 6 files changed, 48 insertions(+), 33 deletions(-) diff --git a/desktop-widgets/diveplanner.cpp b/desktop-widgets/diveplanner.cpp index 2fda1f3ac..93f6f2bde 100644 --- a/desktop-widgets/diveplanner.cpp +++ b/desktop-widgets/diveplanner.cpp @@ -51,7 +51,9 @@ DivePlannerWidget::DivePlannerWidget(QWidget *parent) : QWidget(parent, QFlag(0) view->setColumnHidden(CylindersModel::SIZE_INT, true); view->setColumnHidden(CylindersModel::SENSORS, true); view->setItemDelegateForColumn(CylindersModel::TYPE, new TankInfoDelegate(this)); - view->setItemDelegateForColumn(CylindersModel::USE, new TankUseDelegate(this)); + auto tankUseDelegate = new TankUseDelegate(this); + tankUseDelegate->setCurrentDC(get_dive_dc(&displayed_dive, 0)); + view->setItemDelegateForColumn(CylindersModel::USE, tankUseDelegate); connect(ui.cylinderTableWidget, &TableView::addButtonClicked, plannerModel, &DivePlannerPointsModel::addCylinder_clicked); connect(ui.tableWidget, &TableView::addButtonClicked, plannerModel, &DivePlannerPointsModel::addDefaultStop); connect(cylinders, &CylindersModel::dataChanged, GasSelectionModel::instance(), &GasSelectionModel::repopulate); @@ -543,7 +545,7 @@ PlannerWidgets::PlannerWidgets() &plannerDetails, &PlannerDetails::setPlanNotes); } -void PlannerWidgets::planDive() +void PlannerWidgets::planDive(dive *currentDive) { DivePlannerPointsModel::instance()->setPlanMode(DivePlannerPointsModel::PLAN); dc_number = 0; @@ -552,11 +554,11 @@ void PlannerWidgets::planDive() DivePlannerPointsModel::instance()->createSimpleDive(&displayed_dive); // plan the dive in the same mode as the currently selected one - if (current_dive) { - plannerSettingsWidget.setDiveMode(current_dive->dc.divemode); - plannerSettingsWidget.setBailoutVisibility(current_dive->dc.divemode); - if (current_dive->salinity) - plannerWidget.setSalinity(current_dive->salinity); + if (currentDive) { + plannerSettingsWidget.setDiveMode(currentDive->dc.divemode); + plannerSettingsWidget.setBailoutVisibility(currentDive->dc.divemode); + if (currentDive->salinity) + plannerWidget.setSalinity(currentDive->salinity); else // No salinity means salt water plannerWidget.setSalinity(SEAWATER_SALINITY); } @@ -565,13 +567,10 @@ void PlannerWidgets::planDive() plannerWidget.setupStartTime(timestampToDateTime(displayed_dive.when)); // This will reload the profile! } -void PlannerWidgets::replanDive() +void PlannerWidgets::replanDive(int currentDC) { - if (!current_dive) - return; - copy_dive(current_dive, &displayed_dive); // Planning works on a copy of the dive (for now). DivePlannerPointsModel::instance()->setPlanMode(DivePlannerPointsModel::PLAN); - DivePlannerPointsModel::instance()->loadFromDive(&displayed_dive, dc_number); + DivePlannerPointsModel::instance()->loadFromDive(&displayed_dive, currentDC); plannerWidget.setReplanButton(true); plannerWidget.setupStartTime(timestampToDateTime(displayed_dive.when)); @@ -580,7 +579,7 @@ void PlannerWidgets::replanDive() if (displayed_dive.salinity) plannerWidget.setSalinity(displayed_dive.salinity); reset_cylinders(&displayed_dive, true); - DivePlannerPointsModel::instance()->cylindersModel()->updateDive(&displayed_dive, dc_number); + DivePlannerPointsModel::instance()->cylindersModel()->updateDive(&displayed_dive, currentDC); } void PlannerWidgets::printDecoPlan() diff --git a/desktop-widgets/diveplanner.h b/desktop-widgets/diveplanner.h index 7f1857f6a..4f3dcbaab 100644 --- a/desktop-widgets/diveplanner.h +++ b/desktop-widgets/diveplanner.h @@ -9,6 +9,7 @@ class QListView; class QModelIndex; class DivePlannerPointsModel; +struct dive; #include "ui_diveplanner.h" @@ -75,8 +76,8 @@ class PlannerWidgets : public QObject { Q_OBJECT public: PlannerWidgets(); - void planDive(); - void replanDive(); + void planDive(dive *currentDive); + void replanDive(int currentDC); public slots: void printDecoPlan(); diff --git a/desktop-widgets/mainwindow.cpp b/desktop-widgets/mainwindow.cpp index 0430e2679..5af0b8c5b 100644 --- a/desktop-widgets/mainwindow.cpp +++ b/desktop-widgets/mainwindow.cpp @@ -664,8 +664,9 @@ void MainWindow::on_actionReplanDive_triggered() setApplicationState(ApplicationState::PlanDive); disableShortcuts(true); - profile->setPlanState(&displayed_dive, 0); - plannerWidgets->replanDive(); + copy_dive(current_dive, &displayed_dive); // Planning works on a copy of the dive (for now). + profile->setPlanState(&displayed_dive, dc_number); + plannerWidgets->replanDive(dc_number); } void MainWindow::on_actionDivePlanner_triggered() @@ -678,7 +679,7 @@ void MainWindow::on_actionDivePlanner_triggered() disableShortcuts(true); profile->setPlanState(&displayed_dive, 0); - plannerWidgets->planDive(); + plannerWidgets->planDive(current_dive); } void MainWindow::on_actionAddDive_triggered() diff --git a/desktop-widgets/modeldelegates.cpp b/desktop-widgets/modeldelegates.cpp index e719e7372..4e9502cf3 100644 --- a/desktop-widgets/modeldelegates.cpp +++ b/desktop-widgets/modeldelegates.cpp @@ -250,27 +250,24 @@ void TankInfoDelegate::editorClosed(QWidget *, QAbstractItemDelegate::EndEditHin mymodel->setData(IDX(CylindersModel::TYPE), currCombo.activeText, CylindersModel::COMMIT_ROLE); } -TankUseDelegate::TankUseDelegate(QObject *parent) : QStyledItemDelegate(parent) +TankUseDelegate::TankUseDelegate(QObject *parent) : QStyledItemDelegate(parent), currentdc(nullptr) { } +void TankUseDelegate::setCurrentDC(divecomputer *dc) +{ + currentdc = dc; +} + QWidget *TankUseDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const { - struct divecomputer *currentDc; - if (DivePlannerPointsModel::instance()->currentMode() != DivePlannerPointsModel::NOTHING) { - currentDc = &displayed_dive.dc; - } else { - currentDc = get_dive_dc(current_dive, dc_number); - } QComboBox *comboBox = new QComboBox(parent); - if (!currentDc) { + if (!currentdc) return comboBox; - } - bool isCcrDive = currentDc->divemode == CCR; + bool isCcrDive = currentdc->divemode == CCR; for (int i = 0; i < NUM_GAS_USE; i++) { - if (isCcrDive || (i != DILUENT && i != OXYGEN)) { + if (isCcrDive || (i != DILUENT && i != OXYGEN)) comboBox->addItem(gettextFromC::tr(cylinderuse_text[i])); - } } return comboBox; } @@ -288,16 +285,23 @@ void TankUseDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, c model->setData(index, cylinderuse_from_text(qPrintable(comboBox->currentText()))); } - -SensorDelegate::SensorDelegate(QObject *parent) : QStyledItemDelegate(parent) +SensorDelegate::SensorDelegate(QObject *parent) : QStyledItemDelegate(parent), currentdc(nullptr) { } +void SensorDelegate::setCurrentDC(divecomputer *dc) +{ + currentdc = dc; +} + QWidget *SensorDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index) const { QComboBox *comboBox = new QComboBox(parent); + + if (!currentdc) + return comboBox; + std::vector sensors; - const struct divecomputer *currentdc = get_dive_dc(current_dive, dc_number); for (int i = 0; i < currentdc->samples; ++i) { auto &sample = currentdc->sample[i]; for (int s = 0; s < MAX_SENSORS; ++s) { diff --git a/desktop-widgets/modeldelegates.h b/desktop-widgets/modeldelegates.h index 7d76b6b9b..ebededd25 100644 --- a/desktop-widgets/modeldelegates.h +++ b/desktop-widgets/modeldelegates.h @@ -6,7 +6,9 @@ #include #include + class QPainter; +struct divecomputer; class DiveListDelegate : public QStyledItemDelegate { public: @@ -73,19 +75,23 @@ class TankUseDelegate : public QStyledItemDelegate { Q_OBJECT public: explicit TankUseDelegate(QObject *parent = 0); + void setCurrentDC(divecomputer *dc); private: void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override; QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override; void setEditorData(QWidget *editor, const QModelIndex &index) const override; + divecomputer *currentdc; }; class SensorDelegate : public QStyledItemDelegate { Q_OBJECT public: explicit SensorDelegate(QObject *parent = 0); + void setCurrentDC(divecomputer *dc); private: void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override; QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override; + divecomputer *currentdc; }; class WSInfoDelegate : public ComboBoxDelegate { diff --git a/desktop-widgets/tab-widgets/TabDiveEquipment.cpp b/desktop-widgets/tab-widgets/TabDiveEquipment.cpp index ef739c131..94eaf83bf 100644 --- a/desktop-widgets/tab-widgets/TabDiveEquipment.cpp +++ b/desktop-widgets/tab-widgets/TabDiveEquipment.cpp @@ -138,8 +138,12 @@ void TabDiveEquipment::toggleTriggeredColumn() void TabDiveEquipment::updateData(const std::vector &, dive *currentDive, int currentDC) { + divecomputer *dc = get_dive_dc(currentDive, currentDC); + cylindersModel->updateDive(currentDive, currentDC); weightModel->updateDive(currentDive); + sensorDelegate.setCurrentDC(dc); + tankUseDelegate.setCurrentDC(dc); if (currentDive && currentDive->suit) ui.suit->setText(QString(currentDive->suit));