From acee77e516bc77cd49a57b5aac620833ea655eb6 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sun, 3 Jan 2021 23:18:48 +0100 Subject: profile: move calculations out of DivePercentageItem::paint() The DivePercentageItem is a polygon-item with a custom paint() method. Calculation of the polygon is done once in replot(), but calculation of the corresponding colors is done in every paint() call. The problem is, we have no control over paint(). It is called whenever Qt feels like. Therefore using live dive data is a dangerous proposition if we ever want to get rid of the global displayed_dive. Do all the calculations in replot(). Store the colors in an additional array of the same size as the polygon. Signed-off-by: Berthold Stoeger --- profile-widget/diveprofileitem.cpp | 28 +++++++++++++--------------- profile-widget/diveprofileitem.h | 1 + 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/profile-widget/diveprofileitem.cpp b/profile-widget/diveprofileitem.cpp index f20d0bee3..c48f345b6 100644 --- a/profile-widget/diveprofileitem.cpp +++ b/profile-widget/diveprofileitem.cpp @@ -285,14 +285,20 @@ DivePercentageItem::DivePercentageItem(const DivePlotDataModel &model, const Div void DivePercentageItem::replot() { - int sec = 0; - // Ignore empty values. a heart rate of 0 would be a bad sign. QPolygonF poly; + colors.clear(); for (int i = 0, modelDataCount = dataModel.rowCount(); i < modelDataCount; i++) { - sec = dataModel.index(i, hDataColumn).data().toInt(); + int sec = dataModel.index(i, hDataColumn).data().toInt(); QPointF point(hAxis.posAtValue(sec), vAxis.posAtValue(64 - 4 * tissueIndex)); poly.append(point); + + double value = dataModel.index(i, vDataColumn).data().toDouble(); + struct gasmix gasmix = gasmix_air; + const struct event *ev = NULL; + gasmix = get_gasmix(&displayed_dive, displayed_dc, sec, &ev, gasmix); + int inert = get_n2(gasmix) + get_he(gasmix); + colors.push_back(ColorScale(value, inert)); } setPolygon(poly); @@ -333,18 +339,10 @@ void DivePercentageItem::paint(QPainter *painter, const QStyleOptionGraphicsItem mypen.setCapStyle(Qt::FlatCap); mypen.setCosmetic(false); QPolygonF poly = polygon(); - for (int i = 1, modelDataCount = dataModel.rowCount(); i < modelDataCount; i++) { - if (i < poly.count()) { - double value = dataModel.index(i, vDataColumn).data().toDouble(); - struct gasmix gasmix = gasmix_air; - const struct event *ev = NULL; - int sec = dataModel.index(i, DivePlotDataModel::TIME).data().toInt(); - gasmix = get_gasmix(&displayed_dive, displayed_dc, sec, &ev, gasmix); - int inert = get_n2(gasmix) + get_he(gasmix); - mypen.setBrush(QBrush(ColorScale(value, inert))); - painter->setPen(mypen); - painter->drawLine(poly[i - 1], poly[i]); - } + for (int i = 1; i < poly.count(); i++) { + mypen.setBrush(QBrush(colors[i])); + painter->setPen(mypen); + painter->drawLine(poly[i - 1], poly[i]); } painter->restore(); } diff --git a/profile-widget/diveprofileitem.h b/profile-widget/diveprofileitem.h index fc123c133..18902374a 100644 --- a/profile-widget/diveprofileitem.h +++ b/profile-widget/diveprofileitem.h @@ -122,6 +122,7 @@ public: void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override; private: + std::vector colors; // Must have same number of elements as the polygon QString visibilityKey; int tissueIndex; QColor ColorScale(double value, int inert); -- cgit v1.2.3-70-g09d2