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