CalculationPage.qml 11.7 KB
Newer Older
Han Young's avatar
Han Young committed
1
/*
Han Young's avatar
Han Young committed
2
3
 * SPDX-FileCopyrightText: 2020-2021 Han Young <hanyoung@protonmail.com>
 * SPDX-FileCopyrightText: 2020 Devin Lin <espidev@gmail.com>
Han Young's avatar
Han Young committed
4
 *
Han Young's avatar
Han Young committed
5
 * SPDX-License-Identifier: GPL-3.0-or-later
Han Young's avatar
Han Young committed
6
7
8
9
 */
import QtQuick 2.7
import QtQuick.Layouts 1.1
import QtQuick.Controls 2.1 as Controls
10
import QtGraphicalEffects 1.12
Han Young's avatar
Han Young committed
11
12
13
14
import org.kde.kirigami 2.13 as Kirigami

Kirigami.Page {
    id: initialPage
Han Young's avatar
Han Young committed
15
    title: i18n("Calculator")
16
    topPadding: 0
Han Young's avatar
Han Young committed
17
18
19
    leftPadding: 0
    rightPadding: 0
    bottomPadding: 0
20
    
Devin Lin's avatar
Devin Lin committed
21
22
23
    property int yTranslate: 0
    property real mainOpacity: 1
    
24
    property color dropShadowColor: Qt.darker(Kirigami.Theme.backgroundColor, 1.15)
25
    readonly property bool inPortrait: initialPage.width < initialPage.height
Devin Lin's avatar
Devin Lin committed
26
    property int keypadHeight: {
Han Young's avatar
Han Young committed
27
        let height = 0;
28
29
30
        if (inPortrait) {
            height = initialPage.width * 7/6;
        } else {
Han Young's avatar
Han Young committed
31
            height = initialPage.width * 3/4;
Devin Lin's avatar
Devin Lin committed
32
        }
33
        if (height > initialPage.height - Kirigami.Units.gridUnit * 7) {
Han Young's avatar
Han Young committed
34
            height = initialPage.height - Kirigami.Units.gridUnit * 7;
35
        }
Han Young's avatar
Han Young committed
36
        return height;
Devin Lin's avatar
Devin Lin committed
37
    }
38
    
39
40
    Keys.onPressed: {
        switch(event.key) {
Han Young's avatar
Han Young committed
41
42
        case Qt.Key_Delete:
        case Qt.Key_Backspace:
Han Young's avatar
Han Young committed
43
            inputManager.backspace(); break;
44
        case Qt.Key_0:
Han Young's avatar
Han Young committed
45
            inputManager.append("0"); break;
46
        case Qt.Key_1:
Han Young's avatar
Han Young committed
47
            inputManager.append("1"); break;
48
        case Qt.Key_2:
Han Young's avatar
Han Young committed
49
            inputManager.append("2"); break;
50
        case Qt.Key_3:
Han Young's avatar
Han Young committed
51
            inputManager.append("3"); break;
52
        case Qt.Key_4:
Han Young's avatar
Han Young committed
53
            inputManager.append("4"); break;
54
        case Qt.Key_5:
Han Young's avatar
Han Young committed
55
            inputManager.append("5"); break;
56
        case Qt.Key_6:
Han Young's avatar
Han Young committed
57
            inputManager.append("6"); break;
58
        case Qt.Key_7:
Han Young's avatar
Han Young committed
59
            inputManager.append("7"); break;
60
        case Qt.Key_8:
Han Young's avatar
Han Young committed
61
            inputManager.append("8"); break;
62
        case Qt.Key_9:
Han Young's avatar
Han Young committed
63
            inputManager.append("9"); break;
64
        case Qt.Key_Plus:
Han Young's avatar
Han Young committed
65
            inputManager.append("+"); break;
66
        case Qt.Key_Minus:
Han Young's avatar
Han Young committed
67
            inputManager.append("-"); break;
Han Young's avatar
Han Young committed
68
        case Qt.Key_Asterisk:
Han Young's avatar
Han Young committed
69
            inputManager.append("×"); break;
Han Young's avatar
Han Young committed
70
        case Qt.Key_Slash:
Han Young's avatar
Han Young committed
71
            inputManager.append("÷"); break;
72
        case Qt.Key_AsciiCircum:
Han Young's avatar
Han Young committed
73
            inputManager.append("^"); break;
74
        case Qt.Key_Period:
Han Young's avatar
Han Young committed
75
            inputManager.append("."); break;
76
77
78
        case Qt.Key_Equal:
        case Qt.Key_Return:
        case Qt.Key_Enter:
Han Young's avatar
Han Young committed
79
            inputManager.equal(); break;
80
        }
Han Young's avatar
Han Young committed
81
        event.accepted = true;
82
    }
83
84
85

    // Changes the current mode of the backend to non-binary
    onIsCurrentPageChanged: {
Han Young's avatar
Han Young committed
86
87
        if (inputManager.binaryMode)
            inputManager.binaryMode = false
88
    }
89
    
90
    background: Rectangle {
Devin Lin's avatar
Devin Lin committed
91
92
        opacity: mainOpacity
        
93
94
95
96
97
98
        Kirigami.Theme.colorSet: Kirigami.Theme.View
        Kirigami.Theme.inherit: false
        color: Kirigami.Theme.backgroundColor
        anchors.fill: parent
    }
    
Devin Lin's avatar
Devin Lin committed
99
    Item {
Han Young's avatar
Han Young committed
100
        anchors.fill: parent
Devin Lin's avatar
Devin Lin committed
101
102
        opacity: mainOpacity
        transform: Translate { y: yTranslate }
103
        
Devin Lin's avatar
Devin Lin committed
104
105
106
        // top panel drop shadow
        RectangularGlow {
            opacity: mainOpacity
107
            
Devin Lin's avatar
Devin Lin committed
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
            anchors.fill: topPanelBackground
            anchors.topMargin: 1
            glowRadius: 4
            spread: 0.2
            color: dropShadowColor
        }
        
        Rectangle {
            id: topPanelBackground
            opacity: mainOpacity
            
            anchors.top: parent.top
            anchors.left: parent.left
            anchors.right: parent.right
            color: Kirigami.Theme.backgroundColor
            implicitHeight: outputScreen.height
        }
        
        ColumnLayout {
            opacity: mainOpacity
            transform: Translate { y: yTranslate }
            anchors.fill: parent
            spacing: 0
            
            Item {
                id: outputScreen
                z: 1
                Layout.fillWidth: true
                Layout.alignment: Qt.AlignTop
                Layout.preferredHeight: initialPage.height - initialPage.keypadHeight
138
                
Devin Lin's avatar
Devin Lin committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
                Column {
                    id: outputColumn
                    anchors.fill: parent
                    anchors.margins: Kirigami.Units.largeSpacing
                    spacing: Kirigami.Units.gridUnit
                    
                    Flickable {
                        anchors.right: parent.right
                        height: Kirigami.Units.gridUnit * 1.5
                        width: Math.min(parent.width, contentWidth)
                        contentHeight: expressionRow.height
                        contentWidth: expressionRow.width
                        flickableDirection: Flickable.HorizontalFlick
                        Controls.Label {
                            id: expressionRow
                            horizontalAlignment: Text.AlignRight
                            font.pointSize: Kirigami.Units.gridUnit
                            font.weight: Font.Light
                            text: inputManager.expression
                            color: Kirigami.Theme.disabledTextColor
159
                        }
Devin Lin's avatar
Devin Lin committed
160
161
162
                        onContentWidthChanged: {
                            if(contentWidth > width)
                                contentX = contentWidth - width;
163
                        }
Devin Lin's avatar
Devin Lin committed
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
                    }
                    
                    Flickable {
                        anchors.right: parent.right
                        height: Kirigami.Units.gridUnit * 4
                        width: Math.min(parent.width, contentWidth)
                        contentHeight: result.height
                        contentWidth: result.width
                        flickableDirection: Flickable.HorizontalFlick
                        Controls.Label {
                            id: result
                            horizontalAlignment: Text.AlignRight
                            font.pointSize: Kirigami.Units.gridUnit * 2
                            font.weight: Font.Light
                            text: inputManager.result
                            NumberAnimation on opacity {
                                id: resultFadeInAnimation
                                from: 0.5
                                to: 1
                                duration: Kirigami.Units.shortDuration
                            }
                            NumberAnimation on opacity {
                                id: resultFadeOutAnimation
                                from: 1
                                to: 0
                                duration: Kirigami.Units.shortDuration
                            }
Han Young's avatar
Han Young committed
191

Devin Lin's avatar
Devin Lin committed
192
193
                            onTextChanged: resultFadeInAnimation.start()
                        }
194
                    }
195
                }
Han Young's avatar
Han Young committed
196
            }
197
            
Devin Lin's avatar
Devin Lin committed
198
            // keypad area
199
            RowLayout {
Devin Lin's avatar
Devin Lin committed
200
                Layout.fillHeight: true
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
                Layout.fillWidth: true
                Item {
                    property string expression: ""
                    id: inputPad
                    Layout.fillHeight: true
                    Layout.preferredWidth: inPortrait ? initialPage.width : initialPage.width * 0.5
                    Layout.alignment: Qt.AlignLeft

                    NumberPad {
                        id: numberPad
                        anchors.fill: parent
                        anchors.topMargin: Kirigami.Units.gridUnit * 0.7
                        anchors.bottomMargin: Kirigami.Units.smallSpacing
                        anchors.leftMargin: Kirigami.Units.smallSpacing
                        anchors.rightMargin: Kirigami.Units.gridUnit * 1.5 // for right side drawer indicator
                        inPortrait: initialPage.inPortrait
                        onPressed: {
                            if (text == "DEL") {
                                inputManager.backspace();
                            } else if (text == "=") {
                                inputManager.equal();
                                resultFadeOutAnimation.start();
                            } else {
                                inputManager.append(text);
                            }
Devin Lin's avatar
Devin Lin committed
226
                        }
227
                        onClear: inputManager.clear()
228
                    }
229
230
231
232
233
234
235
236
237
238
239

                    // fast drop shadow
                    RectangularGlow {
                        visible: inPortrait
                        anchors.rightMargin: 1
                        anchors.fill: drawerIndicator
                        glowRadius: 4
                        spread: 0.2
                        color: initialPage.dropShadowColor
                    }

Devin Lin's avatar
Devin Lin committed
240
                    Rectangle {
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
                        id: drawerIndicator
                        visible: inPortrait
                        anchors.top: parent.top
                        anchors.bottom: parent.bottom
                        width: Kirigami.Units.gridUnit
                        x: parent.width - this.width

                        Kirigami.Theme.colorSet: Kirigami.Theme.View
                        Kirigami.Theme.inherit: false
                        color: Kirigami.Theme.backgroundColor

                        Rectangle {
                            anchors.centerIn: parent
                            height: parent.height / 20
                            width: parent.width / 4
                            radius: 3
                            color: Kirigami.Theme.textColor
                        }
Devin Lin's avatar
Devin Lin committed
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
                    Controls.Drawer {
                        id: functionDrawer
                        parent: initialPage
                        y: initialPage.height - inputPad.height
                        height: inputPad.height
                        width: initialPage.width * 0.8
                        visible: inPortrait
                        dragMargin: drawerIndicator.width
                        edge: Qt.RightEdge
                        dim: false
                        onXChanged: drawerIndicator.x = this.x - drawerIndicator.width + drawerIndicator.radius
                        opacity: 1 // for plasma style
                        FunctionPad {
                            anchors.fill: parent
                            anchors.bottom: parent.Bottom
                            anchors.leftMargin: Kirigami.Units.largeSpacing
                            anchors.rightMargin: Kirigami.Units.largeSpacing
                            anchors.topMargin: Kirigami.Units.largeSpacing
                            anchors.bottomMargin: parent.height / 4
                            onPressed: inputManager.append(text)
                        }
                        // for plasma style
                        background: Rectangle {
                            Kirigami.Theme.colorSet: Kirigami.Theme.View
                            color: Kirigami.Theme.backgroundColor
                            anchors.fill: parent
                        }
                    }
                }
                Item {
                    Layout.alignment:  Qt.AlignRight
                    Layout.fillHeight: true
                    Layout.fillWidth: true
Devin Lin's avatar
Devin Lin committed
294
295
296
297
298
299
300
301
302
303
                    visible: !inPortrait
                    FunctionPad {
                        anchors.fill: parent
                        anchors.bottom: parent.Bottom
                        anchors.leftMargin: Kirigami.Units.largeSpacing
                        anchors.rightMargin: Kirigami.Units.largeSpacing
                        anchors.topMargin: Kirigami.Units.largeSpacing
                        anchors.bottomMargin: parent.height / 4
                        onPressed: inputManager.append(text)
                    }
Han Young's avatar
Han Young committed
304
                }
Han Young's avatar
Han Young committed
305
306
307
            }
        }
    }
308
    
Han Young's avatar
Han Young committed
309
}