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