25 const QColor& textColor,
26 const QColor& bgColor,
27 VIS::TextAnchor anchor,
29 QGraphicsItem* parent =
nullptr)
30 : QGraphicsItem(parent)
33 , m_textColor(textColor)
38 setFlag(QGraphicsItem::ItemIgnoresTransformations,
true);
42 QRectF boundingRect()
const override {
return m_rect; }
44 void paint(QPainter* painter,
45 const QStyleOptionGraphicsItem*,
48 painter->setRenderHint(QPainter::Antialiasing,
true);
49 painter->setRenderHint(QPainter::TextAntialiasing,
true);
50 painter->setFont(m_font);
53 painter->setPen(Qt::NoPen);
54 painter->setBrush(m_bgColor);
55 painter->drawRect(m_rect);
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,
69 QFontMetrics fm(m_font);
71 QRectF textRect = fm.boundingRect(m_text);
74 QRectF boxRect = textRect.adjusted(-m_padding,
82 case VIS::TextAnchor::TopLeft:
83 anchorPoint = boxRect.topLeft();
85 case VIS::TextAnchor::TopCenter:
86 anchorPoint = QPointF(boxRect.center().x(), boxRect.top());
88 case VIS::TextAnchor::TopRight:
89 anchorPoint = boxRect.topRight();
91 case VIS::TextAnchor::CenterLeft:
92 anchorPoint = QPointF(boxRect.left(), boxRect.center().y());
94 case VIS::TextAnchor::Center:
95 anchorPoint = boxRect.center();
97 case VIS::TextAnchor::CenterRight:
98 anchorPoint = QPointF(boxRect.right(), boxRect.center().y());
100 case VIS::TextAnchor::BottomLeft:
101 anchorPoint = boxRect.bottomLeft();
103 case VIS::TextAnchor::BottomCenter:
104 anchorPoint = QPointF(boxRect.center().x(), boxRect.bottom());
106 case VIS::TextAnchor::BottomRight:
107 anchorPoint = boxRect.bottomRight();
112 m_rect = boxRect.translated(-anchorPoint);
120 VIS::TextAnchor m_anchor;
QGraphicsItem that renders a text overlay with background and padding.
Definition textoverlayitem.h:21