IntentModel.py 6.8 KB

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