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 #include <QDesktopWidget>
38 const unsigned int Popup::ArrowLength = 10;
39 const unsigned int Popup::ArrowOverlap = 3;
40 const unsigned int Popup::MarginWidth = 6;
43 QWidthAdjustingScrollArea::QWidthAdjustingScrollArea(QWidget* parent) :
48 void QWidthAdjustingScrollArea::setWidget(QWidget* w)
50 QScrollArea::setWidget(w);
51 // It happens that QScrollArea already filters widget events,
52 // but that's an implementation detail that we shouldn't rely on.
53 w->installEventFilter(this);
56 bool QWidthAdjustingScrollArea::eventFilter(QObject* obj, QEvent* ev)
58 if (obj == widget() && ev->type() == QEvent::Resize) {
59 if (widget()->height() > height())
60 setMinimumWidth(widget()->width() + qApp->style()->pixelMetric(QStyle::PM_ScrollBarExtent));
62 setMinimumWidth(widget()->width());
65 return QScrollArea::eventFilter(obj, ev);
69 Popup::Popup(QWidget *parent) :
70 QWidget(parent, Qt::Popup | Qt::FramelessWindowHint),
76 const QPoint& Popup::point() const
81 Popup::Position Popup::position() const
86 void Popup::set_position(const QPoint point, Position pos)
88 point_ = point, pos_ = pos;
91 MarginWidth + ((pos == Right) ? ArrowLength : 0),
92 MarginWidth + ((pos == Bottom) ? ArrowLength : 0),
93 MarginWidth + ((pos == Left) ? ArrowLength : 0),
94 MarginWidth + ((pos == Top) ? ArrowLength : 0));
97 bool Popup::eventFilter(QObject *obj, QEvent *event)
103 if (event->type() == QEvent::KeyPress) {
104 keyEvent = static_cast<QKeyEvent*>(event);
105 if (keyEvent->key() == Qt::Key_Enter ||
106 keyEvent->key() == Qt::Key_Return) {
121 // We want to close the popup when the Enter key was
122 // pressed and the first editable widget had focus.
123 if ((le = this->findChild<QLineEdit*>())) {
125 // For combo boxes we need to hook into the parent of
126 // the line edit (i.e. the QComboBox). For edit boxes
127 // we hook into the widget directly.
128 if (le->parent()->metaObject()->className() ==
129 this->metaObject()->className())
130 le->installEventFilter(this);
132 le->parent()->installEventFilter(this);
139 bool Popup::space_for_arrow() const
141 // Check if there is room for the arrow
144 if (point_.x() > x())
149 if (point_.y() > y())
154 if (point_.x() < (x() + width()))
159 if (point_.y() < (y() + height()))
167 QPolygon Popup::arrow_polygon() const
171 const QPoint p = mapFromGlobal(point_);
172 const int l = ArrowLength + ArrowOverlap;
176 poly << QPoint(p.x() + l, p.y() - l);
180 poly << QPoint(p.x() - l, p.y() + l);
185 poly << QPoint(p.x() - l, p.y() - l);
194 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);
209 QRegion Popup::arrow_region() const
211 return QRegion(arrow_polygon());
214 QRect Popup::bubble_rect() const
217 QPoint((pos_ == Right) ? ArrowLength : 0,
218 (pos_ == Bottom) ? ArrowLength : 0),
219 QSize(width() - ((pos_ == Left || pos_ == Right) ?
221 height() - ((pos_ == Top || pos_ == Bottom) ?
225 QRegion Popup::bubble_region() const
227 const QRect rect(bubble_rect());
229 const unsigned int r = MarginWidth;
230 const unsigned int d = 2 * r;
231 return QRegion(rect.adjusted(r, 0, -r, 0)).united(
232 QRegion(rect.adjusted(0, r, 0, -r))).united(
233 QRegion(rect.left(), rect.top(), d, d,
234 QRegion::Ellipse)).united(
235 QRegion(rect.right() - d, rect.top(), d, d,
236 QRegion::Ellipse)).united(
237 QRegion(rect.left(), rect.bottom() - d, d, d,
238 QRegion::Ellipse)).united(
239 QRegion(rect.right() - d, rect.bottom() - d, d, d,
243 QRegion Popup::popup_region() const
245 if (space_for_arrow())
246 return arrow_region().united(bubble_region());
248 return bubble_region();
251 void Popup::reposition_widget()
255 #if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
256 const QRect screen_rect = QApplication::screenAt(point_)->availableGeometry();
258 const QRect screen_rect = QApplication::desktop()->availableGeometry(
259 QApplication::desktop()->screenNumber(point_));
262 if (pos_ == Right || pos_ == Left)
263 o.ry() = -height() / 2;
265 o.rx() = -width() / 2;
269 else if (pos_ == Top)
273 move(max(min(o.x(), screen_rect.right() - width()),
275 max(min(o.y(), screen_rect.bottom() - height()),
279 void Popup::closeEvent(QCloseEvent*)
284 void Popup::paintEvent(QPaintEvent*)
286 QPainter painter(this);
287 painter.setRenderHint(QPainter::Antialiasing);
289 const QColor outline_color(QApplication::palette().color(
293 const QRegion b = bubble_region();
294 const QRegion bubble_outline = QRegion(rect()).subtracted(
295 b.translated(1, 0).intersected(b.translated(0, 1).intersected(
296 b.translated(-1, 0).intersected(b.translated(0, -1)))));
298 painter.setPen(Qt::NoPen);
299 painter.setBrush(QApplication::palette().brush(QPalette::Window));
300 painter.drawRect(rect());
303 if (!space_for_arrow())
306 const QPoint ArrowOffsets[] = {
307 QPoint(1, 0), QPoint(0, -1), QPoint(-1, 0), QPoint(0, 1)};
309 const QRegion a(arrow_region());
310 const QRegion arrow_outline = a.subtracted(
311 a.translated(ArrowOffsets[pos_]));
313 painter.setClipRegion(bubble_outline.subtracted(a).united(
315 painter.setBrush(outline_color);
316 painter.drawRect(rect());
319 void Popup::resizeEvent(QResizeEvent*)
322 setMask(popup_region());
325 void Popup::mouseReleaseEvent(QMouseEvent *event)
329 // We need our own out-of-bounds click handler because QWidget counts
330 // the drop-shadow region as inside the widget
331 if (!bubble_rect().contains(event->pos()))
335 void Popup::showEvent(QShowEvent*)
340 } // namespace widgets