on c034fd3 help sign in
unity8/plugins/ScreenGrabber/screengrabber.cpp
117 lines 3.7 kB history raw
1/*
2 * Copyright (C) 2014-2015 Canonical, Ltd.
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; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17#include "screengrabber.h"
19#include <QDir>
20#include <QDateTime>
21#include <QStandardPaths>
22#include <QTemporaryDir>
23#include <QtGui/QImage>
24#include <QtGui/QGuiApplication>
25#include <QtQuick/QQuickWindow>
27#include <QDebug>
29QString saveScreenshot(const QImage &screenshot, const QString &filename, const QString &format, int quality)
31 if (screenshot.save(filename, format.toLatin1().data(), quality)) {
32 return filename;
33 } else {
34 qWarning() << "ScreenGrabber: failed to save snapshot!";
35 }
36 return QString();
39ScreenGrabber::ScreenGrabber(QObject *parent)
40 : QObject(parent)
42 QObject::connect(&m_watcher,
43 &QFutureWatcher<QString>::finished,
44 this,
45 &ScreenGrabber::onScreenshotSaved);
47 QDir screenshotsDir;
48 if (qEnvironmentVariableIsSet("UNITY_TESTING")) {
49 qDebug() << "Using test environment";
50 QTemporaryDir tDir;
51 tDir.setAutoRemove(false);
52 screenshotsDir = tDir.path();
53 } else {
54 qDebug() << "Using real environment";
55 screenshotsDir = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
56 }
57 screenshotsDir.mkpath(QStringLiteral("Screenshots"));
58 screenshotsDir.cd(QStringLiteral("Screenshots"));
59 if (screenshotsDir.exists()) {
60 fileNamePrefix = screenshotsDir.absolutePath();
61 fileNamePrefix.append("/screenshot");
62 } else {
63 qWarning() << "ScreenGrabber: failed to create directory at: " << screenshotsDir.absolutePath();
64 }
67void ScreenGrabber::captureAndSave(int angle)
69 if (fileNamePrefix.isEmpty())
70 {
71 qWarning() << "ScreenShotter: no directory to save screenshot";
72 return;
73 }
75 const QWindowList windows = QGuiApplication::topLevelWindows();
76 if (windows.empty())
77 {
78 qWarning() << "ScreenShotter: no top level windows found!";
79 return;
80 }
82 QQuickWindow *main_window = qobject_cast<QQuickWindow *>(windows[0]);
83 if (!main_window)
84 {
85 qWarning() << "ScreenShotter: can only take screenshots of QQuickWindows";
86 return;
87 }
89 const QImage screenshot = main_window->grabWindow().transformed(QTransform().rotate(angle));
90 const QString filename = makeFileName();
91 qDebug() << "Saving screenshot to" << filename;
92 QFuture<QString> saveFuture(QtConcurrent::run(saveScreenshot, screenshot, filename, getFormat(), screenshotQuality));
93 m_watcher.setFuture(saveFuture);
96void ScreenGrabber::onScreenshotSaved()
98 const QString filename = m_watcher.future().result();
99 if (!filename.isEmpty()) {
100 Q_EMIT screenshotSaved(filename);
101 }
104QString ScreenGrabber::makeFileName() const
106 QString fileName(fileNamePrefix);
107 fileName.append(QDateTime::currentDateTime().toString(QStringLiteral("yyyyMMdd_hhmmsszzz")));
108 fileName.append(".");
109 fileName.append(getFormat());
110 return fileName;
113QString ScreenGrabber::getFormat() const
115 //TODO: This should be configurable (perhaps through gsettings?)
116 return QStringLiteral("png");