ExtrudersModel.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. # The ID of the definition of the extruder.
  25. DefinitionRole = Qt.UserRole + 5
  26. ## List of colours to display if there is no material or the material has no known
  27. # colour.
  28. defaultColors = ["#ffc924", "#86ec21", "#22eeee", "#245bff", "#9124ff", "#ff24c8"]
  29. ## Initialises the extruders model, defining the roles and listening for
  30. # changes in the data.
  31. #
  32. # \param parent Parent QtObject of this list.
  33. def __init__(self, parent = None):
  34. super().__init__(parent)
  35. self.addRoleName(self.IdRole, "id")
  36. self.addRoleName(self.NameRole, "name")
  37. self.addRoleName(self.ColorRole, "color")
  38. self.addRoleName(self.IndexRole, "index")
  39. self.addRoleName(self.DefinitionRole, "definition")
  40. self._add_global = False
  41. self._simple_names = False
  42. self._active_extruder_stack = None
  43. #Listen to changes.
  44. UM.Application.getInstance().globalContainerStackChanged.connect(self._updateExtruders)
  45. manager = ExtruderManager.getInstance()
  46. self._updateExtruders()
  47. manager.activeExtruderChanged.connect(self._onActiveExtruderChanged)
  48. self._onActiveExtruderChanged()
  49. def setAddGlobal(self, add):
  50. if add != self._add_global:
  51. self._add_global = add
  52. self._updateExtruders()
  53. self.addGlobalChanged.emit()
  54. addGlobalChanged = pyqtSignal()
  55. @pyqtProperty(bool, fset = setAddGlobal, notify = addGlobalChanged)
  56. def addGlobal(self):
  57. return self._add_global
  58. ## Set the simpleNames property.
  59. def setSimpleNames(self, simple_names):
  60. if simple_names != self._simple_names:
  61. self._simple_names = simple_names
  62. self.simpleNamesChanged.emit()
  63. self._updateExtruders()
  64. ## Emitted when the simpleNames property changes.
  65. simpleNamesChanged = pyqtSignal()
  66. ## Whether or not the model should show all definitions regardless of visibility.
  67. @pyqtProperty(bool, fset = setSimpleNames, notify = simpleNamesChanged)
  68. def simpleNames(self):
  69. return self._simple_names
  70. def _onActiveExtruderChanged(self):
  71. manager = ExtruderManager.getInstance()
  72. active_extruder_stack = manager.getActiveExtruderStack()
  73. if self._active_extruder_stack != active_extruder_stack:
  74. if self._active_extruder_stack:
  75. self._active_extruder_stack.containersChanged.disconnect(self._onExtruderStackContainersChanged)
  76. if active_extruder_stack:
  77. # Update the model when the material container is changed
  78. active_extruder_stack.containersChanged.connect(self._onExtruderStackContainersChanged)
  79. self._active_extruder_stack = active_extruder_stack
  80. def _onExtruderStackContainersChanged(self, container):
  81. # The ExtrudersModel needs to be updated when the material-name or -color changes, because the user identifies extruders by material-name
  82. if container.getMetaDataEntry("type") == "material":
  83. self._updateExtruders()
  84. modelChanged = pyqtSignal()
  85. ## Update the list of extruders.
  86. #
  87. # This should be called whenever the list of extruders changes.
  88. def _updateExtruders(self):
  89. changed = False
  90. if self.rowCount() != 0:
  91. changed = True
  92. items = []
  93. global_container_stack = UM.Application.getInstance().getGlobalContainerStack()
  94. if global_container_stack:
  95. if self._add_global:
  96. material = global_container_stack.findContainer({ "type": "material" })
  97. color = material.getMetaDataEntry("color_code", default = self.defaultColors[0]) if material else self.defaultColors[0]
  98. item = {
  99. "id": global_container_stack.getId(),
  100. "name": "Global",
  101. "color": color,
  102. "index": -1,
  103. "definition": ""
  104. }
  105. items.append(item)
  106. changed = True
  107. manager = ExtruderManager.getInstance()
  108. for extruder in manager.getMachineExtruders(global_container_stack.getId()):
  109. extruder_name = extruder.getName()
  110. material = extruder.findContainer({ "type": "material" })
  111. position = extruder.getMetaDataEntry("position", default = "0") # Get the position
  112. try:
  113. position = int(position)
  114. except ValueError: #Not a proper int.
  115. position = -1
  116. default_color = self.defaultColors[position] if position >= 0 and position < len(self.defaultColors) else self.defaultColors[0]
  117. color = material.getMetaDataEntry("color_code", default = default_color) if material else default_color
  118. item = { #Construct an item with only the relevant information.
  119. "id": extruder.getId(),
  120. "name": extruder_name,
  121. "color": color,
  122. "index": position,
  123. "definition": extruder.getBottom().getId()
  124. }
  125. items.append(item)
  126. changed = True
  127. if changed:
  128. items.sort(key = lambda i: i["index"])
  129. self.setItems(items)
  130. self.modelChanged.emit()