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 <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2022-06-30 23:17:03 +02:00
parent f302be8798
commit 1faad2c1f1
2 changed files with 24 additions and 32 deletions

View File

@ -8,37 +8,30 @@
#include <QGraphicsView>
#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<QGraphicsPixmapItem> iconItem = std::make_unique<QGraphicsPixmapItem>(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<QGraphicsSimpleTextItem>(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();
}

View File

@ -2,12 +2,12 @@
#ifndef DIVETOOLTIPITEM_H
#define DIVETOOLTIPITEM_H
#include <QVector>
#include <QPair>
#include <QRectF>
#include <QIcon>
#include <QElapsedTimer>
#include <QPainter>
#include <memory>
#include <vector>
#include "backend-shared/roundrectitem.h"
#include "core/profile.h"
@ -45,8 +45,11 @@ slots:
void setRect(const QRectF &rect);
private:
typedef QPair<QGraphicsPixmapItem *, QGraphicsSimpleTextItem *> ToolTip;
QVector<ToolTip> toolTips;
struct ToolTip {
std::unique_ptr<QGraphicsPixmapItem> pixmap;
std::unique_ptr<QGraphicsSimpleTextItem> text;
};
std::vector<ToolTip> toolTips;
ToolTip entryToolTip;
QGraphicsSimpleTextItem *title;
Status status;
@ -60,7 +63,7 @@ private:
QElapsedTimer refreshTime;
QList<QGraphicsItem*> oldSelection;
void addToolTip(const QString &toolTip, const QPixmap &pixmap);
ToolTip makeToolTip(const QString &toolTip, const QPixmap &pixmap);
void collapse();
void expand();
void clear();