Automatically use global- or extruder- containerstack

CURA-1585
This commit is contained in:
fieldOfView 2016-06-10 16:13:47 +02:00
parent 717b9574b9
commit c4a1bd2fd3

View file

@ -18,16 +18,25 @@ class MachineManagerModel(QObject):
def __init__(self, parent = None): def __init__(self, parent = None):
super().__init__(parent) super().__init__(parent)
self._active_container_stack = None
self._global_container_stack = None self._global_container_stack = None
Application.getInstance().globalContainerStackChanged.connect(self._onGlobalContainerChanged) Application.getInstance().globalContainerStackChanged.connect(self._onGlobalContainerChanged)
self._global_stack_valid = None self._global_stack_valid = None
self._onGlobalContainerChanged() self._onGlobalContainerChanged()
ExtruderManager.ExtruderManager.getInstance().activeExtruderChanged.connect(self._onActiveExtruderStackChanged)
self._onActiveExtruderStackChanged()
## When the global container is changed, active material probably needs to be updated. ## When the global container is changed, active material probably needs to be updated.
self.globalContainerChanged.connect(self.activeMaterialChanged) self.globalContainerChanged.connect(self.activeMaterialChanged)
self.globalContainerChanged.connect(self.activeVariantChanged) self.globalContainerChanged.connect(self.activeVariantChanged)
self.globalContainerChanged.connect(self.activeQualityChanged) self.globalContainerChanged.connect(self.activeQualityChanged)
self.globalContainerChanged.connect(self.activeStackChanged)
self.globalValueChanged.connect(self.activeStackChanged)
ExtruderManager.ExtruderManager.getInstance().activeExtruderChanged.connect(self.activeStackChanged)
self._empty_variant_container = UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(id = "empty_variant")[0] self._empty_variant_container = UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(id = "empty_variant")[0]
self._empty_material_container = UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(id = "empty_material")[0] self._empty_material_container = UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(id = "empty_material")[0]
self._empty_quality_container = UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(id = "empty_quality")[0] self._empty_quality_container = UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(id = "empty_quality")[0]
@ -45,6 +54,7 @@ class MachineManagerModel(QObject):
activeMaterialChanged = pyqtSignal() activeMaterialChanged = pyqtSignal()
activeVariantChanged = pyqtSignal() activeVariantChanged = pyqtSignal()
activeQualityChanged = pyqtSignal() activeQualityChanged = pyqtSignal()
activeStackChanged = pyqtSignal()
globalValueChanged = pyqtSignal() # Emitted whenever a value inside global container is changed. globalValueChanged = pyqtSignal() # Emitted whenever a value inside global container is changed.
globalValidationChanged = pyqtSignal() # Emitted whenever a validation inside global container is changed. globalValidationChanged = pyqtSignal() # Emitted whenever a validation inside global container is changed.
@ -63,12 +73,12 @@ class MachineManagerModel(QObject):
self.globalValueChanged.emit() self.globalValueChanged.emit()
if property_name == "validationState": if property_name == "validationState":
if self._global_stack_valid: if self._global_stack_valid:
changed_validation_state = self._global_container_stack.getProperty(key, property_name) changed_validation_state = self._active_container_stack.getProperty(key, property_name)
if changed_validation_state in (ValidatorState.Exception, ValidatorState.MaximumError, ValidatorState.MinimumError): if changed_validation_state in (ValidatorState.Exception, ValidatorState.MaximumError, ValidatorState.MinimumError):
self._global_stack_valid = False self._global_stack_valid = False
self.globalValidationChanged.emit() self.globalValidationChanged.emit()
else: else:
new_validation_state = self._checkStackForErrors(self._global_container_stack) new_validation_state = self._checkStackForErrors(self._active_container_stack)
if new_validation_state: if new_validation_state:
self._global_stack_valid = True self._global_stack_valid = True
self.globalValidationChanged.emit() self.globalValidationChanged.emit()
@ -87,6 +97,11 @@ class MachineManagerModel(QObject):
self._global_container_stack.propertyChanged.connect(self._onGlobalPropertyChanged) self._global_container_stack.propertyChanged.connect(self._onGlobalPropertyChanged)
self._global_stack_valid = not self._checkStackForErrors(self._global_container_stack) self._global_stack_valid = not self._checkStackForErrors(self._global_container_stack)
def _onActiveExtruderStackChanged(self):
self._active_container_stack = ExtruderManager.ExtruderManager.getInstance().getActiveExtruderStack()
if not self._active_container_stack:
self._active_container_stack = self._global_container_stack
def _onInstanceContainersChanged(self, container): def _onInstanceContainersChanged(self, container):
container_type = container.getMetaDataEntry("type") container_type = container.getMetaDataEntry("type")
if container_type == "material": if container_type == "material":
@ -183,18 +198,19 @@ class MachineManagerModel(QObject):
## Remove all instances from the top instanceContainer (effectively removing all user-changed settings) ## Remove all instances from the top instanceContainer (effectively removing all user-changed settings)
@pyqtSlot() @pyqtSlot()
def clearUserSettings(self): def clearUserSettings(self):
if not self._global_container_stack: if not self._active_container_stack:
return return
user_settings = self._global_container_stack.getTop()
user_settings = self._active_container_stack.getTop()
user_settings.clear() user_settings.clear()
## Check if the global_container has instances in the user container ## Check if the global_container has instances in the user container
@pyqtProperty(bool, notify = globalValueChanged) @pyqtProperty(bool, notify = activeStackChanged)
def hasUserSettings(self): def hasUserSettings(self):
if not self._global_container_stack: if not self._active_container_stack:
return return
user_settings = self._global_container_stack.getTop().findInstances(**{}) user_settings = self._active_container_stack.getTop().findInstances(**{})
return len(user_settings) != 0 return len(user_settings) != 0
## Check if the global profile does not contain error states ## Check if the global profile does not contain error states
@ -206,8 +222,8 @@ class MachineManagerModel(QObject):
@pyqtProperty(str, notify = globalContainerChanged) @pyqtProperty(str, notify = globalContainerChanged)
def activeUserProfileId(self): def activeUserProfileId(self):
if self._global_container_stack: if self._active_container_stack:
return self._global_container_stack.getTop().getId() return self._active_container_stack.getTop().getId()
return "" return ""
@ -227,8 +243,8 @@ class MachineManagerModel(QObject):
@pyqtProperty(str, notify = activeMaterialChanged) @pyqtProperty(str, notify = activeMaterialChanged)
def activeMaterialName(self): def activeMaterialName(self):
if self._global_container_stack: if self._active_container_stack:
material = self._global_container_stack.findContainer({"type":"material"}) material = self._active_container_stack.findContainer({"type":"material"})
if material: if material:
return material.getName() return material.getName()
@ -236,8 +252,8 @@ class MachineManagerModel(QObject):
@pyqtProperty(str, notify=activeMaterialChanged) @pyqtProperty(str, notify=activeMaterialChanged)
def activeMaterialId(self): def activeMaterialId(self):
if self._global_container_stack: if self._active_container_stack:
material = self._global_container_stack.findContainer({"type": "material"}) material = self._active_container_stack.findContainer({"type": "material"})
if material: if material:
return material.getId() return material.getId()
@ -245,16 +261,16 @@ class MachineManagerModel(QObject):
@pyqtProperty(str, notify=activeQualityChanged) @pyqtProperty(str, notify=activeQualityChanged)
def activeQualityName(self): def activeQualityName(self):
if self._global_container_stack: if self._active_container_stack:
quality = self._global_container_stack.findContainer({"type": "quality"}) quality = self._active_container_stack.findContainer({"type": "quality"})
if quality: if quality:
return quality.getName() return quality.getName()
return "" return ""
@pyqtProperty(str, notify=activeQualityChanged) @pyqtProperty(str, notify=activeQualityChanged)
def activeQualityId(self): def activeQualityId(self):
if self._global_container_stack: if self._active_container_stack:
quality = self._global_container_stack.findContainer({"type": "quality"}) quality = self._active_container_stack.findContainer({"type": "quality"})
if quality: if quality:
return quality.getId() return quality.getId()
return "" return ""
@ -262,8 +278,8 @@ class MachineManagerModel(QObject):
## Check if a container is read_only ## Check if a container is read_only
@pyqtSlot(str, result = bool) @pyqtSlot(str, result = bool)
def isReadOnly(self, container_id): def isReadOnly(self, container_id):
containers = UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(id=container_id) containers = UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(id = container_id)
if not containers or not self._global_container_stack: if not containers or not self._active_container_stack:
return True return True
return containers[0].isReadOnly() return containers[0].isReadOnly()
@ -278,9 +294,9 @@ class MachineManagerModel(QObject):
@pyqtSlot(str, result=str) @pyqtSlot(str, result=str)
def duplicateContainer(self, container_id): def duplicateContainer(self, container_id):
if not self._global_container_stack: if not self._active_container_stack:
return "" return ""
containers = UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(id=container_id) containers = UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(id = container_id)
if containers: if containers:
new_name = self._createUniqueName("quality", "", containers[0].getName(), catalog.i18nc("@label", "Custom profile")) new_name = self._createUniqueName("quality", "", containers[0].getName(), catalog.i18nc("@label", "Custom profile"))
@ -331,7 +347,7 @@ class MachineManagerModel(QObject):
@pyqtSlot(str) @pyqtSlot(str)
def removeQualityContainer(self, container_id): def removeQualityContainer(self, container_id):
containers = UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(id = container_id) containers = UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(id = container_id)
if not containers or not self._global_container_stack: if not containers or not self._active_container_stack:
return return
# If the container that is being removed is the currently active container, set another machine as the active container # If the container that is being removed is the currently active container, set another machine as the active container
@ -349,10 +365,10 @@ class MachineManagerModel(QObject):
@pyqtSlot() @pyqtSlot()
def updateQualityContainerFromUserContainer(self): def updateQualityContainerFromUserContainer(self):
if not self._global_container_stack: if not self._active_container_stack:
return return
user_settings = self._global_container_stack.getTop() user_settings = self._active_container_stack.getTop()
quality = self._global_container_stack.findContainer({"type": "quality"}) quality = self._active_container_stack.findContainer({"type": "quality"})
for key in user_settings.getAllKeys(): for key in user_settings.getAllKeys():
quality.setProperty(key, "value", user_settings.getProperty(key, "value")) quality.setProperty(key, "value", user_settings.getProperty(key, "value"))
self.clearUserSettings() # As all users settings are noq a quality, remove them. self.clearUserSettings() # As all users settings are noq a quality, remove them.
@ -360,45 +376,45 @@ class MachineManagerModel(QObject):
@pyqtSlot(str) @pyqtSlot(str)
def setActiveMaterial(self, material_id): def setActiveMaterial(self, material_id):
containers = UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(id=material_id) containers = UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(id = material_id)
if not containers or not self._global_container_stack: if not containers or not self._active_container_stack:
return return
old_material = self._global_container_stack.findContainer({"type":"material"}) old_material = self._active_container_stack.findContainer({"type":"material"})
if old_material: if old_material:
material_index = self._global_container_stack.getContainerIndex(old_material) material_index = self._active_container_stack.getContainerIndex(old_material)
self._global_container_stack.replaceContainer(material_index, containers[0]) self._active_container_stack.replaceContainer(material_index, containers[0])
self.setActiveQuality(self._updateQualityContainer(self._global_container_stack.getBottom(), containers[0]).id) self.setActiveQuality(self._updateQualityContainer(self._active_container_stack.getBottom(), containers[0]).id)
@pyqtSlot(str) @pyqtSlot(str)
def setActiveVariant(self, variant_id): def setActiveVariant(self, variant_id):
containers = UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(id=variant_id) containers = UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(id = variant_id)
if not containers or not self._global_container_stack: if not containers or not self._active_container_stack:
return return
old_variant = self._global_container_stack.findContainer({"type": "variant"}) old_variant = self._active_container_stack.findContainer({"type": "variant"})
if old_variant: if old_variant:
variant_index = self._global_container_stack.getContainerIndex(old_variant) variant_index = self._active_container_stack.getContainerIndex(old_variant)
self._global_container_stack.replaceContainer(variant_index, containers[0]) self._active_container_stack.replaceContainer(variant_index, containers[0])
self.setActiveMaterial(self._updateMaterialContainer(self._global_container_stack.getBottom(), containers[0]).id) self.setActiveMaterial(self._updateMaterialContainer(self._active_container_stack.getBottom(), containers[0]).id)
@pyqtSlot(str) @pyqtSlot(str)
def setActiveQuality(self, quality_id): def setActiveQuality(self, quality_id):
containers = UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(id = quality_id) containers = UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(id = quality_id)
if not containers or not self._global_container_stack: if not containers or not self._active_container_stack:
return return
old_quality = self._global_container_stack.findContainer({"type": "quality"}) old_quality = self._active_container_stack.findContainer({"type": "quality"})
if old_quality: if old_quality:
quality_index = self._global_container_stack.getContainerIndex(old_quality) quality_index = self._active_container_stack.getContainerIndex(old_quality)
self._global_container_stack.replaceContainer(quality_index, containers[0]) self._active_container_stack.replaceContainer(quality_index, containers[0])
@pyqtProperty(str, notify = activeVariantChanged) @pyqtProperty(str, notify = activeVariantChanged)
def activeVariantName(self): def activeVariantName(self):
if self._global_container_stack: if self._active_container_stack:
variant = self._global_container_stack.findContainer({"type": "variant"}) variant = self._active_container_stack.findContainer({"type": "variant"})
if variant: if variant:
return variant.getName() return variant.getName()
@ -406,8 +422,8 @@ class MachineManagerModel(QObject):
@pyqtProperty(str, notify = activeVariantChanged) @pyqtProperty(str, notify = activeVariantChanged)
def activeVariantId(self): def activeVariantId(self):
if self._global_container_stack: if self._active_container_stack:
variant = self._global_container_stack.findContainer({"type": "variant"}) variant = self._active_container_stack.findContainer({"type": "variant"})
if variant: if variant:
return variant.getId() return variant.getId()
@ -415,7 +431,7 @@ class MachineManagerModel(QObject):
@pyqtProperty(str, notify = globalContainerChanged) @pyqtProperty(str, notify = globalContainerChanged)
def activeDefinitionId(self): def activeDefinitionId(self):
if self._global_container_stack: if self._active_container_stack:
definition = self._global_container_stack.getBottom() definition = self._global_container_stack.getBottom()
if definition: if definition:
return definition.id return definition.id
@ -449,15 +465,15 @@ class MachineManagerModel(QObject):
@pyqtProperty(bool, notify = globalContainerChanged) @pyqtProperty(bool, notify = globalContainerChanged)
def hasMaterials(self): def hasMaterials(self):
if self._global_container_stack: if self._active_container_stack:
return bool(self._global_container_stack.getMetaDataEntry("has_materials", False)) return bool(self._active_container_stack.getMetaDataEntry("has_materials", False))
return False return False
@pyqtProperty(bool, notify = globalContainerChanged) @pyqtProperty(bool, notify = globalContainerChanged)
def hasVariants(self): def hasVariants(self):
if self._global_container_stack: if self._active_container_stack:
return bool(self._global_container_stack.getMetaDataEntry("has_variants", False)) return bool(self._active_container_stack.getMetaDataEntry("has_variants", False))
return False return False