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));
71 bool Popup::eventFilter(QObject *obj, QEvent *evt)
77 if (evt->type() == QEvent::KeyPress) {
78 keyEvent = static_cast<QKeyEvent*>(evt);
79 if (keyEvent->key() == Qt::Key_Enter ||
80 keyEvent->key() == Qt::Key_Return) {
95 // We want to close the popup when the Enter key was
96 // pressed and the first editable widget had focus.
97 if ((le = this->findChild<QLineEdit*>())) {
99 // For combo boxes we need to hook into the parent of
100 // the line edit (i.e. the QComboBox). For edit boxes
101 // we hook into the widget directly.
102 if (le->parent()->metaObject()->className() ==
103 this->metaObject()->className())
104 le->installEventFilter(this);
106 le->parent()->installEventFilter(this);
113 bool Popup::space_for_arrow() const
115 // Check if there is room for the arrow
118 if (point_.x() > x())
123 if (point_.y() > y())
128 if (point_.x() < (x() + width()))
133 if (point_.y() < (y() + height()))
141 QPolygon Popup::arrow_polygon() const
145 const QPoint p = mapFromGlobal(point_);
146 const int l = ArrowLength + ArrowOverlap;
151 poly << QPoint(p.x() + l, p.y() - l);
155 poly << QPoint(p.x() - l, p.y() + l);
160 poly << QPoint(p.x() - l, p.y() - l);
170 poly << QPoint(p.x() + l, p.y() + l);
174 poly << QPoint(p.x() - l, p.y() + l);
178 poly << QPoint(p.x() + l, p.y() - l);
185 QRegion Popup::arrow_region() const
187 return QRegion(arrow_polygon());
190 QRect Popup::bubble_rect() const
193 QPoint((pos_ == Right) ? ArrowLength : 0,
194 (pos_ == Bottom) ? ArrowLength : 0),
195 QSize(width() - ((pos_ == Left || pos_ == Right) ?
197 height() - ((pos_ == Top || pos_ == Bottom) ?
201 QRegion Popup::bubble_region() const
203 const QRect rect(bubble_rect());
205 const unsigned int r = MarginWidth;
206 const unsigned int d = 2 * r;
207 return QRegion(rect.adjusted(r, 0, -r, 0)).united(
208 QRegion(rect.adjusted(0, r, 0, -r))).united(
209 QRegion(rect.left(), rect.top(), d, d,
210 QRegion::Ellipse)).united(
211 QRegion(rect.right() - d, rect.top(), d, d,
212 QRegion::Ellipse)).united(
213 QRegion(rect.left(), rect.bottom() - d, d, d,
214 QRegion::Ellipse)).united(
215 QRegion(rect.right() - d, rect.bottom() - d, d, d,
219 QRegion Popup::popup_region() const
221 if (space_for_arrow())
222 return arrow_region().united(bubble_region());
224 return bubble_region();
227 void Popup::reposition_widget()
231 const QRect screen_rect = QApplication::desktop()->availableGeometry(
232 QApplication::desktop()->screenNumber(point_));
234 if (pos_ == Right || pos_ == Left)
235 o.ry() = -height() / 2;
237 o.rx() = -width() / 2;
245 move(max(min(o.x(), screen_rect.right() - width()),
247 max(min(o.y(), screen_rect.bottom() - height()),
251 void Popup::closeEvent(QCloseEvent*)
256 void Popup::paintEvent(QPaintEvent*)
258 QPainter painter(this);
259 painter.setRenderHint(QPainter::Antialiasing);
261 const QColor outline_color(QApplication::palette().color(
265 const QRegion b = bubble_region();
266 const QRegion bubble_outline = QRegion(rect()).subtracted(
267 b.translated(1, 0).intersected(b.translated(0, 1).intersected(
268 b.translated(-1, 0).intersected(b.translated(0, -1)))));
270 painter.setPen(Qt::NoPen);
271 painter.setBrush(QApplication::palette().brush(QPalette::Window));
272 painter.drawRect(rect());
275 if (!space_for_arrow())
278 const QPoint ArrowOffsets[] = {
279 QPoint(1, 0), QPoint(0, -1), QPoint(-1, 0), QPoint(0, 1)};
281 const QRegion a(arrow_region());
282 const QRegion arrow_outline = a.subtracted(
283 a.translated(ArrowOffsets[pos_]));
285 painter.setClipRegion(bubble_outline.subtracted(a).united(
287 painter.setBrush(outline_color);
288 painter.drawRect(rect());
291 void Popup::resizeEvent(QResizeEvent*)
294 setMask(popup_region());
297 void Popup::mouseReleaseEvent(QMouseEvent *e)
301 // We need our own out-of-bounds click handler because QWidget counts
302 // the drop-shadow region as inside the widget
303 if(!bubble_rect().contains(e->pos()))
307 void Popup::showEvent(QShowEvent*)
312 } // namespace widgets