| 1 | /* |
| 2 | * Copyright (C) 2017 Damir Porobic <[email protected]> |
| 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 "AbstractRectAreaImageGrabber.h" |
| 21 | #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) |
| 22 | #include <QDesktopWidget> |
| 23 | #endif |
| 24 | |
| 25 | AbstractRectAreaImageGrabber::AbstractRectAreaImageGrabber(AbstractSnippingArea *snippingArea, const QSharedPointer<IConfig> &config) : |
| 26 | AbstractImageGrabber(config), |
| 27 | mSnippingArea(snippingArea), |
| 28 | mFreezeImageWhileSnipping(mConfig->freezeImageWhileSnippingEnabled()) |
| 29 | { |
| 30 | Q_ASSERT(mSnippingArea != nullptr); |
| 31 | |
| 32 | connectSnippingAreaCancel(); |
| 33 | } |
| 34 | |
| 35 | AbstractRectAreaImageGrabber::~AbstractRectAreaImageGrabber() |
| 36 | { |
| 37 | delete mSnippingArea; |
| 38 | } |
| 39 | |
| 40 | void AbstractRectAreaImageGrabber::grabImage(CaptureModes captureMode, bool captureCursor, int delay) |
| 41 | { |
| 42 | setIsCaptureCursorEnabled(captureCursor); |
| 43 | setCaptureDelay(delay); |
| 44 | setCaptureMode(captureMode); |
| 45 | |
| 46 | if (isRectAreaCaptureWithoutBackground()) { |
| 47 | openSnippingAreaWithoutBackground(); |
| 48 | } else { |
| 49 | QTimer::singleShot(captureDelay(), this, &AbstractRectAreaImageGrabber::prepareGrab); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /* |
| 54 | * Returns the rect of the screen where the mouse cursor is currently located |
| 55 | */ |
| 56 | QRect AbstractRectAreaImageGrabber::currentScreenRect() const |
| 57 | { |
| 58 | auto screen = QGuiApplication::screenAt(QCursor::pos()); |
| 59 | if (screen == nullptr) { |
| 60 | screen = QGuiApplication::primaryScreen(); |
| 61 | } |
| 62 | return screen->geometry(); |
| 63 | } |
| 64 | |
| 65 | QRect AbstractRectAreaImageGrabber::lastRectArea() const |
| 66 | { |
| 67 | auto rectArea = mConfig->lastRectArea(); |
| 68 | if(rectArea.isNull()) { |
| 69 | qWarning("ImageGrabber: No RectArea found, capturing full screen."); |
| 70 | return fullScreenRect(); |
| 71 | } |
| 72 | return rectArea; |
| 73 | } |
| 74 | |
| 75 | void AbstractRectAreaImageGrabber::openSnippingAreaWithoutBackground() |
| 76 | { |
| 77 | connectSnippingAreaFinish(); |
| 78 | mSnippingArea->showWithoutBackground(); |
| 79 | } |
| 80 | |
| 81 | void AbstractRectAreaImageGrabber::openSnippingAreaWithBackground(const QPixmap& background) |
| 82 | { |
| 83 | connectSnippingAreaFinish(); |
| 84 | mSnippingArea->showWithBackground(background); |
| 85 | } |
| 86 | |
| 87 | QRect AbstractRectAreaImageGrabber::selectedSnippingAreaRect() const |
| 88 | { |
| 89 | return mSnippingArea->selectedRectArea(); |
| 90 | } |
| 91 | |
| 92 | QPixmap AbstractRectAreaImageGrabber::snippingAreaBackground() const |
| 93 | { |
| 94 | return mSnippingArea->background(); |
| 95 | } |
| 96 | |
| 97 | QPixmap AbstractRectAreaImageGrabber::getScreenshotFromRect(const QRect &rect) const |
| 98 | { |
| 99 | auto screen = QGuiApplication::primaryScreen(); |
| 100 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 101 | auto windowId = 0; |
| 102 | #else |
| 103 | auto windowId = QApplication::desktop()->winId(); |
| 104 | #endif |
| 105 | auto rectPosition = rect.topLeft(); |
| 106 | return screen->grabWindow(windowId, rectPosition.x(), rectPosition.y(), rect.width(), rect.height()); |
| 107 | } |
| 108 | |
| 109 | QPixmap AbstractRectAreaImageGrabber::getScreenshot() const |
| 110 | { |
| 111 | if (isRectAreaCaptureWithBackground()) { |
| 112 | return getScreenshotFromBackground(); |
| 113 | } else { |
| 114 | return getScreenshotFromRect(mCaptureRect); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | QPixmap AbstractRectAreaImageGrabber::getScreenshotFromBackground() const |
| 119 | { |
| 120 | return snippingAreaBackground().copy(mCaptureRect); |
| 121 | } |
| 122 | |
| 123 | void AbstractRectAreaImageGrabber::setCaptureRectFromCorrectSource() |
| 124 | { |
| 125 | switch (captureMode()) { |
| 126 | case CaptureModes::RectArea: |
| 127 | mCaptureRect = selectedSnippingAreaRect(); |
| 128 | break; |
| 129 | case CaptureModes::LastRectArea: |
| 130 | mCaptureRect = lastRectArea(); |
| 131 | break; |
| 132 | case CaptureModes::CurrentScreen: |
| 133 | mCaptureRect = currentScreenRect(); |
| 134 | break; |
| 135 | case CaptureModes::ActiveWindow: |
| 136 | mCaptureRect = activeWindowRect(); |
| 137 | if (mCaptureRect.isNull()) { |
| 138 | qWarning("ImageGrabber::getActiveWindow: Found no window with focus."); |
| 139 | mCaptureRect = currentScreenRect(); |
| 140 | } |
| 141 | break; |
| 142 | default: |
| 143 | mCaptureRect = fullScreenRect(); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | bool AbstractRectAreaImageGrabber::isSnippingAreaBackgroundTransparent() const |
| 148 | { |
| 149 | return !mFreezeImageWhileSnipping; |
| 150 | } |
| 151 | |
| 152 | void AbstractRectAreaImageGrabber::prepareGrab() |
| 153 | { |
| 154 | if (isRectAreaCaptureWithBackground()) { |
| 155 | openSnippingArea(); |
| 156 | } else { |
| 157 | grab(); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | void AbstractRectAreaImageGrabber::grab() |
| 162 | { |
| 163 | setCaptureRectFromCorrectSource(); |
| 164 | CaptureDto capture(getScreenshot()); |
| 165 | |
| 166 | if (shouldCaptureCursor()) { |
| 167 | capture.cursor = getCursorRelativeToScreenshot(); |
| 168 | } |
| 169 | emit finished(capture); |
| 170 | } |
| 171 | |
| 172 | CursorDto AbstractRectAreaImageGrabber::getCursorRelativeToScreenshot() const |
| 173 | { |
| 174 | auto cursor = getCursorImageWithPositionFromCorrectSource(); |
| 175 | |
| 176 | if (mCaptureRect.contains(cursor.position)) { |
| 177 | cursor.position -= mCaptureRect.topLeft(); // The final cursor is inserted with offset from 0,0 |
| 178 | return cursor; |
| 179 | } else { |
| 180 | return {}; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | bool AbstractRectAreaImageGrabber::shouldCaptureCursor() const |
| 185 | { |
| 186 | return isCaptureCursorEnabled() && !(captureMode() == CaptureModes::RectArea && isCaptureDelayBelowMin()); |
| 187 | } |
| 188 | |
| 189 | void AbstractRectAreaImageGrabber::openSnippingArea() |
| 190 | { |
| 191 | if (isSnippingAreaBackgroundTransparent()) { |
| 192 | openSnippingAreaWithoutBackground(); |
| 193 | } else { |
| 194 | auto screenRect = fullScreenRect(); |
| 195 | auto background = getScreenshotFromRect(screenRect); |
| 196 | storeCursorForPostProcessing(screenRect); |
| 197 | openSnippingAreaWithBackground(background); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | void AbstractRectAreaImageGrabber::storeCursorForPostProcessing(const QRect &screenRect) |
| 202 | { |
| 203 | auto offset = screenRect.topLeft(); |
| 204 | auto cursorWithPosition = getCursorWithPosition(); |
| 205 | cursorWithPosition.position -= offset; // Fix in case left most monitor has negative position |
| 206 | mStoredCursorImageWithPosition = cursorWithPosition; |
| 207 | } |
| 208 | |
| 209 | void AbstractRectAreaImageGrabber::connectSnippingAreaCancel() |
| 210 | { |
| 211 | connect(mSnippingArea, &AbstractSnippingArea::canceled, this, &AbstractRectAreaImageGrabber::canceled); |
| 212 | } |
| 213 | |
| 214 | void AbstractRectAreaImageGrabber::connectSnippingAreaFinish() |
| 215 | { |
| 216 | disconnectSnippingAreaFinish(); |
| 217 | |
| 218 | if (isSnippingAreaBackgroundTransparent()) { |
| 219 | connect(mSnippingArea, &AbstractSnippingArea::finished, [this]() |
| 220 | { |
| 221 | QTimer::singleShot(captureDelay(), this, &AbstractRectAreaImageGrabber::grab); |
| 222 | }); |
| 223 | } else { |
| 224 | connect(mSnippingArea, &AbstractSnippingArea::finished, this, &AbstractRectAreaImageGrabber::grab); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | void AbstractRectAreaImageGrabber::disconnectSnippingAreaFinish() |
| 229 | { |
| 230 | disconnect(mSnippingArea, &AbstractSnippingArea::finished, nullptr, nullptr); |
| 231 | } |
| 232 | |
| 233 | CursorDto AbstractRectAreaImageGrabber::getCursorImageWithPositionFromCorrectSource() const |
| 234 | { |
| 235 | return isRectAreaCaptureWithBackground() ? mStoredCursorImageWithPosition : getCursorWithPosition(); |
| 236 | } |
| 237 | |
| 238 | bool AbstractRectAreaImageGrabber::isRectAreaCaptureWithBackground() const |
| 239 | { |
| 240 | return captureMode() == CaptureModes::RectArea && !isSnippingAreaBackgroundTransparent(); |
| 241 | } |
| 242 | |
| 243 | bool AbstractRectAreaImageGrabber::isRectAreaCaptureWithoutBackground() const |
| 244 | { |
| 245 | return captureMode() == CaptureModes::RectArea && isSnippingAreaBackgroundTransparent(); |
| 246 | } |