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