]> sigrok.org Git - pulseview.git/blob - pv/views/trace/trace.cpp
Trace: Force trace replaint when a setting changes
[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
30 #include "trace.hpp"
31 #include "tracepalette.hpp"
32 #include "view.hpp"
33
34 #include "pv/globalsettings.hpp"
35 #include "pv/widgets/colorbutton.hpp"
36 #include "pv/widgets/popup.hpp"
37
38 using std::pair;
39 using std::shared_ptr;
40
41 namespace pv {
42 namespace views {
43 namespace trace {
44
45 const QPen Trace::AxisPen(QColor(0, 0, 0, 30 * 256 / 100));
46 const int Trace::LabelHitPadding = 2;
47
48 const QColor Trace::BrightGrayBGColor = QColor(0, 0, 0, 10 * 255 / 100);
49 const QColor Trace::DarkGrayBGColor = QColor(0, 0, 0, 15 * 255 / 100);
50
51 Trace::Trace(shared_ptr<data::SignalBase> channel) :
52         base_(channel),
53         axis_pen_(AxisPen),
54         segment_display_mode_(ShowLastSegmentOnly),  // Will be overwritten by View
55         current_segment_(0),
56         popup_(nullptr),
57         popup_form_(nullptr)
58 {
59         connect(channel.get(), SIGNAL(name_changed(const QString&)),
60                 this, SLOT(on_name_changed(const QString&)));
61         connect(channel.get(), SIGNAL(color_changed(const QColor&)),
62                 this, SLOT(on_color_changed(const QColor&)));
63
64         GlobalSettings::add_change_handler(this);
65
66         GlobalSettings settings;
67         show_hover_marker_ =
68                 settings.value(GlobalSettings::Key_View_ShowHoverMarker).toBool();
69 }
70
71 Trace::~Trace()
72 {
73         GlobalSettings::remove_change_handler(this);
74 }
75
76 shared_ptr<data::SignalBase> Trace::base() const
77 {
78         return base_;
79 }
80
81 bool Trace::is_selectable(QPoint pos) const
82 {
83         // True if the header was clicked, false if the trace area was clicked
84         const View *view = owner_->view();
85         assert(view);
86
87         return (pos.x() <= view->header_width());
88 }
89
90 bool Trace::is_draggable(QPoint pos) const
91 {
92         // While the header label that belongs to this trace is draggable,
93         // the trace itself shall not be. Hence we return true if the header
94         // was clicked and false if the trace area was clicked
95         const View *view = owner_->view();
96         assert(view);
97
98         return (pos.x() <= view->header_width());
99 }
100
101 void Trace::set_segment_display_mode(SegmentDisplayMode mode)
102 {
103         segment_display_mode_ = mode;
104
105         if (owner_)
106                 owner_->row_item_appearance_changed(true, true);
107 }
108
109 void Trace::on_setting_changed(const QString &key, const QVariant &value)
110 {
111         if (key == GlobalSettings::Key_View_ShowHoverMarker)
112                 show_hover_marker_ = value.toBool();
113
114         // Force a repaint since many options alter the way traces look
115         if (owner_)
116                 owner_->row_item_appearance_changed(false, true);
117 }
118
119 void Trace::paint_label(QPainter &p, const QRect &rect, bool hover)
120 {
121         const int y = get_visual_y();
122
123         p.setBrush(base_->color());
124
125         if (!enabled())
126                 return;
127
128         const QRectF r = label_rect(rect);
129
130         // When selected, move the arrow to the left so that the border can show
131         const QPointF offs = (selected()) ? QPointF(-2, 0) : QPointF(0, 0);
132
133         // Paint the label
134         const float label_arrow_length = r.height() / 2;
135         QPointF points[] = {
136                 offs + r.topLeft(),
137                 offs + QPointF(r.right() - label_arrow_length, r.top()),
138                 offs + QPointF(r.right(), y),
139                 offs + QPointF(r.right() - label_arrow_length, r.bottom()),
140                 offs + r.bottomLeft()
141         };
142         QPointF highlight_points[] = {
143                 offs + QPointF(r.left() + 1, r.top() + 1),
144                 offs + QPointF(r.right() - label_arrow_length, r.top() + 1),
145                 offs + QPointF(r.right() - 1, y),
146                 offs + QPointF(r.right() - label_arrow_length, r.bottom() - 1),
147                 offs + QPointF(r.left() + 1, r.bottom() - 1)
148         };
149
150         if (selected()) {
151                 p.setPen(highlight_pen());
152                 p.setBrush(Qt::transparent);
153                 p.drawPolygon(points, countof(points));
154         }
155
156         p.setPen(Qt::transparent);
157         p.setBrush(hover ? base_->color().lighter() : base_->color());
158         p.drawPolygon(points, countof(points));
159
160         p.setPen(base_->color().lighter());
161         p.setBrush(Qt::transparent);
162         p.drawPolygon(highlight_points, countof(highlight_points));
163
164         p.setPen(base_->color().darker());
165         p.setBrush(Qt::transparent);
166         p.drawPolygon(points, countof(points));
167
168         // Paint the text
169         p.setPen(select_text_color(base_->color()));
170         p.setFont(QApplication::font());
171         p.drawText(QRectF(r.x(), r.y(),
172                 r.width() - label_arrow_length, r.height()),
173                 Qt::AlignCenter | Qt::AlignVCenter, base_->name());
174 }
175
176 QMenu* Trace::create_header_context_menu(QWidget *parent)
177 {
178         QMenu *const menu = ViewItem::create_header_context_menu(parent);
179
180         return menu;
181 }
182
183 pv::widgets::Popup* Trace::create_popup(QWidget *parent)
184 {
185         using pv::widgets::Popup;
186
187         popup_ = new Popup(parent);
188         popup_->set_position(parent->mapToGlobal(
189                 drag_point(parent->rect())), Popup::Right);
190
191         create_popup_form();
192
193         connect(popup_, SIGNAL(closed()), this, SLOT(on_popup_closed()));
194
195         return popup_;
196 }
197
198 QRectF Trace::label_rect(const QRectF &rect) const
199 {
200         QFontMetrics m(QApplication::font());
201         const QSize text_size(
202                 m.boundingRect(QRect(), 0, base_->name()).width(), m.height());
203         const QSizeF label_size(
204                 text_size.width() + LabelPadding.width() * 2,
205                 ceilf((text_size.height() + LabelPadding.height() * 2) / 2) * 2);
206         const float half_height = label_size.height() / 2;
207         return QRectF(
208                 rect.right() - half_height - label_size.width() - 0.5,
209                 get_visual_y() + 0.5f - half_height,
210                 label_size.width() + half_height,
211                 label_size.height());
212 }
213
214 QRectF Trace::hit_box_rect(const ViewItemPaintParams &pp) const
215 {
216         // This one is only for the trace itself, excluding the header area
217         const View *view = owner_->view();
218         assert(view);
219
220         pair<int, int> extents = v_extents();
221         const int top = pp.top() + get_visual_y() + extents.first;
222         const int height = extents.second - extents.first;
223
224         return QRectF(pp.left() + view->header_width(), top,
225                 pp.width() - view->header_width(), height);
226 }
227
228 void Trace::set_current_segment(const int segment)
229 {
230         current_segment_ = segment;
231 }
232
233 int Trace::get_current_segment() const
234 {
235         return current_segment_;
236 }
237
238 void Trace::hover_point_changed(const QPoint &hp)
239 {
240         (void)hp;
241
242         if (owner_)
243                 owner_->row_item_appearance_changed(false, true);
244 }
245
246 void Trace::paint_back(QPainter &p, ViewItemPaintParams &pp)
247 {
248         const View *view = owner_->view();
249         assert(view);
250
251         if (view->colored_bg())
252                 p.setBrush(base_->bgcolor());
253         else
254                 p.setBrush(pp.next_bg_color_state() ? BrightGrayBGColor : DarkGrayBGColor);
255
256         p.setPen(QPen(Qt::NoPen));
257
258         const pair<int, int> extents = v_extents();
259         p.drawRect(pp.left(), get_visual_y() + extents.first,
260                 pp.width(), extents.second - extents.first);
261 }
262
263 void Trace::paint_axis(QPainter &p, ViewItemPaintParams &pp, int y)
264 {
265         p.setRenderHint(QPainter::Antialiasing, false);
266
267         p.setPen(axis_pen_);
268         p.drawLine(QPointF(pp.left(), y), QPointF(pp.right(), y));
269
270         p.setRenderHint(QPainter::Antialiasing, true);
271 }
272
273 void Trace::add_color_option(QWidget *parent, QFormLayout *form)
274 {
275         using pv::widgets::ColorButton;
276
277         ColorButton *const color_button = new ColorButton(
278                 TracePalette::Rows, TracePalette::Cols, parent);
279         color_button->set_palette(TracePalette::Colors);
280         color_button->set_color(base_->color());
281         connect(color_button, SIGNAL(selected(const QColor&)),
282                 this, SLOT(on_coloredit_changed(const QColor&)));
283
284         form->addRow(tr("Color"), color_button);
285 }
286
287 void Trace::paint_hover_marker(QPainter &p)
288 {
289         const View *view = owner_->view();
290         assert(view);
291
292         const int x = view->hover_point().x();
293
294         if (x == -1)
295                 return;
296
297         p.setPen(QPen(QColor(Qt::lightGray)));
298
299         const pair<int, int> extents = v_extents();
300
301         p.setRenderHint(QPainter::Antialiasing, false);
302         p.drawLine(x, get_visual_y() + extents.first,
303                 x, get_visual_y() + extents.second);
304         p.setRenderHint(QPainter::Antialiasing, true);
305 }
306
307 void Trace::create_popup_form()
308 {
309         // Clear the layout
310
311         // Transfer the layout and the child widgets to a temporary widget
312         // which we delete after the event was handled. This way, the layout
313         // and all widgets contained therein are deleted after the event was
314         // handled, leaving the parent popup_ time to handle the change.
315         if (popup_form_) {
316                 QWidget *suicidal = new QWidget();
317                 suicidal->setLayout(popup_form_);
318                 suicidal->deleteLater();
319         }
320
321         // Repopulate the popup
322         popup_form_ = new QFormLayout(popup_);
323         popup_->setLayout(popup_form_);
324         populate_popup_form(popup_, popup_form_);
325 }
326
327 void Trace::populate_popup_form(QWidget *parent, QFormLayout *form)
328 {
329         QLineEdit *const name_edit = new QLineEdit(parent);
330         name_edit->setText(base_->name());
331         connect(name_edit, SIGNAL(textChanged(const QString&)),
332                 this, SLOT(on_nameedit_changed(const QString&)));
333         form->addRow(tr("Name"), name_edit);
334
335         add_color_option(parent, form);
336 }
337
338 void Trace::on_name_changed(const QString &text)
339 {
340         /* This event handler is called by SignalBase when the name was changed there */
341         (void)text;
342
343         if (owner_) {
344                 owner_->extents_changed(true, false);
345                 owner_->row_item_appearance_changed(true, false);
346         }
347 }
348
349 void Trace::on_color_changed(const QColor &color)
350 {
351         /* This event handler is called by SignalBase when the color was changed there */
352         (void)color;
353
354         if (owner_)
355                 owner_->row_item_appearance_changed(true, true);
356 }
357
358 void Trace::on_popup_closed()
359 {
360         popup_ = nullptr;
361         popup_form_ = nullptr;
362 }
363
364 void Trace::on_nameedit_changed(const QString &name)
365 {
366         /* This event handler notifies SignalBase that the name changed */
367         base_->set_name(name);
368 }
369
370 void Trace::on_coloredit_changed(const QColor &color)
371 {
372         /* This event handler notifies SignalBase that the color changed */
373         base_->set_color(color);
374 }
375
376 } // namespace trace
377 } // namespace views
378 } // namespace pv