diff --git a/desktop-widgets/profilewidget.cpp b/desktop-widgets/profilewidget.cpp index 5c916802d..71a1c0dcd 100644 --- a/desktop-widgets/profilewidget.cpp +++ b/desktop-widgets/profilewidget.cpp @@ -51,7 +51,7 @@ void EmptyView::resizeEvent(QResizeEvent *) update(); } -ProfileWidget::ProfileWidget() : originalDive(nullptr) +ProfileWidget::ProfileWidget() : originalDive(nullptr), placingCommand(false) { ui.setupUi(this); @@ -117,6 +117,7 @@ ProfileWidget::ProfileWidget() : originalDive(nullptr) connect(ui.profPn2, &QAction::triggered, pp_gas, &qPrefPartialPressureGas::set_pn2); connect(ui.profPO2, &QAction::triggered, pp_gas, &qPrefPartialPressureGas::set_po2); + connect(&diveListNotifier, &DiveListNotifier::divesChanged, this, &ProfileWidget::divesChanged); connect(&diveListNotifier, &DiveListNotifier::settingsChanged, view.get(), &ProfileWidget2::settingsChanged); connect(view.get(), &ProfileWidget2::editCurrentDive, this, &ProfileWidget::editDive); connect(view.get(), &ProfileWidget2::stopAdded, this, &ProfileWidget::stopAdded); @@ -201,6 +202,27 @@ void ProfileWidget::plotCurrentDive() } } +void ProfileWidget::divesChanged(const QVector &dives, DiveField field) +{ + // If the current dive is not in list of changed dives, do nothing. + // Only if duration or depth changed, the profile needs to be replotted. + // Also, if we are currently placing a command, don't do anything. + // Note that we cannot use Command::placingCommand(), because placing + // a depth or time change on the maintab requires an update. + if (!current_dive || !dives.contains(current_dive) || !(field.duration || field.depth) || placingCommand) + return; + + // If were editing the current dive and not currently + // placing command, we have to update the edited dive. + if (editedDive) { + copy_dive(current_dive, editedDive.get()); + // TODO: Holy moly that function sends too many signals. Fix it! + DivePlannerPointsModel::instance()->loadFromDive(editedDive.get()); + } + + plotCurrentDive(); +} + void ProfileWidget::setPlanState(const struct dive *d, int dc) { exitEditMode(); @@ -256,11 +278,24 @@ static void calcDepth(dive &d, int dcNr) fixup_dive(&d); } +// Silly RAII-variable setter class: reset variable when going out of scope. +template +struct Setter { + T &var, old; + Setter(T &var, T value) : var(var), old(var) { + var = value; + } + ~Setter() { + var = old; + } +}; + void ProfileWidget::stopAdded() { if (!editedDive) return; calcDepth(*editedDive, editedDc); + Setter s(placingCommand, true); Command::editProfile(editedDive.get(), Command::EditProfileType::ADD, 0); } @@ -269,6 +304,7 @@ void ProfileWidget::stopRemoved(int count) if (!editedDive) return; calcDepth(*editedDive, editedDc); + Setter s(placingCommand, true); Command::editProfile(editedDive.get(), Command::EditProfileType::REMOVE, count); } @@ -277,5 +313,6 @@ void ProfileWidget::stopMoved(int count) if (!editedDive) return; calcDepth(*editedDive, editedDc); + Setter s(placingCommand, true); Command::editProfile(editedDive.get(), Command::EditProfileType::MOVE, count); } diff --git a/desktop-widgets/profilewidget.h b/desktop-widgets/profilewidget.h index 5f30cd99a..cdbd0baf3 100644 --- a/desktop-widgets/profilewidget.h +++ b/desktop-widgets/profilewidget.h @@ -5,6 +5,7 @@ #define PROFILEWIDGET_H #include "ui_profilewidget.h" +#include "core/subsurface-qt/divelistnotifier.h" #include #include @@ -27,6 +28,7 @@ public: void setEnabledToolbar(bool enabled); private slots: + void divesChanged(const QVector &dives, DiveField field); void unsetProfHR(); void unsetProfTissues(); void stopAdded(); @@ -48,6 +50,7 @@ private: std::unique_ptr editedDive; int editedDc; dive *originalDive; + bool placingCommand; }; #endif // PROFILEWIDGET_H diff --git a/desktop-widgets/tab-widgets/maintab.cpp b/desktop-widgets/tab-widgets/maintab.cpp index bd8c437d1..71c6d2e02 100644 --- a/desktop-widgets/tab-widgets/maintab.cpp +++ b/desktop-widgets/tab-widgets/maintab.cpp @@ -213,10 +213,6 @@ void MainTab::divesChanged(const QVector &dives, DiveField field) ui.buddy->setText(current_dive->buddy); if (field.diveguide) ui.diveguide->setText(current_dive->diveguide); - - // If duration or depth changed, the profile needs to be replotted - if (field.duration || field.depth) - MainWindow::instance()->refreshProfile(); } void MainTab::diveSiteEdited(dive_site *ds, int)