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
32 const unsigned int Popup::ArrowLength = 15;
33 const unsigned int Popup::ArrowOverlap = 3;
34 const unsigned int Popup::MarginWidth = 10;
36 Popup::Popup(QWidget *parent) :
37 QWidget(parent, Qt::Popup | Qt::FramelessWindowHint),
43 const QPoint& Popup::point() const
48 Popup::Position Popup::position() const
53 void Popup::set_position(const QPoint point, Position pos)
55 _point = point, _pos = pos;
58 MarginWidth + ((pos == Right) ? ArrowLength : 0),
59 MarginWidth + ((pos == Bottom) ? ArrowLength : 0),
60 MarginWidth + ((pos == Left) ? ArrowLength : 0),
61 MarginWidth + ((pos == Top) ? ArrowLength : 0));
65 QPolygon Popup::arrow_polygon() const
69 const QPoint p = mapFromGlobal(_point);
70 const int l = ArrowLength + ArrowOverlap;
75 poly << QPoint(p.x() + l, p.y() - l);
79 poly << QPoint(p.x() - l, p.y() + l);
84 poly << QPoint(p.x() - l, p.y() - l);
94 poly << QPoint(p.x() + l, p.y() + l);
98 poly << QPoint(p.x() - l, p.y() + l);
102 poly << QPoint(p.x() + l, p.y() - l);
109 QRegion Popup::arrow_region() const
111 return QRegion(arrow_polygon());
114 QRect Popup::bubble_rect() const
117 QPoint((_pos == Right) ? ArrowLength : 0,
118 (_pos == Bottom) ? ArrowLength : 0),
119 QSize(width() - ((_pos == Left || _pos == Right) ?
121 height() - ((_pos == Top || _pos == Bottom) ?
125 QRegion Popup::bubble_region() const
127 const QRect rect(bubble_rect());
129 const unsigned int r = MarginWidth;
130 const unsigned int d = 2 * r;
131 return QRegion(rect.adjusted(r, 0, -r, 0)).united(
132 QRegion(rect.adjusted(0, r, 0, -r))).united(
133 QRegion(rect.left(), rect.top(), d, d,
134 QRegion::Ellipse)).united(
135 QRegion(rect.right() - d, rect.top(), d, d,
136 QRegion::Ellipse)).united(
137 QRegion(rect.left(), rect.bottom() - d, d, d,
138 QRegion::Ellipse)).united(
139 QRegion(rect.right() - d, rect.bottom() - d, d, d,
143 QRegion Popup::popup_region() const
145 return arrow_region().united(bubble_region());
148 void Popup::reposition_widget()
152 if (_pos == Right || _pos == Left)
153 o.ry() = -height() / 2;
155 o.rx() = -width() / 2;
165 void Popup::paintEvent(QPaintEvent*)
167 QPainter painter(this);
168 painter.setRenderHint(QPainter::Antialiasing);
170 const QColor outline_color(QApplication::palette().color(
173 const QRegion b = bubble_region();
174 const QRegion bubble_outline = QRegion(rect()).subtracted(
175 b.translated(1, 0).intersected(b.translated(0, 1).intersected(
176 b.translated(-1, 0).intersected(b.translated(0, -1)))));
178 painter.setPen(Qt::NoPen);
179 painter.setBrush(QApplication::palette().brush(QPalette::Window));
180 painter.drawRect(rect());
182 const QPoint ArrowOffsets[] = {
183 QPoint(1, 0), QPoint(0, -1), QPoint(-1, 0), QPoint(0, 1)};
185 const QRegion a(arrow_region());
186 const QRegion arrow_outline = a.subtracted(
187 a.translated(ArrowOffsets[_pos]));
189 painter.setClipRegion(bubble_outline.subtracted(a).united(
191 painter.setBrush(outline_color);
192 painter.drawRect(rect());
195 void Popup::resizeEvent(QResizeEvent*)
198 setMask(popup_region());
201 void Popup::showEvent(QShowEvent*)
206 void Popup::mouseReleaseEvent(QMouseEvent *e)
210 // We need our own out-of-bounds click handler because QWidget counts
211 // the drop-shadow region as inside the widget
212 if(!bubble_rect().contains(e->pos()))
216 } // namespace widgets