AutoSave.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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.Logger import Logger
  5. class AutoSave:
  6. def __init__(self, application):
  7. self._application = application
  8. self._application.getPreferences().preferenceChanged.connect(self._triggerTimer)
  9. self._global_stack = None
  10. self._application.getPreferences().addPreference("cura/autosave_delay", 1000 * 10)
  11. self._change_timer = QTimer()
  12. self._change_timer.setInterval(self._application.getPreferences().getValue("cura/autosave_delay"))
  13. self._change_timer.setSingleShot(True)
  14. self._saving = False
  15. def initialize(self):
  16. # only initialise if the application is created and has started
  17. self._change_timer.timeout.connect(self._onTimeout)
  18. self._application.globalContainerStackChanged.connect(self._onGlobalStackChanged)
  19. self._onGlobalStackChanged()
  20. self._triggerTimer()
  21. def _triggerTimer(self, *args):
  22. if not self._saving:
  23. self._change_timer.start()
  24. def _onGlobalStackChanged(self):
  25. if self._global_stack:
  26. self._global_stack.propertyChanged.disconnect(self._triggerTimer)
  27. self._global_stack.containersChanged.disconnect(self._triggerTimer)
  28. self._global_stack = self._application.getGlobalContainerStack()
  29. if self._global_stack:
  30. self._global_stack.propertyChanged.connect(self._triggerTimer)
  31. self._global_stack.containersChanged.connect(self._triggerTimer)
  32. def _onTimeout(self):
  33. self._saving = True # To prevent the save process from triggering another autosave.
  34. Logger.log("d", "Autosaving preferences, instances and profiles")
  35. self._application.saveSettings()
  36. self._saving = False