profile: don't create "uninteresting" event icons

There is code to dynamically show/hide event icons of a certain
type. The same code is used to hide generally non-interesting/
redundant items.

Instead, don't even create these items. Yes, this is idle
premature optimization.

A loop over all created event icons to hide them can be
removed, because the icon is hidden anyway on construction
time.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2021-08-31 20:10:24 +02:00 committed by Dirk Hohndel
parent 9e831c5001
commit 8a9ca5fcda
3 changed files with 32 additions and 33 deletions

View file

@ -28,7 +28,6 @@ DiveEventItem::DiveEventItem(const struct dive *d, struct event *ev, struct gasm
setupToolTipString(lastgasmix); setupToolTipString(lastgasmix);
recalculatePos(0); recalculatePos(0);
connect(vAxis, &DiveCartesianAxis::sizeChanged, this, connect(vAxis, &DiveCartesianAxis::sizeChanged, this,
[speed, this] { recalculatePos(speed); }); [speed, this] { recalculatePos(speed); });
} }
@ -180,10 +179,19 @@ void DiveEventItem::eventVisibilityChanged(const QString&, bool)
//WARN: lookslike we should implement this. //WARN: lookslike we should implement this.
} }
bool DiveEventItem::shouldBeHidden() static int depthAtTime(const DivePlotDataModel &model, int time)
{ {
const struct divecomputer *dc = get_dive_dc_const(dive, dc_number); QModelIndexList result = model.match(model.index(0, DivePlotDataModel::TIME), Qt::DisplayRole, time);
if (result.isEmpty()) {
qWarning("can't find a spot in the dataModel");
return DEPTH_NOT_FOUND;
}
return model.data(model.index(result.first().row(), DivePlotDataModel::DEPTH)).toInt();
}
bool DiveEventItem::isInteresting(const struct dive *d, const struct divecomputer *dc,
const struct event *ev, const DivePlotDataModel &model)
{
/* /*
* Some gas change events are special. Some dive computers just tell us the initial gas this way. * Some gas change events are special. Some dive computers just tell us the initial gas this way.
* Don't bother showing those * Don't bother showing those
@ -192,8 +200,8 @@ bool DiveEventItem::shouldBeHidden()
if (!strcmp(ev->name, "gaschange") && if (!strcmp(ev->name, "gaschange") &&
(ev->time.seconds == 0 || (ev->time.seconds == 0 ||
(first_sample && ev->time.seconds == first_sample->time.seconds) || (first_sample && ev->time.seconds == first_sample->time.seconds) ||
depthAtTime(ev->time.seconds) < SURFACE_THRESHOLD)) depthAtTime(model, ev->time.seconds) < SURFACE_THRESHOLD))
return true; return false;
/* /*
* Some divecomputers give "surface" events that just aren't interesting. * Some divecomputers give "surface" events that just aren't interesting.
@ -202,9 +210,13 @@ bool DiveEventItem::shouldBeHidden()
if (!strcmp(ev->name, "surface")) { if (!strcmp(ev->name, "surface")) {
int time = ev->time.seconds; int time = ev->time.seconds;
if (time <= 30 || time + 30 >= (int)dc->duration.seconds) if (time <= 30 || time + 30 >= (int)dc->duration.seconds)
return true; return false;
} }
return true;
}
bool DiveEventItem::shouldBeHidden()
{
for (int i = 0; i < evn_used; i++) { for (int i = 0; i < evn_used; i++) {
if (!strcmp(ev->name, ev_namelist[i].ev_name) && ev_namelist[i].plot_ev == false) if (!strcmp(ev->name, ev_namelist[i].ev_name) && ev_namelist[i].plot_ev == false)
return true; return true;
@ -212,17 +224,6 @@ bool DiveEventItem::shouldBeHidden()
return false; return false;
} }
int DiveEventItem::depthAtTime(int time)
{
QModelIndexList result = dataModel->match(dataModel->index(0, DivePlotDataModel::TIME), Qt::DisplayRole, time);
if (result.isEmpty()) {
qWarning("can't find a spot in the dataModel");
hide();
return DEPTH_NOT_FOUND;
}
return dataModel->data(dataModel->index(result.first().row(), DivePlotDataModel::DEPTH)).toInt();
}
void DiveEventItem::recalculatePos(int speed) void DiveEventItem::recalculatePos(int speed)
{ {
if (!ev) if (!ev)
@ -234,17 +235,16 @@ void DiveEventItem::recalculatePos(int speed)
hide(); hide();
return; return;
} }
int depth = depthAtTime(ev->time.seconds); int depth = depthAtTime(*dataModel, ev->time.seconds);
if (depth == DEPTH_NOT_FOUND) if (depth == DEPTH_NOT_FOUND) {
hide();
return; return;
if (!isVisible() && !shouldBeHidden()) }
show(); setVisible(!shouldBeHidden());
qreal x = hAxis->posAtValue(ev->time.seconds); qreal x = hAxis->posAtValue(ev->time.seconds);
qreal y = vAxis->posAtValue(depth); qreal y = vAxis->posAtValue(depth);
if (speed > 0) if (speed > 0)
Animations::moveTo(this, speed, x, y); Animations::moveTo(this, speed, x, y);
else else
setPos(x, y); setPos(x, y);
if (isVisible() && shouldBeHidden())
hide();
} }

View file

@ -23,6 +23,8 @@ public:
void setHorizontalAxis(DiveCartesianAxis *axis); void setHorizontalAxis(DiveCartesianAxis *axis);
void setModel(DivePlotDataModel *model); void setModel(DivePlotDataModel *model);
bool shouldBeHidden(); bool shouldBeHidden();
static bool isInteresting(const struct dive *d, const struct divecomputer *dc,
const struct event *ev, const DivePlotDataModel &model);
public public
slots: slots:
void recalculatePos(int animationSpeed); void recalculatePos(int animationSpeed);
@ -30,7 +32,6 @@ slots:
private: private:
void setupToolTipString(struct gasmix lastgasmix); void setupToolTipString(struct gasmix lastgasmix);
void setupPixmap(struct gasmix lastgasmix, const DivePixmaps &pixmaps); void setupPixmap(struct gasmix lastgasmix, const DivePixmaps &pixmaps);
int depthAtTime(int time);
DiveCartesianAxis *vAxis; DiveCartesianAxis *vAxis;
DiveCartesianAxis *hAxis; DiveCartesianAxis *hAxis;
DivePlotDataModel *dataModel; DivePlotDataModel *dataModel;

View file

@ -500,20 +500,18 @@ void ProfileScene::plotDive(const struct dive *dIn, int dcIn, DivePlannerPointsM
// printMode is always selected for SUBSURFACE_MOBILE due to font problems // printMode is always selected for SUBSURFACE_MOBILE due to font problems
// BUT events are wanted. // BUT events are wanted.
#endif #endif
DiveEventItem *item = new DiveEventItem(d, event, lastgasmix, dataModel, if (DiveEventItem::isInteresting(d, currentdc, event, *dataModel)) {
timeAxis, profileYAxis, animSpeed, *pixmaps); auto item = new DiveEventItem(d, event, lastgasmix, dataModel,
item->setZValue(2); timeAxis, profileYAxis, animSpeed, *pixmaps);
addItem(item); item->setZValue(2);
eventItems.push_back(item); addItem(item);
eventItems.push_back(item);
}
if (event_is_gaschange(event)) if (event_is_gaschange(event))
lastgasmix = get_gasmix_from_event(d, event); lastgasmix = get_gasmix_from_event(d, event);
event = event->next; event = event->next;
} }
// Only set visible the events that should be visible
Q_FOREACH (DiveEventItem *event, eventItems) {
event->setVisible(!event->shouldBeHidden());
}
QString dcText = get_dc_nickname(currentdc); QString dcText = get_dc_nickname(currentdc);
if (dcText == "planned dive") if (dcText == "planned dive")
dcText = tr("Planned dive"); dcText = tr("Planned dive");