summaryrefslogtreecommitdiffstats
path: root/components/package/contents/ui/AutocompleteLineEdit.qml
diff options
context:
space:
mode:
Diffstat (limited to 'components/package/contents/ui/AutocompleteLineEdit.qml')
-rw-r--r--components/package/contents/ui/AutocompleteLineEdit.qml144
1 files changed, 144 insertions, 0 deletions
diff --git a/components/package/contents/ui/AutocompleteLineEdit.qml b/components/package/contents/ui/AutocompleteLineEdit.qml
new file mode 100644
index 00000000..173a006c
--- /dev/null
+++ b/components/package/contents/ui/AutocompleteLineEdit.qml
@@ -0,0 +1,144 @@
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
19import QtQuick 2.7
20import QtQuick.Controls 1.4
21import QtQuick.Controls 2.0 as Controls2
22import QtQuick.Layouts 1.1
23import QtQuick.Dialogs 1.0
24
25import org.kde.kirigami 1.0 as Kirigami
26
27TextField {
28 id: textField
29
30 property string searchTerm
31 property variant model
32 onTextChanged: {
33 if (text.length >= 2) {
34 searchTerm = text
35 startCompleting()
36 } else {
37 searchTerm = ""
38 abort()
39 }
40 }
41 Keys.onDownPressed: {
42 listView.incrementCurrentIndex()
43 }
44 Keys.onUpPressed: {
45 listView.decrementCurrentIndex()
46 }
47 Keys.onRightPressed: {
48 startCompleting()
49 }
50 Keys.onTabPressed: {
51 if (popup.visible) {
52 listView.incrementCurrentIndex()
53 } else {
54 event.accepted = false
55 }
56 }
57 Keys.onReturnPressed: {
58 accept()
59 }
60 Keys.onEscapePressed: {
61 abort()
62 }
63
64 function startCompleting() {
65 if (!popup.visible) {
66 popup.open()
67 listView.currentIndex = -1
68 }
69 }
70
71 function accept() {
72 textField.text = listView.currentItem.text;
73 popup.close()
74 }
75
76 function abort() {
77 popup.close()
78 }
79
80 Controls2.Popup {
81 id: popup
82 x: 0
83 y: textField.y + textField.height
84 padding: 0
85 contentWidth: rect.width
86 contentHeight: rect.height
87
88 Rectangle {
89 color: Kirigami.Theme.backgroundColor
90 id: rect
91 anchors.top: popup.top
92 anchors.left: popup.left
93 height: listView.contentHeight
94 width: textField.width
95 border.color: "Black"
96 radius: 5
97 ScrollView {
98 id: scrollView
99 anchors.fill: parent
100 ListView {
101 id: listView
102 height: childrenRect.height
103 width: scrollView.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 ? Kirigami.Theme.highlightedTextColor : Kirigami.Theme.textColor
136 }
137 }
138 }
139 }
140 }
141 }
142 }
143 }
144}