MachineListModel.py 6.4 KB

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