GlobalStacksModel.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # Copyright (c) 2021 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from PyQt6.QtCore import Qt, QTimer, pyqtProperty, pyqtSignal
  4. from typing import List, Optional
  5. from UM.Qt.ListModel import ListModel
  6. from UM.i18n import i18nCatalog
  7. from UM.Util import parseBool
  8. from cura.PrinterOutput.PrinterOutputDevice import ConnectionType
  9. from cura.Settings.CuraContainerRegistry import CuraContainerRegistry
  10. from cura.Settings.GlobalStack import GlobalStack
  11. from cura.UltimakerCloud.UltimakerCloudConstants import META_CAPABILITIES # To filter on the printer's capabilities.
  12. class GlobalStacksModel(ListModel):
  13. NameRole = Qt.ItemDataRole.UserRole + 1
  14. IdRole = Qt.ItemDataRole.UserRole + 2
  15. HasRemoteConnectionRole = Qt.ItemDataRole.UserRole + 3
  16. ConnectionTypeRole = Qt.ItemDataRole.UserRole + 4
  17. MetaDataRole = Qt.ItemDataRole.UserRole + 5
  18. DiscoverySourceRole = Qt.ItemDataRole.UserRole + 6 # For separating local and remote printers in the machine management page
  19. RemovalWarningRole = Qt.ItemDataRole.UserRole + 7
  20. IsOnlineRole = Qt.ItemDataRole.UserRole + 8
  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.DiscoverySourceRole, "discoverySource")
  29. self.addRoleName(self.IsOnlineRole, "isOnline")
  30. self._change_timer = QTimer()
  31. self._change_timer.setInterval(200)
  32. self._change_timer.setSingleShot(True)
  33. self._change_timer.timeout.connect(self._update)
  34. self._filter_connection_type = None # type: Optional[ConnectionType]
  35. self._filter_online_only = False
  36. self._filter_capabilities: List[str] = [] # Required capabilities that all listed printers must have.
  37. # Listen to changes
  38. CuraContainerRegistry.getInstance().containerAdded.connect(self._onContainerChanged)
  39. CuraContainerRegistry.getInstance().containerMetaDataChanged.connect(self._onContainerChanged)
  40. CuraContainerRegistry.getInstance().containerRemoved.connect(self._onContainerChanged)
  41. self._updateDelayed()
  42. filterConnectionTypeChanged = pyqtSignal()
  43. filterCapabilitiesChanged = pyqtSignal()
  44. filterOnlineOnlyChanged = pyqtSignal()
  45. def setFilterConnectionType(self, new_filter: Optional[ConnectionType]) -> None:
  46. if self._filter_connection_type != new_filter:
  47. self._filter_connection_type = new_filter
  48. self.filterConnectionTypeChanged.emit()
  49. @pyqtProperty(int, fset = setFilterConnectionType, notify = filterConnectionTypeChanged)
  50. def filterConnectionType(self) -> int:
  51. """
  52. The connection type to filter the list of printers by.
  53. Only printers that match this connection type will be listed in the
  54. model.
  55. """
  56. if self._filter_connection_type is None:
  57. return -1
  58. return self._filter_connection_type.value
  59. def setFilterOnlineOnly(self, new_filter: bool) -> None:
  60. if self._filter_online_only != new_filter:
  61. self._filter_online_only = new_filter
  62. self.filterOnlineOnlyChanged.emit()
  63. @pyqtProperty(bool, fset = setFilterOnlineOnly, notify = filterOnlineOnlyChanged)
  64. def filterOnlineOnly(self) -> bool:
  65. """
  66. Whether to filter the global stacks to show only printers that are online.
  67. """
  68. return self._filter_online_only
  69. def setFilterCapabilities(self, new_filter: List[str]) -> None:
  70. if self._filter_capabilities != new_filter:
  71. self._filter_capabilities = new_filter
  72. self.filterCapabilitiesChanged.emit()
  73. @pyqtProperty("QStringList", fset = setFilterCapabilities, notify = filterCapabilitiesChanged)
  74. def filterCapabilities(self) -> List[str]:
  75. """
  76. Capabilities to require on the list of printers.
  77. Only printers that have all of these capabilities will be shown in this model.
  78. """
  79. return self._filter_capabilities
  80. def _onContainerChanged(self, container) -> None:
  81. """Handler for container added/removed events from registry"""
  82. # We only need to update when the added / removed container GlobalStack
  83. if isinstance(container, GlobalStack):
  84. self._updateDelayed()
  85. def _updateDelayed(self) -> None:
  86. self._change_timer.start()
  87. def _update(self) -> None:
  88. items = []
  89. container_stacks = CuraContainerRegistry.getInstance().findContainerStacks(type = "machine")
  90. for container_stack in container_stacks:
  91. if self._filter_connection_type is not None: # We want to filter on connection types.
  92. if not any((connection_type == self._filter_connection_type for connection_type in container_stack.configuredConnectionTypes)):
  93. continue # No connection type on this printer matches the filter.
  94. has_remote_connection = False
  95. for connection_type in container_stack.configuredConnectionTypes:
  96. has_remote_connection |= connection_type in [ConnectionType.NetworkConnection.value,
  97. ConnectionType.CloudConnection.value]
  98. if parseBool(container_stack.getMetaDataEntry("hidden", False)):
  99. continue
  100. is_online = container_stack.getMetaDataEntry("is_online", False)
  101. if self._filter_online_only and not is_online:
  102. continue
  103. capabilities = set(container_stack.getMetaDataEntry(META_CAPABILITIES, "").split(","))
  104. if set(self._filter_capabilities) - capabilities: # Not all required capabilities are met.
  105. continue
  106. device_name = container_stack.getMetaDataEntry("group_name", container_stack.getName())
  107. section_name = "Connected printers" if has_remote_connection else "Preset printers"
  108. section_name = self._catalog.i18nc("@info:title", section_name)
  109. default_removal_warning = self._catalog.i18nc(
  110. "@label {0} is the name of a printer that's about to be deleted.",
  111. "Are you sure you wish to remove {0}? This cannot be undone!", device_name
  112. )
  113. removal_warning = container_stack.getMetaDataEntry("removal_warning", default_removal_warning)
  114. items.append({"name": device_name,
  115. "id": container_stack.getId(),
  116. "hasRemoteConnection": has_remote_connection,
  117. "metadata": container_stack.getMetaData().copy(),
  118. "discoverySource": section_name,
  119. "removalWarning": removal_warning,
  120. "isOnline": is_online})
  121. items.sort(key=lambda i: (not i["hasRemoteConnection"], i["name"]))
  122. self.setItems(items)