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