Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MaterialsList.qml « Materials « Preferences « qml « resources - github.com/Ultimaker/Cura.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 913e213dae92b886da5a9f855e5e477eeb1ebe7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright (c) 2022 Ultimaker B.V.
// Uranium is released under the terms of the LGPLv3 or higher.

import QtQuick 2.7
import QtQuick.Layouts 1.3
import QtQuick.Dialogs

import UM 1.2 as UM
import Cura 1.0 as Cura

Item
{
    id: materialList
    height: childrenRect.height

    // Children
    Cura.MaterialBrandsModel
    {
        id: materialsModel
        extruderPosition: Cura.ExtruderManager.activeExtruderIndex
    }

    Cura.FavoriteMaterialsModel
    {
        id: favoriteMaterialsModel
        extruderPosition: Cura.ExtruderManager.activeExtruderIndex
    }

    Cura.GenericMaterialsModel
    {
        id: genericMaterialsModel
        extruderPosition: Cura.ExtruderManager.activeExtruderIndex
    }

    property var currentType: null
    property var currentBrand: null
    property var expandedBrands: UM.Preferences.getValue("cura/expanded_brands").split(";")
    property var expandedTypes: UM.Preferences.getValue("cura/expanded_types").split(";")

    // Store information about which parts of the tree are expanded
    function persistExpandedCategories()
    {
        UM.Preferences.setValue("cura/expanded_brands", materialList.expandedBrands.join(";"))
        UM.Preferences.setValue("cura/expanded_types", materialList.expandedTypes.join(";"))
    }

    // Expand the list of materials in order to select the current material
    function expandActiveMaterial(search_root_id)
    {
        if (search_root_id == "")
        {
            // When this happens it means that the information of one of the materials has changed, so the model
            // was updated and the list has to highlight the current item.
            var currentItemId = base.currentItem == null ? "" : base.currentItem.root_material_id
            search_root_id = currentItemId
        }
        for (var material_idx = 0; material_idx < genericMaterialsModel.count; material_idx++)
        {
            var material = genericMaterialsModel.getItem(material_idx)
            if (material.root_material_id == search_root_id)
            {
                if (materialList.expandedBrands.indexOf("Generic") == -1)
                {
                    materialList.expandedBrands.push("Generic")
                }
                materialList.currentBrand = "Generic"
                base.currentItem = material
                persistExpandedCategories()
                return true
            }
        }
        for (var brand_idx = 0; brand_idx < materialsModel.count; brand_idx++)
        {
            var brand = materialsModel.getItem(brand_idx)
            var types_model = brand.material_types
            for (var type_idx = 0; type_idx < types_model.count; type_idx++)
            {
                var type = types_model.getItem(type_idx)
                var colors_model = type.colors
                for (var material_idx = 0; material_idx < colors_model.count; material_idx++)
                {
                    var material = colors_model.getItem(material_idx)
                    if (material.root_material_id == search_root_id)
                    {
                        if (materialList.expandedBrands.indexOf(brand.name) == -1)
                        {
                            materialList.expandedBrands.push(brand.name)
                        }
                        materialList.currentBrand = brand.name
                        if (materialList.expandedTypes.indexOf(brand.name + "_" + type.name) == -1)
                        {
                            materialList.expandedTypes.push(brand.name + "_" + type.name)
                        }
                        materialList.currentType = brand.name + "_" + type.name
                        base.currentItem = material
                        persistExpandedCategories()
                        return true
                    }
                }
            }
        }
        base.currentItem = null
        return false
    }

    function updateAfterModelChanges()
    {
        var correctlyExpanded = materialList.expandActiveMaterial(base.newRootMaterialIdToSwitchTo)
        if (correctlyExpanded)
        {
            if (base.toActivateNewMaterial)
            {
                var position = Cura.ExtruderManager.activeExtruderIndex
                Cura.MachineManager.setMaterialById(position, base.newRootMaterialIdToSwitchTo)
            }
            base.newRootMaterialIdToSwitchTo = ""
            base.toActivateNewMaterial = false
        }
    }

    Connections
    {
        target: materialsModel
        function onItemsChanged() { updateAfterModelChanges() }
    }

    Connections
    {
        target: genericMaterialsModel
        function onItemsChanged() { updateAfterModelChanges() }
    }
    
    Column
    {
        width: materialList.width
        height: childrenRect.height

        MaterialsBrandSection
        {
            id: favoriteSection
            sectionName: "Favorites"
            elementsModel: favoriteMaterialsModel
            hasMaterialTypes: false
        }

        MaterialsBrandSection
        {
            id: genericSection
            sectionName: "Generic"
            elementsModel: genericMaterialsModel
            hasMaterialTypes: false
        }

        Repeater
        {
            model: materialsModel
            delegate: MaterialsBrandSection
            {
                id: brandSection
                sectionName: model.name
                elementsModel: model.material_types
                hasMaterialTypes: true
            }
        }
    }
}