GlobalStacksModel.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 cura.PrinterOutput.PrinterOutputDevice import ConnectionType
  7. from cura.Settings.CuraContainerRegistry import CuraContainerRegistry
  8. from cura.Settings.GlobalStack import GlobalStack
  9. class GlobalStacksModel(ListModel):
  10. NameRole = Qt.UserRole + 1
  11. IdRole = Qt.UserRole + 2
  12. HasRemoteConnectionRole = Qt.UserRole + 3
  13. ConnectionTypeRole = Qt.UserRole + 4
  14. MetaDataRole = Qt.UserRole + 5
  15. DiscoverySourceRole = Qt.UserRole + 6 # For separating local and remote printers in the machine management page
  16. def __init__(self, parent = None) -> None:
  17. super().__init__(parent)
  18. self._catalog = i18nCatalog("cura")
  19. self.addRoleName(self.NameRole, "name")
  20. self.addRoleName(self.IdRole, "id")
  21. self.addRoleName(self.HasRemoteConnectionRole, "hasRemoteConnection")
  22. self.addRoleName(self.MetaDataRole, "metadata")
  23. self.addRoleName(self.DiscoverySourceRole, "discoverySource")
  24. self._change_timer = QTimer()
  25. self._change_timer.setInterval(200)
  26. self._change_timer.setSingleShot(True)
  27. self._change_timer.timeout.connect(self._update)
  28. # Listen to changes
  29. CuraContainerRegistry.getInstance().containerAdded.connect(self._onContainerChanged)
  30. CuraContainerRegistry.getInstance().containerMetaDataChanged.connect(self._onContainerChanged)
  31. CuraContainerRegistry.getInstance().containerRemoved.connect(self._onContainerChanged)
  32. self._updateDelayed()
  33. ## Handler for container added/removed events from registry
  34. def _onContainerChanged(self, container) -> None:
  35. # We only need to update when the added / removed container GlobalStack
  36. if isinstance(container, GlobalStack):
  37. self._updateDelayed()
  38. def _updateDelayed(self) -> None:
  39. self._change_timer.start()
  40. def _update(self) -> None:
  41. items = []
  42. container_stacks = CuraContainerRegistry.getInstance().findContainerStacks(type = "machine")
  43. for container_stack in container_stacks:
  44. has_remote_connection = False
  45. for connection_type in container_stack.configuredConnectionTypes:
  46. has_remote_connection |= connection_type in [ConnectionType.NetworkConnection.value,
  47. ConnectionType.CloudConnection.value]
  48. if container_stack.getMetaDataEntry("hidden", False) in ["True", True]:
  49. continue
  50. section_name = "Network enabled printers" if has_remote_connection else "Local printers"
  51. section_name = self._catalog.i18nc("@info:title", section_name)
  52. items.append({"name": container_stack.getMetaDataEntry("group_name", container_stack.getName()),
  53. "id": container_stack.getId(),
  54. "hasRemoteConnection": has_remote_connection,
  55. "metadata": container_stack.getMetaData().copy(),
  56. "discoverySource": section_name})
  57. items.sort(key = lambda i: not i["hasRemoteConnection"])
  58. self.setItems(items)