AutoSave.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # Copyright (c) 2016 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from PyQt5.QtCore import QTimer
  4. from UM.Preferences import Preferences
  5. from UM.Logger import Logger
  6. class AutoSave:
  7. def __init__(self, application):
  8. self._application = application
  9. Preferences.getInstance().preferenceChanged.connect(self._triggerTimer)
  10. self._global_stack = None
  11. Preferences.getInstance().addPreference("cura/autosave_delay", 1000 * 10)
  12. self._change_timer = QTimer()
  13. self._change_timer.setInterval(Preferences.getInstance().getValue("cura/autosave_delay"))
  14. self._change_timer.setSingleShot(True)
  15. self._saving = False
  16. def initialize(self):
  17. # only initialise if the application is created and has started
  18. self._change_timer.timeout.connect(self._onTimeout)
  19. self._application.globalContainerStackChanged.connect(self._onGlobalStackChanged)
  20. self._onGlobalStackChanged()
  21. self._triggerTimer()
  22. def _triggerTimer(self, *args):
  23. if not self._saving:
  24. self._change_timer.start()
  25. def _onGlobalStackChanged(self):
  26. if self._global_stack:
  27. self._global_stack.propertyChanged.disconnect(self._triggerTimer)
  28. self._global_stack.containersChanged.disconnect(self._triggerTimer)
  29. self._global_stack = self._application.getGlobalContainerStack()
  30. if self._global_stack:
  31. self._global_stack.propertyChanged.connect(self._triggerTimer)
  32. self._global_stack.containersChanged.connect(self._triggerTimer)
  33. def _onTimeout(self):
  34. self._saving = True # To prevent the save process from triggering another autosave.
  35. Logger.log("d", "Autosaving preferences, instances and profiles")
  36. self._application.saveSettings()
  37. self._saving = False