ExtrudersModel.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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, 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. ## 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._simple_names = False
  56. self._active_machine_extruders = [] # type: Iterable[ExtruderStack]
  57. self._add_optional_extruder = False
  58. # Listen to changes
  59. Application.getInstance().globalContainerStackChanged.connect(self._extrudersChanged) # When the machine is swapped we must update the active machine extruders
  60. Application.getInstance().getExtruderManager().extrudersChanged.connect(self._extrudersChanged) # When the extruders change we must link to the stack-changed signal of the new extruder
  61. Application.getInstance().getContainerRegistry().containerMetaDataChanged.connect(self._onExtruderStackContainersChanged) # When meta data from a material container changes we must update
  62. self._extrudersChanged() # Also calls _updateExtruders
  63. addOptionalExtruderChanged = pyqtSignal()
  64. def setAddOptionalExtruder(self, add_optional_extruder):
  65. if add_optional_extruder != self._add_optional_extruder:
  66. self._add_optional_extruder = add_optional_extruder
  67. self.addOptionalExtruderChanged.emit()
  68. self._updateExtruders()
  69. @pyqtProperty(bool, fset = setAddOptionalExtruder, notify = addOptionalExtruderChanged)
  70. def addOptionalExtruder(self):
  71. return self._add_optional_extruder
  72. ## Set the simpleNames property.
  73. def setSimpleNames(self, simple_names):
  74. if simple_names != self._simple_names:
  75. self._simple_names = simple_names
  76. self.simpleNamesChanged.emit()
  77. self._updateExtruders()
  78. ## Emitted when the simpleNames property changes.
  79. simpleNamesChanged = pyqtSignal()
  80. ## Whether or not the model should show all definitions regardless of visibility.
  81. @pyqtProperty(bool, fset = setSimpleNames, notify = simpleNamesChanged)
  82. def simpleNames(self):
  83. return self._simple_names
  84. ## Links to the stack-changed signal of the new extruders when an extruder
  85. # is swapped out or added in the current machine.
  86. #
  87. # \param machine_id The machine for which the extruders changed. This is
  88. # filled by the ExtruderManager.extrudersChanged signal when coming from
  89. # that signal. Application.globalContainerStackChanged doesn't fill this
  90. # signal; it's assumed to be the current printer in that case.
  91. def _extrudersChanged(self, machine_id = None):
  92. if machine_id is not None:
  93. if Application.getInstance().getGlobalContainerStack() is None:
  94. # No machine, don't need to update the current machine's extruders
  95. return
  96. if machine_id != Application.getInstance().getGlobalContainerStack().getId():
  97. # Not the current machine
  98. return
  99. # Unlink from old extruders
  100. for extruder in self._active_machine_extruders:
  101. extruder.containersChanged.disconnect(self._onExtruderStackContainersChanged)
  102. # Link to new extruders
  103. self._active_machine_extruders = []
  104. extruder_manager = Application.getInstance().getExtruderManager()
  105. for extruder in extruder_manager.getExtruderStacks():
  106. if extruder is None: #This extruder wasn't loaded yet. This happens asynchronously while this model is constructed from QML.
  107. continue
  108. extruder.containersChanged.connect(self._onExtruderStackContainersChanged)
  109. self._active_machine_extruders.append(extruder)
  110. self._updateExtruders() # Since the new extruders may have different properties, update our own model.
  111. def _onExtruderStackContainersChanged(self, container):
  112. # Update when there is an empty container or material change
  113. if container.getMetaDataEntry("type") == "material" or container.getMetaDataEntry("type") is None:
  114. # The ExtrudersModel needs to be updated when the material-name or -color changes, because the user identifies extruders by material-name
  115. self._updateExtruders()
  116. modelChanged = pyqtSignal()
  117. def _updateExtruders(self):
  118. self._update_extruder_timer.start()
  119. ## Update the list of extruders.
  120. #
  121. # This should be called whenever the list of extruders changes.
  122. @UM.FlameProfiler.profile
  123. def __updateExtruders(self):
  124. extruders_changed = False
  125. if self.rowCount() != 0:
  126. extruders_changed = True
  127. items = []
  128. global_container_stack = Application.getInstance().getGlobalContainerStack()
  129. if global_container_stack:
  130. # get machine extruder count for verification
  131. machine_extruder_count = global_container_stack.getProperty("machine_extruder_count", "value")
  132. for extruder in Application.getInstance().getExtruderManager().getMachineExtruders(global_container_stack.getId()):
  133. position = extruder.getMetaDataEntry("position", default = "0") # Get the position
  134. try:
  135. position = int(position)
  136. except ValueError:
  137. # Not a proper int.
  138. position = -1
  139. if position >= machine_extruder_count:
  140. continue
  141. default_color = self.defaultColors[position] if 0 <= position < len(self.defaultColors) else self.defaultColors[0]
  142. color = extruder.material.getMetaDataEntry("color_code", default = default_color) if extruder.material else default_color
  143. # construct an item with only the relevant information
  144. item = {
  145. "id": extruder.getId(),
  146. "name": extruder.getName(),
  147. "color": color,
  148. "index": position,
  149. "definition": extruder.getBottom().getId(),
  150. "material": extruder.material.getName() if extruder.material else "",
  151. "variant": extruder.variant.getName() if extruder.variant else "", # e.g. print core
  152. }
  153. items.append(item)
  154. extruders_changed = True
  155. if extruders_changed:
  156. # sort by extruder index
  157. items.sort(key = lambda i: i["index"])
  158. # We need optional extruder to be last, so add it after we do sorting.
  159. # This way we can simply interpret the -1 of the index as the last item (which it now always is)
  160. if self._add_optional_extruder:
  161. item = {
  162. "id": "",
  163. "name": catalog.i18nc("@menuitem", "Not overridden"),
  164. "color": "#ffffff",
  165. "index": -1,
  166. "definition": ""
  167. }
  168. items.append(item)
  169. self.setItems(items)
  170. self.modelChanged.emit()