]> sigrok.org Git - pulseview.git/blob - pv/views/trace/trace.cpp
Fix random clazy warnings
[pulseview.git] / pv / views / trace / trace.cpp
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
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
53 Trace::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
73 Trace::~Trace()
74 {
75         GlobalSettings::remove_change_handler(this);
76 }
77
78 shared_ptr<data::SignalBase> Trace::base() const
79 {
80         return base_;
81 }
82
83 bool 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
92 bool 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
103 void 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
111 void 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
121 void 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
178 QMenu* Trace::create_header_context_menu(QWidget *parent)
179 {
180         QMenu *const menu = ViewItem::create_header_context_menu(parent);
181
182         return menu;
183 }
184
185 QMenu* 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()) {
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
213 pv::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
228 QRectF 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
244 QRectF 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
258 void Trace::set_current_segment(const int segment)
259 {
260         current_segment_ = segment;
261 }
262
263 int Trace::get_current_segment() const
264 {
265         return current_segment_;
266 }
267
268 void 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
276 void 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
293 void Trace::paint_axis(QPainter &p, ViewItemPaintParams &pp, int y)
294 {
295         p.setRenderHint(QPainter::Antialiasing, false);
296
297         p.setPen(axis_pen_);
298         p.drawLine(QPointF(pp.left(), y), QPointF(pp.right(), y));
299
300         p.setRenderHint(QPainter::Antialiasing, true);
301 }
302
303 void Trace::add_color_option(QWidget *parent, QFormLayout *form)
304 {
305         using pv::widgets::ColorButton;
306
307         ColorButton *const color_button = new ColorButton(
308                 TracePalette::Rows, TracePalette::Cols, parent);
309         color_button->set_palette(TracePalette::Colors);
310         color_button->set_color(base_->color());
311         connect(color_button, SIGNAL(selected(const QColor&)),
312                 this, SLOT(on_coloredit_changed(const QColor&)));
313
314         form->addRow(tr("Color"), color_button);
315 }
316
317 void Trace::paint_hover_marker(QPainter &p)
318 {
319         const View *view = owner_->view();
320         assert(view);
321
322         const int x = view->hover_point().x();
323
324         if (x == -1)
325                 return;
326
327         p.setPen(QPen(QColor(Qt::lightGray)));
328
329         const pair<int, int> extents = v_extents();
330
331         p.setRenderHint(QPainter::Antialiasing, false);
332         p.drawLine(x, get_visual_y() + extents.first,
333                 x, get_visual_y() + extents.second);
334         p.setRenderHint(QPainter::Antialiasing, true);
335 }
336
337 void Trace::create_popup_form()
338 {
339         // Clear the layout
340
341         // Transfer the layout and the child widgets to a temporary widget
342         // which we delete after the event was handled. This way, the layout
343         // and all widgets contained therein are deleted after the event was
344         // handled, leaving the parent popup_ time to handle the change.
345         if (popup_form_) {
346                 QWidget *suicidal = new QWidget();
347                 suicidal->setLayout(popup_form_);
348                 suicidal->deleteLater();
349         }
350
351         // Repopulate the popup
352         popup_form_ = new QFormLayout(popup_);
353         popup_->setLayout(popup_form_);
354         populate_popup_form(popup_, popup_form_);
355 }
356
357 void Trace::populate_popup_form(QWidget *parent, QFormLayout *form)
358 {
359         QLineEdit *const name_edit = new QLineEdit(parent);
360         name_edit->setText(base_->name());
361         connect(name_edit, SIGNAL(textChanged(const QString&)),
362                 this, SLOT(on_nameedit_changed(const QString&)));
363         form->addRow(tr("Name"), name_edit);
364
365         add_color_option(parent, form);
366 }
367
368 void Trace::on_name_changed(const QString &text)
369 {
370         /* This event handler is called by SignalBase when the name was changed there */
371         (void)text;
372
373         if (owner_) {
374                 owner_->extents_changed(true, false);
375                 owner_->row_item_appearance_changed(true, false);
376         }
377 }
378
379 void Trace::on_color_changed(const QColor &color)
380 {
381         /* This event handler is called by SignalBase when the color was changed there */
382         (void)color;
383
384         if (owner_)
385                 owner_->row_item_appearance_changed(true, true);
386 }
387
388 void Trace::on_popup_closed()
389 {
390         popup_ = nullptr;
391         popup_form_ = nullptr;
392 }
393
394 void Trace::on_nameedit_changed(const QString &name)
395 {
396         /* This event handler notifies SignalBase that the name changed */
397         base_->set_name(name);
398 }
399
400 void Trace::on_coloredit_changed(const QColor &color)
401 {
402         /* This event handler notifies SignalBase that the color changed */
403         base_->set_color(color);
404 }
405
406 void Trace::on_create_marker_here() const
407 {
408         View *view = owner_->view();
409         assert(view);
410
411         const Ruler *ruler = view->ruler();
412         QPoint p = ruler->mapFrom(view, QPoint(context_menu_x_pos_, 0));
413
414         view->add_flag(ruler->get_time_from_x_pos(p.x()));
415 }
416
417 } // namespace trace
418 } // namespace views
419 } // namespace pv