]> sigrok.org Git - pulseview.git/blame - pv/widgets/popup.cpp
Fix an isnan() issue on (at least) MinGW and Mac OS X.
[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 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
JH
41
42Popup::Popup(QWidget *parent) :
43 QWidget(parent, Qt::Popup | Qt::FramelessWindowHint),
8dbbc7f0
JH
44 point_(),
45 pos_(Left)
6e3f046e
JH
46{
47}
48
49const QPoint& Popup::point() const
50{
8dbbc7f0 51 return point_;
6e3f046e
JH
52}
53
54Popup::Position Popup::position() const
55{
8dbbc7f0 56 return pos_;
6e3f046e
JH
57}
58
59void Popup::set_position(const QPoint point, Position pos)
60{
8dbbc7f0 61 point_ = point, pos_ = pos;
6e3f046e
JH
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) {
1db1bdd6 81 close();
0715fb8c
SA
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);
1db1bdd6
SA
107
108 le->selectAll();
109 le->setFocus();
0715fb8c
SA
110 }
111}
112
092e2a0a
JH
113bool Popup::space_for_arrow() const
114{
115 // Check if there is room for the arrow
8dbbc7f0 116 switch (pos_) {
092e2a0a 117 case Right:
8dbbc7f0 118 if (point_.x() > x())
092e2a0a
JH
119 return false;
120 return true;
121
122 case Bottom:
8dbbc7f0 123 if (point_.y() > y())
092e2a0a
JH
124 return false;
125 return true;
126
127 case Left:
8dbbc7f0 128 if (point_.x() < (x() + width()))
092e2a0a
JH
129 return false;
130 return true;
131
132 case Top:
8dbbc7f0 133 if (point_.y() < (y() + height()))
092e2a0a
JH
134 return false;
135 return true;
136 }
137
138 return true;
139}
140
6e3f046e
JH
141QPolygon Popup::arrow_polygon() const
142{
143 QPolygon poly;
144
8dbbc7f0 145 const QPoint p = mapFromGlobal(point_);
6e3f046e
JH
146 const int l = ArrowLength + ArrowOverlap;
147
8dbbc7f0 148 switch (pos_)
360ab9be 149 {
6e3f046e
JH
150 case Right:
151 poly << QPoint(p.x() + l, p.y() - l);
152 break;
153
154 case Bottom:
155 poly << QPoint(p.x() - l, p.y() + l);
156 break;
157
360ab9be 158 case Left:
6e3f046e
JH
159 case Top:
160 poly << QPoint(p.x() - l, p.y() - l);
161 break;
162 }
163
164 poly << p;
165
8dbbc7f0 166 switch (pos_)
360ab9be 167 {
6e3f046e
JH
168 case Right:
169 case Bottom:
170 poly << QPoint(p.x() + l, p.y() + l);
171 break;
172
360ab9be 173 case Left:
6e3f046e
JH
174 poly << QPoint(p.x() - l, p.y() + l);
175 break;
176
177 case Top:
178 poly << QPoint(p.x() + l, p.y() - l);
179 break;
180 }
181
182 return poly;
183}
184
185QRegion Popup::arrow_region() const
186{
187 return QRegion(arrow_polygon());
188}
189
190QRect Popup::bubble_rect() const
191{
192 return QRect(
8dbbc7f0
JH
193 QPoint((pos_ == Right) ? ArrowLength : 0,
194 (pos_ == Bottom) ? ArrowLength : 0),
195 QSize(width() - ((pos_ == Left || pos_ == Right) ?
6e3f046e 196 ArrowLength : 0),
8dbbc7f0 197 height() - ((pos_ == Top || pos_ == Bottom) ?
6e3f046e
JH
198 ArrowLength : 0)));
199}
200
201QRegion Popup::bubble_region() const
202{
203 const QRect rect(bubble_rect());
204
205 const unsigned int r = MarginWidth;
206 const unsigned int d = 2 * r;
207 return QRegion(rect.adjusted(r, 0, -r, 0)).united(
208 QRegion(rect.adjusted(0, r, 0, -r))).united(
209 QRegion(rect.left(), rect.top(), d, d,
210 QRegion::Ellipse)).united(
211 QRegion(rect.right() - d, rect.top(), d, d,
212 QRegion::Ellipse)).united(
213 QRegion(rect.left(), rect.bottom() - d, d, d,
214 QRegion::Ellipse)).united(
215 QRegion(rect.right() - d, rect.bottom() - d, d, d,
216 QRegion::Ellipse));
217}
218
219QRegion Popup::popup_region() const
220{
092e2a0a
JH
221 if (space_for_arrow())
222 return arrow_region().united(bubble_region());
223 else
224 return bubble_region();
6e3f046e
JH
225}
226
227void Popup::reposition_widget()
228{
229 QPoint o;
230
092e2a0a 231 const QRect screen_rect = QApplication::desktop()->availableGeometry(
8dbbc7f0 232 QApplication::desktop()->screenNumber(point_));
092e2a0a 233
8dbbc7f0 234 if (pos_ == Right || pos_ == Left)
6e3f046e
JH
235 o.ry() = -height() / 2;
236 else
237 o.rx() = -width() / 2;
238
8dbbc7f0 239 if (pos_ == Left)
6e3f046e 240 o.rx() = -width();
8dbbc7f0 241 else if(pos_ == Top)
6e3f046e
JH
242 o.ry() = -height();
243
8dbbc7f0 244 o += point_;
092e2a0a
JH
245 move(max(min(o.x(), screen_rect.right() - width()),
246 screen_rect.left()),
247 max(min(o.y(), screen_rect.bottom() - height()),
248 screen_rect.top()));
6e3f046e
JH
249}
250
0bce8609
JH
251void Popup::closeEvent(QCloseEvent*)
252{
253 closed();
254}
255
6e3f046e
JH
256void Popup::paintEvent(QPaintEvent*)
257{
258 QPainter painter(this);
259 painter.setRenderHint(QPainter::Antialiasing);
260
261 const QColor outline_color(QApplication::palette().color(
262 QPalette::Dark));
263
092e2a0a 264 // Draw the bubble
6e3f046e
JH
265 const QRegion b = bubble_region();
266 const QRegion bubble_outline = QRegion(rect()).subtracted(
267 b.translated(1, 0).intersected(b.translated(0, 1).intersected(
268 b.translated(-1, 0).intersected(b.translated(0, -1)))));
269
270 painter.setPen(Qt::NoPen);
271 painter.setBrush(QApplication::palette().brush(QPalette::Window));
272 painter.drawRect(rect());
273
092e2a0a
JH
274 // Draw the arrow
275 if (!space_for_arrow())
276 return;
277
6e3f046e
JH
278 const QPoint ArrowOffsets[] = {
279 QPoint(1, 0), QPoint(0, -1), QPoint(-1, 0), QPoint(0, 1)};
280
281 const QRegion a(arrow_region());
282 const QRegion arrow_outline = a.subtracted(
8dbbc7f0 283 a.translated(ArrowOffsets[pos_]));
6e3f046e
JH
284
285 painter.setClipRegion(bubble_outline.subtracted(a).united(
286 arrow_outline));
287 painter.setBrush(outline_color);
288 painter.drawRect(rect());
289}
290
291void Popup::resizeEvent(QResizeEvent*)
292{
293 reposition_widget();
294 setMask(popup_region());
295}
296
6e3f046e
JH
297void Popup::mouseReleaseEvent(QMouseEvent *e)
298{
299 assert(e);
300
301 // We need our own out-of-bounds click handler because QWidget counts
302 // the drop-shadow region as inside the widget
303 if(!bubble_rect().contains(e->pos()))
304 close();
305}
306
b7b659aa
JH
307void Popup::showEvent(QShowEvent*)
308{
309 reposition_widget();
310}
311
6e3f046e
JH
312} // namespace widgets
313} // namespace pv