]> sigrok.org Git - pulseview.git/blame - pv/signal.cpp
Cache signal label text size at draw time for later use
[pulseview.git] / pv / signal.cpp
CommitLineData
28a4c9c5 1/*
b3f22de0 2 * This file is part of the PulseView project.
28a4c9c5
JH
3 *
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
cef18fc6 21#include <extdef.h>
28a4c9c5 22
cef18fc6 23#include "signal.h"
3e46726a 24
51e77110
JH
25namespace pv {
26
3e46726a 27const QSizeF Signal::LabelPadding(4, 0);
a29bb7fb 28const int Signal::LabelHitPadding = 2;
28a4c9c5 29
2858b391
JH
30Signal::Signal(QString name) :
31 _name(name)
28a4c9c5
JH
32{
33}
34
35QString Signal::get_name() const
36{
37 return _name;
38}
3e46726a 39
a29bb7fb 40void Signal::paint_label(QPainter &p, const QRect &rect, bool hover)
3b258424
JH
41{
42 p.setBrush(get_colour());
43
44 const QColor colour = get_colour();
45 const float nominal_offset = get_nominal_offset(rect);
ed6f0f4f
JH
46
47 compute_text_size(p);
48 const QRectF label_rect = get_label_rect(rect);
3e46726a
JH
49
50 // Paint the label
51 const QPointF points[] = {
52 label_rect.topLeft(),
53 label_rect.topRight(),
54 QPointF(rect.right(), nominal_offset),
55 label_rect.bottomRight(),
56 label_rect.bottomLeft()
57 };
58
c9b6acc1
JH
59 const QPointF highlight_points[] = {
60 QPointF(label_rect.left() + 1, label_rect.top() + 1),
61 QPointF(label_rect.right(), label_rect.top() + 1),
62 QPointF(rect.right() - 1, nominal_offset),
63 QPointF(label_rect.right(), label_rect.bottom() - 1),
64 QPointF(label_rect.left() + 1, label_rect.bottom() - 1)
65 };
66
67 p.setPen(Qt::transparent);
a29bb7fb 68 p.setBrush(hover ? colour.lighter() : colour);
3e46726a
JH
69 p.drawPolygon(points, countof(points));
70
c9b6acc1
JH
71 p.setPen(colour.lighter());
72 p.setBrush(Qt::transparent);
73 p.drawPolygon(highlight_points, countof(highlight_points));
74
75 p.setPen(colour.darker());
76 p.setBrush(Qt::transparent);
77 p.drawPolygon(points, countof(points));
78
3e46726a
JH
79 // Paint the text
80 p.setPen((colour.lightness() > 64) ? Qt::black : Qt::white);
3b258424 81 p.drawText(label_rect, Qt::AlignCenter | Qt::AlignVCenter, _name);
3e46726a 82}
51e77110 83
ed6f0f4f 84bool Signal::pt_in_label_rect(const QRect &rect, const QPoint &point)
a29bb7fb 85{
ed6f0f4f 86 const QRectF label = get_label_rect(rect);
a29bb7fb
JH
87 return QRectF(
88 QPointF(label.left() - LabelHitPadding,
89 label.top() - LabelHitPadding),
90 QPointF(rect.right(),
91 label.bottom() + LabelHitPadding)
92 ).contains(point);
93}
94
ed6f0f4f 95void Signal::compute_text_size(QPainter &p)
a29bb7fb 96{
ed6f0f4f
JH
97 _text_size = p.boundingRect(QRectF(), 0, _name).size();
98}
a29bb7fb 99
ed6f0f4f
JH
100QRectF Signal::get_label_rect(const QRect &rect)
101{
a29bb7fb
JH
102 const float nominal_offset = get_nominal_offset(rect);
103 const QSizeF label_size(
ed6f0f4f
JH
104 _text_size.width() + LabelPadding.width() * 2,
105 _text_size.height() + LabelPadding.height() * 2);
a29bb7fb
JH
106 const float label_arrow_length = label_size.height() / 2;
107 return QRectF(
108 rect.right() - label_arrow_length - label_size.width(),
109 nominal_offset - label_size.height() / 2,
110 label_size.width(), label_size.height());
111}
112
51e77110 113} // namespace pv