| 1 | /* |
| 2 | * Copyright (C) 2018 Damir Porobic <https://github.com/damirporobic> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #include "WinSnippingArea.h" |
| 21 | #include <QDesktopWidget> |
| 22 | |
| 23 | WinSnippingArea::WinSnippingArea(const QSharedPointer<IConfig> &config) : |
| 24 | AbstractSnippingArea(config), |
| 25 | mIsFullScreenSizeSet(false), |
| 26 | mIsMultipleScaledScreens(false) |
| 27 | { |
| 28 | setWindowFlags(windowFlags() | Qt::Tool); |
| 29 | |
| 30 | connect(qGuiApp, &QGuiApplication::screenRemoved, this, &WinSnippingArea::init); |
| 31 | connect(qGuiApp, &QGuiApplication::screenAdded, this, &WinSnippingArea::init); |
| 32 | |
| 33 | init(); |
| 34 | } |
| 35 | |
| 36 | QRect WinSnippingArea::selectedRectArea() const |
| 37 | { |
| 38 | auto captureArea = getCaptureArea(); |
| 39 | |
| 40 | if(isBackgroundTransparent()) { |
| 41 | auto topLeft = mapToGlobal(captureArea.topLeft()); |
| 42 | auto bottomRight = mapToGlobal(captureArea.bottomRight()); |
| 43 | return {topLeft, bottomRight}; |
| 44 | } else { |
| 45 | return mHdpiScaler.scale(captureArea); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | void WinSnippingArea::setFullScreen() |
| 50 | { |
| 51 | /* |
| 52 | * Workaround for Qt HiDPI issue, setting geometry more then once |
| 53 | * enlarges the widget outside the size of the visible desktop. See #668. |
| 54 | * Qt behaves differently in case of one or multiple scaled screens so |
| 55 | * we utilise here different 'hacks' to fix the different use cases. |
| 56 | * This part just be checked after upgrading to newer Qt version if it |
| 57 | * can be simplified again in case the issue was fixed from Qt side. |
| 58 | * See bug https://bugreports.qt.io/browse/QTBUG-94638 |
| 59 | */ |
| 60 | |
| 61 | if (mIsMultipleScaledScreens) { |
| 62 | setGeometry(QApplication::desktop()->geometry()); |
| 63 | QWidget::show(); |
| 64 | setGeometry(QApplication::desktop()->geometry()); |
| 65 | } else if (!mIsFullScreenSizeSet) { |
| 66 | setGeometry(mFullScreenRect); |
| 67 | mIsFullScreenSizeSet = true; |
| 68 | } |
| 69 | |
| 70 | QWidget::show(); |
| 71 | } |
| 72 | |
| 73 | QSizeF WinSnippingArea::getSize() const |
| 74 | { |
| 75 | if (mIsMultipleScaledScreens) { |
| 76 | return { static_cast<double>(mFullScreenRect.width()) / 2, static_cast<double>(mFullScreenRect.height()) / 2 }; |
| 77 | } else { |
| 78 | return { static_cast<double>(geometry().width()), static_cast<double>(geometry().height()) }; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | QPoint WinSnippingArea::getGlobalCursorPosition() const |
| 83 | { |
| 84 | return mapFromGlobal(AbstractSnippingArea::getGlobalCursorPosition()); |
| 85 | } |
| 86 | |
| 87 | void WinSnippingArea::setupScalingVariables() |
| 88 | { |
| 89 | auto scaledScreens = 0; |
| 90 | auto screens = QApplication::screens(); |
| 91 | for (auto screen : screens) { |
| 92 | auto screenGeometry = screen->geometry(); |
| 93 | |
| 94 | if(screen->devicePixelRatio() > 1) { |
| 95 | scaledScreens++; |
| 96 | } |
| 97 | |
| 98 | if (screenGeometry.x() != 0) { |
| 99 | mScalePosition.setX(screenGeometry.x()); |
| 100 | } |
| 101 | |
| 102 | if (screenGeometry.y() != 0) { |
| 103 | mScalePosition.setY(screenGeometry.y()); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | mScalePosition.setX((mScalePosition.x() - mFullScreenRect.x()) / mHdpiScaler.scaleFactor()); |
| 108 | mScalePosition.setY((mScalePosition.y() - mFullScreenRect.y()) / mHdpiScaler.scaleFactor()); |
| 109 | |
| 110 | mIsMultipleScaledScreens = scaledScreens > 1; |
| 111 | } |
| 112 | |
| 113 | void WinSnippingArea::init() |
| 114 | { |
| 115 | mIsFullScreenSizeSet = false; |
| 116 | |
| 117 | mFullScreenRect = mWinWrapper.getFullScreenRect(); |
| 118 | setupScalingVariables(); |
| 119 | } |
| 120 | |
| 121 | QPointF WinSnippingArea::getPosition() const |
| 122 | { |
| 123 | if (mIsMultipleScaledScreens) { |
| 124 | return mScalePosition; |
| 125 | } else { |
| 126 | return AbstractSnippingArea::getPosition(); |
| 127 | } |
| 128 | } |