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