]> sigrok.org Git - pulseview.git/blame - pv/view/trace.cpp
Trace: Renamed get_label_rect to label_rect
[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{
112 assert(_view);
113 const int y = _v_offset - _view->v_offset();
114
931f20b0
JH
115 p.setBrush(_colour);
116
117 if (!enabled())
118 return;
119
120 const QColor colour = get_colour();
121
1053d05c 122 const QRectF r = label_rect(right);
931f20b0
JH
123
124 // Paint the label
125 const QPointF points[] = {
1053d05c
JH
126 r.topLeft(),
127 r.topRight(),
931f20b0 128 QPointF(right, y),
1053d05c
JH
129 r.bottomRight(),
130 r.bottomLeft()
931f20b0
JH
131 };
132
133 const QPointF highlight_points[] = {
1053d05c
JH
134 QPointF(r.left() + 1, r.top() + 1),
135 QPointF(r.right(), r.top() + 1),
931f20b0 136 QPointF(right - 1, y),
1053d05c
JH
137 QPointF(r.right(), r.bottom() - 1),
138 QPointF(r.left() + 1, r.bottom() - 1)
931f20b0
JH
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());
d7c0ca4a 161 p.setFont(QApplication::font());
1053d05c 162 p.drawText(r, Qt::AlignCenter | Qt::AlignVCenter, _name);
931f20b0
JH
163}
164
01fd3263 165bool Trace::pt_in_label_rect(int left, int right, const QPoint &point)
931f20b0
JH
166{
167 (void)left;
168
1053d05c 169 const QRectF label = label_rect(right);
32d83ce1 170 return enabled() && QRectF(
931f20b0
JH
171 QPointF(label.left() - LabelHitPadding,
172 label.top() - LabelHitPadding),
173 QPointF(right, label.bottom() + LabelHitPadding)
174 ).contains(point);
175}
176
9b6378f1
JH
177QMenu* Trace::create_context_menu(QWidget *parent)
178{
179 QMenu *const menu = SelectableItem::create_context_menu(parent);
180
9b6378f1
JH
181 return menu;
182}
183
569d1e41
JH
184pv::widgets::Popup* Trace::create_popup(QWidget *parent)
185{
186 using pv::widgets::Popup;
20eaae39
JH
187
188 _popup = new Popup(parent);
20eaae39 189
37fd11b1 190 create_popup_form();
20eaae39
JH
191
192 connect(_popup, SIGNAL(closed()),
193 this, SLOT(on_popup_closed()));
194
195 return _popup;
569d1e41
JH
196}
197
fe08b6e8
JH
198int Trace::get_y() const
199{
200 return _v_offset - _view->v_offset();
201}
202
1053d05c 203QRectF Trace::label_rect(int right)
d7c0ca4a
JH
204{
205 using pv::view::View;
206
207 assert(_view);
208
209 QFontMetrics m(QApplication::font());
210 const QSize text_size(
211 m.boundingRect(QRect(), 0, _name).width(),
212 m.boundingRect(QRect(), 0, "Tg").height());
213 const QSizeF label_size(
214 text_size.width() + View::LabelPadding.width() * 2,
215 ceilf((text_size.height() + View::LabelPadding.height() * 2) / 2) * 2);
216 const float label_arrow_length = label_size.height() / 2;
217 return QRectF(
218 right - label_arrow_length - label_size.width() - 0.5,
219 get_y() + 0.5f - label_size.height() / 2,
220 label_size.width(), label_size.height());
221}
222
33c62f44
JH
223void Trace::hover_point_changed()
224{
225}
226
410f9c81
JH
227QColor Trace::get_text_colour() const
228{
229 return (_colour.lightness() > 64) ? Qt::black : Qt::white;
230}
231
fe08b6e8
JH
232void Trace::paint_axis(QPainter &p, int y, int left, int right)
233{
234 p.setPen(AxisPen);
235 p.drawLine(QPointF(left, y + 0.5f), QPointF(right, y + 0.5f));
236}
237
91e8bf08
JH
238void Trace::add_colour_option(QWidget *parent, QFormLayout *form)
239{
240 using pv::widgets::ColourButton;
241
242 ColourButton *const colour_button = new ColourButton(
243 TracePalette::Rows, TracePalette::Cols, parent);
244 colour_button->set_palette(TracePalette::Colours);
245 colour_button->set_colour(_colour);
246 connect(colour_button, SIGNAL(selected(const QColor&)),
247 this, SLOT(on_colour_changed(const QColor&)));
248
249 form->addRow(tr("Colour"), colour_button);
250}
251
37fd11b1
JH
252void Trace::create_popup_form()
253{
254 // Clear the layout
255
256 // Transfer the layout and the child widgets to a temporary widget
257 // which then goes out of scope destroying the layout and all the child
258 // widgets.
259 if (_popup_form)
260 QWidget().setLayout(_popup_form);
261
262 // Repopulate the popup
263 _popup_form = new QFormLayout(_popup);
264 _popup->setLayout(_popup_form);
265 populate_popup_form(_popup, _popup_form);
266}
267
569d1e41
JH
268void Trace::populate_popup_form(QWidget *parent, QFormLayout *form)
269{
68456dab
JH
270 QLineEdit *const name_edit = new QLineEdit(parent);
271 name_edit->setText(_name);
272 connect(name_edit, SIGNAL(textChanged(const QString&)),
273 this, SLOT(on_text_changed(const QString&)));
274 form->addRow(tr("Name"), name_edit);
91e8bf08
JH
275
276 add_colour_option(parent, form);
569d1e41
JH
277}
278
20eaae39
JH
279void Trace::on_popup_closed()
280{
281 _popup = NULL;
282 _popup_form = NULL;
283}
284
68456dab
JH
285void Trace::on_text_changed(const QString &text)
286{
287 set_name(text);
288 text_changed();
289}
9b6378f1 290
91e8bf08
JH
291void Trace::on_colour_changed(const QColor &colour)
292{
293 set_colour(colour);
294 colour_changed();
295}
296
931f20b0
JH
297} // namespace view
298} // namespace pv