profile: render DiveTextItem onto a pixmap

The DiveTextItems were redrawn on every paint() call. This was
a prohibitively expensive operation (converting the text into
a path, drawing an outline, etc.), which was called numerous
times.

Instead, render the text only when changing into a QPixmap
and blit that pixmap in the paint() call.

This will make it possible to do absolutely positioned
DiveTextItems. So far they were placed relatively in
scene coordinates ranging from 0-100(!).

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2021-08-22 19:48:49 +02:00 committed by Dirk Hohndel
parent 04e0d96bae
commit 15f20961c7
2 changed files with 48 additions and 49 deletions

View file

@ -4,29 +4,28 @@
#include <QObject>
#include <QFont>
#include <QGraphicsItemGroup>
#include <QGraphicsPixmapItem>
class QBrush;
/* A Line Item that has animated-properties. */
class DiveTextItem : public QObject, public QGraphicsItemGroup {
class DiveTextItem : public QObject, public QGraphicsPixmapItem {
Q_OBJECT
Q_PROPERTY(QPointF pos READ pos WRITE setPos)
Q_PROPERTY(qreal opacity READ opacity WRITE setOpacity)
public:
// Note: vertical centring is based on the actual rendered text, not on the font metrics.
// This is fine for placing text in the "tankbar", but it will look disastrous when
// placing text items next to each other. This may have to be fixed.
DiveTextItem(double dpr, double scale, int alignFlags, QGraphicsItem *parent);
void set(const QString &text, const QBrush &brush);
const QString &text();
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
static QFont getFont(double dpr, double scale);
static double fontHeight(double dpr, double scale);
double height() const;
private:
void updateText();
int internalAlignFlags;
QGraphicsPathItem *textBackgroundItem;
QGraphicsPathItem *textItem;
QString internalText;
double dpr;
double scale;