Debian Codegraph @

src/gui/captureHandler/SingleCaptureHandler.cpp

raw symbols SCIP
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 "SingleCaptureHandler.h"
21
22 SingleCaptureHandler::SingleCaptureHandler(
23 IImageAnnotator *imageAnnotator,
24 const QSharedPointer<INotificationService> &notificationService,
25 const QSharedPointer<IClipboard> &clipboard,
26 const QSharedPointer<IDesktopService> &desktopService,
27 const QSharedPointer<IFileService> &fileService,
28 const QSharedPointer<IMessageBoxService> &messageBoxService,
29 const QSharedPointer<IRecentImageService> &recentImageService,
30 const QSharedPointer<IImageSaver> &imageSaver,
31 const QSharedPointer<ISavePathProvider> &savePathProvider,
32 const QSharedPointer<IFileDialogService> &fileDialogService,
33 const QSharedPointer<IConfig> &config,
34 QWidget *parent) :
35 mImageAnnotator(imageAnnotator),
36 mNotificationService(notificationService),
37 mClipboard(clipboard),
38 mDesktopService(desktopService),
39 mFileService(fileService),
40 mMessageBoxService(messageBoxService),
41 mRecentImageService(recentImageService),
42 mImageSaver(imageSaver),
43 mSavePathProvider(savePathProvider),
44 mFileDialogService(fileDialogService),
45 mConfig(config),
46 mParent(parent),
47 mCaptureChangeListener(nullptr),
48 mIsSaved(true)
49 {
50 mImageAnnotator->setTabBarAutoHide(true);
51
52 connect(mImageAnnotator, &IImageAnnotator::imageChanged, this, &SingleCaptureHandler::markUnsaved);
53 }
54
55 bool SingleCaptureHandler::canClose()
56 {
57 return discardChanges();
58 }
59
60 bool SingleCaptureHandler::canTakeNew()
61 {
62 return discardChanges();
63 }
64
65 bool SingleCaptureHandler::isSaved() const
66 {
67 return mIsSaved;
68 }
69
70 QString SingleCaptureHandler::path() const
71 {
72 return mPath;
73 }
74
75 bool SingleCaptureHandler::isPathValid() const
76 {
77 return PathHelper::isPathValid(mPath);
78 }
79
80 void SingleCaptureHandler::saveAs()
81 {
82 innerSave(false);
83 }
84
85 void SingleCaptureHandler::save()
86 {
87 innerSave(true);
88 }
89
90 void SingleCaptureHandler::saveAll()
91 {
92 innerSave(true);
93 }
94
95 void SingleCaptureHandler::rename()
96 {
97 RenameOperation operation(mPath, QFileInfo(mPath).fileName(), mNotificationService, mConfig, mParent);
98 const auto renameResult = operation.execute();
99 if (renameResult.isSuccessful) {
100 mPath = renameResult.path;
101 captureChanged();
102 }
103 }
104
105 void SingleCaptureHandler::copy()
106 {
107 auto image = mImageAnnotator->image();
108 mClipboard->setImage(image);
109 }
110
111 void SingleCaptureHandler::copyPath()
112 {
113 mClipboard->setText(mPath);
114 }
115
116 void SingleCaptureHandler::openDirectory()
117 {
118 mDesktopService->openFile(PathHelper::extractParentDirectory(mPath));
119 }
120
121 void SingleCaptureHandler::removeImage()
122 {
123 DeleteImageOperation operation(mPath, mFileService.data(), mMessageBoxService.data());
124 if(operation.execute()){
125 reset();
126 }
127 }
128
129 void SingleCaptureHandler::reset()
130 {
131 mImageAnnotator->hide();
132 mPath = QString();
133 mIsSaved = true;
134 captureEmpty();
135 captureChanged();
136 }
137
138 void SingleCaptureHandler::innerSave(bool isInstant)
139 {
140 auto image = mImageAnnotator->image();
141 SaveOperation operation(
142 image,
143 isInstant,
144 mPath,
145 mNotificationService,
146 mRecentImageService,
147 mImageSaver,
148 mSavePathProvider,
149 mFileDialogService,
150 mConfig,
151 mParent);
152 auto saveResult = operation.execute();
153 mIsSaved = saveResult.isSuccessful;
154 if (mIsSaved) {
155 mPath = saveResult.path;
156 captureChanged();
157 }
158 }
159
160 void SingleCaptureHandler::load(const CaptureDto &capture)
161 {
162 mPath = mPathFromCaptureProvider.pathFrom(capture);
163 mIsSaved = PathHelper::isPathValid(mPath);
164 mImageAnnotator->loadImage(capture.screenshot);
165 if (capture.isCursorValid()) {
166 mImageAnnotator->insertImageItem(capture.cursor.position, capture.cursor.image);
167 }
168 }
169
170 QImage SingleCaptureHandler::image() const
171 {
172 return mImageAnnotator->image();
173 }
174
175 void SingleCaptureHandler::insertImageItem(const QPointF &pos, const QPixmap &pixmap)
176 {
177 mImageAnnotator->insertImageItem(pos, pixmap);
178 }
179
180 void SingleCaptureHandler::addListener(ICaptureChangeListener *captureChangeListener)
181 {
182 mCaptureChangeListener = captureChangeListener;
183 }
184
185 bool SingleCaptureHandler::discardChanges()
186 {
187 auto image = mImageAnnotator->image();
188 auto filename = PathHelper::extractFilename(mPath);
189 CanDiscardOperation operation(
190 image,
191 !mIsSaved,
192 mPath,
193 filename,
194 mNotificationService,
195 mRecentImageService,
196 mMessageBoxService,
197 mImageSaver,
198 mSavePathProvider,
199 mFileDialogService,
200 mConfig,
201 mParent);
202 return operation.execute();
203 }
204
205 void SingleCaptureHandler::captureChanged()
206 {
207 if(mCaptureChangeListener != nullptr) {
208 mCaptureChangeListener->captureChanged();
209 }
210 }
211
212 void SingleCaptureHandler::captureEmpty()
213 {
214 if(mCaptureChangeListener != nullptr) {
215 mCaptureChangeListener->captureEmpty();
216 }
217 }
218
219 void SingleCaptureHandler::markUnsaved()
220 {
221 mIsSaved = false;
222 captureChanged();
223 }