From 1faad2c1f18753adc9283508402799b27775fe01 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Thu, 30 Jun 2022 23:17:03 +0200 Subject: [PATCH] cleanup: cleanup profile tooltip code This was a mess of plain pointers. For example to generate the "entryToolTip", an item in the "toolTips" vector was created, then deep-copied and then the "toolTips" vector was cleared. Instead, replace the "addToolTip" by a "makeToolTip" function and replace plain pointers by unique_ptrs. Makes this thing a bit more manageable. Signed-off-by: Berthold Stoeger --- profile-widget/divetooltipitem.cpp | 43 +++++++++++------------------- profile-widget/divetooltipitem.h | 13 +++++---- 2 files changed, 24 insertions(+), 32 deletions(-) diff --git a/profile-widget/divetooltipitem.cpp b/profile-widget/divetooltipitem.cpp index e3584f42b..de5f7f80f 100644 --- a/profile-widget/divetooltipitem.cpp +++ b/profile-widget/divetooltipitem.cpp @@ -8,37 +8,30 @@ #include #include "core/qthelper.h" -void ToolTipItem::addToolTip(const QString &toolTip, const QPixmap &pixmap) +ToolTipItem::ToolTip ToolTipItem::makeToolTip(const QString &toolTip, const QPixmap &pixmap) { const IconMetrics &iconMetrics = defaultIconMetrics(); - QGraphicsPixmapItem *iconItem = 0; double yValue = title->boundingRect().height() + iconMetrics.spacing; - Q_FOREACH (ToolTip t, toolTips) { - yValue += t.second->boundingRect().height(); - } - if (entryToolTip.second) { - yValue += entryToolTip.second->boundingRect().height(); - } - iconItem = new QGraphicsPixmapItem(this); + for (auto &[pixmap, text]: toolTips) + yValue += text->boundingRect().height(); + if (entryToolTip.text) + yValue += entryToolTip.text->boundingRect().height(); + std::unique_ptr iconItem = std::make_unique(this); if (!pixmap.isNull()) iconItem->setPixmap(pixmap); const int sp2 = iconMetrics.spacing * 2; iconItem->setPos(sp2, yValue); - QGraphicsSimpleTextItem *textItem = new QGraphicsSimpleTextItem(toolTip, this); + auto textItem = std::make_unique(toolTip, this); textItem->setPos(sp2 + iconMetrics.sz_small + sp2, yValue); textItem->setBrush(QBrush(Qt::white)); textItem->setFlag(ItemIgnoresTransformations); - toolTips.push_back(qMakePair(iconItem, textItem)); + return { std::move(iconItem), std::move(textItem) }; } void ToolTipItem::clear() { - Q_FOREACH (ToolTip t, toolTips) { - delete t.first; - delete t.second; - } toolTips.clear(); } @@ -77,14 +70,14 @@ void ToolTipItem::expand() const IconMetrics &iconMetrics = defaultIconMetrics(); double width = 0, height = title->boundingRect().height() + iconMetrics.spacing; - Q_FOREACH (const ToolTip &t, toolTips) { - QRectF sRect = t.second->boundingRect(); + for (const auto &[pixmap, text]: toolTips) { + QRectF sRect = text->boundingRect(); if (sRect.width() > width) width = sRect.width(); height += sRect.height(); } - QRectF sRect = entryToolTip.second->boundingRect(); + QRectF sRect = entryToolTip.text->boundingRect(); if (sRect.width() > width) width = sRect.width(); height += sRect.height(); @@ -99,7 +92,7 @@ void ToolTipItem::expand() if (width < title->boundingRect().width() + sp2) width = title->boundingRect().width() + sp2; // clip the height - const int minH = lrint(entryToolTip.first->y() + entryToolTip.first->pixmap().height() + sp2); + const int minH = lrint(entryToolTip.pixmap->y() + entryToolTip.pixmap->pixmap().height() + sp2); if (height < minH) height = minH; @@ -130,8 +123,6 @@ ToolTipItem::ToolTipItem(QGraphicsItem *parent) : RoundRectItem(8.0, parent), lastTime(-1) { clearPlotInfo(); - entryToolTip.first = NULL; - entryToolTip.second = NULL; setFlags(ItemIgnoresTransformations | ItemIsMovable | ItemClipsChildrenToShape); QColor c = QColor(Qt::black); @@ -140,9 +131,7 @@ ToolTipItem::ToolTipItem(QGraphicsItem *parent) : RoundRectItem(8.0, parent), setZValue(99); - addToolTip(QString(), QPixmap(16,60)); - entryToolTip = toolTips.first(); - toolTips.clear(); + entryToolTip = makeToolTip(QString(), QPixmap(16,60)); title->setFlag(ItemIgnoresTransformations); title->setPen(QPen(Qt::white, 1)); @@ -245,15 +234,15 @@ void ToolTipItem::refresh(const dive *d, const QPointF &pos, bool inPlanner) painter.setPen(QColor(0, 0, 0, 127)); for (int i = 0; i < 16; i++) painter.drawLine(i, 60, i, 60 - entry->percentages[i] / 2); - entryToolTip.second->setText(QString::fromUtf8(mb.buffer, mb.len)); + entryToolTip.text->setText(QString::fromUtf8(mb.buffer, mb.len)); } - entryToolTip.first->setPixmap(tissues); + entryToolTip.pixmap->setPixmap(tissues); const auto l = scene()->items(pos, Qt::IntersectsItemBoundingRect, Qt::DescendingOrder, scene()->views().first()->transform()); for (QGraphicsItem *item: l) { if (!item->toolTip().isEmpty()) - addToolTip(item->toolTip(), QPixmap()); + toolTips.push_back(makeToolTip(item->toolTip(), QPixmap())); } expand(); } diff --git a/profile-widget/divetooltipitem.h b/profile-widget/divetooltipitem.h index aa7155463..b6e97c630 100644 --- a/profile-widget/divetooltipitem.h +++ b/profile-widget/divetooltipitem.h @@ -2,12 +2,12 @@ #ifndef DIVETOOLTIPITEM_H #define DIVETOOLTIPITEM_H -#include -#include #include #include #include #include +#include +#include #include "backend-shared/roundrectitem.h" #include "core/profile.h" @@ -45,8 +45,11 @@ slots: void setRect(const QRectF &rect); private: - typedef QPair ToolTip; - QVector toolTips; + struct ToolTip { + std::unique_ptr pixmap; + std::unique_ptr text; + }; + std::vector toolTips; ToolTip entryToolTip; QGraphicsSimpleTextItem *title; Status status; @@ -60,7 +63,7 @@ private: QElapsedTimer refreshTime; QList oldSelection; - void addToolTip(const QString &toolTip, const QPixmap &pixmap); + ToolTip makeToolTip(const QString &toolTip, const QPixmap &pixmap); void collapse(); void expand(); void clear();