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