-
Notifications
You must be signed in to change notification settings - Fork 473
Expand file tree
/
Copy pathMenuField.qml
More file actions
211 lines (158 loc) · 5.67 KB
/
Copy pathMenuField.qml
File metadata and controls
211 lines (158 loc) · 5.67 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/***** THIS FILE CANNOT BE RELICENSED UNDER THE MPL YET *****/
/*
* QML Material - An application framework implementing Material Design.
* Copyright (C) 2015 Michael Spencer <sonrisesoftware@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import QtQuick 2.4
import QtQuick.Layouts 1.1
import Material 0.3
import Material.ListItems 0.1
/*!
\qmltype MenuField
\inqmlmodule Material
\brief A input field similar to a text field but that opens a dropdown menu.
*/
Item {
id: field
implicitHeight: hasHelperText ? helperTextLabel.y + helperTextLabel.height + 4 * Units.dp
: underline.y + 8 * Units.dp
implicitWidth: spinBoxContents.implicitWidth
activeFocusOnTab: true
property color accentColor: Theme.accentColor
property color errorColor: "#F44336"
property alias model: listView.model
property string textRole
readonly property string selectedText: (listView.currentItem) ? listView.currentItem.text : ""
property alias selectedIndex: listView.currentIndex
property int maxVisibleItems: 4
property alias placeholderText: fieldPlaceholder.text
property alias helperText: helperTextLabel.text
property bool floatingLabel: false
property bool hasError: false
property bool hasHelperText: helperText.length > 0
readonly property rect inputRect: Qt.rect(spinBox.x, spinBox.y, spinBox.width, spinBox.height)
signal itemSelected(int index)
Ink {
anchors.fill: parent
onClicked: {
if(listView.currentIndex !== -1) {
listView.positionViewAtIndex(listView.currentIndex, ListView.Center)
var offset = listView.currentItem.itemLabel.mapToItem(menu, 0, 0)
}
menu.open(label, 0, listView.currentIndex !== -1 ? -offset.y : 0)
}
}
Item {
id: spinBox
height: 24 * Units.dp
width: parent.width
y: {
if(!floatingLabel)
return 16 * Units.dp
if(floatingLabel && !hasHelperText)
return 40 * Units.dp
return 28 * Units.dp
}
RowLayout {
id: spinBoxContents
height: parent.height
width: parent.width + 5 * Units.dp
Label {
id: label
Layout.fillWidth: true
Layout.alignment: Qt.AlignVCenter
text: (listView.currentItem) ? listView.currentItem.text : ""
style: "subheading"
elide: Text.ElideRight
}
Icon {
id: dropDownIcon
Layout.alignment: Qt.AlignVCenter | Qt.AlignRight
Layout.preferredWidth: 24 * Units.dp
Layout.preferredHeight: 24 * Units.dp
name: "navigation/arrow_drop_down"
size: 24 * Units.dp
}
}
Dropdown {
id: menu
anchor: Item.TopLeft
width: spinBox.width
//If there are more than max items, show an extra half item so
// it's clear the user can scroll
height: Math.min(maxVisibleItems*48 * Units.dp + 24 * Units.dp, listView.contentHeight)
ListView {
id: listView
width: menu.width
height: count > 0 ? menu.height : 0
interactive: true
delegate: Standard {
id: delegateItem
text: textRole ? model[textRole] : modelData
onClicked: {
itemSelected(index)
listView.currentIndex = index
menu.close()
}
}
}
Scrollbar {
flickableItem: listView
}
}
}
Label {
id: fieldPlaceholder
text: field.placeholderText
visible: floatingLabel
font.pixelSize: 12 * Units.dp
anchors.bottom: spinBox.top
anchors.bottomMargin: 8 * Units.dp
color: Theme.light.hintColor
}
Rectangle {
id: underline
color: field.hasError ? field.errorColor : field.activeFocus ? field.accentColor : Theme.light.hintColor
height: field.activeFocus ? 2 * Units.dp : 1 * Units.dp
anchors {
left: parent.left
right: parent.right
top: spinBox.bottom
topMargin: 8 * Units.dp
}
Behavior on height {
NumberAnimation { duration: 200 }
}
Behavior on color {
ColorAnimation { duration: 200 }
}
}
Label {
id: helperTextLabel
anchors {
left: parent.left
right: parent.right
top: underline.top
topMargin: 4 * Units.dp
}
visible: hasHelperText
font.pixelSize: 12 * Units.dp
color: field.hasError ? field.errorColor : Qt.darker(Theme.light.hintColor)
Behavior on color {
ColorAnimation { duration: 200 }
}
}
}