planner: get rid of global displayed_dive variable

Allocate the dive in the planner. This is all a bit convoluted
and needs more cleanup.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2022-11-05 20:27:49 +01:00 committed by bstoeger
parent b5682369f8
commit 4c02d1c279
9 changed files with 251 additions and 233 deletions

View file

@ -23,7 +23,7 @@
#include <QBuffer>
#endif
DivePlannerWidget::DivePlannerWidget(PlannerWidgets *parent)
DivePlannerWidget::DivePlannerWidget(dive &planned_dive, PlannerWidgets *parent)
{
DivePlannerPointsModel *plannerModel = DivePlannerPointsModel::instance();
CylindersModel *cylinders = DivePlannerPointsModel::instance()->cylindersModel();
@ -52,13 +52,13 @@ DivePlannerWidget::DivePlannerWidget(PlannerWidgets *parent)
view->setColumnHidden(CylindersModel::SENSORS, true);
view->setItemDelegateForColumn(CylindersModel::TYPE, new TankInfoDelegate(this));
auto tankUseDelegate = new TankUseDelegate(this);
tankUseDelegate->setCurrentDC(get_dive_dc(&displayed_dive, 0));
tankUseDelegate->setCurrentDC(get_dive_dc(&planned_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, 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, parent, &PlannerWidgets::repopulateGasModel);
connect(cylinders, &CylindersModel::rowsInserted, parent, &PlannerWidgets::repopulateGasModel);
connect(cylinders, &CylindersModel::rowsRemoved, parent, &PlannerWidgets::repopulateGasModel);
connect(cylinders, &CylindersModel::dataChanged, plannerModel, &DivePlannerPointsModel::emitDataChanged);
connect(cylinders, &CylindersModel::dataChanged, plannerModel, &DivePlannerPointsModel::cylinderModelEdited);
connect(cylinders, &CylindersModel::rowsInserted, plannerModel, &DivePlannerPointsModel::cylinderModelEdited);
@ -524,8 +524,8 @@ void PlannerSettingsWidget::setBackgasBreaks(bool dobreaks)
void PlannerSettingsWidget::setBailoutVisibility(int mode)
{
ui.bailout->setDisabled(!(mode == CCR || mode == PSCR));
ui.sacFactor->setDisabled(mode == CCR);
ui.bailout->setDisabled(!(mode == CCR || mode == PSCR));
ui.sacFactor->setDisabled(mode == CCR);
}
PlannerDetails::PlannerDetails(QWidget *parent) : QWidget(parent)
@ -541,7 +541,9 @@ void PlannerDetails::setPlanNotes(QString plan)
ui.divePlanOutput->setHtml(plan);
}
PlannerWidgets::PlannerWidgets() : plannerWidget(this)
PlannerWidgets::PlannerWidgets() :
planned_dive(alloc_dive()),
plannerWidget(*planned_dive, this)
{
gasModel = std::make_unique<GasSelectionModel>();
diveTypeModel = std::make_unique<DiveTypeSelectionModel>();
@ -554,12 +556,15 @@ PlannerWidgets::~PlannerWidgets()
{
}
void PlannerWidgets::planDive(dive *currentDive)
struct dive *PlannerWidgets::getDive() const
{
DivePlannerPointsModel::instance()->setPlanMode(DivePlannerPointsModel::PLAN);
return planned_dive.get();
}
void PlannerWidgets::preparePlanDive(const dive *currentDive)
{
// create a simple starting dive, using the first gas from the just copied cylinders
DivePlannerPointsModel::instance()->createSimpleDive(&displayed_dive);
DivePlannerPointsModel::instance()->createSimpleDive(planned_dive.get());
// plan the dive in the same mode as the currently selected one
if (currentDive) {
@ -570,27 +575,42 @@ void PlannerWidgets::planDive(dive *currentDive)
else // No salinity means salt water
plannerWidget.setSalinity(SEAWATER_SALINITY);
}
gasModel->repopulate();
}
void PlannerWidgets::planDive()
{
DivePlannerPointsModel::instance()->setPlanMode(DivePlannerPointsModel::PLAN);
repopulateGasModel();
diveTypeModel->repopulate(); // TODO: this doesn't change anything!?
plannerWidget.setReplanButton(false);
plannerWidget.setupStartTime(timestampToDateTime(planned_dive->when)); // This will reload the profile!
}
plannerWidget.setupStartTime(timestampToDateTime(displayed_dive.when)); // This will reload the profile!
void PlannerWidgets::prepareReplanDive(const dive *d)
{
copy_dive(d, planned_dive.get());
}
void PlannerWidgets::replanDive(int currentDC)
{
DivePlannerPointsModel::instance()->setPlanMode(DivePlannerPointsModel::PLAN);
DivePlannerPointsModel::instance()->loadFromDive(&displayed_dive, currentDC);
DivePlannerPointsModel::instance()->loadFromDive(planned_dive.get(), currentDC);
diveTypeModel->repopulate(); // TODO: this doesn't change anything!?
plannerWidget.setReplanButton(true);
plannerWidget.setupStartTime(timestampToDateTime(displayed_dive.when));
if (displayed_dive.surface_pressure.mbar)
plannerWidget.setSurfacePressure(displayed_dive.surface_pressure.mbar);
if (displayed_dive.salinity)
plannerWidget.setSalinity(displayed_dive.salinity);
reset_cylinders(&displayed_dive, true);
DivePlannerPointsModel::instance()->cylindersModel()->updateDive(&displayed_dive, currentDC);
plannerWidget.setupStartTime(timestampToDateTime(planned_dive->when));
if (planned_dive->surface_pressure.mbar)
plannerWidget.setSurfacePressure(planned_dive->surface_pressure.mbar);
if (planned_dive->salinity)
plannerWidget.setSalinity(planned_dive->salinity);
reset_cylinders(planned_dive.get(), true);
DivePlannerPointsModel::instance()->cylindersModel()->updateDive(planned_dive.get(), currentDC);
}
void PlannerWidgets::repopulateGasModel()
{
gasModel->repopulate(planned_dive.get());
}
void PlannerWidgets::printDecoPlan()
@ -625,7 +645,7 @@ void PlannerWidgets::printDecoPlan()
painter.setRenderHint(QPainter::SmoothPixmapTransform);
auto profile = std::make_unique<ProfileScene>(1.0, true, false);
profile->draw(&painter, QRect(0, 0, pixmap.width(), pixmap.height()), &displayed_dive, 0, DivePlannerPointsModel::instance(), true);
profile->draw(&painter, QRect(0, 0, pixmap.width(), pixmap.height()), planned_dive.get(), 0, DivePlannerPointsModel::instance(), true);
QByteArray byteArray;
QBuffer buffer(&byteArray);

View file

@ -2,13 +2,12 @@
#ifndef DIVEPLANNER_H
#define DIVEPLANNER_H
#include <memory>
#include "core/owning_ptrs.h"
#include <QAbstractTableModel>
#include <QAbstractButton>
#include <QDateTime>
class QListView;
class QModelIndex;
class DivePlannerPointsModel;
class GasSelectionModel;
class DiveTypeSelectionModel;
@ -20,7 +19,7 @@ struct dive;
class DivePlannerWidget : public QWidget {
Q_OBJECT
public:
explicit DivePlannerWidget(PlannerWidgets *parent);
explicit DivePlannerWidget(dive &planned_dive, PlannerWidgets *parent);
~DivePlannerWidget();
void setReplanButton(bool replan);
public
@ -82,12 +81,17 @@ class PlannerWidgets : public QObject {
public:
PlannerWidgets();
~PlannerWidgets();
void planDive(dive *currentDive);
void preparePlanDive(const dive *currentDive); // Create a new planned dive
void planDive();
void prepareReplanDive(const dive *d); // Make a copy of the dive to be replanned
void replanDive(int currentDC);
struct dive *getDive() const;
public
slots:
void printDecoPlan();
public:
void repopulateGasModel();
OwningDivePtr planned_dive;
DivePlannerWidget plannerWidget;
PlannerSettingsWidget plannerSettingsWidget;
PlannerDetails plannerDetails;

View file

@ -549,7 +549,7 @@ void MainWindow::on_actionPrint_triggered()
{
#ifndef NO_PRINTING
// When in planner, only print the planned dive.
dive *singleDive = appState == ApplicationState::PlanDive ? &displayed_dive
dive *singleDive = appState == ApplicationState::PlanDive ? plannerWidgets->getDive()
: nullptr;
PrintDialog dlg(singleDive, this);
@ -636,8 +636,6 @@ bool MainWindow::plannerStateClean()
void MainWindow::planCanceled()
{
// while planning we might have modified the displayed_dive
// let's refresh what's shown on the profile
showProfile();
refreshDisplay();
}
@ -665,8 +663,8 @@ void MainWindow::on_actionReplanDive_triggered()
setApplicationState(ApplicationState::PlanDive);
disableShortcuts(true);
copy_dive(current_dive, &displayed_dive); // Planning works on a copy of the dive (for now).
profile->setPlanState(&displayed_dive, profile->dc);
plannerWidgets->prepareReplanDive(current_dive);
profile->setPlanState(plannerWidgets->getDive(), profile->dc);
plannerWidgets->replanDive(profile->dc);
}
@ -679,8 +677,9 @@ void MainWindow::on_actionDivePlanner_triggered()
setApplicationState(ApplicationState::PlanDive);
disableShortcuts(true);
profile->setPlanState(&displayed_dive, 0);
plannerWidgets->planDive(current_dive);
plannerWidgets->preparePlanDive(current_dive);
profile->setPlanState(plannerWidgets->getDive(), 0);
plannerWidgets->planDive();
}
void MainWindow::on_actionAddDive_triggered()