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
35 const unsigned int Popup::ArrowLength = 10;
36 const unsigned int Popup::ArrowOverlap = 3;
37 const unsigned int Popup::MarginWidth = 6;
39 Popup::Popup(QWidget *parent) :
40 QWidget(parent, Qt::Popup | Qt::FramelessWindowHint),
46 const QPoint& Popup::point() const
51 Popup::Position Popup::position() const
56 void Popup::set_position(const QPoint point, Position pos)
58 _point = point, _pos = pos;
61 MarginWidth + ((pos == Right) ? ArrowLength : 0),
62 MarginWidth + ((pos == Bottom) ? ArrowLength : 0),
63 MarginWidth + ((pos == Left) ? ArrowLength : 0),
64 MarginWidth + ((pos == Top) ? ArrowLength : 0));
68 bool Popup::space_for_arrow() const
70 // Check if there is room for the arrow
83 if (_point.x() < (x() + width()))
88 if (_point.y() < (y() + height()))
96 QPolygon Popup::arrow_polygon() const
100 const QPoint p = mapFromGlobal(_point);
101 const int l = ArrowLength + ArrowOverlap;
106 poly << QPoint(p.x() + l, p.y() - l);
110 poly << QPoint(p.x() - l, p.y() + l);
115 poly << QPoint(p.x() - l, p.y() - l);
125 poly << QPoint(p.x() + l, p.y() + l);
129 poly << QPoint(p.x() - l, p.y() + l);
133 poly << QPoint(p.x() + l, p.y() - l);
140 QRegion Popup::arrow_region() const
142 return QRegion(arrow_polygon());
145 QRect Popup::bubble_rect() const
148 QPoint((_pos == Right) ? ArrowLength : 0,
149 (_pos == Bottom) ? ArrowLength : 0),
150 QSize(width() - ((_pos == Left || _pos == Right) ?
152 height() - ((_pos == Top || _pos == Bottom) ?
156 QRegion Popup::bubble_region() const
158 const QRect rect(bubble_rect());
160 const unsigned int r = MarginWidth;
161 const unsigned int d = 2 * r;
162 return QRegion(rect.adjusted(r, 0, -r, 0)).united(
163 QRegion(rect.adjusted(0, r, 0, -r))).united(
164 QRegion(rect.left(), rect.top(), d, d,
165 QRegion::Ellipse)).united(
166 QRegion(rect.right() - d, rect.top(), d, d,
167 QRegion::Ellipse)).united(
168 QRegion(rect.left(), rect.bottom() - d, d, d,
169 QRegion::Ellipse)).united(
170 QRegion(rect.right() - d, rect.bottom() - d, d, d,
174 QRegion Popup::popup_region() const
176 if (space_for_arrow())
177 return arrow_region().united(bubble_region());
179 return bubble_region();
182 void Popup::reposition_widget()
186 const QRect screen_rect = QApplication::desktop()->availableGeometry(
187 QApplication::desktop()->screenNumber(_point));
189 if (_pos == Right || _pos == Left)
190 o.ry() = -height() / 2;
192 o.rx() = -width() / 2;
200 move(max(min(o.x(), screen_rect.right() - width()),
202 max(min(o.y(), screen_rect.bottom() - height()),
206 void Popup::closeEvent(QCloseEvent*)
211 void Popup::paintEvent(QPaintEvent*)
213 QPainter painter(this);
214 painter.setRenderHint(QPainter::Antialiasing);
216 const QColor outline_color(QApplication::palette().color(
220 const QRegion b = bubble_region();
221 const QRegion bubble_outline = QRegion(rect()).subtracted(
222 b.translated(1, 0).intersected(b.translated(0, 1).intersected(
223 b.translated(-1, 0).intersected(b.translated(0, -1)))));
225 painter.setPen(Qt::NoPen);
226 painter.setBrush(QApplication::palette().brush(QPalette::Window));
227 painter.drawRect(rect());
230 if (!space_for_arrow())
233 const QPoint ArrowOffsets[] = {
234 QPoint(1, 0), QPoint(0, -1), QPoint(-1, 0), QPoint(0, 1)};
236 const QRegion a(arrow_region());
237 const QRegion arrow_outline = a.subtracted(
238 a.translated(ArrowOffsets[_pos]));
240 painter.setClipRegion(bubble_outline.subtracted(a).united(
242 painter.setBrush(outline_color);
243 painter.drawRect(rect());
246 void Popup::resizeEvent(QResizeEvent*)
249 setMask(popup_region());
252 void Popup::mouseReleaseEvent(QMouseEvent *e)
256 // We need our own out-of-bounds click handler because QWidget counts
257 // the drop-shadow region as inside the widget
258 if(!bubble_rect().contains(e->pos()))
262 void Popup::showEvent(QShowEvent*)
267 } // namespace widgets