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, see <http://www.gnu.org/licenses/>.
25 #include <QApplication>
26 #include <QDesktopWidget>
37 const unsigned int Popup::ArrowLength = 10;
38 const unsigned int Popup::ArrowOverlap = 3;
39 const unsigned int Popup::MarginWidth = 6;
41 Popup::Popup(QWidget *parent) :
42 QWidget(parent, Qt::Popup | Qt::FramelessWindowHint),
48 const QPoint& Popup::point() const
53 Popup::Position Popup::position() const
58 void Popup::set_position(const QPoint point, Position pos)
60 point_ = point, pos_ = pos;
63 MarginWidth + ((pos == Right) ? ArrowLength : 0),
64 MarginWidth + ((pos == Bottom) ? ArrowLength : 0),
65 MarginWidth + ((pos == Left) ? ArrowLength : 0),
66 MarginWidth + ((pos == Top) ? ArrowLength : 0));
69 bool Popup::eventFilter(QObject *obj, QEvent *event)
75 if (event->type() == QEvent::KeyPress) {
76 keyEvent = static_cast<QKeyEvent*>(event);
77 if (keyEvent->key() == Qt::Key_Enter ||
78 keyEvent->key() == Qt::Key_Return) {
93 // We want to close the popup when the Enter key was
94 // pressed and the first editable widget had focus.
95 if ((le = this->findChild<QLineEdit*>())) {
97 // For combo boxes we need to hook into the parent of
98 // the line edit (i.e. the QComboBox). For edit boxes
99 // we hook into the widget directly.
100 if (le->parent()->metaObject()->className() ==
101 this->metaObject()->className())
102 le->installEventFilter(this);
104 le->parent()->installEventFilter(this);
111 bool Popup::space_for_arrow() const
113 // Check if there is room for the arrow
116 if (point_.x() > x())
121 if (point_.y() > y())
126 if (point_.x() < (x() + width()))
131 if (point_.y() < (y() + height()))
139 QPolygon Popup::arrow_polygon() const
143 const QPoint p = mapFromGlobal(point_);
144 const int l = ArrowLength + ArrowOverlap;
148 poly << QPoint(p.x() + l, p.y() - l);
152 poly << QPoint(p.x() - l, p.y() + l);
157 poly << QPoint(p.x() - l, p.y() - l);
166 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);
181 QRegion Popup::arrow_region() const
183 return QRegion(arrow_polygon());
186 QRect Popup::bubble_rect() const
189 QPoint((pos_ == Right) ? ArrowLength : 0,
190 (pos_ == Bottom) ? ArrowLength : 0),
191 QSize(width() - ((pos_ == Left || pos_ == Right) ?
193 height() - ((pos_ == Top || pos_ == Bottom) ?
197 QRegion Popup::bubble_region() const
199 const QRect rect(bubble_rect());
201 const unsigned int r = MarginWidth;
202 const unsigned int d = 2 * r;
203 return QRegion(rect.adjusted(r, 0, -r, 0)).united(
204 QRegion(rect.adjusted(0, r, 0, -r))).united(
205 QRegion(rect.left(), rect.top(), d, d,
206 QRegion::Ellipse)).united(
207 QRegion(rect.right() - d, rect.top(), d, d,
208 QRegion::Ellipse)).united(
209 QRegion(rect.left(), rect.bottom() - d, d, d,
210 QRegion::Ellipse)).united(
211 QRegion(rect.right() - d, rect.bottom() - d, d, d,
215 QRegion Popup::popup_region() const
217 if (space_for_arrow())
218 return arrow_region().united(bubble_region());
220 return bubble_region();
223 void Popup::reposition_widget()
227 const QRect screen_rect = QApplication::desktop()->availableGeometry(
228 QApplication::desktop()->screenNumber(point_));
230 if (pos_ == Right || pos_ == Left)
231 o.ry() = -height() / 2;
233 o.rx() = -width() / 2;
237 else if (pos_ == Top)
241 move(max(min(o.x(), screen_rect.right() - width()),
243 max(min(o.y(), screen_rect.bottom() - height()),
247 void Popup::closeEvent(QCloseEvent*)
252 void Popup::paintEvent(QPaintEvent*)
254 QPainter painter(this);
255 painter.setRenderHint(QPainter::Antialiasing);
257 const QColor outline_color(QApplication::palette().color(
261 const QRegion b = bubble_region();
262 const QRegion bubble_outline = QRegion(rect()).subtracted(
263 b.translated(1, 0).intersected(b.translated(0, 1).intersected(
264 b.translated(-1, 0).intersected(b.translated(0, -1)))));
266 painter.setPen(Qt::NoPen);
267 painter.setBrush(QApplication::palette().brush(QPalette::Window));
268 painter.drawRect(rect());
271 if (!space_for_arrow())
274 const QPoint ArrowOffsets[] = {
275 QPoint(1, 0), QPoint(0, -1), QPoint(-1, 0), QPoint(0, 1)};
277 const QRegion a(arrow_region());
278 const QRegion arrow_outline = a.subtracted(
279 a.translated(ArrowOffsets[pos_]));
281 painter.setClipRegion(bubble_outline.subtracted(a).united(
283 painter.setBrush(outline_color);
284 painter.drawRect(rect());
287 void Popup::resizeEvent(QResizeEvent*)
290 setMask(popup_region());
293 void Popup::mouseReleaseEvent(QMouseEvent *event)
297 // We need our own out-of-bounds click handler because QWidget counts
298 // the drop-shadow region as inside the widget
299 if (!bubble_rect().contains(event->pos()))
303 void Popup::showEvent(QShowEvent*)
308 } // namespace widgets