From 8a9ca5fcda112ee8b5533720a16e85ec50b1ba21 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Tue, 31 Aug 2021 20:10:24 +0200 Subject: [PATCH] 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 --- profile-widget/diveeventitem.cpp | 46 ++++++++++++++++---------------- profile-widget/diveeventitem.h | 3 ++- profile-widget/profilescene.cpp | 16 +++++------ 3 files changed, 32 insertions(+), 33 deletions(-) diff --git a/profile-widget/diveeventitem.cpp b/profile-widget/diveeventitem.cpp index b6a80a64a..2488284fb 100644 --- a/profile-widget/diveeventitem.cpp +++ b/profile-widget/diveeventitem.cpp @@ -28,7 +28,6 @@ DiveEventItem::DiveEventItem(const struct dive *d, struct event *ev, struct gasm setupToolTipString(lastgasmix); recalculatePos(0); - connect(vAxis, &DiveCartesianAxis::sizeChanged, this, [speed, this] { recalculatePos(speed); }); } @@ -180,10 +179,19 @@ void DiveEventItem::eventVisibilityChanged(const QString&, bool) //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. * Don't bother showing those @@ -192,8 +200,8 @@ bool DiveEventItem::shouldBeHidden() if (!strcmp(ev->name, "gaschange") && (ev->time.seconds == 0 || (first_sample && ev->time.seconds == first_sample->time.seconds) || - depthAtTime(ev->time.seconds) < SURFACE_THRESHOLD)) - return true; + depthAtTime(model, ev->time.seconds) < SURFACE_THRESHOLD)) + return false; /* * Some divecomputers give "surface" events that just aren't interesting. @@ -202,9 +210,13 @@ bool DiveEventItem::shouldBeHidden() if (!strcmp(ev->name, "surface")) { int time = ev->time.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++) { if (!strcmp(ev->name, ev_namelist[i].ev_name) && ev_namelist[i].plot_ev == false) return true; @@ -212,17 +224,6 @@ bool DiveEventItem::shouldBeHidden() 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) { if (!ev) @@ -234,17 +235,16 @@ void DiveEventItem::recalculatePos(int speed) hide(); return; } - int depth = depthAtTime(ev->time.seconds); - if (depth == DEPTH_NOT_FOUND) + int depth = depthAtTime(*dataModel, ev->time.seconds); + if (depth == DEPTH_NOT_FOUND) { + hide(); return; - if (!isVisible() && !shouldBeHidden()) - show(); + } + setVisible(!shouldBeHidden()); qreal x = hAxis->posAtValue(ev->time.seconds); qreal y = vAxis->posAtValue(depth); if (speed > 0) Animations::moveTo(this, speed, x, y); else setPos(x, y); - if (isVisible() && shouldBeHidden()) - hide(); } diff --git a/profile-widget/diveeventitem.h b/profile-widget/diveeventitem.h index 73dbc1fd2..205e194c3 100644 --- a/profile-widget/diveeventitem.h +++ b/profile-widget/diveeventitem.h @@ -23,6 +23,8 @@ public: 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); public slots: void recalculatePos(int animationSpeed); @@ -30,7 +32,6 @@ slots: private: void setupToolTipString(struct gasmix lastgasmix); void setupPixmap(struct gasmix lastgasmix, const DivePixmaps &pixmaps); - int depthAtTime(int time); DiveCartesianAxis *vAxis; DiveCartesianAxis *hAxis; DivePlotDataModel *dataModel; diff --git a/profile-widget/profilescene.cpp b/profile-widget/profilescene.cpp index a5e9ff2aa..9f32953b7 100644 --- a/profile-widget/profilescene.cpp +++ b/profile-widget/profilescene.cpp @@ -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 // BUT events are wanted. #endif - DiveEventItem *item = new DiveEventItem(d, event, lastgasmix, dataModel, - timeAxis, profileYAxis, animSpeed, *pixmaps); - item->setZValue(2); - addItem(item); - eventItems.push_back(item); + if (DiveEventItem::isInteresting(d, currentdc, event, *dataModel)) { + auto item = new DiveEventItem(d, event, lastgasmix, dataModel, + timeAxis, profileYAxis, animSpeed, *pixmaps); + item->setZValue(2); + addItem(item); + eventItems.push_back(item); + } if (event_is_gaschange(event)) lastgasmix = get_gasmix_from_event(d, event); 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); if (dcText == "planned dive") dcText = tr("Planned dive");