1// Copyright (c) 2018 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: material_type_section
16    property var materialType
17
18    property string materialBrand: materialType != null ? materialType.brand : ""
19    property string materialName: materialType != null ? materialType.name : ""
20    property var expanded: materialList.expandedTypes.indexOf(materialBrand + "_" + materialName) > -1
21    property var colorsModel: materialType != null ? materialType.colors: null
22    height: childrenRect.height
23    width: parent ? parent.width :undefined
24    anchors.left: parent ? parent.left : undefined
25    Rectangle
26    {
27        id: material_type_header_background
28        color:
29        {
30            if(!expanded && materialBrand + "_" + materialName == materialList.currentType)
31            {
32                return UM.Theme.getColor("favorites_row_selected")
33            }
34            else
35            {
36                return "transparent"
37            }
38        }
39        width: parent.width
40        height: material_type_header.height
41    }
42    Rectangle
43    {
44        id: material_type_header_border
45        color: UM.Theme.getColor("favorites_header_bar")
46        anchors.bottom: material_type_header.bottom
47        anchors.left: material_type_header.left
48        height: UM.Theme.getSize("default_lining").height
49        width: material_type_header.width
50    }
51    Row
52    {
53        id: material_type_header
54        width: parent.width
55        leftPadding: UM.Theme.getSize("default_margin").width
56        anchors
57        {
58            left: parent ? parent.left : undefined
59        }
60        Label
61        {
62            text: materialName
63            height: UM.Theme.getSize("favorites_row").height
64            width: parent.width - parent.leftPadding - UM.Theme.getSize("favorites_button").width
65            id: material_type_name
66            verticalAlignment: Text.AlignVCenter
67        }
68        Item // this one causes lots of warnings
69        {
70            implicitWidth: UM.Theme.getSize("favorites_button").width
71            implicitHeight: UM.Theme.getSize("favorites_button").height
72            UM.RecolorImage {
73                anchors
74                {
75                    verticalCenter: parent ? parent.verticalCenter : undefined
76                    horizontalCenter: parent ? parent.horizontalCenter : undefined
77                }
78                width: UM.Theme.getSize("standard_arrow").width
79                height: UM.Theme.getSize("standard_arrow").height
80                color: "black"
81                source: material_type_section.expanded ? UM.Theme.getIcon("arrow_bottom") : UM.Theme.getIcon("arrow_left")
82            }
83
84        }
85    }
86    MouseArea // causes lots of warnings
87    {
88        anchors.fill: material_type_header
89        onPressed:
90        {
91            const identifier = materialBrand + "_" + materialName;
92            const i = materialList.expandedTypes.indexOf(identifier)
93            if (i > -1)
94            {
95                // Remove it
96                materialList.expandedTypes.splice(i, 1)
97                material_type_section.expanded = false
98            }
99            else
100            {
101                // Add it
102                materialList.expandedTypes.push(identifier)
103                material_type_section.expanded = true
104            }
105            UM.Preferences.setValue("cura/expanded_types", materialList.expandedTypes.join(";"));
106        }
107    }
108    Column
109    {
110        height: material_type_section.expanded ? childrenRect.height : 0
111        visible: material_type_section.expanded
112        width: parent.width
113        anchors.top: material_type_header.bottom
114        Repeater
115        {
116            model: colorsModel
117            delegate: MaterialsSlot
118            {
119                material: model
120            }
121        }
122    }
123
124    Connections
125    {
126        target: UM.Preferences
127        onPreferenceChanged:
128        {
129            if (preference !== "cura/expanded_types" && preference !== "cura/expanded_brands")
130            {
131                return;
132            }
133
134            expanded = materialList.expandedTypes.indexOf(materialBrand + "_" + materialName) > -1
135        }
136    }
137}