ExtrudersModel.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # Copyright (c) 2016 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. from PyQt5.QtCore import Qt, pyqtSignal, pyqtProperty
  4. import UM.Qt.ListModel
  5. from . import ExtruderManager
  6. ## Model that holds extruders.
  7. #
  8. # This model is designed for use by any list of extruders, but specifically
  9. # intended for drop-down lists of the current machine's extruders in place of
  10. # settings.
  11. class ExtrudersModel(UM.Qt.ListModel.ListModel):
  12. # The ID of the container stack for the extruder.
  13. IdRole = Qt.UserRole + 1
  14. ## Human-readable name of the extruder.
  15. NameRole = Qt.UserRole + 2
  16. ## Colour of the material loaded in the extruder.
  17. ColorRole = Qt.UserRole + 3
  18. ## Index of the extruder, which is also the value of the setting itself.
  19. #
  20. # An index of 0 indicates the first extruder, an index of 1 the second
  21. # one, and so on. This is the value that will be saved in instance
  22. # containers.
  23. IndexRole = Qt.UserRole + 4
  24. ## List of colours to display if there is no material or the material has no known
  25. # colour.
  26. defaultColors = ["#ffc924", "#86ec21", "#22eeee", "#245bff", "#9124ff", "#ff24c8"]
  27. ## Initialises the extruders model, defining the roles and listening for
  28. # changes in the data.
  29. #
  30. # \param parent Parent QtObject of this list.
  31. def __init__(self, parent = None):
  32. super().__init__(parent)
  33. self.addRoleName(self.IdRole, "id")
  34. self.addRoleName(self.NameRole, "name")
  35. self.addRoleName(self.ColorRole, "color")
  36. self.addRoleName(self.IndexRole, "index")
  37. self._add_global = False
  38. self._simple_names = False
  39. self._active_extruder_stack = None
  40. #Listen to changes.
  41. UM.Application.getInstance().globalContainerStackChanged.connect(self._updateExtruders)
  42. manager = ExtruderManager.getInstance()
  43. self._updateExtruders()
  44. manager.activeExtruderChanged.connect(self._onActiveExtruderChanged)
  45. self._onActiveExtruderChanged()
  46. def setAddGlobal(self, add):
  47. if add != self._add_global:
  48. self._add_global = add
  49. self._updateExtruders()
  50. self.addGlobalChanged.emit()
  51. addGlobalChanged = pyqtSignal()
  52. @pyqtProperty(bool, fset = setAddGlobal, notify = addGlobalChanged)
  53. def addGlobal(self):
  54. return self._add_global
  55. ## Set the simpleNames property.
  56. def setSimpleNames(self, simple_names):
  57. if simple_names != self._simple_names:
  58. self._simple_names = simple_names
  59. self.simpleNamesChanged.emit()
  60. self._updateExtruders()
  61. ## Emitted when the simpleNames property changes.
  62. simpleNamesChanged = pyqtSignal()
  63. ## Whether or not the model should show all definitions regardless of visibility.
  64. @pyqtProperty(bool, fset = setSimpleNames, notify = simpleNamesChanged)
  65. def simpleNames(self):
  66. return self._simple_names
  67. def _onActiveExtruderChanged(self):
  68. manager = ExtruderManager.getInstance()
  69. active_extruder_stack = manager.getActiveExtruderStack()
  70. if self._active_extruder_stack != active_extruder_stack:
  71. if self._active_extruder_stack:
  72. self._active_extruder_stack.containersChanged.disconnect(self._onExtruderStackContainersChanged)
  73. if active_extruder_stack:
  74. # Update the model when the material container is changed
  75. active_extruder_stack.containersChanged.connect(self._onExtruderStackContainersChanged)
  76. self._active_extruder_stack = active_extruder_stack
  77. def _onExtruderStackContainersChanged(self, container):
  78. # The ExtrudersModel needs to be updated when the material-name or -color changes, because the user identifies extruders by material-name
  79. if container.getMetaDataEntry("type") == "material":
  80. self._updateExtruders()
  81. modelChanged = pyqtSignal()
  82. ## Update the list of extruders.
  83. #
  84. # This should be called whenever the list of extruders changes.
  85. def _updateExtruders(self):
  86. changed = False
  87. if self.rowCount() != 0:
  88. changed = True
  89. items = []
  90. global_container_stack = UM.Application.getInstance().getGlobalContainerStack()
  91. if global_container_stack:
  92. if self._add_global:
  93. material = global_container_stack.findContainer({ "type": "material" })
  94. color = material.getMetaDataEntry("color_code", default = self.defaultColors[0]) if material else self.defaultColors[0]
  95. item = {
  96. "id": global_container_stack.getId(),
  97. "name": "Global",
  98. "color": color,
  99. "index": -1
  100. }
  101. items.append(item)
  102. changed = True
  103. manager = ExtruderManager.getInstance()
  104. for extruder in manager.getMachineExtruders(global_container_stack.getId()):
  105. extruder_name = extruder.getName()
  106. material = extruder.findContainer({ "type": "material" })
  107. if material and not self._simple_names:
  108. extruder_name = "%s (%s)" % (material.getName(), extruder_name)
  109. position = extruder.getMetaDataEntry("position", default = "0") # Get the position
  110. try:
  111. position = int(position)
  112. except ValueError: #Not a proper int.
  113. position = -1
  114. default_color = self.defaultColors[position] if position >= 0 and position < len(self.defaultColors) else self.defaultColors[0]
  115. color = material.getMetaDataEntry("color_code", default = default_color) if material else default_color
  116. item = { #Construct an item with only the relevant information.
  117. "id": extruder.getId(),
  118. "name": extruder_name,
  119. "color": color,
  120. "index": position
  121. }
  122. items.append(item)
  123. changed = True
  124. if changed:
  125. items.sort(key = lambda i: i["index"])
  126. self.setItems(items)
  127. self.modelChanged.emit()