Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/Ultimaker/Cura.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLipu Fei <lipu.fei815@gmail.com>2018-07-13 10:16:09 +0300
committerLipu Fei <lipu.fei815@gmail.com>2018-07-13 10:16:11 +0300
commitc0b7e40b0d250818060b6dbaded2a6de0fd6b7e6 (patch)
treec53734bd442be72a8a6028a47f3e434786c552f1 /cura/TaskManagement
parentac3d3bc5c08671a5afee481ea0c8f27e69ba98ad (diff)
Add on-exit callback management and check for active USB printing
CURA-5384
Diffstat (limited to 'cura/TaskManagement')
-rw-r--r--cura/TaskManagement/OnExitCallbackManager.py69
-rw-r--r--cura/TaskManagement/__init__.py0
2 files changed, 69 insertions, 0 deletions
diff --git a/cura/TaskManagement/OnExitCallbackManager.py b/cura/TaskManagement/OnExitCallbackManager.py
new file mode 100644
index 0000000000..9e641a1778
--- /dev/null
+++ b/cura/TaskManagement/OnExitCallbackManager.py
@@ -0,0 +1,69 @@
+# Copyright (c) 2018 Ultimaker B.V.
+# Cura is released under the terms of the LGPLv3 or higher.
+
+from typing import TYPE_CHECKING, List
+
+from UM.Logger import Logger
+
+if TYPE_CHECKING:
+ from cura.CuraApplication import CuraApplication
+
+
+#
+# This class manages a all registered upon-exit checks that need to be perform when the application tries to exit.
+# For example, to show a confirmation dialog when there is USB printing in progress, etc. All callbacks will be called
+# in the order of when they got registered. If all callbacks "passes", that is, for example, if the user clicks "yes"
+# on the exit confirmation dialog or nothing that's blocking the exit, then the application will quit after that.
+#
+class OnExitCallbackManager:
+
+ def __init__(self, application: "CuraApplication") -> None:
+ self._application = application
+ self._on_exit_callback_list = list() # type: List[callable]
+ self._current_callback_idx = 0
+ self._is_all_checks_passed = False
+
+ def addCallback(self, callback: callable) -> None:
+ self._on_exit_callback_list.append(callback)
+ Logger.log("d", "on-app-exit callback [%s] added.", callback)
+
+ # Reset the current state so the next time it will call all the callbacks again.
+ def resetCurrentState(self) -> None:
+ self._current_callback_idx = 0
+ self._is_all_checks_passed = False
+
+ def getIsAllChecksPassed(self) -> bool:
+ return self._is_all_checks_passed
+
+ # Trigger the next callback if available. If not, it means that all callbacks have "passed", which means we should
+ # not block the application to quit, and it will call the application to actually quit.
+ def triggerNextCallback(self) -> None:
+ # Get the next callback and schedule that if
+ this_callback = None
+ if self._current_callback_idx < len(self._on_exit_callback_list):
+ this_callback = self._on_exit_callback_list[self._current_callback_idx]
+ self._current_callback_idx += 1
+
+ if this_callback is not None:
+ Logger.log("d", "Scheduled the next on-app-exit callback [%s]", this_callback)
+ self._application.callLater(this_callback)
+ else:
+ Logger.log("d", "No more on-app-exit callbacks to process. Tell the app to exit.")
+
+ self._is_all_checks_passed = True
+
+ # Tell the application to exit
+ self._application.callLater(self._application.closeApplication)
+
+ # This is the callback function which an on-exit callback should call when it finishes, it should provide the
+ # "should_proceed" flag indicating whether this check has "passed", or in other words, whether quiting the
+ # application should be blocked. If the last on-exit callback doesn't block the quiting, it will call the next
+ # registered on-exit callback if available.
+ def onCurrentCallbackFinished(self, should_proceed: bool = True) -> None:
+ if not should_proceed:
+ Logger.log("d", "on-app-exit callback finished and we should not proceed.")
+ # Reset the state
+ self.resetCurrentState()
+ return
+
+ self.triggerNextCallback()
diff --git a/cura/TaskManagement/__init__.py b/cura/TaskManagement/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/cura/TaskManagement/__init__.py