]> sigrok.org Git - pulseview.git/blame - pv/view/signal.cpp
Added _session reference to Signal objects
[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;
28a4c9c5 35
3ef6182a
JH
36const QPen Signal::SignalAxisPen(QColor(128, 128, 128, 64));
37
9e40e83d
JH
38const 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
c0f86852
JH
55Signal::Signal(pv::SigSession &session, const sr_probe *const probe) :
56 _session(session),
cec48d16
JH
57 _probe(probe),
58 _name(probe->name),
9e40e83d
JH
59 _v_offset(0),
60 _name_action(NULL),
61 _name_widget(),
62 _updating_name_widget(false)
28a4c9c5 63{
cec48d16 64 assert(_probe);
9e40e83d
JH
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&)));
28a4c9c5
JH
75}
76
77QString Signal::get_name() const
78{
79 return _name;
80}
3e46726a 81
49f8ff3f
JH
82void Signal::set_name(QString name)
83{
84 _name = name;
9e40e83d
JH
85 _updating_name_widget = true;
86 _name_widget.setEditText(name);
87 _updating_name_widget = false;
49f8ff3f
JH
88}
89
b3b57abc
JH
90QColor Signal::get_colour() const
91{
92 return _colour;
93}
94
95void Signal::set_colour(QColor colour)
96{
97 _colour = colour;
98}
99
2e575351
JH
100int Signal::get_v_offset() const
101{
102 return _v_offset;
103}
104
105void Signal::set_v_offset(int v_offset)
106{
107 _v_offset = v_offset;
108}
109
2658961b 110void Signal::paint_label(QPainter &p, int y, int right, bool hover)
3b258424 111{
b3b57abc 112 p.setBrush(_colour);
3b258424 113
cec48d16
JH
114 if (!_probe->enabled)
115 return;
116
3b258424 117 const QColor colour = get_colour();
ed6f0f4f
JH
118
119 compute_text_size(p);
2658961b 120 const QRectF label_rect = get_label_rect(y, right);
3e46726a
JH
121
122 // Paint the label
123 const QPointF points[] = {
124 label_rect.topLeft(),
125 label_rect.topRight(),
2658961b 126 QPointF(right, y),
3e46726a
JH
127 label_rect.bottomRight(),
128 label_rect.bottomLeft()
129 };
130
c9b6acc1
JH
131 const QPointF highlight_points[] = {
132 QPointF(label_rect.left() + 1, label_rect.top() + 1),
133 QPointF(label_rect.right(), label_rect.top() + 1),
2658961b 134 QPointF(right - 1, y),
c9b6acc1
JH
135 QPointF(label_rect.right(), label_rect.bottom() - 1),
136 QPointF(label_rect.left() + 1, label_rect.bottom() - 1)
137 };
138
f1283456 139 if (selected()) {
d09674d4 140 p.setPen(highlight_pen());
e3374498
JH
141 p.setBrush(Qt::transparent);
142 p.drawPolygon(points, countof(points));
143 }
144
c9b6acc1 145 p.setPen(Qt::transparent);
a29bb7fb 146 p.setBrush(hover ? colour.lighter() : colour);
3e46726a
JH
147 p.drawPolygon(points, countof(points));
148
c9b6acc1
JH
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
3e46726a
JH
157 // Paint the text
158 p.setPen((colour.lightness() > 64) ? Qt::black : Qt::white);
3b258424 159 p.drawText(label_rect, Qt::AlignCenter | Qt::AlignVCenter, _name);
3e46726a 160}
51e77110 161
2658961b
JH
162bool Signal::pt_in_label_rect(int y, int left, int right,
163 const QPoint &point)
a29bb7fb 164{
94fe58ef
UH
165 (void)left;
166
2658961b 167 const QRectF label = get_label_rect(y, right);
a29bb7fb
JH
168 return QRectF(
169 QPointF(label.left() - LabelHitPadding,
170 label.top() - LabelHitPadding),
2658961b
JH
171 QPointF(right, label.bottom() + LabelHitPadding)
172 ).contains(point);
a29bb7fb
JH
173}
174
3ef6182a
JH
175void 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
ed6f0f4f 181void Signal::compute_text_size(QPainter &p)
a29bb7fb 182{
5ce37f0f
JH
183 _text_size = QSize(
184 p.boundingRect(QRectF(), 0, _name).width(),
185 p.boundingRect(QRectF(), 0, "Tg").height());
ed6f0f4f 186}
a29bb7fb 187
2658961b 188QRectF Signal::get_label_rect(int y, int right)
ed6f0f4f 189{
2e04f9bd
JH
190 using pv::view::View;
191
a29bb7fb 192 const QSizeF label_size(
2e04f9bd 193 _text_size.width() + View::LabelPadding.width() * 2,
b47d951e 194 ceilf((_text_size.height() + View::LabelPadding.height() * 2) / 2) * 2);
a29bb7fb
JH
195 const float label_arrow_length = label_size.height() / 2;
196 return QRectF(
2658961b
JH
197 right - label_arrow_length - label_size.width() - 0.5,
198 y + 0.5f - label_size.height() / 2,
a29bb7fb
JH
199 label_size.width(), label_size.height());
200}
201
9e40e83d
JH
202void Signal::on_text_changed(const QString &text)
203{
204 _name = text;
205 text_changed();
206}
207
8d634081 208} // namespace view
51e77110 209} // namespace pv