MachineManagementModel.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from UM.Qt.ListModel import ListModel
  4. from PyQt5.QtCore import Qt
  5. from UM.Settings.ContainerRegistry import ContainerRegistry
  6. from UM.Settings.ContainerStack import ContainerStack
  7. from UM.i18n import i18nCatalog
  8. catalog = i18nCatalog("cura")
  9. #
  10. # This the QML model for the quality management page.
  11. #
  12. class MachineManagementModel(ListModel):
  13. NameRole = Qt.UserRole + 1
  14. IdRole = Qt.UserRole + 2
  15. MetaDataRole = Qt.UserRole + 3
  16. GroupRole = Qt.UserRole + 4
  17. def __init__(self, parent = None):
  18. super().__init__(parent)
  19. self.addRoleName(self.NameRole, "name")
  20. self.addRoleName(self.IdRole, "id")
  21. self.addRoleName(self.MetaDataRole, "metadata")
  22. self.addRoleName(self.GroupRole, "group")
  23. self._local_container_stacks = []
  24. self._network_container_stacks = []
  25. # Listen to changes
  26. ContainerRegistry.getInstance().containerAdded.connect(self._onContainerChanged)
  27. ContainerRegistry.getInstance().containerMetaDataChanged.connect(self._onContainerChanged)
  28. ContainerRegistry.getInstance().containerRemoved.connect(self._onContainerChanged)
  29. self._filter_dict = {}
  30. self._update()
  31. ## Handler for container added/removed events from registry
  32. def _onContainerChanged(self, container):
  33. # We only need to update when the added / removed container is a stack.
  34. if isinstance(container, ContainerStack) and container.getMetaDataEntry("type") == "machine":
  35. self._update()
  36. ## Private convenience function to reset & repopulate the model.
  37. def _update(self):
  38. items = []
  39. # Get first the network enabled printers
  40. network_filter_printers = {"type": "machine",
  41. "um_network_key": "*",
  42. "hidden": "False"}
  43. self._network_container_stacks = ContainerRegistry.getInstance().findContainerStacks(**network_filter_printers)
  44. self._network_container_stacks.sort(key = lambda i: i.getMetaDataEntry("connect_group_name"))
  45. for container in self._network_container_stacks:
  46. metadata = container.getMetaData().copy()
  47. if container.getBottom():
  48. metadata["definition_name"] = container.getBottom().getName()
  49. items.append({"name": metadata["connect_group_name"],
  50. "id": container.getId(),
  51. "metadata": metadata,
  52. "group": catalog.i18nc("@info:title", "Network enabled printers")})
  53. # Get now the local printers
  54. local_filter_printers = {"type": "machine", "um_network_key": None}
  55. self._local_container_stacks = ContainerRegistry.getInstance().findContainerStacks(**local_filter_printers)
  56. self._local_container_stacks.sort(key = lambda i: i.getName())
  57. for container in self._local_container_stacks:
  58. metadata = container.getMetaData().copy()
  59. if container.getBottom():
  60. metadata["definition_name"] = container.getBottom().getName()
  61. items.append({"name": container.getName(),
  62. "id": container.getId(),
  63. "metadata": metadata,
  64. "group": catalog.i18nc("@info:title", "Local printers")})
  65. self.setItems(items)