Debian Codegraph @

app/ui/KeyboardPage.qml

raw
1 /*
2 * Copyright (C) 2017-2019 UBports
3 * Copyright (C) 2015 Canonical Ltd
4 *
5 * This file is part of Lomiri Calculator App
6 *
7 * Lomiri Calculator App is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 3 as
9 * published by the Free Software Foundation.
10 *
11 * Lomiri Calculator App is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 import QtQuick 2.4
20 import Lomiri.Components 1.3
21 import QtQuick.Controls.Suru 2.2
22
23 Grid {
24 id: keyboardRoot
25
26 /*
27 You can change the columns. Better not touching the rows property.
28 The keyboard will resize itelf in height depending on how many buttons you add
29 */
30 columns: 4
31
32 /* Set a model which describes the keys. For example:
33 model: new Array(
34 { number: 1 },
35 { number: 2, wFactor: 2 },
36 { text: "cos" }
37 )
38
39 This would add 3 keys to the keyboard, 1, 2 and a cos button. The button 2 is stretched over 2 columns.
40
41 There can be more properties:
42 * number: A number to be displayed on the key (integer)
43 * text: A text to be displayed on the key (string)
44 * name: Used for the key's objectName for testing. (e.g. name: "foo" -> objectName: "fooButton") (string)
45 * forceNumber: If you assign a text and a number, or the number 0 you can force the number to be displayed over text. (bool)
46 * wFactor: The width factor to stretch the button over multiple columns (int)
47 * hFactor: the height factor to stretch the button over multiple rows (int)
48 * action: The action to execute on button press. Can be "push", "delete", "changeSign" or "calculate". Default is "push".
49 * pushText: The text that will be pushed to the formula when the button is pressed and the action is "push"
50 * kbdKeys: Normally a key would hightlight itself when "text" matches the currently pressed key. This doesn't work with
51 special keys or if you want multiple keyboard keys to trigger a button. You can override this behavor by
52 specifying kbdKeys with a list of keyboard keys.
53 E.g. [Qt.Key_Return, Qt.Key_Enter] to trigger the = button on Enter and Return.
54 */
55 property var keyboardModel: null
56
57 /*
58 Defines the Width/Height ratio of the buttons.
59 1 means square buttons
60 0.5 means the button's height will be half the button's width
61 */
62 property real buttonRatio: 0.7
63 property real buttonMaxHeight: units.gu(10)
64
65 //Space between the buttons
66 spacing: 0
67
68 property bool completed: false
69 Component.onCompleted: {
70 buildModel();
71 completed = true;
72 }
73
74 onKeyboardModelChanged: {
75 if (completed) {
76 buildModel();
77 }
78 }
79
80 signal clicked()
81
82 function buildModel() {
83 generatedModel.clear();
84
85 var emptyPlaces = new Array();
86
87 for (var i = 0; i < keyboardRoot.keyboardModel.length; i++) {
88 var entry = keyboardRoot.keyboardModel[i];
89 var text = entry.number || entry.forceNumber ? Number(entry.number).toLocaleString(Qt.locale(), "f", 0) : entry.text ? entry.text : "";
90 generatedModel.append(
91 {
92 text: text,
93 wFactor: entry.wFactor ? entry.wFactor : 1,
94 hFactor: entry.hFactor ? entry.hFactor : 1,
95 action: entry.action ? entry.action : "push",
96 objectName: entry.name ? entry.name + "Button" : "",
97 pushText: entry.pushText ? entry.pushText : text,
98 kbdKeys: entry.kbdKeys ? JSON.stringify(entry.kbdKeys) : JSON.stringify([]),
99 secondaryAction: entry.secondaryAction ? entry.secondaryAction : "",
100 role: entry.role ? entry.role : ""
101 }
102 )
103
104 if (entry.wFactor && entry.wFactor > 1) {
105 for (var j = 1; j < entry.wFactor; j++) {
106 generatedModel.append({text: ""})
107 }
108 for (var j = 0; j < emptyPlaces.length; j++) {
109 emptyPlaces[j] = emptyPlaces[j] - entry.wFactor + 1;
110 }
111 }
112
113 if (entry.hFactor && entry.hFactor > 1) {
114 for (var j = 1; j < entry.hFactor; j++) {
115 emptyPlaces.push(i + columns * j);
116 }
117 }
118 if (i + 1 === emptyPlaces[0]) {
119 generatedModel.append({text: ""})
120 emptyPlaces = emptyPlaces.splice(1, emptyPlaces.length);
121 }
122 }
123 }
124
125 Repeater {
126 id: repeater
127 model: ListModel {
128 id: generatedModel
129 }
130 property real baseSize: keyboardRoot.width / keyboardRoot.columns - keyboardRoot.spacing
131
132 Loader {
133 sourceComponent: model.text ? buttonComponent : undefined
134
135 height: Math.min(width * keyboardRoot.buttonRatio, keyboardRoot.buttonMaxHeight)
136 width: keyboardRoot.width / keyboardRoot.columns - keyboardRoot.spacing
137
138 Component {
139 id: buttonComponent
140
141 Item {
142 KeyboardButton {
143 height: Math.min(repeater.baseSize * keyboardRoot.buttonRatio, keyboardRoot.buttonMaxHeight) *
144 model.hFactor + (keyboardRoot.spacing * (model.hFactor - 1))
145 width: repeater.baseSize * model.wFactor + (keyboardRoot.spacing * (model.wFactor - 1))
146 text: model.text
147 role: model.role
148 Suru.textStyle: Suru.TertiaryText
149 objectName: model.objectName
150 onClicked: {
151 //If key pressed then scroll down
152
153 switch (model.action) {
154 case "push":
155 resetFormulaFontColor(function() {
156 formulaPush(model.pushText);
157 scrollableView.scrollToBottom();
158 });
159 break;
160 case "delete":
161 resetFormulaFontColor(function() {
162 deleteLastFormulaElement();
163 scrollableView.scrollToBottom();
164 });
165 break;
166 case "calculate":
167 calculate();
168 historyPosition = calculationHistory.getContents().count;
169 scrollableView.scrollToBottom();
170 break;
171 }
172
173 keyboardRoot.clicked();
174 }
175 onPressAndHold: {
176 //If key pressed then scroll down
177 switch (model.secondaryAction) {
178 case "clearFormula":
179 clearFormula();
180 break;
181 }
182 }
183 }
184 }
185 }
186 }
187 }
188 }