on xenial help sign in
implement taking screenshots on desktop using PrtScr key
9216c20Lukáš Tinkl
plugins/ScreenGrabber/screengrabber.cpp+7−8
@@ -1,5 +1,5 @@
1/*
1/*
2− * Copyright (C) 2014 Canonical, Ltd.
2+ * Copyright (C) 2014-2015 Canonical, Ltd.
3 *
3 *
4 * This program is free software; you can redistribute it and/or modify
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
5 * it under the terms of the GNU General Public License as published by
@@ -28,18 +28,17 @@
28
28
29#include <QDebug>
29#include <QDebug>
30
30
31−void saveScreenshot(QImage screenshot, QString filename, QString format, int quality)
31+void saveScreenshot(QImage screenshot, const QString &filename, const QString &format, int quality)
32{
32{
33 if (!screenshot.save(filename, format.toLatin1().data(), quality))
33 if (!screenshot.save(filename, format.toLatin1().data(), quality))
34 qWarning() << "ScreenShotter: failed to save snapshot!";
34 qWarning() << "ScreenShotter: failed to save snapshot!";
35}
35}
36
36
37ScreenGrabber::ScreenGrabber(QObject *parent)
37ScreenGrabber::ScreenGrabber(QObject *parent)
38− : QObject(parent),
38+ : QObject(parent)
39− screenshotQuality(0)
40{
39{
41− QDir screenshotsDir(QStandardPaths::displayName(QStandardPaths::PicturesLocation));
40+ QDir screenshotsDir(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation));
42− screenshotsDir.mkdir("Screenshots");
41+ screenshotsDir.mkpath("Screenshots");
43 screenshotsDir.cd("Screenshots");
42 screenshotsDir.cd("Screenshots");
44 if (screenshotsDir.exists())
43 if (screenshotsDir.exists())
45 {
44 {
@@ -78,7 +77,7 @@ void ScreenGrabber::captureAndSave()
78 QtConcurrent::run(saveScreenshot, screenshot, makeFileName(), getFormat(), screenshotQuality);
77 QtConcurrent::run(saveScreenshot, screenshot, makeFileName(), getFormat(), screenshotQuality);
79}
78}
80
79
81−QString ScreenGrabber::makeFileName()
80+QString ScreenGrabber::makeFileName() const
82{
81{
83 QString fileName(fileNamePrefix);
82 QString fileName(fileNamePrefix);
84 fileName.append(QDateTime::currentDateTime().toString("yyyyMMdd_hhmmsszzz"));
83 fileName.append(QDateTime::currentDateTime().toString("yyyyMMdd_hhmmsszzz"));
@@ -87,7 +86,7 @@ QString ScreenGrabber::makeFileName()
87 return fileName;
86 return fileName;
88}
87}
89
88
90−QString ScreenGrabber::getFormat()
89+QString ScreenGrabber::getFormat() const
91{
90{
92 //TODO: This should be configurable (perhaps through gsettings?)
91 //TODO: This should be configurable (perhaps through gsettings?)
93 return "png";
92 return "png";
plugins/ScreenGrabber/screengrabber.h+4−3
@@ -28,15 +28,16 @@ class ScreenGrabber: public QObject
28
28
29public:
29public:
30 explicit ScreenGrabber(QObject *parent = 0);
30 explicit ScreenGrabber(QObject *parent = 0);
31+ ~ScreenGrabber() = default;
31
32
32public Q_SLOTS:
33public Q_SLOTS:
33 void captureAndSave();
34 void captureAndSave();
34
35
35private:
36private:
36− QString makeFileName();
37+ QString makeFileName() const;
37− QString getFormat();
38+ QString getFormat() const;
38 QString fileNamePrefix;
39 QString fileNamePrefix;
39− int screenshotQuality;
40+ int screenshotQuality = -1; // default quality for the format
40};
41};
41
42
42#endif
43#endif
qml/Components/PhysicalKeysMapper.qml+2
@@ -93,6 +93,8 @@ Item {
93 }
93 }
94 d.volumeUpKeyPressed = true;
94 d.volumeUpKeyPressed = true;
95 }
95 }
96+ } else if (event.key == Qt.Key_Print) {
97+ root.screenshotTriggered();
96 }
98 }
97 }
99 }
98
100
qml/Components/ScreenGrabber.qml+1−5
@@ -1,5 +1,5 @@
1/*
1/*
2− * Copyright (C) 2014 Canonical, Ltd.
2+ * Copyright (C) 2014-2015 Canonical, Ltd.
3 *
3 *
4 * This program is free software; you can redistribute it and/or modify
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
5 * it under the terms of the GNU General Public License as published by
@@ -20,7 +20,6 @@ import ScreenGrabber 0.1
20
20
21Rectangle {
21Rectangle {
22 id: root
22 id: root
23− enabled: true
24 visible: false
23 visible: false
25 color: "white"
24 color: "white"
26 anchors.fill: parent
25 anchors.fill: parent
@@ -38,9 +37,6 @@ Rectangle {
38 }
37 }
39
38
40 function capture() {
39 function capture() {
41− if (!enabled)
42− return;
43−
44 visible = true;
40 visible = true;
45 shutterSound.stop();
41 shutterSound.stop();
46 shutterSound.play();
42 shutterSound.play();
qml/Shell.qml−1
@@ -188,7 +188,6 @@ Item {
188 ScreenGrabber {
188 ScreenGrabber {
189 id: screenGrabber
189 id: screenGrabber
190 z: dialogs.z + 10
190 z: dialogs.z + 10
191− enabled: Powerd.status === Powerd.On
192 }
191 }
193
192
194 Binding {
193 Binding {