1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL3$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPLv3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or later as published by the Free
28** Software Foundation and appearing in the file LICENSE.GPL included in
29** the packaging of this file. Please review the following information to
30** ensure the GNU General Public License version 2.0 requirements will be
31** met: http://www.gnu.org/licenses/gpl-2.0.html.
32**
33** $QT_END_LICENSE$
34**
35****************************************************************************/
36
37import QtQuick 2.15
38import QtQuick.Window 2.15
39import QtQuick.Controls 2.15
40import QtQuick.Controls.impl 2.15
41import QtQuick.Templates 2.15 as T
42import QtQuick.Controls.Material 2.15
43import QtQuick.Controls.Material.impl 2.15
44
45T.ComboBox {
46    id: control
47
48    implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset,
49                            implicitContentWidth + leftPadding + rightPadding)
50    implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
51                             implicitContentHeight + topPadding + bottomPadding,
52                             implicitIndicatorHeight + topPadding + bottomPadding)
53
54    topInset: 6
55    bottomInset: 6
56
57    leftPadding: padding + (!control.mirrored || !indicator || !indicator.visible ? 0 : indicator.width + spacing)
58    rightPadding: padding + (control.mirrored || !indicator || !indicator.visible ? 0 : indicator.width + spacing)
59
60    Material.elevation: flat ? control.pressed || control.hovered ? 2 : 0
61                             : control.pressed ? 8 : 2
62    Material.background: flat ? "transparent" : undefined
63    Material.foreground: flat ? undefined : Material.primaryTextColor
64
65    delegate: MenuItem {
66        width: ListView.view.width
67        text: control.textRole ? (Array.isArray(control.model) ? modelData[control.textRole] : model[control.textRole]) : modelData
68        Material.foreground: control.currentIndex === index ? ListView.view.contentItem.Material.accent : ListView.view.contentItem.Material.foreground
69        highlighted: control.highlightedIndex === index
70        hoverEnabled: control.hoverEnabled
71    }
72
73    indicator: ColorImage {
74        x: control.mirrored ? control.padding : control.width - width - control.padding
75        y: control.topPadding + (control.availableHeight - height) / 2
76        color: control.enabled ? control.Material.foreground : control.Material.hintTextColor
77        source: "qrc:/qt-project.org/imports/QtQuick/Controls.2/Material/images/drop-indicator.png"
78    }
79
80    contentItem: T.TextField {
81        padding: 6
82        leftPadding: control.editable ? 2 : control.mirrored ? 0 : 12
83        rightPadding: control.editable ? 2 : control.mirrored ? 12 : 0
84
85        text: control.editable ? control.editText : control.displayText
86
87        enabled: control.editable
88        autoScroll: control.editable
89        readOnly: control.down
90        inputMethodHints: control.inputMethodHints
91        validator: control.validator
92        selectByMouse: control.selectTextByMouse
93
94        font: control.font
95        color: control.enabled ? control.Material.foreground : control.Material.hintTextColor
96        selectionColor: control.Material.accentColor
97        selectedTextColor: control.Material.primaryHighlightedTextColor
98        verticalAlignment: Text.AlignVCenter
99
100        cursorDelegate: CursorDelegate { }
101    }
102
103    background: Rectangle {
104        implicitWidth: 120
105        implicitHeight: control.Material.buttonHeight
106
107        radius: control.flat ? 0 : 2
108        color: !control.editable ? control.Material.dialogColor : "transparent"
109
110        layer.enabled: control.enabled && !control.editable && control.Material.background.a > 0
111        layer.effect: ElevationEffect {
112            elevation: control.Material.elevation
113        }
114
115        Rectangle {
116            visible: control.editable
117            y: parent.y + control.baselineOffset
118            width: parent.width
119            height: control.activeFocus ? 2 : 1
120            color: control.editable && control.activeFocus ? control.Material.accentColor : control.Material.hintTextColor
121        }
122
123        Ripple {
124            clip: control.flat
125            clipRadius: control.flat ? 0 : 2
126            x: control.editable && control.indicator ? control.indicator.x : 0
127            width: control.editable && control.indicator ? control.indicator.width : parent.width
128            height: parent.height
129            pressed: control.pressed
130            anchor: control.editable && control.indicator ? control.indicator : control
131            active: control.pressed || control.visualFocus || control.hovered
132            color: control.Material.rippleColor
133        }
134    }
135
136    popup: T.Popup {
137        y: control.editable ? control.height - 5 : 0
138        width: control.width
139        height: Math.min(contentItem.implicitHeight, control.Window.height - topMargin - bottomMargin)
140        transformOrigin: Item.Top
141        topMargin: 12
142        bottomMargin: 12
143
144        Material.theme: control.Material.theme
145        Material.accent: control.Material.accent
146        Material.primary: control.Material.primary
147
148        enter: Transition {
149            // grow_fade_in
150            NumberAnimation { property: "scale"; from: 0.9; to: 1.0; easing.type: Easing.OutQuint; duration: 220 }
151            NumberAnimation { property: "opacity"; from: 0.0; to: 1.0; easing.type: Easing.OutCubic; duration: 150 }
152        }
153
154        exit: Transition {
155            // shrink_fade_out
156            NumberAnimation { property: "scale"; from: 1.0; to: 0.9; easing.type: Easing.OutQuint; duration: 220 }
157            NumberAnimation { property: "opacity"; from: 1.0; to: 0.0; easing.type: Easing.OutCubic; duration: 150 }
158        }
159
160        contentItem: ListView {
161            clip: true
162            implicitHeight: contentHeight
163            model: control.delegateModel
164            currentIndex: control.highlightedIndex
165            highlightMoveDuration: 0
166
167            T.ScrollIndicator.vertical: ScrollIndicator { }
168        }
169
170        background: Rectangle {
171            radius: 2
172            color: parent.Material.dialogColor
173
174            layer.enabled: control.enabled
175            layer.effect: ElevationEffect {
176                elevation: 8
177            }
178        }
179    }
180}
181