iVS3D v2.0.9
Loading...
Searching...
No Matches
textoverlayitem.h
Go to the documentation of this file.
1#pragma once
9#include "visualization.h"
10#include <QGraphicsItem>
11#include <QString>
12#include <QFont>
13#include <QColor>
14#include <QRectF>
15#include <QPainter>
16
20class TextOverlayItem : public QGraphicsItem
21{
22public:
23 TextOverlayItem(const QString& text,
24 const QFont& font,
25 const QColor& textColor,
26 const QColor& bgColor,
27 VIS::TextAnchor anchor,
28 qreal padding = 3.0,
29 QGraphicsItem* parent = nullptr)
30 : QGraphicsItem(parent)
31 , m_text(text)
32 , m_font(font)
33 , m_textColor(textColor)
34 , m_bgColor(bgColor)
35 , m_anchor(anchor)
36 , m_padding(padding)
37 {
38 setFlag(QGraphicsItem::ItemIgnoresTransformations, true);
39 updateGeometry();
40 }
41
42 QRectF boundingRect() const override { return m_rect; }
43
44 void paint(QPainter* painter,
45 const QStyleOptionGraphicsItem*,
46 QWidget*) override
47 {
48 painter->setRenderHint(QPainter::Antialiasing, true);
49 painter->setRenderHint(QPainter::TextAntialiasing, true);
50 painter->setFont(m_font);
51
52 // Background
53 painter->setPen(Qt::NoPen);
54 painter->setBrush(m_bgColor);
55 painter->drawRect(m_rect);
56
57 // Text (inside padded area)
58 painter->setPen(m_textColor);
59 QRectF textRect = m_rect.adjusted(m_padding, m_padding,
60 -m_padding, -m_padding);
61 painter->drawText(textRect,
62 Qt::AlignLeft | Qt::AlignVCenter,
63 m_text);
64 }
65
66private:
67 void updateGeometry()
68 {
69 QFontMetrics fm(m_font);
70 // Size of the text in item coordinates (i.e. screen pixels)
71 QRectF textRect = fm.boundingRect(m_text);
72
73 // Add padding around it
74 QRectF boxRect = textRect.adjusted(-m_padding,
75 -m_padding,
76 m_padding,
77 m_padding);
78
79 // Compute anchor point within boxRect
80 QPointF anchorPoint;
81 switch (m_anchor) {
82 case VIS::TextAnchor::TopLeft:
83 anchorPoint = boxRect.topLeft();
84 break;
85 case VIS::TextAnchor::TopCenter:
86 anchorPoint = QPointF(boxRect.center().x(), boxRect.top());
87 break;
88 case VIS::TextAnchor::TopRight:
89 anchorPoint = boxRect.topRight();
90 break;
91 case VIS::TextAnchor::CenterLeft:
92 anchorPoint = QPointF(boxRect.left(), boxRect.center().y());
93 break;
94 case VIS::TextAnchor::Center:
95 anchorPoint = boxRect.center();
96 break;
97 case VIS::TextAnchor::CenterRight:
98 anchorPoint = QPointF(boxRect.right(), boxRect.center().y());
99 break;
100 case VIS::TextAnchor::BottomLeft:
101 anchorPoint = boxRect.bottomLeft();
102 break;
103 case VIS::TextAnchor::BottomCenter:
104 anchorPoint = QPointF(boxRect.center().x(), boxRect.bottom());
105 break;
106 case VIS::TextAnchor::BottomRight:
107 anchorPoint = boxRect.bottomRight();
108 break;
109 }
110
111 // Translate so that anchorPoint becomes (0,0) in item coords.
112 m_rect = boxRect.translated(-anchorPoint);
113 }
114
115private:
116 QString m_text;
117 QFont m_font;
118 QColor m_textColor;
119 QColor m_bgColor;
120 VIS::TextAnchor m_anchor;
121 qreal m_padding;
122 QRectF m_rect; // background rect in item coordinates
123};
QGraphicsItem that renders a text overlay with background and padding.
Definition textoverlayitem.h:21