-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.qml
executable file
·190 lines (166 loc) · 5.16 KB
/
main.qml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import QtQuick 2.7
import Painter 1.0
PainterPlugin
{
ConfigurationStruct {
id: config
}
// timer to display the popup since WorkerScript cannot be used
// with alg context
Timer
{
id: savePostProcess
repeat: false
interval: 1
onTriggered: {
try {
if (!internal.projectOpen) return
alg.log.info(qsTr("Auto saving project (backup number %1 )...").arg(config.actualFileIndex));
var origPath;
var projectIsSaved = true;
try {
alg.project.url();
}
catch(e) {
projectIsSaved = false
}
if (!projectIsSaved) {
// use config default
if (config.tempFileName === "") {
var date = new Date()
config.tempFileName = date.toLocaleString(Qt.locale(), "dd_MM_yyyy_hh_mm")
}
origPath = config.saveDirectoryPath + config.tempFileName + ".spp"
}
else origPath = alg.fileIO.urlToLocalFile(alg.project.url());
// remove extension
var pathTemplate = origPath.replace(/\.[^\.]*$/, "")
// replace path if needed
if (config.alwaysUseSaveDirectory) {
pathTemplate = pathTemplate.replace(/^.*\//, config.saveDirectoryPath)
}
pathTemplate = alg.fileIO.localFileToUrl(pathTemplate)
alg.project.saveAsCopy(pathTemplate + "_autosave_" + config.actualFileIndex + ".spp")
internal.incrFileIndex()
// close() is disable for the popup, we must affect visible directly
savePopup.visible = false
}
catch(err) {
alg.log.exception(err)
savePopup.visible = false
}
}
}
QtObject {
id: internal
property bool projectOpen: alg.project.isOpen()
readonly property alias saving: savePopup.visible
property bool computing: false
property bool busy: false
property bool startProgress: false
onProjectOpenChanged: {
if (projectOpen) {
reinitRemainingTime()
timer.start()
}
else timer.stop()
}
function incrFileIndex() {
++config.actualFileIndex;
if (config.actualFileIndex >= config.filesNumber) config.actualFileIndex = 0
}
function save() {
if(alg.project.needSaving())
{
alg.log.info(qsTr("Autosaving..."));
savePopup.visible = true
savePostProcess.start()
}
}
function initRemainingTime(toTime) {
internal.startProgress = config.warningTime >= toTime
config.remainingTime = toTime
}
function reinitRemainingTime() {
initRemainingTime(config.interval)
}
function snooze() {
initRemainingTime(config.snooze)
}
function onProjectChange() {
projectOpen = true
config.actualFileIndex = 0
}
}
Timer
{
id: timer
repeat: true
interval: 1000
onTriggered: {
if (!internal.saving && config.remainingTime != 0) --config.remainingTime;
// reinitialize timer if null
if (config.remainingTime <= config.warningTime) {
internal.startProgress = true;
// If computing, wait until computation end and painter is not busy
if (internal.computing || internal.busy) return
if (config.remainingTime == 0) {
internal.save();
internal.reinitRemainingTime()
}
}
}
}
Component.onCompleted:
{
// save button
var snoozeButton = alg.ui.addWidgetToPluginToolBar( "SnoozeButton.qml" )
// bind remaining time and saving values
snoozeButton.remainingTime = Qt.binding(function() { return config.remainingTime })
snoozeButton.startProgress = Qt.binding(function() { return internal.startProgress })
snoozeButton.saving = Qt.binding(function() { return internal.saving })
snoozeButton.progressMaxValue = Qt.binding(function() { return config.warningTime })
snoozeButton.active = Qt.binding(function() { return internal.projectOpen })
// bind button status
snoozeButton.clicked.connect(internal.snooze)
}
onProjectSaved: internal.reinitRemainingTime()
onProjectAboutToClose: {
config.tempFileName = ""
internal.projectOpen = false
}
onProjectOpened: internal.onProjectChange()
onNewProjectCreated: internal.onProjectChange()
onComputationStatusChanged: (isComputing) => {
internal.computing = isComputing
}
onBusyStatusChanged: (busy) => {
internal.busy = busy
}
onConfigure:
{
configDialog.open();
}
ConfigurePanel
{
id: configDialog
onVisibleChanged: function() {
if (this.visible) timer.stop()
else if (internal.projectOpen) timer.restart()
}
onConfigurationChanged: function(interval, filesNumber, snooze, warningTime, saveDirectoryPath, alwaysUseSaveDirectory) {
config.interval = interval
config.filesNumber = filesNumber
if (config.actualFileIndex >= filesNumber) config.actualFileIndex = 0
config.snooze = snooze
config.warningTime = warningTime
config.saveDirectoryPath = saveDirectoryPath
config.alwaysUseSaveDirectory = alwaysUseSaveDirectory
if (config.remainingTime == 0 || config.remainingTime > config.interval)
internal.reinitRemainingTime();
}
}
SavePopup {
id: savePopup
}
}