]> sigrok.org Git - pulseview.git/blame_incremental - widgets/popup.cpp
Session: Fix issue #67 by improving error handling
[pulseview.git] / widgets / popup.cpp
... / ...
CommitLineData
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, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <algorithm>
21
22#include <assert.h>
23
24#include <QtGui>
25#include <QApplication>
26#include <QDesktopWidget>
27#include <QLineEdit>
28
29#include "popup.hpp"
30
31using std::max;
32using std::min;
33
34namespace pv {
35namespace widgets {
36
37const unsigned int Popup::ArrowLength = 10;
38const unsigned int Popup::ArrowOverlap = 3;
39const unsigned int Popup::MarginWidth = 6;
40
41Popup::Popup(QWidget *parent) :
42 QWidget(parent, Qt::Popup | Qt::FramelessWindowHint),
43 point_(),
44 pos_(Left)
45{
46}
47
48const QPoint& Popup::point() const
49{
50 return point_;
51}
52
53Popup::Position Popup::position() const
54{
55 return pos_;
56}
57
58void Popup::set_position(const QPoint point, Position pos)
59{
60 point_ = point, pos_ = pos;
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));
67}
68
69bool Popup::eventFilter(QObject *obj, QEvent *event)
70{
71 QKeyEvent *keyEvent;
72
73 (void)obj;
74
75 if (event->type() == QEvent::KeyPress) {
76 keyEvent = static_cast<QKeyEvent*>(event);
77 if (keyEvent->key() == Qt::Key_Enter ||
78 keyEvent->key() == Qt::Key_Return) {
79 close();
80 return true;
81 }
82 }
83
84 return false;
85}
86
87void 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);
105
106 le->selectAll();
107 le->setFocus();
108 }
109}
110
111bool Popup::space_for_arrow() const
112{
113 // Check if there is room for the arrow
114 switch (pos_) {
115 case Right:
116 if (point_.x() > x())
117 return false;
118 return true;
119
120 case Bottom:
121 if (point_.y() > y())
122 return false;
123 return true;
124
125 case Left:
126 if (point_.x() < (x() + width()))
127 return false;
128 return true;
129
130 case Top:
131 if (point_.y() < (y() + height()))
132 return false;
133 return true;
134 }
135
136 return true;
137}
138
139QPolygon Popup::arrow_polygon() const
140{
141 QPolygon poly;
142
143 const QPoint p = mapFromGlobal(point_);
144 const int l = ArrowLength + ArrowOverlap;
145
146 switch (pos_) {
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 case Right:
165 case Bottom:
166 poly << QPoint(p.x() + l, p.y() + l);
167 break;
168
169 case Left:
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
181QRegion Popup::arrow_region() const
182{
183 return QRegion(arrow_polygon());
184}
185
186QRect Popup::bubble_rect() const
187{
188 return QRect(
189 QPoint((pos_ == Right) ? ArrowLength : 0,
190 (pos_ == Bottom) ? ArrowLength : 0),
191 QSize(width() - ((pos_ == Left || pos_ == Right) ?
192 ArrowLength : 0),
193 height() - ((pos_ == Top || pos_ == Bottom) ?
194 ArrowLength : 0)));
195}
196
197QRegion 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
215QRegion Popup::popup_region() const
216{
217 if (space_for_arrow())
218 return arrow_region().united(bubble_region());
219 else
220 return bubble_region();
221}
222
223void Popup::reposition_widget()
224{
225 QPoint o;
226
227 const QRect screen_rect = QApplication::desktop()->availableGeometry(
228 QApplication::desktop()->screenNumber(point_));
229
230 if (pos_ == Right || pos_ == Left)
231 o.ry() = -height() / 2;
232 else
233 o.rx() = -width() / 2;
234
235 if (pos_ == Left)
236 o.rx() = -width();
237 else if (pos_ == Top)
238 o.ry() = -height();
239
240 o += point_;
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()));
245}
246
247void Popup::closeEvent(QCloseEvent*)
248{
249 closed();
250}
251
252void 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
260 // Draw the bubble
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
270 // Draw the arrow
271 if (!space_for_arrow())
272 return;
273
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(
279 a.translated(ArrowOffsets[pos_]));
280
281 painter.setClipRegion(bubble_outline.subtracted(a).united(
282 arrow_outline));
283 painter.setBrush(outline_color);
284 painter.drawRect(rect());
285}
286
287void Popup::resizeEvent(QResizeEvent*)
288{
289 reposition_widget();
290 setMask(popup_region());
291}
292
293void Popup::mouseReleaseEvent(QMouseEvent *event)
294{
295 assert(event);
296
297 // We need our own out-of-bounds click handler because QWidget counts
298 // the drop-shadow region as inside the widget
299 if (!bubble_rect().contains(event->pos()))
300 close();
301}
302
303void Popup::showEvent(QShowEvent*)
304{
305 reposition_widget();
306}
307
308} // namespace widgets
309} // namespace pv