Debian Codegraph @

modules/Lomiri/Components/Extras/qml5/PhotoEditor/EditStack.qml

raw
1 /*
2 * Copyright (C) 2014 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 version 3 as
6 * published by the Free Software Foundation.
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.9
18 import Lomiri.Components 1.3
19 import Lomiri.Components.Extras 0.2
20
21 Item {
22 property PhotoData data
23 property bool actionsEnabled: true
24 property var items: []
25 property int level: 0
26 property string editingSessionPath
27 property string currentFile
28 property string originalFile
29 property string pristineFile
30 property bool modified: level > 0 || _revertedInThisSession
31
32 property bool _revertedInThisSession
33 property bool _pristineFileExists
34
35 signal revertRequested
36
37 function startEditingSession(original) {
38 var originalFileName = FileUtils.nameFromPath(original);
39 var baseName = FileUtils.parentDirectory(original) +
40 "/.photo_editing." + originalFileName + ".";
41 editingSessionPath = FileUtils.createTemporaryDirectory(baseName);
42 if (editingSessionPath == "") return false;
43
44 originalFile = original;
45 currentFile = editingSessionPath + "/current";
46
47 pristineFile = FileUtils.parentDirectory(original) +
48 "/.original/" + originalFileName
49 _revertedInThisSession = false;
50 _pristineFileExists = FileUtils.exists(pristineFile)
51
52 FileUtils.copy(originalFile, currentFile)
53
54 items = [createSnapshot(0)];
55 level = 0;
56 return true;
57 }
58
59 function endEditingSession(saveIfModified) {
60 if (saveIfModified && modified) { // file modified
61 // if we don't have a copy of the very first original, create one
62 if (!_pristineFileExists) {
63 FileUtils.createDirectory(FileUtils.parentDirectory(pristineFile));
64 FileUtils.copy(originalFile, pristineFile);
65 } else {
66 // if we reverted to original (and made no other changes)
67 // we don't need to keep the pristine copy around
68 if (_revertedInThisSession && level <= 0) {
69 FileUtils.remove(pristineFile);
70 }
71 }
72
73 FileUtils.copy(currentFile, originalFile); // actually save
74 }
75
76 FileUtils.removeDirectory(editingSessionPath, true); // clear editing cache
77 editingSessionPath = originalFile = pristineFile = currentFile = "";
78 }
79
80 function createSnapshot(name) {
81 var snapshotFile = editingSessionPath + "/edit." + name;
82 FileUtils.copy(currentFile, snapshotFile);
83 return snapshotFile;
84 }
85
86 function restoreSnapshot(name) {
87 var snapshotFile = editingSessionPath + "/edit." + name;
88 FileUtils.copy(snapshotFile, currentFile);
89 data.refreshFromDisk();
90 }
91
92 function checkpoint() {
93 level++;
94 items = items.slice(0, level);
95 items.push(createSnapshot(items.length));
96 }
97
98 function revertToPristine() {
99 if (!FileUtils.exists(pristineFile)) {
100 restoreSnapshot(0);
101 items = items.slice(0, 1);
102 level = 0;
103 } else {
104 FileUtils.copy(pristineFile, currentFile);
105 data.refreshFromDisk();
106 items = [];
107 checkpoint();
108 level = 0;
109 _revertedInThisSession = true;
110 }
111 }
112
113 property Action undoAction: Action {
114 text: i18n.dtr("lomiri-ui-extras", "Undo")
115 iconName: "undo"
116 enabled: items.length > 0 && level > 0 && actionsEnabled
117 onTriggered: restoreSnapshot(--level);
118 }
119
120 property Action redoAction: Action {
121 text: i18n.dtr("lomiri-ui-extras", "Redo")
122 iconName: "redo"
123 enabled: level < items.length - 1 && actionsEnabled
124 onTriggered: restoreSnapshot(++level);
125 }
126
127 property Action revertAction: Action {
128 text: i18n.dtr("lomiri-ui-extras", "Revert to original")
129 iconSource: Qt.resolvedUrl("assets/edit_revert.png")
130 enabled: actionsEnabled &&
131 (level > 0 || (!_revertedInThisSession && _pristineFileExists))
132 onTriggered: revertRequested()
133 }
134 }