File indexing completed on 2025-01-26 05:06:23

0001 /*
0002     SPDX-FileCopyrightText: 2014 David Edmundson <kde@davidedmundson.co.uk>
0003     SPDX-FileCopyrightText: 2014 Eike Hein <hein@kde.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "subdialog.h"
0009 
0010 #include <QGuiApplication>
0011 #include <QScreen>
0012 
0013 SubDialog::SubDialog(QQuickItem *parent)
0014     : PlasmaQuick::Dialog(parent)
0015 {
0016 }
0017 
0018 SubDialog::~SubDialog()
0019 {
0020 }
0021 
0022 QPoint SubDialog::popupPosition(QQuickItem *item, const QSize &size)
0023 {
0024     if (!item || !item->window()) {
0025         return QPoint(0, 0);
0026     }
0027 
0028     QPointF pos = item->mapToScene(QPointF(0, 0));
0029     pos = item->window()->mapToGlobal(pos.toPoint());
0030 
0031     pos.setX(pos.x() + item->width() / 2);
0032     pos.setY(pos.y() + item->height() / 2);
0033 
0034     if (QGuiApplication::layoutDirection() == Qt::RightToLeft) {
0035         pos.setX(pos.x() - size.width());
0036     }
0037 
0038     QRect avail = availableScreenRectForItem(item);
0039 
0040     if (pos.x() + size.width() > avail.right()) {
0041         pos.setX(pos.x() - size.width());
0042     }
0043 
0044     if (pos.x() < avail.left()) {
0045         pos.setX(pos.x() + size.width());
0046     }
0047 
0048     if (pos.y() + size.height() > avail.bottom()) {
0049         pos.setY(pos.y() - size.height());
0050     }
0051 
0052     return pos.toPoint();
0053 }
0054 
0055 QRect SubDialog::availableScreenRectForItem(QQuickItem *item) const
0056 {
0057     QScreen *screen = QGuiApplication::primaryScreen();
0058 
0059     const QPoint globalPosition = item->window()->mapToGlobal(item->position().toPoint());
0060 
0061     const QList<QScreen *> screens = QGuiApplication::screens();
0062     for (QScreen *s : screens) {
0063         if (s->geometry().contains(globalPosition)) {
0064             screen = s;
0065         }
0066     }
0067 
0068     return screen->availableGeometry();
0069 }