]>
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 | |
efdec55a | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
6e3f046e JH |
18 | */ |
19 | ||
20 | #include <algorithm> | |
21 | ||
b213ef09 JH |
22 | #include <assert.h> |
23 | ||
6e3f046e | 24 | #include <QtGui> |
d528d3d1 MC |
25 | #include <QApplication> |
26 | #include <QDesktopWidget> | |
0715fb8c | 27 | #include <QLineEdit> |
6e3f046e | 28 | |
2acdb232 | 29 | #include "popup.hpp" |
6e3f046e | 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), | |
8dbbc7f0 JH |
43 | point_(), |
44 | pos_(Left) | |
6e3f046e JH |
45 | { |
46 | } | |
47 | ||
48 | const QPoint& Popup::point() const | |
49 | { | |
8dbbc7f0 | 50 | return point_; |
6e3f046e JH |
51 | } |
52 | ||
53 | Popup::Position Popup::position() const | |
54 | { | |
8dbbc7f0 | 55 | return pos_; |
6e3f046e JH |
56 | } |
57 | ||
58 | void Popup::set_position(const QPoint point, Position pos) | |
59 | { | |
8dbbc7f0 | 60 | point_ = point, pos_ = pos; |
6e3f046e JH |
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)); | |
6e3f046e JH |
67 | } |
68 | ||
d9ea9628 | 69 | bool Popup::eventFilter(QObject *obj, QEvent *event) |
0715fb8c SA |
70 | { |
71 | QKeyEvent *keyEvent; | |
72 | ||
73 | (void)obj; | |
74 | ||
d9ea9628 UH |
75 | if (event->type() == QEvent::KeyPress) { |
76 | keyEvent = static_cast<QKeyEvent*>(event); | |
0715fb8c SA |
77 | if (keyEvent->key() == Qt::Key_Enter || |
78 | keyEvent->key() == Qt::Key_Return) { | |
1db1bdd6 | 79 | close(); |
0715fb8c SA |
80 | return true; |
81 | } | |
82 | } | |
83 | ||
84 | return false; | |
85 | } | |
86 | ||
87 | void Popup::show() | |
88 | { | |
89 | QLineEdit* le; | |
90 | ||
91 | QWidget::show(); | |
92 | ||
93 | // We want to close the popup when the Enter key was | |
94 | // pressed and the first editable widget had focus. | |
95 | if ((le = this->findChild<QLineEdit*>())) { | |
96 | ||
97 | // For combo boxes we need to hook into the parent of | |
98 | // the line edit (i.e. the QComboBox). For edit boxes | |
99 | // we hook into the widget directly. | |
100 | if (le->parent()->metaObject()->className() == | |
101 | this->metaObject()->className()) | |
102 | le->installEventFilter(this); | |
103 | else | |
104 | le->parent()->installEventFilter(this); | |
1db1bdd6 SA |
105 | |
106 | le->selectAll(); | |
107 | le->setFocus(); | |
0715fb8c SA |
108 | } |
109 | } | |
110 | ||
092e2a0a JH |
111 | bool Popup::space_for_arrow() const |
112 | { | |
113 | // Check if there is room for the arrow | |
8dbbc7f0 | 114 | switch (pos_) { |
092e2a0a | 115 | case Right: |
8dbbc7f0 | 116 | if (point_.x() > x()) |
092e2a0a JH |
117 | return false; |
118 | return true; | |
119 | ||
120 | case Bottom: | |
8dbbc7f0 | 121 | if (point_.y() > y()) |
092e2a0a JH |
122 | return false; |
123 | return true; | |
124 | ||
125 | case Left: | |
8dbbc7f0 | 126 | if (point_.x() < (x() + width())) |
092e2a0a JH |
127 | return false; |
128 | return true; | |
129 | ||
130 | case Top: | |
8dbbc7f0 | 131 | if (point_.y() < (y() + height())) |
092e2a0a JH |
132 | return false; |
133 | return true; | |
134 | } | |
135 | ||
136 | return true; | |
137 | } | |
138 | ||
6e3f046e JH |
139 | QPolygon Popup::arrow_polygon() const |
140 | { | |
141 | QPolygon poly; | |
142 | ||
8dbbc7f0 | 143 | const QPoint p = mapFromGlobal(point_); |
6e3f046e JH |
144 | const int l = ArrowLength + ArrowOverlap; |
145 | ||
2ad82c2e | 146 | switch (pos_) { |
6e3f046e JH |
147 | case Right: |
148 | poly << QPoint(p.x() + l, p.y() - l); | |
149 | break; | |
150 | ||
151 | case Bottom: | |
152 | poly << QPoint(p.x() - l, p.y() + l); | |
153 | break; | |
154 | ||
360ab9be | 155 | case Left: |
6e3f046e JH |
156 | case Top: |
157 | poly << QPoint(p.x() - l, p.y() - l); | |
158 | break; | |
159 | } | |
160 | ||
161 | poly << p; | |
162 | ||
2ad82c2e | 163 | switch (pos_) { |
6e3f046e JH |
164 | case Right: |
165 | case Bottom: | |
166 | poly << QPoint(p.x() + l, p.y() + l); | |
167 | break; | |
168 | ||
360ab9be | 169 | case Left: |
6e3f046e JH |
170 | poly << QPoint(p.x() - l, p.y() + l); |
171 | break; | |
172 | ||
173 | case Top: | |
174 | poly << QPoint(p.x() + l, p.y() - l); | |
175 | break; | |
176 | } | |
177 | ||
178 | return poly; | |
179 | } | |
180 | ||
181 | QRegion Popup::arrow_region() const | |
182 | { | |
183 | return QRegion(arrow_polygon()); | |
184 | } | |
185 | ||
186 | QRect Popup::bubble_rect() const | |
187 | { | |
188 | return QRect( | |
8dbbc7f0 JH |
189 | QPoint((pos_ == Right) ? ArrowLength : 0, |
190 | (pos_ == Bottom) ? ArrowLength : 0), | |
191 | QSize(width() - ((pos_ == Left || pos_ == Right) ? | |
6e3f046e | 192 | ArrowLength : 0), |
8dbbc7f0 | 193 | height() - ((pos_ == Top || pos_ == Bottom) ? |
6e3f046e JH |
194 | ArrowLength : 0))); |
195 | } | |
196 | ||
197 | QRegion Popup::bubble_region() const | |
198 | { | |
199 | const QRect rect(bubble_rect()); | |
200 | ||
201 | const unsigned int r = MarginWidth; | |
202 | const unsigned int d = 2 * r; | |
203 | return QRegion(rect.adjusted(r, 0, -r, 0)).united( | |
204 | QRegion(rect.adjusted(0, r, 0, -r))).united( | |
205 | QRegion(rect.left(), rect.top(), d, d, | |
206 | QRegion::Ellipse)).united( | |
207 | QRegion(rect.right() - d, rect.top(), d, d, | |
208 | QRegion::Ellipse)).united( | |
209 | QRegion(rect.left(), rect.bottom() - d, d, d, | |
210 | QRegion::Ellipse)).united( | |
211 | QRegion(rect.right() - d, rect.bottom() - d, d, d, | |
212 | QRegion::Ellipse)); | |
213 | } | |
214 | ||
215 | QRegion Popup::popup_region() const | |
216 | { | |
092e2a0a JH |
217 | if (space_for_arrow()) |
218 | return arrow_region().united(bubble_region()); | |
219 | else | |
220 | return bubble_region(); | |
6e3f046e JH |
221 | } |
222 | ||
223 | void Popup::reposition_widget() | |
224 | { | |
225 | QPoint o; | |
226 | ||
092e2a0a | 227 | const QRect screen_rect = QApplication::desktop()->availableGeometry( |
8dbbc7f0 | 228 | QApplication::desktop()->screenNumber(point_)); |
092e2a0a | 229 | |
8dbbc7f0 | 230 | if (pos_ == Right || pos_ == Left) |
6e3f046e JH |
231 | o.ry() = -height() / 2; |
232 | else | |
233 | o.rx() = -width() / 2; | |
234 | ||
8dbbc7f0 | 235 | if (pos_ == Left) |
6e3f046e | 236 | o.rx() = -width(); |
f3290553 | 237 | else if (pos_ == Top) |
6e3f046e JH |
238 | o.ry() = -height(); |
239 | ||
8dbbc7f0 | 240 | o += point_; |
092e2a0a JH |
241 | move(max(min(o.x(), screen_rect.right() - width()), |
242 | screen_rect.left()), | |
243 | max(min(o.y(), screen_rect.bottom() - height()), | |
244 | screen_rect.top())); | |
6e3f046e JH |
245 | } |
246 | ||
0bce8609 JH |
247 | void Popup::closeEvent(QCloseEvent*) |
248 | { | |
249 | closed(); | |
250 | } | |
251 | ||
6e3f046e JH |
252 | void Popup::paintEvent(QPaintEvent*) |
253 | { | |
254 | QPainter painter(this); | |
255 | painter.setRenderHint(QPainter::Antialiasing); | |
256 | ||
257 | const QColor outline_color(QApplication::palette().color( | |
258 | QPalette::Dark)); | |
259 | ||
092e2a0a | 260 | // Draw the bubble |
6e3f046e JH |
261 | const QRegion b = bubble_region(); |
262 | const QRegion bubble_outline = QRegion(rect()).subtracted( | |
263 | b.translated(1, 0).intersected(b.translated(0, 1).intersected( | |
264 | b.translated(-1, 0).intersected(b.translated(0, -1))))); | |
265 | ||
266 | painter.setPen(Qt::NoPen); | |
267 | painter.setBrush(QApplication::palette().brush(QPalette::Window)); | |
268 | painter.drawRect(rect()); | |
269 | ||
092e2a0a JH |
270 | // Draw the arrow |
271 | if (!space_for_arrow()) | |
272 | return; | |
273 | ||
6e3f046e JH |
274 | const QPoint ArrowOffsets[] = { |
275 | QPoint(1, 0), QPoint(0, -1), QPoint(-1, 0), QPoint(0, 1)}; | |
276 | ||
277 | const QRegion a(arrow_region()); | |
278 | const QRegion arrow_outline = a.subtracted( | |
8dbbc7f0 | 279 | a.translated(ArrowOffsets[pos_])); |
6e3f046e JH |
280 | |
281 | painter.setClipRegion(bubble_outline.subtracted(a).united( | |
282 | arrow_outline)); | |
283 | painter.setBrush(outline_color); | |
284 | painter.drawRect(rect()); | |
285 | } | |
286 | ||
287 | void Popup::resizeEvent(QResizeEvent*) | |
288 | { | |
289 | reposition_widget(); | |
290 | setMask(popup_region()); | |
291 | } | |
292 | ||
d9ea9628 | 293 | void Popup::mouseReleaseEvent(QMouseEvent *event) |
6e3f046e | 294 | { |
d9ea9628 | 295 | assert(event); |
6e3f046e JH |
296 | |
297 | // We need our own out-of-bounds click handler because QWidget counts | |
298 | // the drop-shadow region as inside the widget | |
d9ea9628 | 299 | if (!bubble_rect().contains(event->pos())) |
6e3f046e JH |
300 | close(); |
301 | } | |
302 | ||
b7b659aa JH |
303 | void Popup::showEvent(QShowEvent*) |
304 | { | |
305 | reposition_widget(); | |
306 | } | |
307 | ||
6e3f046e JH |
308 | } // namespace widgets |
309 | } // namespace pv |