BaseMaterialsModel.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. try:
  60. self._extruder_stack = global_stack.extruderList[self._extruder_position]
  61. except IndexError:
  62. self._extruder_stack = None
  63. if self._extruder_stack is not None:
  64. self._extruder_stack.pyqtContainersChanged.connect(self._update)
  65. self._extruder_stack.approximateMaterialDiameterChanged.connect(self._update)
  66. # Force update the model when the extruder stack changes
  67. self._update()
  68. def setExtruderPosition(self, position: int):
  69. if self._extruder_stack is None or self._extruder_position != position:
  70. self._extruder_position = position
  71. self._updateExtruderStack()
  72. self.extruderPositionChanged.emit()
  73. @pyqtProperty(int, fset = setExtruderPosition, notify = extruderPositionChanged)
  74. def extruderPosition(self) -> int:
  75. return self._extruder_position
  76. def setEnabled(self, enabled):
  77. if self._enabled != enabled:
  78. self._enabled = enabled
  79. if self._enabled:
  80. # ensure the data is there again.
  81. self._update()
  82. self.enabledChanged.emit()
  83. @pyqtProperty(bool, fset = setEnabled, notify = enabledChanged)
  84. def enabled(self):
  85. return self._enabled
  86. ## Triggered when a list of materials changed somewhere in the container
  87. # tree. This change may trigger an _update() call when the materials
  88. # changed for the configuration that this model is looking for.
  89. def _materialsListChanged(self, material: MaterialNode) -> None:
  90. if self._extruder_stack is None:
  91. return
  92. if material.variant.container_id != self._extruder_stack.variant.getId():
  93. return
  94. global_stack = cura.CuraApplication.CuraApplication.getInstance().getGlobalContainerStack()
  95. if not global_stack:
  96. return
  97. if material.variant.machine.container_id != global_stack.definition.getId():
  98. return
  99. self._update()
  100. ## Triggered when the list of favorite materials is changed.
  101. def _favoritesChanged(self, material_base_file: str) -> None:
  102. if material_base_file in self._available_materials:
  103. self._update()
  104. ## This is an abstract method that needs to be implemented by the specific
  105. # models themselves.
  106. def _update(self):
  107. self._favorite_ids = set(cura.CuraApplication.CuraApplication.getInstance().getPreferences().getValue("cura/favorite_materials").split(";"))
  108. # Update the available materials (ContainerNode) for the current active machine and extruder setup.
  109. global_stack = cura.CuraApplication.CuraApplication.getInstance().getGlobalContainerStack()
  110. if not global_stack.hasMaterials:
  111. return # There are no materials for this machine, so nothing to do.
  112. extruder_stack = global_stack.extruders.get(str(self._extruder_position))
  113. if not extruder_stack:
  114. return
  115. nozzle_name = extruder_stack.variant.getName()
  116. materials = ContainerTree.getInstance().machines[global_stack.definition.getId()].variants[nozzle_name].materials
  117. approximate_material_diameter = extruder_stack.getApproximateMaterialDiameter()
  118. self._available_materials = {key: material for key, material in materials.items() if float(material.container.getMetaDataEntry("approximate_diameter")) == approximate_material_diameter}
  119. ## This method is used by all material models in the beginning of the
  120. # _update() method in order to prevent errors. It's the same in all models
  121. # so it's placed here for easy access.
  122. def _canUpdate(self):
  123. global_stack = self._machine_manager.activeMachine
  124. if global_stack is None or not self._enabled:
  125. return False
  126. extruder_position = str(self._extruder_position)
  127. if extruder_position not in global_stack.extruders:
  128. return False
  129. return True
  130. ## This is another convenience function which is shared by all material
  131. # models so it's put here to avoid having so much duplicated code.
  132. def _createMaterialItem(self, root_material_id, container_node):
  133. metadata_list = CuraContainerRegistry.getInstance().findContainersMetadata(id = container_node.container_id)
  134. if not metadata_list:
  135. return None
  136. metadata = metadata_list[0]
  137. item = {
  138. "root_material_id": root_material_id,
  139. "id": metadata["id"],
  140. "container_id": metadata["id"], # TODO: Remove duplicate in material manager qml
  141. "GUID": metadata["GUID"],
  142. "name": metadata["name"],
  143. "brand": metadata["brand"],
  144. "description": metadata["description"],
  145. "material": metadata["material"],
  146. "color_name": metadata["color_name"],
  147. "color_code": metadata.get("color_code", ""),
  148. "density": metadata.get("properties", {}).get("density", ""),
  149. "diameter": metadata.get("properties", {}).get("diameter", ""),
  150. "approximate_diameter": metadata["approximate_diameter"],
  151. "adhesion_info": metadata["adhesion_info"],
  152. "is_read_only": self._container_registry.isReadOnly(metadata["id"]),
  153. "container_node": container_node,
  154. "is_favorite": root_material_id in self._favorite_ids
  155. }
  156. return item