MachineListModel.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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, List, cast
  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.Settings.CuraContainerRegistry import CuraContainerRegistry
  14. from cura.Settings.GlobalStack import GlobalStack
  15. class MachineListModel(ListModel):
  16. NameRole = Qt.ItemDataRole.UserRole + 1
  17. IdRole = Qt.ItemDataRole.UserRole + 2
  18. HasRemoteConnectionRole = Qt.ItemDataRole.UserRole + 3
  19. MetaDataRole = Qt.ItemDataRole.UserRole + 4
  20. IsOnlineRole = Qt.ItemDataRole.UserRole + 5
  21. MachineCountRole = Qt.ItemDataRole.UserRole + 6
  22. IsAbstractMachineRole = Qt.ItemDataRole.UserRole + 7
  23. ComponentTypeRole = Qt.ItemDataRole.UserRole + 8
  24. IsNetworkedMachineRole = Qt.ItemDataRole.UserRole + 9
  25. def __init__(self, parent: Optional[QObject] = None, machines_filter: List[GlobalStack] = None, listenToChanges: bool = True) -> None:
  26. super().__init__(parent)
  27. self._show_cloud_printers = False
  28. self._machines_filter = machines_filter
  29. self._catalog = i18nCatalog("cura")
  30. self.addRoleName(self.NameRole, "name")
  31. self.addRoleName(self.IdRole, "id")
  32. self.addRoleName(self.HasRemoteConnectionRole, "hasRemoteConnection")
  33. self.addRoleName(self.MetaDataRole, "metadata")
  34. self.addRoleName(self.IsOnlineRole, "isOnline")
  35. self.addRoleName(self.MachineCountRole, "machineCount")
  36. self.addRoleName(self.IsAbstractMachineRole, "isAbstractMachine")
  37. self.addRoleName(self.ComponentTypeRole, "componentType")
  38. self.addRoleName(self.IsNetworkedMachineRole, "isNetworked")
  39. self._change_timer = QTimer()
  40. self._change_timer.setInterval(200)
  41. self._change_timer.setSingleShot(True)
  42. self._change_timer.timeout.connect(self._update)
  43. if listenToChanges:
  44. CuraContainerRegistry.getInstance().containerAdded.connect(self._onContainerChanged)
  45. CuraContainerRegistry.getInstance().containerMetaDataChanged.connect(self._onContainerChanged)
  46. CuraContainerRegistry.getInstance().containerRemoved.connect(self._onContainerChanged)
  47. self._updateDelayed()
  48. showCloudPrintersChanged = pyqtSignal(bool)
  49. @pyqtProperty(bool, notify=showCloudPrintersChanged)
  50. def showCloudPrinters(self) -> bool:
  51. return self._show_cloud_printers
  52. @pyqtSlot(bool)
  53. def setShowCloudPrinters(self, show_cloud_printers: bool) -> None:
  54. self._show_cloud_printers = show_cloud_printers
  55. self._updateDelayed()
  56. self.showCloudPrintersChanged.emit(show_cloud_printers)
  57. def _onContainerChanged(self, container: ContainerInterface) -> None:
  58. """Handler for container added/removed events from registry"""
  59. # We only need to update when the added / removed container GlobalStack
  60. if isinstance(container, GlobalStack):
  61. self._updateDelayed()
  62. def _updateDelayed(self) -> None:
  63. self._change_timer.start()
  64. def _getMachineStacks(self) -> List[ContainerStack]:
  65. return CuraContainerRegistry.getInstance().findContainerStacks(type = "machine")
  66. def _getAbstractMachineStacks(self) -> List[ContainerStack]:
  67. return CuraContainerRegistry.getInstance().findContainerStacks(is_abstract_machine = "True")
  68. def set_machines_filter(self, machines_filter: Optional[List[GlobalStack]]) -> None:
  69. self._machines_filter = machines_filter
  70. self._update()
  71. def _update(self) -> None:
  72. self.clear()
  73. from cura.CuraApplication import CuraApplication
  74. machines_manager = CuraApplication.getInstance().getMachineManager()
  75. other_machine_stacks = self._getMachineStacks()
  76. other_machine_stacks.sort(key = lambda machine: machine.getName().upper())
  77. abstract_machine_stacks = self._getAbstractMachineStacks()
  78. abstract_machine_stacks.sort(key = lambda machine: machine.getName().upper(), reverse = True)
  79. if self._machines_filter is not None:
  80. filter_ids = [machine_filter.id for machine_filter in self._machines_filter]
  81. other_machine_stacks = [machine for machine in other_machine_stacks if machine.id in filter_ids]
  82. abstract_machine_stacks = [machine for machine in abstract_machine_stacks if machine.id in filter_ids]
  83. for abstract_machine in abstract_machine_stacks:
  84. definition_id = abstract_machine.definition.getId()
  85. connected_machine_stacks = machines_manager.getMachinesWithDefinition(definition_id, online_only = False)
  86. connected_machine_stacks = list(filter(lambda machine: machine.hasNetworkedConnection(), connected_machine_stacks))
  87. connected_machine_stacks.sort(key=lambda machine: machine.getName().upper())
  88. if abstract_machine in other_machine_stacks:
  89. other_machine_stacks.remove(abstract_machine)
  90. if abstract_machine in connected_machine_stacks:
  91. connected_machine_stacks.remove(abstract_machine)
  92. # Create a list item for abstract machine
  93. self.addItem(abstract_machine, True, len(connected_machine_stacks))
  94. # Create list of machines that are children of the abstract machine
  95. for stack in connected_machine_stacks:
  96. if self._show_cloud_printers:
  97. self.addItem(stack, True)
  98. # Remove this machine from the other stack list
  99. if stack in other_machine_stacks:
  100. other_machine_stacks.remove(stack)
  101. if len(abstract_machine_stacks) > 0:
  102. self.appendItem({
  103. "componentType": "HIDE_BUTTON" if self._show_cloud_printers else "SHOW_BUTTON",
  104. "isOnline": True,
  105. "isAbstractMachine": False,
  106. "machineCount": 0,
  107. "catergory": "connected",
  108. })
  109. for stack in other_machine_stacks:
  110. self.addItem(stack, False)
  111. def addItem(self, container_stack: ContainerStack, is_online: bool, machine_count: int = 0) -> None:
  112. if parseBool(container_stack.getMetaDataEntry("hidden", False)):
  113. return
  114. self.appendItem({
  115. "componentType": "MACHINE",
  116. "name": container_stack.getName(),
  117. "id": container_stack.getId(),
  118. "metadata": container_stack.getMetaData().copy(),
  119. "isOnline": is_online,
  120. "isAbstractMachine": parseBool(container_stack.getMetaDataEntry("is_abstract_machine", False)),
  121. "isNetworked": cast(GlobalStack, container_stack).hasNetworkedConnection() if isinstance(container_stack, GlobalStack) else False,
  122. "machineCount": machine_count,
  123. "catergory": "connected" if is_online else "other",
  124. })