Moved pv::prop:bindings classes into pv::bindings namespace
[pulseview.git] / pv / widgets / popup.cpp
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
23 #include <assert.h>
24
25 #include <QtGui>
26 #include <QApplication>
27 #include <QDesktopWidget>
28 #include <QLineEdit>
29
30 #include "popup.hpp"
31
32 using std::max;
33 using std::min;
34
35 namespace pv {
36 namespace widgets {
37
38 const unsigned int Popup::ArrowLength = 10;
39 const unsigned int Popup::ArrowOverlap = 3;
40 const unsigned int Popup::MarginWidth = 6;
41
42 Popup::Popup(QWidget *parent) :
43         QWidget(parent, Qt::Popup | Qt::FramelessWindowHint),
44         point_(),
45         pos_(Left)
46 {
47 }
48
49 const QPoint& Popup::point() const
50 {
51         return point_;
52 }
53
54 Popup::Position Popup::position() const
55 {
56         return pos_;
57 }
58
59 void 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
71 bool 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                         close();
82                         return true;
83                 }
84         }
85
86         return false;
87 }
88
89 void 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                 le->selectAll();
109                 le->setFocus();
110         }
111 }
112
113 bool Popup::space_for_arrow() const
114 {
115         // Check if there is room for the arrow
116         switch (pos_) {
117         case Right:
118                 if (point_.x() > x())
119                         return false;
120                 return true;
121
122         case Bottom:
123                 if (point_.y() > y())
124                         return false;
125                 return true;            
126
127         case Left:
128                 if (point_.x() < (x() + width()))
129                         return false;
130                 return true;
131
132         case Top:
133                 if (point_.y() < (y() + height()))
134                         return false;
135                 return true;
136         }
137
138         return true;
139 }
140
141 QPolygon Popup::arrow_polygon() const
142 {
143         QPolygon poly;
144
145         const QPoint p = mapFromGlobal(point_);
146         const int l = ArrowLength + ArrowOverlap; 
147
148         switch (pos_)
149         {
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
158         case Left:
159         case Top:
160                 poly << QPoint(p.x() - l, p.y() - l);
161                 break;
162         }
163
164         poly << p;
165
166         switch (pos_)
167         {
168         case Right:
169         case Bottom:
170                 poly << QPoint(p.x() + l, p.y() + l);
171                 break;
172
173         case Left:
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
185 QRegion Popup::arrow_region() const
186 {
187         return QRegion(arrow_polygon());
188 }
189
190 QRect Popup::bubble_rect() const
191 {
192         return QRect(
193                 QPoint((pos_ == Right) ? ArrowLength : 0,
194                         (pos_ == Bottom) ? ArrowLength : 0),
195                 QSize(width() - ((pos_ == Left || pos_ == Right) ?
196                                 ArrowLength : 0),
197                         height() - ((pos_ == Top || pos_ == Bottom) ?
198                                 ArrowLength : 0)));
199 }
200
201 QRegion 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
219 QRegion Popup::popup_region() const
220 {
221         if (space_for_arrow())
222                 return arrow_region().united(bubble_region());
223         else
224                 return bubble_region();
225 }
226
227 void Popup::reposition_widget()
228 {
229         QPoint o;
230
231         const QRect screen_rect = QApplication::desktop()->availableGeometry(
232                 QApplication::desktop()->screenNumber(point_));
233
234         if (pos_ == Right || pos_ == Left)
235                 o.ry() = -height() / 2;
236         else
237                 o.rx() = -width() / 2;
238
239         if (pos_ == Left)
240                 o.rx() = -width();
241         else if(pos_ == Top)
242                 o.ry() = -height();
243
244         o += point_;
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()));
249 }
250
251 void Popup::closeEvent(QCloseEvent*)
252 {
253         closed();
254 }
255
256 void 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
264         // Draw the bubble
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
274         // Draw the arrow
275         if (!space_for_arrow())
276                 return;
277
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(
283                 a.translated(ArrowOffsets[pos_]));
284
285         painter.setClipRegion(bubble_outline.subtracted(a).united(
286                 arrow_outline));
287         painter.setBrush(outline_color);
288         painter.drawRect(rect());
289 }
290
291 void Popup::resizeEvent(QResizeEvent*)
292 {
293         reposition_widget();
294         setMask(popup_region());
295 }
296
297 void 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
307 void Popup::showEvent(QShowEvent*)
308 {
309         reposition_widget();
310 }
311
312 } // namespace widgets
313 } // namespace pv