| 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
| 21 | #include "MainWindow.h" |
| 22 | |
| 23 | MainWindow::MainWindow(DependencyInjector *dependencyInjector) : |
| 24 | QMainWindow(), |
| 25 | mDependencyInjector(dependencyInjector), |
| 26 | mConfig(mDependencyInjector->get<IConfig>()), |
| 27 | mImageGrabber(mDependencyInjector->get<IImageGrabber>()), |
| 28 | mPluginManager(mDependencyInjector->get<IPluginManager>()), |
| 29 | mTrayIcon(new TrayIcon(mConfig, mDependencyInjector->get<IIconLoader>(), this)), |
| 30 | mNotificationService(NotificationServiceFactory::create(mTrayIcon, mDependencyInjector->get<IPlatformChecker>(), mConfig)), |
| 31 | mToolBar(nullptr), |
| 32 | mImageAnnotator(new KImageAnnotatorAdapter), |
| 33 | mSaveAsAction(new QAction(this)), |
| 34 | mSaveAllAction(new QAction(this)), |
| 35 | mUploadAction(new QAction(this)), |
| 36 | mCopyAsDataUriAction(new QAction(this)), |
| 37 | mPrintAction(new QAction(this)), |
| 38 | mPrintPreviewAction(new QAction(this)), |
| 39 | mQuitAction(new QAction(this)), |
| 40 | mCopyPathAction(new QAction(this)), |
| 41 | mRenameAction(new QAction(this)), |
| 42 | mOpenDirectoryAction(new QAction(this)), |
| 43 | mToggleDocksAction(new QAction(this)), |
| 44 | mSettingsAction(new QAction(this)), |
| 45 | mAboutAction(new QAction(this)), |
| 46 | mOpenImageAction(new QAction(this)), |
| 47 | mScaleAction(new QAction(this)), |
| 48 | mRotateAction(new QAction(this)), |
| 49 | mAddWatermarkAction(new QAction(this)), |
| 50 | mPasteAction(new QAction(this)), |
| 51 | mPasteEmbeddedAction(new QAction(this)), |
| 52 | mPinAction(new QAction(this)), |
| 53 | mRemoveImageAction(new QAction(this)), |
| 54 | mModifyCanvasAction(new QAction(this)), |
| 55 | mCloseWindowAction(new QAction(this)), |
| 56 | mOcrAction(new QAction(this)), |
| 57 | mCutAction(new QAction(this)), |
| 58 | mMainLayout(layout()), |
| 59 | mActionsMenu(new ActionsMenu(mConfig)), |
| 60 | mRecentImagesMenu(new RecentImagesMenu(mDependencyInjector->get<IRecentImageService>(), this)), |
| 61 | mClipboard(mDependencyInjector->get<IClipboard>()), |
| 62 | mCapturePrinter(new CapturePrinter(this)), |
| 63 | mGlobalHotKeyHandler(new GlobalHotKeyHandler(mImageGrabber->supportedCaptureModes(), mDependencyInjector->get<IPlatformChecker>(), mConfig)), |
| 64 | mDragAndDropProcessor(new DragAndDropProcessor(this, mDependencyInjector->get<ITempFileProvider>())), |
| 65 | mUploadHandler(mDependencyInjector->get<IUploadHandler>()), |
| 66 | mSessionManagerRequestedQuit(false), |
| 67 | mResizeOnNormalize(false), |
| 68 | mCaptureHandler(CaptureHandlerFactory::create(mImageAnnotator, mNotificationService, mDependencyInjector, this)), |
| 69 | mPinWindowHandler(mDependencyInjector->get<IPinWindowHandler>()), |
| 70 | mVisibilityHandler(WidgetVisibilityHandlerFactory::create(this, mDependencyInjector->get<IPlatformChecker>())), |
| 71 | mFileDialogService(mDependencyInjector->get<IFileDialogService>()), |
| 72 | mWindowResizer(new WindowResizer(this, mConfig, this)), |
| 73 | mActionProcessor(new ActionProcessor), |
| 74 | mSavePathProvider(mDependencyInjector->get<ISavePathProvider>()), |
| 75 | mOcrWindowHandler(mDependencyInjector->get<IOcrWindowHandler>()), |
| 76 | mDelayHandler(mDependencyInjector->get<IDelayHandler>()) |
| 77 | { |
| 78 | initGui(); |
| 79 | |
| 80 | setPosition(); |
| 81 | |
| 82 | auto coreApplication = dynamic_cast<QApplication *>(QCoreApplication::instance()); |
| 83 | connect(coreApplication, &QGuiApplication::commitDataRequest, this, &MainWindow::sessionFinished); |
| 84 | |
| 85 | setAcceptDrops(true); |
| 86 | coreApplication->installEventFilter(mDragAndDropProcessor); |
| 87 | connect(mDragAndDropProcessor, &DragAndDropProcessor::fileDropped, this, &MainWindow::loadImageFromFile); |
| 88 | connect(mDragAndDropProcessor, &DragAndDropProcessor::imageDropped, this, &MainWindow::loadImageFromPixmap); |
| 89 | |
| 90 | connect(mConfig.data(), &IConfig::annotatorConfigChanged, this, &MainWindow::setupImageAnnotator); |
| 91 | |
| 92 | connect(mImageGrabber.data(), &IImageGrabber::finished, this, &MainWindow::processCapture); |
| 93 | connect(mImageGrabber.data(), &IImageGrabber::canceled, this, &MainWindow::captureCanceled); |
| 94 | connect(mImageGrabber.data(), &IImageGrabber::canceled, mActionProcessor, &ActionProcessor::captureCanceled); |
| 95 | |
| 96 | connect(mGlobalHotKeyHandler, &GlobalHotKeyHandler::captureTriggered, this, &MainWindow::triggerCapture); |
| 97 | connect(mGlobalHotKeyHandler, &GlobalHotKeyHandler::actionTriggered, this, &MainWindow::actionTriggered); |
| 98 | |
| 99 | connect(mUploadHandler.data(), &IUploadHandler::finished, this, &MainWindow::uploadFinished); |
| 100 | |
| 101 | connect(mRecentImagesMenu, &RecentImagesMenu::openRecentSelected, this, &MainWindow::loadImageFromFile); |
| 102 | |
| 103 | connect(this, &MainWindow::imageLoaded, mActionProcessor, &ActionProcessor::captureFinished); |
| 104 | connect(mActionProcessor, &ActionProcessor::triggerCapture, this, &MainWindow::capture); |
| 105 | connect(mActionProcessor, &ActionProcessor::triggerPinImage, mPinAction, &QAction::trigger); |
| 106 | connect(mActionProcessor, &ActionProcessor::triggerUpload, mUploadAction, &QAction::trigger); |
| 107 | connect(mActionProcessor, &ActionProcessor::triggerOpenDirectory, mCaptureHandler, &ICaptureHandler::openDirectory); |
| 108 | connect(mActionProcessor, &ActionProcessor::triggerCopyToClipboard, mToolBar->copyToClipboardAction(), &QAction::trigger); |
| 109 | connect(mActionProcessor, &ActionProcessor::triggerSave, mToolBar->saveAction(), &QAction::trigger); |
| 110 | connect(mActionProcessor, &ActionProcessor::triggerShow, this, &MainWindow::showAfterAction); |
| 111 | |
| 112 | mCaptureHandler->addListener(this); |
| 113 | |
| 114 | handleGuiStartup(); |
| 115 | setupImageAnnotator(); |
| 116 | resize(minimumSize()); |
| 117 | loadSettings(); |
| 118 | } |
| 119 | |
| 120 | MainWindow::~MainWindow() |
| 121 | { |
| 122 | delete mImageAnnotator; |
| 123 | delete mCapturePrinter; |
| 124 | delete mDragAndDropProcessor; |
| 125 | delete mCaptureHandler; |
| 126 | delete mVisibilityHandler; |
| 127 | delete mWindowResizer; |
| 128 | delete mActionProcessor; |
| 129 | delete mActionsMenu; |
| 130 | } |
| 131 | |
| 132 | void MainWindow::handleGuiStartup() |
| 133 | { |
| 134 | if (mConfig->captureOnStartup()) { |
| 135 | triggerCapture(mConfig->captureMode()); |
| 136 | } else if (mTrayIcon->isVisible() && mConfig->startMinimizedToTray()) { |
| 137 | showHidden(); |
| 138 | } else { |
| 139 | showEmpty(); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | void MainWindow::setPosition() |
| 144 | { |
| 145 | auto position = mConfig->windowPosition(); |
| 146 | auto desktopGeometry = QApplication::primaryScreen()->geometry(); |
| 147 | |
| 148 | if(!desktopGeometry.contains(position)) { |
| 149 | auto screenCenter = desktopGeometry.center(); |
| 150 | auto mainWindowSize = size(); |
| 151 | position = QPoint(screenCenter.x() - mainWindowSize.width() / 2, screenCenter.y() - mainWindowSize.height() / 2); |
| 152 | } |
| 153 | move(position); |
| 154 | } |
| 155 | |
| 156 | void MainWindow::captureScreenshot(CaptureModes captureMode, bool captureCursor, int delay) |
| 157 | { |
| 158 | mImageGrabber->grabImage(captureMode, captureCursor, delay); |
| 159 | } |
| 160 | |
| 161 | void MainWindow::quit() |
| 162 | { |
| 163 | if(!mCaptureHandler->canClose()){ |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | mTrayIcon->hide(); |
| 168 | QCoreApplication::exit(0); |
| 169 | } |
| 170 | |
| 171 | void MainWindow::processCapture(const CaptureDto &capture) |
| 172 | { |
| 173 | if (!capture.isValid()) { |
| 174 | auto title = tr("Unable to show image"); |
| 175 | auto message = tr("No image provided but one was expected."); |
| 176 | NotifyOperation operation(title, message, NotificationTypes::Critical, mNotificationService, mConfig); |
| 177 | operation.execute(); |
| 178 | showEmpty(); |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | processImage(capture); |
| 183 | capturePostProcessing(); |
| 184 | } |
| 185 | |
| 186 | void MainWindow::processImage(const CaptureDto &capture) |
| 187 | { |
| 188 | mCaptureHandler->load(capture); |
| 189 | if (!mActionProcessor->isActionInProgress()) { // The action processor handles the showing of the main window |
| 190 | showDefault(); |
| 191 | } |
| 192 | captureChanged(); |
| 193 | setEnablements(true); |
| 194 | emit imageLoaded(); |
| 195 | } |
| 196 | |
| 197 | DragContent MainWindow::dragContent() const |
| 198 | { |
| 199 | return DragContent(mCaptureHandler->image(), mCaptureHandler->path(), mCaptureHandler->isSaved()); |
| 200 | } |
| 201 | |
| 202 | void MainWindow::resizeToContent() |
| 203 | { |
| 204 | if(!mToolBar->isCollapsed() || mImageAnnotator->isVisible()) { |
| 205 | mMainLayout->setSizeConstraint(QLayout::SetFixedSize); // Workaround that allows us to return to toolbar only size |
| 206 | QMainWindow::adjustSize(); |
| 207 | } else { |
| 208 | setFixedSize(menuBar()->sizeHint()); |
| 209 | } |
| 210 | mMainLayout->setSizeConstraint(QLayout::SetMinAndMaxSize); |
| 211 | setMinimumSize(minimumSizeHint()); // Workaround for issue #588 |
| 212 | } |
| 213 | |
| 214 | bool MainWindow::isWindowMaximized() |
| 215 | { |
| 216 | return mVisibilityHandler->isMaximized(); |
| 217 | } |
| 218 | |
| 219 | void MainWindow::capturePostProcessing() |
| 220 | { |
| 221 | if (mConfig->autoCopyToClipboardNewCaptures()) { |
| 222 | copyCaptureToClipboard(); |
| 223 | } |
| 224 | |
| 225 | if (mConfig->autoSaveNewCaptures()) { |
| 226 | mCaptureHandler->save(); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | void MainWindow::showEmpty() |
| 231 | { |
| 232 | mVisibilityHandler->show(); |
| 233 | captureChanged(); |
| 234 | setEnablements(false); |
| 235 | } |
| 236 | |
| 237 | void MainWindow::showHidden() |
| 238 | { |
| 239 | captureChanged(); |
| 240 | setEnablements(false); |
| 241 | mVisibilityHandler->hide(); |
| 242 | } |
| 243 | |
| 244 | void MainWindow::showDefault() |
| 245 | { |
| 246 | auto enforceVisible = mConfig->showMainWindowAfterTakingScreenshotEnabled(); |
| 247 | enforceVisible ? mVisibilityHandler->enforceVisible() : mVisibilityHandler->restoreState(); |
| 248 | |
| 249 | mWindowResizer->resize(); |
| 250 | } |
| 251 | |
| 252 | QMenu* MainWindow::createPopupMenu() |
| 253 | { |
| 254 | // Filtering out the option to hide main toolbar which should no be allowed. |
| 255 | auto filteredMenu = QMainWindow::createPopupMenu(); |
| 256 | filteredMenu->removeAction(mToolBar->toggleViewAction()); |
| 257 | return filteredMenu; |
| 258 | } |
| 259 | |
| 260 | QSize MainWindow::sizeHint() const |
| 261 | { |
| 262 | auto minHeight = mToolBar->sizeHint().height(); |
| 263 | auto minWidth = mToolBar->sizeHint().width(); |
| 264 | auto annotatorSize = mImageAnnotator->sizeHint(); |
| 265 | auto height = minHeight + annotatorSize.height(); |
| 266 | auto width = minWidth > annotatorSize.width() ? minWidth : annotatorSize.width(); |
| 267 | return { width, height }; |
| 268 | } |
| 269 | |
| 270 | void MainWindow::moveEvent(QMoveEvent* event) |
| 271 | { |
| 272 | mConfig->setWindowPosition(pos()); |
| 273 | QWidget::moveEvent(event); |
| 274 | } |
| 275 | |
| 276 | void MainWindow::closeEvent(QCloseEvent* event) |
| 277 | { |
| 278 | if (!mSessionManagerRequestedQuit) { |
| 279 | event->ignore(); |
| 280 | } |
| 281 | |
| 282 | if (mTrayIcon->isVisible() && mConfig->closeToTray()) { |
| 283 | mVisibilityHandler->hide(); |
| 284 | } else { |
| 285 | quit(); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | void MainWindow::changeEvent(QEvent *event) |
| 290 | { |
| 291 | if (event->type() == QEvent::WindowStateChange) { |
| 292 | if(mResizeOnNormalize && !isMaximized()) { |
| 293 | mResizeOnNormalize = false; |
| 294 | resizeToContent(); |
| 295 | } else if(isMinimized() && mTrayIcon->isVisible() && mConfig->minimizeToTray()) { |
| 296 | event->ignore(); |
| 297 | mVisibilityHandler->hide(); |
| 298 | } |
| 299 | mVisibilityHandler->updateState(); |
| 300 | } |
| 301 | QWidget::changeEvent(event); |
| 302 | } |
| 303 | |
| 304 | void MainWindow::captureChanged() |
| 305 | { |
| 306 | mToolBar->setSaveActionEnabled(!mCaptureHandler->isSaved()); |
| 307 | mCopyPathAction->setEnabled(mCaptureHandler->isPathValid()); |
| 308 | mRenameAction->setEnabled(mCaptureHandler->isPathValid()); |
| 309 | mOpenDirectoryAction->setEnabled(mCaptureHandler->isPathValid()); |
| 310 | mRemoveImageAction->setEnabled(mCaptureHandler->isPathValid()); |
| 311 | updateApplicationTitle(); |
| 312 | } |
| 313 | |
| 314 | void MainWindow::updateApplicationTitle() |
| 315 | { |
| 316 | auto path = mCaptureHandler->path(); |
| 317 | auto isUnsaved = !mCaptureHandler->isSaved(); |
| 318 | auto applicationTitle = ApplicationTitleProvider::getApplicationTitle(QApplication::applicationName(), path, tr("Unsaved"), isUnsaved); |
| 319 | setWindowTitle(applicationTitle); |
| 320 | } |
| 321 | |
| 322 | void MainWindow::setEnablements(bool enabled) |
| 323 | { |
| 324 | mPrintAction->setEnabled(enabled); |
| 325 | mPrintPreviewAction->setEnabled(enabled); |
| 326 | mUploadAction->setEnabled(enabled); |
| 327 | mCopyAsDataUriAction->setEnabled(enabled); |
| 328 | mScaleAction->setEnabled(enabled); |
| 329 | mRotateAction->setEnabled(enabled); |
| 330 | mAddWatermarkAction->setEnabled(enabled); |
| 331 | mToolBar->setCopyActionEnabled(enabled); |
| 332 | mToolBar->setCropEnabled(enabled); |
| 333 | mSaveAsAction->setEnabled(enabled); |
| 334 | mSaveAllAction->setEnabled(enabled); |
| 335 | mPinAction->setEnabled(enabled); |
| 336 | mPasteEmbeddedAction->setEnabled(mClipboard->isPixmap() && mImageAnnotator->isVisible()); |
| 337 | mRenameAction->setEnabled(enabled); |
| 338 | mModifyCanvasAction->setEnabled(enabled); |
| 339 | mCutAction->setEnabled(enabled); |
| 340 | mActionProcessor->setPostProcessingEnabled(enabled); |
| 341 | mOcrAction->setEnabled(mPluginManager->isAvailable(PluginType::Ocr) && enabled); |
| 342 | } |
| 343 | |
| 344 | void MainWindow::loadSettings() |
| 345 | { |
| 346 | mToolBar->selectCaptureMode(mConfig->captureMode()); |
| 347 | mToolBar->setCaptureDelay(mConfig->captureDelay() / 1000); |
| 348 | |
| 349 | if(mConfig->autoHideDocks()) { |
| 350 | toggleDocks(); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | void MainWindow::triggerCapture(CaptureModes captureMode) |
| 355 | { |
| 356 | if(!mCaptureHandler->canTakeNew()){ |
| 357 | return; |
| 358 | } |
| 359 | |
| 360 | mConfig->setCaptureMode(captureMode); |
| 361 | |
| 362 | capture(captureMode, mConfig->captureCursor(), mConfig->captureDelay()); |
| 363 | } |
| 364 | |
| 365 | void MainWindow::capture(CaptureModes captureMode, bool captureCursor, int delay) |
| 366 | { |
| 367 | auto isMainWindowVisible = !isMinimized(); |
| 368 | auto adjustedDelay = mDelayHandler->getDelay(delay, isMainWindowVisible); |
| 369 | |
| 370 | hideMainWindowIfRequired(); |
| 371 | |
| 372 | captureScreenshot(captureMode, captureCursor, adjustedDelay); |
| 373 | } |
| 374 | |
| 375 | void MainWindow::hideMainWindowIfRequired() |
| 376 | { |
| 377 | if (mConfig->hideMainWindowDuringScreenshot()) { |
| 378 | mVisibilityHandler->makeInvisible(); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | void MainWindow::toggleDocks() |
| 383 | { |
| 384 | auto newIsCollapsedState = !mToolBar->isCollapsed(); |
| 385 | |
| 386 | mToolBar->setCollapsed(newIsCollapsedState); |
| 387 | mImageAnnotator->setSettingsCollapsed(newIsCollapsedState); |
| 388 | |
| 389 | auto collapsedToggleText = newIsCollapsedState ? tr("Show Docks") : tr("Hide Docks"); |
| 390 | mToggleDocksAction->setText(collapsedToggleText); |
| 391 | |
| 392 | mWindowResizer->resize(); |
| 393 | } |
| 394 | |
| 395 | void MainWindow::initGui() |
| 396 | { |
| 397 | auto iconLoader = mDependencyInjector->get<IIconLoader>(); |
| 398 | |
| 399 | setWindowIcon(iconLoader->load(QLatin1String("ksnip"))); |
| 400 | |
| 401 | mToolBar = new MainToolBar( |
| 402 | mImageGrabber->supportedCaptureModes(), |
| 403 | mImageAnnotator->undoAction(), |
| 404 | mImageAnnotator->redoAction(), |
| 405 | iconLoader, |
| 406 | mDependencyInjector->get<IScaledSizeProvider>()); |
| 407 | |
| 408 | connect(mToolBar, &MainToolBar::captureModeSelected, this, &MainWindow::triggerCapture); |
| 409 | connect(mToolBar, &MainToolBar::saveActionTriggered, this, &MainWindow::saveClicked); |
| 410 | connect(mToolBar, &MainToolBar::copyActionTriggered, this, &MainWindow::copyCaptureToClipboard); |
| 411 | connect(mToolBar, &MainToolBar::captureDelayChanged, this, &MainWindow::captureDelayChanged); |
| 412 | connect(mToolBar, &MainToolBar::cropActionTriggered, mImageAnnotator, &IImageAnnotator::showCropper); |
| 413 | |
| 414 | mSaveAsAction->setText(tr("Save As...")); |
| 415 | mSaveAsAction->setShortcut(Qt::CTRL | Qt::SHIFT | Qt::Key_S); |
| 416 | mSaveAsAction->setIcon(iconLoader->loadForTheme(QLatin1String("saveAs"))); |
| 417 | connect(mSaveAsAction, &QAction::triggered, this, &MainWindow::saveAsClicked); |
| 418 | |
| 419 | mSaveAllAction->setText(tr("Save All")); |
| 420 | mSaveAllAction->setIcon(iconLoader->loadForTheme(QLatin1String("save"))); |
| 421 | connect(mSaveAllAction, &QAction::triggered, this, &MainWindow::saveAllClicked); |
| 422 | |
| 423 | mUploadAction->setText(tr("Upload")); |
| 424 | mUploadAction->setToolTip(tr("Upload triggerCapture to external source")); |
| 425 | mUploadAction->setShortcut(Qt::SHIFT | Qt::Key_U); |
| 426 | connect(mUploadAction, &QAction::triggered, this, &MainWindow::upload); |
| 427 | |
| 428 | mCopyAsDataUriAction->setText(tr("Copy as data URI")); |
| 429 | mCopyAsDataUriAction->setToolTip(tr("Copy triggerCapture to system clipboard")); |
| 430 | connect(mCopyAsDataUriAction, &QAction::triggered, this, &MainWindow::copyAsDataUri); |
| 431 | |
| 432 | mPrintAction->setText(tr("Print")); |
| 433 | mPrintAction->setToolTip(tr("Opens printer dialog and provide option to print image")); |
| 434 | mPrintAction->setShortcut(Qt::CTRL | Qt::Key_P); |
| 435 | mPrintAction->setIcon(QIcon::fromTheme(QLatin1String("document-print"))); |
| 436 | connect(mPrintAction, &QAction::triggered, this, &MainWindow::printClicked); |
| 437 | |
| 438 | mPrintPreviewAction->setText(tr("Print Preview")); |
| 439 | mPrintPreviewAction->setToolTip(tr("Opens Print Preview dialog where the image " |
| 440 | "orientation can be changed")); |
| 441 | mPrintPreviewAction->setIcon(QIcon::fromTheme(QLatin1String("document-print-preview"))); |
| 442 | connect(mPrintPreviewAction, &QAction::triggered, this, &MainWindow::printPreviewClicked); |
| 443 | |
| 444 | mScaleAction->setText(tr("Scale")); |
| 445 | mScaleAction->setToolTip(tr("Scale Image")); |
| 446 | mScaleAction->setShortcut(Qt::SHIFT | Qt::Key_S); |
| 447 | connect(mScaleAction, &QAction::triggered, this, &MainWindow::showScaleDialog); |
| 448 | |
| 449 | mRotateAction->setText(tr("Rotate")); |
| 450 | mRotateAction->setToolTip(tr("Rotate Image")); |
| 451 | mRotateAction->setShortcut(Qt::SHIFT | Qt::Key_O); |
| 452 | connect(mRotateAction, &QAction::triggered, this, &MainWindow::showRotateDialog); |
| 453 | |
| 454 | mAddWatermarkAction->setText(tr("Add Watermark")); |
| 455 | mAddWatermarkAction->setToolTip(tr("Add Watermark to captured image. Multiple watermarks can be added.")); |
| 456 | mAddWatermarkAction->setShortcut(Qt::SHIFT | Qt::Key_W); |
| 457 | connect(mAddWatermarkAction, &QAction::triggered, this, &MainWindow::addWatermark); |
| 458 | |
| 459 | mQuitAction->setText(tr("Quit")); |
| 460 | mQuitAction->setShortcut(Qt::CTRL | Qt::Key_Q); |
| 461 | mQuitAction->setIcon(QIcon::fromTheme(QLatin1String("application-exit"))); |
| 462 | connect(mQuitAction, &QAction::triggered, this, &MainWindow::quit); |
| 463 | |
| 464 | mCloseWindowAction->setText(tr("Close Window")); |
| 465 | mCloseWindowAction->setShortcut(Qt::SHIFT | Qt::Key_Escape); |
| 466 | mCloseWindowAction->setIcon(QIcon::fromTheme(QLatin1String("window-close"))); |
| 467 | connect(mCloseWindowAction, &QAction::triggered, this, &MainWindow::close); |
| 468 | |
| 469 | mCopyPathAction->setText(tr("Copy Path")); |
| 470 | connect(mCopyPathAction, &QAction::triggered, mCaptureHandler, &ICaptureHandler::copyPath); |
| 471 | |
| 472 | mRenameAction->setText(tr("Rename")); |
| 473 | mRenameAction->setShortcut(Qt::Key_F2); |
| 474 | connect(mRenameAction, &QAction::triggered, mCaptureHandler, &ICaptureHandler::rename); |
| 475 | |
| 476 | mOpenDirectoryAction->setText(tr("Open Directory")); |
| 477 | connect(mOpenDirectoryAction, &QAction::triggered, mCaptureHandler, &ICaptureHandler::openDirectory); |
| 478 | |
| 479 | mToggleDocksAction->setText(tr("Hide Docks")); |
| 480 | mToggleDocksAction->setShortcut(Qt::Key_Tab); |
| 481 | connect(mToggleDocksAction, &QAction::triggered, this, &MainWindow::toggleDocks); |
| 482 | |
| 483 | mSettingsAction->setText(tr("Settings")); |
| 484 | mSettingsAction->setIcon(QIcon::fromTheme(QLatin1String("emblem-system"))); |
| 485 | mSettingsAction->setShortcut(Qt::ALT | Qt::Key_F7); |
| 486 | connect(mSettingsAction, &QAction::triggered, this, &MainWindow::showSettingsDialog); |
| 487 | |
| 488 | mAboutAction->setText(tr("&About")); |
| 489 | mAboutAction->setIcon(iconLoader->load(QLatin1String("ksnip"))); |
| 490 | connect(mAboutAction, &QAction::triggered, this, &MainWindow::showAboutDialog); |
| 491 | |
| 492 | mOpenImageAction->setText(tr("Open")); |
| 493 | mOpenImageAction->setIcon(QIcon::fromTheme(QLatin1String("document-open"))); |
| 494 | mOpenImageAction->setShortcut(Qt::CTRL | Qt::Key_O); |
| 495 | connect(mOpenImageAction, &QAction::triggered, this, &MainWindow::showOpenImageDialog); |
| 496 | |
| 497 | mRecentImagesMenu->setTitle(tr("Open &Recent")); |
| 498 | mRecentImagesMenu->setIcon(QIcon::fromTheme(QLatin1String("document-open"))); |
| 499 | |
| 500 | mPasteAction->setText(tr("Paste")); |
| 501 | mPasteAction->setIcon(iconLoader->loadForTheme(QLatin1String("paste"))); |
| 502 | mPasteAction->setShortcut(Qt::CTRL | Qt::Key_V); |
| 503 | mPasteAction->setEnabled(mClipboard->isPixmap()); |
| 504 | connect(mPasteAction, &QAction::triggered, this, &MainWindow::pasteFromClipboard); |
| 505 | connect(mClipboard.data(), &IClipboard::changed, mPasteAction, &QAction::setEnabled); |
| 506 | |
| 507 | mPasteEmbeddedAction->setText(tr("Paste Embedded")); |
| 508 | mPasteEmbeddedAction->setIcon(iconLoader->loadForTheme(QLatin1String("pasteEmbedded"))); |
| 509 | mPasteEmbeddedAction->setShortcut(Qt::CTRL | Qt::SHIFT | Qt::Key_V); |
| 510 | mPasteEmbeddedAction->setEnabled(mClipboard->isPixmap() && mImageAnnotator->isVisible()); |
| 511 | connect(mPasteEmbeddedAction, &QAction::triggered, this, &MainWindow::pasteEmbeddedFromClipboard); |
| 512 | connect(mClipboard.data(), &IClipboard::changed, [this] (bool isPixmap){ mPasteEmbeddedAction->setEnabled(isPixmap && mImageAnnotator->isVisible()); }); |
| 513 | |
| 514 | mPinAction->setText(tr("Pin")); |
| 515 | mPinAction->setToolTip(tr("Pin screenshot to foreground in frameless window")); |
| 516 | mPinAction->setShortcut(Qt::SHIFT | Qt::Key_P); |
| 517 | mPinAction->setIcon(iconLoader->loadForTheme(QLatin1String("pin"))); |
| 518 | connect(mPinAction, &QAction::triggered, this, &MainWindow::showPinWindow); |
| 519 | |
| 520 | mRemoveImageAction->setText(tr("Delete")); |
| 521 | mRemoveImageAction->setIcon(iconLoader->loadForTheme(QLatin1String("delete"))); |
| 522 | connect(mRemoveImageAction, &QAction::triggered, mCaptureHandler, &ICaptureHandler::removeImage); |
| 523 | |
| 524 | mModifyCanvasAction->setText(tr("Modify Canvas")); |
| 525 | connect(mModifyCanvasAction, &QAction::triggered, mImageAnnotator, &IImageAnnotator::showCanvasModifier); |
| 526 | |
| 527 | mCutAction->setText(tr("Cut")); |
| 528 | mCutAction->setShortcut(Qt::SHIFT | Qt::Key_T); |
| 529 | connect(mCutAction, &QAction::triggered, mImageAnnotator, &IImageAnnotator::showCutter); |
| 530 | |
| 531 | mActionsMenu->setTitle(tr("Actions")); |
| 532 | mActionsMenu->setIcon(iconLoader->loadForTheme(QLatin1String("action"))); |
| 533 | connect(mActionsMenu, &ActionsMenu::triggered, this, &MainWindow::actionTriggered); |
| 534 | |
| 535 | mOcrAction->setText(tr("OCR")); |
| 536 | connect(mOcrAction, &QAction::triggered, this, &MainWindow::showOcrWindow); |
| 537 | |
| 538 | auto menu = menuBar()->addMenu(tr("&File")); |
| 539 | menu->addAction(mToolBar->newCaptureAction()); |
| 540 | menu->addSeparator(); |
| 541 | menu->addMenu(mActionsMenu); |
| 542 | menu->addSeparator(); |
| 543 | menu->addAction(mOpenImageAction); |
| 544 | menu->addMenu(mRecentImagesMenu); |
| 545 | menu->addAction(mToolBar->saveAction()); |
| 546 | menu->addAction(mSaveAsAction); |
| 547 | menu->addAction(mSaveAllAction); |
| 548 | menu->addAction(mUploadAction); |
| 549 | menu->addSeparator(); |
| 550 | menu->addAction(mPrintAction); |
| 551 | menu->addAction(mPrintPreviewAction); |
| 552 | menu->addSeparator(); |
| 553 | menu->addAction(mCloseWindowAction); |
| 554 | menu->addAction(mQuitAction); |
| 555 | menu = menuBar()->addMenu(tr("&Edit")); |
| 556 | menu->addAction(mToolBar->undoAction()); |
| 557 | menu->addAction(mToolBar->redoAction()); |
| 558 | menu->addSeparator(); |
| 559 | menu->addAction(mToolBar->copyToClipboardAction()); |
| 560 | menu->addAction(mCopyAsDataUriAction); |
| 561 | menu->addAction(mCopyPathAction); |
| 562 | menu->addAction(mRenameAction); |
| 563 | menu->addAction(mPasteAction); |
| 564 | menu->addAction(mPasteEmbeddedAction); |
| 565 | menu->addSeparator(); |
| 566 | menu->addAction(mToolBar->cropAction()); |
| 567 | menu->addAction(mScaleAction); |
| 568 | menu->addAction(mRotateAction); |
| 569 | menu->addAction(mCutAction); |
| 570 | menu->addAction(mAddWatermarkAction); |
| 571 | menu->addSeparator(); |
| 572 | menu->addAction(mRemoveImageAction); |
| 573 | menu = menuBar()->addMenu(tr("&View")); |
| 574 | menu->addAction(mOpenDirectoryAction); |
| 575 | menu->addAction(mToggleDocksAction); |
| 576 | menu->addAction(mModifyCanvasAction); |
| 577 | menu = menuBar()->addMenu(tr("&Options")); |
| 578 | menu->addAction(mPinAction); |
| 579 | menu->addAction(mOcrAction); |
| 580 | menu->addAction(mSettingsAction); |
| 581 | menu = menuBar()->addMenu(tr("&Help")); |
| 582 | menu->addAction(mAboutAction); |
| 583 | |
| 584 | addToolBar(mToolBar); |
| 585 | |
| 586 | if(mConfig->useTrayIcon()) { |
| 587 | connect(mTrayIcon, &TrayIcon::showEditorTriggered, [this](){ mVisibilityHandler->enforceVisible(); }); |
| 588 | mTrayIcon->setCaptureActions(mToolBar->captureActions()); |
| 589 | mTrayIcon->setOpenAction(mOpenImageAction); |
| 590 | mTrayIcon->setSaveAction(mToolBar->saveAction()); |
| 591 | mTrayIcon->setPasteAction(mPasteAction); |
| 592 | mTrayIcon->setCopyAction(mToolBar->copyToClipboardAction()); |
| 593 | mTrayIcon->setUploadAction(mUploadAction); |
| 594 | mTrayIcon->setQuitAction(mQuitAction); |
| 595 | mTrayIcon->setActionsMenu(mActionsMenu); |
| 596 | mTrayIcon->setEnabled(true); |
| 597 | } |
| 598 | |
| 599 | setCentralWidget(mImageAnnotator->widget()); |
| 600 | } |
| 601 | |
| 602 | void MainWindow::copyCaptureToClipboard() |
| 603 | { |
| 604 | mCaptureHandler->copy(); |
| 605 | } |
| 606 | |
| 607 | void MainWindow::upload() |
| 608 | { |
| 609 | auto image = mCaptureHandler->image(); |
| 610 | UploadOperation operation(image, mUploadHandler, mConfig, mDependencyInjector->get<IMessageBoxService>()); |
| 611 | operation.execute(); |
| 612 | } |
| 613 | |
| 614 | void MainWindow::copyAsDataUri() |
| 615 | { |
| 616 | auto image = mCaptureHandler->image(); |
| 617 | CopyAsDataUriOperation operation(image, mClipboard, mNotificationService, mConfig); |
| 618 | operation.execute(); |
| 619 | } |
| 620 | |
| 621 | void MainWindow::printClicked() |
| 622 | { |
| 623 | auto savePath = mSavePathProvider->savePathWithFormat(QLatin1String("pdf")); |
| 624 | auto image = mCaptureHandler->image(); |
| 625 | mCapturePrinter->print(image, savePath); |
| 626 | } |
| 627 | |
| 628 | void MainWindow::printPreviewClicked() |
| 629 | { |
| 630 | auto savePath = mSavePathProvider->savePathWithFormat(QLatin1String("pdf")); |
| 631 | auto image = mCaptureHandler->image(); |
| 632 | mCapturePrinter->printPreview(image, savePath); |
| 633 | } |
| 634 | |
| 635 | void MainWindow::showOpenImageDialog() |
| 636 | { |
| 637 | auto title = tr("Open Images"); |
| 638 | auto directory = mSavePathProvider->saveDirectory(); |
| 639 | auto filter = tr("Image Files") + FileDialogFilterHelper::ImageFilesImport(); |
| 640 | auto pathList = mFileDialogService->getOpenFileNames(this, title, directory, filter); |
| 641 | |
| 642 | for (const auto &path : pathList) { |
| 643 | loadImageFromFile(path); |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | void MainWindow::setupImageAnnotator() |
| 648 | { |
| 649 | mImageAnnotator->setSaveToolSelection(mConfig->rememberToolSelection()); |
| 650 | mImageAnnotator->setSmoothFactor(mConfig->smoothFactor()); |
| 651 | mImageAnnotator->setSmoothPathEnabled(mConfig->smoothPathEnabled()); |
| 652 | mImageAnnotator->setSwitchToSelectToolAfterDrawingItem(mConfig->switchToSelectToolAfterDrawingItem()); |
| 653 | mImageAnnotator->setSelectItemAfterDrawing(mConfig->selectItemAfterDrawing()); |
| 654 | mImageAnnotator->setNumberToolSeedChangeUpdatesAllItems(mConfig->numberToolSeedChangeUpdatesAllItems()); |
| 655 | mImageAnnotator->setStickers(mConfig->stickerPaths(), mConfig->useDefaultSticker()); |
| 656 | mImageAnnotator->setCanvasColor(mConfig->canvasColor()); |
| 657 | mImageAnnotator->setControlsWidgetVisible(mConfig->isControlsWidgetVisible()); |
| 658 | } |
| 659 | |
| 660 | void MainWindow::captureDelayChanged(int delay) |
| 661 | { |
| 662 | mConfig->setCaptureDelay(delay * 1000); |
| 663 | } |
| 664 | |
| 665 | void MainWindow::addWatermark() |
| 666 | { |
| 667 | AddWatermarkOperation operation(mImageAnnotator, mConfig); |
| 668 | operation.execute(); |
| 669 | } |
| 670 | |
| 671 | void MainWindow::showDialog(const std::function<void ()>& showDialogMethod) |
| 672 | { |
| 673 | mGlobalHotKeyHandler->setEnabled(false); |
| 674 | showDialogMethod(); |
| 675 | mGlobalHotKeyHandler->setEnabled(true); |
| 676 | } |
| 677 | |
| 678 | void MainWindow::showSettingsDialog() |
| 679 | { |
| 680 | showDialog([&](){ |
| 681 | SettingsDialog settingsDialog( |
| 682 | mImageGrabber->supportedCaptureModes(), |
| 683 | mConfig, |
| 684 | mDependencyInjector->get<IScaledSizeProvider>(), |
| 685 | mDependencyInjector->get<IDirectoryPathProvider>(), |
| 686 | mDependencyInjector->get<IFileDialogService>(), |
| 687 | mDependencyInjector->get<IPlatformChecker>(), |
| 688 | mDependencyInjector->get<IPluginFinder>(), |
| 689 | this); |
| 690 | settingsDialog.exec(); |
| 691 | }); |
| 692 | } |
| 693 | |
| 694 | void MainWindow::showAboutDialog() |
| 695 | { |
| 696 | showDialog([&](){ |
| 697 | AboutDialog aboutDialog(this); |
| 698 | aboutDialog.exec(); |
| 699 | }); |
| 700 | } |
| 701 | |
| 702 | void MainWindow::showScaleDialog() |
| 703 | { |
| 704 | showDialog([&](){ |
| 705 | mImageAnnotator->showScaler(); |
| 706 | }); |
| 707 | } |
| 708 | |
| 709 | void MainWindow::showRotateDialog() |
| 710 | { |
| 711 | showDialog([&](){ |
| 712 | mImageAnnotator->showRotator(); |
| 713 | }); |
| 714 | } |
| 715 | |
| 716 | void MainWindow::pasteFromClipboard() |
| 717 | { |
| 718 | auto pixmap = mClipboard->pixmap(); |
| 719 | |
| 720 | if (mClipboard->url().isNull()) { |
| 721 | loadImageFromPixmap(pixmap); |
| 722 | } else if (!pixmap.isNull()) { |
| 723 | CaptureFromFileDto captureDto(pixmap, mClipboard->url()); |
| 724 | processImage(captureDto); |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | void MainWindow::pasteEmbeddedFromClipboard() |
| 729 | { |
| 730 | auto pixmap = mClipboard->pixmap(); |
| 731 | mCaptureHandler->insertImageItem({}, pixmap); |
| 732 | } |
| 733 | |
| 734 | void MainWindow::saveClicked() |
| 735 | { |
| 736 | mCaptureHandler->save(); |
| 737 | } |
| 738 | |
| 739 | void MainWindow::saveAsClicked() |
| 740 | { |
| 741 | mCaptureHandler->saveAs(); |
| 742 | } |
| 743 | |
| 744 | void MainWindow::saveAllClicked() |
| 745 | { |
| 746 | mCaptureHandler->saveAll(); |
| 747 | } |
| 748 | |
| 749 | void MainWindow::loadImageFromFile(const QString &path) |
| 750 | { |
| 751 | LoadImageFromFileOperation operation( |
| 752 | path, |
| 753 | this, |
| 754 | mNotificationService, |
| 755 | mDependencyInjector->get<IRecentImageService>(), |
| 756 | mDependencyInjector->get<IFileService>(), |
| 757 | mConfig); |
| 758 | operation.execute(); |
| 759 | } |
| 760 | |
| 761 | void MainWindow::loadImageFromPixmap(const QPixmap &pixmap) |
| 762 | { |
| 763 | if (!pixmap.isNull()) { |
| 764 | CaptureDto captureDto(pixmap); |
| 765 | processImage(captureDto); |
| 766 | } else { |
| 767 | qWarning("Provided pixmap is not valid."); |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | void MainWindow::sessionFinished() |
| 772 | { |
| 773 | mSessionManagerRequestedQuit = true; |
| 774 | } |
| 775 | |
| 776 | void MainWindow::captureCanceled() |
| 777 | { |
| 778 | mVisibilityHandler->restoreState(); |
| 779 | } |
| 780 | |
| 781 | void MainWindow::uploadFinished(const UploadResult &result) |
| 782 | { |
| 783 | HandleUploadResultOperation handleUploadResponseOperation(result, mNotificationService, mClipboard, mDependencyInjector->get<IDesktopService>(), mConfig); |
| 784 | handleUploadResponseOperation.execute(); |
| 785 | } |
| 786 | |
| 787 | void MainWindow::captureEmpty() |
| 788 | { |
| 789 | setEnablements(false); |
| 790 | mWindowResizer->resetAndResize(); |
| 791 | |
| 792 | if (isWindowMaximized()) { |
| 793 | mResizeOnNormalize = true; |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | void MainWindow::showPinWindow() |
| 798 | { |
| 799 | mPinWindowHandler->add(QPixmap::fromImage(mCaptureHandler->image())); |
| 800 | } |
| 801 | |
| 802 | void MainWindow::actionTriggered(const Action &action) |
| 803 | { |
| 804 | if(action.isCaptureEnabled() && !mCaptureHandler->canTakeNew()){ |
| 805 | return; |
| 806 | } |
| 807 | |
| 808 | mActionProcessor->process(action); |
| 809 | } |
| 810 | |
| 811 | void MainWindow::showAfterAction(bool minimized) |
| 812 | { |
| 813 | if (minimized && mTrayIcon->isVisible()) { |
| 814 | mVisibilityHandler->hide(); |
| 815 | } else if (minimized) { |
| 816 | mVisibilityHandler->minimize(); |
| 817 | mVisibilityHandler->restoreState(); |
| 818 | } else { |
| 819 | mVisibilityHandler->restoreState(); |
| 820 | } |
| 821 | |
| 822 | mWindowResizer->resize(); |
| 823 | } |
| 824 | |
| 825 | void MainWindow::showOcrWindow() |
| 826 | { |
| 827 | mOcrWindowHandler->add(QPixmap::fromImage(mCaptureHandler->image())); |
| 828 | } |