From b5682369f89796a94e47e3a63e73195daf4e3679 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Mon, 7 Nov 2022 19:50:37 +0100 Subject: [PATCH] planner: remove global model instances The only user of the DivePlannerPointsModel and the GasSelectionModel is the planner. Let's keep these models there. Signed-off-by: Berthold Stoeger --- desktop-widgets/diveplanner.cpp | 30 ++++++++++++++++++++---------- desktop-widgets/diveplanner.h | 10 +++++++++- desktop-widgets/modeldelegates.cpp | 4 ++-- desktop-widgets/modeldelegates.h | 4 ++-- qt-models/models.cpp | 12 ------------ qt-models/models.h | 2 -- 6 files changed, 33 insertions(+), 29 deletions(-) diff --git a/desktop-widgets/diveplanner.cpp b/desktop-widgets/diveplanner.cpp index 7c65e2c28..21c65b9ec 100644 --- a/desktop-widgets/diveplanner.cpp +++ b/desktop-widgets/diveplanner.cpp @@ -23,7 +23,7 @@ #include #endif -DivePlannerWidget::DivePlannerWidget(QWidget *parent) : QWidget(parent, QFlag(0)) +DivePlannerWidget::DivePlannerWidget(PlannerWidgets *parent) { DivePlannerPointsModel *plannerModel = DivePlannerPointsModel::instance(); CylindersModel *cylinders = DivePlannerPointsModel::instance()->cylindersModel(); @@ -32,8 +32,8 @@ DivePlannerWidget::DivePlannerWidget(QWidget *parent) : QWidget(parent, QFlag(0) ui.tableWidget->setTitle(tr("Dive planner points")); ui.tableWidget->setModel(plannerModel); connect(ui.tableWidget, &TableView::itemClicked, plannerModel, &DivePlannerPointsModel::remove); - ui.tableWidget->view()->setItemDelegateForColumn(DivePlannerPointsModel::GAS, new AirTypesDelegate(this)); - ui.tableWidget->view()->setItemDelegateForColumn(DivePlannerPointsModel::DIVEMODE, new DiveTypesDelegate(this)); + ui.tableWidget->view()->setItemDelegateForColumn(DivePlannerPointsModel::GAS, new AirTypesDelegate(parent->gasModel.get(), this)); + ui.tableWidget->view()->setItemDelegateForColumn(DivePlannerPointsModel::DIVEMODE, new DiveTypesDelegate(parent->diveTypeModel.get(), this)); ui.cylinderTableWidget->setTitle(tr("Available gases")); ui.cylinderTableWidget->setBtnToolTip(tr("Add cylinder")); ui.cylinderTableWidget->setModel(cylinders); @@ -56,9 +56,9 @@ DivePlannerWidget::DivePlannerWidget(QWidget *parent) : QWidget(parent, QFlag(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); - connect(cylinders, &CylindersModel::rowsInserted, GasSelectionModel::instance(), &GasSelectionModel::repopulate); - connect(cylinders, &CylindersModel::rowsRemoved, GasSelectionModel::instance(), &GasSelectionModel::repopulate); + connect(cylinders, &CylindersModel::dataChanged, parent->gasModel.get(), &GasSelectionModel::repopulate); + connect(cylinders, &CylindersModel::rowsInserted, parent->gasModel.get(), &GasSelectionModel::repopulate); + connect(cylinders, &CylindersModel::rowsRemoved, parent->gasModel.get(), &GasSelectionModel::repopulate); connect(cylinders, &CylindersModel::dataChanged, plannerModel, &DivePlannerPointsModel::emitDataChanged); connect(cylinders, &CylindersModel::dataChanged, plannerModel, &DivePlannerPointsModel::cylinderModelEdited); connect(cylinders, &CylindersModel::rowsInserted, plannerModel, &DivePlannerPointsModel::cylinderModelEdited); @@ -99,6 +99,10 @@ DivePlannerWidget::DivePlannerWidget(QWidget *parent) : QWidget(parent, QFlag(0) setMinimumHeight(0); } +DivePlannerWidget::~DivePlannerWidget() +{ +} + void DivePlannerWidget::setReplanButton(bool replan) { replanButton->setVisible(replan); @@ -537,13 +541,19 @@ void PlannerDetails::setPlanNotes(QString plan) ui.divePlanOutput->setHtml(plan); } -PlannerWidgets::PlannerWidgets() +PlannerWidgets::PlannerWidgets() : plannerWidget(this) { + gasModel = std::make_unique(); + diveTypeModel = std::make_unique(); connect(plannerDetails.printPlan(), &QPushButton::pressed, this, &PlannerWidgets::printDecoPlan); connect(DivePlannerPointsModel::instance(), &DivePlannerPointsModel::calculatedPlanNotes, &plannerDetails, &PlannerDetails::setPlanNotes); } +PlannerWidgets::~PlannerWidgets() +{ +} + void PlannerWidgets::planDive(dive *currentDive) { DivePlannerPointsModel::instance()->setPlanMode(DivePlannerPointsModel::PLAN); @@ -560,8 +570,8 @@ void PlannerWidgets::planDive(dive *currentDive) else // No salinity means salt water plannerWidget.setSalinity(SEAWATER_SALINITY); } - GasSelectionModel::instance()->repopulate(); - DiveTypeSelectionModel::instance()->repopulate(); + gasModel->repopulate(); + diveTypeModel->repopulate(); // TODO: this doesn't change anything!? plannerWidget.setReplanButton(false); plannerWidget.setupStartTime(timestampToDateTime(displayed_dive.when)); // This will reload the profile! @@ -572,7 +582,7 @@ void PlannerWidgets::replanDive(int currentDC) DivePlannerPointsModel::instance()->setPlanMode(DivePlannerPointsModel::PLAN); DivePlannerPointsModel::instance()->loadFromDive(&displayed_dive, currentDC); - DiveTypeSelectionModel::instance()->repopulate(); + diveTypeModel->repopulate(); // TODO: this doesn't change anything!? plannerWidget.setReplanButton(true); plannerWidget.setupStartTime(timestampToDateTime(displayed_dive.when)); if (displayed_dive.surface_pressure.mbar) diff --git a/desktop-widgets/diveplanner.h b/desktop-widgets/diveplanner.h index 4f3dcbaab..7f144ea5b 100644 --- a/desktop-widgets/diveplanner.h +++ b/desktop-widgets/diveplanner.h @@ -2,6 +2,7 @@ #ifndef DIVEPLANNER_H #define DIVEPLANNER_H +#include #include #include #include @@ -9,6 +10,9 @@ class QListView; class QModelIndex; class DivePlannerPointsModel; +class GasSelectionModel; +class DiveTypeSelectionModel; +class PlannerWidgets; struct dive; #include "ui_diveplanner.h" @@ -16,7 +20,8 @@ struct dive; class DivePlannerWidget : public QWidget { Q_OBJECT public: - explicit DivePlannerWidget(QWidget *parent = 0); + explicit DivePlannerWidget(PlannerWidgets *parent); + ~DivePlannerWidget(); void setReplanButton(bool replan); public slots: @@ -76,6 +81,7 @@ class PlannerWidgets : public QObject { Q_OBJECT public: PlannerWidgets(); + ~PlannerWidgets(); void planDive(dive *currentDive); void replanDive(int currentDC); public @@ -85,6 +91,8 @@ public: DivePlannerWidget plannerWidget; PlannerSettingsWidget plannerSettingsWidget; PlannerDetails plannerDetails; + std::unique_ptr gasModel; + std::unique_ptr diveTypeModel; }; #endif // DIVEPLANNER_H diff --git a/desktop-widgets/modeldelegates.cpp b/desktop-widgets/modeldelegates.cpp index 4e9502cf3..070f7d7ab 100644 --- a/desktop-widgets/modeldelegates.cpp +++ b/desktop-widgets/modeldelegates.cpp @@ -369,7 +369,7 @@ void AirTypesDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, model->setData(index, QVariant(combo->currentIndex())); } -AirTypesDelegate::AirTypesDelegate(QObject *parent) : ComboBoxDelegate(GasSelectionModel::instance(), parent, false) +AirTypesDelegate::AirTypesDelegate(QAbstractItemModel *model, QObject *parent) : ComboBoxDelegate(model, parent, false) { } @@ -385,7 +385,7 @@ void DiveTypesDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, model->setData(index, QVariant(combo->currentIndex())); } -DiveTypesDelegate::DiveTypesDelegate(QObject *parent) : ComboBoxDelegate(DiveTypeSelectionModel::instance(), parent, false) +DiveTypesDelegate::DiveTypesDelegate(QAbstractItemModel *model, QObject *parent) : ComboBoxDelegate(model, parent, false) { } diff --git a/desktop-widgets/modeldelegates.h b/desktop-widgets/modeldelegates.h index ebededd25..93307e48b 100644 --- a/desktop-widgets/modeldelegates.h +++ b/desktop-widgets/modeldelegates.h @@ -106,7 +106,7 @@ private: class AirTypesDelegate : public ComboBoxDelegate { Q_OBJECT public: - explicit AirTypesDelegate(QObject *parent = 0); + explicit AirTypesDelegate(QAbstractItemModel *model, QObject *parent = 0); private: void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override; void editorClosed(QWidget *widget, QAbstractItemDelegate::EndEditHint hint) override; @@ -115,7 +115,7 @@ private: class DiveTypesDelegate : public ComboBoxDelegate { Q_OBJECT public: - explicit DiveTypesDelegate(QObject *parent = 0); + explicit DiveTypesDelegate(QAbstractItemModel *model, QObject *parent = 0); private: void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override; void editorClosed(QWidget *widget, QAbstractItemDelegate::EndEditHint hint) override; diff --git a/qt-models/models.cpp b/qt-models/models.cpp index 314d73aec..bb1c7ff51 100644 --- a/qt-models/models.cpp +++ b/qt-models/models.cpp @@ -18,12 +18,6 @@ Qt::ItemFlags GasSelectionModel::flags(const QModelIndex&) const return Qt::ItemIsEnabled | Qt::ItemIsSelectable; } -GasSelectionModel *GasSelectionModel::instance() -{ - static GasSelectionModel self; - return &self; -} - void GasSelectionModel::repopulate() { setStringList(get_dive_gas_list(&displayed_dive)); @@ -43,12 +37,6 @@ Qt::ItemFlags DiveTypeSelectionModel::flags(const QModelIndex&) const return Qt::ItemIsEnabled | Qt::ItemIsSelectable; } -DiveTypeSelectionModel *DiveTypeSelectionModel::instance() -{ - static DiveTypeSelectionModel self; - return &self; -} - void DiveTypeSelectionModel::repopulate() { QStringList modes; diff --git a/qt-models/models.h b/qt-models/models.h index 012b1f8b6..402d856b4 100644 --- a/qt-models/models.h +++ b/qt-models/models.h @@ -22,7 +22,6 @@ class GasSelectionModel : public QStringListModel { Q_OBJECT public: - static GasSelectionModel *instance(); Qt::ItemFlags flags(const QModelIndex &index) const; QVariant data(const QModelIndex &index, int role) const override; public @@ -33,7 +32,6 @@ slots: class DiveTypeSelectionModel : public QStringListModel { Q_OBJECT public: - static DiveTypeSelectionModel *instance(); Qt::ItemFlags flags(const QModelIndex &index) const; QVariant data(const QModelIndex &index, int role) const override; public