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