]> sigrok.org Git - pulseview.git/blame_incremental - pv/views/trace/trace.cpp
TabularDecView: Allow return/enter press and don't change scale
[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);
52
53Trace::Trace(shared_ptr<data::SignalBase> channel) :
54 base_(channel),
55 axis_pen_(AxisPen),
56 segment_display_mode_(ShowLastSegmentOnly), // Will be overwritten by View
57 current_segment_(0),
58 popup_(nullptr),
59 popup_form_(nullptr)
60{
61 connect(channel.get(), SIGNAL(name_changed(const QString&)),
62 this, SLOT(on_name_changed(const QString&)));
63 connect(channel.get(), SIGNAL(color_changed(const QColor&)),
64 this, SLOT(on_color_changed(const QColor&)));
65
66 GlobalSettings::add_change_handler(this);
67
68 GlobalSettings settings;
69 show_hover_marker_ =
70 settings.value(GlobalSettings::Key_View_ShowHoverMarker).toBool();
71}
72
73Trace::~Trace()
74{
75 GlobalSettings::remove_change_handler(this);
76}
77
78shared_ptr<data::SignalBase> Trace::base() const
79{
80 return base_;
81}
82
83bool Trace::is_selectable(QPoint pos) const
84{
85 // True if the header was clicked, false if the trace area was clicked
86 const View *view = owner_->view();
87 assert(view);
88
89 return (pos.x() <= view->header_width());
90}
91
92bool Trace::is_draggable(QPoint pos) const
93{
94 // While the header label that belongs to this trace is draggable,
95 // the trace itself shall not be. Hence we return true if the header
96 // was clicked and false if the trace area was clicked
97 const View *view = owner_->view();
98 assert(view);
99
100 return (pos.x() <= view->header_width());
101}
102
103void Trace::set_segment_display_mode(SegmentDisplayMode mode)
104{
105 segment_display_mode_ = mode;
106
107 if (owner_)
108 owner_->row_item_appearance_changed(true, true);
109}
110
111void Trace::on_setting_changed(const QString &key, const QVariant &value)
112{
113 if (key == GlobalSettings::Key_View_ShowHoverMarker)
114 show_hover_marker_ = value.toBool();
115
116 // Force a repaint since many options alter the way traces look
117 if (owner_)
118 owner_->row_item_appearance_changed(false, true);
119}
120
121void Trace::paint_label(QPainter &p, const QRect &rect, bool hover)
122{
123 const int y = get_visual_y();
124
125 p.setBrush(base_->color());
126
127 if (!enabled())
128 return;
129
130 const QRectF r = label_rect(rect);
131
132 // When selected, move the arrow to the left so that the border can show
133 const QPointF offs = (selected()) ? QPointF(-2, 0) : QPointF(0, 0);
134
135 // Paint the label
136 const float label_arrow_length = r.height() / 2;
137 QPointF points[] = {
138 offs + r.topLeft(),
139 offs + QPointF(r.right() - label_arrow_length, r.top()),
140 offs + QPointF(r.right(), y),
141 offs + QPointF(r.right() - label_arrow_length, r.bottom()),
142 offs + r.bottomLeft()
143 };
144 QPointF highlight_points[] = {
145 offs + QPointF(r.left() + 1, r.top() + 1),
146 offs + QPointF(r.right() - label_arrow_length, r.top() + 1),
147 offs + QPointF(r.right() - 1, y),
148 offs + QPointF(r.right() - label_arrow_length, r.bottom() - 1),
149 offs + QPointF(r.left() + 1, r.bottom() - 1)
150 };
151
152 if (selected()) {
153 p.setPen(highlight_pen());
154 p.setBrush(Qt::transparent);
155 p.drawPolygon(points, countof(points));
156 }
157
158 p.setPen(Qt::transparent);
159 p.setBrush(hover ? base_->color().lighter() : base_->color());
160 p.drawPolygon(points, countof(points));
161
162 p.setPen(base_->color().lighter());
163 p.setBrush(Qt::transparent);
164 p.drawPolygon(highlight_points, countof(highlight_points));
165
166 p.setPen(base_->color().darker());
167 p.setBrush(Qt::transparent);
168 p.drawPolygon(points, countof(points));
169
170 // Paint the text
171 p.setPen(select_text_color(base_->color()));
172 p.setFont(QApplication::font());
173 p.drawText(QRectF(r.x(), r.y(),
174 r.width() - label_arrow_length, r.height()),
175 Qt::AlignCenter | Qt::AlignVCenter, base_->name());
176}
177
178QMenu* Trace::create_header_context_menu(QWidget *parent)
179{
180 QMenu *const menu = ViewItem::create_header_context_menu(parent);
181
182 return menu;
183}
184
185QMenu* Trace::create_view_context_menu(QWidget *parent, QPoint &click_pos)
186{
187 context_menu_x_pos_ = click_pos.x();
188
189 // Get entries from default menu before adding our own
190 QMenu *const menu = new QMenu(parent);
191
192 QMenu* default_menu = TraceTreeItem::create_view_context_menu(parent, click_pos);
193 if (default_menu) {
194 for (QAction *action : default_menu->actions()) { // clazy:exclude=range-loop
195 menu->addAction(action);
196 if (action->parent() == default_menu)
197 action->setParent(menu);
198 }
199 delete default_menu;
200
201 // Add separator if needed
202 if (menu->actions().length() > 0)
203 menu->addSeparator();
204 }
205
206 QAction *const create_marker_here = new QAction(tr("Create marker here"), this);
207 connect(create_marker_here, SIGNAL(triggered()), this, SLOT(on_create_marker_here()));
208 menu->addAction(create_marker_here);
209
210 return menu;
211}
212
213pv::widgets::Popup* Trace::create_popup(QWidget *parent)
214{
215 using pv::widgets::Popup;
216
217 popup_ = new Popup(parent);
218 popup_->set_position(parent->mapToGlobal(
219 drag_point(parent->rect())), Popup::Right);
220
221 create_popup_form();
222
223 connect(popup_, SIGNAL(closed()), this, SLOT(on_popup_closed()));
224
225 return popup_;
226}
227
228QRectF Trace::label_rect(const QRectF &rect) const
229{
230 QFontMetrics m(QApplication::font());
231 const QSize text_size(
232 m.boundingRect(QRect(), 0, base_->name()).width(), m.height());
233 const QSizeF label_size(
234 text_size.width() + LabelPadding.width() * 2,
235 ceilf((text_size.height() + LabelPadding.height() * 2) / 2) * 2);
236 const float half_height = label_size.height() / 2;
237 return QRectF(
238 rect.right() - half_height - label_size.width() - 0.5,
239 get_visual_y() + 0.5f - half_height,
240 label_size.width() + half_height,
241 label_size.height());
242}
243
244QRectF Trace::hit_box_rect(const ViewItemPaintParams &pp) const
245{
246 // This one is only for the trace itself, excluding the header area
247 const View *view = owner_->view();
248 assert(view);
249
250 pair<int, int> extents = v_extents();
251 const int top = pp.top() + get_visual_y() + extents.first;
252 const int height = extents.second - extents.first;
253
254 return QRectF(pp.left() + view->header_width(), top,
255 pp.width() - view->header_width(), height);
256}
257
258void Trace::set_current_segment(const int segment)
259{
260 current_segment_ = segment;
261}
262
263int Trace::get_current_segment() const
264{
265 return current_segment_;
266}
267
268void Trace::hover_point_changed(const QPoint &hp)
269{
270 (void)hp;
271
272 if (owner_)
273 owner_->row_item_appearance_changed(false, true);
274}
275
276void Trace::paint_back(QPainter &p, ViewItemPaintParams &pp)
277{
278 const View *view = owner_->view();
279 assert(view);
280
281 if (view->colored_bg())
282 p.setBrush(base_->bgcolor());
283 else
284 p.setBrush(pp.next_bg_color_state() ? BrightGrayBGColor : DarkGrayBGColor);
285
286 p.setPen(QPen(Qt::NoPen));
287
288 const pair<int, int> extents = v_extents();
289 p.drawRect(pp.left(), get_visual_y() + extents.first,
290 pp.width(), extents.second - extents.first);
291}
292
293void Trace::paint_axis(QPainter &p, ViewItemPaintParams &pp, int y)
294{
295 bool was_antialiased = p.testRenderHint(QPainter::Antialiasing);
296 p.setRenderHint(QPainter::Antialiasing, false);
297
298 p.setPen(axis_pen_);
299 p.drawLine(QPointF(pp.left(), y), QPointF(pp.right(), y));
300
301 p.setRenderHint(QPainter::Antialiasing, was_antialiased);
302}
303
304void Trace::add_color_option(QWidget *parent, QFormLayout *form)
305{
306 using pv::widgets::ColorButton;
307
308 ColorButton *const color_button = new ColorButton(
309 TracePalette::Rows, TracePalette::Cols, parent);
310 color_button->set_palette(TracePalette::Colors);
311 color_button->set_color(base_->color());
312 connect(color_button, SIGNAL(selected(const QColor&)),
313 this, SLOT(on_coloredit_changed(const QColor&)));
314
315 form->addRow(tr("Color"), color_button);
316}
317
318void Trace::paint_hover_marker(QPainter &p)
319{
320 const View *view = owner_->view();
321 assert(view);
322
323 const int x = view->hover_point().x();
324
325 if (x == -1)
326 return;
327
328 p.setPen(QPen(QColor(Qt::lightGray)));
329
330 const pair<int, int> extents = v_extents();
331
332 bool was_antialiased = p.testRenderHint(QPainter::Antialiasing);
333 p.setRenderHint(QPainter::Antialiasing, false);
334 p.drawLine(x, get_visual_y() + extents.first,
335 x, get_visual_y() + extents.second);
336 p.setRenderHint(QPainter::Antialiasing, was_antialiased);
337}
338
339void Trace::create_popup_form()
340{
341 // Clear the layout
342
343 // Transfer the layout and the child widgets to a temporary widget
344 // which we delete after the event was handled. This way, the layout
345 // and all widgets contained therein are deleted after the event was
346 // handled, leaving the parent popup_ time to handle the change.
347 if (popup_form_) {
348 QWidget *suicidal = new QWidget();
349 suicidal->setLayout(popup_->layout());
350 suicidal->deleteLater();
351 }
352
353 // Repopulate the popup
354 widgets::QWidthAdjustingScrollArea* scrollarea = new widgets::QWidthAdjustingScrollArea();
355 QWidget* scrollarea_content = new QWidget(scrollarea);
356
357 scrollarea->setWidget(scrollarea_content);
358 scrollarea->setWidgetResizable(true);
359 scrollarea->setContentsMargins(0, 0, 0, 0);
360 scrollarea->setFrameShape(QFrame::NoFrame);
361 scrollarea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
362 scrollarea_content->setContentsMargins(0, 0, 0, 0);
363
364 popup_->setLayout(new QVBoxLayout());
365 popup_->layout()->addWidget(scrollarea);
366 popup_->layout()->setContentsMargins(0, 0, 0, 0);
367
368 popup_form_ = new QFormLayout(scrollarea_content);
369 popup_form_->setSizeConstraint(QLayout::SetMinAndMaxSize);
370
371 populate_popup_form(popup_, popup_form_);
372}
373
374void Trace::populate_popup_form(QWidget *parent, QFormLayout *form)
375{
376 QLineEdit *const name_edit = new QLineEdit(parent);
377 name_edit->setText(base_->name());
378 connect(name_edit, SIGNAL(textChanged(const QString&)),
379 this, SLOT(on_nameedit_changed(const QString&)));
380 form->addRow(tr("Name"), name_edit);
381
382 add_color_option(parent, form);
383}
384
385void Trace::on_name_changed(const QString &text)
386{
387 /* This event handler is called by SignalBase when the name was changed there */
388 (void)text;
389
390 if (owner_) {
391 owner_->extents_changed(true, false);
392 owner_->row_item_appearance_changed(true, false);
393 }
394}
395
396void Trace::on_color_changed(const QColor &color)
397{
398 /* This event handler is called by SignalBase when the color was changed there */
399 (void)color;
400
401 if (owner_)
402 owner_->row_item_appearance_changed(true, true);
403}
404
405void Trace::on_popup_closed()
406{
407 popup_ = nullptr;
408 popup_form_ = nullptr;
409}
410
411void Trace::on_nameedit_changed(const QString &name)
412{
413 /* This event handler notifies SignalBase that the name changed */
414 base_->set_name(name);
415}
416
417void Trace::on_coloredit_changed(const QColor &color)
418{
419 /* This event handler notifies SignalBase that the color changed */
420 base_->set_color(color);
421}
422
423void Trace::on_create_marker_here() const
424{
425 View *view = owner_->view();
426 assert(view);
427
428 const Ruler *ruler = view->ruler();
429 QPoint p = ruler->mapFrom(view, QPoint(context_menu_x_pos_, 0));
430
431 view->add_flag(ruler->get_absolute_time_from_x_pos(p.x()));
432}
433
434} // namespace trace
435} // namespace views
436} // namespace pv