2 * This file is part of the PulseView project.
4 * Copyright (C) 2013 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
26 #include <QApplication>
27 #include <QDesktopWidget>
37 const unsigned int Popup::ArrowLength = 10;
38 const unsigned int Popup::ArrowOverlap = 3;
39 const unsigned int Popup::MarginWidth = 6;
41 Popup::Popup(QWidget *parent) :
42 QWidget(parent, Qt::Popup | Qt::FramelessWindowHint),
48 const QPoint& Popup::point() const
53 Popup::Position Popup::position() const
58 void Popup::set_position(const QPoint point, Position pos)
60 _point = point, _pos = pos;
63 MarginWidth + ((pos == Right) ? ArrowLength : 0),
64 MarginWidth + ((pos == Bottom) ? ArrowLength : 0),
65 MarginWidth + ((pos == Left) ? ArrowLength : 0),
66 MarginWidth + ((pos == Top) ? ArrowLength : 0));
70 bool Popup::space_for_arrow() const
72 // Check if there is room for the arrow
85 if (_point.x() < (x() + width()))
90 if (_point.y() < (y() + height()))
98 QPolygon Popup::arrow_polygon() const
102 const QPoint p = mapFromGlobal(_point);
103 const int l = ArrowLength + ArrowOverlap;
108 poly << QPoint(p.x() + l, p.y() - l);
112 poly << QPoint(p.x() - l, p.y() + l);
117 poly << QPoint(p.x() - l, p.y() - l);
127 poly << QPoint(p.x() + l, p.y() + l);
131 poly << QPoint(p.x() - l, p.y() + l);
135 poly << QPoint(p.x() + l, p.y() - l);
142 QRegion Popup::arrow_region() const
144 return QRegion(arrow_polygon());
147 QRect Popup::bubble_rect() const
150 QPoint((_pos == Right) ? ArrowLength : 0,
151 (_pos == Bottom) ? ArrowLength : 0),
152 QSize(width() - ((_pos == Left || _pos == Right) ?
154 height() - ((_pos == Top || _pos == Bottom) ?
158 QRegion Popup::bubble_region() const
160 const QRect rect(bubble_rect());
162 const unsigned int r = MarginWidth;
163 const unsigned int d = 2 * r;
164 return QRegion(rect.adjusted(r, 0, -r, 0)).united(
165 QRegion(rect.adjusted(0, r, 0, -r))).united(
166 QRegion(rect.left(), rect.top(), d, d,
167 QRegion::Ellipse)).united(
168 QRegion(rect.right() - d, rect.top(), d, d,
169 QRegion::Ellipse)).united(
170 QRegion(rect.left(), rect.bottom() - d, d, d,
171 QRegion::Ellipse)).united(
172 QRegion(rect.right() - d, rect.bottom() - d, d, d,
176 QRegion Popup::popup_region() const
178 if (space_for_arrow())
179 return arrow_region().united(bubble_region());
181 return bubble_region();
184 void Popup::reposition_widget()
188 const QRect screen_rect = QApplication::desktop()->availableGeometry(
189 QApplication::desktop()->screenNumber(_point));
191 if (_pos == Right || _pos == Left)
192 o.ry() = -height() / 2;
194 o.rx() = -width() / 2;
202 move(max(min(o.x(), screen_rect.right() - width()),
204 max(min(o.y(), screen_rect.bottom() - height()),
208 void Popup::closeEvent(QCloseEvent*)
213 void Popup::paintEvent(QPaintEvent*)
215 QPainter painter(this);
216 painter.setRenderHint(QPainter::Antialiasing);
218 const QColor outline_color(QApplication::palette().color(
222 const QRegion b = bubble_region();
223 const QRegion bubble_outline = QRegion(rect()).subtracted(
224 b.translated(1, 0).intersected(b.translated(0, 1).intersected(
225 b.translated(-1, 0).intersected(b.translated(0, -1)))));
227 painter.setPen(Qt::NoPen);
228 painter.setBrush(QApplication::palette().brush(QPalette::Window));
229 painter.drawRect(rect());
232 if (!space_for_arrow())
235 const QPoint ArrowOffsets[] = {
236 QPoint(1, 0), QPoint(0, -1), QPoint(-1, 0), QPoint(0, 1)};
238 const QRegion a(arrow_region());
239 const QRegion arrow_outline = a.subtracted(
240 a.translated(ArrowOffsets[_pos]));
242 painter.setClipRegion(bubble_outline.subtracted(a).united(
244 painter.setBrush(outline_color);
245 painter.drawRect(rect());
248 void Popup::resizeEvent(QResizeEvent*)
251 setMask(popup_region());
254 void Popup::mouseReleaseEvent(QMouseEvent *e)
258 // We need our own out-of-bounds click handler because QWidget counts
259 // the drop-shadow region as inside the widget
260 if(!bubble_rect().contains(e->pos()))
264 void Popup::showEvent(QShowEvent*)
269 } // namespace widgets