GlobalStacksModel.py 2.7 KB

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