wiRedPanda
Logic Circuit Simulator
Loading...
Searching...
No Matches
PortHoverLabel.cpp
Go to the documentation of this file.
1// Copyright 2015 - 2026, GIBIS-UNIFESP and the wiRedPanda contributors
2// SPDX-License-Identifier: GPL-3.0-or-later
3
5
6#include <QFontMetricsF>
7#include <QPainter>
8#include <QToolTip>
9
11
12namespace {
13constexpr qreal kPaddingX = 5.0;
14constexpr qreal kPaddingY = 2.0;
16constexpr qreal kPortGap = 8.0;
18constexpr qreal kHoverLabelZ = 1000.0;
19} // namespace
20
21PortHoverLabel::PortHoverLabel(const QString &text, const Side side, QGraphicsItem *parent)
22 : QGraphicsObject(parent)
23 , m_text(text)
24{
25 // Match the native tooltip font so the chip is indistinguishable from the hovered
26 // port's own QToolTip.
27 m_font = QToolTip::font();
28
29 const QFontMetricsF metrics(m_font);
30 const qreal width = metrics.horizontalAdvance(m_text) + (2.0 * kPaddingX);
31 const qreal height = metrics.height() + (2.0 * kPaddingY);
32
33 // The item is anchored at the port (origin); offset the drawn rect to the side away
34 // from the element body, centred on the port along the perpendicular axis.
35 switch (side) {
36 case Side::Left:
37 m_bounds = QRectF(-(kPortGap + width), -height / 2.0, width, height);
38 break;
39 case Side::Right:
40 m_bounds = QRectF(kPortGap, -height / 2.0, width, height);
41 break;
42 case Side::Top:
43 m_bounds = QRectF(-width / 2.0, -(kPortGap + height), width, height);
44 break;
45 case Side::Bottom:
46 m_bounds = QRectF(-width / 2.0, kPortGap, width, height);
47 break;
48 }
49
50 // Keep a constant on-screen size regardless of zoom, exactly like a tooltip widget.
51 setFlag(QGraphicsItem::ItemIgnoresTransformations, true);
52 setZValue(kHoverLabelZ);
53 setAcceptedMouseButtons(Qt::NoButton);
54}
55
57{
58 // Inflate by the 1px pen so the border is never clipped.
59 return m_bounds.adjusted(-1.0, -1.0, 1.0, 1.0);
60}
61
62QPainterPath PortHoverLabel::shape() const
63{
64 // Purely decorative: stay out of scene hit-testing (Scene::itemAt(), rubber-band
65 // selection, etc.) so the chip can never shadow a port, wire, or element underneath it.
66 return {};
67}
68
69void PortHoverLabel::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
70{
71 Q_UNUSED(option)
72 Q_UNUSED(widget)
73
74 const auto attributes = ThemeManager::attributes();
75
76 // Square 1px border + flat fill, mirroring the application's QToolTip stylesheet.
77 painter->setPen(QPen(attributes.m_portHoverLabelText, 1.0));
78 painter->setBrush(attributes.m_portHoverLabelBg);
79 painter->drawRect(m_bounds);
80
81 painter->setFont(m_font);
82 painter->setPen(attributes.m_portHoverLabelText);
83 painter->drawText(m_bounds, Qt::AlignCenter, m_text);
84}
PortHoverLabel: a transient tooltip-style chip showing a port's label on the canvas.
Theme management types and singleton ThemeManager.
QString text() const
Returns the label text (the connected port's name).
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
PortHoverLabel(const QString &text, Side side, QGraphicsItem *parent=nullptr)
Constructs a label chip showing text, anchored at the item's origin (the port).
QPainterPath shape() const override
QRectF boundingRect() const override
Side
Which side of the port the chip is biased towards, away from the element body.
static const ThemeAttributes & attributes()
Returns the current ThemeAttributes color set.