-
Notifications
You must be signed in to change notification settings - Fork 0
/
qsnapper.cpp
executable file
·365 lines (342 loc) · 10.7 KB
/
qsnapper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include "qsnapper.h"
#include <QFileDialog>
#include <QPixmap>
#include <QApplication>
#include <QDesktopWidget>
#include <tbb/parallel_for.h>
#include <atomic>
#include <mutex>
#include <iostream>
#if QT_VERSION >= 0x050000
#include <QScreen>
#endif
#ifdef Q_OS_WIN
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
#include "dbusadaptor.h"
#endif
/*!
* \brief Constructs the snapper, sets image save location.
* \details Checks the global QSettings for if taking pictures is enabled, and
* where they should be saved to.
* If either of these are not in the global QSettings then pictures will not be
* taken.
* \param parent The component's parent widget, used for Qt's Memory
* Management.
*/
QSnapper::QSnapper(QWidget *parent)
: Component(parent), nextWakeup(QDateTime::currentDateTime().addSecs(60))
#ifndef Q_OS_WIN
,
screensaver("org.freedesktop.ScreenSaver", "/ScreenSaver")
#endif
{
QVariant logSetting = settings.value("QSnapper_Enable", false);
canSnap = logSetting.toBool();
QVariant saveDirSetting = settings.value("QSnapper_Directory");
if(saveDirSetting.isValid())
saveDir = saveDirSetting.toString();
QVariant diffSetting = settings.value("QSnapper_Diff", false);
toggleDiffAction = new QAction("Save image of differences", this);
toggleDiffAction->setCheckable(true);
saveDifferenceImage = diffSetting.toBool();
toggleDiffAction->setChecked(saveDifferenceImage);
QVariant lenientSetting = settings.value("QSnapper_Lenient", false);
lenientOption = new QAction("Allow minor differences", this);
lenientOption->setCheckable(true);
lenient = lenientSetting.toBool();
lenientOption->setChecked(lenient);
toggleDiffAction->setEnabled(lenient);
QVariant mutedSetting = settings.value("QSnapper_Muted", true);
muted = mutedSetting.toBool();
muteAction->setChecked(muted);
whenToSpeak.setSingleShot(false);
whenToSpeak.setInterval(60000);
connect(&whenToSpeak, SIGNAL(timeout()), this, SLOT(emitSpeak()));
connect(muteAction, SIGNAL(triggered(bool)), this,
SLOT(setMuteSettings(bool)));
connect(toggleDiffAction, SIGNAL(triggered(bool)), this, SLOT(setDiff(bool)));
emitSpeak();
#ifndef Q_OS_WIN
new QsnapperAdaptor(this);
QDBusConnection dbus = QDBusConnection::sessionBus();
dbus.registerObject("/Snapper", this);
// dbus.registerService("com.coderfrog.qcompanion.speaker");
#endif
whenToSpeak.start();
}
/*!
* \brief Destroys the QSnapper, does nothing interesting.
*/
QSnapper::~QSnapper() {}
/*!
* \brief Gets what to say aloud and notify
* \return The word "Snap"
*/
QString QSnapper::getText() { return "Snap"; }
/*!
* \brief Gets the items the user will use to interact with the component.
*In addition to the default mute, QSnapper also provides the ability to change
*save locations, and toggle taking pictures.
* \return A list of actions that will be inserted into the main application's
*menu.
*/
QList<QAction *> QSnapper::getMenuContents()
{
QList<QAction *> actions;
QAction *changeFolderAction = new QAction("Change Save Location", this);
actions.append(changeFolderAction);
actions += Component::getMenuContents();
QAction *enableLogging = new QAction("Enabled", this);
enableLogging->setCheckable(true);
enableLogging->setChecked(canSnap);
actions.append(enableLogging);
actions.append(lenientOption);
actions.append(toggleDiffAction);
connect(changeFolderAction, SIGNAL(triggered()), this,
SLOT(changeSaveFolder()));
connect(enableLogging, SIGNAL(triggered(bool)), this,
SLOT(enableSnapping(bool)));
connect(lenientOption, SIGNAL(triggered(bool)), this, SLOT(setLenient(bool)));
return actions;
}
/*!
* \brief Returns if the logger can take pictures or not
* \return If the logger is allowed to take pictures.
*/
bool QSnapper::isEnabled() { return canSnap; }
/*!
* \brief Prompts the user to pick which folder the screenshots should be logged
*to.
*This is saved to settings, and persists between runs.
*/
void QSnapper::changeSaveFolder()
{
QFileDialog dialog(this, "Select Folder");
QString dir = dialog.getExistingDirectory(
this, "Open Directory", "~/Pictures", QFileDialog::ShowDirsOnly);
if(!dir.isNull())
{
saveDir = dir;
settings.setValue("QSnapper_Directory", dir);
}
}
/*!
* \brief Sets if pictures should be taken.
* \details Sets if pictures should be taken and logs it in the global QSettings
* \param enable If taking pictures should be enabled.
*/
void QSnapper::enableSnapping(bool enable)
{
canSnap = enable;
settings.setValue("QSnapper_Enable", enable);
}
/*!
* \brief Sets lenient to true or false, based on the checkbox
* \param isLenient if it should be lenient or not
*/
void QSnapper::setLenient(bool isLenient)
{
lenient = isLenient;
settings.setValue("QSnapper_Lenient", isLenient);
toggleDiffAction->setEnabled(lenient);
}
/*!
* \brief Stores the value of the muted checkbox to settings
* \param shouldMute if it should be muted.
*/
void QSnapper::setMuteSettings(bool shouldMute)
{
settings.setValue("QSnapper_Muted", shouldMute);
muted = shouldMute;
}
/*!
* \brief Sets if "Difference" images should be generated, and stores it into
* settings.
* \param enable If difference files should be made.
*/
void QSnapper::setDiff(bool enable)
{
settings.setValue("QSnapper_Diff", enable);
saveDifferenceImage = enable;
}
/*!
* \brief Compares two images, and if they are different enough(1% of the
* screen), returns true
* \details Uses a loop through all the x pixels and the y pixels, and compares
* them. The "X loop" is done in parallel using TBB.
* \param oldImage the old image
* \param newImage the new image, if this returns true, it will be saved.
* \return True if the images differ.
*/
bool QSnapper::imagesDiffer(const QImage oldImage, const QImage newImage)
{
bool exceedsDiffenceLimit = false;
if(oldImage.width() == newImage.width() &&
oldImage.height() == newImage.height())
{
std::atomic_int difference(0);
const int height = oldImage.height();
const int width = oldImage.width();
const int differenceLimit = (height * width) / 100;
tbb::parallel_for(tbb::blocked_range<int>(0, width),
[&](const tbb::blocked_range<int> &range)
{
for(int i = range.begin(); i != range.end(); ++i)
{
for(int j = 0; j < height; ++j)
{
if(exceedsDiffenceLimit)
return;
if(oldImage.pixel(i, j) != newImage.pixel(i, j))
++difference;
if(difference > differenceLimit)
{
exceedsDiffenceLimit = true;
}
}
}
});
}
else
{
return true;
}
return exceedsDiffenceLimit;
}
/*!
* \brief Compares two images, and if they are different enough(1% of the
* screen), returns true. Also saves a new image of where they differ.
* \param oldImage the old image
* \param newImage the new image, if this returns true, it will be saved.
* \param filename the name of the generated difference file
* \return True if the images differ.
*/
bool QSnapper::imagesDiffer(const QImage oldImage, const QImage newImage,
const QString filename)
{
bool exceedsDiffenceLimit = true;
const int height = oldImage.height();
const int width = oldImage.width();
QImage diff(width, height, oldImage.format());
if(width == newImage.width() && height == newImage.height())
{
exceedsDiffenceLimit = false;
std::atomic_int difference(0);
const int differenceLimit = (height * width) / 100;
std::mutex diffMutex;
diff.fill(QColor(00, 0xF2, 0xFF));
tbb::parallel_for(tbb::blocked_range<int>(0, width),
[&](const tbb::blocked_range<int> &range)
{
for(int i = range.begin(); i != range.end(); ++i)
{
for(int j = 0; j < height; ++j)
{
if(oldImage.pixel(i, j) != newImage.pixel(i, j))
{
++difference;
QRgb pixel = newImage.pixel(i, j);
std::lock_guard<std::mutex> l(diffMutex);
diff.setPixel(i, j, pixel);
}
if(difference > differenceLimit)
{
exceedsDiffenceLimit = true;
}
}
}
});
if(!muted)
std::cout << difference << std::endl;
}
else
{
if(!muted)
std::cout << "Different sizes" << std::endl;
diff = newImage;
}
if(!muted || exceedsDiffenceLimit)
{
diff.save(filename);
}
return exceedsDiffenceLimit;
}
/*!
* \brief Returns if the screensaver is running. This is OS-dependent.
* \return If the screensaver is running.
*/
bool QSnapper::screensaverIsActive()
{
#ifdef Q_OS_WIN
bool isActive = false;
SystemParametersInfo(SPI_GETSCREENSAVEACTIVE, 0, &isActive, 0);
return isActive;
#else
QDBusReply<bool> reply = screensaver.call("GetActive");
return reply.isValid() && reply.value();
#endif
}
/*!
* \brief Takes a picture
* \details Checks if it is allowed to check pictures, and if the save directory
* exists, and if so takes a picture.
* If the picture is different from the last one, it gets a name from
* getNextFileName() and saves it.
* It then sets the next time another screen shot should occur.
* If saveDifferenceImage is true, a difference between the current image and
* the previous image is stored instead of a whole copy. This reduces size, and
* makes changes more noticable.
* \return If a picture was taken.
*/
bool QSnapper::snap()
{
if(canSnap && !saveDir.isNull() && QDir(saveDir).exists() &&
!screensaverIsActive())
{
QString saveFileName = getNextFileName();
#if QT_VERSION < 0x050000
QPixmap desktop = QPixmap::grabWindow(QApplication::desktop()->winId());
#else
QPixmap desktop = QGuiApplication::primaryScreen()->grabWindow(
QApplication::desktop()->winId());
#endif
QImage newImage = desktop.toImage();
nextWakeup = QDateTime::currentDateTime().addSecs(60);
if(lenient && saveDifferenceImage &&
imagesDiffer(oldImage, newImage, getNextFileName()))
{
oldImage = newImage;
}
else if((!lenient && oldImage != newImage) ||
(lenient && imagesDiffer(oldImage, newImage)))
{
oldImage = newImage;
oldImage.save(saveFileName);
saveFileName = getNextFileName();
return true;
}
}
return false;
}
/*!
* \brief Gets where the image should be saved next.
* \return A QString comprosed of the path, the current time (yyyyMMddhhmmss)
* and the extension (.jpg, to save space)
*/
QString QSnapper::getNextFileName()
{
return saveDir + '/' +
QDateTime::currentDateTime().toString("yyyyMMddhhmmss") + ".jpg";
}
/*!
* \brief Takes a picture, and if unmuted says "Snap".
*/
void QSnapper::emitSpeak()
{
if(snap() && !muted)
Q_EMIT wantsToSpeak(getText());
else
Q_EMIT wantsToSpeak("");
}