GlobalStacksModel.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from PyQt5.QtCore import pyqtProperty, Qt, QTimer
  4. from UM.Qt.ListModel import ListModel
  5. from UM.i18n import i18nCatalog
  6. from cura.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. SectionNameRole = Qt.UserRole + 6 # For separating local and remote printers in the machine management page
  16. def __init__(self, parent = 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.SectionNameRole, "sectionName")
  24. self._container_stacks = []
  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._filter_dict = {}
  34. self._updateDelayed()
  35. ## Handler for container added/removed events from registry
  36. def _onContainerChanged(self, container):
  37. # We only need to update when the added / removed container GlobalStack
  38. if isinstance(container, GlobalStack):
  39. self._updateDelayed()
  40. def _updateDelayed(self):
  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, ConnectionType.CloudConnection.value]
  49. if container_stack.getMetaDataEntry("hidden", False) in ["True", True]:
  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. "sectionName": section_name})
  58. items.sort(key=lambda i: not i["hasRemoteConnection"])
  59. self.setItems(items)