| 1 | /* |
| 2 | * Copyright (C) 2016 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 "AbstractSnippingArea.h" |
| 21 | |
| 22 | AbstractSnippingArea::AbstractSnippingArea(const QSharedPointer<IConfig> &config) : |
| 23 | mConfig(config), |
| 24 | mBackground(nullptr), |
| 25 | mResizer(new SnippingAreaResizer(mConfig, this)), |
| 26 | mSelector(new SnippingAreaSelector(mConfig, this)), |
| 27 | mSelectorInfoText(new SnippingAreaSelectorInfoText(this)), |
| 28 | mResizerInfoText(new SnippingAreaResizerInfoText(this)), |
| 29 | mIsSwitchPressed(false), |
| 30 | mTimer(new QTimer(this)), |
| 31 | mUnselectedRegionAlpha(150) |
| 32 | { |
| 33 | // Make the frame span across the screen and show above any other widget |
| 34 | setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint); |
| 35 | |
| 36 | setMouseTracking(true); |
| 37 | |
| 38 | connect(mResizer, &SnippingAreaResizer::rectChanged, this, &AbstractSnippingArea::updateCapturedArea); |
| 39 | connect(mResizer, &SnippingAreaResizer::cursorChanged, this, &AbstractSnippingArea::updateCursor); |
| 40 | |
| 41 | connect(mSelector, &SnippingAreaSelector::rectChanged, this, &AbstractSnippingArea::updateCapturedArea); |
| 42 | connect(mSelector, &SnippingAreaSelector::cursorChanged, this, &AbstractSnippingArea::updateCursor); |
| 43 | |
| 44 | connect(mTimer, &QTimer::timeout, this, &AbstractSnippingArea::cancelSelection); |
| 45 | |
| 46 | connect(mConfig.data(), &IConfig::snippingAreaChangedChanged, this, &AbstractSnippingArea::updatePosition); |
| 47 | |
| 48 | updatePosition(); |
| 49 | } |
| 50 | |
| 51 | AbstractSnippingArea::~AbstractSnippingArea() |
| 52 | { |
| 53 | delete mBackground; |
| 54 | delete mResizer; |
| 55 | delete mSelector; |
| 56 | delete mSelectorInfoText; |
| 57 | delete mResizerInfoText; |
| 58 | delete mTimer; |
| 59 | } |
| 60 | |
| 61 | void AbstractSnippingArea::showWithoutBackground() |
| 62 | { |
| 63 | setAttribute(Qt::WA_TranslucentBackground, true); |
| 64 | clearBackgroundImage(); |
| 65 | showSnippingArea(); |
| 66 | } |
| 67 | |
| 68 | void AbstractSnippingArea::showWithBackground(const QPixmap &background) |
| 69 | { |
| 70 | setAttribute(Qt::WA_TranslucentBackground, false); |
| 71 | setBackgroundImage(background); |
| 72 | showSnippingArea(); |
| 73 | } |
| 74 | |
| 75 | QRect AbstractSnippingArea::getCaptureArea() const |
| 76 | { |
| 77 | auto offset = getPosition().toPoint(); |
| 78 | const QRect &anAuto = mCaptureArea.translated(-offset.x(), -offset.y()); |
| 79 | return anAuto; |
| 80 | } |
| 81 | |
| 82 | QRect AbstractSnippingArea::getGlobalCaptureArea() const |
| 83 | { |
| 84 | auto captureArea = getCaptureArea(); |
| 85 | return { mapToGlobal(captureArea.topLeft()), captureArea.size() }; |
| 86 | } |
| 87 | |
| 88 | void AbstractSnippingArea::showSnippingArea() |
| 89 | { |
| 90 | startTimeout(); |
| 91 | mIsSwitchPressed = false; |
| 92 | setFullScreen(); |
| 93 | mSelector->activate(getGeometry(), getGlobalCursorPosition()); |
| 94 | mUnselectedRegionAlpha = mConfig->snippingAreaTransparency(); |
| 95 | if(mConfig->showSnippingAreaInfoText()) { |
| 96 | mSelectorInfoText->activate(getGeometry(), mConfig->allowResizingRectSelection()); |
| 97 | } |
| 98 | grabKeyboardFocus(); |
| 99 | } |
| 100 | |
| 101 | void AbstractSnippingArea::setBackgroundImage(const QPixmap &background) |
| 102 | { |
| 103 | clearBackgroundImage(); |
| 104 | mBackground = new QPixmap(background); |
| 105 | mSelector->setBackgroundImage(mBackground); |
| 106 | } |
| 107 | |
| 108 | void AbstractSnippingArea::clearBackgroundImage() |
| 109 | { |
| 110 | delete mBackground; |
| 111 | mBackground = nullptr; |
| 112 | mSelector->setBackgroundImage(nullptr); |
| 113 | } |
| 114 | |
| 115 | void AbstractSnippingArea::mousePressEvent(QMouseEvent *event) |
| 116 | { |
| 117 | if (event->button() != Qt::LeftButton) { |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | auto pos = event->pos(); |
| 122 | mResizer->handleMousePress(pos); |
| 123 | mSelector->handleMousePress(pos); |
| 124 | } |
| 125 | |
| 126 | void AbstractSnippingArea::mouseReleaseEvent(QMouseEvent *event) |
| 127 | { |
| 128 | if (event->button() != Qt::LeftButton) { |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | mResizer->handleMouseRelease(); |
| 133 | mSelector->handleMouseRelease(); |
| 134 | |
| 135 | |
| 136 | if(isResizerSwitchRequired() && !mResizer->isActive()) { |
| 137 | switchToResizer(event->pos()); |
| 138 | } else if(mSelector->isActive()){ |
| 139 | finishSelection(); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | void AbstractSnippingArea::mouseMoveEvent(QMouseEvent *event) |
| 144 | { |
| 145 | auto pos = event->pos(); |
| 146 | mResizer->handleMouseMove(pos); |
| 147 | mSelector->handleMouseMove(pos); |
| 148 | mSelectorInfoText->handleMouseMove(pos); |
| 149 | mResizerInfoText->handleMouseMove(pos); |
| 150 | update(); |
| 151 | QWidget::mouseMoveEvent(event); |
| 152 | } |
| 153 | |
| 154 | void AbstractSnippingArea::mouseDoubleClickEvent(QMouseEvent *event) |
| 155 | { |
| 156 | if (mResizer->isActive()) { |
| 157 | finishSelection(); |
| 158 | } |
| 159 | |
| 160 | QWidget::mouseDoubleClickEvent(event); |
| 161 | } |
| 162 | |
| 163 | bool AbstractSnippingArea::isResizerSwitchRequired() const |
| 164 | { |
| 165 | bool allowResizingRectSelection = mConfig->allowResizingRectSelection(); |
| 166 | return (allowResizingRectSelection && !mIsSwitchPressed) || (!allowResizingRectSelection && mIsSwitchPressed); |
| 167 | } |
| 168 | |
| 169 | void AbstractSnippingArea::switchToResizer(const QPointF &pos) |
| 170 | { |
| 171 | mSelector->deactivate(); |
| 172 | mSelectorInfoText->deactivate(); |
| 173 | if(mConfig->showSnippingAreaInfoText()) { |
| 174 | mResizerInfoText->activate(getGeometry()); |
| 175 | } |
| 176 | mResizer->activate(mCaptureArea, pos); |
| 177 | update(); |
| 178 | } |
| 179 | |
| 180 | QPixmap AbstractSnippingArea::background() const |
| 181 | { |
| 182 | Q_ASSERT(!isBackgroundTransparent()); |
| 183 | return *mBackground; |
| 184 | } |
| 185 | |
| 186 | bool AbstractSnippingArea::closeSnippingArea() |
| 187 | { |
| 188 | mSelector->deactivate(); |
| 189 | mResizer->deactivate(); |
| 190 | mSelectorInfoText->deactivate(); |
| 191 | mResizerInfoText->deactivate(); |
| 192 | releaseKeyboard(); // Issue #57 |
| 193 | return QWidget::close(); |
| 194 | } |
| 195 | |
| 196 | void AbstractSnippingArea::paintEvent(QPaintEvent *event) |
| 197 | { |
| 198 | QPainter painter(this); |
| 199 | |
| 200 | auto geometry = getGeometry(); |
| 201 | if (!isBackgroundTransparent()) { |
| 202 | painter.drawPixmap(geometry, *mBackground, mBackground->rect()); |
| 203 | } |
| 204 | |
| 205 | painter.setClipRegion(mClippingRegion); |
| 206 | |
| 207 | painter.setBrush(QColor(0, 0, 0, mUnselectedRegionAlpha)); |
| 208 | painter.drawRect(geometry); |
| 209 | |
| 210 | mResizer->paint(&painter); |
| 211 | mSelector->paint(&painter); |
| 212 | |
| 213 | painter.setClipRect(geometry); |
| 214 | |
| 215 | mSelectorInfoText->paint(&painter); |
| 216 | mResizerInfoText->paint(&painter); |
| 217 | |
| 218 | QWidget::paintEvent(event); |
| 219 | } |
| 220 | |
| 221 | bool AbstractSnippingArea::isBackgroundTransparent() const |
| 222 | { |
| 223 | return mBackground == nullptr; |
| 224 | } |
| 225 | |
| 226 | QPoint AbstractSnippingArea::getGlobalCursorPosition() const |
| 227 | { |
| 228 | return QCursor::pos(); |
| 229 | } |
| 230 | |
| 231 | void AbstractSnippingArea::keyPressEvent(QKeyEvent *event) |
| 232 | { |
| 233 | if (event->key() == Qt::Key_Escape) { |
| 234 | cancelSelection(); |
| 235 | } else if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) { |
| 236 | finishSelection(); |
| 237 | } else if (event->key() == Qt::Key_Control){ |
| 238 | mIsSwitchPressed = true; |
| 239 | } |
| 240 | |
| 241 | mSelector->handleKeyPress(event); |
| 242 | mResizer->handleKeyPress(event); |
| 243 | update(); |
| 244 | |
| 245 | QWidget::keyPressEvent(event); |
| 246 | } |
| 247 | |
| 248 | void AbstractSnippingArea::keyReleaseEvent(QKeyEvent *event) |
| 249 | { |
| 250 | if (event->key() == Qt::Key_Control) { |
| 251 | mIsSwitchPressed = false; |
| 252 | } |
| 253 | |
| 254 | mResizer->handleKeyRelease(event); |
| 255 | |
| 256 | QWidget::keyReleaseEvent(event); |
| 257 | } |
| 258 | |
| 259 | void AbstractSnippingArea::cancelSelection() |
| 260 | { |
| 261 | stopTimeout(); |
| 262 | closeSnippingArea(); |
| 263 | emit canceled(); |
| 264 | } |
| 265 | |
| 266 | void AbstractSnippingArea::updateCapturedArea(const QRectF &rect) |
| 267 | { |
| 268 | mCaptureArea = rect.toRect(); |
| 269 | mClippingRegion = QRegion(getGeometry().toRect()).subtracted(QRegion(mCaptureArea)); |
| 270 | } |
| 271 | |
| 272 | void AbstractSnippingArea::finishSelection() |
| 273 | { |
| 274 | stopTimeout(); |
| 275 | closeSnippingArea(); |
| 276 | mConfig->setLastRectArea(selectedRectArea()); |
| 277 | emit finished(); |
| 278 | } |
| 279 | |
| 280 | void AbstractSnippingArea::grabKeyboardFocus() |
| 281 | { |
| 282 | QApplication::setActiveWindow(this); |
| 283 | activateWindow(); |
| 284 | setFocus(); |
| 285 | grabKeyboard(); |
| 286 | } |
| 287 | |
| 288 | QPointF AbstractSnippingArea::getPosition() const |
| 289 | { |
| 290 | return mPosition; |
| 291 | } |
| 292 | |
| 293 | QRectF AbstractSnippingArea::getGeometry() const |
| 294 | { |
| 295 | return { getPosition(), getSize() }; |
| 296 | } |
| 297 | |
| 298 | void AbstractSnippingArea::updateCursor(const QCursor &cursor) |
| 299 | { |
| 300 | setCursor(cursor); |
| 301 | } |
| 302 | |
| 303 | void AbstractSnippingArea::startTimeout() |
| 304 | { |
| 305 | mTimer->start(60000); |
| 306 | } |
| 307 | |
| 308 | void AbstractSnippingArea::stopTimeout() |
| 309 | { |
| 310 | mTimer->stop(); |
| 311 | } |
| 312 | |
| 313 | void AbstractSnippingArea::updatePosition() |
| 314 | { |
| 315 | if(mConfig->snippingAreaOffsetEnable()) { |
| 316 | mPosition = mConfig->snippingAreaOffset(); |
| 317 | } else { |
| 318 | mPosition = {}; |
| 319 | } |
| 320 | } |