]> sigrok.org Git - pulseview.git/blame - pv/widgets/popup.cpp
Reduced popup padding
[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
JH
25#include <QtGui>
26
27#include "popup.h"
28
29using namespace std;
30
31namespace pv {
32namespace widgets {
33
b6a2899a 34const unsigned int Popup::ArrowLength = 10;
6e3f046e 35const unsigned int Popup::ArrowOverlap = 3;
b6a2899a 36const unsigned int Popup::MarginWidth = 6;
6e3f046e
JH
37
38Popup::Popup(QWidget *parent) :
39 QWidget(parent, Qt::Popup | Qt::FramelessWindowHint),
40 _point(),
41 _pos(Left)
42{
43}
44
45const QPoint& Popup::point() const
46{
47 return _point;
48}
49
50Popup::Position Popup::position() const
51{
52 return _pos;
53}
54
55void 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
67QPolygon 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
111QRegion Popup::arrow_region() const
112{
113 return QRegion(arrow_polygon());
114}
115
116QRect 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
127QRegion 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
145QRegion Popup::popup_region() const
146{
147 return arrow_region().united(bubble_region());
148}
149
150void 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
167void Popup::paintEvent(QPaintEvent*)
168{
169 QPainter painter(this);
170 painter.setRenderHint(QPainter::Antialiasing);
171
172 const QColor outline_color(QApplication::palette().color(
173 QPalette::Dark));
174
175 const QRegion b = bubble_region();
176 const QRegion bubble_outline = QRegion(rect()).subtracted(
177 b.translated(1, 0).intersected(b.translated(0, 1).intersected(
178 b.translated(-1, 0).intersected(b.translated(0, -1)))));
179
180 painter.setPen(Qt::NoPen);
181 painter.setBrush(QApplication::palette().brush(QPalette::Window));
182 painter.drawRect(rect());
183
184 const QPoint ArrowOffsets[] = {
185 QPoint(1, 0), QPoint(0, -1), QPoint(-1, 0), QPoint(0, 1)};
186
187 const QRegion a(arrow_region());
188 const QRegion arrow_outline = a.subtracted(
189 a.translated(ArrowOffsets[_pos]));
190
191 painter.setClipRegion(bubble_outline.subtracted(a).united(
192 arrow_outline));
193 painter.setBrush(outline_color);
194 painter.drawRect(rect());
195}
196
197void Popup::resizeEvent(QResizeEvent*)
198{
199 reposition_widget();
200 setMask(popup_region());
201}
202
6e3f046e
JH
203void Popup::mouseReleaseEvent(QMouseEvent *e)
204{
205 assert(e);
206
207 // We need our own out-of-bounds click handler because QWidget counts
208 // the drop-shadow region as inside the widget
209 if(!bubble_rect().contains(e->pos()))
210 close();
211}
212
b7b659aa
JH
213void Popup::showEvent(QShowEvent*)
214{
215 reposition_widget();
216}
217
6e3f046e
JH
218} // namespace widgets
219} // namespace pv
220