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