MachineActionManager.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # Copyright (c) 2016 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. from UM.Logger import Logger
  4. from UM.PluginRegistry import PluginRegistry # So MachineAction can be added as plugin type
  5. from UM.Settings.ContainerRegistry import ContainerRegistry
  6. from UM.Settings.DefinitionContainer import DefinitionContainer
  7. from PyQt5.QtCore import QObject, pyqtSlot
  8. ## Raised when trying to add an unknown machine action as a required action
  9. class UnknownMachineActionError(Exception):
  10. pass
  11. ## Raised when trying to add a machine action that does not have an unique key.
  12. class NotUniqueMachineActionError(Exception):
  13. pass
  14. class MachineActionManager(QObject):
  15. def __init__(self, parent = None):
  16. super().__init__(parent)
  17. self._machine_actions = {} # Dict of all known machine actions
  18. self._required_actions = {} # Dict of all required actions by definition ID
  19. self._supported_actions = {} # Dict of all supported actions by definition ID
  20. self._first_start_actions = {} # Dict of all actions that need to be done when first added by definition ID
  21. # Add machine_action as plugin type
  22. PluginRegistry.addType("machine_action", self.addMachineAction)
  23. # Ensure that all containers that were registered before creation of this registry are also handled.
  24. # This should not have any effect, but it makes it safer if we ever refactor the order of things.
  25. for container in ContainerRegistry.getInstance().findDefinitionContainers():
  26. self._onContainerAdded(container)
  27. ContainerRegistry.getInstance().containerAdded.connect(self._onContainerAdded)
  28. def _onContainerAdded(self, container):
  29. ## Ensure that the actions are added to this manager
  30. if isinstance(container, DefinitionContainer):
  31. supported_actions = container.getMetaDataEntry("supported_actions", [])
  32. for action in supported_actions:
  33. self.addSupportedAction(container.getId(), action)
  34. required_actions = container.getMetaDataEntry("required_actions", [])
  35. for action in required_actions:
  36. self.addRequiredAction(container.getId(), action)
  37. first_start_actions = container.getMetaDataEntry("first_start_actions", [])
  38. for action in first_start_actions:
  39. self.addFirstStartAction(container.getId(), action)
  40. ## Add a required action to a machine
  41. # Raises an exception when the action is not recognised.
  42. def addRequiredAction(self, definition_id, action_key):
  43. if action_key in self._machine_actions:
  44. if definition_id in self._required_actions:
  45. if self._machine_actions[action_key] not in self._required_actions[definition_id]:
  46. self._required_actions[definition_id].append(self._machine_actions[action_key])
  47. else:
  48. self._required_actions[definition_id] = [self._machine_actions[action_key]]
  49. else:
  50. raise UnknownMachineActionError("Action %s, which is required for %s is not known." % (action_key, definition_id))
  51. ## Add a supported action to a machine.
  52. def addSupportedAction(self, definition_id, action_key):
  53. if action_key in self._machine_actions:
  54. if definition_id in self._supported_actions:
  55. if self._machine_actions[action_key] not in self._supported_actions[definition_id]:
  56. self._supported_actions[definition_id].append(self._machine_actions[action_key])
  57. else:
  58. self._supported_actions[definition_id] = [self._machine_actions[action_key]]
  59. else:
  60. Logger.log("w", "Unable to add %s to %s, as the action is not recognised", action_key, definition_id)
  61. ## Add an action to the first start list of a machine.
  62. def addFirstStartAction(self, definition_id, action_key, index = None):
  63. if action_key in self._machine_actions:
  64. if definition_id in self._first_start_actions:
  65. if index is not None:
  66. self._first_start_actions[definition_id].insert(index, self._machine_actions[action_key])
  67. else:
  68. self._first_start_actions[definition_id].append(self._machine_actions[action_key])
  69. else:
  70. self._first_start_actions[definition_id] = [self._machine_actions[action_key]]
  71. else:
  72. Logger.log("w", "Unable to add %s to %s, as the action is not recognised", action_key, definition_id)
  73. ## Add a (unique) MachineAction
  74. # if the Key of the action is not unique, an exception is raised.
  75. def addMachineAction(self, action):
  76. if action.getKey() not in self._machine_actions:
  77. self._machine_actions[action.getKey()] = action
  78. else:
  79. raise NotUniqueMachineActionError("MachineAction with key %s was already added. Actions must have unique keys.", action.getKey())
  80. ## Get all actions supported by given machine
  81. # \param definition_id The ID of the definition you want the supported actions of
  82. # \returns set of supported actions.
  83. @pyqtSlot(str, result = "QVariantList")
  84. def getSupportedActions(self, definition_id):
  85. if definition_id in self._supported_actions:
  86. return list(self._supported_actions[definition_id])
  87. else:
  88. return set()
  89. ## Get all actions required by given machine
  90. # \param definition_id The ID of the definition you want the required actions of
  91. # \returns set of required actions.
  92. def getRequiredActions(self, definition_id):
  93. if definition_id in self._required_actions:
  94. return self._required_actions[definition_id]
  95. else:
  96. return set()
  97. ## Get all actions that need to be performed upon first start of a given machine.
  98. # Note that contrary to required / supported actions a list is returned (as it could be required to run the same
  99. # action multiple times).
  100. # \param definition_id The ID of the definition that you want to get the "on added" actions for.
  101. # \returns List of actions.
  102. @pyqtSlot(str, result="QVariantList")
  103. def getFirstStartActions(self, definition_id):
  104. if definition_id in self._first_start_actions:
  105. return self._first_start_actions[definition_id]
  106. else:
  107. return []
  108. ## Remove Machine action from manager
  109. # \param action to remove
  110. def removeMachineAction(self, action):
  111. try:
  112. del self._machine_actions[action.getKey()]
  113. except KeyError:
  114. Logger.log("w", "Trying to remove MachineAction (%s) that was already removed", action.getKey())
  115. ## Get MachineAction by key
  116. # \param key String of key to select
  117. # \return Machine action if found, None otherwise
  118. def getMachineAction(self, key):
  119. if key in self._machine_actions:
  120. return self._machine_actions[key]
  121. else:
  122. return None