Browse Source

Only initialise AutoSave after Application finishes its start up

CURA-4895
Lipu Fei 7 years ago
parent
commit
c2aa5fc2a2
2 changed files with 24 additions and 2 deletions
  1. 8 0
      cura/CuraApplication.py
  2. 16 2
      plugins/AutoSave/AutoSave.py

+ 8 - 0
cura/CuraApplication.py

@@ -106,6 +106,14 @@ if not MYPY:
         CuraDebugMode = False
 
 
+#
+# A global signal which is triggered when CuraApplication has finished its start up.
+# This is used to initialise some plugins such as AutoSave which should only be started after the application passed
+# the start up successfully.
+#
+applicationStarted = pyqtSignal()
+
+
 class CuraApplication(QtApplication):
     # SettingVersion represents the set of settings available in the machine/extruder definitions.
     # You need to make sure that this version number needs to be increased if there is any non-backwards-compatible

+ 16 - 2
plugins/AutoSave/AutoSave.py

@@ -9,6 +9,7 @@ from UM.Application import Application
 from UM.Resources import Resources
 from UM.Logger import Logger
 
+
 class AutoSave(Extension):
     def __init__(self):
         super().__init__()
@@ -16,8 +17,6 @@ class AutoSave(Extension):
         Preferences.getInstance().preferenceChanged.connect(self._triggerTimer)
 
         self._global_stack = None
-        Application.getInstance().globalContainerStackChanged.connect(self._onGlobalStackChanged)
-        self._onGlobalStackChanged()
 
         Preferences.getInstance().addPreference("cura/autosave_delay", 1000 * 10)
 
@@ -28,6 +27,21 @@ class AutoSave(Extension):
 
         self._saving = False
 
+        # At this point, the Application instance has not finished its constructor call yet, so directly using something
+        # like Application.getInstance() is not correct. The initialisation now will only gets triggered after the
+        # application finishes its start up successfully.
+        from cura.CuraApplication import applicationStarted
+        applicationStarted.connect(self.initialize)
+
+    def initialize(self):
+        from cura.CuraApplication import applicationStarted
+        applicationStarted.disconnect(self.initialize)
+
+        Application.getInstance().globalContainerStackChanged.connect(self._onGlobalStackChanged)
+        self._onGlobalStackChanged()
+
+        self._triggerTimer()
+
     def _triggerTimer(self, *args):
         if not self._saving:
             self._change_timer.start()