mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-08-03 03:54:01 -06:00
Add an ID role and addGlobal property to ExtrudersModel
Contributes to CURA-340
This commit is contained in:
parent
1a573345f2
commit
d57e2b2e22
1 changed files with 36 additions and 5 deletions
|
@ -1,7 +1,7 @@
|
||||||
# Copyright (c) 2016 Ultimaker B.V.
|
# Copyright (c) 2016 Ultimaker B.V.
|
||||||
# Cura is released under the terms of the AGPLv3 or higher.
|
# Cura is released under the terms of the AGPLv3 or higher.
|
||||||
|
|
||||||
from PyQt5.QtCore import Qt
|
from PyQt5.QtCore import Qt, pyqtSignal, pyqtProperty
|
||||||
|
|
||||||
import cura.ExtruderManager
|
import cura.ExtruderManager
|
||||||
import UM.Qt.ListModel
|
import UM.Qt.ListModel
|
||||||
|
@ -12,18 +12,21 @@ import UM.Qt.ListModel
|
||||||
# intended for drop-down lists of the current machine's extruders in place of
|
# intended for drop-down lists of the current machine's extruders in place of
|
||||||
# settings.
|
# settings.
|
||||||
class ExtrudersModel(UM.Qt.ListModel.ListModel):
|
class ExtrudersModel(UM.Qt.ListModel.ListModel):
|
||||||
|
# The ID of the container stack for the extruder.
|
||||||
|
IdRole = Qt.UserRole + 1
|
||||||
|
|
||||||
## Human-readable name of the extruder.
|
## Human-readable name of the extruder.
|
||||||
NameRole = Qt.UserRole + 1
|
NameRole = Qt.UserRole + 2
|
||||||
|
|
||||||
## Colour of the material loaded in the extruder.
|
## Colour of the material loaded in the extruder.
|
||||||
ColourRole = Qt.UserRole + 2
|
ColourRole = Qt.UserRole + 3
|
||||||
|
|
||||||
## Index of the extruder, which is also the value of the setting itself.
|
## Index of the extruder, which is also the value of the setting itself.
|
||||||
#
|
#
|
||||||
# An index of 0 indicates the first extruder, an index of 1 the second
|
# An index of 0 indicates the first extruder, an index of 1 the second
|
||||||
# one, and so on. This is the value that will be saved in instance
|
# one, and so on. This is the value that will be saved in instance
|
||||||
# containers.
|
# containers.
|
||||||
IndexRole = Qt.UserRole + 3
|
IndexRole = Qt.UserRole + 4
|
||||||
|
|
||||||
## Initialises the extruders model, defining the roles and listening for
|
## Initialises the extruders model, defining the roles and listening for
|
||||||
# changes in the data.
|
# changes in the data.
|
||||||
|
@ -32,16 +35,30 @@ class ExtrudersModel(UM.Qt.ListModel.ListModel):
|
||||||
def __init__(self, parent = None):
|
def __init__(self, parent = None):
|
||||||
super().__init__(parent)
|
super().__init__(parent)
|
||||||
|
|
||||||
|
self.addRoleName(self.IdRole, "id")
|
||||||
self.addRoleName(self.NameRole, "name")
|
self.addRoleName(self.NameRole, "name")
|
||||||
self.addRoleName(self.ColourRole, "colour")
|
self.addRoleName(self.ColourRole, "colour")
|
||||||
self.addRoleName(self.IndexRole, "index")
|
self.addRoleName(self.IndexRole, "index")
|
||||||
|
|
||||||
|
self._add_global = False
|
||||||
|
|
||||||
#Listen to changes.
|
#Listen to changes.
|
||||||
manager = cura.ExtruderManager.ExtruderManager.getInstance()
|
manager = cura.ExtruderManager.ExtruderManager.getInstance()
|
||||||
manager.extrudersChanged.connect(self._updateExtruders) #When the list of extruders changes in general.
|
manager.extrudersChanged.connect(self._updateExtruders) #When the list of extruders changes in general.
|
||||||
UM.Application.globalContainerStackChanged.connect(self._updateExtruders) #When the current machine changes.
|
UM.Application.globalContainerStackChanged.connect(self._updateExtruders) #When the current machine changes.
|
||||||
self._updateExtruders()
|
self._updateExtruders()
|
||||||
|
|
||||||
|
def setAddGlobal(self, add):
|
||||||
|
if add != self._add_global:
|
||||||
|
self._add_global = add
|
||||||
|
self._updateExtruders()
|
||||||
|
self.addGlobalChanged.emit()
|
||||||
|
|
||||||
|
addGlobalChanged = pyqtSignal()
|
||||||
|
@pyqtProperty(bool, fset = setAddGlobal, notify = addGlobalChanged)
|
||||||
|
def addGlobal(self):
|
||||||
|
return self._add_global
|
||||||
|
|
||||||
## Update the list of extruders.
|
## Update the list of extruders.
|
||||||
#
|
#
|
||||||
# This should be called whenever the list of extruders changes.
|
# This should be called whenever the list of extruders changes.
|
||||||
|
@ -51,6 +68,18 @@ class ExtrudersModel(UM.Qt.ListModel.ListModel):
|
||||||
global_container_stack = UM.Application.getInstance().getGlobalContainerStack()
|
global_container_stack = UM.Application.getInstance().getGlobalContainerStack()
|
||||||
if not global_container_stack:
|
if not global_container_stack:
|
||||||
return #There is no machine to get the extruders of.
|
return #There is no machine to get the extruders of.
|
||||||
|
|
||||||
|
if self._add_global:
|
||||||
|
material = global_container_stack.findContainer({ "type": "material" })
|
||||||
|
colour = material.getMetaDataEntry("color_code", default = "#FFFF00") if material else "#FFFF00"
|
||||||
|
item = {
|
||||||
|
"id": global_container_stack.getId(),
|
||||||
|
"name": "Global",
|
||||||
|
"colour": colour,
|
||||||
|
"index": -1
|
||||||
|
}
|
||||||
|
self.appendItem(item)
|
||||||
|
|
||||||
for extruder in manager.getMachineExtruders(global_container_stack.getBottom()):
|
for extruder in manager.getMachineExtruders(global_container_stack.getBottom()):
|
||||||
material = extruder.findContainer({ "type": "material" })
|
material = extruder.findContainer({ "type": "material" })
|
||||||
colour = material.getMetaDataEntry("color_code", default = "#FFFF00") if material else "#FFFF00"
|
colour = material.getMetaDataEntry("color_code", default = "#FFFF00") if material else "#FFFF00"
|
||||||
|
@ -60,9 +89,11 @@ class ExtrudersModel(UM.Qt.ListModel.ListModel):
|
||||||
except ValueError: #Not a proper int.
|
except ValueError: #Not a proper int.
|
||||||
position = -1
|
position = -1
|
||||||
item = { #Construct an item with only the relevant information.
|
item = { #Construct an item with only the relevant information.
|
||||||
|
"id": extruder.getId(),
|
||||||
"name": extruder.getName(),
|
"name": extruder.getName(),
|
||||||
"colour": colour,
|
"colour": colour,
|
||||||
"index": position
|
"index": position
|
||||||
}
|
}
|
||||||
self.appendItem(item)
|
self.appendItem(item)
|
||||||
self.sort(lambda item: item["index"])
|
|
||||||
|
self.sort(lambda item: item["index"])
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue