Lomiri
Loading...
Searching...
No Matches
ItemGrabber.qml
1/*
2 * Copyright (C) 2014-2016 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 */
16
17import QtQuick 2.15
18import ScreenshotDirectory 0.1
19
20/*
21 Captures an image of the given item and saves it in a screenshots directory.
22 It also displays a flash visual effect and camera shutter sound, as feedback
23 to the user to hint that a screenshot was taken.
24 */
25Rectangle {
26 id: root
27 visible: false
28 color: "white"
29 opacity: 0.0
30
31 signal snapshotTaken(string path)
32
33 ScreenshotDirectory {
34 id: screenshotDirectory
35 objectName: "screenGrabber"
36 }
37
38 NotificationAudio {
39 id: shutterSound
40 source: "/usr/share/sounds/lomiri/camera/click/camera_click.ogg"
41 }
42
43 function capture(item) {
44 // Disallow spamming the lightdm home directory with screenshots
45 // without access to remove them easily.
46 if (!root.enabled) {
47 console.log("Taking screenshots is blocked.");
48 return;
49 }
50
51 d.target = item;
52 visible = true;
53 shutterSound.stop();
54 shutterSound.play();
55 fadeIn.start();
56 }
57
58 NumberAnimation on opacity {
59 id: fadeIn
60 from: 0.0
61 to: 1.0
62 onStopped: {
63 if (visible) {
64 fadeOut.start();
65 }
66 }
67 }
68
69 QtObject {
70 id: d
71 property Item target
72 }
73
74 NumberAnimation on opacity {
75 id: fadeOut
76 from: 1.0
77 to: 0.0
78 onStopped: {
79 if (visible) {
80 d.target.grabToImage(function(result)
81 {
82 const fileName = screenshotDirectory.makeFileName();
83 if (fileName.length === 0) {
84 console.warn("ItemGrabber: No fileName to save image to");
85 return;
86 }
87
88 console.log("ItemGrabber: Saving image to " + fileName);
89 const success = result.saveToFile(fileName);
90 if (!success) {
91 console.log("Failed to save image.");
92 return;
93 }
94
95 root.snapshotTaken(fileName);
96 });
97
98 visible = false;
99 }
100 }
101 }
102}