ExtrudersModel.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. ## List of colours to display if there is no material or the material has no known
  39. # colour.
  40. defaultColors = ["#ffc924", "#86ec21", "#22eeee", "#245bff", "#9124ff", "#ff24c8"]
  41. ## Initialises the extruders model, defining the roles and listening for
  42. # changes in the data.
  43. #
  44. # \param parent Parent QtObject of this list.
  45. def __init__(self, parent = None):
  46. super().__init__(parent)
  47. self.addRoleName(self.IdRole, "id")
  48. self.addRoleName(self.NameRole, "name")
  49. self.addRoleName(self.EnabledRole, "enabled")
  50. self.addRoleName(self.ColorRole, "color")
  51. self.addRoleName(self.IndexRole, "index")
  52. self.addRoleName(self.DefinitionRole, "definition")
  53. self.addRoleName(self.MaterialRole, "material")
  54. self.addRoleName(self.VariantRole, "variant")
  55. self.addRoleName(self.StackRole, "stack")
  56. self._update_extruder_timer = QTimer()
  57. self._update_extruder_timer.setInterval(100)
  58. self._update_extruder_timer.setSingleShot(True)
  59. self._update_extruder_timer.timeout.connect(self.__updateExtruders)
  60. self._simple_names = False
  61. self._active_machine_extruders = [] # type: Iterable[ExtruderStack]
  62. self._add_optional_extruder = False
  63. # Listen to changes
  64. Application.getInstance().globalContainerStackChanged.connect(self._extrudersChanged) # When the machine is swapped we must update the active machine extruders
  65. Application.getInstance().getExtruderManager().extrudersChanged.connect(self._extrudersChanged) # When the extruders change we must link to the stack-changed signal of the new extruder
  66. Application.getInstance().getContainerRegistry().containerMetaDataChanged.connect(self._onExtruderStackContainersChanged) # When meta data from a material container changes we must update
  67. self._extrudersChanged() # Also calls _updateExtruders
  68. addOptionalExtruderChanged = pyqtSignal()
  69. def setAddOptionalExtruder(self, add_optional_extruder):
  70. if add_optional_extruder != self._add_optional_extruder:
  71. self._add_optional_extruder = add_optional_extruder
  72. self.addOptionalExtruderChanged.emit()
  73. self._updateExtruders()
  74. @pyqtProperty(bool, fset = setAddOptionalExtruder, notify = addOptionalExtruderChanged)
  75. def addOptionalExtruder(self):
  76. return self._add_optional_extruder
  77. ## Set the simpleNames property.
  78. def setSimpleNames(self, simple_names):
  79. if simple_names != self._simple_names:
  80. self._simple_names = simple_names
  81. self.simpleNamesChanged.emit()
  82. self._updateExtruders()
  83. ## Emitted when the simpleNames property changes.
  84. simpleNamesChanged = pyqtSignal()
  85. ## Whether or not the model should show all definitions regardless of visibility.
  86. @pyqtProperty(bool, fset = setSimpleNames, notify = simpleNamesChanged)
  87. def simpleNames(self):
  88. return self._simple_names
  89. ## Links to the stack-changed signal of the new extruders when an extruder
  90. # is swapped out or added in the current machine.
  91. #
  92. # \param machine_id The machine for which the extruders changed. This is
  93. # filled by the ExtruderManager.extrudersChanged signal when coming from
  94. # that signal. Application.globalContainerStackChanged doesn't fill this
  95. # signal; it's assumed to be the current printer in that case.
  96. def _extrudersChanged(self, machine_id = None):
  97. if machine_id is not None:
  98. if Application.getInstance().getGlobalContainerStack() is None:
  99. # No machine, don't need to update the current machine's extruders
  100. return
  101. if machine_id != Application.getInstance().getGlobalContainerStack().getId():
  102. # Not the current machine
  103. return
  104. # Unlink from old extruders
  105. for extruder in self._active_machine_extruders:
  106. extruder.containersChanged.disconnect(self._onExtruderStackContainersChanged)
  107. # Link to new extruders
  108. self._active_machine_extruders = []
  109. extruder_manager = Application.getInstance().getExtruderManager()
  110. for extruder in extruder_manager.getActiveExtruderStacks():
  111. if extruder is None: #This extruder wasn't loaded yet. This happens asynchronously while this model is constructed from QML.
  112. continue
  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. extruders_changed = False
  130. if self.rowCount() != 0:
  131. extruders_changed = True
  132. items = []
  133. global_container_stack = Application.getInstance().getGlobalContainerStack()
  134. if global_container_stack:
  135. # get machine extruder count for verification
  136. machine_extruder_count = global_container_stack.getProperty("machine_extruder_count", "value")
  137. for extruder in Application.getInstance().getExtruderManager().getActiveExtruderStacks():
  138. position = extruder.getMetaDataEntry("position", default = "0") # Get the position
  139. try:
  140. position = int(position)
  141. except ValueError:
  142. # Not a proper int.
  143. position = -1
  144. if position >= machine_extruder_count:
  145. continue
  146. default_color = self.defaultColors[position] if 0 <= position < len(self.defaultColors) else self.defaultColors[0]
  147. color = extruder.material.getMetaDataEntry("color_code", default = default_color) if extruder.material else default_color
  148. # construct an item with only the relevant information
  149. item = {
  150. "id": extruder.getId(),
  151. "name": extruder.getName(),
  152. "enabled": extruder.isEnabled,
  153. "color": color,
  154. "index": position,
  155. "definition": extruder.getBottom().getId(),
  156. "material": extruder.material.getName() if extruder.material else "",
  157. "variant": extruder.variant.getName() if extruder.variant else "", # e.g. print core
  158. "stack": extruder,
  159. }
  160. items.append(item)
  161. extruders_changed = True
  162. if extruders_changed:
  163. # sort by extruder index
  164. items.sort(key = lambda i: i["index"])
  165. # We need optional extruder to be last, so add it after we do sorting.
  166. # This way we can simply interpret the -1 of the index as the last item (which it now always is)
  167. if self._add_optional_extruder:
  168. item = {
  169. "id": "",
  170. "name": catalog.i18nc("@menuitem", "Not overridden"),
  171. "enabled": True,
  172. "color": "#ffffff",
  173. "index": -1,
  174. "definition": ""
  175. }
  176. items.append(item)
  177. self.setItems(items)
  178. self.modelChanged.emit()