Warning, /education/kalgebra/mobile/content/ui/controls/ExpressionInput.qml is written in an unsupported language. File is not indexed.

0001 /*************************************************************************************
0002  *  Copyright (C) 2015 by Aleix Pol <aleixpol@kde.org>                               *
0003  *                                                                                   *
0004  *  This program is free software; you can redistribute it and/or                    *
0005  *  modify it under the terms of the GNU General Public License                      *
0006  *  as published by the Free Software Foundation; either version 2                   *
0007  *  of the License, or (at your option) any later version.                           *
0008  *                                                                                   *
0009  *  This program is distributed in the hope that it will be useful,                  *
0010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of                   *
0011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                    *
0012  *  GNU General Public License for more details.                                     *
0013  *                                                                                   *
0014  *  You should have received a copy of the GNU General Public License                *
0015  *  along with this program; if not, write to the Free Software                      *
0016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA   *
0017  *************************************************************************************/
0018 
0019 import QtQuick
0020 import QtQuick.Window
0021 import QtQuick.Controls as QQC2
0022 import org.kde.kirigami as Kirigami
0023 import org.kde.kirigamiaddons.delegates as Delegates
0024 import org.kde.analitza
0025 import QtQuick.Layouts
0026 import org.kde.kalgebra.mobile
0027 
0028 ColumnLayout {
0029     spacing: 0
0030 
0031     property alias text: field.text
0032 
0033     signal addOperation(operation: string)
0034 
0035     function selectAll(): void {
0036         field.selectAll();
0037     }
0038 
0039     function remove(start: int, end: int): void {
0040         field.remove(start, end);
0041     }
0042 
0043     function insert(start: int, text: string): void {
0044         field.insert(start, text);
0045     }
0046 
0047     Kirigami.Separator {
0048         Layout.fillWidth: true
0049     }
0050 
0051     ListView {
0052         id: view
0053         currentIndex: -1
0054         keyNavigationWraps: true
0055         Layout.fillWidth: true
0056         Layout.preferredHeight: Math.min(contentHeight, Kirigami.Units.gridUnit * 10)
0057         clip: true
0058         model: field.currentWord.length > 0 ? filterModel : []
0059 
0060         QSortFilterProxyModel {
0061             id: filterModel
0062             sourceModel: OperatorsModel { id: operators }
0063             filterRegularExpression: new RegExp("^" + field.currentWord)
0064             filterCaseSensitivity: Qt.CaseInsensitive
0065         }
0066 
0067         delegate: Delegates.RoundedItemDelegate {
0068             required property int index
0069             required property string description
0070             required property string name
0071             required property bool isVariable
0072 
0073             text: name + " - " + description
0074             highlighted: view.currentIndex === index
0075             width: ListView.view.width
0076 
0077             function complete() {
0078                 let toInsert = name.substr(field.currentWord.length);
0079                 if(isVariable)
0080                     toInsert += '(';
0081                 field.insert(field.cursorPosition, toInsert)
0082             }
0083 
0084             onClicked: complete()
0085             Keys.onReturnPressed: complete()
0086         }
0087     }
0088 
0089     Kirigami.Separator {
0090         Layout.fillWidth: true
0091         visible: view.count > 0
0092     }
0093 
0094     QQC2.TextField {
0095         id: field
0096 
0097         focus: true
0098 
0099         readonly property string currentWord: operators.lastWord(field.cursorPosition, field.text)
0100 
0101         Layout.minimumWidth: parent.width
0102         Layout.preferredHeight: Kirigami.Units.gridUnit * 2
0103 
0104         background: null
0105 
0106         placeholderText: i18n("Expression to calculate...")
0107         inputMethodHints: /*Qt.ImhPreferNumbers |*/ Qt.ImhNoPredictiveText | Qt.ImhNoAutoUppercase
0108 
0109         Keys.forwardTo: view.visible && view.currentIndex >= 0 ? [ view.currentItem ] : null
0110         Keys.onTabPressed: view.incrementCurrentIndex()
0111         Keys.onUpPressed: view.decrementCurrentIndex()
0112         Keys.onDownPressed: view.incrementCurrentIndex()
0113         Keys.onReturnPressed: {
0114             view.currentIndex = -1;
0115             addOperation(text);
0116             field.text = '';
0117         }
0118     }
0119 }
0120