| 1 | /* |
| 2 | * Copyright (C) 2020 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 "MultiCaptureHandler.h" |
| 21 | |
| 22 | MultiCaptureHandler::MultiCaptureHandler( |
| 23 | IImageAnnotator *imageAnnotator, |
| 24 | const QSharedPointer<INotificationService> ¬ificationService, |
| 25 | const QSharedPointer<ICaptureTabStateHandler> &captureTabStateHandler, |
| 26 | const QSharedPointer<IConfig> &config, |
| 27 | const QSharedPointer<IClipboard> &clipboard, |
| 28 | const QSharedPointer<IDesktopService> &desktopService, |
| 29 | const QSharedPointer<IFileService> &fileService, |
| 30 | const QSharedPointer<IMessageBoxService> &messageBoxService, |
| 31 | const QSharedPointer<IRecentImageService> &recentImageService, |
| 32 | const QSharedPointer<IImageSaver> &imageSaver, |
| 33 | const QSharedPointer<ISavePathProvider> &savePathProvider, |
| 34 | const QSharedPointer<IIconLoader> &iconLoader, |
| 35 | const QSharedPointer<IFileDialogService> &fileDialogService, |
| 36 | QWidget *parent) : |
| 37 | mImageAnnotator(imageAnnotator), |
| 38 | mNotificationService(notificationService), |
| 39 | mParent(parent), |
| 40 | mCaptureChangeListener(nullptr), |
| 41 | mTabStateHandler(captureTabStateHandler), |
| 42 | mConfig(config), |
| 43 | mClipboard(clipboard), |
| 44 | mDesktopService(desktopService), |
| 45 | mMessageBoxService(messageBoxService), |
| 46 | mFileService(fileService), |
| 47 | mRecentImageService(recentImageService), |
| 48 | mImageSaver(imageSaver), |
| 49 | mSavePathProvider(savePathProvider), |
| 50 | mFileDialogService(fileDialogService), |
| 51 | mSaveContextMenuAction(new TabContextMenuAction(this)), |
| 52 | mSaveAsContextMenuAction(new TabContextMenuAction(this)), |
| 53 | mSaveAllContextMenuAction(new TabContextMenuAction(this)), |
| 54 | mRenameContextMenuAction(new TabContextMenuAction(this)), |
| 55 | mOpenDirectoryContextMenuAction(new TabContextMenuAction(this)), |
| 56 | mCopyPathToClipboardContextMenuAction(new TabContextMenuAction(this)), |
| 57 | mCopyToClipboardContextMenuAction(new TabContextMenuAction(this)), |
| 58 | mDeleteImageContextMenuAction(new TabContextMenuAction(this)), |
| 59 | mContextMenuSeparatorAction(new QAction(this)) |
| 60 | { |
| 61 | connect(mImageAnnotator, &IImageAnnotator::currentTabChanged, mTabStateHandler.data(), &ICaptureTabStateHandler::currentTabChanged); |
| 62 | connect(mImageAnnotator, &IImageAnnotator::tabMoved, mTabStateHandler.data(), &ICaptureTabStateHandler::tabMoved); |
| 63 | connect(mImageAnnotator, &IImageAnnotator::imageChanged, mTabStateHandler.data(), &ICaptureTabStateHandler::currentTabContentChanged); |
| 64 | connect(mTabStateHandler.data(), &ICaptureTabStateHandler::updateTabInfo, mImageAnnotator, &IImageAnnotator::updateTabInfo); |
| 65 | |
| 66 | connect(mImageAnnotator, &IImageAnnotator::imageChanged, this, &MultiCaptureHandler::captureChanged); |
| 67 | connect(mImageAnnotator, &IImageAnnotator::currentTabChanged, this, &MultiCaptureHandler::captureChanged); |
| 68 | connect(mImageAnnotator, &IImageAnnotator::tabCloseRequested, this, &MultiCaptureHandler::tabCloseRequested); |
| 69 | connect(mImageAnnotator, &IImageAnnotator::tabContextMenuOpened, this, &MultiCaptureHandler::updateContextMenuActions); |
| 70 | |
| 71 | connect(mConfig.data(), &IConfig::annotatorConfigChanged, this, &MultiCaptureHandler::annotatorConfigChanged); |
| 72 | |
| 73 | addTabContextMenuActions(iconLoader); |
| 74 | |
| 75 | annotatorConfigChanged(); |
| 76 | } |
| 77 | |
| 78 | bool MultiCaptureHandler::canClose() |
| 79 | { |
| 80 | while (mTabStateHandler->count() != 0) { |
| 81 | int index = mTabStateHandler->currentTabIndex(); |
| 82 | if (!discardChanges(index)) { |
| 83 | return false; |
| 84 | } |
| 85 | removeTab(index); |
| 86 | } |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | bool MultiCaptureHandler::canTakeNew() |
| 91 | { |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | bool MultiCaptureHandler::discardChanges(int index) |
| 96 | { |
| 97 | auto image = mImageAnnotator->imageAt(index); |
| 98 | auto isUnsaved = !mTabStateHandler->isSaved(index); |
| 99 | auto pathToSource = mTabStateHandler->path(index); |
| 100 | auto filename = mTabStateHandler->filename(index); |
| 101 | CanDiscardOperation operation( |
| 102 | image, |
| 103 | isUnsaved, |
| 104 | pathToSource, |
| 105 | filename, |
| 106 | mNotificationService, |
| 107 | mRecentImageService, |
| 108 | mMessageBoxService, |
| 109 | mImageSaver, |
| 110 | mSavePathProvider, |
| 111 | mFileDialogService, |
| 112 | mConfig, |
| 113 | mParent); |
| 114 | return operation.execute(); |
| 115 | } |
| 116 | |
| 117 | void MultiCaptureHandler::removeTab(int currentTabIndex) |
| 118 | { |
| 119 | mImageAnnotator->removeTab(currentTabIndex); |
| 120 | mTabStateHandler->tabRemoved(currentTabIndex); |
| 121 | captureChanged(); |
| 122 | |
| 123 | if(mTabStateHandler->count() == 0) { |
| 124 | mImageAnnotator->hide(); |
| 125 | captureEmpty(); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | bool MultiCaptureHandler::isSaved() const |
| 130 | { |
| 131 | return mTabStateHandler->isSaved(mTabStateHandler->currentTabIndex()); |
| 132 | } |
| 133 | |
| 134 | QString MultiCaptureHandler::path() const |
| 135 | { |
| 136 | return mTabStateHandler->path(mTabStateHandler->currentTabIndex()); |
| 137 | } |
| 138 | |
| 139 | bool MultiCaptureHandler::isPathValid() const |
| 140 | { |
| 141 | return mTabStateHandler->isPathValid(mTabStateHandler->currentTabIndex()); |
| 142 | } |
| 143 | |
| 144 | void MultiCaptureHandler::saveAs() |
| 145 | { |
| 146 | saveAsTab(mTabStateHandler->currentTabIndex()); |
| 147 | } |
| 148 | |
| 149 | void MultiCaptureHandler::save() |
| 150 | { |
| 151 | saveTab(mTabStateHandler->currentTabIndex()); |
| 152 | } |
| 153 | |
| 154 | void MultiCaptureHandler::saveAll() |
| 155 | { |
| 156 | saveAllTabs(); |
| 157 | } |
| 158 | |
| 159 | void MultiCaptureHandler::rename() |
| 160 | { |
| 161 | renameTab(mTabStateHandler->currentTabIndex()); |
| 162 | } |
| 163 | |
| 164 | void MultiCaptureHandler::copy() |
| 165 | { |
| 166 | copyToClipboardTab(mTabStateHandler->currentTabIndex()); |
| 167 | } |
| 168 | |
| 169 | void MultiCaptureHandler::copyPath() |
| 170 | { |
| 171 | copyPathToClipboardTab(mTabStateHandler->currentTabIndex()); |
| 172 | } |
| 173 | |
| 174 | void MultiCaptureHandler::openDirectory() |
| 175 | { |
| 176 | openDirectoryTab(mTabStateHandler->currentTabIndex()); |
| 177 | } |
| 178 | |
| 179 | void MultiCaptureHandler::removeImage() |
| 180 | { |
| 181 | deleteImageTab(mTabStateHandler->currentTabIndex()); |
| 182 | } |
| 183 | |
| 184 | void MultiCaptureHandler::saveAt(int index, bool isInstant) |
| 185 | { |
| 186 | auto image = mImageAnnotator->imageAt(index); |
| 187 | SaveOperation operation( |
| 188 | image, |
| 189 | isInstant, |
| 190 | mTabStateHandler->path(index), |
| 191 | mNotificationService, |
| 192 | mRecentImageService, |
| 193 | mImageSaver, |
| 194 | mSavePathProvider, |
| 195 | mFileDialogService, |
| 196 | mConfig, |
| 197 | mParent); |
| 198 | auto saveResult = operation.execute(); |
| 199 | mTabStateHandler->setSaveState(index, saveResult); |
| 200 | captureChanged(); |
| 201 | } |
| 202 | |
| 203 | void MultiCaptureHandler::load(const CaptureDto &capture) |
| 204 | { |
| 205 | auto path = mPathFromCaptureProvider.pathFrom(capture); |
| 206 | auto isSaved = PathHelper::isPathValid(path); |
| 207 | auto filename = mNewCaptureNameProvider.nextName(path); |
| 208 | auto index = mImageAnnotator->addTab(capture.screenshot, filename, path); |
| 209 | mTabStateHandler->add(index, filename, path, isSaved); |
| 210 | if (capture.isCursorValid()) { |
| 211 | mImageAnnotator->insertImageItem(capture.cursor.position, capture.cursor.image); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | void MultiCaptureHandler::tabCloseRequested(int index) |
| 216 | { |
| 217 | if (!discardChanges(index)) { |
| 218 | return; |
| 219 | } |
| 220 | removeTab(index); |
| 221 | } |
| 222 | |
| 223 | QImage MultiCaptureHandler::image() const |
| 224 | { |
| 225 | return mImageAnnotator->image(); |
| 226 | } |
| 227 | |
| 228 | void MultiCaptureHandler::insertImageItem(const QPointF &pos, const QPixmap &pixmap) |
| 229 | { |
| 230 | mImageAnnotator->insertImageItem(pos, pixmap); |
| 231 | } |
| 232 | |
| 233 | void MultiCaptureHandler::addListener(ICaptureChangeListener *captureChangeListener) |
| 234 | { |
| 235 | mCaptureChangeListener = captureChangeListener; |
| 236 | } |
| 237 | |
| 238 | void MultiCaptureHandler::captureChanged() |
| 239 | { |
| 240 | if(mCaptureChangeListener != nullptr) { |
| 241 | mCaptureChangeListener->captureChanged(); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | void MultiCaptureHandler::captureEmpty() |
| 246 | { |
| 247 | if(mCaptureChangeListener != nullptr) { |
| 248 | mCaptureChangeListener->captureEmpty(); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | void MultiCaptureHandler::annotatorConfigChanged() |
| 253 | { |
| 254 | mImageAnnotator->setTabBarAutoHide(mConfig->autoHideTabs()); |
| 255 | } |
| 256 | |
| 257 | void MultiCaptureHandler::addTabContextMenuActions(const QSharedPointer<IIconLoader> &iconLoader) |
| 258 | { |
| 259 | mSaveContextMenuAction->setText(tr("Save")); |
| 260 | mSaveContextMenuAction->setIcon(iconLoader->loadForTheme(QLatin1String("save.svg"))); |
| 261 | |
| 262 | mSaveAsContextMenuAction->setText(tr("Save As")); |
| 263 | mSaveAsContextMenuAction->setIcon(iconLoader->loadForTheme(QLatin1String("saveAs.svg"))); |
| 264 | |
| 265 | mSaveAllContextMenuAction->setText(tr("Save All")); |
| 266 | mSaveAllContextMenuAction->setIcon(iconLoader->loadForTheme(QLatin1String("save.svg"))); |
| 267 | |
| 268 | mRenameContextMenuAction->setText(tr("Rename")); |
| 269 | |
| 270 | mOpenDirectoryContextMenuAction->setText(tr("Open Directory")); |
| 271 | |
| 272 | mCopyToClipboardContextMenuAction->setText(tr("Copy")); |
| 273 | mCopyToClipboardContextMenuAction->setIcon(iconLoader->loadForTheme(QLatin1String("copy.svg"))); |
| 274 | |
| 275 | mCopyPathToClipboardContextMenuAction->setText(tr("Copy Path")); |
| 276 | |
| 277 | mContextMenuSeparatorAction->setSeparator(true); |
| 278 | |
| 279 | mDeleteImageContextMenuAction->setText(tr("Delete")); |
| 280 | mDeleteImageContextMenuAction->setIcon(iconLoader->loadForTheme(QLatin1String("delete.svg"))); |
| 281 | |
| 282 | connect(mSaveContextMenuAction, &TabContextMenuAction::triggered, this, &MultiCaptureHandler::saveTab); |
| 283 | connect(mSaveAsContextMenuAction, &TabContextMenuAction::triggered, this, &MultiCaptureHandler::saveAsTab); |
| 284 | connect(mSaveAllContextMenuAction, &TabContextMenuAction::triggered, this, &MultiCaptureHandler::saveAllTabs); |
| 285 | connect(mRenameContextMenuAction, &TabContextMenuAction::triggered, this, &MultiCaptureHandler::renameTab); |
| 286 | connect(mOpenDirectoryContextMenuAction, &TabContextMenuAction::triggered, this, &MultiCaptureHandler::openDirectoryTab); |
| 287 | connect(mCopyPathToClipboardContextMenuAction, &TabContextMenuAction::triggered, this, &MultiCaptureHandler::copyPathToClipboardTab); |
| 288 | connect(mCopyToClipboardContextMenuAction, &TabContextMenuAction::triggered, this, &MultiCaptureHandler::copyToClipboardTab); |
| 289 | connect(mDeleteImageContextMenuAction, &TabContextMenuAction::triggered, this, &MultiCaptureHandler::deleteImageTab); |
| 290 | |
| 291 | auto actions = QList<QAction *>{mSaveContextMenuAction, |
| 292 | mSaveAsContextMenuAction, |
| 293 | mSaveAllContextMenuAction, |
| 294 | mRenameContextMenuAction, |
| 295 | mOpenDirectoryContextMenuAction, |
| 296 | mCopyToClipboardContextMenuAction, |
| 297 | mCopyPathToClipboardContextMenuAction, |
| 298 | mContextMenuSeparatorAction, |
| 299 | mDeleteImageContextMenuAction |
| 300 | }; |
| 301 | mImageAnnotator->addTabContextMenuActions(actions); |
| 302 | } |
| 303 | |
| 304 | void MultiCaptureHandler::saveAsTab(int index) |
| 305 | { |
| 306 | saveAt(index, false); |
| 307 | } |
| 308 | |
| 309 | void MultiCaptureHandler::saveTab(int index) |
| 310 | { |
| 311 | saveAt(index, true); |
| 312 | } |
| 313 | |
| 314 | void MultiCaptureHandler::saveAllTabs() |
| 315 | { |
| 316 | int tabIndex = 0; |
| 317 | while (tabIndex < mTabStateHandler->count()) { |
| 318 | if (!mTabStateHandler->isSaved(tabIndex)) { |
| 319 | saveAt(tabIndex, true); |
| 320 | } |
| 321 | ++tabIndex; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | void MultiCaptureHandler::renameTab(int index) |
| 326 | { |
| 327 | RenameOperation operation(mTabStateHandler->path(index), mTabStateHandler->filename(index), mNotificationService, mConfig, mParent); |
| 328 | auto renameResult = operation.execute(); |
| 329 | if(renameResult.isSuccessful) { |
| 330 | mTabStateHandler->renameFile(index, renameResult); |
| 331 | captureChanged(); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | void MultiCaptureHandler::updateContextMenuActions(int index) |
| 336 | { |
| 337 | auto isPathValid = mTabStateHandler->isPathValid(index); |
| 338 | mSaveContextMenuAction->setEnabled(!mTabStateHandler->isSaved(index)); |
| 339 | mRenameContextMenuAction->setEnabled(isPathValid); |
| 340 | mOpenDirectoryContextMenuAction->setEnabled(isPathValid); |
| 341 | mCopyPathToClipboardContextMenuAction->setEnabled(isPathValid); |
| 342 | mDeleteImageContextMenuAction->setEnabled(isPathValid); |
| 343 | } |
| 344 | |
| 345 | void MultiCaptureHandler::openDirectoryTab(int index) |
| 346 | { |
| 347 | auto path = mTabStateHandler->path(index); |
| 348 | mDesktopService->openFile(PathHelper::extractParentDirectory(path)); |
| 349 | } |
| 350 | |
| 351 | void MultiCaptureHandler::copyToClipboardTab(int index) |
| 352 | { |
| 353 | auto image = mImageAnnotator->imageAt(index); |
| 354 | mClipboard->setImage(image); |
| 355 | } |
| 356 | |
| 357 | void MultiCaptureHandler::copyPathToClipboardTab(int index) |
| 358 | { |
| 359 | auto path = mTabStateHandler->path(index); |
| 360 | mClipboard->setText(path); |
| 361 | } |
| 362 | |
| 363 | void MultiCaptureHandler::deleteImageTab(int index) |
| 364 | { |
| 365 | auto path = mTabStateHandler->path(index); |
| 366 | |
| 367 | DeleteImageOperation operation(path, mFileService.data(), mMessageBoxService.data()); |
| 368 | |
| 369 | if(operation.execute()) { |
| 370 | removeTab(index); |
| 371 | } |
| 372 | } |