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/>.
23 #include <QApplication>
24 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
27 #include <QDesktopWidget>
42 const unsigned int Popup::ArrowLength = 10;
43 const unsigned int Popup::ArrowOverlap = 3;
44 const unsigned int Popup::MarginWidth = 6;
47 QWidthAdjustingScrollArea::QWidthAdjustingScrollArea(QWidget* parent) :
52 void QWidthAdjustingScrollArea::setWidget(QWidget* w)
54 QScrollArea::setWidget(w);
55 // It happens that QScrollArea already filters widget events,
56 // but that's an implementation detail that we shouldn't rely on.
57 w->installEventFilter(this);
60 bool QWidthAdjustingScrollArea::eventFilter(QObject* obj, QEvent* ev)
62 if (obj == widget() && ev->type() == QEvent::Resize) {
63 if (widget()->height() > height())
64 setMinimumWidth(widget()->width() + qApp->style()->pixelMetric(QStyle::PM_ScrollBarExtent));
66 setMinimumWidth(widget()->width());
69 return QScrollArea::eventFilter(obj, ev);
73 Popup::Popup(QWidget *parent) :
74 QWidget(parent, Qt::Popup | Qt::FramelessWindowHint),
80 const QPoint& Popup::point() const
85 Popup::Position Popup::position() const
90 void Popup::set_position(const QPoint point, Position pos)
92 point_ = point, pos_ = pos;
95 MarginWidth + ((pos == Right) ? ArrowLength : 0),
96 MarginWidth + ((pos == Bottom) ? ArrowLength : 0),
97 MarginWidth + ((pos == Left) ? ArrowLength : 0),
98 MarginWidth + ((pos == Top) ? ArrowLength : 0));
101 bool Popup::eventFilter(QObject *obj, QEvent *event)
107 if (event->type() == QEvent::KeyPress) {
108 keyEvent = static_cast<QKeyEvent*>(event);
109 if (keyEvent->key() == Qt::Key_Enter ||
110 keyEvent->key() == Qt::Key_Return) {
125 // We want to close the popup when the Enter key was
126 // pressed and the first editable widget had focus.
127 if ((le = this->findChild<QLineEdit*>())) {
129 // For combo boxes we need to hook into the parent of
130 // the line edit (i.e. the QComboBox). For edit boxes
131 // we hook into the widget directly.
132 if (le->parent()->metaObject()->className() ==
133 this->metaObject()->className())
134 le->installEventFilter(this);
136 le->parent()->installEventFilter(this);
143 bool Popup::space_for_arrow() const
145 // Check if there is room for the arrow
148 if (point_.x() > x())
153 if (point_.y() > y())
158 if (point_.x() < (x() + width()))
163 if (point_.y() < (y() + height()))
171 QPolygon Popup::arrow_polygon() const
175 const QPoint p = mapFromGlobal(point_);
176 const int l = ArrowLength + ArrowOverlap;
180 poly << QPoint(p.x() + l, p.y() - l);
184 poly << QPoint(p.x() - l, p.y() + l);
189 poly << QPoint(p.x() - l, p.y() - l);
198 poly << QPoint(p.x() + l, p.y() + l);
202 poly << QPoint(p.x() - l, p.y() + l);
206 poly << QPoint(p.x() + l, p.y() - l);
213 QRegion Popup::arrow_region() const
215 return QRegion(arrow_polygon());
218 QRect Popup::bubble_rect() const
221 QPoint((pos_ == Right) ? ArrowLength : 0,
222 (pos_ == Bottom) ? ArrowLength : 0),
223 QSize(width() - ((pos_ == Left || pos_ == Right) ?
225 height() - ((pos_ == Top || pos_ == Bottom) ?
229 QRegion Popup::bubble_region() const
231 const QRect rect(bubble_rect());
233 const unsigned int r = MarginWidth;
234 const unsigned int d = 2 * r;
235 return QRegion(rect.adjusted(r, 0, -r, 0)).united(
236 QRegion(rect.adjusted(0, r, 0, -r))).united(
237 QRegion(rect.left(), rect.top(), d, d,
238 QRegion::Ellipse)).united(
239 QRegion(rect.right() - d, rect.top(), d, d,
240 QRegion::Ellipse)).united(
241 QRegion(rect.left(), rect.bottom() - d, d, d,
242 QRegion::Ellipse)).united(
243 QRegion(rect.right() - d, rect.bottom() - d, d, d,
247 QRegion Popup::popup_region() const
249 if (space_for_arrow())
250 return arrow_region().united(bubble_region());
252 return bubble_region();
255 void Popup::reposition_widget()
259 #if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
260 const QRect screen_rect = QApplication::screenAt(point_)->availableGeometry();
262 const QRect screen_rect = QApplication::desktop()->availableGeometry(
263 QApplication::desktop()->screenNumber(point_));
266 if (pos_ == Right || pos_ == Left)
267 o.ry() = -height() / 2;
269 o.rx() = -width() / 2;
273 else if (pos_ == Top)
277 move(max(min(o.x(), screen_rect.right() - width()),
279 max(min(o.y(), screen_rect.bottom() - height()),
283 void Popup::closeEvent(QCloseEvent*)
288 void Popup::paintEvent(QPaintEvent*)
290 QPainter painter(this);
291 painter.setRenderHint(QPainter::Antialiasing);
293 const QColor outline_color(QApplication::palette().color(
297 const QRegion b = bubble_region();
298 const QRegion bubble_outline = QRegion(rect()).subtracted(
299 b.translated(1, 0).intersected(b.translated(0, 1).intersected(
300 b.translated(-1, 0).intersected(b.translated(0, -1)))));
302 painter.setPen(Qt::NoPen);
303 painter.setBrush(QApplication::palette().brush(QPalette::Window));
304 painter.drawRect(rect());
307 if (!space_for_arrow())
310 const QPoint ArrowOffsets[] = {
311 QPoint(1, 0), QPoint(0, -1), QPoint(-1, 0), QPoint(0, 1)};
313 const QRegion a(arrow_region());
314 const QRegion arrow_outline = a.subtracted(
315 a.translated(ArrowOffsets[pos_]));
317 painter.setClipRegion(bubble_outline.subtracted(a).united(
319 painter.setBrush(outline_color);
320 painter.drawRect(rect());
323 void Popup::resizeEvent(QResizeEvent*)
326 setMask(popup_region());
329 void Popup::mouseReleaseEvent(QMouseEvent *event)
333 // We need our own out-of-bounds click handler because QWidget counts
334 // the drop-shadow region as inside the widget
335 if (!bubble_rect().contains(event->pos()))
339 void Popup::showEvent(QShowEvent*)
344 } // namespace widgets