MachineAction.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # Copyright (c) 2016 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from PyQt5.QtCore import QObject, pyqtSlot, pyqtProperty, pyqtSignal
  4. from UM.PluginObject import PluginObject
  5. from UM.PluginRegistry import PluginRegistry
  6. from UM.Application import Application
  7. import os
  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, label = ""):
  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):
  26. return self._key
  27. @pyqtProperty(str, notify = labelChanged)
  28. def label(self):
  29. return self._label
  30. def setLabel(self, label):
  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):
  39. self._finished = False
  40. self._reset()
  41. ## Protected implementation of reset.
  42. # /sa reset()
  43. def _reset(self):
  44. pass
  45. @pyqtSlot()
  46. def setFinished(self):
  47. self._finished = True
  48. self._reset()
  49. self.onFinished.emit()
  50. @pyqtProperty(bool, notify = onFinished)
  51. def finished(self):
  52. return self._finished
  53. ## Protected helper to create a view object based on provided QML.
  54. def _createViewFromQML(self):
  55. path = os.path.join(PluginRegistry.getInstance().getPluginPath(self.getPluginId()), self._qml_url)
  56. self._view = Application.getInstance().createQmlComponent(path, {"manager": self})
  57. @pyqtProperty(QObject, constant = True)
  58. def displayItem(self):
  59. if not self._view:
  60. self._createViewFromQML()
  61. return self._view