-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.qml
397 lines (318 loc) · 13.1 KB
/
main.qml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/* Copyright (C) 2017 reHackable
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* Author : Patrick Pedersen <ctx.xda@gmail.com>
* Part of the reHackable organization <https://github.com/reHackable>
* Description : A primitive touch based calculator written for the reMarkable tablet
* The design and functionality are primarily based on the stock Android
* Material design calculator found on most Android 5.0+ devices
* Notations : Functionality for more complex maths, such as calculations with parentheses
* as well as common functions such as cos(), sin() etc. are planned to be
* implemented in the nearby future.
*/
import QtQuick 2.6
import QtQuick.Window 2.2
Window {
id: win
visible: true
visibility: "FullScreen"
height: Screen.height
width: Screen.width
/* Virtual Display */
Rectangle {
id: display
height: 0.3645 * parent.height
width: parent.width
color: "white"
/* Error Flag */
property bool _error: false
/** OO Abstract Display Interface **/
function clear() {
display_text.focus = false
display_text.text = ""
}
function draw(buff) {
display_text.text = buff
}
function add(ch) {
if (_error) {
draw(ch)
_error = false
} else {
display_text.text += ch
display_text.scale()
}
}
function deleteAtCursor() {
if (_error) {
if (display_text.cursorPosition == 0) {
display_text.focus = false;
}
clear()
_error = false
} else {
if (display_text.cursorPosition - 1 < 1) {
display_text.focus = false;
}
display_text.remove(display_text.cursorPosition - 1, display_text.cursorPosition)
display_text.scale()
}
}
function deleteLastChar() {
if (_error) {
clear()
_error = false
} else {
display_text.text = display_text.text.substring(0, display_text.text.length - 1)
display_text.scale()
}
}
function replaceLastChar(ch) {
display_text.text = display_text.text.substring(0, display_text.text.length - 1) + ch
display_text.scale()
}
function error(msg) {
_error = true
display_text.text = msg
}
function getChar(pos) {
return display_text.text.substring(pos, pos + 1)
}
function getFirstChar() {
return getChar(0)
}
function getLastChar() {
return getChar(display_text.text.length - 1);
}
function getBuffer() {
return display_text.text
}
function getBufferLen() {
return display_text.text.length
}
function isEmpty() {
return display_text.text.length == 0 // Alternatively we could test the string aswell
}
TextInput {
id: display_text
text: qsTr("")
anchors.verticalCenter: parent.verticalCenter
font.pointSize: 0.00004 * (win.height * win.width)
/* Scale text to fit into virtual display */
function scale() {
while(true) {
if (font.pointSize > 0.00001 * (win.height * win.width) && width > display.width) {
font.pointSize /= 2
}
else if (font.pointSize < 0.00004 * (win.height * win.width) && width * 2 <= display.width) {
font.pointSize *= 2
} else {
/* Ideal scale */
break
}
}
}
}
}
/* Number Keys 0 - 9, ., = */
Rectangle {
id: numkeys
height: win.height - display.height
width: 0.733 * parent.width
color: "black"
anchors.top: display.bottom
Grid {
rows: 4
columns: 3
Repeater {
id: numkeys_grid
model: 12
/* Number Key */
Rectangle {
id: numkey
height: numkeys.height / 4
width: numkeys.width / 3
color: numkeys.color
Text {
id: numkey_text
/* Refactor this with a more mathematical or simpler solution */
text: {
var invIndex = 9 - index
if (invIndex > 0) {parent
switch(invIndex % 3) {
case 0:
return String(invIndex- 2)
case 1:
return String(invIndex + 2)
default:
return String(invIndex)
}
} else {
switch(invIndex) {
case 0:
return ".";
case -1:
return "0";
case -2:
return "=";
}
}
}
font.pointSize: 0.00001 * (win.height * win.width)
anchors.centerIn: parent
color: "white"
}
/* Registers clicks/touches for key */
MouseArea {
id: numkeyMouseArea
anchors.fill: parent
onClicked: {
/* "=" has been clicked/pressed, evaluate input */
if(numkey_text.text == "=")
{
if (!display.isEmpty()) {
/* Adjust/Fix statement for evaluation */
var stmt = display.getBuffer().split('−').join('-')
stmt = stmt.split('÷').join('/')
stmt = stmt.split('×').join('*')
try {
var res = eval(stmt)
if (isFinite(res)) {
/* Floor too large decimals */
if (res < 0) {
res = String(res).substring(0, 14) // Floor at the 14th character (12th decimal)
}
/* Draw result to display */
display.draw(res)
} else {
throw ""
}
} catch(e) {
display.error("Error")
}
opkeys_grid.itemAt(0).children[0].text = "CLR"
}
}
/* Add character to win */
else
{
if(opkeys_grid.itemAt(0).children[0].text === "CLR") {
display.draw(numkey_text.text)
opkeys_grid.itemAt(0).children[0].text = "DEL"
} else {
display.add(numkey_text.text)
}
}
}
}
/* Invert Button Colors while clicked/pressed */
states: State {
name: "pressed";
when: numkeyMouseArea.pressed
PropertyChanges {
target: numkey;
color: "white"
}
PropertyChanges {
target: numkey_text
color: "black"
}
}
}
}
}
}
Rectangle {
id: opkeys
height: numkeys.height
width: win.width - numkeys.width
anchors.top: display.bottom
anchors.left: numkeys.right
Grid {
rows: 5
columns: 1
Repeater {
id: opkeys_grid
model: 5
Rectangle {
id: opkey
height: opkeys.height / 5
width: opkeys.width
Text {
id: opkey_text
anchors.centerIn: parent
text: operators[index]
font.pointSize: 0.00001 * (win.height * win.width)
property var operators: ["DEL", "÷", "×", "−", "+"]
}
/* Registers clicks/touches for key */
MouseArea {
id: opkeyMouseArea
anchors.fill: parent
/* Checks if last character was an operator */
function lastCharIsOp() {
for (var i = 0; i < opkey_text.operators.length; i++) {
if (display.getLastChar() === opkey_text.operators[i]) {
return true;
}
}
return false;
}
/* Corrects length for following conditions to work properly with less exceptions */
function offset() { return (!display.isEmpty() && display.getFirstChar() === "−") }
onClicked: {
if (opkey_text.text == "DEL") {
if(display_text.focus) {
display.deleteAtCursor()
} else {
display.deleteLastChar()
}
}
else if (opkey_text.text == "CLR") {
display.clear()
opkey_text.text = "DEL"
}
else if (opkey_text.text === "−" || display.getBufferLen() - offset() && !display._error) {
if (lastCharIsOp() === true) {
display.replaceLastChar(opkey_text.text)
} else {
if (opkeys_grid.itemAt(0).children[0].text == "CLR") {
opkeys_grid.itemAt(0).children[0].text = "DEL"
}
display.add(opkey_text.text)
}
}
}
onPressAndHold: {
/* Clear virtual display if user held DEL */
if (opkey_text.text == "DEL") {
display.clear()
}
}
}
/* Invert Button Colors while clicked/pressed */
states: State {
name: "pressed";
when: opkeyMouseArea.pressed
PropertyChanges {
target: opkey;
color: "black"
}
PropertyChanges {
target: opkey_text
color: "white"
}
}
}
}
}
}
}