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 <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2022-11-07 19:50:37 +01:00 committed by bstoeger
parent 3dd09b31e3
commit b5682369f8
6 changed files with 33 additions and 29 deletions

View file

@ -23,7 +23,7 @@
#include <QBuffer> #include <QBuffer>
#endif #endif
DivePlannerWidget::DivePlannerWidget(QWidget *parent) : QWidget(parent, QFlag(0)) DivePlannerWidget::DivePlannerWidget(PlannerWidgets *parent)
{ {
DivePlannerPointsModel *plannerModel = DivePlannerPointsModel::instance(); DivePlannerPointsModel *plannerModel = DivePlannerPointsModel::instance();
CylindersModel *cylinders = DivePlannerPointsModel::instance()->cylindersModel(); 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->setTitle(tr("Dive planner points"));
ui.tableWidget->setModel(plannerModel); ui.tableWidget->setModel(plannerModel);
connect(ui.tableWidget, &TableView::itemClicked, plannerModel, &DivePlannerPointsModel::remove); connect(ui.tableWidget, &TableView::itemClicked, plannerModel, &DivePlannerPointsModel::remove);
ui.tableWidget->view()->setItemDelegateForColumn(DivePlannerPointsModel::GAS, new AirTypesDelegate(this)); ui.tableWidget->view()->setItemDelegateForColumn(DivePlannerPointsModel::GAS, new AirTypesDelegate(parent->gasModel.get(), this));
ui.tableWidget->view()->setItemDelegateForColumn(DivePlannerPointsModel::DIVEMODE, new DiveTypesDelegate(this)); ui.tableWidget->view()->setItemDelegateForColumn(DivePlannerPointsModel::DIVEMODE, new DiveTypesDelegate(parent->diveTypeModel.get(), this));
ui.cylinderTableWidget->setTitle(tr("Available gases")); ui.cylinderTableWidget->setTitle(tr("Available gases"));
ui.cylinderTableWidget->setBtnToolTip(tr("Add cylinder")); ui.cylinderTableWidget->setBtnToolTip(tr("Add cylinder"));
ui.cylinderTableWidget->setModel(cylinders); ui.cylinderTableWidget->setModel(cylinders);
@ -56,9 +56,9 @@ DivePlannerWidget::DivePlannerWidget(QWidget *parent) : QWidget(parent, QFlag(0)
view->setItemDelegateForColumn(CylindersModel::USE, tankUseDelegate); view->setItemDelegateForColumn(CylindersModel::USE, tankUseDelegate);
connect(ui.cylinderTableWidget, &TableView::addButtonClicked, plannerModel, &DivePlannerPointsModel::addCylinder_clicked); connect(ui.cylinderTableWidget, &TableView::addButtonClicked, plannerModel, &DivePlannerPointsModel::addCylinder_clicked);
connect(ui.tableWidget, &TableView::addButtonClicked, plannerModel, &DivePlannerPointsModel::addDefaultStop); connect(ui.tableWidget, &TableView::addButtonClicked, plannerModel, &DivePlannerPointsModel::addDefaultStop);
connect(cylinders, &CylindersModel::dataChanged, GasSelectionModel::instance(), &GasSelectionModel::repopulate); connect(cylinders, &CylindersModel::dataChanged, parent->gasModel.get(), &GasSelectionModel::repopulate);
connect(cylinders, &CylindersModel::rowsInserted, GasSelectionModel::instance(), &GasSelectionModel::repopulate); connect(cylinders, &CylindersModel::rowsInserted, parent->gasModel.get(), &GasSelectionModel::repopulate);
connect(cylinders, &CylindersModel::rowsRemoved, GasSelectionModel::instance(), &GasSelectionModel::repopulate); connect(cylinders, &CylindersModel::rowsRemoved, parent->gasModel.get(), &GasSelectionModel::repopulate);
connect(cylinders, &CylindersModel::dataChanged, plannerModel, &DivePlannerPointsModel::emitDataChanged); connect(cylinders, &CylindersModel::dataChanged, plannerModel, &DivePlannerPointsModel::emitDataChanged);
connect(cylinders, &CylindersModel::dataChanged, plannerModel, &DivePlannerPointsModel::cylinderModelEdited); connect(cylinders, &CylindersModel::dataChanged, plannerModel, &DivePlannerPointsModel::cylinderModelEdited);
connect(cylinders, &CylindersModel::rowsInserted, plannerModel, &DivePlannerPointsModel::cylinderModelEdited); connect(cylinders, &CylindersModel::rowsInserted, plannerModel, &DivePlannerPointsModel::cylinderModelEdited);
@ -99,6 +99,10 @@ DivePlannerWidget::DivePlannerWidget(QWidget *parent) : QWidget(parent, QFlag(0)
setMinimumHeight(0); setMinimumHeight(0);
} }
DivePlannerWidget::~DivePlannerWidget()
{
}
void DivePlannerWidget::setReplanButton(bool replan) void DivePlannerWidget::setReplanButton(bool replan)
{ {
replanButton->setVisible(replan); replanButton->setVisible(replan);
@ -537,13 +541,19 @@ void PlannerDetails::setPlanNotes(QString plan)
ui.divePlanOutput->setHtml(plan); ui.divePlanOutput->setHtml(plan);
} }
PlannerWidgets::PlannerWidgets() PlannerWidgets::PlannerWidgets() : plannerWidget(this)
{ {
gasModel = std::make_unique<GasSelectionModel>();
diveTypeModel = std::make_unique<DiveTypeSelectionModel>();
connect(plannerDetails.printPlan(), &QPushButton::pressed, this, &PlannerWidgets::printDecoPlan); connect(plannerDetails.printPlan(), &QPushButton::pressed, this, &PlannerWidgets::printDecoPlan);
connect(DivePlannerPointsModel::instance(), &DivePlannerPointsModel::calculatedPlanNotes, connect(DivePlannerPointsModel::instance(), &DivePlannerPointsModel::calculatedPlanNotes,
&plannerDetails, &PlannerDetails::setPlanNotes); &plannerDetails, &PlannerDetails::setPlanNotes);
} }
PlannerWidgets::~PlannerWidgets()
{
}
void PlannerWidgets::planDive(dive *currentDive) void PlannerWidgets::planDive(dive *currentDive)
{ {
DivePlannerPointsModel::instance()->setPlanMode(DivePlannerPointsModel::PLAN); DivePlannerPointsModel::instance()->setPlanMode(DivePlannerPointsModel::PLAN);
@ -560,8 +570,8 @@ void PlannerWidgets::planDive(dive *currentDive)
else // No salinity means salt water else // No salinity means salt water
plannerWidget.setSalinity(SEAWATER_SALINITY); plannerWidget.setSalinity(SEAWATER_SALINITY);
} }
GasSelectionModel::instance()->repopulate(); gasModel->repopulate();
DiveTypeSelectionModel::instance()->repopulate(); diveTypeModel->repopulate(); // TODO: this doesn't change anything!?
plannerWidget.setReplanButton(false); plannerWidget.setReplanButton(false);
plannerWidget.setupStartTime(timestampToDateTime(displayed_dive.when)); // This will reload the profile! 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()->setPlanMode(DivePlannerPointsModel::PLAN);
DivePlannerPointsModel::instance()->loadFromDive(&displayed_dive, currentDC); DivePlannerPointsModel::instance()->loadFromDive(&displayed_dive, currentDC);
DiveTypeSelectionModel::instance()->repopulate(); diveTypeModel->repopulate(); // TODO: this doesn't change anything!?
plannerWidget.setReplanButton(true); plannerWidget.setReplanButton(true);
plannerWidget.setupStartTime(timestampToDateTime(displayed_dive.when)); plannerWidget.setupStartTime(timestampToDateTime(displayed_dive.when));
if (displayed_dive.surface_pressure.mbar) if (displayed_dive.surface_pressure.mbar)

View file

@ -2,6 +2,7 @@
#ifndef DIVEPLANNER_H #ifndef DIVEPLANNER_H
#define DIVEPLANNER_H #define DIVEPLANNER_H
#include <memory>
#include <QAbstractTableModel> #include <QAbstractTableModel>
#include <QAbstractButton> #include <QAbstractButton>
#include <QDateTime> #include <QDateTime>
@ -9,6 +10,9 @@
class QListView; class QListView;
class QModelIndex; class QModelIndex;
class DivePlannerPointsModel; class DivePlannerPointsModel;
class GasSelectionModel;
class DiveTypeSelectionModel;
class PlannerWidgets;
struct dive; struct dive;
#include "ui_diveplanner.h" #include "ui_diveplanner.h"
@ -16,7 +20,8 @@ struct dive;
class DivePlannerWidget : public QWidget { class DivePlannerWidget : public QWidget {
Q_OBJECT Q_OBJECT
public: public:
explicit DivePlannerWidget(QWidget *parent = 0); explicit DivePlannerWidget(PlannerWidgets *parent);
~DivePlannerWidget();
void setReplanButton(bool replan); void setReplanButton(bool replan);
public public
slots: slots:
@ -76,6 +81,7 @@ class PlannerWidgets : public QObject {
Q_OBJECT Q_OBJECT
public: public:
PlannerWidgets(); PlannerWidgets();
~PlannerWidgets();
void planDive(dive *currentDive); void planDive(dive *currentDive);
void replanDive(int currentDC); void replanDive(int currentDC);
public public
@ -85,6 +91,8 @@ public:
DivePlannerWidget plannerWidget; DivePlannerWidget plannerWidget;
PlannerSettingsWidget plannerSettingsWidget; PlannerSettingsWidget plannerSettingsWidget;
PlannerDetails plannerDetails; PlannerDetails plannerDetails;
std::unique_ptr<GasSelectionModel> gasModel;
std::unique_ptr<DiveTypeSelectionModel> diveTypeModel;
}; };
#endif // DIVEPLANNER_H #endif // DIVEPLANNER_H

View file

@ -369,7 +369,7 @@ void AirTypesDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
model->setData(index, QVariant(combo->currentIndex())); 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())); 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)
{ {
} }

View file

@ -106,7 +106,7 @@ private:
class AirTypesDelegate : public ComboBoxDelegate { class AirTypesDelegate : public ComboBoxDelegate {
Q_OBJECT Q_OBJECT
public: public:
explicit AirTypesDelegate(QObject *parent = 0); explicit AirTypesDelegate(QAbstractItemModel *model, QObject *parent = 0);
private: private:
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override; void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
void editorClosed(QWidget *widget, QAbstractItemDelegate::EndEditHint hint) override; void editorClosed(QWidget *widget, QAbstractItemDelegate::EndEditHint hint) override;
@ -115,7 +115,7 @@ private:
class DiveTypesDelegate : public ComboBoxDelegate { class DiveTypesDelegate : public ComboBoxDelegate {
Q_OBJECT Q_OBJECT
public: public:
explicit DiveTypesDelegate(QObject *parent = 0); explicit DiveTypesDelegate(QAbstractItemModel *model, QObject *parent = 0);
private: private:
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override; void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
void editorClosed(QWidget *widget, QAbstractItemDelegate::EndEditHint hint) override; void editorClosed(QWidget *widget, QAbstractItemDelegate::EndEditHint hint) override;

View file

@ -18,12 +18,6 @@ Qt::ItemFlags GasSelectionModel::flags(const QModelIndex&) const
return Qt::ItemIsEnabled | Qt::ItemIsSelectable; return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
} }
GasSelectionModel *GasSelectionModel::instance()
{
static GasSelectionModel self;
return &self;
}
void GasSelectionModel::repopulate() void GasSelectionModel::repopulate()
{ {
setStringList(get_dive_gas_list(&displayed_dive)); setStringList(get_dive_gas_list(&displayed_dive));
@ -43,12 +37,6 @@ Qt::ItemFlags DiveTypeSelectionModel::flags(const QModelIndex&) const
return Qt::ItemIsEnabled | Qt::ItemIsSelectable; return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
} }
DiveTypeSelectionModel *DiveTypeSelectionModel::instance()
{
static DiveTypeSelectionModel self;
return &self;
}
void DiveTypeSelectionModel::repopulate() void DiveTypeSelectionModel::repopulate()
{ {
QStringList modes; QStringList modes;

View file

@ -22,7 +22,6 @@
class GasSelectionModel : public QStringListModel { class GasSelectionModel : public QStringListModel {
Q_OBJECT Q_OBJECT
public: public:
static GasSelectionModel *instance();
Qt::ItemFlags flags(const QModelIndex &index) const; Qt::ItemFlags flags(const QModelIndex &index) const;
QVariant data(const QModelIndex &index, int role) const override; QVariant data(const QModelIndex &index, int role) const override;
public public
@ -33,7 +32,6 @@ slots:
class DiveTypeSelectionModel : public QStringListModel { class DiveTypeSelectionModel : public QStringListModel {
Q_OBJECT Q_OBJECT
public: public:
static DiveTypeSelectionModel *instance();
Qt::ItemFlags flags(const QModelIndex &index) const; Qt::ItemFlags flags(const QModelIndex &index) const;
QVariant data(const QModelIndex &index, int role) const override; QVariant data(const QModelIndex &index, int role) const override;
public public