IntentModel.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # Copyright (c) 2019 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Optional, Dict, Any, Set, List
  4. from PyQt5.QtCore import Qt, QObject, pyqtProperty, pyqtSignal
  5. import cura.CuraApplication
  6. from UM.Qt.ListModel import ListModel
  7. from UM.Settings.ContainerRegistry import ContainerRegistry
  8. from cura.Machines.ContainerTree import ContainerTree
  9. from cura.Machines.MaterialNode import MaterialNode
  10. from cura.Machines.Models.MachineModelUtils import fetchLayerHeight
  11. from cura.Machines.QualityGroup import QualityGroup
  12. class IntentModel(ListModel):
  13. NameRole = Qt.UserRole + 1
  14. QualityTypeRole = Qt.UserRole + 2
  15. LayerHeightRole = Qt.UserRole + 3
  16. AvailableRole = Qt.UserRole + 4
  17. IntentRole = Qt.UserRole + 5
  18. def __init__(self, parent: Optional[QObject] = None) -> None:
  19. super().__init__(parent)
  20. self.addRoleName(self.NameRole, "name")
  21. self.addRoleName(self.QualityTypeRole, "quality_type")
  22. self.addRoleName(self.LayerHeightRole, "layer_height")
  23. self.addRoleName(self.AvailableRole, "available")
  24. self.addRoleName(self.IntentRole, "intent_category")
  25. self._intent_category = "engineering"
  26. machine_manager = cura.CuraApplication.CuraApplication.getInstance().getMachineManager()
  27. machine_manager.globalContainerChanged.connect(self._update)
  28. machine_manager.extruderChanged.connect(self._update) # We also need to update if an extruder gets disabled
  29. ContainerRegistry.getInstance().containerAdded.connect(self._onChanged)
  30. ContainerRegistry.getInstance().containerRemoved.connect(self._onChanged)
  31. self._layer_height_unit = "" # This is cached
  32. self._update()
  33. intentCategoryChanged = pyqtSignal()
  34. def setIntentCategory(self, new_category: str) -> None:
  35. if self._intent_category != new_category:
  36. self._intent_category = new_category
  37. self.intentCategoryChanged.emit()
  38. self._update()
  39. @pyqtProperty(str, fset = setIntentCategory, notify = intentCategoryChanged)
  40. def intentCategory(self) -> str:
  41. return self._intent_category
  42. def _onChanged(self, container):
  43. if container.getMetaDataEntry("type") == "intent":
  44. self._update()
  45. def _update(self) -> None:
  46. new_items = [] # type: List[Dict[str, Any]]
  47. global_stack = cura.CuraApplication.CuraApplication.getInstance().getGlobalContainerStack()
  48. if not global_stack:
  49. self.setItems(new_items)
  50. return
  51. quality_groups = ContainerTree.getInstance().getCurrentQualityGroups()
  52. material_nodes = self._getActiveMaterials()
  53. added_quality_type_set = set() # type: Set[str]
  54. for material_node in material_nodes:
  55. intents = self._getIntentsForMaterial(material_node, quality_groups)
  56. for intent in intents:
  57. if intent["quality_type"] not in added_quality_type_set:
  58. new_items.append(intent)
  59. added_quality_type_set.add(intent["quality_type"])
  60. # Now that we added all intents that we found something for, ensure that we set add ticks (and layer_heights)
  61. # for all groups that we don't have anything for (and set it to not available)
  62. for quality_type, quality_group in quality_groups.items():
  63. # Add the intents that are of the correct category
  64. if quality_type not in added_quality_type_set:
  65. layer_height = fetchLayerHeight(quality_group)
  66. new_items.append({"name": "Unavailable",
  67. "quality_type": quality_type,
  68. "layer_height": layer_height,
  69. "intent_category": self._intent_category,
  70. "available": False})
  71. added_quality_type_set.add(quality_type)
  72. new_items = sorted(new_items, key = lambda x: x["layer_height"])
  73. self.setItems(new_items)
  74. ## Get the active materials for all extruders. No duplicates will be returned
  75. def _getActiveMaterials(self) -> Set["MaterialNode"]:
  76. global_stack = cura.CuraApplication.CuraApplication.getInstance().getGlobalContainerStack()
  77. if global_stack is None:
  78. return set()
  79. container_tree = ContainerTree.getInstance()
  80. machine_node = container_tree.machines[global_stack.definition.getId()]
  81. nodes = set() # type: Set[MaterialNode]
  82. for extruder in global_stack.extruderList:
  83. active_variant_name = extruder.variant.getMetaDataEntry("name")
  84. active_variant_node = machine_node.variants[active_variant_name]
  85. active_material_node = active_variant_node.materials[extruder.material.getMetaDataEntry("base_file")]
  86. nodes.add(active_material_node)
  87. return nodes
  88. def _getIntentsForMaterial(self, active_material_node: "MaterialNode", quality_groups: Dict[str, "QualityGroup"]) -> List[Dict[str, Any]]:
  89. extruder_intents = [] # type: List[Dict[str, Any]]
  90. for quality_id, quality_node in active_material_node.qualities.items():
  91. if quality_node.quality_type not in quality_groups: # Don't add the empty quality type (or anything else that would crash, defensively).
  92. continue
  93. quality_group = quality_groups[quality_node.quality_type]
  94. layer_height = fetchLayerHeight(quality_group)
  95. for intent_id, intent_node in quality_node.intents.items():
  96. if intent_node.intent_category != self._intent_category:
  97. continue
  98. extruder_intents.append({"name": quality_group.name,
  99. "quality_type": quality_group.quality_type,
  100. "layer_height": layer_height,
  101. "available": quality_group.is_available,
  102. "intent_category": self._intent_category
  103. })
  104. return extruder_intents
  105. def __repr__(self):
  106. return str(self.items)