MachineAction.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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, QUrl
  4. from PyQt5.QtQml import QQmlComponent, QQmlContext
  5. from UM.PluginObject import PluginObject
  6. from UM.PluginRegistry import PluginRegistry
  7. from UM.Logger import Logger
  8. from UM.Application import Application
  9. import os
  10. ## Machine actions are actions that are added to a specific machine type. Examples of such actions are
  11. # updating the firmware, connecting with remote devices or doing bed leveling. A machine action can also have a
  12. # qml, which should contain a "Cura.MachineAction" item. When activated, the item will be displayed in a dialog
  13. # and this object will be added as "manager" (so all pyqtSlot() functions can be called by calling manager.func())
  14. class MachineAction(QObject, PluginObject):
  15. ## Create a new Machine action.
  16. # \param key unique key of the machine action
  17. # \param label Human readable label used to identify the machine action.
  18. def __init__(self, key, label = ""):
  19. super().__init__()
  20. self._key = key
  21. self._label = label
  22. self._qml_url = ""
  23. self._component = None
  24. self._context = None
  25. self._view = None
  26. self._finished = False
  27. labelChanged = pyqtSignal()
  28. onFinished = pyqtSignal()
  29. def getKey(self):
  30. return self._key
  31. @pyqtProperty(str, notify = labelChanged)
  32. def label(self):
  33. return self._label
  34. def setLabel(self, label):
  35. if self._label != label:
  36. self._label = label
  37. self.labelChanged.emit()
  38. ## Reset the action to it's default state.
  39. # This should not be re-implemented by child classes, instead re-implement _reset.
  40. # /sa _reset
  41. @pyqtSlot()
  42. def reset(self):
  43. self._component = None
  44. self._finished = False
  45. self._reset()
  46. ## Protected implementation of reset.
  47. # /sa reset()
  48. def _reset(self):
  49. pass
  50. @pyqtSlot()
  51. def setFinished(self):
  52. self._finished = True
  53. self._reset()
  54. self.onFinished.emit()
  55. @pyqtProperty(bool, notify = onFinished)
  56. def finished(self):
  57. return self._finished
  58. ## Protected helper to create a view object based on provided QML.
  59. def _createViewFromQML(self):
  60. path = QUrl.fromLocalFile(os.path.join(PluginRegistry.getInstance().getPluginPath(self.getPluginId()), self._qml_url))
  61. self._component = QQmlComponent(Application.getInstance()._engine, path)
  62. self._context = QQmlContext(Application.getInstance()._engine.rootContext())
  63. self._context.setContextProperty("manager", self)
  64. self._view = self._component.create(self._context)
  65. if self._view is None:
  66. Logger.log("c", "QQmlComponent status %s", self._component.status())
  67. Logger.log("c", "QQmlComponent error string %s", self._component.errorString())
  68. @pyqtProperty(QObject, constant = True)
  69. def displayItem(self):
  70. if not self._component:
  71. self._createViewFromQML()
  72. return self._view