Link signals to probes
[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 const int Signal::LabelHighlightRadius = 6;
36
37 const QPen Signal::SignalAxisPen(QColor(128, 128, 128, 64));
38
39 Signal::Signal(const sr_probe *const probe) :
40         _probe(probe),
41         _name(probe->name),
42         _v_offset(0),
43         _selected(false)
44 {
45         assert(_probe);
46 }
47
48 QString Signal::get_name() const
49 {
50         return _name;
51 }
52
53 void Signal::set_name(QString name)
54 {
55         _name = name;
56 }
57
58 QColor Signal::get_colour() const
59 {
60         return _colour;
61 }
62
63 void Signal::set_colour(QColor colour)
64 {
65         _colour = colour;
66 }
67
68 int Signal::get_v_offset() const
69 {
70         return _v_offset;
71 }
72
73 void Signal::set_v_offset(int v_offset)
74 {
75         _v_offset = v_offset;
76 }
77
78 bool Signal::selected() const
79 {
80         return _selected;
81 }
82
83 void Signal::select(bool select)
84 {
85         _selected = select;
86 }
87
88 void Signal::paint_label(QPainter &p, int y, int right, bool hover)
89 {
90         p.setBrush(_colour);
91
92         if (!_probe->enabled)
93                 return;
94
95         const QColor colour = get_colour();
96
97         compute_text_size(p);
98         const QRectF label_rect = get_label_rect(y, right);
99
100         // Paint the label
101         const QPointF points[] = {
102                 label_rect.topLeft(),
103                 label_rect.topRight(),
104                 QPointF(right, y),
105                 label_rect.bottomRight(),
106                 label_rect.bottomLeft()
107         };
108
109         const QPointF highlight_points[] = {
110                 QPointF(label_rect.left() + 1, label_rect.top() + 1),
111                 QPointF(label_rect.right(), label_rect.top() + 1),
112                 QPointF(right - 1, y),
113                 QPointF(label_rect.right(), label_rect.bottom() - 1),
114                 QPointF(label_rect.left() + 1, label_rect.bottom() - 1)
115         };
116
117         if (_selected) {
118                 p.setPen(QPen(QApplication::palette().brush(
119                         QPalette::Highlight), LabelHighlightRadius,
120                         Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
121                 p.setBrush(Qt::transparent);
122                 p.drawPolygon(points, countof(points));
123         }
124
125         p.setPen(Qt::transparent);
126         p.setBrush(hover ? colour.lighter() : colour);
127         p.drawPolygon(points, countof(points));
128
129         p.setPen(colour.lighter());
130         p.setBrush(Qt::transparent);
131         p.drawPolygon(highlight_points, countof(highlight_points));
132
133         p.setPen(colour.darker());
134         p.setBrush(Qt::transparent);
135         p.drawPolygon(points, countof(points));
136
137         // Paint the text
138         p.setPen((colour.lightness() > 64) ? Qt::black : Qt::white);
139         p.drawText(label_rect, Qt::AlignCenter | Qt::AlignVCenter, _name);
140 }
141
142 bool Signal::pt_in_label_rect(int y, int left, int right,
143         const QPoint &point)
144 {
145         (void)left;
146
147         const QRectF label = get_label_rect(y, right);
148         return QRectF(
149                 QPointF(label.left() - LabelHitPadding,
150                         label.top() - LabelHitPadding),
151                 QPointF(right, label.bottom() + LabelHitPadding)
152                         ).contains(point);
153 }
154
155 void Signal::paint_axis(QPainter &p, int y, int left, int right)
156 {
157         p.setPen(SignalAxisPen);
158         p.drawLine(QPointF(left, y + 0.5f), QPointF(right, y + 0.5f));
159 }
160
161 void Signal::compute_text_size(QPainter &p)
162 {
163         _text_size = QSize(
164                 p.boundingRect(QRectF(), 0, _name).width(),
165                 p.boundingRect(QRectF(), 0, "Tg").height());
166 }
167
168 QRectF Signal::get_label_rect(int y, int right)
169 {
170         using pv::view::View;
171
172         const QSizeF label_size(
173                 _text_size.width() + View::LabelPadding.width() * 2,
174                 ceilf((_text_size.height() + View::LabelPadding.height() * 2) / 2) * 2);
175         const float label_arrow_length = label_size.height() / 2;
176         return QRectF(
177                 right - label_arrow_length - label_size.width() - 0.5,
178                 y + 0.5f - label_size.height() / 2,
179                 label_size.width(), label_size.height());
180 }
181
182 } // namespace view
183 } // namespace pv