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 <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2021-08-31 20:44:37 +02:00 committed by Dirk Hohndel
parent 9ddaf791ae
commit 9740651e01
3 changed files with 16 additions and 18 deletions

View file

@ -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;

View file

@ -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

View file

@ -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);