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