MachineListModel.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # Copyright (c) 2022 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. # The MachineListModel is used to display the connected printers in the interface. Both the abstract machines and all
  4. # online cloud connected printers are represented within this ListModel. Additional information such as the number of
  5. # connected printers for each printer type is gathered.
  6. from typing import Optional
  7. from PyQt6.QtCore import Qt, QTimer, QObject, pyqtSlot, pyqtProperty, pyqtSignal
  8. from UM.Qt.ListModel import ListModel
  9. from UM.Settings.ContainerStack import ContainerStack
  10. from UM.Settings.Interfaces import ContainerInterface
  11. from UM.i18n import i18nCatalog
  12. from UM.Util import parseBool
  13. from cura.PrinterOutput.PrinterOutputDevice import ConnectionType
  14. from cura.Settings.CuraContainerRegistry import CuraContainerRegistry
  15. from cura.Settings.GlobalStack import GlobalStack
  16. class MachineListModel(ListModel):
  17. NameRole = Qt.ItemDataRole.UserRole + 1
  18. IdRole = Qt.ItemDataRole.UserRole + 2
  19. HasRemoteConnectionRole = Qt.ItemDataRole.UserRole + 3
  20. MetaDataRole = Qt.ItemDataRole.UserRole + 4
  21. IsOnlineRole = Qt.ItemDataRole.UserRole + 5
  22. MachineCountRole = Qt.ItemDataRole.UserRole + 6
  23. IsAbstractMachineRole = Qt.ItemDataRole.UserRole + 7
  24. ComponentTypeRole = Qt.ItemDataRole.UserRole + 8
  25. def __init__(self, parent: Optional[QObject] = None) -> None:
  26. super().__init__(parent)
  27. self._show_cloud_printers = False
  28. self._catalog = i18nCatalog("cura")
  29. self.addRoleName(self.NameRole, "name")
  30. self.addRoleName(self.IdRole, "id")
  31. self.addRoleName(self.HasRemoteConnectionRole, "hasRemoteConnection")
  32. self.addRoleName(self.MetaDataRole, "metadata")
  33. self.addRoleName(self.IsOnlineRole, "isOnline")
  34. self.addRoleName(self.MachineCountRole, "machineCount")
  35. self.addRoleName(self.IsAbstractMachineRole, "isAbstractMachine")
  36. self.addRoleName(self.ComponentTypeRole, "componentType")
  37. self._change_timer = QTimer()
  38. self._change_timer.setInterval(200)
  39. self._change_timer.setSingleShot(True)
  40. self._change_timer.timeout.connect(self._update)
  41. # Listen to changes
  42. CuraContainerRegistry.getInstance().containerAdded.connect(self._onContainerChanged)
  43. CuraContainerRegistry.getInstance().containerMetaDataChanged.connect(self._onContainerChanged)
  44. CuraContainerRegistry.getInstance().containerRemoved.connect(self._onContainerChanged)
  45. self._updateDelayed()
  46. showCloudPrintersChanged = pyqtSignal(bool)
  47. @pyqtProperty(bool, notify=showCloudPrintersChanged)
  48. def showCloudPrinters(self) -> bool:
  49. return self._show_cloud_printers
  50. @pyqtSlot(bool)
  51. def setShowCloudPrinters(self, show_cloud_printers: bool) -> None:
  52. self._show_cloud_printers = show_cloud_printers
  53. self._updateDelayed()
  54. self.showCloudPrintersChanged.emit(show_cloud_printers)
  55. def _onContainerChanged(self, container: ContainerInterface) -> None:
  56. """Handler for container added/removed events from registry"""
  57. # We only need to update when the added / removed container GlobalStack
  58. if isinstance(container, GlobalStack):
  59. self._updateDelayed()
  60. def _updateDelayed(self) -> None:
  61. self._change_timer.start()
  62. def _update(self) -> None:
  63. self.clear()
  64. from cura.CuraApplication import CuraApplication
  65. machines_manager = CuraApplication.getInstance().getMachineManager()
  66. other_machine_stacks = CuraContainerRegistry.getInstance().findContainerStacks(type="machine")
  67. other_machine_stacks.sort(key = lambda machine: machine.getName().upper())
  68. abstract_machine_stacks = CuraContainerRegistry.getInstance().findContainerStacks(is_abstract_machine = "True")
  69. abstract_machine_stacks.sort(key = lambda machine: machine.getName().upper(), reverse = True)
  70. for abstract_machine in abstract_machine_stacks:
  71. definition_id = abstract_machine.definition.getId()
  72. online_machine_stacks = machines_manager.getMachinesWithDefinition(definition_id, online_only = True)
  73. online_machine_stacks = list(filter(lambda machine: machine.hasNetworkedConnection(), online_machine_stacks))
  74. online_machine_stacks.sort(key=lambda machine: machine.getName().upper())
  75. other_machine_stacks.remove(abstract_machine)
  76. if abstract_machine in online_machine_stacks:
  77. online_machine_stacks.remove(abstract_machine)
  78. # Create a list item for abstract machine
  79. self.addItem(abstract_machine, True, len(online_machine_stacks))
  80. # Create list of machines that are children of the abstract machine
  81. for stack in online_machine_stacks:
  82. if self._show_cloud_printers:
  83. self.addItem(stack, True)
  84. # Remove this machine from the other stack list
  85. if stack in other_machine_stacks:
  86. other_machine_stacks.remove(stack)
  87. if len(abstract_machine_stacks) > 0:
  88. if self._show_cloud_printers:
  89. self.appendItem({"componentType": "HIDE_BUTTON",
  90. "isOnline": True,
  91. "isAbstractMachine": False,
  92. "machineCount": 0
  93. })
  94. else:
  95. self.appendItem({"componentType": "SHOW_BUTTON",
  96. "isOnline": True,
  97. "isAbstractMachine": False,
  98. "machineCount": 0
  99. })
  100. for stack in other_machine_stacks:
  101. self.addItem(stack, False)
  102. def addItem(self, container_stack: ContainerStack, is_online: bool, machine_count: int = 0) -> None:
  103. if parseBool(container_stack.getMetaDataEntry("hidden", False)):
  104. return
  105. self.appendItem({
  106. "componentType": "MACHINE",
  107. "name": container_stack.getName(),
  108. "id": container_stack.getId(),
  109. "metadata": container_stack.getMetaData().copy(),
  110. "isOnline": is_online,
  111. "isAbstractMachine": parseBool(container_stack.getMetaDataEntry("is_abstract_machine", False)),
  112. "machineCount": machine_count,
  113. })