]>
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 | |
efdec55a | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
931f20b0 JH |
18 | */ |
19 | ||
20 | #include <extdef.h> | |
21 | ||
eb8269e3 | 22 | #include <cassert> |
d9e71737 | 23 | #include <cmath> |
931f20b0 | 24 | |
d7c0ca4a | 25 | #include <QApplication> |
b213ef09 | 26 | #include <QFormLayout> |
0891e69b | 27 | #include <QKeyEvent> |
b213ef09 | 28 | #include <QLineEdit> |
79b53a1a | 29 | #include <QMenu> |
b213ef09 | 30 | |
79b53a1a | 31 | #include "ruler.hpp" |
2acdb232 JH |
32 | #include "trace.hpp" |
33 | #include "tracepalette.hpp" | |
34 | #include "view.hpp" | |
931f20b0 | 35 | |
24c29d4f | 36 | #include "pv/globalsettings.hpp" |
641574bc | 37 | #include "pv/widgets/colorbutton.hpp" |
24c29d4f | 38 | #include "pv/widgets/popup.hpp" |
569d1e41 | 39 | |
6f925ba9 UH |
40 | using std::pair; |
41 | using std::shared_ptr; | |
42 | ||
931f20b0 | 43 | namespace pv { |
f4e57597 | 44 | namespace views { |
1573bf16 | 45 | namespace trace { |
931f20b0 | 46 | |
c063290a | 47 | const QPen Trace::AxisPen(QColor(0, 0, 0, 30 * 256 / 100)); |
931f20b0 JH |
48 | const int Trace::LabelHitPadding = 2; |
49 | ||
641574bc SA |
50 | const QColor Trace::BrightGrayBGColor = QColor(0, 0, 0, 10 * 255 / 100); |
51 | const QColor Trace::DarkGrayBGColor = QColor(0, 0, 0, 15 * 255 / 100); | |
f6a93932 | 52 | const QColor Trace::ErrorBgColor = QColor(0xEF, 0x29, 0x29); |
ac0708fb | 53 | |
4640a84e SA |
54 | Trace::Trace(shared_ptr<data::SignalBase> signal) : |
55 | base_(signal), | |
561ba3ae | 56 | axis_pen_(AxisPen), |
7daebd05 | 57 | segment_display_mode_(ShowLastSegmentOnly), // Will be overwritten by View |
2749b858 | 58 | current_segment_(0), |
4c60462b JH |
59 | popup_(nullptr), |
60 | popup_form_(nullptr) | |
931f20b0 | 61 | { |
4640a84e | 62 | connect(signal.get(), SIGNAL(name_changed(const QString&)), |
bf0edd2b | 63 | this, SLOT(on_name_changed(const QString&))); |
4640a84e | 64 | connect(signal.get(), SIGNAL(color_changed(const QColor&)), |
641574bc | 65 | this, SLOT(on_color_changed(const QColor&))); |
f6a93932 SA |
66 | connect(signal.get(), SIGNAL(error_message_changed(const QString&)), |
67 | this, SLOT(on_error_message_changed(const QString&))); | |
1931b5f9 SA |
68 | |
69 | GlobalSettings::add_change_handler(this); | |
70 | ||
71 | GlobalSettings settings; | |
72 | show_hover_marker_ = | |
73 | settings.value(GlobalSettings::Key_View_ShowHoverMarker).toBool(); | |
931f20b0 JH |
74 | } |
75 | ||
99c49526 SA |
76 | Trace::~Trace() |
77 | { | |
78 | GlobalSettings::remove_change_handler(this); | |
79 | } | |
80 | ||
5ed05b69 SA |
81 | shared_ptr<data::SignalBase> Trace::base() const |
82 | { | |
83 | return base_; | |
84 | } | |
85 | ||
cbd9ec7f SA |
86 | bool Trace::is_selectable(QPoint pos) const |
87 | { | |
88 | // True if the header was clicked, false if the trace area was clicked | |
89 | const View *view = owner_->view(); | |
90 | assert(view); | |
91 | ||
92 | return (pos.x() <= view->header_width()); | |
93 | } | |
94 | ||
119c5c23 | 95 | bool Trace::is_draggable(QPoint pos) const |
cbd9ec7f SA |
96 | { |
97 | // While the header label that belongs to this trace is draggable, | |
119c5c23 SA |
98 | // the trace itself shall not be. Hence we return true if the header |
99 | // was clicked and false if the trace area was clicked | |
100 | const View *view = owner_->view(); | |
101 | assert(view); | |
102 | ||
103 | return (pos.x() <= view->header_width()); | |
cbd9ec7f SA |
104 | } |
105 | ||
1931b5f9 SA |
106 | void Trace::set_segment_display_mode(SegmentDisplayMode mode) |
107 | { | |
108 | segment_display_mode_ = mode; | |
109 | ||
110 | if (owner_) | |
111 | owner_->row_item_appearance_changed(true, true); | |
112 | } | |
113 | ||
114 | void Trace::on_setting_changed(const QString &key, const QVariant &value) | |
115 | { | |
116 | if (key == GlobalSettings::Key_View_ShowHoverMarker) | |
117 | show_hover_marker_ = value.toBool(); | |
39dce56b SA |
118 | |
119 | // Force a repaint since many options alter the way traces look | |
120 | if (owner_) | |
121 | owner_->row_item_appearance_changed(false, true); | |
1931b5f9 SA |
122 | } |
123 | ||
b3f44329 | 124 | void Trace::paint_label(QPainter &p, const QRect &rect, bool hover) |
01fd3263 | 125 | { |
be9e7b4b | 126 | const int y = get_visual_y(); |
01fd3263 | 127 | |
641574bc | 128 | p.setBrush(base_->color()); |
931f20b0 JH |
129 | |
130 | if (!enabled()) | |
131 | return; | |
132 | ||
b3f44329 | 133 | const QRectF r = label_rect(rect); |
931f20b0 | 134 | |
c1a6513b SA |
135 | // When selected, move the arrow to the left so that the border can show |
136 | const QPointF offs = (selected()) ? QPointF(-2, 0) : QPointF(0, 0); | |
137 | ||
931f20b0 | 138 | // Paint the label |
b5cb6c35 | 139 | const float label_arrow_length = r.height() / 2; |
c1a6513b SA |
140 | QPointF points[] = { |
141 | offs + r.topLeft(), | |
142 | offs + QPointF(r.right() - label_arrow_length, r.top()), | |
143 | offs + QPointF(r.right(), y), | |
144 | offs + QPointF(r.right() - label_arrow_length, r.bottom()), | |
145 | offs + r.bottomLeft() | |
931f20b0 | 146 | }; |
c1a6513b SA |
147 | QPointF highlight_points[] = { |
148 | offs + QPointF(r.left() + 1, r.top() + 1), | |
149 | offs + QPointF(r.right() - label_arrow_length, r.top() + 1), | |
150 | offs + QPointF(r.right() - 1, y), | |
151 | offs + QPointF(r.right() - label_arrow_length, r.bottom() - 1), | |
152 | offs + QPointF(r.left() + 1, r.bottom() - 1) | |
931f20b0 JH |
153 | }; |
154 | ||
155 | if (selected()) { | |
156 | p.setPen(highlight_pen()); | |
157 | p.setBrush(Qt::transparent); | |
158 | p.drawPolygon(points, countof(points)); | |
159 | } | |
160 | ||
161 | p.setPen(Qt::transparent); | |
641574bc | 162 | p.setBrush(hover ? base_->color().lighter() : base_->color()); |
931f20b0 JH |
163 | p.drawPolygon(points, countof(points)); |
164 | ||
641574bc | 165 | p.setPen(base_->color().lighter()); |
931f20b0 JH |
166 | p.setBrush(Qt::transparent); |
167 | p.drawPolygon(highlight_points, countof(highlight_points)); | |
168 | ||
641574bc | 169 | p.setPen(base_->color().darker()); |
931f20b0 JH |
170 | p.setBrush(Qt::transparent); |
171 | p.drawPolygon(points, countof(points)); | |
172 | ||
173 | // Paint the text | |
641574bc | 174 | p.setPen(select_text_color(base_->color())); |
d7c0ca4a | 175 | p.setFont(QApplication::font()); |
b5cb6c35 JH |
176 | p.drawText(QRectF(r.x(), r.y(), |
177 | r.width() - label_arrow_length, r.height()), | |
73a25a6e | 178 | Qt::AlignCenter | Qt::AlignVCenter, base_->name()); |
931f20b0 JH |
179 | } |
180 | ||
f6a93932 SA |
181 | void Trace::paint_error(QPainter &p, const ViewItemPaintParams &pp) |
182 | { | |
183 | const QString message = base_->get_error_message(); | |
184 | ||
185 | const int y = get_visual_y(); | |
186 | ||
187 | p.setPen(ErrorBgColor.darker()); | |
188 | p.setBrush(ErrorBgColor); | |
189 | ||
190 | const QRectF bounding_rect = QRectF(pp.left(), INT_MIN / 2 + y, pp.right(), INT_MAX); | |
191 | ||
192 | const QRectF text_rect = p.boundingRect(bounding_rect, Qt::AlignCenter, message); | |
193 | const qreal r = text_rect.height() / 4; | |
194 | ||
195 | p.drawRoundedRect(text_rect.adjusted(-r, -r, r, r), r, r, Qt::AbsoluteSize); | |
196 | ||
197 | p.setPen(Qt::black); | |
198 | p.drawText(text_rect, message); | |
199 | } | |
200 | ||
9e773fec | 201 | QMenu* Trace::create_header_context_menu(QWidget *parent) |
9b6378f1 | 202 | { |
9e773fec | 203 | QMenu *const menu = ViewItem::create_header_context_menu(parent); |
9b6378f1 | 204 | |
9b6378f1 JH |
205 | return menu; |
206 | } | |
207 | ||
79b53a1a SA |
208 | QMenu* Trace::create_view_context_menu(QWidget *parent, QPoint &click_pos) |
209 | { | |
210 | context_menu_x_pos_ = click_pos.x(); | |
211 | ||
212 | // Get entries from default menu before adding our own | |
213 | QMenu *const menu = new QMenu(parent); | |
214 | ||
215 | QMenu* default_menu = TraceTreeItem::create_view_context_menu(parent, click_pos); | |
216 | if (default_menu) { | |
7a0d99e6 | 217 | for (QAction *action : default_menu->actions()) { // clazy:exclude=range-loop |
79b53a1a SA |
218 | menu->addAction(action); |
219 | if (action->parent() == default_menu) | |
220 | action->setParent(menu); | |
221 | } | |
222 | delete default_menu; | |
223 | ||
224 | // Add separator if needed | |
225 | if (menu->actions().length() > 0) | |
226 | menu->addSeparator(); | |
227 | } | |
228 | ||
229 | QAction *const create_marker_here = new QAction(tr("Create marker here"), this); | |
230 | connect(create_marker_here, SIGNAL(triggered()), this, SLOT(on_create_marker_here())); | |
231 | menu->addAction(create_marker_here); | |
232 | ||
233 | return menu; | |
234 | } | |
235 | ||
569d1e41 JH |
236 | pv::widgets::Popup* Trace::create_popup(QWidget *parent) |
237 | { | |
238 | using pv::widgets::Popup; | |
20eaae39 | 239 | |
8dbbc7f0 | 240 | popup_ = new Popup(parent); |
786b7678 | 241 | popup_->set_position(parent->mapToGlobal( |
a3d5a7c7 | 242 | drag_point(parent->rect())), Popup::Right); |
20eaae39 | 243 | |
37fd11b1 | 244 | create_popup_form(); |
20eaae39 | 245 | |
c063290a | 246 | connect(popup_, SIGNAL(closed()), this, SLOT(on_popup_closed())); |
20eaae39 | 247 | |
8dbbc7f0 | 248 | return popup_; |
569d1e41 JH |
249 | } |
250 | ||
b3f44329 | 251 | QRectF Trace::label_rect(const QRectF &rect) const |
d7c0ca4a | 252 | { |
d7c0ca4a JH |
253 | QFontMetrics m(QApplication::font()); |
254 | const QSize text_size( | |
73a25a6e | 255 | m.boundingRect(QRect(), 0, base_->name()).width(), m.height()); |
d7c0ca4a | 256 | const QSizeF label_size( |
ec39632d JH |
257 | text_size.width() + LabelPadding.width() * 2, |
258 | ceilf((text_size.height() + LabelPadding.height() * 2) / 2) * 2); | |
b5cb6c35 | 259 | const float half_height = label_size.height() / 2; |
d7c0ca4a | 260 | return QRectF( |
b3f44329 | 261 | rect.right() - half_height - label_size.width() - 0.5, |
be9e7b4b | 262 | get_visual_y() + 0.5f - half_height, |
b5cb6c35 JH |
263 | label_size.width() + half_height, |
264 | label_size.height()); | |
d7c0ca4a JH |
265 | } |
266 | ||
d9b55cc8 SA |
267 | QRectF Trace::hit_box_rect(const ViewItemPaintParams &pp) const |
268 | { | |
cbd9ec7f SA |
269 | // This one is only for the trace itself, excluding the header area |
270 | const View *view = owner_->view(); | |
271 | assert(view); | |
272 | ||
d9b55cc8 SA |
273 | pair<int, int> extents = v_extents(); |
274 | const int top = pp.top() + get_visual_y() + extents.first; | |
275 | const int height = extents.second - extents.first; | |
cbd9ec7f SA |
276 | |
277 | return QRectF(pp.left() + view->header_width(), top, | |
278 | pp.width() - view->header_width(), height); | |
d9b55cc8 SA |
279 | } |
280 | ||
2749b858 SA |
281 | void Trace::set_current_segment(const int segment) |
282 | { | |
283 | current_segment_ = segment; | |
284 | } | |
285 | ||
286 | int Trace::get_current_segment() const | |
287 | { | |
288 | return current_segment_; | |
289 | } | |
290 | ||
1931b5f9 SA |
291 | void Trace::hover_point_changed(const QPoint &hp) |
292 | { | |
293 | (void)hp; | |
294 | ||
295 | if (owner_) | |
296 | owner_->row_item_appearance_changed(false, true); | |
297 | } | |
298 | ||
60938e04 | 299 | void Trace::paint_back(QPainter &p, ViewItemPaintParams &pp) |
99af6802 | 300 | { |
48995388 JH |
301 | const View *view = owner_->view(); |
302 | assert(view); | |
303 | ||
641574bc SA |
304 | if (view->colored_bg()) |
305 | p.setBrush(base_->bgcolor()); | |
ac0708fb | 306 | else |
641574bc | 307 | p.setBrush(pp.next_bg_color_state() ? BrightGrayBGColor : DarkGrayBGColor); |
99af6802 | 308 | |
ac0708fb | 309 | p.setPen(QPen(Qt::NoPen)); |
99af6802 | 310 | |
6f925ba9 | 311 | const pair<int, int> extents = v_extents(); |
857ebbef JH |
312 | p.drawRect(pp.left(), get_visual_y() + extents.first, |
313 | pp.width(), extents.second - extents.first); | |
99af6802 SA |
314 | } |
315 | ||
60938e04 | 316 | void Trace::paint_axis(QPainter &p, ViewItemPaintParams &pp, int y) |
fe08b6e8 | 317 | { |
f0b99257 | 318 | bool was_antialiased = p.testRenderHint(QPainter::Antialiasing); |
46a0cadc SA |
319 | p.setRenderHint(QPainter::Antialiasing, false); |
320 | ||
561ba3ae | 321 | p.setPen(axis_pen_); |
46a0cadc SA |
322 | p.drawLine(QPointF(pp.left(), y), QPointF(pp.right(), y)); |
323 | ||
f0b99257 | 324 | p.setRenderHint(QPainter::Antialiasing, was_antialiased); |
fe08b6e8 JH |
325 | } |
326 | ||
641574bc | 327 | void Trace::add_color_option(QWidget *parent, QFormLayout *form) |
91e8bf08 | 328 | { |
641574bc | 329 | using pv::widgets::ColorButton; |
91e8bf08 | 330 | |
641574bc | 331 | ColorButton *const color_button = new ColorButton( |
91e8bf08 | 332 | TracePalette::Rows, TracePalette::Cols, parent); |
641574bc SA |
333 | color_button->set_palette(TracePalette::Colors); |
334 | color_button->set_color(base_->color()); | |
335 | connect(color_button, SIGNAL(selected(const QColor&)), | |
336 | this, SLOT(on_coloredit_changed(const QColor&))); | |
91e8bf08 | 337 | |
641574bc | 338 | form->addRow(tr("Color"), color_button); |
91e8bf08 JH |
339 | } |
340 | ||
1931b5f9 SA |
341 | void Trace::paint_hover_marker(QPainter &p) |
342 | { | |
343 | const View *view = owner_->view(); | |
344 | assert(view); | |
345 | ||
346 | const int x = view->hover_point().x(); | |
347 | ||
348 | if (x == -1) | |
349 | return; | |
350 | ||
351 | p.setPen(QPen(QColor(Qt::lightGray))); | |
352 | ||
353 | const pair<int, int> extents = v_extents(); | |
354 | ||
f0b99257 | 355 | bool was_antialiased = p.testRenderHint(QPainter::Antialiasing); |
1931b5f9 SA |
356 | p.setRenderHint(QPainter::Antialiasing, false); |
357 | p.drawLine(x, get_visual_y() + extents.first, | |
358 | x, get_visual_y() + extents.second); | |
f0b99257 | 359 | p.setRenderHint(QPainter::Antialiasing, was_antialiased); |
1931b5f9 SA |
360 | } |
361 | ||
37fd11b1 JH |
362 | void Trace::create_popup_form() |
363 | { | |
364 | // Clear the layout | |
365 | ||
366 | // Transfer the layout and the child widgets to a temporary widget | |
e3004449 EO |
367 | // which we delete after the event was handled. This way, the layout |
368 | // and all widgets contained therein are deleted after the event was | |
369 | // handled, leaving the parent popup_ time to handle the change. | |
370 | if (popup_form_) { | |
371 | QWidget *suicidal = new QWidget(); | |
f52ffd9d | 372 | suicidal->setLayout(popup_->layout()); |
e3004449 EO |
373 | suicidal->deleteLater(); |
374 | } | |
37fd11b1 JH |
375 | |
376 | // Repopulate the popup | |
f52ffd9d SA |
377 | widgets::QWidthAdjustingScrollArea* scrollarea = new widgets::QWidthAdjustingScrollArea(); |
378 | QWidget* scrollarea_content = new QWidget(scrollarea); | |
379 | ||
380 | scrollarea->setWidget(scrollarea_content); | |
381 | scrollarea->setWidgetResizable(true); | |
382 | scrollarea->setContentsMargins(0, 0, 0, 0); | |
383 | scrollarea->setFrameShape(QFrame::NoFrame); | |
384 | scrollarea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | |
385 | scrollarea_content->setContentsMargins(0, 0, 0, 0); | |
386 | ||
387 | popup_->setLayout(new QVBoxLayout()); | |
388 | popup_->layout()->addWidget(scrollarea); | |
389 | popup_->layout()->setContentsMargins(0, 0, 0, 0); | |
390 | ||
391 | popup_form_ = new QFormLayout(scrollarea_content); | |
392 | popup_form_->setSizeConstraint(QLayout::SetMinAndMaxSize); | |
393 | ||
8dbbc7f0 | 394 | populate_popup_form(popup_, popup_form_); |
37fd11b1 JH |
395 | } |
396 | ||
569d1e41 JH |
397 | void Trace::populate_popup_form(QWidget *parent, QFormLayout *form) |
398 | { | |
68456dab | 399 | QLineEdit *const name_edit = new QLineEdit(parent); |
73a25a6e | 400 | name_edit->setText(base_->name()); |
68456dab | 401 | connect(name_edit, SIGNAL(textChanged(const QString&)), |
bf0edd2b | 402 | this, SLOT(on_nameedit_changed(const QString&))); |
68456dab | 403 | form->addRow(tr("Name"), name_edit); |
91e8bf08 | 404 | |
641574bc | 405 | add_color_option(parent, form); |
569d1e41 JH |
406 | } |
407 | ||
bf0edd2b SA |
408 | void Trace::on_name_changed(const QString &text) |
409 | { | |
410 | /* This event handler is called by SignalBase when the name was changed there */ | |
411 | (void)text; | |
412 | ||
413 | if (owner_) { | |
8dbbc7f0 | 414 | owner_->extents_changed(true, false); |
bf0edd2b SA |
415 | owner_->row_item_appearance_changed(true, false); |
416 | } | |
68456dab | 417 | } |
9b6378f1 | 418 | |
641574bc | 419 | void Trace::on_color_changed(const QColor &color) |
91e8bf08 | 420 | { |
641574bc SA |
421 | /* This event handler is called by SignalBase when the color was changed there */ |
422 | (void)color; | |
32218d3e | 423 | |
8dbbc7f0 | 424 | if (owner_) |
99af6802 | 425 | owner_->row_item_appearance_changed(true, true); |
91e8bf08 JH |
426 | } |
427 | ||
f6a93932 SA |
428 | void Trace::on_error_message_changed(const QString &msg) |
429 | { | |
430 | (void)msg; | |
431 | ||
432 | if (owner_) | |
433 | owner_->row_item_appearance_changed(false, true); | |
434 | } | |
435 | ||
bf0edd2b SA |
436 | void Trace::on_popup_closed() |
437 | { | |
438 | popup_ = nullptr; | |
439 | popup_form_ = nullptr; | |
440 | } | |
441 | ||
442 | void Trace::on_nameedit_changed(const QString &name) | |
443 | { | |
444 | /* This event handler notifies SignalBase that the name changed */ | |
0acf629d | 445 | base_->set_name(name); |
bf0edd2b SA |
446 | } |
447 | ||
641574bc | 448 | void Trace::on_coloredit_changed(const QColor &color) |
bf0edd2b | 449 | { |
641574bc | 450 | /* This event handler notifies SignalBase that the color changed */ |
0acf629d | 451 | base_->set_color(color); |
bf0edd2b SA |
452 | } |
453 | ||
79b53a1a SA |
454 | void Trace::on_create_marker_here() const |
455 | { | |
456 | View *view = owner_->view(); | |
457 | assert(view); | |
458 | ||
459 | const Ruler *ruler = view->ruler(); | |
460 | QPoint p = ruler->mapFrom(view, QPoint(context_menu_x_pos_, 0)); | |
461 | ||
fe68068b | 462 | view->add_flag(ruler->get_absolute_time_from_x_pos(p.x())); |
79b53a1a SA |
463 | } |
464 | ||
1573bf16 | 465 | } // namespace trace |
f4e57597 | 466 | } // namespace views |
931f20b0 | 467 | } // namespace pv |