Move creating extruder manager logic to ExtruderManager

This logic was both in Extruder.py and in MachineManagerModel.py due to a planning mishap.

Contributes to issues CURA-1278 and CURA-340.
This commit is contained in:
Ghostkeeper 2016-06-06 13:41:49 +02:00
parent 79c37d667e
commit 927d33145f
6 changed files with 109 additions and 279 deletions

View file

@ -1,7 +1,8 @@
# Copyright (c) 2016 Ultimaker B.V.
# Cura is released under the terms of the AGPLv3 or higher.
from cura.Extruder import Extruder #The individual extruders managed by this manager.
from PyQt5.QtCore import pyqtSignal, pyqtProperty, pyqtSlot, QObject
import UM.Application #To get the global container stack to find the current machine.
import UM.Logger
import UM.Settings.ContainerRegistry #Finding containers by ID.
@ -13,21 +14,24 @@ import UM.Signal #To notify other components of changes in the extruders.
# This finds the extruders that are available for the currently active machine
# and makes sure that whenever the machine is swapped, this list is kept up to
# date. It also contains and updates the setting stacks for the extruders.
class ExtruderManager:
class ExtruderManager(QObject):
## The singleton instance of this manager.
__instance = None
## Signal to notify other components when the list of extruders changes.
extrudersChanged = UM.Signal()
## Registers listeners and such to listen to changes to the extruders.
def __init__(self):
self._extruders = [] #Extruders for the current machine.
self._global_container_stack = None
self._next_item = 0 #For when you use this class as iterator.
## Notify when the user switches the currently active extruder.
activeExtruderChanged = pyqtSignal()
UM.Application.getInstance().globalContainerStackChanged.connect(self._reconnectExtruderReload) #When the current machine changes, we need to reload all extruders belonging to the new machine.
self._reconnectExtruderReload()
## Registers listeners and such to listen to changes to the extruders.
def __init__(self, parent = None):
super().__init__(parent)
self._extruder_trains = { } #Extruders for the current machine.
self._next_item = 0 #For when you use this class as iterator.
self._active_extruder_index = 0
self._repopulate()
## Gets an instance of this extruder manager.
#
@ -45,34 +49,99 @@ class ExtruderManager:
def __iter__(self):
return iter(self._extruders)
## When the global container stack changes, this reconnects to the new
# signal for containers changing.
def _reconnectExtruderReload(self):
if self._global_container_stack:
self._global_container_stack.containersChanged.disconnect(self._reloadExtruders) #Disconnect from the old global container stack.
self._global_container_stack = UM.Application.getInstance().getGlobalContainerStack()
self._global_container_stack.containersChanged.connect(self._reloadExtruders) #When the current machine changes, we need to reload all extruders belonging to the new machine.
self._reloadExtruders()
@pyqtProperty(str, notify = activeExtruderChanged)
def activeExtruderStackId(self):
if UM.Application.getInstance().getGlobalContainerStack():
try:
return self._extruder_trains[UM.Application.getInstance().getGlobalContainerStack().getId()][str(self._active_extruder_index)]
except KeyError:
pass
## (Re)loads all extruders of the currently active machine.
#
# This looks at the global container stack to see which machine is active.
# Then it loads the extruders for that machine and loads each of them in a
# list of extruders.
def _reloadExtruders(self, *args):
self._extruders = []
if not self._global_container_stack: #No machine has been added yet.
@pyqtSlot(int)
def setActiveExtruderIndex(self, index):
self._active_extruder_index = index
self.activeExtruderChanged.emit()
## (Re)populates the collections of extruders by machine.
def _repopulate(self):
self._extruder_trains = { }
if not UM.Application.getInstance().getGlobalContainerStack(): #No machine has been added yet.
self.extrudersChanged.emit() #Yes, we just cleared the _extruders list!
return #Then leave them empty!
#Get the extruder definitions belonging to the current machine.
machine = self._global_container_stack.getBottom()
extruder_train_ids = machine.getMetaDataEntry("machine_extruder_trains", { })
for _,extruder_train_id in extruder_train_ids.items():
extruder_definitions = UM.Settings.ContainerRegistry.getInstance().findDefinitionContainers(id = extruder_train_id) #Should be only 1 definition if IDs are unique, but add the whole list anyway.
if not extruder_definitions: #Empty list or error.
UM.Logger.log("w", "Machine definition %s refers to an extruder train \"%s\", but no such extruder was found.", machine.getId(), extruder_train_id)
extruder_trains = UM.Settings.ContainerRegistry.getInstance().findContainerStacks(type = "extruder_train")
for extruder_train in extruder_trains:
machine_id = extruder_train.getMetaDataEntry("machine")
if not machine_id:
continue
for extruder_definition in extruder_definitions:
self._extruders.append(Extruder(extruder_definition))
self.extrudersChanged.emit()
if machine_id not in self._extruder_trains:
self._extruder_trains[machine_id] = { }
self._extruder_trains[machine_id][extruder_train.getMetaDataEntry("position")] = extruder_train.getId()
self.extrudersChanged.emit()
def createExtruderTrain(self, definition, extruder_id):
container_registry = UM.Settings.ContainerRegistry.getInstance()
#Create a container stack for this extruder.
name = self._uniqueName(extruder_id)
container_stack = UM.Settings.ContainerStack(name)
container_stack.addMetaDataEntry("type", "extruder_train")
container_stack.addContainer(definition)
"""
Yes, I'm committing this code which needs to be transformed to work later.
#Find the nozzle to use for this extruder.
nozzle = container_registry.getEmptyInstanceContainer()
if definition.getMetaDataEntry("has_nozzles", default = "False") == "True":
if len(self._nozzles) >= 1: #First add any extruder. Later, overwrite with preference if the preference is valid.
self._nozzle = self._nozzles[0]
preferred_nozzle_id = definition.getMetaDataEntry("preferred_nozzle")
if preferred_nozzle_id:
for nozzle in self._nozzles:
if nozzle.getId() == preferred_nozzle_id:
self._nozzle = nozzle
break
self._container_stack.addContainer(self._nozzle)
#Find a material to use for this nozzle.
self._material = container_registry.getEmptyInstanceContainer()
if self._definition.getMetaDataEntry("has_materials", default = "False") == "True":
if self._definition.getMetaDataEntry("has_nozzle_materials", default = "False") == "True":
all_materials = container_registry.findInstanceContainers(type = "material", nozzle = self._nozzle.getId())
else:
all_materials = container_registry.findInstanceContainers(type = "material")
if len(all_materials) >= 1:
self._material = all_materials[0]
preferred_material_id = self._definition.getMetaDataEntry("preferred_material")
if preferred_material_id:
preferred_material = container_registry.findInstanceContainers(type = "material", id = preferred_material_id.lower())
if len(preferred_material) >= 1:
self._material = preferred_material[0]
self._container_stack.addContainer(self._material)
#Find a quality to use for this extruder.
self._quality = container_registry.getEmptyInstanceContainer()
if self._definition.getMetaDataEntry("has_machine_quality"):
all_qualities = container_registry.findInstanceContainers(type = "quality")
if len(all_qualities) >= 1:
self._quality = all_qualities[0]
preferred_quality_id = self._definition.getMetaDataEntry("preferred_quality")
if preferred_quality_id:
preferred_quality = container_registry.findInstanceContainers(type = "quality", id = preferred_quality_id.lower())
if len(preferred_quality) >= 1:
self._quality = preferred_quality[0]
self._container_stack.addContainer(self._quality)
"""
#Add an empty user profile.
user_profile = UM.Settings.InstanceContainer(name + "_current_settings")
user_profile.addMetaDataEntry("type", "user")
container_stack.addContainer(user_profile)
container_registry.addContainer(user_profile)
container_stack.setNextStack(UM.Application.getInstance().getGlobalContainerStack())
container_registry.addContainer(container_stack)
def createExtruderManager(engine, script_engine):
return ExtruderManager()