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