]> sigrok.org Git - pulseview.git/blame - pv/view/trace.cpp
DecodeTrace: Speed up annotation drawing
[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
JH
38namespace pv {
39namespace view {
40
fe08b6e8 41const QPen Trace::AxisPen(QColor(128, 128, 128, 64));
931f20b0
JH
42const int Trace::LabelHitPadding = 2;
43
ac0708fb
SA
44const QColor Trace::DarkBGColour(235, 235, 235); // Quite light grey
45const QColor Trace::BrightBGColour(245, 245, 245); // Very light grey
46
83c23cc9 47Trace::Trace(QString name) :
8dbbc7f0 48 name_(name),
574c568d 49 coloured_bg_(true), // Default setting is set in MainWindow::setup_ui()
4c60462b
JH
50 popup_(nullptr),
51 popup_form_(nullptr)
931f20b0
JH
52{
53}
54
0a47889b 55QString Trace::name() const
931f20b0 56{
8dbbc7f0 57 return name_;
931f20b0
JH
58}
59
60void Trace::set_name(QString name)
61{
8dbbc7f0 62 name_ = name;
931f20b0
JH
63}
64
06149d43 65QColor Trace::colour() const
931f20b0 66{
8dbbc7f0 67 return colour_;
931f20b0
JH
68}
69
70void Trace::set_colour(QColor colour)
71{
8dbbc7f0 72 colour_ = colour;
99af6802
SA
73
74 bgcolour_ = colour;
75 bgcolour_.setAlpha(20);
931f20b0
JH
76}
77
574c568d
SA
78void Trace::set_coloured_bg(bool state)
79{
80 coloured_bg_ = state;
81}
82
b3f44329 83void Trace::paint_label(QPainter &p, const QRect &rect, bool hover)
01fd3263 84{
be9e7b4b 85 const int y = get_visual_y();
01fd3263 86
8dbbc7f0 87 p.setBrush(colour_);
931f20b0
JH
88
89 if (!enabled())
90 return;
91
b3f44329 92 const QRectF r = label_rect(rect);
931f20b0
JH
93
94 // Paint the label
b5cb6c35 95 const float label_arrow_length = r.height() / 2;
931f20b0 96 const QPointF points[] = {
1053d05c 97 r.topLeft(),
b5cb6c35
JH
98 QPointF(r.right() - label_arrow_length, r.top()),
99 QPointF(r.right(), y),
100 QPointF(r.right() - label_arrow_length, r.bottom()),
1053d05c 101 r.bottomLeft()
931f20b0 102 };
931f20b0 103 const QPointF highlight_points[] = {
1053d05c 104 QPointF(r.left() + 1, r.top() + 1),
b5cb6c35
JH
105 QPointF(r.right() - label_arrow_length, r.top() + 1),
106 QPointF(r.right() - 1, y),
107 QPointF(r.right() - label_arrow_length, r.bottom() - 1),
1053d05c 108 QPointF(r.left() + 1, r.bottom() - 1)
931f20b0
JH
109 };
110
111 if (selected()) {
112 p.setPen(highlight_pen());
113 p.setBrush(Qt::transparent);
114 p.drawPolygon(points, countof(points));
115 }
116
117 p.setPen(Qt::transparent);
8dbbc7f0 118 p.setBrush(hover ? colour_.lighter() : colour_);
931f20b0
JH
119 p.drawPolygon(points, countof(points));
120
8dbbc7f0 121 p.setPen(colour_.lighter());
931f20b0
JH
122 p.setBrush(Qt::transparent);
123 p.drawPolygon(highlight_points, countof(highlight_points));
124
8dbbc7f0 125 p.setPen(colour_.darker());
931f20b0
JH
126 p.setBrush(Qt::transparent);
127 p.drawPolygon(points, countof(points));
128
129 // Paint the text
d8d724cc 130 p.setPen(select_text_colour(colour_));
d7c0ca4a 131 p.setFont(QApplication::font());
b5cb6c35
JH
132 p.drawText(QRectF(r.x(), r.y(),
133 r.width() - label_arrow_length, r.height()),
8dbbc7f0 134 Qt::AlignCenter | Qt::AlignVCenter, name_);
931f20b0
JH
135}
136
9b6378f1
JH
137QMenu* Trace::create_context_menu(QWidget *parent)
138{
26e3af6b 139 QMenu *const menu = ViewItem::create_context_menu(parent);
9b6378f1 140
9b6378f1
JH
141 return menu;
142}
143
569d1e41
JH
144pv::widgets::Popup* Trace::create_popup(QWidget *parent)
145{
146 using pv::widgets::Popup;
20eaae39 147
8dbbc7f0 148 popup_ = new Popup(parent);
786b7678
JH
149 popup_->set_position(parent->mapToGlobal(
150 point(parent->rect())), Popup::Right);
20eaae39 151
37fd11b1 152 create_popup_form();
20eaae39 153
8dbbc7f0 154 connect(popup_, SIGNAL(closed()),
20eaae39
JH
155 this, SLOT(on_popup_closed()));
156
8dbbc7f0 157 return popup_;
569d1e41
JH
158}
159
b3f44329 160QRectF Trace::label_rect(const QRectF &rect) const
d7c0ca4a
JH
161{
162 using pv::view::View;
163
d7c0ca4a
JH
164 QFontMetrics m(QApplication::font());
165 const QSize text_size(
c0096500 166 m.boundingRect(QRect(), 0, name_).width(), m.height());
d7c0ca4a 167 const QSizeF label_size(
ec39632d
JH
168 text_size.width() + LabelPadding.width() * 2,
169 ceilf((text_size.height() + LabelPadding.height() * 2) / 2) * 2);
b5cb6c35 170 const float half_height = label_size.height() / 2;
d7c0ca4a 171 return QRectF(
b3f44329 172 rect.right() - half_height - label_size.width() - 0.5,
be9e7b4b 173 get_visual_y() + 0.5f - half_height,
b5cb6c35
JH
174 label_size.width() + half_height,
175 label_size.height());
d7c0ca4a
JH
176}
177
cbf0f87e 178QRectF Trace::hit_box_rect(const ViewItemPaintParams &pp) const
7708eb92
JH
179{
180 const float h = QFontMetrics(QApplication::font()).height();
cbf0f87e
JH
181 return QRectF(pp.left(), get_visual_y() - h / 2.0f,
182 pp.width(), h);
7708eb92
JH
183}
184
99af6802
SA
185void Trace::paint_back(QPainter &p, const ViewItemPaintParams &pp)
186{
ac0708fb 187 if (coloured_bg_)
574c568d 188 p.setBrush(bgcolour_);
ac0708fb
SA
189 else
190 p.setBrush(bgcolour_state_ ? BrightBGColour : DarkBGColour);
99af6802 191
ac0708fb 192 p.setPen(QPen(Qt::NoPen));
99af6802 193
ac0708fb 194 const std::pair<int, int> extents = v_extents();
99af6802 195
ac0708fb
SA
196 const int x = 0;
197 const int y = get_visual_y() + extents.first;
198 const int w = pp.right() - pp.left();
199 const int h = extents.second - extents.first;
200
201 p.drawRect(x, y, w, h);
99af6802
SA
202}
203
5b5fa4da 204void Trace::paint_axis(QPainter &p, const ViewItemPaintParams &pp, int y)
fe08b6e8
JH
205{
206 p.setPen(AxisPen);
97904bf7 207 p.drawLine(QPointF(pp.left(), y + 0.5f), QPointF(pp.right(), y + 0.5f));
fe08b6e8
JH
208}
209
91e8bf08
JH
210void Trace::add_colour_option(QWidget *parent, QFormLayout *form)
211{
212 using pv::widgets::ColourButton;
213
214 ColourButton *const colour_button = new ColourButton(
215 TracePalette::Rows, TracePalette::Cols, parent);
216 colour_button->set_palette(TracePalette::Colours);
8dbbc7f0 217 colour_button->set_colour(colour_);
91e8bf08
JH
218 connect(colour_button, SIGNAL(selected(const QColor&)),
219 this, SLOT(on_colour_changed(const QColor&)));
220
221 form->addRow(tr("Colour"), colour_button);
222}
223
37fd11b1
JH
224void Trace::create_popup_form()
225{
226 // Clear the layout
227
228 // Transfer the layout and the child widgets to a temporary widget
229 // which then goes out of scope destroying the layout and all the child
230 // widgets.
8dbbc7f0
JH
231 if (popup_form_)
232 QWidget().setLayout(popup_form_);
37fd11b1
JH
233
234 // Repopulate the popup
8dbbc7f0
JH
235 popup_form_ = new QFormLayout(popup_);
236 popup_->setLayout(popup_form_);
237 populate_popup_form(popup_, popup_form_);
37fd11b1
JH
238}
239
569d1e41
JH
240void Trace::populate_popup_form(QWidget *parent, QFormLayout *form)
241{
68456dab 242 QLineEdit *const name_edit = new QLineEdit(parent);
8dbbc7f0 243 name_edit->setText(name_);
68456dab
JH
244 connect(name_edit, SIGNAL(textChanged(const QString&)),
245 this, SLOT(on_text_changed(const QString&)));
246 form->addRow(tr("Name"), name_edit);
91e8bf08
JH
247
248 add_colour_option(parent, form);
569d1e41
JH
249}
250
20eaae39
JH
251void Trace::on_popup_closed()
252{
4c60462b
JH
253 popup_ = nullptr;
254 popup_form_ = nullptr;
20eaae39
JH
255}
256
68456dab
JH
257void Trace::on_text_changed(const QString &text)
258{
259 set_name(text);
32218d3e 260
8dbbc7f0
JH
261 if (owner_)
262 owner_->extents_changed(true, false);
68456dab 263}
9b6378f1 264
91e8bf08
JH
265void Trace::on_colour_changed(const QColor &colour)
266{
267 set_colour(colour);
32218d3e 268
8dbbc7f0 269 if (owner_)
99af6802 270 owner_->row_item_appearance_changed(true, true);
91e8bf08
JH
271}
272
931f20b0
JH
273} // namespace view
274} // namespace pv