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