]> sigrok.org Git - pulseview.git/blob - pv/widgets/popup.cpp
9863887ed5dab9595e69f6c8ba5b1308186223dd
[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 QPolygon Popup::arrow_polygon() const
68 {
69         QPolygon poly;
70
71         const QPoint p = mapFromGlobal(_point);
72         const int l = ArrowLength + ArrowOverlap; 
73
74         switch (_pos)
75         {
76         case Right:
77                 poly << QPoint(p.x() + l, p.y() - l);
78                 break;
79
80         case Bottom:
81                 poly << QPoint(p.x() - l, p.y() + l);
82                 break;
83
84         case Left:
85         case Top:
86                 poly << QPoint(p.x() - l, p.y() - l);
87                 break;
88         }
89
90         poly << p;
91
92         switch (_pos)
93         {
94         case Right:
95         case Bottom:
96                 poly << QPoint(p.x() + l, p.y() + l);
97                 break;
98
99         case Left:
100                 poly << QPoint(p.x() - l, p.y() + l);
101                 break;
102                 
103         case Top:
104                 poly << QPoint(p.x() + l, p.y() - l);
105                 break;
106         }
107
108         return poly;
109 }
110
111 QRegion Popup::arrow_region() const
112 {
113         return QRegion(arrow_polygon());
114 }
115
116 QRect Popup::bubble_rect() const
117 {
118         return QRect(
119                 QPoint((_pos == Right) ? ArrowLength : 0,
120                         (_pos == Bottom) ? ArrowLength : 0),
121                 QSize(width() - ((_pos == Left || _pos == Right) ?
122                                 ArrowLength : 0),
123                         height() - ((_pos == Top || _pos == Bottom) ?
124                                 ArrowLength : 0)));
125 }
126
127 QRegion Popup::bubble_region() const
128 {
129         const QRect rect(bubble_rect());
130
131         const unsigned int r = MarginWidth;
132         const unsigned int d = 2 * r;
133         return QRegion(rect.adjusted(r, 0, -r, 0)).united(
134                 QRegion(rect.adjusted(0, r, 0, -r))).united(
135                 QRegion(rect.left(), rect.top(), d, d,
136                         QRegion::Ellipse)).united(
137                 QRegion(rect.right() - d, rect.top(), d, d,
138                         QRegion::Ellipse)).united(
139                 QRegion(rect.left(), rect.bottom() - d, d, d,
140                         QRegion::Ellipse)).united(
141                 QRegion(rect.right() - d, rect.bottom() - d, d, d,
142                         QRegion::Ellipse));
143 }
144
145 QRegion Popup::popup_region() const
146 {
147         return arrow_region().united(bubble_region());
148 }
149
150 void Popup::reposition_widget()
151 {
152         QPoint o;
153
154         if (_pos == Right || _pos == Left)
155                 o.ry() = -height() / 2;
156         else
157                 o.rx() = -width() / 2;
158
159         if (_pos == Left)
160                 o.rx() = -width();
161         else if(_pos == Top)
162                 o.ry() = -height();
163
164         move(_point + o);
165 }
166
167 void Popup::closeEvent(QCloseEvent*)
168 {
169         closed();
170 }
171
172 void Popup::paintEvent(QPaintEvent*)
173 {
174         QPainter painter(this);
175         painter.setRenderHint(QPainter::Antialiasing);
176
177         const QColor outline_color(QApplication::palette().color(
178                 QPalette::Dark));
179
180         const QRegion b = bubble_region();
181         const QRegion bubble_outline = QRegion(rect()).subtracted(
182                 b.translated(1, 0).intersected(b.translated(0, 1).intersected(
183                 b.translated(-1, 0).intersected(b.translated(0, -1)))));
184
185         painter.setPen(Qt::NoPen);
186         painter.setBrush(QApplication::palette().brush(QPalette::Window));
187         painter.drawRect(rect());
188
189         const QPoint ArrowOffsets[] = {
190                 QPoint(1, 0), QPoint(0, -1), QPoint(-1, 0), QPoint(0, 1)};
191
192         const QRegion a(arrow_region());
193         const QRegion arrow_outline = a.subtracted(
194                 a.translated(ArrowOffsets[_pos]));
195
196         painter.setClipRegion(bubble_outline.subtracted(a).united(
197                 arrow_outline));
198         painter.setBrush(outline_color);
199         painter.drawRect(rect());
200 }
201
202 void Popup::resizeEvent(QResizeEvent*)
203 {
204         reposition_widget();
205         setMask(popup_region());
206 }
207
208 void Popup::mouseReleaseEvent(QMouseEvent *e)
209 {
210         assert(e);
211
212         // We need our own out-of-bounds click handler because QWidget counts
213         // the drop-shadow region as inside the widget
214         if(!bubble_rect().contains(e->pos()))
215                 close();
216 }
217
218 void Popup::showEvent(QShowEvent*)
219 {
220         reposition_widget();
221 }
222
223 } // namespace widgets
224 } // namespace pv
225