From 9740651e0196ee58890e0bc2205d74b03632dbfe Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Tue, 31 Aug 2021 20:44:37 +0200 Subject: [PATCH] profile: save depth of event The DivePlotDataModel was saved with every event to access the depth. However, since the depth never changes, we can simply save the depth instead. Also, since we only need the model to access the plot_info, pass the plot_info directly. As noted in a previous commit message, I believe that Qt models are a very bad choice for intra-application data transfer. They should only ever be used to interface with Qt. And since touching this code, pass duration_t instead of int to depthAtTime() to make the callers less cluttered. Signed-off-by: Berthold Stoeger --- profile-widget/diveeventitem.cpp | 21 ++++++++++----------- profile-widget/diveeventitem.h | 9 ++++----- profile-widget/profilescene.cpp | 4 ++-- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/profile-widget/diveeventitem.cpp b/profile-widget/diveeventitem.cpp index 57ef7ebba..a4fb55e3f 100644 --- a/profile-widget/diveeventitem.cpp +++ b/profile-widget/diveeventitem.cpp @@ -9,18 +9,19 @@ #include "core/gettextfromc.h" #include "core/sample.h" #include "core/subsurface-string.h" -#include "qt-models/diveplotdatamodel.h" #define DEPTH_NOT_FOUND (-2342) +static int depthAtTime(const plot_info &pi, duration_t time); + DiveEventItem::DiveEventItem(const struct dive *d, struct event *ev, struct gasmix lastgasmix, - DivePlotDataModel *model, DiveCartesianAxis *hAxis, DiveCartesianAxis *vAxis, + const plot_info &pi, DiveCartesianAxis *hAxis, DiveCartesianAxis *vAxis, int speed, const DivePixmaps &pixmaps, QGraphicsItem *parent) : DivePixmapItem(parent), vAxis(vAxis), hAxis(hAxis), - dataModel(model), ev(ev), - dive(d) + dive(d), + depth(depthAtTime(pi, ev->time)) { setFlag(ItemIgnoresTransformations); @@ -179,13 +180,12 @@ void DiveEventItem::eventVisibilityChanged(const QString&, bool) //WARN: lookslike we should implement this. } -static int depthAtTime(const DivePlotDataModel &model, int time) +static int depthAtTime(const plot_info &pi, duration_t time) { // Do a binary search for the timestamp - const plot_info &pi = model.data(); auto it = std::lower_bound(pi.entry, pi.entry + pi.nr, time, - [](const plot_data &d1, int time) { return d1.sec < time; }); - if (it == pi.entry + pi.nr || it->sec != time) { + [](const plot_data &d1, duration_t t) { return d1.sec < t.seconds; }); + if (it == pi.entry + pi.nr || it->sec != time.seconds) { qWarning("can't find a spot in the dataModel"); return DEPTH_NOT_FOUND; } @@ -193,7 +193,7 @@ static int depthAtTime(const DivePlotDataModel &model, int time) } bool DiveEventItem::isInteresting(const struct dive *d, const struct divecomputer *dc, - const struct event *ev, const DivePlotDataModel &model) + const struct event *ev, const plot_info &pi) { /* * Some gas change events are special. Some dive computers just tell us the initial gas this way. @@ -203,7 +203,7 @@ bool DiveEventItem::isInteresting(const struct dive *d, const struct divecompute if (!strcmp(ev->name, "gaschange") && (ev->time.seconds == 0 || (first_sample && ev->time.seconds == first_sample->time.seconds) || - depthAtTime(model, ev->time.seconds) < SURFACE_THRESHOLD)) + depthAtTime(pi, ev->time) < SURFACE_THRESHOLD)) return false; /* @@ -232,7 +232,6 @@ void DiveEventItem::recalculatePos(int speed) if (!ev) return; - int depth = depthAtTime(*dataModel, ev->time.seconds); if (depth == DEPTH_NOT_FOUND) { hide(); return; diff --git a/profile-widget/diveeventitem.h b/profile-widget/diveeventitem.h index 205e194c3..2ddabade5 100644 --- a/profile-widget/diveeventitem.h +++ b/profile-widget/diveeventitem.h @@ -5,15 +5,15 @@ #include "divepixmapitem.h" class DiveCartesianAxis; -class DivePixmapCache; class DivePixmaps; struct event; +struct plot_info; class DiveEventItem : public DivePixmapItem { Q_OBJECT public: DiveEventItem(const struct dive *d, struct event *ev, struct gasmix lastgasmix, - DivePlotDataModel *model, DiveCartesianAxis *hAxis, DiveCartesianAxis *vAxis, + const struct plot_info &pi, DiveCartesianAxis *hAxis, DiveCartesianAxis *vAxis, int speed, const DivePixmaps &pixmaps, QGraphicsItem *parent = nullptr); ~DiveEventItem(); const struct event *getEvent() const; @@ -21,10 +21,9 @@ public: void eventVisibilityChanged(const QString &eventName, bool visible); void setVerticalAxis(DiveCartesianAxis *axis, int speed); void setHorizontalAxis(DiveCartesianAxis *axis); - void setModel(DivePlotDataModel *model); bool shouldBeHidden(); static bool isInteresting(const struct dive *d, const struct divecomputer *dc, - const struct event *ev, const DivePlotDataModel &model); + const struct event *ev, const struct plot_info &pi); public slots: void recalculatePos(int animationSpeed); @@ -34,9 +33,9 @@ private: void setupPixmap(struct gasmix lastgasmix, const DivePixmaps &pixmaps); DiveCartesianAxis *vAxis; DiveCartesianAxis *hAxis; - DivePlotDataModel *dataModel; struct event *ev; const struct dive *dive; + int depth; }; #endif // DIVEEVENTITEM_H diff --git a/profile-widget/profilescene.cpp b/profile-widget/profilescene.cpp index 9f32953b7..16e89afc2 100644 --- a/profile-widget/profilescene.cpp +++ b/profile-widget/profilescene.cpp @@ -500,8 +500,8 @@ void ProfileScene::plotDive(const struct dive *dIn, int dcIn, DivePlannerPointsM // printMode is always selected for SUBSURFACE_MOBILE due to font problems // BUT events are wanted. #endif - if (DiveEventItem::isInteresting(d, currentdc, event, *dataModel)) { - auto item = new DiveEventItem(d, event, lastgasmix, dataModel, + if (DiveEventItem::isInteresting(d, currentdc, event, plotInfo)) { + auto item = new DiveEventItem(d, event, lastgasmix, plotInfo, timeAxis, profileYAxis, animSpeed, *pixmaps); item->setZValue(2); addItem(item);