BaseMaterialsModel.py 8.7 KB

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