| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/widgets/kernel/qdesktopwidget.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||
|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||
| 2 | ** | - | ||||||
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||
| 4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||
| 5 | ** | - | ||||||
| 6 | ** This file is part of the QtWidgets module of the Qt Toolkit. | - | ||||||
| 7 | ** | - | ||||||
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||
| 9 | ** Commercial License Usage | - | ||||||
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||
| 11 | ** accordance with the commercial license agreement provided with the | - | ||||||
| 12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||
| 16 | ** | - | ||||||
| 17 | ** GNU Lesser General Public License Usage | - | ||||||
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||
| 19 | ** General Public License version 3 as published by the Free Software | - | ||||||
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||
| 21 | ** packaging of this file. Please review the following information to | - | ||||||
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||
| 24 | ** | - | ||||||
| 25 | ** GNU General Public License Usage | - | ||||||
| 26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||
| 27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||
| 28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||
| 29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||
| 31 | ** included in the packaging of this file. Please review the following | - | ||||||
| 32 | ** information to ensure the GNU General Public License requirements will | - | ||||||
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||
| 35 | ** | - | ||||||
| 36 | ** $QT_END_LICENSE$ | - | ||||||
| 37 | ** | - | ||||||
| 38 | ****************************************************************************/ | - | ||||||
| 39 | - | |||||||
| 40 | #include "qglobal.h" | - | ||||||
| 41 | #include "qdesktopwidget.h" | - | ||||||
| 42 | #include "qdesktopwidget_p.h" | - | ||||||
| 43 | #include "qscreen.h" | - | ||||||
| 44 | #include "qwidget_p.h" | - | ||||||
| 45 | #include "qwindow.h" | - | ||||||
| 46 | - | |||||||
| 47 | #include <private/qhighdpiscaling_p.h> | - | ||||||
| 48 | #include <qpa/qplatformscreen.h> | - | ||||||
| 49 | - | |||||||
| 50 | QT_BEGIN_NAMESPACE | - | ||||||
| 51 | - | |||||||
| 52 | QDesktopScreenWidget::QDesktopScreenWidget(QScreen *screen, const QRect &geometry) | - | ||||||
| 53 | : QWidget(Q_NULLPTR, Qt::Desktop), m_screen(screen) | - | ||||||
| 54 | { | - | ||||||
| 55 | setVisible(false); | - | ||||||
| 56 | if (QWindow *winHandle = windowHandle()) | - | ||||||
| 57 | winHandle->setScreen(screen); | - | ||||||
| 58 | setScreenGeometry(geometry); | - | ||||||
| 59 | } | - | ||||||
| 60 | - | |||||||
| 61 | void QDesktopScreenWidget::setScreenGeometry(const QRect &geometry) | - | ||||||
| 62 | { | - | ||||||
| 63 | m_geometry = geometry; | - | ||||||
| 64 | setGeometry(geometry); | - | ||||||
| 65 | } | - | ||||||
| 66 | - | |||||||
| 67 | int QDesktopScreenWidget::screenNumber() const | - | ||||||
| 68 | { | - | ||||||
| 69 | const QDesktopWidgetPrivate *desktopWidgetP | - | ||||||
| 70 | = static_cast<const QDesktopWidgetPrivate *>(qt_widget_private(QApplication::desktop())); | - | ||||||
| 71 | return desktopWidgetP->screens.indexOf(const_cast<QDesktopScreenWidget *>(this)); | - | ||||||
| 72 | } | - | ||||||
| 73 | - | |||||||
| 74 | const QRect QDesktopWidget::screenGeometry(const QWidget *widget) const | - | ||||||
| 75 | { | - | ||||||
| 76 | if (Q_UNLIKELY(!widget))) {
| 0 | ||||||
| 77 | qWarning("QDesktopWidget::screenGeometry(): Attempt " | - | ||||||
| 78 | "to get the screen geometry of a null widget"); | - | ||||||
| 79 | return QRect(); never executed: return QRect(); | 0 | ||||||
| 80 | } | - | ||||||
| 81 | QRect rect = QWidgetPrivate::screenGeometry(widget); | - | ||||||
| 82 | if (rect.isNull())
| 0 | ||||||
| 83 | return screenGeometry(screenNumber(widget)); never executed: return screenGeometry(screenNumber(widget)); | 0 | ||||||
| 84 | else return rect; never executed: return rect; | 0 | ||||||
| 85 | } | - | ||||||
| 86 | - | |||||||
| 87 | const QRect QDesktopWidget::availableGeometry(const QWidget *widget) const | - | ||||||
| 88 | { | - | ||||||
| 89 | if (Q_UNLIKELY(!widget))) {
| 0 | ||||||
| 90 | qWarning("QDesktopWidget::availableGeometry(): Attempt " | - | ||||||
| 91 | "to get the available geometry of a null widget"); | - | ||||||
| 92 | return QRect(); never executed: return QRect(); | 0 | ||||||
| 93 | } | - | ||||||
| 94 | QRect rect = QWidgetPrivate::screenGeometry(widget); | - | ||||||
| 95 | if (rect.isNull())
| 0 | ||||||
| 96 | return availableGeometry(screenNumber(widget)); never executed: return availableGeometry(screenNumber(widget)); | 0 | ||||||
| 97 | else | - | ||||||
| 98 | return rect; never executed: return rect; | 0 | ||||||
| 99 | } | - | ||||||
| 100 | - | |||||||
| 101 | QDesktopScreenWidget *QDesktopWidgetPrivate::widgetForScreen(QScreen *qScreen) const | - | ||||||
| 102 | { | - | ||||||
| 103 | foreach (QDesktopScreenWidget *widget, screens) { | - | ||||||
| 104 | if (widget->screen() == qScreen) | - | ||||||
| 105 | return widget; | - | ||||||
| 106 | } | - | ||||||
| 107 | return Q_NULLPTR; | - | ||||||
| 108 | } | - | ||||||
| 109 | - | |||||||
| 110 | void QDesktopWidgetPrivate::_q_updateScreens() | - | ||||||
| 111 | { | - | ||||||
| 112 | Q_Q(QDesktopWidget); | - | ||||||
| 113 | const QList<QScreen *> screenList = QGuiApplication::screens(); | - | ||||||
| 114 | const int targetLength = screenList.length(); | - | ||||||
| 115 | bool screenCountChanged = false; | - | ||||||
| 116 | - | |||||||
| 117 | // Re-build our screens list. This is the easiest way to later compute which signals to emit. | - | ||||||
| 118 | // Create new screen widgets as necessary. While iterating, keep the old list in place so | - | ||||||
| 119 | // that widgetForScreen works. | - | ||||||
| 120 | // Furthermore, we note which screens have changed, and compute the overall virtual geometry. | - | ||||||
| 121 | QList<QDesktopScreenWidget *> newScreens; | - | ||||||
| 122 | QList<int> changedScreens; | - | ||||||
| 123 | QRegion virtualGeometry; | - | ||||||
| 124 | - | |||||||
| 125 | for (int i = 0; i < targetLength; ++i) { | - | ||||||
| 126 | QScreen *qScreen = screenList.at(i); | - | ||||||
| 127 | const QRect screenGeometry = qScreen->geometry(); | - | ||||||
| 128 | QDesktopScreenWidget *screenWidget = widgetForScreen(qScreen); | - | ||||||
| 129 | if (screenWidget) { | - | ||||||
| 130 | // an old screen. update geometry and remember the index in the *new* list | - | ||||||
| 131 | if (screenGeometry != screenWidget->screenGeometry()) { | - | ||||||
| 132 | screenWidget->setScreenGeometry(screenGeometry); | - | ||||||
| 133 | changedScreens.push_back(i); | - | ||||||
| 134 | } | - | ||||||
| 135 | } else { | - | ||||||
| 136 | // a new screen, create a widget and connect the signals. | - | ||||||
| 137 | screenWidget = new QDesktopScreenWidget(qScreen, screenGeometry); | - | ||||||
| 138 | QObject::connect(qScreen, SIGNAL(geometryChanged(QRect)), | - | ||||||
| 139 | q, SLOT(_q_updateScreens()), Qt::QueuedConnection); | - | ||||||
| 140 | QObject::connect(qScreen, SIGNAL(availableGeometryChanged(QRect)), | - | ||||||
| 141 | q, SLOT(_q_availableGeometryChanged()), Qt::QueuedConnection); | - | ||||||
| 142 | QObject::connect(qScreen, SIGNAL(destroyed()), | - | ||||||
| 143 | q, SLOT(_q_updateScreens()), Qt::QueuedConnection); | - | ||||||
| 144 | screenCountChanged = true; | - | ||||||
| 145 | } | - | ||||||
| 146 | // record all the screens and the overall geometry. | - | ||||||
| 147 | newScreens.push_back(screenWidget); | - | ||||||
| 148 | virtualGeometry += screenGeometry; | - | ||||||
| 149 | } | - | ||||||
| 150 | - | |||||||
| 151 | // Now we apply the accumulated updates. | - | ||||||
| 152 | screens.swap(newScreens); // now [newScreens] is the old screen list | - | ||||||
| 153 | Q_ASSERT(screens.size() == targetLength); | - | ||||||
| 154 | q->setGeometry(virtualGeometry.boundingRect()); | - | ||||||
| 155 | - | |||||||
| 156 | // Delete the QDesktopScreenWidget that are not used any more. | - | ||||||
| 157 | foreach (QDesktopScreenWidget *screen, newScreens) { | - | ||||||
| 158 | if (!screens.contains(screen)) { | - | ||||||
| 159 | delete screen; | - | ||||||
| 160 | screenCountChanged = true; | - | ||||||
| 161 | } | - | ||||||
| 162 | } | - | ||||||
| 163 | - | |||||||
| 164 | // Finally, emit the signals. | - | ||||||
| 165 | if (screenCountChanged) { | - | ||||||
| 166 | // Notice that we trigger screenCountChanged even if a screen was removed and another one added, | - | ||||||
| 167 | // in which case the total number of screens did not change. This is the only way for applications | - | ||||||
| 168 | // to notice that a screen was swapped out against another one. | - | ||||||
| 169 | emit q->screenCountChanged(targetLength); | - | ||||||
| 170 | } | - | ||||||
| 171 | foreach (int changedScreen, changedScreens) | - | ||||||
| 172 | emit q->resized(changedScreen); | - | ||||||
| 173 | } | - | ||||||
| 174 | - | |||||||
| 175 | void QDesktopWidgetPrivate::_q_availableGeometryChanged() | - | ||||||
| 176 | { | - | ||||||
| 177 | Q_Q(QDesktopWidget); | - | ||||||
| 178 | if (QScreen *screen = qobject_cast<QScreen *>(q->sender())) | - | ||||||
| 179 | emit q->workAreaResized(QGuiApplication::screens().indexOf(screen)); | - | ||||||
| 180 | } | - | ||||||
| 181 | - | |||||||
| 182 | QDesktopWidget::QDesktopWidget() | - | ||||||
| 183 | : QWidget(*new QDesktopWidgetPrivate, 0, Qt::Desktop) | - | ||||||
| 184 | { | - | ||||||
| 185 | Q_D(QDesktopWidget); | - | ||||||
| 186 | setObjectName(QLatin1String("desktop")); | - | ||||||
| 187 | d->_q_updateScreens(); | - | ||||||
| 188 | connect(qApp, SIGNAL(screenAdded(QScreen*)), this, SLOT(_q_updateScreens())); | - | ||||||
| 189 | connect(qApp, SIGNAL(primaryScreenChanged(QScreen*)), this, SIGNAL(primaryScreenChanged())); | - | ||||||
| 190 | } | - | ||||||
| 191 | - | |||||||
| 192 | QDesktopWidget::~QDesktopWidget() | - | ||||||
| 193 | { | - | ||||||
| 194 | } | - | ||||||
| 195 | - | |||||||
| 196 | bool QDesktopWidget::isVirtualDesktop() const | - | ||||||
| 197 | { | - | ||||||
| 198 | return QGuiApplication::primaryScreen()->virtualSiblings().size() > 1; | - | ||||||
| 199 | } | - | ||||||
| 200 | - | |||||||
| 201 | int QDesktopWidget::primaryScreen() const | - | ||||||
| 202 | { | - | ||||||
| 203 | return 0; | - | ||||||
| 204 | } | - | ||||||
| 205 | - | |||||||
| 206 | int QDesktopWidget::numScreens() const | - | ||||||
| 207 | { | - | ||||||
| 208 | return qMax(QGuiApplication::screens().size(), 1); | - | ||||||
| 209 | } | - | ||||||
| 210 | - | |||||||
| 211 | QWidget *QDesktopWidget::screen(int screen) | - | ||||||
| 212 | { | - | ||||||
| 213 | Q_D(QDesktopWidget); | - | ||||||
| 214 | if (screen < 0 || screen >= d->screens.length()) | - | ||||||
| 215 | return d->screens.at(0); | - | ||||||
| 216 | return d->screens.at(screen); | - | ||||||
| 217 | } | - | ||||||
| 218 | - | |||||||
| 219 | const QRect QDesktopWidget::availableGeometry(int screenNo) const | - | ||||||
| 220 | { | - | ||||||
| 221 | QList<QScreen *> screens = QGuiApplication::screens(); | - | ||||||
| 222 | if (screenNo == -1) | - | ||||||
| 223 | screenNo = 0; | - | ||||||
| 224 | if (screenNo < 0 || screenNo >= screens.size()) | - | ||||||
| 225 | return QRect(); | - | ||||||
| 226 | else | - | ||||||
| 227 | return screens.at(screenNo)->availableGeometry(); | - | ||||||
| 228 | } | - | ||||||
| 229 | - | |||||||
| 230 | const QRect QDesktopWidget::screenGeometry(int screenNo) const | - | ||||||
| 231 | { | - | ||||||
| 232 | QList<QScreen *> screens = QGuiApplication::screens(); | - | ||||||
| 233 | if (screenNo == -1) | - | ||||||
| 234 | screenNo = 0; | - | ||||||
| 235 | if (screenNo < 0 || screenNo >= screens.size()) | - | ||||||
| 236 | return QRect(); | - | ||||||
| 237 | else | - | ||||||
| 238 | return screens.at(screenNo)->geometry(); | - | ||||||
| 239 | } | - | ||||||
| 240 | - | |||||||
| 241 | int QDesktopWidget::screenNumber(const QWidget *w) const | - | ||||||
| 242 | { | - | ||||||
| 243 | if (!w)
| 0 | ||||||
| 244 | return primaryScreen(); never executed: return primaryScreen(); | 0 | ||||||
| 245 | - | |||||||
| 246 | const QList<QScreen *> allScreens = QGuiApplication::screens(); | - | ||||||
| 247 | QList<QScreen *> screens = allScreens; | - | ||||||
| 248 | if (screens.isEmpty()) // This should never happen
| 0 | ||||||
| 249 | return primaryScreen(); never executed: return primaryScreen(); | 0 | ||||||
| 250 | - | |||||||
| 251 | const QWindow *winHandle = w->windowHandle(); | - | ||||||
| 252 | if (!winHandle) {
| 0 | ||||||
| 253 | if (const QWidget *nativeParent = w->nativeParentWidget())
| 0 | ||||||
| 254 | winHandle = nativeParent->windowHandle(); never executed: winHandle = nativeParent->windowHandle(); | 0 | ||||||
| 255 | } never executed: end of block | 0 | ||||||
| 256 | - | |||||||
| 257 | // If there is more than one virtual desktop | - | ||||||
| 258 | if (screens.count() != screens.constFirst()->virtualSiblings().count()) {
| 0 | ||||||
| 259 | // Find the root widget, get a QScreen from it and use the | - | ||||||
| 260 | // virtual siblings for checking the window position. | - | ||||||
| 261 | if (winHandle) {
| 0 | ||||||
| 262 | if (const QScreen *winScreen = winHandle->screen())
| 0 | ||||||
| 263 | screens = winScreen->virtualSiblings(); never executed: screens = winScreen->virtualSiblings(); | 0 | ||||||
| 264 | } never executed: end of block | 0 | ||||||
| 265 | } never executed: end of block | 0 | ||||||
| 266 | - | |||||||
| 267 | // Get the screen number from window position using screen geometry | - | ||||||
| 268 | // and proper screens. | - | ||||||
| 269 | QRect frame = w->frameGeometry(); | - | ||||||
| 270 | if (!w->isWindow())
| 0 | ||||||
| 271 | frame.moveTopLeft(w->mapToGlobal(QPoint(0, 0))); never executed: frame.moveTopLeft(w->mapToGlobal(QPoint(0, 0))); | 0 | ||||||
| 272 | const QRect nativeFrame = QHighDpi::toNativePixels(frame, winHandle); | - | ||||||
| 273 | - | |||||||
| 274 | QScreen *widgetScreen = Q_NULLPTR; | - | ||||||
| 275 | int largestArea = 0; | - | ||||||
| 276 | foreach (QScreen *screen, screens) { | - | ||||||
| 277 | const QRect deviceIndependentScreenGeometryintersected = QHighDpi::fromNativePixels(screen->handle()->geometry(), screen); | - | ||||||
| const QRect intersected = deviceIndependentScreenGeometry.().intersected(framenativeFrame); | ||||||||
| 278 | int area = intersected.width() * intersected.height(); | - | ||||||
| 279 | if (largestArea < area) {
| 0 | ||||||
| 280 | widgetScreen = screen; | - | ||||||
| 281 | largestArea = area; | - | ||||||
| 282 | } never executed: end of block | 0 | ||||||
| 283 | } never executed: end of block | 0 | ||||||
| 284 | return allScreens.indexOf(widgetScreen); never executed: return allScreens.indexOf(widgetScreen); | 0 | ||||||
| 285 | } | - | ||||||
| 286 | - | |||||||
| 287 | int QDesktopWidget::screenNumber(const QPoint &p) const | - | ||||||
| 288 | { | - | ||||||
| 289 | const QList<QScreen *> screens = QGuiApplication::screens(); | - | ||||||
| 290 | if (!screens.isEmpty()) { | - | ||||||
| 291 | const QList<QScreen *> primaryScreens = screens.first()->virtualSiblings(); | - | ||||||
| 292 | // Find the screen index on the primary virtual desktop first | - | ||||||
| 293 | foreach (QScreen *screen, primaryScreens) { | - | ||||||
| 294 | if (screen->geometry().contains(p)) | - | ||||||
| 295 | return screens.indexOf(screen); | - | ||||||
| 296 | } | - | ||||||
| 297 | // If the screen index is not found on primary virtual desktop, find | - | ||||||
| 298 | // the screen index on all screens except the first which was for | - | ||||||
| 299 | // sure in the previous loop. Some other screens may repeat. Find | - | ||||||
| 300 | // only when there is more than one virtual desktop. | - | ||||||
| 301 | if (screens.count() != primaryScreens.count()) { | - | ||||||
| 302 | for (int i = 1; i < screens.size(); ++i) { | - | ||||||
| 303 | if (screens[i]->geometry().contains(p)) | - | ||||||
| 304 | return i; | - | ||||||
| 305 | } | - | ||||||
| 306 | } | - | ||||||
| 307 | } | - | ||||||
| 308 | return primaryScreen(); //even better would be closest screen | - | ||||||
| 309 | } | - | ||||||
| 310 | - | |||||||
| 311 | void QDesktopWidget::resizeEvent(QResizeEvent *) | - | ||||||
| 312 | { | - | ||||||
| 313 | } | - | ||||||
| 314 | - | |||||||
| 315 | QT_END_NAMESPACE | - | ||||||
| 316 | - | |||||||
| 317 | #include "moc_qdesktopwidget.cpp" | - | ||||||
| 318 | #include "moc_qdesktopwidget_p.cpp" | - | ||||||
| Source code | Switch to Preprocessed file |