MachineAction.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Copyright (c) 2016 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. from PyQt5.QtCore import QObject, pyqtSlot, pyqtProperty, pyqtSignal, QUrl
  4. from PyQt5.QtQml import QQmlComponent, QQmlContext
  5. from UM.PluginObject import PluginObject
  6. from UM.PluginRegistry import PluginRegistry
  7. from UM.Application import Application
  8. import os
  9. class MachineAction(QObject, PluginObject):
  10. def __init__(self, key, label = ""):
  11. super().__init__()
  12. self._key = key
  13. self._label = label
  14. self._qml_url = ""
  15. self._component = None
  16. self._context = None
  17. self._view = None
  18. labelChanged = pyqtSignal()
  19. def getKey(self):
  20. return self._key
  21. @pyqtProperty(str, notify = labelChanged)
  22. def label(self):
  23. return self._label
  24. def setLabel(self, label):
  25. if self._label != label:
  26. self._label = label
  27. self.labelChanged.emit()
  28. @pyqtSlot()
  29. def execute(self):
  30. self._execute()
  31. def _execute(self):
  32. raise NotImplementedError("Execute() must be implemented")
  33. def _createViewFromQML(self):
  34. path = QUrl.fromLocalFile(
  35. os.path.join(PluginRegistry.getInstance().getPluginPath(self.getPluginId()), self._qml_url))
  36. self._component = QQmlComponent(Application.getInstance()._engine, path)
  37. self._context = QQmlContext(Application.getInstance()._engine.rootContext())
  38. self._context.setContextProperty("manager", self)
  39. self._view = self._component.create(self._context)
  40. @pyqtProperty(QObject, constant = True)
  41. def displayItem(self):
  42. if not self._component:
  43. self._createViewFromQML()
  44. return self._view