BaseMaterialsModel.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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, Set
  4. from PyQt5.QtCore import Qt, pyqtSignal, pyqtProperty
  5. from UM.Qt.ListModel import ListModel
  6. import cura.CuraApplication # Imported like this to prevent a circular reference.
  7. from cura.Machines.ContainerTree import ContainerTree
  8. from cura.Machines.MaterialNode import MaterialNode
  9. from cura.Settings.CuraContainerRegistry import CuraContainerRegistry
  10. ## This is the base model class for GenericMaterialsModel and MaterialBrandsModel.
  11. # Those 2 models are used by the material drop down menu to show generic materials and branded materials separately.
  12. # The extruder position defined here is being used to bound a menu to the correct extruder. This is used in the top
  13. # bar menu "Settings" -> "Extruder nr" -> "Material" -> this menu
  14. class BaseMaterialsModel(ListModel):
  15. extruderPositionChanged = pyqtSignal()
  16. enabledChanged = pyqtSignal()
  17. def __init__(self, parent = None):
  18. super().__init__(parent)
  19. from cura.CuraApplication import CuraApplication
  20. self._application = CuraApplication.getInstance()
  21. # Make these managers available to all material models
  22. self._container_registry = self._application.getInstance().getContainerRegistry()
  23. self._machine_manager = self._application.getMachineManager()
  24. # Update the stack and the model data when the machine changes
  25. self._machine_manager.globalContainerChanged.connect(self._updateExtruderStack)
  26. # Update this model when switching machines, when adding materials or changing their metadata.
  27. self._machine_manager.activeStackChanged.connect(self._update)
  28. ContainerTree.getInstance().materialsChanged.connect(self._materialsListChanged)
  29. self.addRoleName(Qt.UserRole + 1, "root_material_id")
  30. self.addRoleName(Qt.UserRole + 2, "id")
  31. self.addRoleName(Qt.UserRole + 3, "GUID")
  32. self.addRoleName(Qt.UserRole + 4, "name")
  33. self.addRoleName(Qt.UserRole + 5, "brand")
  34. self.addRoleName(Qt.UserRole + 6, "description")
  35. self.addRoleName(Qt.UserRole + 7, "material")
  36. self.addRoleName(Qt.UserRole + 8, "color_name")
  37. self.addRoleName(Qt.UserRole + 9, "color_code")
  38. self.addRoleName(Qt.UserRole + 10, "density")
  39. self.addRoleName(Qt.UserRole + 11, "diameter")
  40. self.addRoleName(Qt.UserRole + 12, "approximate_diameter")
  41. self.addRoleName(Qt.UserRole + 13, "adhesion_info")
  42. self.addRoleName(Qt.UserRole + 14, "is_read_only")
  43. self.addRoleName(Qt.UserRole + 15, "container_node")
  44. self.addRoleName(Qt.UserRole + 16, "is_favorite")
  45. self._extruder_position = 0
  46. self._extruder_stack = None
  47. self._available_materials = None # type: Optional[Dict[str, MaterialNode]]
  48. self._favorite_ids = set() # type: Set[str]
  49. self._enabled = True
  50. def _updateExtruderStack(self):
  51. global_stack = self._machine_manager.activeMachine
  52. if global_stack is None:
  53. return
  54. if self._extruder_stack is not None:
  55. self._extruder_stack.pyqtContainersChanged.disconnect(self._update)
  56. self._extruder_stack.approximateMaterialDiameterChanged.disconnect(self._update)
  57. self._extruder_stack = global_stack.extruders.get(str(self._extruder_position))
  58. if self._extruder_stack is not None:
  59. self._extruder_stack.pyqtContainersChanged.connect(self._update)
  60. self._extruder_stack.approximateMaterialDiameterChanged.connect(self._update)
  61. # Force update the model when the extruder stack changes
  62. self._update()
  63. def setExtruderPosition(self, position: int):
  64. if self._extruder_stack is None or self._extruder_position != position:
  65. self._extruder_position = position
  66. self._updateExtruderStack()
  67. self.extruderPositionChanged.emit()
  68. @pyqtProperty(int, fset = setExtruderPosition, notify = extruderPositionChanged)
  69. def extruderPosition(self) -> int:
  70. return self._extruder_position
  71. def setEnabled(self, enabled):
  72. if self._enabled != enabled:
  73. self._enabled = enabled
  74. if self._enabled:
  75. # ensure the data is there again.
  76. self._update()
  77. self.enabledChanged.emit()
  78. @pyqtProperty(bool, fset=setEnabled, notify=enabledChanged)
  79. def enabled(self):
  80. return self._enabled
  81. ## Triggered when a list of materials changed somewhere in the container
  82. # tree. This change may trigger an _update() call when the materials
  83. # changed for the configuration that this model is looking for.
  84. def _materialsListChanged(self, material: MaterialNode) -> None:
  85. if self._extruder_stack is None:
  86. return
  87. if material.variant.container_id != self._extruder_stack.variant.getId():
  88. return
  89. if material.variant.machine.container_id != cura.CuraApplication.CuraApplication.getInstance().getGlobalContainerStack().definition.getId():
  90. return
  91. self._update()
  92. ## This is an abstract method that needs to be implemented by the specific
  93. # models themselves.
  94. def _update(self):
  95. self._favorite_ids = set(cura.CuraApplication.CuraApplication.getInstance().getPreferences().getValue("cura/favorite_materials").split(";"))
  96. # Update the available materials (ContainerNode) for the current active machine and extruder setup.
  97. global_stack = cura.CuraApplication.CuraApplication.getInstance().getGlobalContainerStack()
  98. extruder_stack = global_stack.extruders.get(str(self._extruder_position))
  99. if not extruder_stack:
  100. return
  101. nozzle_name = extruder_stack.variant.getName()
  102. materials = ContainerTree.getInstance().machines[global_stack.definition.getId()].variants[nozzle_name].materials
  103. compatible_material_diameter = str(round(extruder_stack.getCompatibleMaterialDiameter()))
  104. self._available_materials = {key: material for key, material in materials.items() if material.container.getMetaDataEntry("approximate_diameter") == compatible_material_diameter}
  105. ## This method is used by all material models in the beginning of the
  106. # _update() method in order to prevent errors. It's the same in all models
  107. # so it's placed here for easy access.
  108. def _canUpdate(self):
  109. global_stack = self._machine_manager.activeMachine
  110. if global_stack is None or not self._enabled:
  111. return False
  112. extruder_position = str(self._extruder_position)
  113. if extruder_position not in global_stack.extruders:
  114. return False
  115. return True
  116. ## This is another convenience function which is shared by all material
  117. # models so it's put here to avoid having so much duplicated code.
  118. def _createMaterialItem(self, root_material_id, container_node):
  119. metadata_list = CuraContainerRegistry.getInstance().findContainersMetadata(id = container_node.container_id)
  120. if not metadata_list:
  121. return None
  122. metadata = metadata_list[0]
  123. item = {
  124. "root_material_id": root_material_id,
  125. "id": metadata["id"],
  126. "container_id": metadata["id"], # TODO: Remove duplicate in material manager qml
  127. "GUID": metadata["GUID"],
  128. "name": metadata["name"],
  129. "brand": metadata["brand"],
  130. "description": metadata["description"],
  131. "material": metadata["material"],
  132. "color_name": metadata["color_name"],
  133. "color_code": metadata.get("color_code", ""),
  134. "density": metadata.get("properties", {}).get("density", ""),
  135. "diameter": metadata.get("properties", {}).get("diameter", ""),
  136. "approximate_diameter": metadata["approximate_diameter"],
  137. "adhesion_info": metadata["adhesion_info"],
  138. "is_read_only": self._container_registry.isReadOnly(metadata["id"]),
  139. "container_node": container_node,
  140. "is_favorite": root_material_id in self._favorite_ids
  141. }
  142. return item