diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-04-04 19:19:41 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-04-04 19:19:41 +0200 |
commit | d9295fc8f19e4005f8454e7f193f80316550ac0c (patch) | |
tree | f27c370d54bced09212b9c4a12b827d1cebb6110 /framework/qml/AutocompleteLineEdit.qml | |
parent | d002eae7f8b443dd1bad914444c296088c2b6e85 (diff) | |
download | kube-d9295fc8f19e4005f8454e7f193f80316550ac0c.tar.gz kube-d9295fc8f19e4005f8454e7f193f80316550ac0c.zip |
One framework plugin to rule them all
Diffstat (limited to 'framework/qml/AutocompleteLineEdit.qml')
-rw-r--r-- | framework/qml/AutocompleteLineEdit.qml | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/framework/qml/AutocompleteLineEdit.qml b/framework/qml/AutocompleteLineEdit.qml new file mode 100644 index 00000000..64e5940f --- /dev/null +++ b/framework/qml/AutocompleteLineEdit.qml | |||
@@ -0,0 +1,143 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2016 Christian Mollekopf, <mollekopf@kolabsys.com> | ||
3 | * | ||
4 | * This program is free software; you can redistribute it and/or modify | ||
5 | * it under the terms of the GNU General Public License as published by | ||
6 | * the Free Software Foundation; either version 2 of the License, or | ||
7 | * (at your option) any later version. | ||
8 | * | ||
9 | * This program is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | * GNU General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU General Public License along | ||
15 | * with this program; if not, write to the Free Software Foundation, Inc., | ||
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
17 | */ | ||
18 | |||
19 | import QtQuick 2.7 | ||
20 | import QtQuick.Controls 2.0 as Controls2 | ||
21 | import QtQuick.Layouts 1.1 | ||
22 | |||
23 | import org.kde.kirigami 1.0 as Kirigami | ||
24 | import org.kube.components.theme 1.0 as KubeTheme | ||
25 | |||
26 | Controls2.TextField { | ||
27 | id: textField | ||
28 | |||
29 | property string searchTerm | ||
30 | property variant model | ||
31 | onTextChanged: { | ||
32 | if (text.length >= 2) { | ||
33 | searchTerm = text | ||
34 | startCompleting() | ||
35 | } else { | ||
36 | searchTerm = "" | ||
37 | abort() | ||
38 | } | ||
39 | } | ||
40 | Keys.onDownPressed: { | ||
41 | listView.incrementCurrentIndex() | ||
42 | } | ||
43 | Keys.onUpPressed: { | ||
44 | listView.decrementCurrentIndex() | ||
45 | } | ||
46 | Keys.onRightPressed: { | ||
47 | startCompleting() | ||
48 | } | ||
49 | Keys.onTabPressed: { | ||
50 | if (popup.visible) { | ||
51 | listView.incrementCurrentIndex() | ||
52 | } else { | ||
53 | event.accepted = false | ||
54 | } | ||
55 | } | ||
56 | Keys.onReturnPressed: { | ||
57 | accept() | ||
58 | } | ||
59 | Keys.onEscapePressed: { | ||
60 | abort() | ||
61 | } | ||
62 | |||
63 | function startCompleting() { | ||
64 | if (!popup.visible) { | ||
65 | popup.open() | ||
66 | listView.currentIndex = -1 | ||
67 | } | ||
68 | } | ||
69 | |||
70 | function accept() { | ||
71 | textField.text = listView.currentItem.text; | ||
72 | popup.close() | ||
73 | } | ||
74 | |||
75 | function abort() { | ||
76 | popup.close() | ||
77 | } | ||
78 | |||
79 | Controls2.Popup { | ||
80 | id: popup | ||
81 | x: 0 | ||
82 | y: textField.y + textField.height | ||
83 | padding: 0 | ||
84 | contentWidth: rect.width | ||
85 | contentHeight: rect.height | ||
86 | |||
87 | Rectangle { | ||
88 | id: rect | ||
89 | |||
90 | anchors.top: popup.top | ||
91 | anchors.left: popup.left | ||
92 | |||
93 | height: listView.contentHeight | ||
94 | width: textField.width | ||
95 | |||
96 | border.color: KubeTheme.Colors.textColor | ||
97 | color: KubeTheme.Colors.backgroundColor | ||
98 | |||
99 | radius: 5 | ||
100 | ListView { | ||
101 | id: listView | ||
102 | height: childrenRect.height | ||
103 | width: parent.width | ||
104 | interactive: true | ||
105 | model: textField.model | ||
106 | delegate: Kirigami.AbstractListItem { | ||
107 | id: listDelegate | ||
108 | property string text: model.text | ||
109 | |||
110 | width: listView.width | ||
111 | height: textField.height | ||
112 | |||
113 | enabled: true | ||
114 | supportsMouseEvents: true | ||
115 | |||
116 | checked: listView.currentIndex == index | ||
117 | onClicked: { | ||
118 | listView.currentIndex = model.index | ||
119 | accept() | ||
120 | } | ||
121 | |||
122 | //Content | ||
123 | Item { | ||
124 | width: parent.width | ||
125 | height: parent.height | ||
126 | |||
127 | Column { | ||
128 | anchors { | ||
129 | verticalCenter: parent.verticalCenter | ||
130 | left: parent.left | ||
131 | } | ||
132 | |||
133 | Text{ | ||
134 | text: model.text | ||
135 | color: listDelegate.checked ? KubeTheme.Colors.highlightedTextColor : KubeTheme.Colors.textColor | ||
136 | } | ||
137 | } | ||
138 | } | ||
139 | } | ||
140 | } | ||
141 | } | ||
142 | } | ||
143 | } | ||