]> sigrok.org Git - pulseview.git/blame - pv/view/signal.cpp
Link signals to probes
[pulseview.git] / pv / view / signal.cpp
CommitLineData
28a4c9c5 1/*
b3f22de0 2 * This file is part of the PulseView project.
28a4c9c5
JH
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
cef18fc6 21#include <extdef.h>
28a4c9c5 22
cec48d16 23#include <assert.h>
b47d951e
JH
24#include <math.h>
25
e3374498
JH
26#include <QApplication>
27
cef18fc6 28#include "signal.h"
8d634081 29#include "view.h"
3e46726a 30
51e77110 31namespace pv {
8d634081 32namespace view {
51e77110 33
a29bb7fb 34const int Signal::LabelHitPadding = 2;
e3374498 35const int Signal::LabelHighlightRadius = 6;
28a4c9c5 36
3ef6182a
JH
37const QPen Signal::SignalAxisPen(QColor(128, 128, 128, 64));
38
cec48d16
JH
39Signal::Signal(const sr_probe *const probe) :
40 _probe(probe),
41 _name(probe->name),
e3374498
JH
42 _v_offset(0),
43 _selected(false)
28a4c9c5 44{
cec48d16 45 assert(_probe);
28a4c9c5
JH
46}
47
48QString Signal::get_name() const
49{
50 return _name;
51}
3e46726a 52
49f8ff3f
JH
53void Signal::set_name(QString name)
54{
55 _name = name;
56}
57
b3b57abc
JH
58QColor Signal::get_colour() const
59{
60 return _colour;
61}
62
63void Signal::set_colour(QColor colour)
64{
65 _colour = colour;
66}
67
2e575351
JH
68int Signal::get_v_offset() const
69{
70 return _v_offset;
71}
72
73void Signal::set_v_offset(int v_offset)
74{
75 _v_offset = v_offset;
76}
77
e3374498
JH
78bool Signal::selected() const
79{
80 return _selected;
81}
82
83void Signal::select(bool select)
84{
85 _selected = select;
86}
87
2658961b 88void Signal::paint_label(QPainter &p, int y, int right, bool hover)
3b258424 89{
b3b57abc 90 p.setBrush(_colour);
3b258424 91
cec48d16
JH
92 if (!_probe->enabled)
93 return;
94
3b258424 95 const QColor colour = get_colour();
ed6f0f4f
JH
96
97 compute_text_size(p);
2658961b 98 const QRectF label_rect = get_label_rect(y, right);
3e46726a
JH
99
100 // Paint the label
101 const QPointF points[] = {
102 label_rect.topLeft(),
103 label_rect.topRight(),
2658961b 104 QPointF(right, y),
3e46726a
JH
105 label_rect.bottomRight(),
106 label_rect.bottomLeft()
107 };
108
c9b6acc1
JH
109 const QPointF highlight_points[] = {
110 QPointF(label_rect.left() + 1, label_rect.top() + 1),
111 QPointF(label_rect.right(), label_rect.top() + 1),
2658961b 112 QPointF(right - 1, y),
c9b6acc1
JH
113 QPointF(label_rect.right(), label_rect.bottom() - 1),
114 QPointF(label_rect.left() + 1, label_rect.bottom() - 1)
115 };
116
333d5bbc 117 if (_selected) {
e3374498
JH
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
c9b6acc1 125 p.setPen(Qt::transparent);
a29bb7fb 126 p.setBrush(hover ? colour.lighter() : colour);
3e46726a
JH
127 p.drawPolygon(points, countof(points));
128
c9b6acc1
JH
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
3e46726a
JH
137 // Paint the text
138 p.setPen((colour.lightness() > 64) ? Qt::black : Qt::white);
3b258424 139 p.drawText(label_rect, Qt::AlignCenter | Qt::AlignVCenter, _name);
3e46726a 140}
51e77110 141
2658961b
JH
142bool Signal::pt_in_label_rect(int y, int left, int right,
143 const QPoint &point)
a29bb7fb 144{
94fe58ef
UH
145 (void)left;
146
2658961b 147 const QRectF label = get_label_rect(y, right);
a29bb7fb
JH
148 return QRectF(
149 QPointF(label.left() - LabelHitPadding,
150 label.top() - LabelHitPadding),
2658961b
JH
151 QPointF(right, label.bottom() + LabelHitPadding)
152 ).contains(point);
a29bb7fb
JH
153}
154
3ef6182a
JH
155void 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
ed6f0f4f 161void Signal::compute_text_size(QPainter &p)
a29bb7fb 162{
5ce37f0f
JH
163 _text_size = QSize(
164 p.boundingRect(QRectF(), 0, _name).width(),
165 p.boundingRect(QRectF(), 0, "Tg").height());
ed6f0f4f 166}
a29bb7fb 167
2658961b 168QRectF Signal::get_label_rect(int y, int right)
ed6f0f4f 169{
2e04f9bd
JH
170 using pv::view::View;
171
a29bb7fb 172 const QSizeF label_size(
2e04f9bd 173 _text_size.width() + View::LabelPadding.width() * 2,
b47d951e 174 ceilf((_text_size.height() + View::LabelPadding.height() * 2) / 2) * 2);
a29bb7fb
JH
175 const float label_arrow_length = label_size.height() / 2;
176 return QRectF(
2658961b
JH
177 right - label_arrow_length - label_size.width() - 0.5,
178 y + 0.5f - label_size.height() / 2,
a29bb7fb
JH
179 label_size.width(), label_size.height());
180}
181
8d634081 182} // namespace view
51e77110 183} // namespace pv