MachineAction.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # Copyright (c) 2016 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import os
  4. from PyQt5.QtCore import QObject, pyqtSlot, pyqtProperty, pyqtSignal
  5. from UM.Logger import Logger
  6. from UM.PluginObject import PluginObject
  7. from UM.PluginRegistry import PluginRegistry
  8. ## Machine actions are actions that are added to a specific machine type. Examples of such actions are
  9. # updating the firmware, connecting with remote devices or doing bed leveling. A machine action can also have a
  10. # qml, which should contain a "Cura.MachineAction" item. When activated, the item will be displayed in a dialog
  11. # and this object will be added as "manager" (so all pyqtSlot() functions can be called by calling manager.func())
  12. class MachineAction(QObject, PluginObject):
  13. ## Create a new Machine action.
  14. # \param key unique key of the machine action
  15. # \param label Human readable label used to identify the machine action.
  16. def __init__(self, key: str, label: str = "") -> None:
  17. super().__init__()
  18. self._key = key
  19. self._label = label
  20. self._qml_url = ""
  21. self._view = None
  22. self._finished = False
  23. labelChanged = pyqtSignal()
  24. onFinished = pyqtSignal()
  25. def getKey(self) -> str:
  26. return self._key
  27. @pyqtProperty(str, notify = labelChanged)
  28. def label(self) -> str:
  29. return self._label
  30. def setLabel(self, label: str) -> None:
  31. if self._label != label:
  32. self._label = label
  33. self.labelChanged.emit()
  34. ## Reset the action to it's default state.
  35. # This should not be re-implemented by child classes, instead re-implement _reset.
  36. # /sa _reset
  37. @pyqtSlot()
  38. def reset(self) -> None:
  39. self._finished = False
  40. self._reset()
  41. ## Protected implementation of reset.
  42. # /sa reset()
  43. def _reset(self) -> None:
  44. pass
  45. @pyqtSlot()
  46. def setFinished(self) -> None:
  47. self._finished = True
  48. self._reset()
  49. self.onFinished.emit()
  50. @pyqtProperty(bool, notify = onFinished)
  51. def finished(self) -> bool:
  52. return self._finished
  53. ## Protected helper to create a view object based on provided QML.
  54. def _createViewFromQML(self) -> None:
  55. plugin_path = PluginRegistry.getInstance().getPluginPath(self.getPluginId())
  56. if plugin_path is None:
  57. Logger.log("e", "Cannot create QML view: cannot find plugin path for plugin [%s]", self.getPluginId())
  58. return
  59. path = os.path.join(plugin_path, self._qml_url)
  60. from cura.CuraApplication import CuraApplication
  61. self._view = CuraApplication.getInstance().createQmlComponent(path, {"manager": self})
  62. @pyqtProperty(QObject, constant = True)
  63. def displayItem(self):
  64. if not self._view:
  65. self._createViewFromQML()
  66. return self._view