Add variants as they get added to the registry

Contributes to issue CURA-6600.
This commit is contained in:
Ghostkeeper 2019-08-06 08:58:34 +02:00
parent e84a75094a
commit 2565be01f3
No known key found for this signature in database
GPG key ID: 86BEF881AE2CF276
2 changed files with 25 additions and 4 deletions

View file

@ -16,10 +16,10 @@ from typing import Dict
class ContainerTree: class ContainerTree:
def __init__(self) -> None: def __init__(self) -> None:
self.machines = {} # type: Dict[str, MachineNode] # Mapping from definition ID to machine nodes. self.machines = {} # type: Dict[str, MachineNode] # Mapping from definition ID to machine nodes.
ContainerRegistry.getInstance().containerAdded.connect(self.machineAdded) ContainerRegistry.getInstance().containerAdded.connect(self._machineAdded)
## When a printer gets added, we need to build up the tree for that container. ## When a printer gets added, we need to build up the tree for that container.
def machineAdded(self, definition_container: ContainerInterface): def _machineAdded(self, definition_container: ContainerInterface):
if not isinstance(definition_container, DefinitionContainer): if not isinstance(definition_container, DefinitionContainer):
return # Not our concern. return # Not our concern.
definition_id = definition_container.getId() definition_id = definition_container.getId()

View file

@ -3,8 +3,9 @@
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from UM.Settings.ContainerRegistry import ContainerRegistry # To find all the variants for this machine.
from UM.Settings.Interfaces import ContainerInterface
from cura.Machines.ContainerNode import ContainerNode from cura.Machines.ContainerNode import ContainerNode
from cura.Machines.ContainerTree import ContainerTree
from cura.Machines.VariantNode import VariantNode from cura.Machines.VariantNode import VariantNode
if TYPE_CHECKING: if TYPE_CHECKING:
@ -16,4 +17,24 @@ if TYPE_CHECKING:
class MachineNode(ContainerNode): class MachineNode(ContainerNode):
def __init__(self, container_id: str) -> None: def __init__(self, container_id: str) -> None:
super().__init__(container_id, None) super().__init__(container_id, None)
self.variants = {} # type: Dict[str, VariantNode] # mapping variant IDs to their nodes. self.variants = {} # type: Dict[str, VariantNode] # mapping variant names to their nodes.
container_registry = ContainerRegistry.getInstance()
container_registry.containerAdded.connect(self._variantAdded)
# Find all the variants for this definition ID.
variants = container_registry.findInstanceContainersMetadata(type = "variant", definition = self.container_id, hardware_type = "nozzle")
for variant in variants:
variant_name = variant["name"]
if variant_name not in self.variants:
self.variants[variant_name] = VariantNode(variant["id"], parent = self)
## When a variant gets added to the set of profiles, we need to update our
# tree here.
def _variantAdded(self, container: ContainerInterface):
if container.getMetaDataEntry("type") != "variant":
return # Not interested.
name = container.getMetaDataEntry("name")
if name in self.variants:
return # Already have this one.
self.variants[name] = VariantNode(container.getId(), parent = self)