GlobalStacksModel.py 5.6 KB

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