GlobalStacksModel.py 5.4 KB

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