1# Copyright (c) 2019 Ultimaker B.V.
2# Cura is released under the terms of the LGPLv3 or higher.
3
4from PyQt5.QtCore import Qt
5
6from UM.Logger import Logger
7from UM.Qt.ListModel import ListModel
8import cura.CuraApplication  # Imported like this to prevent circular dependencies.
9from cura.Machines.ContainerTree import ContainerTree
10
11
12class NozzleModel(ListModel):
13    IdRole = Qt.UserRole + 1
14    HotendNameRole = Qt.UserRole + 2
15    ContainerNodeRole = Qt.UserRole + 3
16
17    def __init__(self, parent = None):
18        super().__init__(parent)
19
20        self.addRoleName(self.IdRole, "id")
21        self.addRoleName(self.HotendNameRole, "hotend_name")
22        self.addRoleName(self.ContainerNodeRole, "container_node")
23
24        cura.CuraApplication.CuraApplication.getInstance().getMachineManager().globalContainerChanged.connect(self._update)
25        self._update()
26
27    def _update(self):
28        Logger.log("d", "Updating {model_class_name}.".format(model_class_name = self.__class__.__name__))
29
30        global_stack = cura.CuraApplication.CuraApplication.getInstance().getGlobalContainerStack()
31        if global_stack is None:
32            self.setItems([])
33            return
34        machine_node = ContainerTree.getInstance().machines[global_stack.definition.getId()]
35
36        if not machine_node.has_variants:
37            self.setItems([])
38            return
39
40        item_list = []
41        for hotend_name, container_node in sorted(machine_node.variants.items(), key = lambda i: i[0].upper()):
42            item = {"id": hotend_name,
43                    "hotend_name": hotend_name,
44                    "container_node": container_node
45                    }
46
47            item_list.append(item)
48
49        self.setItems(item_list)