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
34 const unsigned int Popup::ArrowLength = 10;
35 const unsigned int Popup::ArrowOverlap = 3;
36 const unsigned int Popup::MarginWidth = 6;
38 Popup::Popup(QWidget *parent) :
39 QWidget(parent, Qt::Popup | Qt::FramelessWindowHint),
45 const QPoint& Popup::point() const
50 Popup::Position Popup::position() const
55 void Popup::set_position(const QPoint point, Position pos)
57 _point = point, _pos = pos;
60 MarginWidth + ((pos == Right) ? ArrowLength : 0),
61 MarginWidth + ((pos == Bottom) ? ArrowLength : 0),
62 MarginWidth + ((pos == Left) ? ArrowLength : 0),
63 MarginWidth + ((pos == Top) ? ArrowLength : 0));
67 bool Popup::space_for_arrow() const
69 // Check if there is room for the arrow
82 if (_point.x() < (x() + width()))
87 if (_point.y() < (y() + height()))
95 QPolygon Popup::arrow_polygon() const
99 const QPoint p = mapFromGlobal(_point);
100 const int l = ArrowLength + ArrowOverlap;
105 poly << QPoint(p.x() + l, p.y() - l);
109 poly << QPoint(p.x() - l, p.y() + l);
114 poly << QPoint(p.x() - l, p.y() - l);
124 poly << QPoint(p.x() + l, p.y() + l);
128 poly << QPoint(p.x() - l, p.y() + l);
132 poly << QPoint(p.x() + l, p.y() - l);
139 QRegion Popup::arrow_region() const
141 return QRegion(arrow_polygon());
144 QRect Popup::bubble_rect() const
147 QPoint((_pos == Right) ? ArrowLength : 0,
148 (_pos == Bottom) ? ArrowLength : 0),
149 QSize(width() - ((_pos == Left || _pos == Right) ?
151 height() - ((_pos == Top || _pos == Bottom) ?
155 QRegion Popup::bubble_region() const
157 const QRect rect(bubble_rect());
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,
173 QRegion Popup::popup_region() const
175 if (space_for_arrow())
176 return arrow_region().united(bubble_region());
178 return bubble_region();
181 void Popup::reposition_widget()
185 const QRect screen_rect = QApplication::desktop()->availableGeometry(
186 QApplication::desktop()->screenNumber(_point));
188 if (_pos == Right || _pos == Left)
189 o.ry() = -height() / 2;
191 o.rx() = -width() / 2;
199 move(max(min(o.x(), screen_rect.right() - width()),
201 max(min(o.y(), screen_rect.bottom() - height()),
205 void Popup::closeEvent(QCloseEvent*)
210 void Popup::paintEvent(QPaintEvent*)
212 QPainter painter(this);
213 painter.setRenderHint(QPainter::Antialiasing);
215 const QColor outline_color(QApplication::palette().color(
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)))));
224 painter.setPen(Qt::NoPen);
225 painter.setBrush(QApplication::palette().brush(QPalette::Window));
226 painter.drawRect(rect());
229 if (!space_for_arrow())
232 const QPoint ArrowOffsets[] = {
233 QPoint(1, 0), QPoint(0, -1), QPoint(-1, 0), QPoint(0, 1)};
235 const QRegion a(arrow_region());
236 const QRegion arrow_outline = a.subtracted(
237 a.translated(ArrowOffsets[_pos]));
239 painter.setClipRegion(bubble_outline.subtracted(a).united(
241 painter.setBrush(outline_color);
242 painter.drawRect(rect());
245 void Popup::resizeEvent(QResizeEvent*)
248 setMask(popup_region());
251 void Popup::mouseReleaseEvent(QMouseEvent *e)
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()))
261 void Popup::showEvent(QShowEvent*)
266 } // namespace widgets