BuildPlateModel.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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
  4. from UM.Application import Application
  5. from UM.Logger import Logger
  6. from UM.Qt.ListModel import ListModel
  7. from UM.Util import parseBool
  8. from cura.Machines.VariantType import VariantType
  9. class BuildPlateModel(ListModel):
  10. NameRole = Qt.UserRole + 1
  11. ContainerNodeRole = Qt.UserRole + 2
  12. def __init__(self, parent = None):
  13. super().__init__(parent)
  14. self.addRoleName(self.NameRole, "name")
  15. self.addRoleName(self.ContainerNodeRole, "container_node")
  16. self._application = Application.getInstance()
  17. self._variant_manager = self._application._variant_manager
  18. self._machine_manager = self._application.getMachineManager()
  19. self._machine_manager.globalContainerChanged.connect(self._update)
  20. self._update()
  21. def _update(self):
  22. Logger.log("d", "Updating {model_class_name}.".format(model_class_name = self.__class__.__name__))
  23. global_stack = self._machine_manager._global_container_stack
  24. if not global_stack:
  25. self.setItems([])
  26. return
  27. has_variants = parseBool(global_stack.getMetaDataEntry("has_variant_buildplates", False))
  28. if not has_variants:
  29. self.setItems([])
  30. return
  31. variant_dict = self._variant_manager.getVariantNodes(global_stack, variant_type = VariantType.BUILD_PLATE)
  32. item_list = []
  33. for name, variant_node in variant_dict.items():
  34. item = {"name": name,
  35. "container_node": variant_node}
  36. item_list.append(item)
  37. self.setItems(item_list)