PulseView  unreleased development snapshot
A Qt-based sigrok GUI
trace.cpp
Go to the documentation of this file.
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"
38 #include "pv/widgets/popup.hpp"
39 
40 using std::pair;
41 using std::shared_ptr;
42 
43 namespace pv {
44 namespace views {
45 namespace trace {
46 
47 const QPen Trace::AxisPen(QColor(0, 0, 0, 30 * 256 / 100));
48 const int Trace::LabelHitPadding = 2;
49 
50 const QColor Trace::BrightGrayBGColor = QColor(0, 0, 0, 10 * 255 / 100);
51 const QColor Trace::DarkGrayBGColor = QColor(0, 0, 0, 15 * 255 / 100);
52 const QColor Trace::ErrorBgColor = QColor(0xEF, 0x29, 0x29);
53 
54 Trace::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 
70 
71  GlobalSettings settings;
73  settings.value(GlobalSettings::Key_View_ShowHoverMarker).toBool();
74 }
75 
77 {
79 }
80 
81 shared_ptr<data::SignalBase> Trace::base() const
82 {
83  return base_;
84 }
85 
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 
95 bool 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 
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 {
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 
124 void 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 
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 
201 QMenu* Trace::create_header_context_menu(QWidget *parent)
202 {
203  QMenu *const menu = ViewItem::create_header_context_menu(parent);
204 
205  return menu;
206 }
207 
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) {
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 
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 
245 
246  connect(popup_, SIGNAL(closed()), this, SLOT(on_popup_closed()));
247 
248  return popup_;
249 }
250 
251 QRectF 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 
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 
281 void Trace::set_current_segment(const int segment)
282 {
283  current_segment_ = segment;
284 }
285 
287 {
288  return current_segment_;
289 }
290 
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 
300 {
301  const View *view = owner_->view();
302  assert(view);
303 
304  if (view->colored_bg())
305  p.setBrush(base_->bgcolor());
306  else
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 
316 void 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 
327 void Trace::add_color_option(QWidget *parent, QFormLayout *form)
328 {
330 
331  ColorButton *const color_button = new ColorButton(
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 
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 
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 
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
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 
395 }
396 
397 void 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 
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_) {
414  owner_->extents_changed(true, false);
415  owner_->row_item_appearance_changed(true, false);
416  }
417 }
418 
419 void 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 
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 
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 */
445  if (!name.isEmpty())
446  base_->set_name(name);
447 }
448 
449 void Trace::on_coloredit_changed(const QColor &color)
450 {
451  /* This event handler notifies SignalBase that the color changed */
452  base_->set_color(color);
453 }
454 
456 {
457  View *view = owner_->view();
458  assert(view);
459 
460  const Ruler *ruler = view->ruler();
461  QPoint p = ruler->mapFrom(view, QPoint(context_menu_x_pos_, 0));
462 
463  view->add_flag(ruler->get_absolute_time_from_x_pos(p.x()));
464 }
465 
466 } // namespace trace
467 } // namespace views
468 } // namespace pv
virtual QMenu * create_view_context_menu(QWidget *parent, QPoint &click_pos)
Definition: viewitem.cpp:97
const Ruler * ruler() const
Definition: view.cpp:491
static const QColor BrightGrayBGColor
Definition: trace.hpp:92
int header_width() const
Definition: view.cpp:1175
virtual bool enabled() const =0
bool colored_bg() const
Definition: view.cpp:963
virtual void paint_back(QPainter &p, ViewItemPaintParams &pp)
Definition: trace.cpp:299
void set_position(const QPoint point, Position pos)
Definition: popup.cpp:90
static const QColor DarkGrayBGColor
Definition: trace.hpp:93
virtual void populate_popup_form(QWidget *parent, QFormLayout *form)
Definition: trace.cpp:397
static const unsigned int Rows
virtual void extents_changed(bool horz, bool vert)=0
virtual void set_segment_display_mode(SegmentDisplayMode mode)
Definition: trace.cpp:106
QFormLayout * popup_form_
Definition: trace.hpp:221
uint32_t context_menu_x_pos_
Definition: trace.hpp:214
virtual pair< int, int > v_extents() const =0
T value(details::expression_node< T > *n)
Definition: exprtk.hpp:12358
void on_nameedit_changed(const QString &name)
Definition: trace.cpp:442
void on_create_marker_here() const
Definition: trace.cpp:455
virtual void on_color_changed(const QColor &color)
Definition: trace.cpp:419
virtual void on_name_changed(const QString &text)
Definition: trace.cpp:408
virtual void on_error_message_changed(const QString &msg)
Definition: trace.cpp:428
int current_segment_
The ID of the currently displayed segment.
Definition: trace.hpp:217
m
Definition: CMakeCache.txt:621
shared_ptr< data::SignalBase > base_
Definition: trace.hpp:208
virtual void paint_label(QPainter &p, const QRect &rect, bool hover)
Definition: trace.cpp:124
static QPen highlight_pen()
Definition: viewitem.cpp:114
static void remove_change_handler(GlobalSettingsInterface *cb)
virtual bool is_selectable(QPoint pos) const
Definition: trace.cpp:86
static const QSizeF LabelPadding
Definition: viewitem.hpp:51
virtual QMenu * create_view_context_menu(QWidget *parent, QPoint &click_pos)
Definition: trace.cpp:208
virtual QRectF hit_box_rect(const ViewItemPaintParams &pp) const
Definition: trace.cpp:267
shared_ptr< data::SignalBase > base() const
Definition: trace.cpp:81
virtual void hover_point_changed(const QPoint &hp)
Definition: trace.cpp:291
virtual QMenu * create_header_context_menu(QWidget *parent)
Definition: trace.cpp:201
static const int LabelHitPadding
Definition: trace.hpp:90
int get_current_segment() const
Definition: trace.cpp:286
static const QPen AxisPen
Definition: trace.hpp:89
void paint_axis(QPainter &p, ViewItemPaintParams &pp, int y)
Definition: trace.cpp:316
virtual bool is_draggable(QPoint pos) const
Definition: trace.cpp:95
static const QString Key_View_ShowHoverMarker
pv::widgets::Popup * popup_
Definition: trace.hpp:220
#define countof(x)
Definition: extdef.h:23
QRectF label_rect(const QRectF &rect) const
Definition: trace.cpp:251
static QColor select_text_color(QColor background)
Definition: viewitem.cpp:146
static const unsigned int Cols
const QPoint & hover_point() const
Definition: view.cpp:1042
void add_color_option(QWidget *parent, QFormLayout *form)
Definition: trace.cpp:327
SegmentDisplayMode segment_display_mode_
Definition: trace.hpp:211
Trace(shared_ptr< data::SignalBase > signal)
Definition: trace.cpp:54
x y t t *t x y t t t x y t t t x *y t *t t x *y t *t t x y t t t x y t t t x(y+z)
bool selected() const
Definition: viewitem.cpp:48
pv::util::Timestamp get_absolute_time_from_x_pos(uint32_t x) const
Definition: ruler.cpp:121
QPoint drag_point(const QRect &rect) const
static const QColor Colors[Cols *Rows]
void on_coloredit_changed(const QColor &color)
Definition: trace.cpp:449
pv::widgets::Popup * create_popup(QWidget *parent)
Definition: trace.cpp:236
void set_current_segment(const int segment)
Definition: trace.cpp:281
static const QColor ErrorBgColor
Definition: trace.hpp:94
virtual QMenu * create_header_context_menu(QWidget *parent)
Definition: viewitem.cpp:91
virtual void row_item_appearance_changed(bool label, bool content)=0
virtual void on_setting_changed(const QString &key, const QVariant &value)
Definition: trace.cpp:114
static void add_change_handler(GlobalSettingsInterface *cb)
void paint_hover_marker(QPainter &p)
Definition: trace.cpp:341
shared_ptr< Flag > add_flag(const pv::util::Timestamp &time)
Definition: view.cpp:1012
virtual void paint_error(QPainter &p, const ViewItemPaintParams &pp)
Definition: trace.cpp:181