GlobalStacksModel.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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, QTimer
  5. from cura.PrinterOutputDevice import ConnectionType
  6. from cura.Settings.CuraContainerRegistry import CuraContainerRegistry
  7. from cura.Settings.GlobalStack import GlobalStack
  8. class GlobalStacksModel(ListModel):
  9. NameRole = Qt.UserRole + 1
  10. IdRole = Qt.UserRole + 2
  11. HasRemoteConnectionRole = Qt.UserRole + 3
  12. ConnectionTypeRole = Qt.UserRole + 4
  13. MetaDataRole = Qt.UserRole + 5
  14. def __init__(self, parent = None):
  15. super().__init__(parent)
  16. self.addRoleName(self.NameRole, "name")
  17. self.addRoleName(self.IdRole, "id")
  18. self.addRoleName(self.HasRemoteConnectionRole, "hasRemoteConnection")
  19. self.addRoleName(self.MetaDataRole, "metadata")
  20. self._container_stacks = []
  21. self._change_timer = QTimer()
  22. self._change_timer.setInterval(200)
  23. self._change_timer.setSingleShot(True)
  24. self._change_timer.timeout.connect(self._update)
  25. # Listen to changes
  26. CuraContainerRegistry.getInstance().containerAdded.connect(self._onContainerChanged)
  27. CuraContainerRegistry.getInstance().containerMetaDataChanged.connect(self._onContainerChanged)
  28. CuraContainerRegistry.getInstance().containerRemoved.connect(self._onContainerChanged)
  29. self._filter_dict = {}
  30. self._updateDelayed()
  31. ## Handler for container added/removed events from registry
  32. def _onContainerChanged(self, container):
  33. # We only need to update when the added / removed container GlobalStack
  34. if isinstance(container, GlobalStack):
  35. self._updateDelayed()
  36. def _updateDelayed(self):
  37. self._change_timer.start()
  38. def _update(self) -> None:
  39. items = []
  40. container_stacks = CuraContainerRegistry.getInstance().findContainerStacks(type = "machine")
  41. for container_stack in container_stacks:
  42. has_remote_connection = False
  43. for connection_type in container_stack.configuredConnectionTypes:
  44. has_remote_connection |= connection_type in [ConnectionType.NetworkConnection.value, ConnectionType.CloudConnection.value]
  45. if container_stack.getMetaDataEntry("hidden", False) in ["True", True]:
  46. continue
  47. items.append({"name": container_stack.getMetaDataEntry("group_name", container_stack.getName()),
  48. "id": container_stack.getId(),
  49. "hasRemoteConnection": has_remote_connection,
  50. "metadata": container_stack.getMetaData().copy()})
  51. items.sort(key=lambda i: not i["hasRemoteConnection"])
  52. self.setItems(items)