]> sigrok.org Git - pulseview.git/blob - pv/widgets/popup.cpp
73ac1d84bd81eb5d268bd48c82492c6d84afc935
[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
27 #include "popup.h"
28
29 using namespace std;
30
31 namespace pv {
32 namespace widgets {
33
34 const unsigned int Popup::ArrowLength = 10;
35 const unsigned int Popup::ArrowOverlap = 3;
36 const unsigned int Popup::MarginWidth = 6;
37
38 Popup::Popup(QWidget *parent) :
39         QWidget(parent, Qt::Popup | Qt::FramelessWindowHint),
40         _point(),
41         _pos(Left)
42 {
43 }
44
45 const QPoint& Popup::point() const
46 {
47         return _point;
48 }
49
50 Popup::Position Popup::position() const
51 {
52         return _pos;
53 }
54
55 void Popup::set_position(const QPoint point, Position pos)
56 {
57         _point = point, _pos = pos;
58
59         setContentsMargins(
60                 MarginWidth + ((pos == Right) ? ArrowLength : 0),
61                 MarginWidth + ((pos == Bottom) ? ArrowLength : 0),
62                 MarginWidth + ((pos == Left) ? ArrowLength : 0),
63                 MarginWidth + ((pos == Top) ? ArrowLength : 0));
64
65 }
66
67 bool Popup::space_for_arrow() const
68 {
69         // Check if there is room for the arrow
70         switch (_pos) {
71         case Right:
72                 if (_point.x() > x())
73                         return false;
74                 return true;
75
76         case Bottom:
77                 if (_point.y() > y())
78                         return false;
79                 return true;            
80
81         case Left:
82                 if (_point.x() < (x() + width()))
83                         return false;
84                 return true;
85
86         case Top:
87                 if (_point.y() < (y() + height()))
88                         return false;
89                 return true;
90         }
91
92         return true;
93 }
94
95 QPolygon Popup::arrow_polygon() const
96 {
97         QPolygon poly;
98
99         const QPoint p = mapFromGlobal(_point);
100         const int l = ArrowLength + ArrowOverlap; 
101
102         switch (_pos)
103         {
104         case Right:
105                 poly << QPoint(p.x() + l, p.y() - l);
106                 break;
107
108         case Bottom:
109                 poly << QPoint(p.x() - l, p.y() + l);
110                 break;
111
112         case Left:
113         case Top:
114                 poly << QPoint(p.x() - l, p.y() - l);
115                 break;
116         }
117
118         poly << p;
119
120         switch (_pos)
121         {
122         case Right:
123         case Bottom:
124                 poly << QPoint(p.x() + l, p.y() + l);
125                 break;
126
127         case Left:
128                 poly << QPoint(p.x() - l, p.y() + l);
129                 break;
130                 
131         case Top:
132                 poly << QPoint(p.x() + l, p.y() - l);
133                 break;
134         }
135
136         return poly;
137 }
138
139 QRegion Popup::arrow_region() const
140 {
141         return QRegion(arrow_polygon());
142 }
143
144 QRect Popup::bubble_rect() const
145 {
146         return QRect(
147                 QPoint((_pos == Right) ? ArrowLength : 0,
148                         (_pos == Bottom) ? ArrowLength : 0),
149                 QSize(width() - ((_pos == Left || _pos == Right) ?
150                                 ArrowLength : 0),
151                         height() - ((_pos == Top || _pos == Bottom) ?
152                                 ArrowLength : 0)));
153 }
154
155 QRegion Popup::bubble_region() const
156 {
157         const QRect rect(bubble_rect());
158
159         const unsigned int r = MarginWidth;
160         const unsigned int d = 2 * r;
161         return QRegion(rect.adjusted(r, 0, -r, 0)).united(
162                 QRegion(rect.adjusted(0, r, 0, -r))).united(
163                 QRegion(rect.left(), rect.top(), d, d,
164                         QRegion::Ellipse)).united(
165                 QRegion(rect.right() - d, rect.top(), d, d,
166                         QRegion::Ellipse)).united(
167                 QRegion(rect.left(), rect.bottom() - d, d, d,
168                         QRegion::Ellipse)).united(
169                 QRegion(rect.right() - d, rect.bottom() - d, d, d,
170                         QRegion::Ellipse));
171 }
172
173 QRegion Popup::popup_region() const
174 {
175         if (space_for_arrow())
176                 return arrow_region().united(bubble_region());
177         else
178                 return bubble_region();
179 }
180
181 void Popup::reposition_widget()
182 {
183         QPoint o;
184
185         const QRect screen_rect = QApplication::desktop()->availableGeometry(
186                 QApplication::desktop()->screenNumber(_point));
187
188         if (_pos == Right || _pos == Left)
189                 o.ry() = -height() / 2;
190         else
191                 o.rx() = -width() / 2;
192
193         if (_pos == Left)
194                 o.rx() = -width();
195         else if(_pos == Top)
196                 o.ry() = -height();
197
198         o += _point;
199         move(max(min(o.x(), screen_rect.right() - width()),
200                         screen_rect.left()),
201                 max(min(o.y(), screen_rect.bottom() - height()),
202                         screen_rect.top()));
203 }
204
205 void Popup::closeEvent(QCloseEvent*)
206 {
207         closed();
208 }
209
210 void Popup::paintEvent(QPaintEvent*)
211 {
212         QPainter painter(this);
213         painter.setRenderHint(QPainter::Antialiasing);
214
215         const QColor outline_color(QApplication::palette().color(
216                 QPalette::Dark));
217
218         // Draw the bubble
219         const QRegion b = bubble_region();
220         const QRegion bubble_outline = QRegion(rect()).subtracted(
221                 b.translated(1, 0).intersected(b.translated(0, 1).intersected(
222                 b.translated(-1, 0).intersected(b.translated(0, -1)))));
223
224         painter.setPen(Qt::NoPen);
225         painter.setBrush(QApplication::palette().brush(QPalette::Window));
226         painter.drawRect(rect());
227
228         // Draw the arrow
229         if (!space_for_arrow())
230                 return;
231
232         const QPoint ArrowOffsets[] = {
233                 QPoint(1, 0), QPoint(0, -1), QPoint(-1, 0), QPoint(0, 1)};
234
235         const QRegion a(arrow_region());
236         const QRegion arrow_outline = a.subtracted(
237                 a.translated(ArrowOffsets[_pos]));
238
239         painter.setClipRegion(bubble_outline.subtracted(a).united(
240                 arrow_outline));
241         painter.setBrush(outline_color);
242         painter.drawRect(rect());
243 }
244
245 void Popup::resizeEvent(QResizeEvent*)
246 {
247         reposition_widget();
248         setMask(popup_region());
249 }
250
251 void Popup::mouseReleaseEvent(QMouseEvent *e)
252 {
253         assert(e);
254
255         // We need our own out-of-bounds click handler because QWidget counts
256         // the drop-shadow region as inside the widget
257         if(!bubble_rect().contains(e->pos()))
258                 close();
259 }
260
261 void Popup::showEvent(QShowEvent*)
262 {
263         reposition_widget();
264 }
265
266 } // namespace widgets
267 } // namespace pv
268