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