]>
Commit | Line | Data |
---|---|---|
931f20b0 JH |
1 | /* |
2 | * This file is part of the PulseView project. | |
3 | * | |
4 | * Copyright (C) 2013 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 | ||
21 | #include <extdef.h> | |
22 | ||
23 | #include <assert.h> | |
24 | #include <math.h> | |
25 | ||
d7c0ca4a | 26 | #include <QApplication> |
b213ef09 | 27 | #include <QFormLayout> |
0891e69b | 28 | #include <QKeyEvent> |
b213ef09 JH |
29 | #include <QLineEdit> |
30 | ||
931f20b0 | 31 | #include "trace.h" |
91e8bf08 | 32 | #include "tracepalette.h" |
931f20b0 JH |
33 | #include "view.h" |
34 | ||
91e8bf08 | 35 | #include <pv/widgets/colourbutton.h> |
1f81a394 | 36 | #include <pv/widgets/popup.h> |
569d1e41 | 37 | |
931f20b0 JH |
38 | namespace pv { |
39 | namespace view { | |
40 | ||
fe08b6e8 | 41 | const QPen Trace::AxisPen(QColor(128, 128, 128, 64)); |
931f20b0 JH |
42 | const int Trace::LabelHitPadding = 2; |
43 | ||
83c23cc9 | 44 | Trace::Trace(QString name) : |
9555ca8b | 45 | _view(NULL), |
931f20b0 | 46 | _name(name), |
20eaae39 JH |
47 | _v_offset(0), |
48 | _popup(NULL), | |
49 | _popup_form(NULL) | |
931f20b0 JH |
50 | { |
51 | } | |
52 | ||
53 | QString Trace::get_name() const | |
54 | { | |
55 | return _name; | |
56 | } | |
57 | ||
58 | void Trace::set_name(QString name) | |
59 | { | |
60 | _name = name; | |
61 | } | |
62 | ||
63 | QColor Trace::get_colour() const | |
64 | { | |
65 | return _colour; | |
66 | } | |
67 | ||
68 | void Trace::set_colour(QColor colour) | |
69 | { | |
70 | _colour = colour; | |
71 | } | |
72 | ||
73 | int Trace::get_v_offset() const | |
74 | { | |
75 | return _v_offset; | |
76 | } | |
77 | ||
78 | void Trace::set_v_offset(int v_offset) | |
79 | { | |
80 | _v_offset = v_offset; | |
81 | } | |
82 | ||
01fd3263 | 83 | void Trace::set_view(pv::view::View *view) |
931f20b0 | 84 | { |
01fd3263 | 85 | assert(view); |
9555ca8b SA |
86 | |
87 | if (_view) | |
88 | disconnect(_view, SIGNAL(hover_point_changed()), | |
89 | this, SLOT(on_hover_point_changed())); | |
90 | ||
01fd3263 | 91 | _view = view; |
9555ca8b SA |
92 | |
93 | connect(view, SIGNAL(hover_point_changed()), | |
94 | this, SLOT(on_hover_point_changed())); | |
01fd3263 JH |
95 | } |
96 | ||
fe08b6e8 JH |
97 | void Trace::paint_back(QPainter &p, int left, int right) |
98 | { | |
99 | (void)p; | |
100 | (void)left; | |
101 | (void)right; | |
102 | } | |
103 | ||
104 | void Trace::paint_mid(QPainter &p, int left, int right) | |
105 | { | |
106 | (void)p; | |
107 | (void)left; | |
108 | (void)right; | |
109 | } | |
110 | ||
111 | void Trace::paint_fore(QPainter &p, int left, int right) | |
112 | { | |
113 | (void)p; | |
114 | (void)left; | |
115 | (void)right; | |
116 | } | |
117 | ||
01fd3263 JH |
118 | void Trace::paint_label(QPainter &p, int right, bool hover) |
119 | { | |
120 | assert(_view); | |
121 | const int y = _v_offset - _view->v_offset(); | |
122 | ||
931f20b0 JH |
123 | p.setBrush(_colour); |
124 | ||
125 | if (!enabled()) | |
126 | return; | |
127 | ||
128 | const QColor colour = get_colour(); | |
129 | ||
01fd3263 | 130 | const QRectF label_rect = get_label_rect(right); |
931f20b0 JH |
131 | |
132 | // Paint the label | |
133 | const QPointF points[] = { | |
134 | label_rect.topLeft(), | |
135 | label_rect.topRight(), | |
136 | QPointF(right, y), | |
137 | label_rect.bottomRight(), | |
138 | label_rect.bottomLeft() | |
139 | }; | |
140 | ||
141 | const QPointF highlight_points[] = { | |
142 | QPointF(label_rect.left() + 1, label_rect.top() + 1), | |
143 | QPointF(label_rect.right(), label_rect.top() + 1), | |
144 | QPointF(right - 1, y), | |
145 | QPointF(label_rect.right(), label_rect.bottom() - 1), | |
146 | QPointF(label_rect.left() + 1, label_rect.bottom() - 1) | |
147 | }; | |
148 | ||
149 | if (selected()) { | |
150 | p.setPen(highlight_pen()); | |
151 | p.setBrush(Qt::transparent); | |
152 | p.drawPolygon(points, countof(points)); | |
153 | } | |
154 | ||
155 | p.setPen(Qt::transparent); | |
156 | p.setBrush(hover ? colour.lighter() : colour); | |
157 | p.drawPolygon(points, countof(points)); | |
158 | ||
159 | p.setPen(colour.lighter()); | |
160 | p.setBrush(Qt::transparent); | |
161 | p.drawPolygon(highlight_points, countof(highlight_points)); | |
162 | ||
163 | p.setPen(colour.darker()); | |
164 | p.setBrush(Qt::transparent); | |
165 | p.drawPolygon(points, countof(points)); | |
166 | ||
167 | // Paint the text | |
410f9c81 | 168 | p.setPen(get_text_colour()); |
d7c0ca4a | 169 | p.setFont(QApplication::font()); |
931f20b0 JH |
170 | p.drawText(label_rect, Qt::AlignCenter | Qt::AlignVCenter, _name); |
171 | } | |
172 | ||
01fd3263 | 173 | bool Trace::pt_in_label_rect(int left, int right, const QPoint &point) |
931f20b0 JH |
174 | { |
175 | (void)left; | |
176 | ||
01fd3263 | 177 | const QRectF label = get_label_rect(right); |
32d83ce1 | 178 | return enabled() && QRectF( |
931f20b0 JH |
179 | QPointF(label.left() - LabelHitPadding, |
180 | label.top() - LabelHitPadding), | |
181 | QPointF(right, label.bottom() + LabelHitPadding) | |
182 | ).contains(point); | |
183 | } | |
184 | ||
9b6378f1 JH |
185 | QMenu* Trace::create_context_menu(QWidget *parent) |
186 | { | |
187 | QMenu *const menu = SelectableItem::create_context_menu(parent); | |
188 | ||
9b6378f1 JH |
189 | return menu; |
190 | } | |
191 | ||
569d1e41 JH |
192 | pv::widgets::Popup* Trace::create_popup(QWidget *parent) |
193 | { | |
194 | using pv::widgets::Popup; | |
20eaae39 JH |
195 | |
196 | _popup = new Popup(parent); | |
20eaae39 | 197 | |
37fd11b1 | 198 | create_popup_form(); |
20eaae39 JH |
199 | |
200 | connect(_popup, SIGNAL(closed()), | |
201 | this, SLOT(on_popup_closed())); | |
202 | ||
203 | return _popup; | |
569d1e41 JH |
204 | } |
205 | ||
fe08b6e8 JH |
206 | int Trace::get_y() const |
207 | { | |
208 | return _v_offset - _view->v_offset(); | |
209 | } | |
210 | ||
d7c0ca4a JH |
211 | QRectF Trace::get_label_rect(int right) |
212 | { | |
213 | using pv::view::View; | |
214 | ||
215 | assert(_view); | |
216 | ||
217 | QFontMetrics m(QApplication::font()); | |
218 | const QSize text_size( | |
219 | m.boundingRect(QRect(), 0, _name).width(), | |
220 | m.boundingRect(QRect(), 0, "Tg").height()); | |
221 | const QSizeF label_size( | |
222 | text_size.width() + View::LabelPadding.width() * 2, | |
223 | ceilf((text_size.height() + View::LabelPadding.height() * 2) / 2) * 2); | |
224 | const float label_arrow_length = label_size.height() / 2; | |
225 | return QRectF( | |
226 | right - label_arrow_length - label_size.width() - 0.5, | |
227 | get_y() + 0.5f - label_size.height() / 2, | |
228 | label_size.width(), label_size.height()); | |
229 | } | |
230 | ||
410f9c81 JH |
231 | QColor Trace::get_text_colour() const |
232 | { | |
233 | return (_colour.lightness() > 64) ? Qt::black : Qt::white; | |
234 | } | |
235 | ||
fe08b6e8 JH |
236 | void Trace::paint_axis(QPainter &p, int y, int left, int right) |
237 | { | |
238 | p.setPen(AxisPen); | |
239 | p.drawLine(QPointF(left, y + 0.5f), QPointF(right, y + 0.5f)); | |
240 | } | |
241 | ||
91e8bf08 JH |
242 | void Trace::add_colour_option(QWidget *parent, QFormLayout *form) |
243 | { | |
244 | using pv::widgets::ColourButton; | |
245 | ||
246 | ColourButton *const colour_button = new ColourButton( | |
247 | TracePalette::Rows, TracePalette::Cols, parent); | |
248 | colour_button->set_palette(TracePalette::Colours); | |
249 | colour_button->set_colour(_colour); | |
250 | connect(colour_button, SIGNAL(selected(const QColor&)), | |
251 | this, SLOT(on_colour_changed(const QColor&))); | |
252 | ||
253 | form->addRow(tr("Colour"), colour_button); | |
254 | } | |
255 | ||
37fd11b1 JH |
256 | void Trace::create_popup_form() |
257 | { | |
258 | // Clear the layout | |
259 | ||
260 | // Transfer the layout and the child widgets to a temporary widget | |
261 | // which then goes out of scope destroying the layout and all the child | |
262 | // widgets. | |
263 | if (_popup_form) | |
264 | QWidget().setLayout(_popup_form); | |
265 | ||
266 | // Repopulate the popup | |
267 | _popup_form = new QFormLayout(_popup); | |
268 | _popup->setLayout(_popup_form); | |
269 | populate_popup_form(_popup, _popup_form); | |
270 | } | |
271 | ||
569d1e41 JH |
272 | void Trace::populate_popup_form(QWidget *parent, QFormLayout *form) |
273 | { | |
68456dab JH |
274 | QLineEdit *const name_edit = new QLineEdit(parent); |
275 | name_edit->setText(_name); | |
276 | connect(name_edit, SIGNAL(textChanged(const QString&)), | |
277 | this, SLOT(on_text_changed(const QString&))); | |
278 | form->addRow(tr("Name"), name_edit); | |
91e8bf08 JH |
279 | |
280 | add_colour_option(parent, form); | |
569d1e41 JH |
281 | } |
282 | ||
9555ca8b SA |
283 | void Trace::hover_point_changed() |
284 | { | |
285 | } | |
286 | ||
20eaae39 JH |
287 | void Trace::on_popup_closed() |
288 | { | |
289 | _popup = NULL; | |
290 | _popup_form = NULL; | |
291 | } | |
292 | ||
68456dab JH |
293 | void Trace::on_text_changed(const QString &text) |
294 | { | |
295 | set_name(text); | |
296 | text_changed(); | |
297 | } | |
9b6378f1 | 298 | |
91e8bf08 JH |
299 | void Trace::on_colour_changed(const QColor &colour) |
300 | { | |
301 | set_colour(colour); | |
302 | colour_changed(); | |
303 | } | |
304 | ||
9555ca8b SA |
305 | void Trace::on_hover_point_changed() |
306 | { | |
307 | hover_point_changed(); | |
308 | } | |
309 | ||
931f20b0 JH |
310 | } // namespace view |
311 | } // namespace pv |