]>
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 | ||
9e40e83d JH |
38 | const char *const ProbeNames[] = { |
39 | "CLK", | |
40 | "DATA", | |
41 | "IN", | |
42 | "OUT", | |
43 | "RST", | |
44 | "Tx", | |
45 | "Rx", | |
46 | "EN", | |
47 | "SCLK", | |
48 | "MOSI", | |
49 | "MISO", | |
50 | "/SS", | |
51 | "SDA", | |
52 | "SCL" | |
53 | }; | |
54 | ||
cec48d16 JH |
55 | Signal::Signal(const sr_probe *const probe) : |
56 | _probe(probe), | |
57 | _name(probe->name), | |
9e40e83d JH |
58 | _v_offset(0), |
59 | _name_action(NULL), | |
60 | _name_widget(), | |
61 | _updating_name_widget(false) | |
28a4c9c5 | 62 | { |
cec48d16 | 63 | assert(_probe); |
9e40e83d JH |
64 | |
65 | _name_action.setDefaultWidget(&_name_widget); | |
66 | ||
67 | _name_widget.setEditable(true); | |
68 | for(unsigned int i = 0; i < countof(ProbeNames); i++) | |
69 | _name_widget.insertItem(i, ProbeNames[i]); | |
70 | _name_widget.setEditText(probe->name); | |
71 | ||
72 | connect(&_name_widget, SIGNAL(editTextChanged(const QString&)), | |
73 | this, SLOT(on_text_changed(const QString&))); | |
28a4c9c5 JH |
74 | } |
75 | ||
76 | QString Signal::get_name() const | |
77 | { | |
78 | return _name; | |
79 | } | |
3e46726a | 80 | |
49f8ff3f JH |
81 | void Signal::set_name(QString name) |
82 | { | |
83 | _name = name; | |
9e40e83d JH |
84 | _updating_name_widget = true; |
85 | _name_widget.setEditText(name); | |
86 | _updating_name_widget = false; | |
49f8ff3f JH |
87 | } |
88 | ||
b3b57abc JH |
89 | QColor Signal::get_colour() const |
90 | { | |
91 | return _colour; | |
92 | } | |
93 | ||
94 | void Signal::set_colour(QColor colour) | |
95 | { | |
96 | _colour = colour; | |
97 | } | |
98 | ||
2e575351 JH |
99 | int Signal::get_v_offset() const |
100 | { | |
101 | return _v_offset; | |
102 | } | |
103 | ||
104 | void Signal::set_v_offset(int v_offset) | |
105 | { | |
106 | _v_offset = v_offset; | |
107 | } | |
108 | ||
2658961b | 109 | void Signal::paint_label(QPainter &p, int y, int right, bool hover) |
3b258424 | 110 | { |
b3b57abc | 111 | p.setBrush(_colour); |
3b258424 | 112 | |
cec48d16 JH |
113 | if (!_probe->enabled) |
114 | return; | |
115 | ||
3b258424 | 116 | const QColor colour = get_colour(); |
ed6f0f4f JH |
117 | |
118 | compute_text_size(p); | |
2658961b | 119 | const QRectF label_rect = get_label_rect(y, right); |
3e46726a JH |
120 | |
121 | // Paint the label | |
122 | const QPointF points[] = { | |
123 | label_rect.topLeft(), | |
124 | label_rect.topRight(), | |
2658961b | 125 | QPointF(right, y), |
3e46726a JH |
126 | label_rect.bottomRight(), |
127 | label_rect.bottomLeft() | |
128 | }; | |
129 | ||
c9b6acc1 JH |
130 | const QPointF highlight_points[] = { |
131 | QPointF(label_rect.left() + 1, label_rect.top() + 1), | |
132 | QPointF(label_rect.right(), label_rect.top() + 1), | |
2658961b | 133 | QPointF(right - 1, y), |
c9b6acc1 JH |
134 | QPointF(label_rect.right(), label_rect.bottom() - 1), |
135 | QPointF(label_rect.left() + 1, label_rect.bottom() - 1) | |
136 | }; | |
137 | ||
f1283456 | 138 | if (selected()) { |
d09674d4 | 139 | p.setPen(highlight_pen()); |
e3374498 JH |
140 | p.setBrush(Qt::transparent); |
141 | p.drawPolygon(points, countof(points)); | |
142 | } | |
143 | ||
c9b6acc1 | 144 | p.setPen(Qt::transparent); |
a29bb7fb | 145 | p.setBrush(hover ? colour.lighter() : colour); |
3e46726a JH |
146 | p.drawPolygon(points, countof(points)); |
147 | ||
c9b6acc1 JH |
148 | p.setPen(colour.lighter()); |
149 | p.setBrush(Qt::transparent); | |
150 | p.drawPolygon(highlight_points, countof(highlight_points)); | |
151 | ||
152 | p.setPen(colour.darker()); | |
153 | p.setBrush(Qt::transparent); | |
154 | p.drawPolygon(points, countof(points)); | |
155 | ||
3e46726a JH |
156 | // Paint the text |
157 | p.setPen((colour.lightness() > 64) ? Qt::black : Qt::white); | |
3b258424 | 158 | p.drawText(label_rect, Qt::AlignCenter | Qt::AlignVCenter, _name); |
3e46726a | 159 | } |
51e77110 | 160 | |
2658961b JH |
161 | bool Signal::pt_in_label_rect(int y, int left, int right, |
162 | const QPoint &point) | |
a29bb7fb | 163 | { |
94fe58ef UH |
164 | (void)left; |
165 | ||
2658961b | 166 | const QRectF label = get_label_rect(y, right); |
a29bb7fb JH |
167 | return QRectF( |
168 | QPointF(label.left() - LabelHitPadding, | |
169 | label.top() - LabelHitPadding), | |
2658961b JH |
170 | QPointF(right, label.bottom() + LabelHitPadding) |
171 | ).contains(point); | |
a29bb7fb JH |
172 | } |
173 | ||
3ef6182a JH |
174 | void Signal::paint_axis(QPainter &p, int y, int left, int right) |
175 | { | |
176 | p.setPen(SignalAxisPen); | |
177 | p.drawLine(QPointF(left, y + 0.5f), QPointF(right, y + 0.5f)); | |
178 | } | |
179 | ||
ed6f0f4f | 180 | void Signal::compute_text_size(QPainter &p) |
a29bb7fb | 181 | { |
5ce37f0f JH |
182 | _text_size = QSize( |
183 | p.boundingRect(QRectF(), 0, _name).width(), | |
184 | p.boundingRect(QRectF(), 0, "Tg").height()); | |
ed6f0f4f | 185 | } |
a29bb7fb | 186 | |
2658961b | 187 | QRectF Signal::get_label_rect(int y, int right) |
ed6f0f4f | 188 | { |
2e04f9bd JH |
189 | using pv::view::View; |
190 | ||
a29bb7fb | 191 | const QSizeF label_size( |
2e04f9bd | 192 | _text_size.width() + View::LabelPadding.width() * 2, |
b47d951e | 193 | ceilf((_text_size.height() + View::LabelPadding.height() * 2) / 2) * 2); |
a29bb7fb JH |
194 | const float label_arrow_length = label_size.height() / 2; |
195 | return QRectF( | |
2658961b JH |
196 | right - label_arrow_length - label_size.width() - 0.5, |
197 | y + 0.5f - label_size.height() / 2, | |
a29bb7fb JH |
198 | label_size.width(), label_size.height()); |
199 | } | |
200 | ||
9e40e83d JH |
201 | void Signal::on_text_changed(const QString &text) |
202 | { | |
203 | _name = text; | |
204 | text_changed(); | |
205 | } | |
206 | ||
8d634081 | 207 | } // namespace view |
51e77110 | 208 | } // namespace pv |