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