GlobalStacksModel.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from PyQt5.QtCore import Qt, QTimer
  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. def __init__(self, parent = None) -> None:
  19. super().__init__(parent)
  20. self._catalog = i18nCatalog("cura")
  21. self.addRoleName(self.NameRole, "name")
  22. self.addRoleName(self.IdRole, "id")
  23. self.addRoleName(self.HasRemoteConnectionRole, "hasRemoteConnection")
  24. self.addRoleName(self.MetaDataRole, "metadata")
  25. self.addRoleName(self.DiscoverySourceRole, "discoverySource")
  26. self._change_timer = QTimer()
  27. self._change_timer.setInterval(200)
  28. self._change_timer.setSingleShot(True)
  29. self._change_timer.timeout.connect(self._update)
  30. # Listen to changes
  31. CuraContainerRegistry.getInstance().containerAdded.connect(self._onContainerChanged)
  32. CuraContainerRegistry.getInstance().containerMetaDataChanged.connect(self._onContainerChanged)
  33. CuraContainerRegistry.getInstance().containerRemoved.connect(self._onContainerChanged)
  34. self._updateDelayed()
  35. def _onContainerChanged(self, container) -> None:
  36. """Handler for container added/removed events from registry"""
  37. # We only need to update when the added / removed container GlobalStack
  38. if isinstance(container, GlobalStack):
  39. self._updateDelayed()
  40. def _updateDelayed(self) -> None:
  41. self._change_timer.start()
  42. def _update(self) -> None:
  43. items = []
  44. container_stacks = CuraContainerRegistry.getInstance().findContainerStacks(type = "machine")
  45. for container_stack in container_stacks:
  46. has_remote_connection = False
  47. for connection_type in container_stack.configuredConnectionTypes:
  48. has_remote_connection |= connection_type in [ConnectionType.NetworkConnection.value,
  49. ConnectionType.CloudConnection.value]
  50. if parseBool(container_stack.getMetaDataEntry("hidden", False)):
  51. continue
  52. device_name = container_stack.getMetaDataEntry("group_name", container_stack.getName())
  53. section_name = "Connected printers" if has_remote_connection else "Preset printers"
  54. section_name = self._catalog.i18nc("@info:title", section_name)
  55. default_removal_warning = self._catalog.i18nc(
  56. "@label {0} is the name of a printer that's about to be deleted.",
  57. "Are you sure you wish to remove {0}? This cannot be undone!", device_name
  58. )
  59. removal_warning = container_stack.getMetaDataEntry("removal_warning", default_removal_warning)
  60. items.append({"name": device_name,
  61. "id": container_stack.getId(),
  62. "hasRemoteConnection": has_remote_connection,
  63. "metadata": container_stack.getMetaData().copy(),
  64. "discoverySource": section_name,
  65. "removalWarning": removal_warning})
  66. items.sort(key=lambda i: (not i["hasRemoteConnection"], i["name"]))
  67. self.setItems(items)