]>
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 | ||
b213ef09 JH |
23 | #include <assert.h> |
24 | ||
6e3f046e | 25 | #include <QtGui> |
d528d3d1 MC |
26 | #include <QApplication> |
27 | #include <QDesktopWidget> | |
6e3f046e JH |
28 | |
29 | #include "popup.h" | |
30 | ||
819f4c25 JH |
31 | using std::max; |
32 | using std::min; | |
6e3f046e JH |
33 | |
34 | namespace pv { | |
35 | namespace widgets { | |
36 | ||
b6a2899a | 37 | const unsigned int Popup::ArrowLength = 10; |
6e3f046e | 38 | const unsigned int Popup::ArrowOverlap = 3; |
b6a2899a | 39 | const unsigned int Popup::MarginWidth = 6; |
6e3f046e JH |
40 | |
41 | Popup::Popup(QWidget *parent) : | |
42 | QWidget(parent, Qt::Popup | Qt::FramelessWindowHint), | |
43 | _point(), | |
44 | _pos(Left) | |
45 | { | |
46 | } | |
47 | ||
48 | const QPoint& Popup::point() const | |
49 | { | |
50 | return _point; | |
51 | } | |
52 | ||
53 | Popup::Position Popup::position() const | |
54 | { | |
55 | return _pos; | |
56 | } | |
57 | ||
58 | void Popup::set_position(const QPoint point, Position pos) | |
59 | { | |
60 | _point = point, _pos = pos; | |
61 | ||
62 | setContentsMargins( | |
63 | MarginWidth + ((pos == Right) ? ArrowLength : 0), | |
64 | MarginWidth + ((pos == Bottom) ? ArrowLength : 0), | |
65 | MarginWidth + ((pos == Left) ? ArrowLength : 0), | |
66 | MarginWidth + ((pos == Top) ? ArrowLength : 0)); | |
67 | ||
68 | } | |
69 | ||
092e2a0a JH |
70 | bool Popup::space_for_arrow() const |
71 | { | |
72 | // Check if there is room for the arrow | |
73 | switch (_pos) { | |
74 | case Right: | |
75 | if (_point.x() > x()) | |
76 | return false; | |
77 | return true; | |
78 | ||
79 | case Bottom: | |
80 | if (_point.y() > y()) | |
81 | return false; | |
82 | return true; | |
83 | ||
84 | case Left: | |
85 | if (_point.x() < (x() + width())) | |
86 | return false; | |
87 | return true; | |
88 | ||
89 | case Top: | |
90 | if (_point.y() < (y() + height())) | |
91 | return false; | |
92 | return true; | |
93 | } | |
94 | ||
95 | return true; | |
96 | } | |
97 | ||
6e3f046e JH |
98 | QPolygon Popup::arrow_polygon() const |
99 | { | |
100 | QPolygon poly; | |
101 | ||
102 | const QPoint p = mapFromGlobal(_point); | |
103 | const int l = ArrowLength + ArrowOverlap; | |
104 | ||
105 | switch (_pos) | |
106 | { | |
107 | case Right: | |
108 | poly << QPoint(p.x() + l, p.y() - l); | |
109 | break; | |
110 | ||
111 | case Bottom: | |
112 | poly << QPoint(p.x() - l, p.y() + l); | |
113 | break; | |
114 | ||
115 | case Left: | |
116 | case Top: | |
117 | poly << QPoint(p.x() - l, p.y() - l); | |
118 | break; | |
119 | } | |
120 | ||
121 | poly << p; | |
122 | ||
123 | switch (_pos) | |
124 | { | |
125 | case Right: | |
126 | case Bottom: | |
127 | poly << QPoint(p.x() + l, p.y() + l); | |
128 | break; | |
129 | ||
130 | case Left: | |
131 | poly << QPoint(p.x() - l, p.y() + l); | |
132 | break; | |
133 | ||
134 | case Top: | |
135 | poly << QPoint(p.x() + l, p.y() - l); | |
136 | break; | |
137 | } | |
138 | ||
139 | return poly; | |
140 | } | |
141 | ||
142 | QRegion Popup::arrow_region() const | |
143 | { | |
144 | return QRegion(arrow_polygon()); | |
145 | } | |
146 | ||
147 | QRect Popup::bubble_rect() const | |
148 | { | |
149 | return QRect( | |
150 | QPoint((_pos == Right) ? ArrowLength : 0, | |
151 | (_pos == Bottom) ? ArrowLength : 0), | |
152 | QSize(width() - ((_pos == Left || _pos == Right) ? | |
153 | ArrowLength : 0), | |
154 | height() - ((_pos == Top || _pos == Bottom) ? | |
155 | ArrowLength : 0))); | |
156 | } | |
157 | ||
158 | QRegion Popup::bubble_region() const | |
159 | { | |
160 | const QRect rect(bubble_rect()); | |
161 | ||
162 | const unsigned int r = MarginWidth; | |
163 | const unsigned int d = 2 * r; | |
164 | return QRegion(rect.adjusted(r, 0, -r, 0)).united( | |
165 | QRegion(rect.adjusted(0, r, 0, -r))).united( | |
166 | QRegion(rect.left(), rect.top(), d, d, | |
167 | QRegion::Ellipse)).united( | |
168 | QRegion(rect.right() - d, rect.top(), d, d, | |
169 | QRegion::Ellipse)).united( | |
170 | QRegion(rect.left(), rect.bottom() - d, d, d, | |
171 | QRegion::Ellipse)).united( | |
172 | QRegion(rect.right() - d, rect.bottom() - d, d, d, | |
173 | QRegion::Ellipse)); | |
174 | } | |
175 | ||
176 | QRegion Popup::popup_region() const | |
177 | { | |
092e2a0a JH |
178 | if (space_for_arrow()) |
179 | return arrow_region().united(bubble_region()); | |
180 | else | |
181 | return bubble_region(); | |
6e3f046e JH |
182 | } |
183 | ||
184 | void Popup::reposition_widget() | |
185 | { | |
186 | QPoint o; | |
187 | ||
092e2a0a JH |
188 | const QRect screen_rect = QApplication::desktop()->availableGeometry( |
189 | QApplication::desktop()->screenNumber(_point)); | |
190 | ||
6e3f046e JH |
191 | if (_pos == Right || _pos == Left) |
192 | o.ry() = -height() / 2; | |
193 | else | |
194 | o.rx() = -width() / 2; | |
195 | ||
196 | if (_pos == Left) | |
197 | o.rx() = -width(); | |
198 | else if(_pos == Top) | |
199 | o.ry() = -height(); | |
200 | ||
092e2a0a JH |
201 | o += _point; |
202 | move(max(min(o.x(), screen_rect.right() - width()), | |
203 | screen_rect.left()), | |
204 | max(min(o.y(), screen_rect.bottom() - height()), | |
205 | screen_rect.top())); | |
6e3f046e JH |
206 | } |
207 | ||
0bce8609 JH |
208 | void Popup::closeEvent(QCloseEvent*) |
209 | { | |
210 | closed(); | |
211 | } | |
212 | ||
6e3f046e JH |
213 | void Popup::paintEvent(QPaintEvent*) |
214 | { | |
215 | QPainter painter(this); | |
216 | painter.setRenderHint(QPainter::Antialiasing); | |
217 | ||
218 | const QColor outline_color(QApplication::palette().color( | |
219 | QPalette::Dark)); | |
220 | ||
092e2a0a | 221 | // Draw the bubble |
6e3f046e JH |
222 | const QRegion b = bubble_region(); |
223 | const QRegion bubble_outline = QRegion(rect()).subtracted( | |
224 | b.translated(1, 0).intersected(b.translated(0, 1).intersected( | |
225 | b.translated(-1, 0).intersected(b.translated(0, -1))))); | |
226 | ||
227 | painter.setPen(Qt::NoPen); | |
228 | painter.setBrush(QApplication::palette().brush(QPalette::Window)); | |
229 | painter.drawRect(rect()); | |
230 | ||
092e2a0a JH |
231 | // Draw the arrow |
232 | if (!space_for_arrow()) | |
233 | return; | |
234 | ||
6e3f046e JH |
235 | const QPoint ArrowOffsets[] = { |
236 | QPoint(1, 0), QPoint(0, -1), QPoint(-1, 0), QPoint(0, 1)}; | |
237 | ||
238 | const QRegion a(arrow_region()); | |
239 | const QRegion arrow_outline = a.subtracted( | |
240 | a.translated(ArrowOffsets[_pos])); | |
241 | ||
242 | painter.setClipRegion(bubble_outline.subtracted(a).united( | |
243 | arrow_outline)); | |
244 | painter.setBrush(outline_color); | |
245 | painter.drawRect(rect()); | |
246 | } | |
247 | ||
248 | void Popup::resizeEvent(QResizeEvent*) | |
249 | { | |
250 | reposition_widget(); | |
251 | setMask(popup_region()); | |
252 | } | |
253 | ||
6e3f046e JH |
254 | void Popup::mouseReleaseEvent(QMouseEvent *e) |
255 | { | |
256 | assert(e); | |
257 | ||
258 | // We need our own out-of-bounds click handler because QWidget counts | |
259 | // the drop-shadow region as inside the widget | |
260 | if(!bubble_rect().contains(e->pos())) | |
261 | close(); | |
262 | } | |
263 | ||
b7b659aa JH |
264 | void Popup::showEvent(QShowEvent*) |
265 | { | |
266 | reposition_widget(); | |
267 | } | |
268 | ||
6e3f046e JH |
269 | } // namespace widgets |
270 | } // namespace pv | |
271 |