ExtrudersModel.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. # Copyright (c) 2017 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. from PyQt5.QtCore import Qt, pyqtSignal, pyqtProperty, QTimer
  4. from typing import Iterable
  5. import UM.Qt.ListModel
  6. from UM.Application import Application
  7. import UM.FlameProfiler
  8. from cura.Settings.ExtruderManager import ExtruderManager
  9. from cura.Settings.ExtruderStack import ExtruderStack #To listen to changes on the extruders.
  10. from cura.Settings.MachineManager import MachineManager #To listen to changes on the extruders of the currently active machine.
  11. ## Model that holds extruders.
  12. #
  13. # This model is designed for use by any list of extruders, but specifically
  14. # intended for drop-down lists of the current machine's extruders in place of
  15. # settings.
  16. class ExtrudersModel(UM.Qt.ListModel.ListModel):
  17. # The ID of the container stack for the extruder.
  18. IdRole = Qt.UserRole + 1
  19. ## Human-readable name of the extruder.
  20. NameRole = Qt.UserRole + 2
  21. ## Colour of the material loaded in the extruder.
  22. ColorRole = Qt.UserRole + 3
  23. ## Index of the extruder, which is also the value of the setting itself.
  24. #
  25. # An index of 0 indicates the first extruder, an index of 1 the second
  26. # one, and so on. This is the value that will be saved in instance
  27. # containers.
  28. IndexRole = Qt.UserRole + 4
  29. # The ID of the definition of the extruder.
  30. DefinitionRole = Qt.UserRole + 5
  31. # The material of the extruder.
  32. MaterialRole = Qt.UserRole + 6
  33. # The variant of the extruder.
  34. VariantRole = Qt.UserRole + 7
  35. ## List of colours to display if there is no material or the material has no known
  36. # colour.
  37. defaultColors = ["#ffc924", "#86ec21", "#22eeee", "#245bff", "#9124ff", "#ff24c8"]
  38. ## Initialises the extruders model, defining the roles and listening for
  39. # changes in the data.
  40. #
  41. # \param parent Parent QtObject of this list.
  42. def __init__(self, parent = None):
  43. super().__init__(parent)
  44. self.addRoleName(self.IdRole, "id")
  45. self.addRoleName(self.NameRole, "name")
  46. self.addRoleName(self.ColorRole, "color")
  47. self.addRoleName(self.IndexRole, "index")
  48. self.addRoleName(self.DefinitionRole, "definition")
  49. self.addRoleName(self.MaterialRole, "material")
  50. self.addRoleName(self.VariantRole, "variant")
  51. self._update_extruder_timer = QTimer()
  52. self._update_extruder_timer.setInterval(100)
  53. self._update_extruder_timer.setSingleShot(True)
  54. self._update_extruder_timer.timeout.connect(self.__updateExtruders)
  55. self._add_global = False
  56. self._simple_names = False
  57. self._active_machine_extruders = [] # type: Iterable[ExtruderStack]
  58. self._add_optional_extruder = False
  59. #Listen to changes.
  60. Application.getInstance().globalContainerStackChanged.connect(self._extrudersChanged) #When the machine is swapped we must update the active machine extruders.
  61. ExtruderManager.getInstance().extrudersChanged.connect(self._extrudersChanged) #When the extruders change we must link to the stack-changed signal of the new extruder.
  62. self._extrudersChanged() #Also calls _updateExtruders.
  63. def setAddGlobal(self, add):
  64. if add != self._add_global:
  65. self._add_global = add
  66. self._updateExtruders()
  67. self.addGlobalChanged.emit()
  68. addGlobalChanged = pyqtSignal()
  69. @pyqtProperty(bool, fset = setAddGlobal, notify = addGlobalChanged)
  70. def addGlobal(self):
  71. return self._add_global
  72. addOptionalExtruderChanged = pyqtSignal()
  73. def setAddOptionalExtruder(self, add_optional_extruder):
  74. if add_optional_extruder != self._add_optional_extruder:
  75. self._add_optional_extruder = add_optional_extruder
  76. self.addOptionalExtruderChanged.emit()
  77. self._updateExtruders()
  78. @pyqtProperty(bool, fset = setAddOptionalExtruder, notify = addOptionalExtruderChanged)
  79. def addOptionalExtruder(self):
  80. return self._add_optional_extruder
  81. ## Set the simpleNames property.
  82. def setSimpleNames(self, simple_names):
  83. if simple_names != self._simple_names:
  84. self._simple_names = simple_names
  85. self.simpleNamesChanged.emit()
  86. self._updateExtruders()
  87. ## Emitted when the simpleNames property changes.
  88. simpleNamesChanged = pyqtSignal()
  89. ## Whether or not the model should show all definitions regardless of visibility.
  90. @pyqtProperty(bool, fset = setSimpleNames, notify = simpleNamesChanged)
  91. def simpleNames(self):
  92. return self._simple_names
  93. ## Links to the stack-changed signal of the new extruders when an extruder
  94. # is swapped out or added in the current machine.
  95. #
  96. # \param machine_id The machine for which the extruders changed. This is
  97. # filled by the ExtruderManager.extrudersChanged signal when coming from
  98. # that signal. Application.globalContainerStackChanged doesn't fill this
  99. # signal; it's assumed to be the current printer in that case.
  100. def _extrudersChanged(self, machine_id = None):
  101. if machine_id is not None:
  102. if Application.getInstance().getGlobalContainerStack() is None:
  103. return #No machine, don't need to update the current machine's extruders.
  104. if machine_id != Application.getInstance().getGlobalContainerStack().getId():
  105. return #Not the current machine.
  106. #Unlink from old extruders.
  107. for extruder in self._active_machine_extruders:
  108. extruder.containersChanged.disconnect(self._onExtruderStackContainersChanged)
  109. #Link to new extruders.
  110. self._active_machine_extruders = []
  111. extruder_manager = ExtruderManager.getInstance()
  112. for extruder in extruder_manager.getExtruderStacks():
  113. extruder.containersChanged.connect(self._onExtruderStackContainersChanged)
  114. self._active_machine_extruders.append(extruder)
  115. self._updateExtruders() #Since the new extruders may have different properties, update our own model.
  116. def _onExtruderStackContainersChanged(self, container):
  117. # Update when there is an empty container or material change
  118. if container.getMetaDataEntry("type") == "material" or container.getMetaDataEntry("type") is None:
  119. # The ExtrudersModel needs to be updated when the material-name or -color changes, because the user identifies extruders by material-name
  120. self._updateExtruders()
  121. modelChanged = pyqtSignal()
  122. def _updateExtruders(self):
  123. self._update_extruder_timer.start()
  124. ## Update the list of extruders.
  125. #
  126. # This should be called whenever the list of extruders changes.
  127. @UM.FlameProfiler.profile
  128. def __updateExtruders(self):
  129. changed = False
  130. if self.rowCount() != 0:
  131. changed = True
  132. items = []
  133. global_container_stack = Application.getInstance().getGlobalContainerStack()
  134. if global_container_stack:
  135. if self._add_global:
  136. material = global_container_stack.material
  137. color = material.getMetaDataEntry("color_code", default = self.defaultColors[0]) if material else self.defaultColors[0]
  138. item = {
  139. "id": global_container_stack.getId(),
  140. "name": "Global",
  141. "color": color,
  142. "index": -1,
  143. "definition": ""
  144. }
  145. items.append(item)
  146. changed = True
  147. machine_extruder_count = global_container_stack.getProperty("machine_extruder_count", "value")
  148. manager = ExtruderManager.getInstance()
  149. for extruder in manager.getMachineExtruders(global_container_stack.getId()):
  150. position = extruder.getMetaDataEntry("position", default = "0") # Get the position
  151. try:
  152. position = int(position)
  153. except ValueError: #Not a proper int.
  154. position = -1
  155. if position >= machine_extruder_count:
  156. continue
  157. extruder_name = extruder.getName()
  158. material = extruder.material
  159. variant = extruder.variant
  160. default_color = self.defaultColors[position] if position >= 0 and position < len(self.defaultColors) else self.defaultColors[0]
  161. color = material.getMetaDataEntry("color_code", default = default_color) if material else default_color
  162. item = { #Construct an item with only the relevant information.
  163. "id": extruder.getId(),
  164. "name": extruder_name,
  165. "color": color,
  166. "index": position,
  167. "definition": extruder.getBottom().getId(),
  168. "material": material.getName() if material else "",
  169. "variant": variant.getName() if variant else "",
  170. }
  171. items.append(item)
  172. changed = True
  173. if changed:
  174. items.sort(key = lambda i: i["index"])
  175. # We need optional extruder to be last, so add it after we do sorting.
  176. # This way we can simply intrepret the -1 of the index as the last item (which it now always is)
  177. if self._add_optional_extruder:
  178. item = {
  179. "id": "",
  180. "name": "Not overridden",
  181. "color": "#ffffff",
  182. "index": -1,
  183. "definition": ""
  184. }
  185. items.append(item)
  186. self.setItems(items)
  187. self.modelChanged.emit()