1/*************************************************************************************
2 *  Copyright (C) 2015 by Aleix Pol <aleixpol@kde.org>                               *
3 *                                                                                   *
4 *  This program is free software; you can redistribute it and/or                    *
5 *  modify it under the terms of the GNU General Public License                      *
6 *  as published by the Free Software Foundation; either version 2                   *
7 *  of the License, or (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                *
15 *  along with this program; if not, write to the Free Software                      *
16 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA   *
17 *************************************************************************************/
18
19import QtQuick 2.0
20import QtQuick.Window 2.0
21import QtQuick.Controls 2.14 as QQC2
22import org.kde.kirigami 2.14 as Kirigami
23import org.kde.analitza 1.0
24import QtQuick.Layouts 1.2
25import org.kde.kalgebra.mobile 1.0
26
27QQC2.TextField {
28    id: field
29    readonly property string currentWord: operators.lastWord(field.cursorPosition, field.text)
30
31    QQC2.Popup {
32        id: popupCompletion
33        x: 0
34        y: -height - Kirigami.Units.smallSpacing
35        width: parent.width
36        visible: view.count > 0 && field.activeFocus && field.currentWord.length > 0
37        topPadding: 0
38        leftPadding: 0
39        bottomPadding: 0
40        rightPadding: 0
41        height: Math.min(Kirigami.Units.gridUnit * 10, view.contentHeight)
42        contentItem: QQC2.ScrollView {
43            ListView {
44                id: view
45                currentIndex: 0
46                keyNavigationWraps: true
47                model: QSortFilterProxyModel {
48                    sourceModel: OperatorsModel { id: operators }
49                    filterRegularExpression: new RegExp("^" + field.currentWord)
50                    filterCaseSensitivity: Qt.CaseInsensitive
51                }
52
53                delegate: Kirigami.BasicListItem {
54                    text: model.display + " - " + description
55                    highlighted: view.currentIndex === index
56                    width: ListView.view.width
57
58                    function complete() {
59                        var toInsert = model.display.substr(field.currentWord.length);
60                        if(isVariable)
61                            toInsert += '(';
62                        field.insert(field.cursorPosition, toInsert)
63                    }
64
65                    onClicked: complete()
66                    Keys.onReturnPressed: complete()
67                }
68            }
69        }
70    }
71
72    placeholderText: i18n("Expression to calculate...")
73    inputMethodHints: /*Qt.ImhPreferNumbers |*/ Qt.ImhNoPredictiveText | Qt.ImhNoAutoUppercase
74
75    Keys.forwardTo: view.visible && view.currentItem ? [ view.currentItem ] : null
76    Keys.onTabPressed: view.incrementCurrentIndex()
77    Keys.onUpPressed: view.decrementCurrentIndex()
78    Keys.onDownPressed: view.incrementCurrentIndex()
79    Keys.onReturnPressed: view.currentIndex = -1
80}
81
82