forked from Ultimaker/Cura
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoSave.py
65 lines (47 loc) · 2.28 KB
/
AutoSave.py
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
# Copyright (c) 2021 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher.
from PyQt6.QtCore import QTimer
from typing import Any, TYPE_CHECKING
from UM.Logger import Logger
import time
if TYPE_CHECKING:
from cura.CuraApplication import CuraApplication
class AutoSave:
def __init__(self, application: "CuraApplication") -> None:
self._application = application
self._application.getPreferences().preferenceChanged.connect(self._triggerTimer)
self._global_stack = None
self._application.getPreferences().addPreference("cura/autosave_delay", 1000 * 10)
self._change_timer = QTimer()
self._change_timer.setInterval(int(self._application.getPreferences().getValue("cura/autosave_delay")))
self._change_timer.setSingleShot(True)
self._enabled = True
self._saving = False
def initialize(self) -> None:
# only initialise if the application is created and has started
self._change_timer.timeout.connect(self._onTimeout)
self._application.globalContainerStackChanged.connect(self._onGlobalStackChanged)
self._onGlobalStackChanged()
def _triggerTimer(self, *args: Any) -> None:
if not self._saving:
self._change_timer.start()
def setEnabled(self, enabled: bool) -> None:
self._enabled = enabled
if self._enabled:
self._change_timer.start()
else:
self._change_timer.stop()
def _onGlobalStackChanged(self) -> None:
if self._global_stack:
self._global_stack.propertyChanged.disconnect(self._triggerTimer)
self._global_stack.containersChanged.disconnect(self._triggerTimer)
self._global_stack = self._application.getGlobalContainerStack()
if self._global_stack:
self._global_stack.propertyChanged.connect(self._triggerTimer)
self._global_stack.containersChanged.connect(self._triggerTimer)
def _onTimeout(self) -> None:
self._saving = True # To prevent the save process from triggering another autosave.
save_start_time = time.time()
self._application.saveSettings()
Logger.log("d", "Autosaving preferences, instances and profiles took %s seconds", time.time() - save_start_time)
self._saving = False