MachineAction.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # Copyright (c) 2016 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import os
  4. from typing import Optional
  5. from PyQt5.QtCore import QObject, QUrl, pyqtSlot, pyqtProperty, pyqtSignal
  6. from UM.Logger import Logger
  7. from UM.PluginObject import PluginObject
  8. from UM.PluginRegistry import PluginRegistry
  9. ## Machine actions are actions that are added to a specific machine type. Examples of such actions are
  10. # updating the firmware, connecting with remote devices or doing bed leveling. A machine action can also have a
  11. # qml, which should contain a "Cura.MachineAction" item. When activated, the item will be displayed in a dialog
  12. # and this object will be added as "manager" (so all pyqtSlot() functions can be called by calling manager.func())
  13. class MachineAction(QObject, PluginObject):
  14. ## Create a new Machine action.
  15. # \param key unique key of the machine action
  16. # \param label Human readable label used to identify the machine action.
  17. def __init__(self, key: str, label: str = "") -> None:
  18. super().__init__()
  19. self._key = key
  20. self._label = label
  21. self._qml_url = ""
  22. self._view = None
  23. self._finished = False
  24. labelChanged = pyqtSignal()
  25. onFinished = pyqtSignal()
  26. def getKey(self) -> str:
  27. return self._key
  28. ## Whether this action needs to ask the user anything.
  29. # If not, we shouldn't present the user with certain screens which otherwise show up.
  30. # Defaults to true to be in line with the old behaviour.
  31. def needsUserInteraction(self) -> bool:
  32. return True
  33. @pyqtProperty(str, notify = labelChanged)
  34. def label(self) -> str:
  35. return self._label
  36. def setLabel(self, label: str) -> None:
  37. if self._label != label:
  38. self._label = label
  39. self.labelChanged.emit()
  40. ## Reset the action to it's default state.
  41. # This should not be re-implemented by child classes, instead re-implement _reset.
  42. # /sa _reset
  43. @pyqtSlot()
  44. def reset(self) -> None:
  45. self._finished = False
  46. self._reset()
  47. ## Protected implementation of reset.
  48. # /sa reset()
  49. def _reset(self) -> None:
  50. pass
  51. @pyqtSlot()
  52. def setFinished(self) -> None:
  53. self._finished = True
  54. self._reset()
  55. self.onFinished.emit()
  56. @pyqtProperty(bool, notify = onFinished)
  57. def finished(self) -> bool:
  58. return self._finished
  59. ## Protected helper to create a view object based on provided QML.
  60. def _createViewFromQML(self) -> Optional["QObject"]:
  61. plugin_path = PluginRegistry.getInstance().getPluginPath(self.getPluginId())
  62. if plugin_path is None:
  63. Logger.log("e", "Cannot create QML view: cannot find plugin path for plugin [%s]", self.getPluginId())
  64. return None
  65. path = os.path.join(plugin_path, self._qml_url)
  66. from cura.CuraApplication import CuraApplication
  67. view = CuraApplication.getInstance().createQmlComponent(path, {"manager": self})
  68. return view
  69. @pyqtProperty(QUrl, constant = True)
  70. def qmlPath(self) -> "QUrl":
  71. plugin_path = PluginRegistry.getInstance().getPluginPath(self.getPluginId())
  72. if plugin_path is None:
  73. Logger.log("e", "Cannot create QML view: cannot find plugin path for plugin [%s]", self.getPluginId())
  74. return QUrl("")
  75. path = os.path.join(plugin_path, self._qml_url)
  76. return QUrl.fromLocalFile(path)
  77. @pyqtSlot(result = QObject)
  78. def getDisplayItem(self) -> Optional["QObject"]:
  79. return self._createViewFromQML()