MachineListModel.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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
  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.Settings.CuraContainerRegistry import CuraContainerRegistry
  12. from cura.Settings.GlobalStack import GlobalStack
  13. class MachineListModel(ListModel):
  14. NameRole = Qt.ItemDataRole.UserRole + 1
  15. IdRole = Qt.ItemDataRole.UserRole + 2
  16. HasRemoteConnectionRole = Qt.ItemDataRole.UserRole + 3
  17. MetaDataRole = Qt.ItemDataRole.UserRole + 4
  18. IsOnlineRole = Qt.ItemDataRole.UserRole + 5
  19. MachineCountRole = Qt.ItemDataRole.UserRole + 6
  20. IsAbstractMachine = Qt.ItemDataRole.UserRole + 7
  21. def __init__(self, parent=None) -> None:
  22. super().__init__(parent)
  23. self._catalog = i18nCatalog("cura")
  24. self.addRoleName(self.NameRole, "name")
  25. self.addRoleName(self.IdRole, "id")
  26. self.addRoleName(self.HasRemoteConnectionRole, "hasRemoteConnection")
  27. self.addRoleName(self.MetaDataRole, "metadata")
  28. self.addRoleName(self.IsOnlineRole, "isOnline")
  29. self.addRoleName(self.MachineCountRole, "machineCount")
  30. self.addRoleName(self.IsAbstractMachine, "isAbstractMachine")
  31. self._change_timer = QTimer()
  32. self._change_timer.setInterval(200)
  33. self._change_timer.setSingleShot(True)
  34. self._change_timer.timeout.connect(self._update)
  35. # Listen to changes
  36. CuraContainerRegistry.getInstance().containerAdded.connect(self._onContainerChanged)
  37. CuraContainerRegistry.getInstance().containerMetaDataChanged.connect(self._onContainerChanged)
  38. CuraContainerRegistry.getInstance().containerRemoved.connect(self._onContainerChanged)
  39. self._updateDelayed()
  40. def _onContainerChanged(self, container) -> None:
  41. """Handler for container added/removed events from registry"""
  42. # We only need to update when the added / removed container GlobalStack
  43. if isinstance(container, GlobalStack):
  44. self._updateDelayed()
  45. def _updateDelayed(self) -> None:
  46. self._change_timer.start()
  47. def _update(self) -> None:
  48. self.setItems([]) # Clear items
  49. other_machine_stacks = CuraContainerRegistry.getInstance().findContainerStacks(type="machine")
  50. abstract_machine_stacks = CuraContainerRegistry.getInstance().findContainerStacks(is_abstract_machine = "True")
  51. abstract_machine_stacks.sort(key = lambda machine: machine.getName(), reverse = True)
  52. for abstract_machine in abstract_machine_stacks:
  53. definition_id = abstract_machine.definition.getId()
  54. from cura.CuraApplication import CuraApplication
  55. machines_manager = CuraApplication.getInstance().getMachineManager()
  56. online_machine_stacks = machines_manager.getMachinesWithDefinition(definition_id, online_only = True)
  57. # Create a list item for abstract machine
  58. self.addItem(abstract_machine, len(online_machine_stacks))
  59. other_machine_stacks.remove(abstract_machine)
  60. # Create list of machines that are children of the abstract machine
  61. for stack in online_machine_stacks:
  62. self.addItem(stack)
  63. # Remove this machine from the other stack list
  64. other_machine_stacks.remove(stack)
  65. for stack in other_machine_stacks:
  66. self.addItem(stack)
  67. def addItem(self, container_stack: ContainerStack, machine_count: int = 0) -> None:
  68. if parseBool(container_stack.getMetaDataEntry("hidden", False)):
  69. return
  70. self.appendItem({"name": container_stack.getName(),
  71. "id": container_stack.getId(),
  72. "metadata": container_stack.getMetaData().copy(),
  73. "isOnline": parseBool(container_stack.getMetaDataEntry("is_online", False)),
  74. "isAbstractMachine": parseBool(container_stack.getMetaDataEntry("is_abstract_machine", False)),
  75. "machineCount": machine_count,
  76. })