on xenial help sign in
implement taking screenshots on desktop using PrtScr key
commit9216c209a0d3b51025322f3252c9511ccb9e4944 authorLukáš Tinkl <lukas.tinkl@canonical.com> dateWed 15 Jul 2015, 13:45 (11 years ago) parente0d72bc
5 files changed+14−17
plugins/ScreenGrabber/screengrabber.cpp+7−8 plugins/ScreenGrabber/screengrabber.h+4−3 qml/Components/PhysicalKeysMapper.qml+2 qml/Components/ScreenGrabber.qml+1−5 qml/Shell.qml−1
plugins/ScreenGrabber/screengrabber.cpp+7−8
@@ -1,5 +1,5 @@
11/*
2− * Copyright (C) 2014 Canonical, Ltd.
2+ * Copyright (C) 2014-2015 Canonical, Ltd.
33 *
44 * This program is free software; you can redistribute it and/or modify
55 * it under the terms of the GNU General Public License as published by
@@ -28,18 +28,17 @@
2828
2929#include <QDebug>
3030
31−void saveScreenshot(QImage screenshot, QString filename, QString format, int quality)
31+void saveScreenshot(QImage screenshot, const QString &filename, const QString &format, int quality)
3232{
3333 if (!screenshot.save(filename, format.toLatin1().data(), quality))
3434 qWarning() << "ScreenShotter: failed to save snapshot!";
3535}
3636
3737ScreenGrabber::ScreenGrabber(QObject *parent)
38− : QObject(parent),
39− screenshotQuality(0)
38+ : QObject(parent)
4039{
41− QDir screenshotsDir(QStandardPaths::displayName(QStandardPaths::PicturesLocation));
42− screenshotsDir.mkdir("Screenshots");
40+ QDir screenshotsDir(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation));
41+ screenshotsDir.mkpath("Screenshots");
4342 screenshotsDir.cd("Screenshots");
4443 if (screenshotsDir.exists())
4544 {
@@ -78,7 +77,7 @@ void ScreenGrabber::captureAndSave()
7877 QtConcurrent::run(saveScreenshot, screenshot, makeFileName(), getFormat(), screenshotQuality);
7978}
8079
81−QString ScreenGrabber::makeFileName()
80+QString ScreenGrabber::makeFileName() const
8281{
8382 QString fileName(fileNamePrefix);
8483 fileName.append(QDateTime::currentDateTime().toString("yyyyMMdd_hhmmsszzz"));
@@ -87,7 +86,7 @@ QString ScreenGrabber::makeFileName()
8786 return fileName;
8887}
8988
90−QString ScreenGrabber::getFormat()
89+QString ScreenGrabber::getFormat() const
9190{
9291 //TODO: This should be configurable (perhaps through gsettings?)
9392 return "png";
plugins/ScreenGrabber/screengrabber.h+4−3
@@ -28,15 +28,16 @@ class ScreenGrabber: public QObject
2828
2929public:
3030 explicit ScreenGrabber(QObject *parent = 0);
31+ ~ScreenGrabber() = default;
3132
3233public Q_SLOTS:
3334 void captureAndSave();
3435
3536private:
36− QString makeFileName();
37− QString getFormat();
37+ QString makeFileName() const;
38+ QString getFormat() const;
3839 QString fileNamePrefix;
39− int screenshotQuality;
40+ int screenshotQuality = -1; // default quality for the format
4041};
4142
4243#endif
qml/Components/PhysicalKeysMapper.qml+2
@@ -93,6 +93,8 @@ Item {
9393 }
9494 d.volumeUpKeyPressed = true;
9595 }
96+ } else if (event.key == Qt.Key_Print) {
97+ root.screenshotTriggered();
9698 }
9799 }
98100
qml/Components/ScreenGrabber.qml+1−5
@@ -1,5 +1,5 @@
11/*
2− * Copyright (C) 2014 Canonical, Ltd.
2+ * Copyright (C) 2014-2015 Canonical, Ltd.
33 *
44 * This program is free software; you can redistribute it and/or modify
55 * it under the terms of the GNU General Public License as published by
@@ -20,7 +20,6 @@ import ScreenGrabber 0.1
2020
2121Rectangle {
2222 id: root
23− enabled: true
2423 visible: false
2524 color: "white"
2625 anchors.fill: parent
@@ -38,9 +37,6 @@ Rectangle {
3837 }
3938
4039 function capture() {
41− if (!enabled)
42− return;
43−
4440 visible = true;
4541 shutterSound.stop();
4642 shutterSound.play();
qml/Shell.qml−1
@@ -188,7 +188,6 @@ Item {
188188 ScreenGrabber {
189189 id: screenGrabber
190190 z: dialogs.z + 10
191− enabled: Powerd.status === Powerd.On
192191 }
193192
194193 Binding {