]> sigrok.org Git - pulseview.git/blame - pv/views/trace/trace.cpp
View: Fixes related to multi-segment display
[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
JH
28#include <QLineEdit>
29
2acdb232
JH
30#include "trace.hpp"
31#include "tracepalette.hpp"
32#include "view.hpp"
931f20b0 33
24c29d4f
SA
34#include "pv/globalsettings.hpp"
35#include "pv/widgets/colourbutton.hpp"
36#include "pv/widgets/popup.hpp"
569d1e41 37
6f925ba9
UH
38using std::pair;
39using std::shared_ptr;
40
931f20b0 41namespace pv {
f4e57597 42namespace views {
1573bf16 43namespace trace {
931f20b0 44
c063290a 45const QPen Trace::AxisPen(QColor(0, 0, 0, 30 * 256 / 100));
931f20b0
JH
46const int Trace::LabelHitPadding = 2;
47
c063290a
UH
48const QColor Trace::BrightGrayBGColour = QColor(0, 0, 0, 10 * 255 / 100);
49const QColor Trace::DarkGrayBGColour = QColor(0, 0, 0, 15 * 255 / 100);
ac0708fb 50
6f925ba9 51Trace::Trace(shared_ptr<data::SignalBase> channel) :
73a25a6e 52 base_(channel),
561ba3ae 53 axis_pen_(AxisPen),
7daebd05 54 segment_display_mode_(ShowLastSegmentOnly), // Will be overwritten by View
2749b858 55 current_segment_(0),
4c60462b
JH
56 popup_(nullptr),
57 popup_form_(nullptr)
931f20b0 58{
bf0edd2b
SA
59 connect(channel.get(), SIGNAL(name_changed(const QString&)),
60 this, SLOT(on_name_changed(const QString&)));
61 connect(channel.get(), SIGNAL(colour_changed(const QColor&)),
62 this, SLOT(on_colour_changed(const QColor&)));
931f20b0
JH
63}
64
5ed05b69
SA
65shared_ptr<data::SignalBase> Trace::base() const
66{
67 return base_;
68}
69
b3f44329 70void Trace::paint_label(QPainter &p, const QRect &rect, bool hover)
01fd3263 71{
be9e7b4b 72 const int y = get_visual_y();
01fd3263 73
73a25a6e 74 p.setBrush(base_->colour());
931f20b0
JH
75
76 if (!enabled())
77 return;
78
b3f44329 79 const QRectF r = label_rect(rect);
931f20b0 80
c1a6513b
SA
81 // When selected, move the arrow to the left so that the border can show
82 const QPointF offs = (selected()) ? QPointF(-2, 0) : QPointF(0, 0);
83
931f20b0 84 // Paint the label
b5cb6c35 85 const float label_arrow_length = r.height() / 2;
c1a6513b
SA
86 QPointF points[] = {
87 offs + r.topLeft(),
88 offs + QPointF(r.right() - label_arrow_length, r.top()),
89 offs + QPointF(r.right(), y),
90 offs + QPointF(r.right() - label_arrow_length, r.bottom()),
91 offs + r.bottomLeft()
931f20b0 92 };
c1a6513b
SA
93 QPointF highlight_points[] = {
94 offs + QPointF(r.left() + 1, r.top() + 1),
95 offs + QPointF(r.right() - label_arrow_length, r.top() + 1),
96 offs + QPointF(r.right() - 1, y),
97 offs + QPointF(r.right() - label_arrow_length, r.bottom() - 1),
98 offs + QPointF(r.left() + 1, r.bottom() - 1)
931f20b0
JH
99 };
100
101 if (selected()) {
102 p.setPen(highlight_pen());
103 p.setBrush(Qt::transparent);
104 p.drawPolygon(points, countof(points));
105 }
106
107 p.setPen(Qt::transparent);
73a25a6e 108 p.setBrush(hover ? base_->colour().lighter() : base_->colour());
931f20b0
JH
109 p.drawPolygon(points, countof(points));
110
73a25a6e 111 p.setPen(base_->colour().lighter());
931f20b0
JH
112 p.setBrush(Qt::transparent);
113 p.drawPolygon(highlight_points, countof(highlight_points));
114
73a25a6e 115 p.setPen(base_->colour().darker());
931f20b0
JH
116 p.setBrush(Qt::transparent);
117 p.drawPolygon(points, countof(points));
118
119 // Paint the text
73a25a6e 120 p.setPen(select_text_colour(base_->colour()));
d7c0ca4a 121 p.setFont(QApplication::font());
b5cb6c35
JH
122 p.drawText(QRectF(r.x(), r.y(),
123 r.width() - label_arrow_length, r.height()),
73a25a6e 124 Qt::AlignCenter | Qt::AlignVCenter, base_->name());
931f20b0
JH
125}
126
9b6378f1
JH
127QMenu* Trace::create_context_menu(QWidget *parent)
128{
26e3af6b 129 QMenu *const menu = ViewItem::create_context_menu(parent);
9b6378f1 130
9b6378f1
JH
131 return menu;
132}
133
569d1e41
JH
134pv::widgets::Popup* Trace::create_popup(QWidget *parent)
135{
136 using pv::widgets::Popup;
20eaae39 137
8dbbc7f0 138 popup_ = new Popup(parent);
786b7678 139 popup_->set_position(parent->mapToGlobal(
a3d5a7c7 140 drag_point(parent->rect())), Popup::Right);
20eaae39 141
37fd11b1 142 create_popup_form();
20eaae39 143
c063290a 144 connect(popup_, SIGNAL(closed()), this, SLOT(on_popup_closed()));
20eaae39 145
8dbbc7f0 146 return popup_;
569d1e41
JH
147}
148
b3f44329 149QRectF Trace::label_rect(const QRectF &rect) const
d7c0ca4a 150{
d7c0ca4a
JH
151 QFontMetrics m(QApplication::font());
152 const QSize text_size(
73a25a6e 153 m.boundingRect(QRect(), 0, base_->name()).width(), m.height());
d7c0ca4a 154 const QSizeF label_size(
ec39632d
JH
155 text_size.width() + LabelPadding.width() * 2,
156 ceilf((text_size.height() + LabelPadding.height() * 2) / 2) * 2);
b5cb6c35 157 const float half_height = label_size.height() / 2;
d7c0ca4a 158 return QRectF(
b3f44329 159 rect.right() - half_height - label_size.width() - 0.5,
be9e7b4b 160 get_visual_y() + 0.5f - half_height,
b5cb6c35
JH
161 label_size.width() + half_height,
162 label_size.height());
d7c0ca4a
JH
163}
164
2749b858
SA
165void Trace::set_current_segment(const int segment)
166{
167 current_segment_ = segment;
168}
169
170int Trace::get_current_segment() const
171{
172 return current_segment_;
173}
174
60938e04 175void Trace::paint_back(QPainter &p, ViewItemPaintParams &pp)
99af6802 176{
48995388
JH
177 const View *view = owner_->view();
178 assert(view);
179
180 if (view->coloured_bg())
73a25a6e 181 p.setBrush(base_->bgcolour());
ac0708fb 182 else
59ec98e2 183 p.setBrush(pp.next_bg_colour_state() ? BrightGrayBGColour : DarkGrayBGColour);
99af6802 184
ac0708fb 185 p.setPen(QPen(Qt::NoPen));
99af6802 186
6f925ba9 187 const pair<int, int> extents = v_extents();
857ebbef
JH
188 p.drawRect(pp.left(), get_visual_y() + extents.first,
189 pp.width(), extents.second - extents.first);
99af6802
SA
190}
191
60938e04 192void Trace::paint_axis(QPainter &p, ViewItemPaintParams &pp, int y)
fe08b6e8 193{
46a0cadc
SA
194 p.setRenderHint(QPainter::Antialiasing, false);
195
561ba3ae 196 p.setPen(axis_pen_);
46a0cadc
SA
197 p.drawLine(QPointF(pp.left(), y), QPointF(pp.right(), y));
198
199 p.setRenderHint(QPainter::Antialiasing, true);
fe08b6e8
JH
200}
201
91e8bf08
JH
202void Trace::add_colour_option(QWidget *parent, QFormLayout *form)
203{
204 using pv::widgets::ColourButton;
205
206 ColourButton *const colour_button = new ColourButton(
207 TracePalette::Rows, TracePalette::Cols, parent);
208 colour_button->set_palette(TracePalette::Colours);
73a25a6e 209 colour_button->set_colour(base_->colour());
91e8bf08 210 connect(colour_button, SIGNAL(selected(const QColor&)),
bf0edd2b 211 this, SLOT(on_colouredit_changed(const QColor&)));
91e8bf08
JH
212
213 form->addRow(tr("Colour"), colour_button);
214}
215
37fd11b1
JH
216void Trace::create_popup_form()
217{
218 // Clear the layout
219
220 // Transfer the layout and the child widgets to a temporary widget
e3004449
EO
221 // which we delete after the event was handled. This way, the layout
222 // and all widgets contained therein are deleted after the event was
223 // handled, leaving the parent popup_ time to handle the change.
224 if (popup_form_) {
225 QWidget *suicidal = new QWidget();
226 suicidal->setLayout(popup_form_);
227 suicidal->deleteLater();
228 }
37fd11b1
JH
229
230 // Repopulate the popup
8dbbc7f0
JH
231 popup_form_ = new QFormLayout(popup_);
232 popup_->setLayout(popup_form_);
233 populate_popup_form(popup_, popup_form_);
37fd11b1
JH
234}
235
569d1e41
JH
236void Trace::populate_popup_form(QWidget *parent, QFormLayout *form)
237{
68456dab 238 QLineEdit *const name_edit = new QLineEdit(parent);
73a25a6e 239 name_edit->setText(base_->name());
68456dab 240 connect(name_edit, SIGNAL(textChanged(const QString&)),
bf0edd2b 241 this, SLOT(on_nameedit_changed(const QString&)));
68456dab 242 form->addRow(tr("Name"), name_edit);
91e8bf08
JH
243
244 add_colour_option(parent, form);
569d1e41
JH
245}
246
bf0edd2b 247void Trace::set_name(QString name)
20eaae39 248{
73a25a6e 249 base_->set_name(name);
20eaae39
JH
250}
251
bf0edd2b 252void Trace::set_colour(QColor colour)
68456dab 253{
73a25a6e 254 base_->set_colour(colour);
bf0edd2b 255}
32218d3e 256
7daebd05
SA
257void Trace::set_segment_display_mode(SegmentDisplayMode mode)
258{
259 segment_display_mode_ = mode;
260
261 if (owner_)
262 owner_->row_item_appearance_changed(true, true);
263}
264
bf0edd2b
SA
265void Trace::on_name_changed(const QString &text)
266{
267 /* This event handler is called by SignalBase when the name was changed there */
268 (void)text;
269
270 if (owner_) {
8dbbc7f0 271 owner_->extents_changed(true, false);
bf0edd2b
SA
272 owner_->row_item_appearance_changed(true, false);
273 }
68456dab 274}
9b6378f1 275
91e8bf08
JH
276void Trace::on_colour_changed(const QColor &colour)
277{
bf0edd2b
SA
278 /* This event handler is called by SignalBase when the colour was changed there */
279 (void)colour;
32218d3e 280
8dbbc7f0 281 if (owner_)
99af6802 282 owner_->row_item_appearance_changed(true, true);
91e8bf08
JH
283}
284
bf0edd2b
SA
285void Trace::on_popup_closed()
286{
287 popup_ = nullptr;
288 popup_form_ = nullptr;
289}
290
291void Trace::on_nameedit_changed(const QString &name)
292{
293 /* This event handler notifies SignalBase that the name changed */
294 set_name(name);
295}
296
297void Trace::on_colouredit_changed(const QColor &colour)
298{
299 /* This event handler notifies SignalBase that the colour changed */
300 set_colour(colour);
301}
302
1573bf16 303} // namespace trace
f4e57597 304} // namespace views
931f20b0 305} // namespace pv