From 4b801f1f50e0ca78d92673e92f9f05f22514f2d7 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Thu, 25 Feb 2021 22:20:26 +0100 Subject: planner: split createTemporaryPlan() function. The DivePlannerPointsModel::createTemporaryPlan() function had two distinct and independent parts: 1) create the data points. 2) create the dive sample and calculate variations. The second part was only exectuted if the recalc flag was set. Out of the two callers, one was explicitly disabling and setting the recalc flag to avoid the second part. The much more logical thing is to simply split the function in two and only call the first part. To avoid any functional change, the second caller (the profile) still tests for the recalc flag. However, if it shouldn't replot a new plan, why calculate it in the first place!? And why does the display function change the plan at all? This appears all very ill-thought out and should be changed in due course. Signed-off-by: Berthold Stoeger --- qt-models/diveplannermodel.cpp | 49 ++++++++++++++++++++++-------------------- qt-models/diveplannermodel.h | 1 + 2 files changed, 27 insertions(+), 23 deletions(-) (limited to 'qt-models') diff --git a/qt-models/diveplannermodel.cpp b/qt-models/diveplannermodel.cpp index ec83483f3..967ef3de7 100644 --- a/qt-models/diveplannermodel.cpp +++ b/qt-models/diveplannermodel.cpp @@ -1016,8 +1016,6 @@ void DivePlannerPointsModel::createTemporaryPlan() plan_add_segment(&diveplan, deltaT, p.depth.mm, p.cylinderid, p.setpoint, true, p.divemode); } - // what does the cache do??? - struct deco_state *cache = NULL; struct divedatapoint *dp = NULL; for (int i = 0; i < d->cylinders.nr; i++) { cylinder_t *cyl = get_cylinder(d, i); @@ -1035,28 +1033,35 @@ void DivePlannerPointsModel::createTemporaryPlan() #if DEBUG_PLAN dump_plan(&diveplan); #endif - if (recalcQ() && !diveplan_empty(&diveplan)) { - struct decostop stoptable[60]; - struct deco_state plan_deco_state; - struct diveplan *plan_copy; - - memset(&plan_deco_state, 0, sizeof(struct deco_state)); - plan(&plan_deco_state, &diveplan, d, DECOTIMESTEP, stoptable, &cache, isPlanner(), false); - plan_copy = (struct diveplan *)malloc(sizeof(struct diveplan)); - lock_planner(); - cloneDiveplan(&diveplan, plan_copy); - unlock_planner(); +} + +void DivePlannerPointsModel::recalcTemporaryPlan() +{ + if (diveplan_empty(&diveplan)) + return; + + struct deco_state *cache = NULL; + struct decostop stoptable[60]; + struct deco_state plan_deco_state; + struct diveplan *plan_copy; + + memset(&plan_deco_state, 0, sizeof(struct deco_state)); + plan(&plan_deco_state, &diveplan, d, DECOTIMESTEP, stoptable, &cache, isPlanner(), false); + plan_copy = (struct diveplan *)malloc(sizeof(struct diveplan)); + lock_planner(); + cloneDiveplan(&diveplan, plan_copy); + unlock_planner(); #ifdef VARIATIONS_IN_BACKGROUND - // Since we're calling computeVariations asynchronously and plan_deco_state is allocated - // on the stack, it must be copied and freed by the worker-thread. - struct deco_state *plan_deco_state_copy = new deco_state(plan_deco_state); - QtConcurrent::run(this, &DivePlannerPointsModel::computeVariationsFreeDeco, plan_copy, plan_deco_state_copy); + // Since we're calling computeVariations asynchronously and plan_deco_state is allocated + // on the stack, it must be copied and freed by the worker-thread. + struct deco_state *plan_deco_state_copy = new deco_state(plan_deco_state); + QtConcurrent::run(this, &DivePlannerPointsModel::computeVariationsFreeDeco, plan_copy, plan_deco_state_copy); #else - computeVariations(plan_copy, &plan_deco_state); + computeVariations(plan_copy, &plan_deco_state); #endif - final_deco_state = plan_deco_state; - emit calculatedPlanNotes(QString(d->notes)); - } + final_deco_state = plan_deco_state; + emit calculatedPlanNotes(QString(d->notes)); + // throw away the cache free(cache); #if DEBUG_PLAN @@ -1246,10 +1251,8 @@ void DivePlannerPointsModel::createPlan(bool replanCopy) { // Ok, so, here the diveplan creates a dive struct deco_state *cache = NULL; - bool oldrec = std::exchange(recalc, false); removeDeco(); createTemporaryPlan(); - recalc = oldrec; //TODO: C-based function here? struct decostop stoptable[60]; diff --git a/qt-models/diveplannermodel.h b/qt-models/diveplannermodel.h index 9b79232d6..70f06b7c6 100644 --- a/qt-models/diveplannermodel.h +++ b/qt-models/diveplannermodel.h @@ -92,6 +92,7 @@ slots: void remove(const QModelIndex &index); void cancelPlan(); void createTemporaryPlan(); + void recalcTemporaryPlan(); // Writes the plan into the dive. void deleteTemporaryPlan(); void emitDataChanged(); void setRebreatherMode(int mode); -- cgit v1.2.3-70-g09d2