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