Ensure we initialize the list of containers for GlobalStack

This commit is contained in:
Arjen Hiemstra 2017-03-27 17:52:38 +02:00
parent 39803cf7dd
commit b6fafb06ed

View file

@ -22,6 +22,7 @@ class GlobalStack(ContainerStack):
self._empty_instance_container = ContainerRegistry.getInstance().getEmptyInstanceContainer()
self._containers = [self._empty_instance_container for i in range(len(_ContainerIndexes.IndexTypeMap))]
self._extruders = []
self.containersChanged.connect(self._onContainersChanged)
@ -32,9 +33,16 @@ class GlobalStack(ContainerStack):
def userChanges(self) -> InstanceContainer:
return self._containers[_ContainerIndexes.UserChanges]
def setQualtiyChanges(self, new_quality_changes: InstanceContainer) -> None:
def setQualityChanges(self, new_quality_changes: InstanceContainer) -> None:
self.replaceContainer(_ContainerIndexes.QualityChanges, new_quality_changes)
def setQualityChangesById(self, new_quality_changes_id: str) -> None:
quality_changes = ContainerRegistry.getInstance().findInstanceContainers(id = new_quality_changes_id)
if quality_changes:
self.setQualityChanges(quality_changes[0])
else:
raise Exceptions.InvalidContainerError("Could not find container with id {id}".format(id = new_quality_changes_id))
@pyqtProperty(InstanceContainer, notify = pyqtContainersChanged)
def qualityChanges(self) -> InstanceContainer:
return self._containers[_ContainerIndexes.QualityChanges]
@ -42,6 +50,13 @@ class GlobalStack(ContainerStack):
def setQuality(self, new_quality: InstanceContainer) -> None:
self.replaceContainer(_ContainerIndexes.Quality, new_quality)
def setQualityById(self, new_quality_id: str) -> None:
quality = ContainerRegistry.getInstance().findInstanceContainers(id = new_quality_id)
if quality:
self.setQuality(quality[0])
else:
raise Exceptions.InvalidContainerError("Could not find container with id {id}".format(id = new_quality_id))
@pyqtProperty(InstanceContainer, notify = pyqtContainersChanged)
def quality(self) -> InstanceContainer:
return self._containers[_ContainerIndexes.Quality]
@ -49,6 +64,13 @@ class GlobalStack(ContainerStack):
def setMaterial(self, new_material: InstanceContainer) -> None:
self.replaceContainer(_ContainerIndexes.Material, new_material)
def setMaterialById(self, new_material_id: str) -> None:
material = ContainerRegistry.getInstance().findInstanceContainers(id = new_material_id)
if material:
self.setMaterial(material[0])
else:
raise Exceptions.InvalidContainerError("Could not find container with id {id}".format(id = new_material_id))
@pyqtProperty(InstanceContainer, notify = pyqtContainersChanged)
def material(self) -> InstanceContainer:
return self._containers[_ContainerIndexes.Material]
@ -56,10 +78,27 @@ class GlobalStack(ContainerStack):
def setVariant(self, new_variant: InstanceContainer) -> None:
self.replaceContainer(_ContainerIndexes.Variant, new_variant)
def setVariantById(self, new_variant_id: str) -> None:
variant = ContainerRegistry.getInstance().findInstanceContainers(id = new_variant_id)
if variant:
self.setVariant(variant[0])
else:
raise Exceptions.InvalidContainerError("Could not find container with id {id}".format(id = new_variant_id))
@pyqtProperty(InstanceContainer, notify = pyqtContainersChanged)
def variant(self) -> InstanceContainer:
return self._containers[_ContainerIndexes.Variant]
def setDefinitionChanges(self, new_definition_changes: InstanceContainer) -> None:
self.replaceContainer(_ContainerIndexes.DefinitionChanges, new_definition_changes)
def setDefinitionChangesById(self, new_definition_changes_id: str) -> None:
new_definition_changes = ContainerRegistry.getInstance().findInstanceContainers(id = new_definition_changes_id)
if new_definition_changes:
self.setDefinitionChanges(new_definition_changes[0])
else:
raise Exceptions.InvalidContainerError("Could not find container with id {id}".format(id = new_definition_changes_id))
@pyqtProperty(InstanceContainer, notify = pyqtContainersChanged)
def definitionChanges(self) -> InstanceContainer:
return self._containers[_ContainerIndexes.DefinitionChanges]
@ -97,7 +136,7 @@ class GlobalStack(ContainerStack):
## Overridden from ContainerStack
@override(ContainerStack)
def getProperty(self, key: str, property_name: str):
def getProperty(self, key: str, property_name: str) -> Any:
if property_name == "value":
if not self.hasUserValue(key):
resolve = super().getProperty(key, "resolve")
@ -106,6 +145,13 @@ class GlobalStack(ContainerStack):
return super().getProperty(key, property_name)
def setProperty(self, key: str, property_name: str, new_value: Any, target_container: str = "user") -> None:
container_index = _ContainerIndexes.indexForType(target_container)
if container_index != -1:
self._containers[container_index].setProperty(key, property_name, new_value)
else:
raise IndexError("Invalid target container {type}".format(type = target_container))
## Overridden from ContainerStack
@override(ContainerStack)
def setNextStack(self, next_stack: ContainerStack) -> None:
@ -145,6 +191,7 @@ class GlobalStack(ContainerStack):
super().deserialize(contents)
new_containers = self._containers.copy()
print("containers before", new_containers)
while(len(new_containers) < len(_ContainerIndexes.IndexTypeMap)):
new_containers.append(self._empty_instance_container)
@ -169,6 +216,7 @@ class GlobalStack(ContainerStack):
if actual_container:
new_containers[index] = actual_container
print("containers after", new_containers)
self._containers = new_containers
def _onContainersChanged(self, container):
@ -204,3 +252,11 @@ class _ContainerIndexes:
DefinitionChanges: "definition_changes",
Definition: "definition",
}
@classmethod
def indexForType(cls, type_name: str) -> int:
for key, value in cls.IndexTypeMap.items():
if value == type_name:
return key
return -1