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