MachineAction.py 3.1 KB

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