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