]>
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 | |
cec48d16 | 23 | #include <assert.h> |
b47d951e JH |
24 | #include <math.h> |
25 | ||
e3374498 JH |
26 | #include <QApplication> |
27 | ||
cef18fc6 | 28 | #include "signal.h" |
8d634081 | 29 | #include "view.h" |
3e46726a | 30 | |
51e77110 | 31 | namespace pv { |
8d634081 | 32 | namespace view { |
51e77110 | 33 | |
a29bb7fb | 34 | const int Signal::LabelHitPadding = 2; |
e3374498 | 35 | const int Signal::LabelHighlightRadius = 6; |
28a4c9c5 | 36 | |
3ef6182a JH |
37 | const QPen Signal::SignalAxisPen(QColor(128, 128, 128, 64)); |
38 | ||
cec48d16 JH |
39 | Signal::Signal(const sr_probe *const probe) : |
40 | _probe(probe), | |
41 | _name(probe->name), | |
f1283456 | 42 | _v_offset(0) |
28a4c9c5 | 43 | { |
cec48d16 | 44 | assert(_probe); |
28a4c9c5 JH |
45 | } |
46 | ||
47 | QString Signal::get_name() const | |
48 | { | |
49 | return _name; | |
50 | } | |
3e46726a | 51 | |
49f8ff3f JH |
52 | void Signal::set_name(QString name) |
53 | { | |
54 | _name = name; | |
55 | } | |
56 | ||
b3b57abc JH |
57 | QColor Signal::get_colour() const |
58 | { | |
59 | return _colour; | |
60 | } | |
61 | ||
62 | void Signal::set_colour(QColor colour) | |
63 | { | |
64 | _colour = colour; | |
65 | } | |
66 | ||
2e575351 JH |
67 | int Signal::get_v_offset() const |
68 | { | |
69 | return _v_offset; | |
70 | } | |
71 | ||
72 | void Signal::set_v_offset(int v_offset) | |
73 | { | |
74 | _v_offset = v_offset; | |
75 | } | |
76 | ||
2658961b | 77 | void Signal::paint_label(QPainter &p, int y, int right, bool hover) |
3b258424 | 78 | { |
b3b57abc | 79 | p.setBrush(_colour); |
3b258424 | 80 | |
cec48d16 JH |
81 | if (!_probe->enabled) |
82 | return; | |
83 | ||
3b258424 | 84 | const QColor colour = get_colour(); |
ed6f0f4f JH |
85 | |
86 | compute_text_size(p); | |
2658961b | 87 | const QRectF label_rect = get_label_rect(y, right); |
3e46726a JH |
88 | |
89 | // Paint the label | |
90 | const QPointF points[] = { | |
91 | label_rect.topLeft(), | |
92 | label_rect.topRight(), | |
2658961b | 93 | QPointF(right, y), |
3e46726a JH |
94 | label_rect.bottomRight(), |
95 | label_rect.bottomLeft() | |
96 | }; | |
97 | ||
c9b6acc1 JH |
98 | const QPointF highlight_points[] = { |
99 | QPointF(label_rect.left() + 1, label_rect.top() + 1), | |
100 | QPointF(label_rect.right(), label_rect.top() + 1), | |
2658961b | 101 | QPointF(right - 1, y), |
c9b6acc1 JH |
102 | QPointF(label_rect.right(), label_rect.bottom() - 1), |
103 | QPointF(label_rect.left() + 1, label_rect.bottom() - 1) | |
104 | }; | |
105 | ||
f1283456 | 106 | if (selected()) { |
e3374498 JH |
107 | p.setPen(QPen(QApplication::palette().brush( |
108 | QPalette::Highlight), LabelHighlightRadius, | |
109 | Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); | |
110 | p.setBrush(Qt::transparent); | |
111 | p.drawPolygon(points, countof(points)); | |
112 | } | |
113 | ||
c9b6acc1 | 114 | p.setPen(Qt::transparent); |
a29bb7fb | 115 | p.setBrush(hover ? colour.lighter() : colour); |
3e46726a JH |
116 | p.drawPolygon(points, countof(points)); |
117 | ||
c9b6acc1 JH |
118 | p.setPen(colour.lighter()); |
119 | p.setBrush(Qt::transparent); | |
120 | p.drawPolygon(highlight_points, countof(highlight_points)); | |
121 | ||
122 | p.setPen(colour.darker()); | |
123 | p.setBrush(Qt::transparent); | |
124 | p.drawPolygon(points, countof(points)); | |
125 | ||
3e46726a JH |
126 | // Paint the text |
127 | p.setPen((colour.lightness() > 64) ? Qt::black : Qt::white); | |
3b258424 | 128 | p.drawText(label_rect, Qt::AlignCenter | Qt::AlignVCenter, _name); |
3e46726a | 129 | } |
51e77110 | 130 | |
2658961b JH |
131 | bool Signal::pt_in_label_rect(int y, int left, int right, |
132 | const QPoint &point) | |
a29bb7fb | 133 | { |
94fe58ef UH |
134 | (void)left; |
135 | ||
2658961b | 136 | const QRectF label = get_label_rect(y, right); |
a29bb7fb JH |
137 | return QRectF( |
138 | QPointF(label.left() - LabelHitPadding, | |
139 | label.top() - LabelHitPadding), | |
2658961b JH |
140 | QPointF(right, label.bottom() + LabelHitPadding) |
141 | ).contains(point); | |
a29bb7fb JH |
142 | } |
143 | ||
3ef6182a JH |
144 | void Signal::paint_axis(QPainter &p, int y, int left, int right) |
145 | { | |
146 | p.setPen(SignalAxisPen); | |
147 | p.drawLine(QPointF(left, y + 0.5f), QPointF(right, y + 0.5f)); | |
148 | } | |
149 | ||
ed6f0f4f | 150 | void Signal::compute_text_size(QPainter &p) |
a29bb7fb | 151 | { |
5ce37f0f JH |
152 | _text_size = QSize( |
153 | p.boundingRect(QRectF(), 0, _name).width(), | |
154 | p.boundingRect(QRectF(), 0, "Tg").height()); | |
ed6f0f4f | 155 | } |
a29bb7fb | 156 | |
2658961b | 157 | QRectF Signal::get_label_rect(int y, int right) |
ed6f0f4f | 158 | { |
2e04f9bd JH |
159 | using pv::view::View; |
160 | ||
a29bb7fb | 161 | const QSizeF label_size( |
2e04f9bd | 162 | _text_size.width() + View::LabelPadding.width() * 2, |
b47d951e | 163 | ceilf((_text_size.height() + View::LabelPadding.height() * 2) / 2) * 2); |
a29bb7fb JH |
164 | const float label_arrow_length = label_size.height() / 2; |
165 | return QRectF( | |
2658961b JH |
166 | right - label_arrow_length - label_size.width() - 0.5, |
167 | y + 0.5f - label_size.height() / 2, | |
a29bb7fb JH |
168 | label_size.width(), label_size.height()); |
169 | } | |
170 | ||
8d634081 | 171 | } // namespace view |
51e77110 | 172 | } // namespace pv |