-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathAxisArrowOverlay.cpp
More file actions
157 lines (137 loc) · 5.14 KB
/
Copy pathAxisArrowOverlay.cpp
File metadata and controls
157 lines (137 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright 2026 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// This file is part of eProsima Fast DDS Shapes Demo.
//
// eProsima Fast DDS Shapes Demo is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// eProsima Fast DDS Shapes Demo is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with eProsima Fast DDS Shapes Demo. If not, see <https://www.gnu.org/licenses/>.
#include <eprosimashapesdemo/qt/AxisArrowOverlay.h>
#include <eprosimashapesdemo/shapesdemo/ShapesDemo.h>
#include <QEvent>
#include <QFont>
#include <QFontMetrics>
#include <QPainter>
#include <QPalette>
#include <QPen>
#include <QPolygon>
#include <QResizeEvent>
// Filled-triangle arrowhead dimensions
static const int TRI_LEN = 5; // tip to base (along the axis)
static const int TRI_HALF = 4; // base half-width (perpendicular to axis)
// Origin dot radius
static const int DOT_R = 2;
AxisArrowOverlay::AxisArrowOverlay(
QWidget* parent,
QFrame* innerFrame,
ShapesDemo* sd)
: QWidget(parent)
, mp_fd2(innerFrame)
, mp_sd(sd)
{
setAttribute(Qt::WA_TransparentForMouseEvents);
// Do not fill the background — the parent's composited content shows through
// everywhere we do not explicitly paint, making this overlay truly transparent.
setAttribute(Qt::WA_NoSystemBackground);
setAutoFillBackground(false);
setGeometry(parent->rect());
parent->installEventFilter(this);
m_timerId = startTimer(100);
}
bool AxisArrowOverlay::eventFilter(
QObject* obj,
QEvent* e)
{
if (obj == parent() && e->type() == QEvent::Resize)
{
resize(static_cast<QResizeEvent*>(e)->size());
update();
}
return QWidget::eventFilter(obj, e);
}
void AxisArrowOverlay::timerEvent(
QTimerEvent*)
{
update();
}
void AxisArrowOverlay::paintEvent(
QPaintEvent*)
{
if (!mp_fd2)
{
return;
}
bool invertY = mp_sd ? mp_sd->getOptions().m_invertYAxis : false;
const QRect fd2 = mp_fd2->geometry();
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing, true);
QFont f = font();
f.setPointSize(7);
p.setFont(f);
const QFontMetrics fm(f);
const int lh = fm.height();
const int xw = fm.horizontalAdvance("X") + 2;
const int yw = fm.horizontalAdvance("Y") + 2;
// Label corner positions in the outer margin, anchored to fd2's corners.
const int rightX = fd2.right() + 4;
const int topY = qMax(0, fd2.top() - lh);
const int bottomY = fd2.bottom() - 1;
const int leftX = qMax(0, fd2.left() - yw - 3);
// Follow the theme foreground: black in a light theme, light grey in a dark
// theme. darker(140) tones the dark-theme near-white down to a grey; on the
// light-theme black it has no effect (black cannot be darkened further).
const QColor axisColor = palette().color(QPalette::WindowText).darker(140);
// Draw filled shapes (no outline)
p.setPen(Qt::NoPen);
p.setBrush(axisColor);
// Origin dot at the corner where both axes meet.
// Not inverted: top-left corner. Inverted: bottom-left corner.
const QPoint origin(fd2.left() + 1, invertY ? fd2.bottom() : fd2.top() + 1);
p.drawEllipse(origin, DOT_R, DOT_R);
// X arrowhead: right-pointing filled triangle, tip 1px past fd2's right border.
// Not inverted: aligned with the top border. Inverted: with the bottom border.
{
const int xTip = fd2.right() + 1;
const int yAxis = invertY ? fd2.bottom() : fd2.top();
QPolygon tri;
tri << QPoint(xTip, yAxis)
<< QPoint(xTip - TRI_LEN, yAxis - TRI_HALF)
<< QPoint(xTip - TRI_LEN, yAxis + TRI_HALF);
p.drawPolygon(tri);
}
// Y arrowhead: down/up-pointing filled triangle, tip 1px inside fd2's left border.
// Not inverted: aligned with the bottom border. Inverted: with the top border.
{
const int xTip = fd2.left() + 1;
if (!invertY)
{
QPolygon tri;
tri << QPoint(xTip, fd2.bottom())
<< QPoint(xTip - TRI_HALF, fd2.bottom() - TRI_LEN)
<< QPoint(xTip + TRI_HALF, fd2.bottom() - TRI_LEN);
p.drawPolygon(tri);
}
else
{
QPolygon tri;
tri << QPoint(xTip, fd2.top())
<< QPoint(xTip - TRI_HALF, fd2.top() + TRI_LEN)
<< QPoint(xTip + TRI_HALF, fd2.top() + TRI_LEN);
p.drawPolygon(tri);
}
}
// Axis labels
p.setPen(axisColor);
p.drawText(QRect(rightX, invertY ? bottomY : topY, xw, lh),
Qt::AlignLeft | Qt::AlignTop, "X");
p.drawText(QRect(leftX, invertY ? topY : bottomY, yw, lh),
Qt::AlignLeft | Qt::AlignTop, "Y");
}