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