]> sigrok.org Git - pulseview.git/blob - pv/view/signal.cpp
Added _session reference to Signal objects
[pulseview.git] / pv / view / signal.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2012 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
28 #include "signal.h"
29 #include "view.h"
30
31 namespace pv {
32 namespace view {
33
34 const int Signal::LabelHitPadding = 2;
35
36 const QPen Signal::SignalAxisPen(QColor(128, 128, 128, 64));
37
38 const char *const ProbeNames[] = {
39         "CLK",
40         "DATA",
41         "IN",
42         "OUT",
43         "RST",
44         "Tx",
45         "Rx",
46         "EN",
47         "SCLK",
48         "MOSI",
49         "MISO",
50         "/SS",
51         "SDA",
52         "SCL"
53 };
54
55 Signal::Signal(pv::SigSession &session, const sr_probe *const probe) :
56         _session(session),
57         _probe(probe),
58         _name(probe->name),
59         _v_offset(0),
60         _name_action(NULL),
61         _name_widget(),
62         _updating_name_widget(false)
63 {
64         assert(_probe);
65
66         _name_action.setDefaultWidget(&_name_widget);
67
68         _name_widget.setEditable(true);
69         for(unsigned int i = 0; i < countof(ProbeNames); i++)
70                 _name_widget.insertItem(i, ProbeNames[i]);
71         _name_widget.setEditText(probe->name);
72
73         connect(&_name_widget, SIGNAL(editTextChanged(const QString&)),
74                 this, SLOT(on_text_changed(const QString&)));
75 }
76
77 QString Signal::get_name() const
78 {
79         return _name;
80 }
81
82 void Signal::set_name(QString name)
83 {
84         _name = name;
85         _updating_name_widget = true;
86         _name_widget.setEditText(name);
87         _updating_name_widget = false;
88 }
89
90 QColor Signal::get_colour() const
91 {
92         return _colour;
93 }
94
95 void Signal::set_colour(QColor colour)
96 {
97         _colour = colour;
98 }
99
100 int Signal::get_v_offset() const
101 {
102         return _v_offset;
103 }
104
105 void Signal::set_v_offset(int v_offset)
106 {
107         _v_offset = v_offset;
108 }
109
110 void Signal::paint_label(QPainter &p, int y, int right, bool hover)
111 {
112         p.setBrush(_colour);
113
114         if (!_probe->enabled)
115                 return;
116
117         const QColor colour = get_colour();
118
119         compute_text_size(p);
120         const QRectF label_rect = get_label_rect(y, 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((colour.lightness() > 64) ? Qt::black : Qt::white);
159         p.drawText(label_rect, Qt::AlignCenter | Qt::AlignVCenter, _name);
160 }
161
162 bool Signal::pt_in_label_rect(int y, int left, int right,
163         const QPoint &point)
164 {
165         (void)left;
166
167         const QRectF label = get_label_rect(y, 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 void Signal::paint_axis(QPainter &p, int y, int left, int right)
176 {
177         p.setPen(SignalAxisPen);
178         p.drawLine(QPointF(left, y + 0.5f), QPointF(right, y + 0.5f));
179 }
180
181 void Signal::compute_text_size(QPainter &p)
182 {
183         _text_size = QSize(
184                 p.boundingRect(QRectF(), 0, _name).width(),
185                 p.boundingRect(QRectF(), 0, "Tg").height());
186 }
187
188 QRectF Signal::get_label_rect(int y, int right)
189 {
190         using pv::view::View;
191
192         const QSizeF label_size(
193                 _text_size.width() + View::LabelPadding.width() * 2,
194                 ceilf((_text_size.height() + View::LabelPadding.height() * 2) / 2) * 2);
195         const float label_arrow_length = label_size.height() / 2;
196         return QRectF(
197                 right - label_arrow_length - label_size.width() - 0.5,
198                 y + 0.5f - label_size.height() / 2,
199                 label_size.width(), label_size.height());
200 }
201
202 void Signal::on_text_changed(const QString &text)
203 {
204         _name = text;
205         text_changed();
206 }
207
208 } // namespace view
209 } // namespace pv