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 UM.Application import Application
  6. from cura.Settings.ExtruderManager import ExtruderManager
  7. ## Model that holds extruders.
  8. #
  9. # This model is designed for use by any list of extruders, but specifically
  10. # intended for drop-down lists of the current machine's extruders in place of
  11. # settings.
  12. class ExtrudersModel(UM.Qt.ListModel.ListModel):
  13. # The ID of the container stack for the extruder.
  14. IdRole = Qt.UserRole + 1
  15. ## Human-readable name of the extruder.
  16. NameRole = Qt.UserRole + 2
  17. ## Colour of the material loaded in the extruder.
  18. ColorRole = Qt.UserRole + 3
  19. ## Index of the extruder, which is also the value of the setting itself.
  20. #
  21. # An index of 0 indicates the first extruder, an index of 1 the second
  22. # one, and so on. This is the value that will be saved in instance
  23. # containers.
  24. IndexRole = Qt.UserRole + 4
  25. # The ID of the definition of the extruder.
  26. DefinitionRole = Qt.UserRole + 5
  27. ## List of colours to display if there is no material or the material has no known
  28. # colour.
  29. defaultColors = ["#ffc924", "#86ec21", "#22eeee", "#245bff", "#9124ff", "#ff24c8"]
  30. ## Initialises the extruders model, defining the roles and listening for
  31. # changes in the data.
  32. #
  33. # \param parent Parent QtObject of this list.
  34. def __init__(self, parent = None):
  35. super().__init__(parent)
  36. self.addRoleName(self.IdRole, "id")
  37. self.addRoleName(self.NameRole, "name")
  38. self.addRoleName(self.ColorRole, "color")
  39. self.addRoleName(self.IndexRole, "index")
  40. self.addRoleName(self.DefinitionRole, "definition")
  41. self._add_global = False
  42. self._simple_names = False
  43. self._active_extruder_stack = None
  44. #Listen to changes.
  45. Application.getInstance().globalContainerStackChanged.connect(self._updateExtruders)
  46. manager = ExtruderManager.getInstance()
  47. self._updateExtruders()
  48. manager.activeExtruderChanged.connect(self._onActiveExtruderChanged)
  49. self._onActiveExtruderChanged()
  50. def setAddGlobal(self, add):
  51. if add != self._add_global:
  52. self._add_global = add
  53. self._updateExtruders()
  54. self.addGlobalChanged.emit()
  55. addGlobalChanged = pyqtSignal()
  56. @pyqtProperty(bool, fset = setAddGlobal, notify = addGlobalChanged)
  57. def addGlobal(self):
  58. return self._add_global
  59. ## Set the simpleNames property.
  60. def setSimpleNames(self, simple_names):
  61. if simple_names != self._simple_names:
  62. self._simple_names = simple_names
  63. self.simpleNamesChanged.emit()
  64. self._updateExtruders()
  65. ## Emitted when the simpleNames property changes.
  66. simpleNamesChanged = pyqtSignal()
  67. ## Whether or not the model should show all definitions regardless of visibility.
  68. @pyqtProperty(bool, fset = setSimpleNames, notify = simpleNamesChanged)
  69. def simpleNames(self):
  70. return self._simple_names
  71. def _onActiveExtruderChanged(self):
  72. manager = ExtruderManager.getInstance()
  73. active_extruder_stack = manager.getActiveExtruderStack()
  74. if self._active_extruder_stack != active_extruder_stack:
  75. if self._active_extruder_stack:
  76. self._active_extruder_stack.containersChanged.disconnect(self._onExtruderStackContainersChanged)
  77. if active_extruder_stack:
  78. # Update the model when the material container is changed
  79. active_extruder_stack.containersChanged.connect(self._onExtruderStackContainersChanged)
  80. self._active_extruder_stack = active_extruder_stack
  81. def _onExtruderStackContainersChanged(self, container):
  82. # The ExtrudersModel needs to be updated when the material-name or -color changes, because the user identifies extruders by material-name
  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 = 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()