ExtrudersModel.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. ColourRole = 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. defaultColours = ["#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.ColourRole, "colour")
  36. self.addRoleName(self.IndexRole, "index")
  37. self._add_global = False
  38. self._active_extruder_stack = None
  39. #Listen to changes.
  40. manager = ExtruderManager.getInstance()
  41. manager.extrudersChanged.connect(self._updateExtruders) #When the list of extruders changes in general.
  42. self._updateExtruders()
  43. manager.activeExtruderChanged.connect(self._onActiveExtruderChanged)
  44. self._onActiveExtruderChanged()
  45. def setAddGlobal(self, add):
  46. if add != self._add_global:
  47. self._add_global = add
  48. self._updateExtruders()
  49. self.addGlobalChanged.emit()
  50. addGlobalChanged = pyqtSignal()
  51. @pyqtProperty(bool, fset = setAddGlobal, notify = addGlobalChanged)
  52. def addGlobal(self):
  53. return self._add_global
  54. def _onActiveExtruderChanged(self):
  55. manager = ExtruderManager.getInstance()
  56. active_extruder_stack = manager.getActiveExtruderStack()
  57. if self._active_extruder_stack != active_extruder_stack:
  58. if self._active_extruder_stack:
  59. self._active_extruder_stack.containersChanged.disconnect(self._onExtruderStackContainersChanged)
  60. if active_extruder_stack:
  61. # Update the model when the material container is changed
  62. active_extruder_stack.containersChanged.connect(self._onExtruderStackContainersChanged)
  63. self._active_extruder_stack = active_extruder_stack
  64. def _onExtruderStackContainersChanged(self, container):
  65. # The ExtrudersModel needs to be updated when the material-name or -color changes, because the user identifies extruders by material-name
  66. if container.getMetaDataEntry("type") == "material":
  67. self._updateExtruders()
  68. modelChanged = pyqtSignal()
  69. ## Update the list of extruders.
  70. #
  71. # This should be called whenever the list of extruders changes.
  72. def _updateExtruders(self):
  73. changed = False
  74. if self.rowCount() != 0:
  75. self.clear()
  76. changed = True
  77. global_container_stack = UM.Application.getInstance().getGlobalContainerStack()
  78. if global_container_stack:
  79. if self._add_global:
  80. material = global_container_stack.findContainer({ "type": "material" })
  81. colour = material.getMetaDataEntry("color_code", default = self.defaultColours[0]) if material else self.defaultColours[0]
  82. item = {
  83. "id": global_container_stack.getId(),
  84. "name": "Global",
  85. "colour": colour,
  86. "index": -1
  87. }
  88. self.appendItem(item)
  89. changed = True
  90. manager = ExtruderManager.getInstance()
  91. for extruder in manager.getMachineExtruders(global_container_stack.getBottom().getId()):
  92. extruder_name = extruder.getName()
  93. material = extruder.findContainer({ "type": "material" })
  94. if material:
  95. extruder_name = "%s (%s)" % (material.getName(), extruder_name)
  96. position = extruder.getBottom().getMetaDataEntry("position", default = "0") #Position in the definition.
  97. try:
  98. position = int(position)
  99. except ValueError: #Not a proper int.
  100. position = -1
  101. default_colour = self.defaultColours[position] if position >= 0 and position < len(self.defaultColours) else self.defaultColours[0]
  102. colour = material.getMetaDataEntry("color_code", default = default_colour) if material else default_colour
  103. item = { #Construct an item with only the relevant information.
  104. "id": extruder.getId(),
  105. "name": extruder_name,
  106. "colour": colour,
  107. "index": position
  108. }
  109. self.appendItem(item)
  110. changed = True
  111. if changed:
  112. self.sort(lambda item: item["index"])
  113. self.modelChanged.emit()