]> sigrok.org Git - pulseview.git/blame - pv/views/trace/trace.cpp
TabularDecView: Allow return/enter press and don't change scale
[pulseview.git] / pv / views / trace / trace.cpp
CommitLineData
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
40using std::pair;
41using std::shared_ptr;
42
931f20b0 43namespace pv {
f4e57597 44namespace views {
1573bf16 45namespace trace {
931f20b0 46
c063290a 47const QPen Trace::AxisPen(QColor(0, 0, 0, 30 * 256 / 100));
931f20b0
JH
48const int Trace::LabelHitPadding = 2;
49
641574bc
SA
50const QColor Trace::BrightGrayBGColor = QColor(0, 0, 0, 10 * 255 / 100);
51const QColor Trace::DarkGrayBGColor = QColor(0, 0, 0, 15 * 255 / 100);
ac0708fb 52
6f925ba9 53Trace::Trace(shared_ptr<data::SignalBase> channel) :
73a25a6e 54 base_(channel),
561ba3ae 55 axis_pen_(AxisPen),
7daebd05 56 segment_display_mode_(ShowLastSegmentOnly), // Will be overwritten by View
2749b858 57 current_segment_(0),
4c60462b
JH
58 popup_(nullptr),
59 popup_form_(nullptr)
931f20b0 60{
bf0edd2b
SA
61 connect(channel.get(), SIGNAL(name_changed(const QString&)),
62 this, SLOT(on_name_changed(const QString&)));
641574bc
SA
63 connect(channel.get(), SIGNAL(color_changed(const QColor&)),
64 this, SLOT(on_color_changed(const QColor&)));
1931b5f9
SA
65
66 GlobalSettings::add_change_handler(this);
67
68 GlobalSettings settings;
69 show_hover_marker_ =
70 settings.value(GlobalSettings::Key_View_ShowHoverMarker).toBool();
931f20b0
JH
71}
72
99c49526
SA
73Trace::~Trace()
74{
75 GlobalSettings::remove_change_handler(this);
76}
77
5ed05b69
SA
78shared_ptr<data::SignalBase> Trace::base() const
79{
80 return base_;
81}
82
cbd9ec7f
SA
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
119c5c23 92bool Trace::is_draggable(QPoint pos) const
cbd9ec7f
SA
93{
94 // While the header label that belongs to this trace is draggable,
119c5c23
SA
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());
cbd9ec7f
SA
101}
102
1931b5f9
SA
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();
39dce56b
SA
115
116 // Force a repaint since many options alter the way traces look
117 if (owner_)
118 owner_->row_item_appearance_changed(false, true);
1931b5f9
SA
119}
120
b3f44329 121void Trace::paint_label(QPainter &p, const QRect &rect, bool hover)
01fd3263 122{
be9e7b4b 123 const int y = get_visual_y();
01fd3263 124
641574bc 125 p.setBrush(base_->color());
931f20b0
JH
126
127 if (!enabled())
128 return;
129
b3f44329 130 const QRectF r = label_rect(rect);
931f20b0 131
c1a6513b
SA
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
931f20b0 135 // Paint the label
b5cb6c35 136 const float label_arrow_length = r.height() / 2;
c1a6513b
SA
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()
931f20b0 143 };
c1a6513b
SA
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)
931f20b0
JH
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);
641574bc 159 p.setBrush(hover ? base_->color().lighter() : base_->color());
931f20b0
JH
160 p.drawPolygon(points, countof(points));
161
641574bc 162 p.setPen(base_->color().lighter());
931f20b0
JH
163 p.setBrush(Qt::transparent);
164 p.drawPolygon(highlight_points, countof(highlight_points));
165
641574bc 166 p.setPen(base_->color().darker());
931f20b0
JH
167 p.setBrush(Qt::transparent);
168 p.drawPolygon(points, countof(points));
169
170 // Paint the text
641574bc 171 p.setPen(select_text_color(base_->color()));
d7c0ca4a 172 p.setFont(QApplication::font());
b5cb6c35
JH
173 p.drawText(QRectF(r.x(), r.y(),
174 r.width() - label_arrow_length, r.height()),
73a25a6e 175 Qt::AlignCenter | Qt::AlignVCenter, base_->name());
931f20b0
JH
176}
177
9e773fec 178QMenu* Trace::create_header_context_menu(QWidget *parent)
9b6378f1 179{
9e773fec 180 QMenu *const menu = ViewItem::create_header_context_menu(parent);
9b6378f1 181
9b6378f1
JH
182 return menu;
183}
184
79b53a1a
SA
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) {
7a0d99e6 194 for (QAction *action : default_menu->actions()) { // clazy:exclude=range-loop
79b53a1a
SA
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
569d1e41
JH
213pv::widgets::Popup* Trace::create_popup(QWidget *parent)
214{
215 using pv::widgets::Popup;
20eaae39 216
8dbbc7f0 217 popup_ = new Popup(parent);
786b7678 218 popup_->set_position(parent->mapToGlobal(
a3d5a7c7 219 drag_point(parent->rect())), Popup::Right);
20eaae39 220
37fd11b1 221 create_popup_form();
20eaae39 222
c063290a 223 connect(popup_, SIGNAL(closed()), this, SLOT(on_popup_closed()));
20eaae39 224
8dbbc7f0 225 return popup_;
569d1e41
JH
226}
227
b3f44329 228QRectF Trace::label_rect(const QRectF &rect) const
d7c0ca4a 229{
d7c0ca4a
JH
230 QFontMetrics m(QApplication::font());
231 const QSize text_size(
73a25a6e 232 m.boundingRect(QRect(), 0, base_->name()).width(), m.height());
d7c0ca4a 233 const QSizeF label_size(
ec39632d
JH
234 text_size.width() + LabelPadding.width() * 2,
235 ceilf((text_size.height() + LabelPadding.height() * 2) / 2) * 2);
b5cb6c35 236 const float half_height = label_size.height() / 2;
d7c0ca4a 237 return QRectF(
b3f44329 238 rect.right() - half_height - label_size.width() - 0.5,
be9e7b4b 239 get_visual_y() + 0.5f - half_height,
b5cb6c35
JH
240 label_size.width() + half_height,
241 label_size.height());
d7c0ca4a
JH
242}
243
d9b55cc8
SA
244QRectF Trace::hit_box_rect(const ViewItemPaintParams &pp) const
245{
cbd9ec7f
SA
246 // This one is only for the trace itself, excluding the header area
247 const View *view = owner_->view();
248 assert(view);
249
d9b55cc8
SA
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;
cbd9ec7f
SA
253
254 return QRectF(pp.left() + view->header_width(), top,
255 pp.width() - view->header_width(), height);
d9b55cc8
SA
256}
257
2749b858
SA
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
1931b5f9
SA
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
60938e04 276void Trace::paint_back(QPainter &p, ViewItemPaintParams &pp)
99af6802 277{
48995388
JH
278 const View *view = owner_->view();
279 assert(view);
280
641574bc
SA
281 if (view->colored_bg())
282 p.setBrush(base_->bgcolor());
ac0708fb 283 else
641574bc 284 p.setBrush(pp.next_bg_color_state() ? BrightGrayBGColor : DarkGrayBGColor);
99af6802 285
ac0708fb 286 p.setPen(QPen(Qt::NoPen));
99af6802 287
6f925ba9 288 const pair<int, int> extents = v_extents();
857ebbef
JH
289 p.drawRect(pp.left(), get_visual_y() + extents.first,
290 pp.width(), extents.second - extents.first);
99af6802
SA
291}
292
60938e04 293void Trace::paint_axis(QPainter &p, ViewItemPaintParams &pp, int y)
fe08b6e8 294{
f0b99257 295 bool was_antialiased = p.testRenderHint(QPainter::Antialiasing);
46a0cadc
SA
296 p.setRenderHint(QPainter::Antialiasing, false);
297
561ba3ae 298 p.setPen(axis_pen_);
46a0cadc
SA
299 p.drawLine(QPointF(pp.left(), y), QPointF(pp.right(), y));
300
f0b99257 301 p.setRenderHint(QPainter::Antialiasing, was_antialiased);
fe08b6e8
JH
302}
303
641574bc 304void Trace::add_color_option(QWidget *parent, QFormLayout *form)
91e8bf08 305{
641574bc 306 using pv::widgets::ColorButton;
91e8bf08 307
641574bc 308 ColorButton *const color_button = new ColorButton(
91e8bf08 309 TracePalette::Rows, TracePalette::Cols, parent);
641574bc
SA
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&)));
91e8bf08 314
641574bc 315 form->addRow(tr("Color"), color_button);
91e8bf08
JH
316}
317
1931b5f9
SA
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
f0b99257 332 bool was_antialiased = p.testRenderHint(QPainter::Antialiasing);
1931b5f9
SA
333 p.setRenderHint(QPainter::Antialiasing, false);
334 p.drawLine(x, get_visual_y() + extents.first,
335 x, get_visual_y() + extents.second);
f0b99257 336 p.setRenderHint(QPainter::Antialiasing, was_antialiased);
1931b5f9
SA
337}
338
37fd11b1
JH
339void Trace::create_popup_form()
340{
341 // Clear the layout
342
343 // Transfer the layout and the child widgets to a temporary widget
e3004449
EO
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();
f52ffd9d 349 suicidal->setLayout(popup_->layout());
e3004449
EO
350 suicidal->deleteLater();
351 }
37fd11b1
JH
352
353 // Repopulate the popup
f52ffd9d
SA
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
8dbbc7f0 371 populate_popup_form(popup_, popup_form_);
37fd11b1
JH
372}
373
569d1e41
JH
374void Trace::populate_popup_form(QWidget *parent, QFormLayout *form)
375{
68456dab 376 QLineEdit *const name_edit = new QLineEdit(parent);
73a25a6e 377 name_edit->setText(base_->name());
68456dab 378 connect(name_edit, SIGNAL(textChanged(const QString&)),
bf0edd2b 379 this, SLOT(on_nameedit_changed(const QString&)));
68456dab 380 form->addRow(tr("Name"), name_edit);
91e8bf08 381
641574bc 382 add_color_option(parent, form);
569d1e41
JH
383}
384
bf0edd2b
SA
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_) {
8dbbc7f0 391 owner_->extents_changed(true, false);
bf0edd2b
SA
392 owner_->row_item_appearance_changed(true, false);
393 }
68456dab 394}
9b6378f1 395
641574bc 396void Trace::on_color_changed(const QColor &color)
91e8bf08 397{
641574bc
SA
398 /* This event handler is called by SignalBase when the color was changed there */
399 (void)color;
32218d3e 400
8dbbc7f0 401 if (owner_)
99af6802 402 owner_->row_item_appearance_changed(true, true);
91e8bf08
JH
403}
404
bf0edd2b
SA
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 */
0acf629d 414 base_->set_name(name);
bf0edd2b
SA
415}
416
641574bc 417void Trace::on_coloredit_changed(const QColor &color)
bf0edd2b 418{
641574bc 419 /* This event handler notifies SignalBase that the color changed */
0acf629d 420 base_->set_color(color);
bf0edd2b
SA
421}
422
79b53a1a
SA
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
fe68068b 431 view->add_flag(ruler->get_absolute_time_from_x_pos(p.x()));
79b53a1a
SA
432}
433
1573bf16 434} // namespace trace
f4e57597 435} // namespace views
931f20b0 436} // namespace pv