]> sigrok.org Git - pulseview.git/blame - pv/view/trace.cpp
DecodeTrace: Removed set_view
[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
d7c0ca4a 26#include <QApplication>
b213ef09 27#include <QFormLayout>
0891e69b 28#include <QKeyEvent>
b213ef09
JH
29#include <QLineEdit>
30
931f20b0 31#include "trace.h"
91e8bf08 32#include "tracepalette.h"
931f20b0
JH
33#include "view.h"
34
91e8bf08 35#include <pv/widgets/colourbutton.h>
1f81a394 36#include <pv/widgets/popup.h>
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) :
9555ca8b 45 _view(NULL),
931f20b0 46 _name(name),
20eaae39
JH
47 _v_offset(0),
48 _popup(NULL),
49 _popup_form(NULL)
931f20b0
JH
50{
51}
52
53QString Trace::get_name() const
54{
55 return _name;
56}
57
58void Trace::set_name(QString name)
59{
60 _name = name;
61}
62
63QColor Trace::get_colour() const
64{
65 return _colour;
66}
67
68void Trace::set_colour(QColor colour)
69{
70 _colour = colour;
71}
72
73int Trace::get_v_offset() const
74{
75 return _v_offset;
76}
77
78void Trace::set_v_offset(int v_offset)
79{
80 _v_offset = v_offset;
81}
82
01fd3263 83void Trace::set_view(pv::view::View *view)
931f20b0 84{
01fd3263
JH
85 assert(view);
86 _view = view;
87}
88
fe08b6e8
JH
89void Trace::paint_back(QPainter &p, int left, int right)
90{
91 (void)p;
92 (void)left;
93 (void)right;
94}
95
96void Trace::paint_mid(QPainter &p, int left, int right)
97{
98 (void)p;
99 (void)left;
100 (void)right;
101}
102
103void Trace::paint_fore(QPainter &p, int left, int right)
104{
105 (void)p;
106 (void)left;
107 (void)right;
108}
109
01fd3263
JH
110void Trace::paint_label(QPainter &p, int right, bool hover)
111{
d5d1f925 112 const int y = get_y();
01fd3263 113
931f20b0
JH
114 p.setBrush(_colour);
115
116 if (!enabled())
117 return;
118
119 const QColor colour = get_colour();
120
1053d05c 121 const QRectF r = label_rect(right);
931f20b0
JH
122
123 // Paint the label
b5cb6c35 124 const float label_arrow_length = r.height() / 2;
931f20b0 125 const QPointF points[] = {
1053d05c 126 r.topLeft(),
b5cb6c35
JH
127 QPointF(r.right() - label_arrow_length, r.top()),
128 QPointF(r.right(), y),
129 QPointF(r.right() - label_arrow_length, r.bottom()),
1053d05c 130 r.bottomLeft()
931f20b0 131 };
931f20b0 132 const QPointF highlight_points[] = {
1053d05c 133 QPointF(r.left() + 1, r.top() + 1),
b5cb6c35
JH
134 QPointF(r.right() - label_arrow_length, r.top() + 1),
135 QPointF(r.right() - 1, y),
136 QPointF(r.right() - label_arrow_length, r.bottom() - 1),
1053d05c 137 QPointF(r.left() + 1, r.bottom() - 1)
931f20b0
JH
138 };
139
140 if (selected()) {
141 p.setPen(highlight_pen());
142 p.setBrush(Qt::transparent);
143 p.drawPolygon(points, countof(points));
144 }
145
146 p.setPen(Qt::transparent);
147 p.setBrush(hover ? colour.lighter() : colour);
148 p.drawPolygon(points, countof(points));
149
150 p.setPen(colour.lighter());
151 p.setBrush(Qt::transparent);
152 p.drawPolygon(highlight_points, countof(highlight_points));
153
154 p.setPen(colour.darker());
155 p.setBrush(Qt::transparent);
156 p.drawPolygon(points, countof(points));
157
158 // Paint the text
410f9c81 159 p.setPen(get_text_colour());
d7c0ca4a 160 p.setFont(QApplication::font());
b5cb6c35
JH
161 p.drawText(QRectF(r.x(), r.y(),
162 r.width() - label_arrow_length, r.height()),
163 Qt::AlignCenter | Qt::AlignVCenter, _name);
931f20b0
JH
164}
165
9b6378f1
JH
166QMenu* Trace::create_context_menu(QWidget *parent)
167{
168 QMenu *const menu = SelectableItem::create_context_menu(parent);
169
9b6378f1
JH
170 return menu;
171}
172
569d1e41
JH
173pv::widgets::Popup* Trace::create_popup(QWidget *parent)
174{
175 using pv::widgets::Popup;
20eaae39
JH
176
177 _popup = new Popup(parent);
20eaae39 178
37fd11b1 179 create_popup_form();
20eaae39
JH
180
181 connect(_popup, SIGNAL(closed()),
182 this, SLOT(on_popup_closed()));
183
184 return _popup;
569d1e41
JH
185}
186
fe08b6e8
JH
187int Trace::get_y() const
188{
d5d1f925 189 assert(_view);
fe08b6e8
JH
190 return _v_offset - _view->v_offset();
191}
192
1053d05c 193QRectF Trace::label_rect(int right)
d7c0ca4a
JH
194{
195 using pv::view::View;
196
d7c0ca4a
JH
197 QFontMetrics m(QApplication::font());
198 const QSize text_size(
199 m.boundingRect(QRect(), 0, _name).width(),
200 m.boundingRect(QRect(), 0, "Tg").height());
201 const QSizeF label_size(
202 text_size.width() + View::LabelPadding.width() * 2,
203 ceilf((text_size.height() + View::LabelPadding.height() * 2) / 2) * 2);
b5cb6c35 204 const float half_height = label_size.height() / 2;
d7c0ca4a 205 return QRectF(
b5cb6c35
JH
206 right - half_height - label_size.width() - 0.5,
207 get_y() + 0.5f - half_height,
208 label_size.width() + half_height,
209 label_size.height());
d7c0ca4a
JH
210}
211
33c62f44
JH
212void Trace::hover_point_changed()
213{
214}
215
410f9c81
JH
216QColor Trace::get_text_colour() const
217{
218 return (_colour.lightness() > 64) ? Qt::black : Qt::white;
219}
220
fe08b6e8
JH
221void Trace::paint_axis(QPainter &p, int y, int left, int right)
222{
223 p.setPen(AxisPen);
224 p.drawLine(QPointF(left, y + 0.5f), QPointF(right, y + 0.5f));
225}
226
91e8bf08
JH
227void Trace::add_colour_option(QWidget *parent, QFormLayout *form)
228{
229 using pv::widgets::ColourButton;
230
231 ColourButton *const colour_button = new ColourButton(
232 TracePalette::Rows, TracePalette::Cols, parent);
233 colour_button->set_palette(TracePalette::Colours);
234 colour_button->set_colour(_colour);
235 connect(colour_button, SIGNAL(selected(const QColor&)),
236 this, SLOT(on_colour_changed(const QColor&)));
237
238 form->addRow(tr("Colour"), colour_button);
239}
240
37fd11b1
JH
241void Trace::create_popup_form()
242{
243 // Clear the layout
244
245 // Transfer the layout and the child widgets to a temporary widget
246 // which then goes out of scope destroying the layout and all the child
247 // widgets.
248 if (_popup_form)
249 QWidget().setLayout(_popup_form);
250
251 // Repopulate the popup
252 _popup_form = new QFormLayout(_popup);
253 _popup->setLayout(_popup_form);
254 populate_popup_form(_popup, _popup_form);
255}
256
569d1e41
JH
257void Trace::populate_popup_form(QWidget *parent, QFormLayout *form)
258{
68456dab
JH
259 QLineEdit *const name_edit = new QLineEdit(parent);
260 name_edit->setText(_name);
261 connect(name_edit, SIGNAL(textChanged(const QString&)),
262 this, SLOT(on_text_changed(const QString&)));
263 form->addRow(tr("Name"), name_edit);
91e8bf08
JH
264
265 add_colour_option(parent, form);
569d1e41
JH
266}
267
20eaae39
JH
268void Trace::on_popup_closed()
269{
270 _popup = NULL;
271 _popup_form = NULL;
272}
273
68456dab
JH
274void Trace::on_text_changed(const QString &text)
275{
276 set_name(text);
277 text_changed();
278}
9b6378f1 279
91e8bf08
JH
280void Trace::on_colour_changed(const QColor &colour)
281{
282 set_colour(colour);
283 colour_changed();
284}
285
931f20b0
JH
286} // namespace view
287} // namespace pv