GlobalStacksModel.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from UM.Qt.ListModel import ListModel
  4. from PyQt5.QtCore import pyqtProperty, Qt, pyqtSignal
  5. from UM.Settings.ContainerRegistry import ContainerRegistry
  6. from UM.Settings.ContainerStack import ContainerStack
  7. from cura.PrinterOutputDevice import ConnectionType
  8. from cura.Settings.GlobalStack import GlobalStack
  9. class GlobalStacksModel(ListModel):
  10. NameRole = Qt.UserRole + 1
  11. IdRole = Qt.UserRole + 2
  12. HasRemoteConnectionRole = Qt.UserRole + 3
  13. ConnectionTypeRole = Qt.UserRole + 4
  14. MetaDataRole = Qt.UserRole + 5
  15. def __init__(self, parent = None):
  16. super().__init__(parent)
  17. self.addRoleName(self.NameRole, "name")
  18. self.addRoleName(self.IdRole, "id")
  19. self.addRoleName(self.HasRemoteConnectionRole, "hasRemoteConnection")
  20. self.addRoleName(self.ConnectionTypeRole, "connectionType")
  21. self.addRoleName(self.MetaDataRole, "metadata")
  22. self._container_stacks = []
  23. # Listen to changes
  24. ContainerRegistry.getInstance().containerAdded.connect(self._onContainerChanged)
  25. ContainerRegistry.getInstance().containerMetaDataChanged.connect(self._onContainerChanged)
  26. ContainerRegistry.getInstance().containerRemoved.connect(self._onContainerChanged)
  27. self._filter_dict = {}
  28. self._update()
  29. ## Handler for container added/removed events from registry
  30. def _onContainerChanged(self, container):
  31. # We only need to update when the added / removed container GlobalStack
  32. if isinstance(container, GlobalStack):
  33. self._update()
  34. def _update(self) -> None:
  35. items = []
  36. container_stacks = ContainerRegistry.getInstance().findContainerStacks(type = "machine")
  37. for container_stack in container_stacks:
  38. connection_type = int(container_stack.getMetaDataEntry("connection_type", ConnectionType.NotConnected.value))
  39. has_remote_connection = connection_type in [ConnectionType.NetworkConnection.value, ConnectionType.CloudConnection.value]
  40. if container_stack.getMetaDataEntry("hidden", False) in ["True", True]:
  41. continue
  42. # TODO: Remove reference to connect group name.
  43. items.append({"name": container_stack.getMetaDataEntry("connect_group_name", container_stack.getName()),
  44. "id": container_stack.getId(),
  45. "hasRemoteConnection": has_remote_connection,
  46. "connectionType": connection_type,
  47. "metadata": container_stack.getMetaData().copy()})
  48. items.sort(key=lambda i: not i["hasRemoteConnection"])
  49. self.setItems(items)