GlobalStacksModel.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. self._filter_abstract_machines: Optional[bool] = None
  38. # Listen to changes
  39. CuraContainerRegistry.getInstance().containerAdded.connect(self._onContainerChanged)
  40. CuraContainerRegistry.getInstance().containerMetaDataChanged.connect(self._onContainerChanged)
  41. CuraContainerRegistry.getInstance().containerRemoved.connect(self._onContainerChanged)
  42. self._updateDelayed()
  43. filterConnectionTypeChanged = pyqtSignal()
  44. filterCapabilitiesChanged = pyqtSignal()
  45. filterOnlineOnlyChanged = pyqtSignal()
  46. filterAbstractMachinesChanged = pyqtSignal()
  47. def setFilterConnectionType(self, new_filter: Optional[ConnectionType]) -> None:
  48. if self._filter_connection_type != new_filter:
  49. self._filter_connection_type = new_filter
  50. self.filterConnectionTypeChanged.emit()
  51. @pyqtProperty(int, fset = setFilterConnectionType, notify = filterConnectionTypeChanged)
  52. def filterConnectionType(self) -> int:
  53. """
  54. The connection type to filter the list of printers by.
  55. Only printers that match this connection type will be listed in the
  56. model.
  57. """
  58. if self._filter_connection_type is None:
  59. return -1
  60. return self._filter_connection_type.value
  61. def setFilterOnlineOnly(self, new_filter: bool) -> None:
  62. if self._filter_online_only != new_filter:
  63. self._filter_online_only = new_filter
  64. self.filterOnlineOnlyChanged.emit()
  65. @pyqtProperty(bool, fset = setFilterOnlineOnly, notify = filterOnlineOnlyChanged)
  66. def filterOnlineOnly(self) -> bool:
  67. """
  68. Whether to filter the global stacks to show only printers that are online.
  69. """
  70. return self._filter_online_only
  71. def setFilterCapabilities(self, new_filter: List[str]) -> None:
  72. if self._filter_capabilities != new_filter:
  73. self._filter_capabilities = new_filter
  74. self.filterCapabilitiesChanged.emit()
  75. @pyqtProperty("QStringList", fset = setFilterCapabilities, notify = filterCapabilitiesChanged)
  76. def filterCapabilities(self) -> List[str]:
  77. """
  78. Capabilities to require on the list of printers.
  79. Only printers that have all of these capabilities will be shown in this model.
  80. """
  81. return self._filter_capabilities
  82. def setFilterAbstractMachines(self, new_filter: Optional[bool]) -> None:
  83. if self._filter_abstract_machines != new_filter:
  84. self._filter_abstract_machines = new_filter
  85. self.filterAbstractMachinesChanged.emit()
  86. @pyqtProperty(bool, fset = setFilterAbstractMachines, notify = filterAbstractMachinesChanged)
  87. def filterAbstractMachines(self) -> Optional[bool]:
  88. """
  89. Weather we include abstract printers, non-abstract printers or both
  90. if this is set to None both abstract and non-abstract printers will be included in the list
  91. set to True will only include abstract printers
  92. set to False will only inclde non-abstract printers
  93. """
  94. return self._filter_abstract_machines
  95. def _onContainerChanged(self, container) -> None:
  96. """Handler for container added/removed events from registry"""
  97. # We only need to update when the added / removed container GlobalStack
  98. if isinstance(container, GlobalStack):
  99. self._updateDelayed()
  100. def _updateDelayed(self) -> None:
  101. self._change_timer.start()
  102. def _update(self) -> None:
  103. items = []
  104. container_stacks = CuraContainerRegistry.getInstance().findContainerStacks(type = "machine")
  105. for container_stack in container_stacks:
  106. if self._filter_connection_type is not None: # We want to filter on connection types.
  107. if not any((connection_type == self._filter_connection_type for connection_type in container_stack.configuredConnectionTypes)):
  108. continue # No connection type on this printer matches the filter.
  109. has_remote_connection = False
  110. for connection_type in container_stack.configuredConnectionTypes:
  111. has_remote_connection |= connection_type in [ConnectionType.NetworkConnection.value,
  112. ConnectionType.CloudConnection.value]
  113. if parseBool(container_stack.getMetaDataEntry("hidden", False)):
  114. continue
  115. is_online = container_stack.getMetaDataEntry("is_online", False)
  116. if self._filter_online_only and not is_online:
  117. continue
  118. is_abstract_machine = parseBool(container_stack.getMetaDataEntry("is_abstract_machine", False))
  119. if self._filter_abstract_machines is not None and self._filter_abstract_machines is not is_abstract_machine:
  120. continue
  121. capabilities = set(container_stack.getMetaDataEntry(META_CAPABILITIES, "").split(","))
  122. if set(self._filter_capabilities) - capabilities: # Not all required capabilities are met.
  123. continue
  124. device_name = container_stack.getMetaDataEntry("group_name", container_stack.getName())
  125. section_name = self._catalog.i18nc("@label", "Connected printers") if has_remote_connection else self._catalog.i18nc("@label", "Preset printers")
  126. section_name = self._catalog.i18nc("@info:title", section_name)
  127. default_removal_warning = self._catalog.i18nc(
  128. "@label {0} is the name of a printer that's about to be deleted.",
  129. "Are you sure you wish to remove {0}? This cannot be undone!", device_name
  130. )
  131. removal_warning = container_stack.getMetaDataEntry("removal_warning", default_removal_warning)
  132. items.append({"name": device_name,
  133. "id": container_stack.getId(),
  134. "hasRemoteConnection": has_remote_connection,
  135. "metadata": container_stack.getMetaData().copy(),
  136. "discoverySource": section_name,
  137. "removalWarning": removal_warning,
  138. "isOnline": is_online})
  139. items.sort(key=lambda i: (not i["hasRemoteConnection"], i["name"]))
  140. self.setItems(items)