]>
Commit | Line | Data |
---|---|---|
6e3f046e JH |
1 | /* |
2 | * This file is part of the PulseView project. | |
3 | * | |
4 | * Copyright (C) 2013 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 | ||
21 | #include <algorithm> | |
22 | ||
23 | #include <QtGui> | |
24 | ||
25 | #include "popup.h" | |
26 | ||
27 | using namespace std; | |
28 | ||
29 | namespace pv { | |
30 | namespace widgets { | |
31 | ||
32 | const unsigned int Popup::ArrowLength = 15; | |
33 | const unsigned int Popup::ArrowOverlap = 3; | |
34 | const unsigned int Popup::MarginWidth = 10; | |
35 | ||
36 | Popup::Popup(QWidget *parent) : | |
37 | QWidget(parent, Qt::Popup | Qt::FramelessWindowHint), | |
38 | _point(), | |
39 | _pos(Left) | |
40 | { | |
41 | } | |
42 | ||
43 | const QPoint& Popup::point() const | |
44 | { | |
45 | return _point; | |
46 | } | |
47 | ||
48 | Popup::Position Popup::position() const | |
49 | { | |
50 | return _pos; | |
51 | } | |
52 | ||
53 | void Popup::set_position(const QPoint point, Position pos) | |
54 | { | |
55 | _point = point, _pos = pos; | |
56 | ||
57 | setContentsMargins( | |
58 | MarginWidth + ((pos == Right) ? ArrowLength : 0), | |
59 | MarginWidth + ((pos == Bottom) ? ArrowLength : 0), | |
60 | MarginWidth + ((pos == Left) ? ArrowLength : 0), | |
61 | MarginWidth + ((pos == Top) ? ArrowLength : 0)); | |
62 | ||
63 | } | |
64 | ||
65 | QPolygon Popup::arrow_polygon() const | |
66 | { | |
67 | QPolygon poly; | |
68 | ||
69 | const QPoint p = mapFromGlobal(_point); | |
70 | const int l = ArrowLength + ArrowOverlap; | |
71 | ||
72 | switch (_pos) | |
73 | { | |
74 | case Right: | |
75 | poly << QPoint(p.x() + l, p.y() - l); | |
76 | break; | |
77 | ||
78 | case Bottom: | |
79 | poly << QPoint(p.x() - l, p.y() + l); | |
80 | break; | |
81 | ||
82 | case Left: | |
83 | case Top: | |
84 | poly << QPoint(p.x() - l, p.y() - l); | |
85 | break; | |
86 | } | |
87 | ||
88 | poly << p; | |
89 | ||
90 | switch (_pos) | |
91 | { | |
92 | case Right: | |
93 | case Bottom: | |
94 | poly << QPoint(p.x() + l, p.y() + l); | |
95 | break; | |
96 | ||
97 | case Left: | |
98 | poly << QPoint(p.x() - l, p.y() + l); | |
99 | break; | |
100 | ||
101 | case Top: | |
102 | poly << QPoint(p.x() + l, p.y() - l); | |
103 | break; | |
104 | } | |
105 | ||
106 | return poly; | |
107 | } | |
108 | ||
109 | QRegion Popup::arrow_region() const | |
110 | { | |
111 | return QRegion(arrow_polygon()); | |
112 | } | |
113 | ||
114 | QRect Popup::bubble_rect() const | |
115 | { | |
116 | return QRect( | |
117 | QPoint((_pos == Right) ? ArrowLength : 0, | |
118 | (_pos == Bottom) ? ArrowLength : 0), | |
119 | QSize(width() - ((_pos == Left || _pos == Right) ? | |
120 | ArrowLength : 0), | |
121 | height() - ((_pos == Top || _pos == Bottom) ? | |
122 | ArrowLength : 0))); | |
123 | } | |
124 | ||
125 | QRegion Popup::bubble_region() const | |
126 | { | |
127 | const QRect rect(bubble_rect()); | |
128 | ||
129 | const unsigned int r = MarginWidth; | |
130 | const unsigned int d = 2 * r; | |
131 | return QRegion(rect.adjusted(r, 0, -r, 0)).united( | |
132 | QRegion(rect.adjusted(0, r, 0, -r))).united( | |
133 | QRegion(rect.left(), rect.top(), d, d, | |
134 | QRegion::Ellipse)).united( | |
135 | QRegion(rect.right() - d, rect.top(), d, d, | |
136 | QRegion::Ellipse)).united( | |
137 | QRegion(rect.left(), rect.bottom() - d, d, d, | |
138 | QRegion::Ellipse)).united( | |
139 | QRegion(rect.right() - d, rect.bottom() - d, d, d, | |
140 | QRegion::Ellipse)); | |
141 | } | |
142 | ||
143 | QRegion Popup::popup_region() const | |
144 | { | |
145 | return arrow_region().united(bubble_region()); | |
146 | } | |
147 | ||
148 | void Popup::reposition_widget() | |
149 | { | |
150 | QPoint o; | |
151 | ||
152 | if (_pos == Right || _pos == Left) | |
153 | o.ry() = -height() / 2; | |
154 | else | |
155 | o.rx() = -width() / 2; | |
156 | ||
157 | if (_pos == Left) | |
158 | o.rx() = -width(); | |
159 | else if(_pos == Top) | |
160 | o.ry() = -height(); | |
161 | ||
162 | move(_point + o); | |
163 | } | |
164 | ||
165 | void Popup::paintEvent(QPaintEvent*) | |
166 | { | |
167 | QPainter painter(this); | |
168 | painter.setRenderHint(QPainter::Antialiasing); | |
169 | ||
170 | const QColor outline_color(QApplication::palette().color( | |
171 | QPalette::Dark)); | |
172 | ||
173 | const QRegion b = bubble_region(); | |
174 | const QRegion bubble_outline = QRegion(rect()).subtracted( | |
175 | b.translated(1, 0).intersected(b.translated(0, 1).intersected( | |
176 | b.translated(-1, 0).intersected(b.translated(0, -1))))); | |
177 | ||
178 | painter.setPen(Qt::NoPen); | |
179 | painter.setBrush(QApplication::palette().brush(QPalette::Window)); | |
180 | painter.drawRect(rect()); | |
181 | ||
182 | const QPoint ArrowOffsets[] = { | |
183 | QPoint(1, 0), QPoint(0, -1), QPoint(-1, 0), QPoint(0, 1)}; | |
184 | ||
185 | const QRegion a(arrow_region()); | |
186 | const QRegion arrow_outline = a.subtracted( | |
187 | a.translated(ArrowOffsets[_pos])); | |
188 | ||
189 | painter.setClipRegion(bubble_outline.subtracted(a).united( | |
190 | arrow_outline)); | |
191 | painter.setBrush(outline_color); | |
192 | painter.drawRect(rect()); | |
193 | } | |
194 | ||
195 | void Popup::resizeEvent(QResizeEvent*) | |
196 | { | |
197 | reposition_widget(); | |
198 | setMask(popup_region()); | |
199 | } | |
200 | ||
6e3f046e JH |
201 | void Popup::mouseReleaseEvent(QMouseEvent *e) |
202 | { | |
203 | assert(e); | |
204 | ||
205 | // We need our own out-of-bounds click handler because QWidget counts | |
206 | // the drop-shadow region as inside the widget | |
207 | if(!bubble_rect().contains(e->pos())) | |
208 | close(); | |
209 | } | |
210 | ||
b7b659aa JH |
211 | void Popup::showEvent(QShowEvent*) |
212 | { | |
213 | reposition_widget(); | |
214 | } | |
215 | ||
6e3f046e JH |
216 | } // namespace widgets |
217 | } // namespace pv | |
218 |