Unity 8
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 
17 import QtQuick 2.4
18 import 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  */
25 Rectangle {
26  id: root
27  visible: false
28  color: "white"
29  opacity: 0.0
30 
31  ScreenshotDirectory {
32  id: screenshotDirectory
33  objectName: "screenGrabber"
34  }
35 
36  NotificationAudio {
37  id: shutterSound
38  source: "/system/media/audio/ui/camera_click.ogg"
39  }
40 
41  function capture(item) {
42  d.target = item;
43  visible = true;
44  shutterSound.stop();
45  shutterSound.play();
46  fadeIn.start();
47  }
48 
49  NumberAnimation on opacity {
50  id: fadeIn
51  from: 0.0
52  to: 1.0
53  onStopped: {
54  if (visible) {
55  fadeOut.start();
56  }
57  }
58  }
59 
60  QtObject {
61  id: d
62  property Item target
63  }
64 
65  NumberAnimation on opacity {
66  id: fadeOut
67  from: 1.0
68  to: 0.0
69  onStopped: {
70  if (visible) {
71  d.target.grabToImage(function(result)
72  {
73  var fileName = screenshotDirectory.makeFileName();
74  if (fileName.length === 0) {
75  console.warn("ItemGrabber: No fileName to save image to");
76  } else {
77  console.log("ItemGrabber: Saving image to " + fileName);
78  result.saveToFile(fileName);
79  }
80  });
81 
82  visible = false;
83  }
84  }
85  }
86 }