ExtrudersModel.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. # Copyright (c) 2017 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from PyQt5.QtCore import Qt, pyqtSignal, pyqtSlot, pyqtProperty, QTimer
  4. from typing import Iterable
  5. from UM.i18n import i18nCatalog
  6. import UM.Qt.ListModel
  7. from UM.Application import Application
  8. import UM.FlameProfiler
  9. from cura.Settings.ExtruderStack import ExtruderStack # To listen to changes on the extruders.
  10. catalog = i18nCatalog("cura")
  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. ## Is the extruder enabled?
  22. EnabledRole = Qt.UserRole + 9
  23. ## Colour of the material loaded in the extruder.
  24. ColorRole = Qt.UserRole + 3
  25. ## Index of the extruder, which is also the value of the setting itself.
  26. #
  27. # An index of 0 indicates the first extruder, an index of 1 the second
  28. # one, and so on. This is the value that will be saved in instance
  29. # containers.
  30. IndexRole = Qt.UserRole + 4
  31. # The ID of the definition of the extruder.
  32. DefinitionRole = Qt.UserRole + 5
  33. # The material of the extruder.
  34. MaterialRole = Qt.UserRole + 6
  35. # The variant of the extruder.
  36. VariantRole = Qt.UserRole + 7
  37. StackRole = Qt.UserRole + 8
  38. MaterialBrandRole = Qt.UserRole + 9
  39. ColorNameRole = Qt.UserRole + 10
  40. ## List of colours to display if there is no material or the material has no known
  41. # colour.
  42. defaultColors = ["#ffc924", "#86ec21", "#22eeee", "#245bff", "#9124ff", "#ff24c8"]
  43. ## Initialises the extruders model, defining the roles and listening for
  44. # changes in the data.
  45. #
  46. # \param parent Parent QtObject of this list.
  47. def __init__(self, parent = None):
  48. super().__init__(parent)
  49. self.addRoleName(self.IdRole, "id")
  50. self.addRoleName(self.NameRole, "name")
  51. self.addRoleName(self.EnabledRole, "enabled")
  52. self.addRoleName(self.ColorRole, "color")
  53. self.addRoleName(self.IndexRole, "index")
  54. self.addRoleName(self.DefinitionRole, "definition")
  55. self.addRoleName(self.MaterialRole, "material")
  56. self.addRoleName(self.VariantRole, "variant")
  57. self.addRoleName(self.StackRole, "stack")
  58. self.addRoleName(self.MaterialBrandRole, "material_brand")
  59. self.addRoleName(self.ColorNameRole, "color_name")
  60. self._update_extruder_timer = QTimer()
  61. self._update_extruder_timer.setInterval(100)
  62. self._update_extruder_timer.setSingleShot(True)
  63. self._update_extruder_timer.timeout.connect(self.__updateExtruders)
  64. self._simple_names = False
  65. self._active_machine_extruders = [] # type: Iterable[ExtruderStack]
  66. self._add_optional_extruder = False
  67. # Listen to changes
  68. Application.getInstance().globalContainerStackChanged.connect(self._extrudersChanged) # When the machine is swapped we must update the active machine extruders
  69. Application.getInstance().getExtruderManager().extrudersChanged.connect(self._extrudersChanged) # When the extruders change we must link to the stack-changed signal of the new extruder
  70. Application.getInstance().getContainerRegistry().containerMetaDataChanged.connect(self._onExtruderStackContainersChanged) # When meta data from a material container changes we must update
  71. self._extrudersChanged() # Also calls _updateExtruders
  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. # No machine, don't need to update the current machine's extruders
  104. return
  105. if machine_id != Application.getInstance().getGlobalContainerStack().getId():
  106. # Not the current machine
  107. return
  108. # Unlink from old extruders
  109. for extruder in self._active_machine_extruders:
  110. extruder.containersChanged.disconnect(self._onExtruderStackContainersChanged)
  111. # Link to new extruders
  112. self._active_machine_extruders = []
  113. extruder_manager = Application.getInstance().getExtruderManager()
  114. for extruder in extruder_manager.getActiveExtruderStacks():
  115. if extruder is None: #This extruder wasn't loaded yet. This happens asynchronously while this model is constructed from QML.
  116. continue
  117. extruder.containersChanged.connect(self._onExtruderStackContainersChanged)
  118. self._active_machine_extruders.append(extruder)
  119. self._updateExtruders() # Since the new extruders may have different properties, update our own model.
  120. def _onExtruderStackContainersChanged(self, container):
  121. # Update when there is an empty container or material change
  122. if container.getMetaDataEntry("type") == "material" or container.getMetaDataEntry("type") is None:
  123. # The ExtrudersModel needs to be updated when the material-name or -color changes, because the user identifies extruders by material-name
  124. self._updateExtruders()
  125. modelChanged = pyqtSignal()
  126. def _updateExtruders(self):
  127. self._update_extruder_timer.start()
  128. ## Update the list of extruders.
  129. #
  130. # This should be called whenever the list of extruders changes.
  131. @UM.FlameProfiler.profile
  132. def __updateExtruders(self):
  133. extruders_changed = False
  134. if self.rowCount() != 0:
  135. extruders_changed = True
  136. items = []
  137. global_container_stack = Application.getInstance().getGlobalContainerStack()
  138. if global_container_stack:
  139. # get machine extruder count for verification
  140. machine_extruder_count = global_container_stack.getProperty("machine_extruder_count", "value")
  141. for extruder in Application.getInstance().getExtruderManager().getActiveExtruderStacks():
  142. position = extruder.getMetaDataEntry("position", default = "0") # Get the position
  143. try:
  144. position = int(position)
  145. except ValueError:
  146. # Not a proper int.
  147. position = -1
  148. if position >= machine_extruder_count:
  149. continue
  150. default_color = self.defaultColors[position] if 0 <= position < len(self.defaultColors) else self.defaultColors[0]
  151. color = extruder.material.getMetaDataEntry("color_code", default = default_color) if extruder.material else default_color
  152. material_brand = extruder.material.getMetaDataEntry("brand", default = "generic")
  153. color_name = extruder.material.getMetaDataEntry("color_name")
  154. # construct an item with only the relevant information
  155. item = {
  156. "id": extruder.getId(),
  157. "name": extruder.getName(),
  158. "enabled": extruder.isEnabled,
  159. "color": color,
  160. "index": position,
  161. "definition": extruder.getBottom().getId(),
  162. "material": extruder.material.getName() if extruder.material else "",
  163. "variant": extruder.variant.getName() if extruder.variant else "", # e.g. print core
  164. "stack": extruder,
  165. "material_brand": material_brand,
  166. "color_name": color_name
  167. }
  168. items.append(item)
  169. extruders_changed = True
  170. if extruders_changed:
  171. # sort by extruder index
  172. items.sort(key = lambda i: i["index"])
  173. # We need optional extruder to be last, so add it after we do sorting.
  174. # This way we can simply interpret the -1 of the index as the last item (which it now always is)
  175. if self._add_optional_extruder:
  176. item = {
  177. "id": "",
  178. "name": catalog.i18nc("@menuitem", "Not overridden"),
  179. "enabled": True,
  180. "color": "#ffffff",
  181. "index": -1,
  182. "definition": ""
  183. }
  184. items.append(item)
  185. self.setItems(items)
  186. self.modelChanged.emit()