123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- # Copyright (c) 2016 Ultimaker B.V.
- # Cura is released under the terms of the AGPLv3 or higher.
- from PyQt5.QtCore import QObject, pyqtSlot, pyqtProperty, pyqtSignal, QUrl
- from PyQt5.QtQml import QQmlComponent, QQmlContext
- from UM.PluginObject import PluginObject
- from UM.PluginRegistry import PluginRegistry
- from UM.Application import Application
- import os
- class MachineAction(QObject, PluginObject):
- def __init__(self, key, label = ""):
- super().__init__()
- self._key = key
- self._label = label
- self._qml_url = ""
- self._component = None
- self._context = None
- self._view = None
- labelChanged = pyqtSignal()
- def getKey(self):
- return self._key
- @pyqtProperty(str, notify = labelChanged)
- def label(self):
- return self._label
- def setLabel(self, label):
- if self._label != label:
- self._label = label
- self.labelChanged.emit()
- @pyqtSlot()
- def execute(self):
- self._execute()
- def _execute(self):
- raise NotImplementedError("Execute() must be implemented")
- def _createViewFromQML(self):
- path = QUrl.fromLocalFile(
- os.path.join(PluginRegistry.getInstance().getPluginPath(self.getPluginId()), self._qml_url))
- self._component = QQmlComponent(Application.getInstance()._engine, path)
- self._context = QQmlContext(Application.getInstance()._engine.rootContext())
- self._context.setContextProperty("manager", self)
- self._view = self._component.create(self._context)
- @pyqtProperty(QObject, constant = True)
- def displayItem(self):
- if not self._component:
- self._createViewFromQML()
- return self._view
|