profile: pass DivePlannerPointsModel at construction time

This model is only needed when in plan mode. To enable multiple
profilewidgets at the same time (e.g. for the mobile app or
for printing), make the pointer to DivePlannerPointsModel a
member variable that is initialized at construction time.

Moreover, allow passing null as the DivePlannerPointsModel,
in which case planning will be disabled. This will be useful
for simple printing.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2021-01-25 15:06:53 +01:00 committed by Dirk Hohndel
parent 94633d2156
commit b9673df60b
4 changed files with 29 additions and 36 deletions

View file

@ -134,7 +134,7 @@ MainWindow::MainWindow() : QMainWindow(),
// for the "default" mode // for the "default" mode
mainTab.reset(new MainTab); mainTab.reset(new MainTab);
diveList.reset(new DiveListView); diveList.reset(new DiveListView);
graphics = new ProfileWidget2(this); graphics = new ProfileWidget2(DivePlannerPointsModel::instance(), this);
mapWidget.reset(MapWidget::instance()); // Yes, this is ominous see comment in mapwidget.cpp. mapWidget.reset(MapWidget::instance()); // Yes, this is ominous see comment in mapwidget.cpp.
plannerWidgets.reset(new PlannerWidgets); plannerWidgets.reset(new PlannerWidgets);
statistics.reset(new StatsWidget); statistics.reset(new StatsWidget);

View file

@ -102,9 +102,10 @@ T *ProfileWidget2::createItem(const DiveCartesianAxis &vAxis, int vColumn, int z
return res; return res;
} }
ProfileWidget2::ProfileWidget2(QWidget *parent) : QGraphicsView(parent), ProfileWidget2::ProfileWidget2(DivePlannerPointsModel *plannerModelIn, QWidget *parent) : QGraphicsView(parent),
currentState(INVALID), currentState(INVALID),
dataModel(new DivePlotDataModel(this)), dataModel(new DivePlotDataModel(this)),
plannerModel(plannerModelIn),
zoomLevel(0), zoomLevel(0),
zoomFactor(1.15), zoomFactor(1.15),
isGrayscale(false), isGrayscale(false),
@ -527,7 +528,7 @@ void ProfileWidget2::plotDive(const struct dive *d, bool force, bool doClearPict
#ifdef SUBSURFACE_MOBILE #ifdef SUBSURFACE_MOBILE
Q_UNUSED(doClearPictures); Q_UNUSED(doClearPictures);
#endif #endif
if (currentState != ADD && currentState != PLAN) { if ((currentState != ADD && currentState != PLAN) || !plannerModel) {
if (!d) { if (!d) {
setEmptyState(); setEmptyState();
return; return;
@ -549,7 +550,6 @@ void ProfileWidget2::plotDive(const struct dive *d, bool force, bool doClearPict
decoModelParameters->setText(QString("GF %1/%2").arg(prefs.gflow).arg(prefs.gfhigh)); decoModelParameters->setText(QString("GF %1/%2").arg(prefs.gflow).arg(prefs.gfhigh));
#ifndef SUBSURFACE_MOBILE #ifndef SUBSURFACE_MOBILE
} else { } else {
DivePlannerPointsModel *plannerModel = DivePlannerPointsModel::instance();
plannerModel->createTemporaryPlan(); plannerModel->createTemporaryPlan();
struct diveplan &diveplan = plannerModel->getDiveplan(); struct diveplan &diveplan = plannerModel->getDiveplan();
if (!diveplan.dp) { if (!diveplan.dp) {
@ -601,7 +601,7 @@ void ProfileWidget2::plotDive(const struct dive *d, bool force, bool doClearPict
// create_plot_info_new() automatically frees old plot data // create_plot_info_new() automatically frees old plot data
#ifndef SUBSURFACE_MOBILE #ifndef SUBSURFACE_MOBILE
// A non-null planner_ds signals to create_plot_info_new that the dive is currently planned. // A non-null planner_ds signals to create_plot_info_new that the dive is currently planned.
struct deco_state *planner_ds = currentState == PLAN ? &DivePlannerPointsModel::instance()->final_deco_state : nullptr; struct deco_state *planner_ds = currentState == PLAN && plannerModel ? &plannerModel->final_deco_state : nullptr;
create_plot_info_new(&displayed_dive, currentdc, &plotInfo, !shouldCalculateMaxDepth, planner_ds); create_plot_info_new(&displayed_dive, currentdc, &plotInfo, !shouldCalculateMaxDepth, planner_ds);
#else #else
create_plot_info_new(&displayed_dive, currentdc, &plotInfo, !shouldCalculateMaxDepth, nullptr); create_plot_info_new(&displayed_dive, currentdc, &plotInfo, !shouldCalculateMaxDepth, nullptr);
@ -790,10 +790,9 @@ void ProfileWidget2::plotDive(const struct dive *d, bool force, bool doClearPict
diveComputerText->setText(dcText); diveComputerText->setText(dcText);
#ifndef SUBSURFACE_MOBILE #ifndef SUBSURFACE_MOBILE
if (currentState == ADD || currentState == PLAN) { // TODO: figure a way to move this from here. if ((currentState == ADD || currentState == PLAN) && plannerModel) {
repositionDiveHandlers(); repositionDiveHandlers();
DivePlannerPointsModel *model = DivePlannerPointsModel::instance(); plannerModel->deleteTemporaryPlan();
model->deleteTemporaryPlan();
} }
if (doClearPictures) if (doClearPictures)
clearPictures(); clearPictures();
@ -998,8 +997,7 @@ void ProfileWidget2::wheelEvent(QWheelEvent *event)
void ProfileWidget2::mouseDoubleClickEvent(QMouseEvent *event) void ProfileWidget2::mouseDoubleClickEvent(QMouseEvent *event)
{ {
if (currentState == PLAN || currentState == ADD) { if ((currentState == PLAN || currentState == ADD) && plannerModel) {
DivePlannerPointsModel *plannerModel = DivePlannerPointsModel::instance();
QPointF mappedPos = mapToScene(event->pos()); QPointF mappedPos = mapToScene(event->pos());
if (isPointOutOfBoundaries(mappedPos)) if (isPointOutOfBoundaries(mappedPos))
return; return;
@ -1266,7 +1264,6 @@ void ProfileWidget2::setToolTipVisibile(bool visible)
void ProfileWidget2::connectPlannerModel() void ProfileWidget2::connectPlannerModel()
{ {
DivePlannerPointsModel *plannerModel = DivePlannerPointsModel::instance();
connect(plannerModel, &DivePlannerPointsModel::dataChanged, this, &ProfileWidget2::replot); connect(plannerModel, &DivePlannerPointsModel::dataChanged, this, &ProfileWidget2::replot);
connect(plannerModel, &DivePlannerPointsModel::cylinderModelEdited, this, &ProfileWidget2::replot); connect(plannerModel, &DivePlannerPointsModel::cylinderModelEdited, this, &ProfileWidget2::replot);
connect(plannerModel, &DivePlannerPointsModel::modelReset, this, &ProfileWidget2::pointsReset); connect(plannerModel, &DivePlannerPointsModel::modelReset, this, &ProfileWidget2::pointsReset);
@ -1685,7 +1682,7 @@ void ProfileWidget2::editName(DiveEventItem *item)
void ProfileWidget2::disconnectTemporaryConnections() void ProfileWidget2::disconnectTemporaryConnections()
{ {
#ifndef SUBSURFACE_MOBILE #ifndef SUBSURFACE_MOBILE
DivePlannerPointsModel *plannerModel = DivePlannerPointsModel::instance(); if (plannerModel) {
disconnect(plannerModel, &DivePlannerPointsModel::dataChanged, this, &ProfileWidget2::replot); disconnect(plannerModel, &DivePlannerPointsModel::dataChanged, this, &ProfileWidget2::replot);
disconnect(plannerModel, &DivePlannerPointsModel::cylinderModelEdited, this, &ProfileWidget2::replot); disconnect(plannerModel, &DivePlannerPointsModel::cylinderModelEdited, this, &ProfileWidget2::replot);
@ -1693,6 +1690,7 @@ void ProfileWidget2::disconnectTemporaryConnections()
disconnect(plannerModel, &DivePlannerPointsModel::rowsInserted, this, &ProfileWidget2::pointInserted); disconnect(plannerModel, &DivePlannerPointsModel::rowsInserted, this, &ProfileWidget2::pointInserted);
disconnect(plannerModel, &DivePlannerPointsModel::rowsRemoved, this, &ProfileWidget2::pointsRemoved); disconnect(plannerModel, &DivePlannerPointsModel::rowsRemoved, this, &ProfileWidget2::pointsRemoved);
disconnect(plannerModel, &DivePlannerPointsModel::rowsMoved, this, &ProfileWidget2::pointsMoved); disconnect(plannerModel, &DivePlannerPointsModel::rowsMoved, this, &ProfileWidget2::pointsMoved);
}
#endif #endif
Q_FOREACH (QAction *action, actionsForKeys.values()) { Q_FOREACH (QAction *action, actionsForKeys.values()) {
action->setShortcut(QKeySequence()); action->setShortcut(QKeySequence());
@ -1733,7 +1731,7 @@ void ProfileWidget2::pointsReset()
{ {
handles.clear(); handles.clear();
gases.clear(); gases.clear();
int count = DivePlannerPointsModel::instance()->rowCount(); int count = plannerModel->rowCount();
for (int i = 0; i < count; ++i) { for (int i = 0; i < count; ++i) {
handles.emplace_back(createHandle()); handles.emplace_back(createHandle());
gases.emplace_back(createGas()); gases.emplace_back(createGas());
@ -1747,7 +1745,7 @@ void ProfileWidget2::pointInserted(const QModelIndex &, int from, int to)
gases.emplace(gases.begin() + i, createGas()); gases.emplace(gases.begin() + i, createGas());
} }
if (DivePlannerPointsModel::instance()->recalcQ()) if (plannerModel->recalcQ())
replot(); replot();
} }
@ -1768,7 +1766,6 @@ void ProfileWidget2::pointsMoved(const QModelIndex &, int start, int end, const
void ProfileWidget2::repositionDiveHandlers() void ProfileWidget2::repositionDiveHandlers()
{ {
DivePlannerPointsModel *plannerModel = DivePlannerPointsModel::instance();
hideAll(gases); hideAll(gases);
// Re-position the user generated dive handlers // Re-position the user generated dive handlers
for (int i = 0; i < plannerModel->rowCount(); i++) { for (int i = 0; i < plannerModel->rowCount(); i++) {
@ -1822,7 +1819,6 @@ int ProfileWidget2::fixHandlerIndex(DiveHandler *activeHandler)
void ProfileWidget2::recreatePlannedDive() void ProfileWidget2::recreatePlannedDive()
{ {
DiveHandler *activeHandler = qobject_cast<DiveHandler *>(sender()); DiveHandler *activeHandler = qobject_cast<DiveHandler *>(sender());
DivePlannerPointsModel *plannerModel = DivePlannerPointsModel::instance();
int index = fixHandlerIndex(activeHandler); int index = fixHandlerIndex(activeHandler);
int mintime = 0; int mintime = 0;
int maxtime = plannerModel->at(plannerModel->size() - 1).time * 3 / 2; int maxtime = plannerModel->at(plannerModel->size() - 1).time * 3 / 2;
@ -1849,10 +1845,9 @@ void ProfileWidget2::recreatePlannedDive()
void ProfileWidget2::keyDownAction() void ProfileWidget2::keyDownAction()
{ {
if (currentState != ADD && currentState != PLAN) if ((currentState != ADD && currentState != PLAN) || !plannerModel)
return; return;
DivePlannerPointsModel *plannerModel = DivePlannerPointsModel::instance();
bool oldRecalc = plannerModel->setRecalc(false); bool oldRecalc = plannerModel->setRecalc(false);
Q_FOREACH (QGraphicsItem *i, scene()->selectedItems()) { Q_FOREACH (QGraphicsItem *i, scene()->selectedItems()) {
@ -1872,10 +1867,9 @@ void ProfileWidget2::keyDownAction()
void ProfileWidget2::keyUpAction() void ProfileWidget2::keyUpAction()
{ {
if (currentState != ADD && currentState != PLAN) if ((currentState != ADD && currentState != PLAN) || !plannerModel)
return; return;
DivePlannerPointsModel *plannerModel = DivePlannerPointsModel::instance();
bool oldRecalc = plannerModel->setRecalc(false); bool oldRecalc = plannerModel->setRecalc(false);
Q_FOREACH (QGraphicsItem *i, scene()->selectedItems()) { Q_FOREACH (QGraphicsItem *i, scene()->selectedItems()) {
if (DiveHandler *handler = qgraphicsitem_cast<DiveHandler *>(i)) { if (DiveHandler *handler = qgraphicsitem_cast<DiveHandler *>(i)) {
@ -1895,10 +1889,9 @@ void ProfileWidget2::keyUpAction()
void ProfileWidget2::keyLeftAction() void ProfileWidget2::keyLeftAction()
{ {
if (currentState != ADD && currentState != PLAN) if ((currentState != ADD && currentState != PLAN) || !plannerModel)
return; return;
DivePlannerPointsModel *plannerModel = DivePlannerPointsModel::instance();
bool oldRecalc = plannerModel->setRecalc(false); bool oldRecalc = plannerModel->setRecalc(false);
Q_FOREACH (QGraphicsItem *i, scene()->selectedItems()) { Q_FOREACH (QGraphicsItem *i, scene()->selectedItems()) {
if (DiveHandler *handler = qgraphicsitem_cast<DiveHandler *>(i)) { if (DiveHandler *handler = qgraphicsitem_cast<DiveHandler *>(i)) {
@ -1931,10 +1924,9 @@ void ProfileWidget2::keyLeftAction()
void ProfileWidget2::keyRightAction() void ProfileWidget2::keyRightAction()
{ {
if (currentState != ADD && currentState != PLAN) if ((currentState != ADD && currentState != PLAN) || !plannerModel)
return; return;
DivePlannerPointsModel *plannerModel = DivePlannerPointsModel::instance();
bool oldRecalc = plannerModel->setRecalc(false); bool oldRecalc = plannerModel->setRecalc(false);
Q_FOREACH (QGraphicsItem *i, scene()->selectedItems()) { Q_FOREACH (QGraphicsItem *i, scene()->selectedItems()) {
if (DiveHandler *handler = qgraphicsitem_cast<DiveHandler *>(i)) { if (DiveHandler *handler = qgraphicsitem_cast<DiveHandler *>(i)) {
@ -1966,10 +1958,9 @@ void ProfileWidget2::keyRightAction()
void ProfileWidget2::keyDeleteAction() void ProfileWidget2::keyDeleteAction()
{ {
if (currentState != ADD && currentState != PLAN) if ((currentState != ADD && currentState != PLAN) || !plannerModel)
return; return;
DivePlannerPointsModel *plannerModel = DivePlannerPointsModel::instance();
int selCount = scene()->selectedItems().count(); int selCount = scene()->selectedItems().count();
if (selCount) { if (selCount) {
QVector<int> selectedIndices; QVector<int> selectedIndices;
@ -1985,7 +1976,7 @@ void ProfileWidget2::keyDeleteAction()
void ProfileWidget2::keyEscAction() void ProfileWidget2::keyEscAction()
{ {
if (currentState != ADD && currentState != PLAN) if ((currentState != ADD && currentState != PLAN) || !plannerModel)
return; return;
if (scene()->selectedItems().count()) { if (scene()->selectedItems().count()) {
@ -1993,7 +1984,6 @@ void ProfileWidget2::keyEscAction()
return; return;
} }
DivePlannerPointsModel *plannerModel = DivePlannerPointsModel::instance();
if (plannerModel->isPlanner()) if (plannerModel->isPlanner())
plannerModel->cancelPlan(); plannerModel->cancelPlan();
} }

View file

@ -38,6 +38,7 @@ class DiveRectItem;
class DepthAxis; class DepthAxis;
class DiveCartesianAxis; class DiveCartesianAxis;
class DiveProfileItem; class DiveProfileItem;
class DivePlannerPointsModel;
class TimeAxis; class TimeAxis;
class DiveTemperatureItem; class DiveTemperatureItem;
class DiveHeartrateItem; class DiveHeartrateItem;
@ -74,7 +75,8 @@ public:
COLUMNS COLUMNS
}; };
ProfileWidget2(QWidget *parent = 0); // Pass null as plannerModel if no support for planning required
ProfileWidget2(DivePlannerPointsModel *plannerModel, QWidget *parent = 0);
~ProfileWidget2(); ~ProfileWidget2();
void resetZoom(); void resetZoom();
void scale(qreal sx, qreal sy); void scale(qreal sx, qreal sy);
@ -179,6 +181,7 @@ private:
void splitCurrentDC(); void splitCurrentDC();
DivePlotDataModel *dataModel; DivePlotDataModel *dataModel;
DivePlannerPointsModel *plannerModel; // If null, no planning supported.
int zoomLevel; int zoomLevel;
qreal zoomFactor; qreal zoomFactor;
bool isGrayscale; bool isGrayscale;

View file

@ -16,7 +16,7 @@ QMLProfile::QMLProfile(QQuickItem *parent) :
m_margin(0), m_margin(0),
m_xOffset(0.0), m_xOffset(0.0),
m_yOffset(0.0), m_yOffset(0.0),
m_profileWidget(new ProfileWidget2) m_profileWidget(new ProfileWidget2(nullptr, nullptr))
{ {
setAntialiasing(true); setAntialiasing(true);
setFlags(QQuickItem::ItemClipsChildrenToShape | QQuickItem::ItemHasContents ); setFlags(QQuickItem::ItemClipsChildrenToShape | QQuickItem::ItemHasContents );