diff options
author | Błażej Szczygieł <spaz16@wp.pl> | 2015-12-31 21:47:41 +0100 |
---|---|---|
committer | Błażej Szczygieł <spaz16@wp.pl> | 2016-01-06 16:02:13 +0000 |
commit | 1606a0e508b8ecdcbdff953ef55136c8ff59ba79 (patch) | |
tree | d15cf1d5d9cc354d2a761196c16196231b31989c /src/widgets/kernel/qdesktopwidget.cpp | |
parent | f3114120f2d6f81f424ee542635c2711f66b516b (diff) |
QDesktopWidget::screenNumber(QWidget*): check virtual sibling screens
Find the root widget only when more than one virtual desktop exists
and find the screen index using virtual siblings from this root
widget.
Use intersecting rects instead of middle point to obtain the screen.
This can help to get the screen index when the middle point is outside
the screen geometry, but part of the window is still on the screen.
If the widget is completely outside the screen geometry, -1 is
returned.
This commit amends: a6b2a4642f07cd6e52b447e1e441b257990a8d03
Change-Id: I80247fc1956a82c487ee6f728d1576bf48b28748
Reviewed-by: Shawn Rutledge <shawn.rutledge@theqtcompany.com>
Diffstat (limited to 'src/widgets/kernel/qdesktopwidget.cpp')
-rw-r--r-- | src/widgets/kernel/qdesktopwidget.cpp | 52 |
1 files changed, 35 insertions, 17 deletions
diff --git a/src/widgets/kernel/qdesktopwidget.cpp b/src/widgets/kernel/qdesktopwidget.cpp index d21e60198e..e586be206e 100644 --- a/src/widgets/kernel/qdesktopwidget.cpp +++ b/src/widgets/kernel/qdesktopwidget.cpp @@ -195,28 +195,46 @@ const QRect QDesktopWidget::screenGeometry(int screenNo) const int QDesktopWidget::screenNumber(const QWidget *w) const { if (!w) - return 0; - - // Find the root widget, get a QScreen pointer from it and find - // the screen number. - const QWidget *root = w; - const QWidget *tmp = w; - while ((tmp = tmp->parentWidget())) - root = tmp; - QWindow *winHandle = root->windowHandle(); - if (winHandle) { - int screenIdx = QGuiApplication::screens().indexOf(winHandle->screen()); - if (screenIdx > -1) - return screenIdx; + return primaryScreen(); + + const QList<QScreen *> allScreens = QGuiApplication::screens(); + QList<QScreen *> screens = allScreens; + if (screens.isEmpty()) // This should never happen + return primaryScreen(); + + // If there is more than one virtual desktop + if (screens.count() != screens.first()->virtualSiblings().count()) { + // Find the root widget, get a QScreen from it and use the + // virtual siblings for checking the window position. + const QWidget *root = w; + const QWidget *tmp = w; + while ((tmp = tmp->parentWidget())) + root = tmp; + const QWindow *winHandle = root->windowHandle(); + if (winHandle) { + const QScreen *winScreen = winHandle->screen(); + if (winScreen) + screens = winScreen->virtualSiblings(); + } } - // If the screen number cannot be obtained using QScreen pointer, - // get it from window position using screen geometry. + // Get the screen number from window position using screen geometry + // and proper screens. QRect frame = w->frameGeometry(); if (!w->isWindow()) frame.moveTopLeft(w->mapToGlobal(QPoint(0, 0))); - const QPoint midpoint = (frame.topLeft() + frame.bottomRight()) / 2; - return screenNumber(midpoint); + + QScreen *widgetScreen = Q_NULLPTR; + int largestArea = 0; + foreach (QScreen *screen, screens) { + QRect intersected = screen->geometry().intersected(frame); + int area = intersected.width() * intersected.height(); + if (largestArea < area) { + widgetScreen = screen; + largestArea = area; + } + } + return allScreens.indexOf(widgetScreen); } int QDesktopWidget::screenNumber(const QPoint &p) const |