1// Copyright (c) 2019 Ultimaker B.V.
2// Uranium is released under the terms of the LGPLv3 or higher.
3
4import QtQuick 2.7
5import QtQuick.Controls 1.4
6import QtQuick.Controls.Styles 1.4
7import QtQuick.Layouts 1.3
8import QtQuick.Dialogs 1.2
9
10import UM 1.2 as UM
11import Cura 1.0 as Cura
12
13Item
14{
15    id: materialList
16    height: childrenRect.height
17
18    // Children
19    UM.I18nCatalog { id: catalog; name: "cura"; }
20    Cura.MaterialBrandsModel
21    {
22        id: materialsModel
23        extruderPosition: Cura.ExtruderManager.activeExtruderIndex
24    }
25
26    Cura.FavoriteMaterialsModel
27    {
28        id: favoriteMaterialsModel
29        extruderPosition: Cura.ExtruderManager.activeExtruderIndex
30    }
31
32    Cura.GenericMaterialsModel
33    {
34        id: genericMaterialsModel
35        extruderPosition: Cura.ExtruderManager.activeExtruderIndex
36    }
37
38    property var currentType: null
39    property var currentBrand: null
40    property var expandedBrands: UM.Preferences.getValue("cura/expanded_brands").split(";")
41    property var expandedTypes: UM.Preferences.getValue("cura/expanded_types").split(";")
42
43    // Store information about which parts of the tree are expanded
44    function persistExpandedCategories()
45    {
46        UM.Preferences.setValue("cura/expanded_brands", materialList.expandedBrands.join(";"))
47        UM.Preferences.setValue("cura/expanded_types", materialList.expandedTypes.join(";"))
48    }
49
50    // Expand the list of materials in order to select the current material
51    function expandActiveMaterial(search_root_id)
52    {
53        if (search_root_id == "")
54        {
55            // When this happens it means that the information of one of the materials has changed, so the model
56            // was updated and the list has to highlight the current item.
57            var currentItemId = base.currentItem == null ? "" : base.currentItem.root_material_id
58            search_root_id = currentItemId
59        }
60        for (var material_idx = 0; material_idx < genericMaterialsModel.count; material_idx++)
61        {
62            var material = genericMaterialsModel.getItem(material_idx)
63            if (material.root_material_id == search_root_id)
64            {
65                if (materialList.expandedBrands.indexOf("Generic") == -1)
66                {
67                    materialList.expandedBrands.push("Generic")
68                }
69                materialList.currentBrand = "Generic"
70                base.currentItem = material
71                persistExpandedCategories()
72                return true
73            }
74        }
75        for (var brand_idx = 0; brand_idx < materialsModel.count; brand_idx++)
76        {
77            var brand = materialsModel.getItem(brand_idx)
78            var types_model = brand.material_types
79            for (var type_idx = 0; type_idx < types_model.count; type_idx++)
80            {
81                var type = types_model.getItem(type_idx)
82                var colors_model = type.colors
83                for (var material_idx = 0; material_idx < colors_model.count; material_idx++)
84                {
85                    var material = colors_model.getItem(material_idx)
86                    if (material.root_material_id == search_root_id)
87                    {
88                        if (materialList.expandedBrands.indexOf(brand.name) == -1)
89                        {
90                            materialList.expandedBrands.push(brand.name)
91                        }
92                        materialList.currentBrand = brand.name
93                        if (materialList.expandedTypes.indexOf(brand.name + "_" + type.name) == -1)
94                        {
95                            materialList.expandedTypes.push(brand.name + "_" + type.name)
96                        }
97                        materialList.currentType = brand.name + "_" + type.name
98                        base.currentItem = material
99                        persistExpandedCategories()
100                        return true
101                    }
102                }
103            }
104        }
105        base.currentItem = null
106        return false
107    }
108
109    function updateAfterModelChanges()
110    {
111        var correctlyExpanded = materialList.expandActiveMaterial(base.newRootMaterialIdToSwitchTo)
112        if (correctlyExpanded)
113        {
114            if (base.toActivateNewMaterial)
115            {
116                var position = Cura.ExtruderManager.activeExtruderIndex
117                Cura.MachineManager.setMaterialById(position, base.newRootMaterialIdToSwitchTo)
118            }
119            base.newRootMaterialIdToSwitchTo = ""
120            base.toActivateNewMaterial = false
121        }
122    }
123
124    Connections
125    {
126        target: materialsModel
127        onItemsChanged: updateAfterModelChanges()
128    }
129
130    Connections
131    {
132        target: genericMaterialsModel
133        onItemsChanged: updateAfterModelChanges()
134    }
135
136    Column
137    {
138        width: materialList.width
139        height: childrenRect.height
140
141        MaterialsBrandSection
142        {
143            id: favoriteSection
144            sectionName: "Favorites"
145            elementsModel: favoriteMaterialsModel
146            hasMaterialTypes: false
147        }
148
149        MaterialsBrandSection
150        {
151            id: genericSection
152            sectionName: "Generic"
153            elementsModel: genericMaterialsModel
154            hasMaterialTypes: false
155        }
156
157        Repeater
158        {
159            model: materialsModel
160            delegate: MaterialsBrandSection
161            {
162                id: brandSection
163                sectionName: model.name
164                elementsModel: model.material_types
165                hasMaterialTypes: true
166            }
167        }
168    }
169}