on xenial help sign in
Use the date for the screenshot file name instead of a sequential number.
692f0f5Alberto Aguirre
plugins/Screenshotter/screenshotter.cpp+28−46
@@ -18,10 +18,8 @@
18
18
19#include "screenshotter.h"
19#include "screenshotter.h"
20
20
21−#include <QUuid>
21+#include <QDir>
22−#include <QChar>
22+#include <QDateTime>
23−#include <QString>
24−#include <QStringList>
25#include <QStandardPaths>
23#include <QStandardPaths>
26#include <QtGui/QGuiApplication>
24#include <QtGui/QGuiApplication>
27#include <QtQuick/QQuickWindow>
25#include <QtQuick/QQuickWindow>
@@ -30,15 +28,30 @@
30
28
31ScreenShotter::ScreenShotter(QObject *parent)
29ScreenShotter::ScreenShotter(QObject *parent)
32 : QObject(parent),
30 : QObject(parent),
33− screenshotsDir(QStandardPaths::displayName(QStandardPaths::PicturesLocation)),
34 screenshotQuality(90)
31 screenshotQuality(90)
35{
32{
33+ QDir screenshotsDir(QStandardPaths::displayName(QStandardPaths::PicturesLocation));
36 screenshotsDir.mkdir("Screenshots");
34 screenshotsDir.mkdir("Screenshots");
37 screenshotsDir.cd("Screenshots");
35 screenshotsDir.cd("Screenshots");
36+ if (screenshotsDir.exists())
37+ {
38+ fileNamePrefix = screenshotsDir.absolutePath();
39+ fileNamePrefix.append("/screenshot");
40+ }
41+ else
42+ {
43+ qWarning() << "ScreenShotter: failed to create directory at: " << screenshotsDir.absolutePath();
44+ }
38}
45}
39
46
40void ScreenShotter::takeScreenshot()
47void ScreenShotter::takeScreenshot()
41{
48{
49+ if (fileNamePrefix.isEmpty())
50+ {
51+ qWarning() << "ScreenShotter: no directory to save screenshot";
52+ return;
53+ }
54+
42 QWindowList windows = QGuiApplication::topLevelWindows();
55 QWindowList windows = QGuiApplication::topLevelWindows();
43 if (windows.empty())
56 if (windows.empty())
44 {
57 {
@@ -54,52 +67,21 @@ void ScreenShotter::takeScreenshot()
54 }
67 }
55
68
56 QImage snapshot = main_window->grabWindow();
69 QImage snapshot = main_window->grabWindow();
57− if (!snapshot.save(generateName(), "JPG", screenshotQuality))
70+ if (!snapshot.save(makeFileName(), getFormat().toLatin1().data(), screenshotQuality))
58 qWarning() << "ScreenShotter: failed to save snapshot!";
71 qWarning() << "ScreenShotter: failed to save snapshot!";
59}
72}
60
73
61−QString ScreenShotter::generateUniqueNum()
74+QString ScreenShotter::makeFileName()
62{
75{
63− // First lets look for existing files using our numbering pattern
64− QStringList nameFilter;
65− nameFilter << "screenshot????.jpg";
66− QStringList fileList = screenshotsDir.entryList(nameFilter, QDir::Files, QDir::Name | QDir::Reversed);
67−
68− if (!fileList.empty())
69− {
70− Q_FOREACH(QString const& fileName, fileList)
76+ QString fileName(fileNamePrefix);
71− {
77+ fileName.append(QDateTime::currentDateTime().toString("yyyymmdd_hhmmsszzz"));
72− // Just the the four digit number
78+ fileName.append(".");
73− QString fileNumber = fileName.mid(10, 4);
79+ fileName.append(getFormat());
74− bool ok = false;
80+ return fileName;
75− int num = fileNumber.toInt(&ok) + 1;
76− if (!ok)
77− continue;
78−
79− // OK it's an actual number
80− if (num <= 9999)
81− {
82− // Produce a string that has the next sequential number, appending 0's when necessary
83− return QString("%1").arg(num, 4, 10, QChar('0'));
84− }
85− else
86− {
87− // 10000 screenshots huh? ummm, well let's start using uuids then...
88− QString uuid = QUuid::createUuid().toString();
89− // Remove the curly brackets
90− return uuid.mid(1, 36);
91− }
92− }
93− }
94− //No files exist using our numbering scheme
95− return QString("0000");
96}
81}
97
82
98−QString ScreenShotter::generateName()
83+QString ScreenShotter::getFormat()
99{
84{
100− QString fileName = screenshotsDir.absolutePath();
85+ //TODO: Maybe this could be configurable
101− fileName.append("/screenshot");
102− fileName.append(generateUniqueNum());
103− fileName.append(".jpg");
104− return fileName;
86+ return "jpg";
105}
87}
plugins/Screenshotter/screenshotter.h+4−4
@@ -20,7 +20,7 @@
20#define SNAPSHOTTER_H
20#define SNAPSHOTTER_H
21
21
22#include <QObject>
22#include <QObject>
23−#include <QDir>
23+#include <QString>
24
24
25class ScreenShotter: public QObject
25class ScreenShotter: public QObject
26{
26{
@@ -33,9 +33,9 @@ public Q_SLOTS:
33 void takeScreenshot();
33 void takeScreenshot();
34
34
35private:
35private:
36− QString generateName();
36+ QString makeFileName();
37− QString generateUniqueNum();
37+ QString getFormat();
38− QDir screenshotsDir;
38+ QString fileNamePrefix;
39 int screenshotQuality;
39 int screenshotQuality;
40};
40};
41
41