]> sigrok.org Git - pulseview.git/blame_incremental - pv/views/trace/trace.cpp
Session: Fix issue #67 by improving error handling
[pulseview.git] / pv / views / trace / trace.cpp
... / ...
CommitLineData
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, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <extdef.h>
21
22#include <cassert>
23#include <cmath>
24
25#include <QApplication>
26#include <QFormLayout>
27#include <QKeyEvent>
28#include <QLineEdit>
29#include <QMenu>
30
31#include "ruler.hpp"
32#include "trace.hpp"
33#include "tracepalette.hpp"
34#include "view.hpp"
35
36#include "pv/globalsettings.hpp"
37#include "pv/widgets/colorbutton.hpp"
38#include "pv/widgets/popup.hpp"
39
40using std::pair;
41using std::shared_ptr;
42
43namespace pv {
44namespace views {
45namespace trace {
46
47const QPen Trace::AxisPen(QColor(0, 0, 0, 30 * 256 / 100));
48const int Trace::LabelHitPadding = 2;
49
50const QColor Trace::BrightGrayBGColor = QColor(0, 0, 0, 10 * 255 / 100);
51const QColor Trace::DarkGrayBGColor = QColor(0, 0, 0, 15 * 255 / 100);
52const QColor Trace::ErrorBgColor = QColor(0xEF, 0x29, 0x29);
53
54Trace::Trace(shared_ptr<data::SignalBase> signal) :
55 base_(signal),
56 axis_pen_(AxisPen),
57 segment_display_mode_(ShowLastSegmentOnly), // Will be overwritten by View
58 current_segment_(0),
59 popup_(nullptr),
60 popup_form_(nullptr)
61{
62 connect(signal.get(), SIGNAL(name_changed(const QString&)),
63 this, SLOT(on_name_changed(const QString&)));
64 connect(signal.get(), SIGNAL(color_changed(const QColor&)),
65 this, SLOT(on_color_changed(const QColor&)));
66 connect(signal.get(), SIGNAL(error_message_changed(const QString&)),
67 this, SLOT(on_error_message_changed(const QString&)));
68
69 GlobalSettings::add_change_handler(this);
70
71 GlobalSettings settings;
72 show_hover_marker_ =
73 settings.value(GlobalSettings::Key_View_ShowHoverMarker).toBool();
74}
75
76Trace::~Trace()
77{
78 GlobalSettings::remove_change_handler(this);
79}
80
81shared_ptr<data::SignalBase> Trace::base() const
82{
83 return base_;
84}
85
86bool 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
95bool Trace::is_draggable(QPoint pos) const
96{
97 // While the header label that belongs to this trace is draggable,
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());
104}
105
106void 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
114void Trace::on_setting_changed(const QString &key, const QVariant &value)
115{
116 if (key == GlobalSettings::Key_View_ShowHoverMarker)
117 show_hover_marker_ = value.toBool();
118
119 // Force a repaint since many options alter the way traces look
120 if (owner_)
121 owner_->row_item_appearance_changed(false, true);
122}
123
124void Trace::paint_label(QPainter &p, const QRect &rect, bool hover)
125{
126 const int y = get_visual_y();
127
128 p.setBrush(base_->color());
129
130 if (!enabled())
131 return;
132
133 const QRectF r = label_rect(rect);
134
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
138 // Paint the label
139 const float label_arrow_length = r.height() / 2;
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()
146 };
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)
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);
162 p.setBrush(hover ? base_->color().lighter() : base_->color());
163 p.drawPolygon(points, countof(points));
164
165 p.setPen(base_->color().lighter());
166 p.setBrush(Qt::transparent);
167 p.drawPolygon(highlight_points, countof(highlight_points));
168
169 p.setPen(base_->color().darker());
170 p.setBrush(Qt::transparent);
171 p.drawPolygon(points, countof(points));
172
173 // Paint the text
174 p.setPen(select_text_color(base_->color()));
175 p.setFont(QApplication::font());
176 p.drawText(QRectF(r.x(), r.y(),
177 r.width() - label_arrow_length, r.height()),
178 Qt::AlignCenter | Qt::AlignVCenter, base_->name());
179}
180
181void 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
201QMenu* Trace::create_header_context_menu(QWidget *parent)
202{
203 QMenu *const menu = ViewItem::create_header_context_menu(parent);
204
205 return menu;
206}
207
208QMenu* 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) {
217 for (QAction *action : default_menu->actions()) { // clazy:exclude=range-loop
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
236pv::widgets::Popup* Trace::create_popup(QWidget *parent)
237{
238 using pv::widgets::Popup;
239
240 popup_ = new Popup(parent);
241 popup_->set_position(parent->mapToGlobal(
242 drag_point(parent->rect())), Popup::Right);
243
244 create_popup_form();
245
246 connect(popup_, SIGNAL(closed()), this, SLOT(on_popup_closed()));
247
248 return popup_;
249}
250
251QRectF Trace::label_rect(const QRectF &rect) const
252{
253 QFontMetrics m(QApplication::font());
254 const QSize text_size(
255 m.boundingRect(QRect(), 0, base_->name()).width(), m.height());
256 const QSizeF label_size(
257 text_size.width() + LabelPadding.width() * 2,
258 ceilf((text_size.height() + LabelPadding.height() * 2) / 2) * 2);
259 const float half_height = label_size.height() / 2;
260 return QRectF(
261 rect.right() - half_height - label_size.width() - 0.5,
262 get_visual_y() + 0.5f - half_height,
263 label_size.width() + half_height,
264 label_size.height());
265}
266
267QRectF Trace::hit_box_rect(const ViewItemPaintParams &pp) const
268{
269 // This one is only for the trace itself, excluding the header area
270 const View *view = owner_->view();
271 assert(view);
272
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;
276
277 return QRectF(pp.left() + view->header_width(), top,
278 pp.width() - view->header_width(), height);
279}
280
281void Trace::set_current_segment(const int segment)
282{
283 current_segment_ = segment;
284}
285
286int Trace::get_current_segment() const
287{
288 return current_segment_;
289}
290
291void 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
299void Trace::paint_back(QPainter &p, ViewItemPaintParams &pp)
300{
301 const View *view = owner_->view();
302 assert(view);
303
304 if (view->colored_bg())
305 p.setBrush(base_->bgcolor());
306 else
307 p.setBrush(pp.next_bg_color_state() ? BrightGrayBGColor : DarkGrayBGColor);
308
309 p.setPen(QPen(Qt::NoPen));
310
311 const pair<int, int> extents = v_extents();
312 p.drawRect(pp.left(), get_visual_y() + extents.first,
313 pp.width(), extents.second - extents.first);
314}
315
316void Trace::paint_axis(QPainter &p, ViewItemPaintParams &pp, int y)
317{
318 bool was_antialiased = p.testRenderHint(QPainter::Antialiasing);
319 p.setRenderHint(QPainter::Antialiasing, false);
320
321 p.setPen(axis_pen_);
322 p.drawLine(QPointF(pp.left(), y), QPointF(pp.right(), y));
323
324 p.setRenderHint(QPainter::Antialiasing, was_antialiased);
325}
326
327void Trace::add_color_option(QWidget *parent, QFormLayout *form)
328{
329 using pv::widgets::ColorButton;
330
331 ColorButton *const color_button = new ColorButton(
332 TracePalette::Rows, TracePalette::Cols, parent);
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&)));
337
338 form->addRow(tr("Color"), color_button);
339}
340
341void 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
355 bool was_antialiased = p.testRenderHint(QPainter::Antialiasing);
356 p.setRenderHint(QPainter::Antialiasing, false);
357 p.drawLine(x, get_visual_y() + extents.first,
358 x, get_visual_y() + extents.second);
359 p.setRenderHint(QPainter::Antialiasing, was_antialiased);
360}
361
362void Trace::create_popup_form()
363{
364 // Clear the layout
365
366 // Transfer the layout and the child widgets to a temporary widget
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();
372 suicidal->setLayout(popup_->layout());
373 suicidal->deleteLater();
374 }
375
376 // Repopulate the popup
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
394 populate_popup_form(popup_, popup_form_);
395}
396
397void Trace::populate_popup_form(QWidget *parent, QFormLayout *form)
398{
399 QLineEdit *const name_edit = new QLineEdit(parent);
400 name_edit->setText(base_->name());
401 connect(name_edit, SIGNAL(textChanged(const QString&)),
402 this, SLOT(on_nameedit_changed(const QString&)));
403 form->addRow(tr("Name"), name_edit);
404
405 add_color_option(parent, form);
406}
407
408void 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_) {
414 owner_->extents_changed(true, false);
415 owner_->row_item_appearance_changed(true, false);
416 }
417}
418
419void Trace::on_color_changed(const QColor &color)
420{
421 /* This event handler is called by SignalBase when the color was changed there */
422 (void)color;
423
424 if (owner_)
425 owner_->row_item_appearance_changed(true, true);
426}
427
428void 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
436void Trace::on_popup_closed()
437{
438 popup_ = nullptr;
439 popup_form_ = nullptr;
440}
441
442void Trace::on_nameedit_changed(const QString &name)
443{
444 /* This event handler notifies SignalBase that the name changed */
445 base_->set_name(name);
446}
447
448void Trace::on_coloredit_changed(const QColor &color)
449{
450 /* This event handler notifies SignalBase that the color changed */
451 base_->set_color(color);
452}
453
454void 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
462 view->add_flag(ruler->get_absolute_time_from_x_pos(p.x()));
463}
464
465} // namespace trace
466} // namespace views
467} // namespace pv