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