]>
Commit | Line | Data |
---|---|---|
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 |
25 | namespace pv { |
26 | ||
a29bb7fb | 27 | const int Signal::LabelHitPadding = 2; |
28a4c9c5 | 28 | |
2858b391 JH |
29 | Signal::Signal(QString name) : |
30 | _name(name) | |
28a4c9c5 JH |
31 | { |
32 | } | |
33 | ||
34 | QString Signal::get_name() const | |
35 | { | |
36 | return _name; | |
37 | } | |
3e46726a | 38 | |
49f8ff3f JH |
39 | void Signal::set_name(QString name) |
40 | { | |
41 | _name = name; | |
42 | } | |
43 | ||
a29bb7fb | 44 | void Signal::paint_label(QPainter &p, const QRect &rect, bool hover) |
3b258424 JH |
45 | { |
46 | p.setBrush(get_colour()); | |
47 | ||
48 | const QColor colour = get_colour(); | |
49 | const float nominal_offset = get_nominal_offset(rect); | |
ed6f0f4f JH |
50 | |
51 | compute_text_size(p); | |
52 | const QRectF label_rect = get_label_rect(rect); | |
3e46726a JH |
53 | |
54 | // Paint the label | |
55 | const QPointF points[] = { | |
56 | label_rect.topLeft(), | |
57 | label_rect.topRight(), | |
58 | QPointF(rect.right(), nominal_offset), | |
59 | label_rect.bottomRight(), | |
60 | label_rect.bottomLeft() | |
61 | }; | |
62 | ||
c9b6acc1 JH |
63 | const QPointF highlight_points[] = { |
64 | QPointF(label_rect.left() + 1, label_rect.top() + 1), | |
65 | QPointF(label_rect.right(), label_rect.top() + 1), | |
66 | QPointF(rect.right() - 1, nominal_offset), | |
67 | QPointF(label_rect.right(), label_rect.bottom() - 1), | |
68 | QPointF(label_rect.left() + 1, label_rect.bottom() - 1) | |
69 | }; | |
70 | ||
71 | p.setPen(Qt::transparent); | |
a29bb7fb | 72 | p.setBrush(hover ? colour.lighter() : colour); |
3e46726a JH |
73 | p.drawPolygon(points, countof(points)); |
74 | ||
c9b6acc1 JH |
75 | p.setPen(colour.lighter()); |
76 | p.setBrush(Qt::transparent); | |
77 | p.drawPolygon(highlight_points, countof(highlight_points)); | |
78 | ||
79 | p.setPen(colour.darker()); | |
80 | p.setBrush(Qt::transparent); | |
81 | p.drawPolygon(points, countof(points)); | |
82 | ||
3e46726a JH |
83 | // Paint the text |
84 | p.setPen((colour.lightness() > 64) ? Qt::black : Qt::white); | |
3b258424 | 85 | p.drawText(label_rect, Qt::AlignCenter | Qt::AlignVCenter, _name); |
3e46726a | 86 | } |
51e77110 | 87 | |
ed6f0f4f | 88 | bool Signal::pt_in_label_rect(const QRect &rect, const QPoint &point) |
a29bb7fb | 89 | { |
ed6f0f4f | 90 | const QRectF label = get_label_rect(rect); |
a29bb7fb JH |
91 | return QRectF( |
92 | QPointF(label.left() - LabelHitPadding, | |
93 | label.top() - LabelHitPadding), | |
94 | QPointF(rect.right(), | |
95 | label.bottom() + LabelHitPadding) | |
96 | ).contains(point); | |
97 | } | |
98 | ||
ed6f0f4f | 99 | void Signal::compute_text_size(QPainter &p) |
a29bb7fb | 100 | { |
ed6f0f4f JH |
101 | _text_size = p.boundingRect(QRectF(), 0, _name).size(); |
102 | } | |
a29bb7fb | 103 | |
ed6f0f4f JH |
104 | QRectF Signal::get_label_rect(const QRect &rect) |
105 | { | |
2e04f9bd JH |
106 | using pv::view::View; |
107 | ||
a29bb7fb JH |
108 | const float nominal_offset = get_nominal_offset(rect); |
109 | const QSizeF label_size( | |
2e04f9bd JH |
110 | _text_size.width() + View::LabelPadding.width() * 2, |
111 | _text_size.height() + View::LabelPadding.height() * 2); | |
a29bb7fb JH |
112 | const float label_arrow_length = label_size.height() / 2; |
113 | return QRectF( | |
114 | rect.right() - label_arrow_length - label_size.width(), | |
115 | nominal_offset - label_size.height() / 2, | |
116 | label_size.width(), label_size.height()); | |
117 | } | |
118 | ||
51e77110 | 119 | } // namespace pv |