MachineAction.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # Copyright (c) 2016 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import os
  4. from typing import Optional
  5. from PyQt6.QtCore import QObject, QUrl, pyqtSlot, pyqtProperty, pyqtSignal
  6. from UM.Logger import Logger
  7. from UM.PluginObject import PluginObject
  8. from UM.PluginRegistry import PluginRegistry
  9. class MachineAction(QObject, PluginObject):
  10. """Machine actions are actions that are added to a specific machine type.
  11. Examples of such actions are updating the firmware, connecting with remote devices or doing bed leveling. A
  12. machine action can also have a qml, which should contain a :py:class:`cura.MachineAction.MachineAction` item.
  13. When activated, the item will be displayed in a dialog and this object will be added as "manager" (so all
  14. pyqtSlot() functions can be called by calling manager.func())
  15. """
  16. def __init__(self, key: str, label: str = "") -> None:
  17. """Create a new Machine action.
  18. :param key: unique key of the machine action
  19. :param label: Human readable label used to identify the machine action.
  20. """
  21. super().__init__()
  22. self._key = key
  23. self._label = label
  24. self._qml_url = ""
  25. self._view = None
  26. self._finished = False
  27. self._open_as_dialog = True
  28. self._visible = True
  29. labelChanged = pyqtSignal()
  30. visibilityChanged = pyqtSignal()
  31. onFinished = pyqtSignal()
  32. def getKey(self) -> str:
  33. return self._key
  34. def needsUserInteraction(self) -> bool:
  35. """Whether this action needs to ask the user anything.
  36. If not, we shouldn't present the user with certain screens which otherwise show up.
  37. :return: Defaults to true to be in line with the old behaviour.
  38. """
  39. return True
  40. @pyqtProperty(str, notify = labelChanged)
  41. def label(self) -> str:
  42. return self._label
  43. def setLabel(self, label: str) -> None:
  44. if self._label != label:
  45. self._label = label
  46. self.labelChanged.emit()
  47. @pyqtSlot()
  48. def reset(self) -> None:
  49. """Reset the action to it's default state.
  50. This should not be re-implemented by child classes, instead re-implement _reset.
  51. :py:meth:`cura.MachineAction.MachineAction._reset`
  52. """
  53. self._finished = False
  54. self._reset()
  55. def _reset(self) -> None:
  56. """Protected implementation of reset.
  57. See also :py:meth:`cura.MachineAction.MachineAction.reset`
  58. """
  59. pass
  60. @pyqtSlot()
  61. def execute(self) -> None:
  62. self._execute()
  63. def _execute(self) -> None:
  64. """Protected implementation of execute."""
  65. pass
  66. @pyqtSlot()
  67. def setFinished(self) -> None:
  68. self._finished = True
  69. self._reset()
  70. self.onFinished.emit()
  71. @pyqtProperty(bool, notify = onFinished)
  72. def finished(self) -> bool:
  73. return self._finished
  74. def _createViewFromQML(self) -> Optional["QObject"]:
  75. """Protected helper to create a view object based on provided QML."""
  76. plugin_path = PluginRegistry.getInstance().getPluginPath(self.getPluginId())
  77. if plugin_path is None:
  78. Logger.error(f"Cannot create QML view: cannot find plugin path for plugin {self.getPluginId()}")
  79. return None
  80. path = os.path.join(plugin_path, self._qml_url)
  81. from cura.CuraApplication import CuraApplication
  82. view = CuraApplication.getInstance().createQmlComponent(path, {"manager": self})
  83. return view
  84. @pyqtProperty(QUrl, constant = True)
  85. def qmlPath(self) -> "QUrl":
  86. plugin_path = PluginRegistry.getInstance().getPluginPath(self.getPluginId())
  87. if plugin_path is None:
  88. Logger.error(f"Cannot create QML view: cannot find plugin path for plugin {self.getPluginId()}")
  89. return QUrl("")
  90. path = os.path.join(plugin_path, self._qml_url)
  91. return QUrl.fromLocalFile(path)
  92. @pyqtSlot(result = QObject)
  93. def getDisplayItem(self) -> Optional["QObject"]:
  94. return self._createViewFromQML()
  95. @pyqtProperty(bool, constant=True)
  96. def shouldOpenAsDialog(self) -> bool:
  97. """Whether this action will show a dialog.
  98. If not, the action will directly run the function inside execute().
  99. :return: Defaults to true to be in line with the old behaviour.
  100. """
  101. return self._open_as_dialog
  102. @pyqtSlot()
  103. def setVisible(self, visible: bool) -> None:
  104. if self._visible != visible:
  105. self._visible = visible
  106. self.visibilityChanged.emit()
  107. @pyqtProperty(bool, notify = visibilityChanged)
  108. def visible(self) -> bool:
  109. """Whether this action button will be visible.
  110. Example: Show only when isLoggedIn
  111. :return: Defaults to true to be in line with the old behaviour.
  112. """
  113. return self._visible