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