AutoSave.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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.Extension import Extension
  5. from UM.Preferences import Preferences
  6. from UM.Application import Application
  7. from UM.Resources import Resources
  8. from UM.Logger import Logger
  9. class AutoSave(Extension):
  10. def __init__(self):
  11. super().__init__()
  12. Preferences.getInstance().preferenceChanged.connect(self._triggerTimer)
  13. self._global_stack = None
  14. Preferences.getInstance().addPreference("cura/autosave_delay", 1000 * 10)
  15. self._change_timer = QTimer()
  16. self._change_timer.setInterval(Preferences.getInstance().getValue("cura/autosave_delay"))
  17. self._change_timer.setSingleShot(True)
  18. self._saving = False
  19. # At this point, the Application instance has not finished its constructor call yet, so directly using something
  20. # like Application.getInstance() is not correct. The initialisation now will only gets triggered after the
  21. # application finishes its start up successfully.
  22. self._init_timer = QTimer()
  23. self._init_timer.setInterval(1000)
  24. self._init_timer.setSingleShot(True)
  25. self._init_timer.timeout.connect(self.initialize)
  26. self._init_timer.start()
  27. def initialize(self):
  28. # only initialise if the application is created and has started
  29. from cura.CuraApplication import CuraApplication
  30. if not CuraApplication.Created:
  31. self._init_timer.start()
  32. return
  33. if not CuraApplication.getInstance().started:
  34. self._init_timer.start()
  35. return
  36. self._change_timer.timeout.connect(self._onTimeout)
  37. Application.getInstance().globalContainerStackChanged.connect(self._onGlobalStackChanged)
  38. self._onGlobalStackChanged()
  39. self._triggerTimer()
  40. def _triggerTimer(self, *args):
  41. if not self._saving:
  42. self._change_timer.start()
  43. def _onGlobalStackChanged(self):
  44. if self._global_stack:
  45. self._global_stack.propertyChanged.disconnect(self._triggerTimer)
  46. self._global_stack.containersChanged.disconnect(self._triggerTimer)
  47. self._global_stack = Application.getInstance().getGlobalContainerStack()
  48. if self._global_stack:
  49. self._global_stack.propertyChanged.connect(self._triggerTimer)
  50. self._global_stack.containersChanged.connect(self._triggerTimer)
  51. def _onTimeout(self):
  52. self._saving = True # To prevent the save process from triggering another autosave.
  53. Logger.log("d", "Autosaving preferences, instances and profiles")
  54. Application.getInstance().saveSettings()
  55. Preferences.getInstance().writeToFile(Resources.getStoragePath(Resources.Preferences, Application.getInstance().getApplicationName() + ".cfg"))
  56. self._saving = False