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