MachineActionManager.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import TYPE_CHECKING, Optional, List, Set, Dict
  4. from PyQt6.QtCore import QObject
  5. from UM.FlameProfiler import pyqtSlot
  6. from UM.Logger import Logger
  7. from UM.PluginRegistry import PluginRegistry # So MachineAction can be added as plugin type
  8. if TYPE_CHECKING:
  9. from cura.CuraApplication import CuraApplication
  10. from cura.Settings.GlobalStack import GlobalStack
  11. from cura.MachineAction import MachineAction
  12. class UnknownMachineActionError(Exception):
  13. """Raised when trying to add an unknown machine action as a required action"""
  14. pass
  15. class NotUniqueMachineActionError(Exception):
  16. """Raised when trying to add a machine action that does not have an unique key."""
  17. pass
  18. class MachineActionManager(QObject):
  19. def __init__(self, application: "CuraApplication", parent: Optional["QObject"] = None) -> None:
  20. super().__init__(parent = parent)
  21. self._application = application
  22. self._container_registry = self._application.getContainerRegistry()
  23. # Keeps track of which machines have already been processed so we don't do that again.
  24. self._definition_ids_with_default_actions_added = set() # type: Set[str]
  25. # Dict of all known machine actions
  26. self._machine_actions = {} # type: Dict[str, MachineAction]
  27. # Dict of all required actions by definition ID
  28. self._required_actions = {} # type: Dict[str, List[MachineAction]]
  29. # Dict of all supported actions by definition ID
  30. self._supported_actions = {} # type: Dict[str, List[MachineAction]]
  31. # Dict of all actions that need to be done when first added by definition ID
  32. self._first_start_actions = {} # type: Dict[str, List[MachineAction]]
  33. def initialize(self) -> None:
  34. # Add machine_action as plugin type
  35. PluginRegistry.addType("machine_action", self.addMachineAction)
  36. # Adds all default machine actions that are defined in the machine definition for the given machine.
  37. def addDefaultMachineActions(self, global_stack: "GlobalStack") -> None:
  38. definition_id = global_stack.definition.getId()
  39. if definition_id in self._definition_ids_with_default_actions_added:
  40. Logger.log("i", "Default machine actions have been added for machine definition [%s], do nothing.",
  41. definition_id)
  42. return
  43. supported_actions = global_stack.getMetaDataEntry("supported_actions", [])
  44. for action_key in supported_actions:
  45. self.addSupportedAction(definition_id, action_key)
  46. required_actions = global_stack.getMetaDataEntry("required_actions", [])
  47. for action_key in required_actions:
  48. self.addRequiredAction(definition_id, action_key)
  49. first_start_actions = global_stack.getMetaDataEntry("first_start_actions", [])
  50. for action_key in first_start_actions:
  51. self.addFirstStartAction(definition_id, action_key)
  52. self._definition_ids_with_default_actions_added.add(definition_id)
  53. Logger.log("i", "Default machine actions added for machine definition [%s]", definition_id)
  54. def addRequiredAction(self, definition_id: str, action_key: str) -> None:
  55. """Add a required action to a machine
  56. Raises an exception when the action is not recognised.
  57. """
  58. if action_key in self._machine_actions:
  59. if definition_id in self._required_actions:
  60. if self._machine_actions[action_key] not in self._required_actions[definition_id]:
  61. self._required_actions[definition_id].append(self._machine_actions[action_key])
  62. else:
  63. self._required_actions[definition_id] = [self._machine_actions[action_key]]
  64. else:
  65. raise UnknownMachineActionError("Action %s, which is required for %s is not known." % (action_key, definition_id))
  66. def addSupportedAction(self, definition_id: str, action_key: str) -> None:
  67. """Add a supported action to a machine."""
  68. if action_key in self._machine_actions:
  69. if definition_id in self._supported_actions:
  70. if self._machine_actions[action_key] not in self._supported_actions[definition_id]:
  71. self._supported_actions[definition_id].append(self._machine_actions[action_key])
  72. else:
  73. self._supported_actions[definition_id] = [self._machine_actions[action_key]]
  74. else:
  75. Logger.log("w", "Unable to add %s to %s, as the action is not recognised", action_key, definition_id)
  76. def addFirstStartAction(self, definition_id: str, action_key: str) -> None:
  77. """Add an action to the first start list of a machine."""
  78. if action_key in self._machine_actions:
  79. if definition_id in self._first_start_actions:
  80. self._first_start_actions[definition_id].append(self._machine_actions[action_key])
  81. else:
  82. self._first_start_actions[definition_id] = [self._machine_actions[action_key]]
  83. else:
  84. Logger.log("w", "Unable to add %s to %s, as the action is not recognised", action_key, definition_id)
  85. def addMachineAction(self, action: "MachineAction") -> None:
  86. """Add a (unique) MachineAction
  87. if the Key of the action is not unique, an exception is raised.
  88. """
  89. if action.getKey() not in self._machine_actions:
  90. self._machine_actions[action.getKey()] = action
  91. else:
  92. raise NotUniqueMachineActionError("MachineAction with key %s was already added. Actions must have unique keys.", action.getKey())
  93. @pyqtSlot(str, result = "QVariantList")
  94. def getSupportedActions(self, definition_id: str) -> List["MachineAction"]:
  95. """Get all actions supported by given machine
  96. :param definition_id: The ID of the definition you want the supported actions of
  97. :returns: set of supported actions.
  98. """
  99. if definition_id in self._supported_actions:
  100. return list(self._supported_actions[definition_id])
  101. else:
  102. return list()
  103. def getRequiredActions(self, definition_id: str) -> List["MachineAction"]:
  104. """Get all actions required by given machine
  105. :param definition_id: The ID of the definition you want the required actions of
  106. :returns: set of required actions.
  107. """
  108. if definition_id in self._required_actions:
  109. return self._required_actions[definition_id]
  110. else:
  111. return list()
  112. @pyqtSlot(str, result = "QVariantList")
  113. def getFirstStartActions(self, definition_id: str) -> List["MachineAction"]:
  114. """Get all actions that need to be performed upon first start of a given machine.
  115. Note that contrary to required / supported actions a list is returned (as it could be required to run the same
  116. action multiple times).
  117. :param definition_id: The ID of the definition that you want to get the "on added" actions for.
  118. :returns: List of actions.
  119. """
  120. if definition_id in self._first_start_actions:
  121. return self._first_start_actions[definition_id]
  122. else:
  123. return []
  124. def removeMachineAction(self, action: "MachineAction") -> None:
  125. """Remove Machine action from manager
  126. :param action: to remove
  127. """
  128. try:
  129. del self._machine_actions[action.getKey()]
  130. except KeyError:
  131. Logger.log("w", "Trying to remove MachineAction (%s) that was already removed", action.getKey())
  132. def getMachineAction(self, key: str) -> Optional["MachineAction"]:
  133. """Get MachineAction by key
  134. :param key: String of key to select
  135. :return: Machine action if found, None otherwise
  136. """
  137. if key in self._machine_actions:
  138. return self._machine_actions[key]
  139. else:
  140. return None