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>
38 const unsigned int Popup::ArrowLength = 10;
39 const unsigned int Popup::ArrowOverlap = 3;
40 const unsigned int Popup::MarginWidth = 6;
42 Popup::Popup(QWidget *parent) :
43 QWidget(parent, Qt::Popup | Qt::FramelessWindowHint),
49 const QPoint& Popup::point() const
54 Popup::Position Popup::position() const
59 void Popup::set_position(const QPoint point, Position pos)
61 point_ = point, pos_ = pos;
64 MarginWidth + ((pos == Right) ? ArrowLength : 0),
65 MarginWidth + ((pos == Bottom) ? ArrowLength : 0),
66 MarginWidth + ((pos == Left) ? ArrowLength : 0),
67 MarginWidth + ((pos == Top) ? ArrowLength : 0));
70 bool Popup::eventFilter(QObject *obj, QEvent *evt)
76 if (evt->type() == QEvent::KeyPress) {
77 keyEvent = static_cast<QKeyEvent*>(evt);
78 if (keyEvent->key() == Qt::Key_Enter ||
79 keyEvent->key() == Qt::Key_Return) {
94 // We want to close the popup when the Enter key was
95 // pressed and the first editable widget had focus.
96 if ((le = this->findChild<QLineEdit*>())) {
98 // For combo boxes we need to hook into the parent of
99 // the line edit (i.e. the QComboBox). For edit boxes
100 // we hook into the widget directly.
101 if (le->parent()->metaObject()->className() ==
102 this->metaObject()->className())
103 le->installEventFilter(this);
105 le->parent()->installEventFilter(this);
112 bool Popup::space_for_arrow() const
114 // Check if there is room for the arrow
117 if (point_.x() > x())
122 if (point_.y() > y())
127 if (point_.x() < (x() + width()))
132 if (point_.y() < (y() + height()))
140 QPolygon Popup::arrow_polygon() const
144 const QPoint p = mapFromGlobal(point_);
145 const int l = ArrowLength + ArrowOverlap;
149 poly << QPoint(p.x() + l, p.y() - l);
153 poly << QPoint(p.x() - l, p.y() + l);
158 poly << QPoint(p.x() - l, p.y() - l);
167 poly << QPoint(p.x() + l, p.y() + l);
171 poly << QPoint(p.x() - l, p.y() + l);
175 poly << QPoint(p.x() + l, p.y() - l);
182 QRegion Popup::arrow_region() const
184 return QRegion(arrow_polygon());
187 QRect Popup::bubble_rect() const
190 QPoint((pos_ == Right) ? ArrowLength : 0,
191 (pos_ == Bottom) ? ArrowLength : 0),
192 QSize(width() - ((pos_ == Left || pos_ == Right) ?
194 height() - ((pos_ == Top || pos_ == Bottom) ?
198 QRegion Popup::bubble_region() const
200 const QRect rect(bubble_rect());
202 const unsigned int r = MarginWidth;
203 const unsigned int d = 2 * r;
204 return QRegion(rect.adjusted(r, 0, -r, 0)).united(
205 QRegion(rect.adjusted(0, r, 0, -r))).united(
206 QRegion(rect.left(), rect.top(), d, d,
207 QRegion::Ellipse)).united(
208 QRegion(rect.right() - d, rect.top(), d, d,
209 QRegion::Ellipse)).united(
210 QRegion(rect.left(), rect.bottom() - d, d, d,
211 QRegion::Ellipse)).united(
212 QRegion(rect.right() - d, rect.bottom() - d, d, d,
216 QRegion Popup::popup_region() const
218 if (space_for_arrow())
219 return arrow_region().united(bubble_region());
221 return bubble_region();
224 void Popup::reposition_widget()
228 const QRect screen_rect = QApplication::desktop()->availableGeometry(
229 QApplication::desktop()->screenNumber(point_));
231 if (pos_ == Right || pos_ == Left)
232 o.ry() = -height() / 2;
234 o.rx() = -width() / 2;
238 else if (pos_ == Top)
242 move(max(min(o.x(), screen_rect.right() - width()),
244 max(min(o.y(), screen_rect.bottom() - height()),
248 void Popup::closeEvent(QCloseEvent*)
253 void Popup::paintEvent(QPaintEvent*)
255 QPainter painter(this);
256 painter.setRenderHint(QPainter::Antialiasing);
258 const QColor outline_color(QApplication::palette().color(
262 const QRegion b = bubble_region();
263 const QRegion bubble_outline = QRegion(rect()).subtracted(
264 b.translated(1, 0).intersected(b.translated(0, 1).intersected(
265 b.translated(-1, 0).intersected(b.translated(0, -1)))));
267 painter.setPen(Qt::NoPen);
268 painter.setBrush(QApplication::palette().brush(QPalette::Window));
269 painter.drawRect(rect());
272 if (!space_for_arrow())
275 const QPoint ArrowOffsets[] = {
276 QPoint(1, 0), QPoint(0, -1), QPoint(-1, 0), QPoint(0, 1)};
278 const QRegion a(arrow_region());
279 const QRegion arrow_outline = a.subtracted(
280 a.translated(ArrowOffsets[pos_]));
282 painter.setClipRegion(bubble_outline.subtracted(a).united(
284 painter.setBrush(outline_color);
285 painter.drawRect(rect());
288 void Popup::resizeEvent(QResizeEvent*)
291 setMask(popup_region());
294 void Popup::mouseReleaseEvent(QMouseEvent *e)
298 // We need our own out-of-bounds click handler because QWidget counts
299 // the drop-shadow region as inside the widget
300 if (!bubble_rect().contains(e->pos()))
304 void Popup::showEvent(QShowEvent*)
309 } // namespace widgets