]> sigrok.org Git - pulseview.git/blame - pv/view/trace.cpp
Made Probes popup reusable
[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>
24#include <math.h>
25
26#include "trace.h"
91e8bf08 27#include "tracepalette.h"
931f20b0
JH
28#include "view.h"
29
91e8bf08 30#include <pv/widgets/colourbutton.h>
569d1e41 31
931f20b0
JH
32namespace pv {
33namespace view {
34
fe08b6e8 35const QPen Trace::AxisPen(QColor(128, 128, 128, 64));
931f20b0
JH
36const int Trace::LabelHitPadding = 2;
37
38Trace::Trace(pv::SigSession &session, QString name) :
39 _session(session),
40 _name(name),
41 _v_offset(0)
42{
43}
44
45QString Trace::get_name() const
46{
47 return _name;
48}
49
50void Trace::set_name(QString name)
51{
52 _name = name;
53}
54
55QColor Trace::get_colour() const
56{
57 return _colour;
58}
59
60void Trace::set_colour(QColor colour)
61{
62 _colour = colour;
63}
64
65int Trace::get_v_offset() const
66{
67 return _v_offset;
68}
69
70void Trace::set_v_offset(int v_offset)
71{
72 _v_offset = v_offset;
73}
74
01fd3263 75void Trace::set_view(pv::view::View *view)
931f20b0 76{
01fd3263
JH
77 assert(view);
78 _view = view;
79}
80
fe08b6e8
JH
81void Trace::paint_back(QPainter &p, int left, int right)
82{
83 (void)p;
84 (void)left;
85 (void)right;
86}
87
88void Trace::paint_mid(QPainter &p, int left, int right)
89{
90 (void)p;
91 (void)left;
92 (void)right;
93}
94
95void Trace::paint_fore(QPainter &p, int left, int right)
96{
97 (void)p;
98 (void)left;
99 (void)right;
100}
101
01fd3263
JH
102void Trace::paint_label(QPainter &p, int right, bool hover)
103{
104 assert(_view);
105 const int y = _v_offset - _view->v_offset();
106
931f20b0
JH
107 p.setBrush(_colour);
108
109 if (!enabled())
110 return;
111
112 const QColor colour = get_colour();
113
114 compute_text_size(p);
01fd3263 115 const QRectF label_rect = get_label_rect(right);
931f20b0
JH
116
117 // Paint the label
118 const QPointF points[] = {
119 label_rect.topLeft(),
120 label_rect.topRight(),
121 QPointF(right, y),
122 label_rect.bottomRight(),
123 label_rect.bottomLeft()
124 };
125
126 const QPointF highlight_points[] = {
127 QPointF(label_rect.left() + 1, label_rect.top() + 1),
128 QPointF(label_rect.right(), label_rect.top() + 1),
129 QPointF(right - 1, y),
130 QPointF(label_rect.right(), label_rect.bottom() - 1),
131 QPointF(label_rect.left() + 1, label_rect.bottom() - 1)
132 };
133
134 if (selected()) {
135 p.setPen(highlight_pen());
136 p.setBrush(Qt::transparent);
137 p.drawPolygon(points, countof(points));
138 }
139
140 p.setPen(Qt::transparent);
141 p.setBrush(hover ? colour.lighter() : colour);
142 p.drawPolygon(points, countof(points));
143
144 p.setPen(colour.lighter());
145 p.setBrush(Qt::transparent);
146 p.drawPolygon(highlight_points, countof(highlight_points));
147
148 p.setPen(colour.darker());
149 p.setBrush(Qt::transparent);
150 p.drawPolygon(points, countof(points));
151
152 // Paint the text
410f9c81 153 p.setPen(get_text_colour());
931f20b0
JH
154 p.drawText(label_rect, Qt::AlignCenter | Qt::AlignVCenter, _name);
155}
156
01fd3263 157bool Trace::pt_in_label_rect(int left, int right, const QPoint &point)
931f20b0
JH
158{
159 (void)left;
160
01fd3263 161 const QRectF label = get_label_rect(right);
931f20b0
JH
162 return QRectF(
163 QPointF(label.left() - LabelHitPadding,
164 label.top() - LabelHitPadding),
165 QPointF(right, label.bottom() + LabelHitPadding)
166 ).contains(point);
167}
168
9b6378f1
JH
169QMenu* Trace::create_context_menu(QWidget *parent)
170{
171 QMenu *const menu = SelectableItem::create_context_menu(parent);
172
9b6378f1
JH
173 return menu;
174}
175
569d1e41
JH
176pv::widgets::Popup* Trace::create_popup(QWidget *parent)
177{
178 using pv::widgets::Popup;
179 Popup *const popup = new Popup(parent);
180 QFormLayout *const form = new QFormLayout(popup);
181 popup->setLayout(form);
182 populate_popup_form(popup, form);
183 return popup;
184}
185
fe08b6e8
JH
186int Trace::get_y() const
187{
188 return _v_offset - _view->v_offset();
189}
190
410f9c81
JH
191QColor Trace::get_text_colour() const
192{
193 return (_colour.lightness() > 64) ? Qt::black : Qt::white;
194}
195
fe08b6e8
JH
196void Trace::paint_axis(QPainter &p, int y, int left, int right)
197{
198 p.setPen(AxisPen);
199 p.drawLine(QPointF(left, y + 0.5f), QPointF(right, y + 0.5f));
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);
209 colour_button->set_colour(_colour);
210 connect(colour_button, SIGNAL(selected(const QColor&)),
211 this, SLOT(on_colour_changed(const QColor&)));
212
213 form->addRow(tr("Colour"), colour_button);
214}
215
569d1e41
JH
216void Trace::populate_popup_form(QWidget *parent, QFormLayout *form)
217{
68456dab
JH
218 QLineEdit *const name_edit = new QLineEdit(parent);
219 name_edit->setText(_name);
220 connect(name_edit, SIGNAL(textChanged(const QString&)),
221 this, SLOT(on_text_changed(const QString&)));
222 form->addRow(tr("Name"), name_edit);
91e8bf08
JH
223
224 add_colour_option(parent, form);
569d1e41
JH
225}
226
931f20b0
JH
227void Trace::compute_text_size(QPainter &p)
228{
229 _text_size = QSize(
230 p.boundingRect(QRectF(), 0, _name).width(),
231 p.boundingRect(QRectF(), 0, "Tg").height());
232}
233
01fd3263 234QRectF Trace::get_label_rect(int right)
931f20b0
JH
235{
236 using pv::view::View;
237
01fd3263 238 assert(_view);
01fd3263 239
931f20b0
JH
240 const QSizeF label_size(
241 _text_size.width() + View::LabelPadding.width() * 2,
242 ceilf((_text_size.height() + View::LabelPadding.height() * 2) / 2) * 2);
243 const float label_arrow_length = label_size.height() / 2;
244 return QRectF(
245 right - label_arrow_length - label_size.width() - 0.5,
f39936fb 246 get_y() + 0.5f - label_size.height() / 2,
931f20b0
JH
247 label_size.width(), label_size.height());
248}
249
68456dab
JH
250void Trace::on_text_changed(const QString &text)
251{
252 set_name(text);
253 text_changed();
254}
9b6378f1 255
91e8bf08
JH
256void Trace::on_colour_changed(const QColor &colour)
257{
258 set_colour(colour);
259 colour_changed();
260}
261
931f20b0
JH
262} // namespace view
263} // namespace pv