MachineAction.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 PyQt5.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. labelChanged = pyqtSignal()
  28. onFinished = pyqtSignal()
  29. def getKey(self) -> str:
  30. return self._key
  31. def needsUserInteraction(self) -> bool:
  32. """Whether this action needs to ask the user anything.
  33. If not, we shouldn't present the user with certain screens which otherwise show up.
  34. :return: Defaults to true to be in line with the old behaviour.
  35. """
  36. return True
  37. @pyqtProperty(str, notify = labelChanged)
  38. def label(self) -> str:
  39. return self._label
  40. def setLabel(self, label: str) -> None:
  41. if self._label != label:
  42. self._label = label
  43. self.labelChanged.emit()
  44. @pyqtSlot()
  45. def reset(self) -> None:
  46. """Reset the action to it's default state.
  47. This should not be re-implemented by child classes, instead re-implement _reset.
  48. :py:meth:`cura.MachineAction.MachineAction._reset`
  49. """
  50. self._finished = False
  51. self._reset()
  52. def _reset(self) -> None:
  53. """Protected implementation of reset.
  54. See also :py:meth:`cura.MachineAction.MachineAction.reset`
  55. """
  56. pass
  57. @pyqtSlot()
  58. def setFinished(self) -> None:
  59. self._finished = True
  60. self._reset()
  61. self.onFinished.emit()
  62. @pyqtProperty(bool, notify = onFinished)
  63. def finished(self) -> bool:
  64. return self._finished
  65. def _createViewFromQML(self) -> Optional["QObject"]:
  66. """Protected helper to create a view object based on provided QML."""
  67. plugin_path = PluginRegistry.getInstance().getPluginPath(self.getPluginId())
  68. if plugin_path is None:
  69. Logger.log("e", "Cannot create QML view: cannot find plugin path for plugin [%s]", self.getPluginId())
  70. return None
  71. path = os.path.join(plugin_path, self._qml_url)
  72. from cura.CuraApplication import CuraApplication
  73. view = CuraApplication.getInstance().createQmlComponent(path, {"manager": self})
  74. return view
  75. @pyqtProperty(QUrl, constant = True)
  76. def qmlPath(self) -> "QUrl":
  77. plugin_path = PluginRegistry.getInstance().getPluginPath(self.getPluginId())
  78. if plugin_path is None:
  79. Logger.log("e", "Cannot create QML view: cannot find plugin path for plugin [%s]", self.getPluginId())
  80. return QUrl("")
  81. path = os.path.join(plugin_path, self._qml_url)
  82. return QUrl.fromLocalFile(path)
  83. @pyqtSlot(result = QObject)
  84. def getDisplayItem(self) -> Optional["QObject"]:
  85. return self._createViewFromQML()