MachineListModel.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. online_machine_stacks = machines_manager.getMachinesWithDefinition(definition_id, online_only = True)
  86. online_machine_stacks = list(filter(lambda machine: machine.hasNetworkedConnection(), online_machine_stacks))
  87. online_machine_stacks.sort(key=lambda machine: machine.getName().upper())
  88. other_machine_stacks.remove(abstract_machine)
  89. if abstract_machine in online_machine_stacks:
  90. online_machine_stacks.remove(abstract_machine)
  91. # Create a list item for abstract machine
  92. self.addItem(abstract_machine, True, len(online_machine_stacks))
  93. # Create list of machines that are children of the abstract machine
  94. for stack in online_machine_stacks:
  95. if self._show_cloud_printers:
  96. self.addItem(stack, True)
  97. # Remove this machine from the other stack list
  98. if stack in other_machine_stacks:
  99. other_machine_stacks.remove(stack)
  100. if len(abstract_machine_stacks) > 0:
  101. self.appendItem({
  102. "componentType": "HIDE_BUTTON" if self._show_cloud_printers else "SHOW_BUTTON",
  103. "isOnline": True,
  104. "isAbstractMachine": False,
  105. "machineCount": 0,
  106. "catergory": "connected",
  107. })
  108. for stack in other_machine_stacks:
  109. self.addItem(stack, False)
  110. def addItem(self, container_stack: ContainerStack, is_online: bool, machine_count: int = 0) -> None:
  111. if parseBool(container_stack.getMetaDataEntry("hidden", False)):
  112. return
  113. self.appendItem({
  114. "componentType": "MACHINE",
  115. "name": container_stack.getName(),
  116. "id": container_stack.getId(),
  117. "metadata": container_stack.getMetaData().copy(),
  118. "isOnline": is_online,
  119. "isAbstractMachine": parseBool(container_stack.getMetaDataEntry("is_abstract_machine", False)),
  120. "isNetworked": cast(GlobalStack, container_stack).hasNetworkedConnection() if isinstance(container_stack, GlobalStack) else False,
  121. "machineCount": machine_count,
  122. "catergory": "connected" if is_online else "other",
  123. })