Console.qml 5.64 KB
Newer Older
Yuri Chornoivan's avatar
Yuri Chornoivan committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*************************************************************************************
 *  Copyright (C) 2015 by Aleix Pol <aleixpol@kde.org>                               *
 *                                                                                   *
 *  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 2                   *
 *  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, write to the Free Software                      *
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA   *
 *************************************************************************************/

19
import org.kde.kirigami 2.5 as Kirigami
20
import QtQuick 2.2
21
import QtQuick.Controls 2.5 as QQC2
22
import QtQml.Models 2.10
23
import QtQuick.Dialogs 1.0
24
import org.kde.analitza 1.0
25
import org.kde.kalgebra.mobile 1.0
26

27
Kirigami.Page {
28
    id: page
29
30

    title: i18n("Calculator")
31
    ListModel { id: itemModel }
32

33
34
35
36
    Clipboard {
        id: clipboard
    }

37
38
    ConsoleModel {
        id: consoleModel
Carl Schwan's avatar
Carl Schwan committed
39
        variables: App.variables
40
        onMessage: {
41
            itemModel.append({ result: msg, expression: result.toString()  })
42
            input.selectAll()
43
44
            view.currentIndex = view.count-1
            view.positionViewAtIndex(view.currentIndex, ListView.Contain)
45
        }
46
47
48
49
50
51
52
53
54
55
56
    }

    FileDialog {
        id: fileDialog
        folder: shortcuts.home
        onAccepted: proceed()

        property var proceed
    }

    contextualActions: [
57
        Kirigami.Action {
58
59
60
            text: i18n("Load Script...")
            onTriggered: {
                fileDialog.title = text
61
62
                fileDialog.proceed = function() { consoleModel.loadScript(fileDialog.fileUrl) }
                fileDialog.nameFilters = [ i18n("Script (*.kal)") ]
63
                fileDialog.selectExisting = true
64
                fileDialog.open()
65
66
            }
        },
67
        Kirigami.Action {
68
            text: i18n("Save Script...")
69
70
71
72
            onTriggered: {
                fileDialog.title = text
                fileDialog.proceed = function() { consoleModel.saveScript(fileDialog.fileUrl) }
                fileDialog.nameFilters = [ i18n("Script (*.kal)") ]
73
                fileDialog.selectExisting = false
74
75
                fileDialog.open()
            }
76
77
        },
        //TODO: Recent scripts
78
        Kirigami.Action {
79
            text: i18n("Export Log...")
80
81
82
83
84
85
86
            onTriggered: {
                fileDialog.title = text
                fileDialog.proceed = function() { consoleModel.saveLog(fileDialog.fileUrl) }
                fileDialog.nameFilters = [ i18n("HTML (*.html)") ]
                fileDialog.selectExisting = false
                fileDialog.open()
            }
87
88
        },
        // --
89
        Kirigami.Action {
Laurent Montel's avatar
Laurent Montel committed
90
91
            text: consoleModel.mode === ConsoleModel.Calculate ? i18n("Evaluate...") : i18n("Calculate...")
            onTriggered: consoleModel.mode = consoleModel.mode === ConsoleModel.Calculate ? ConsoleModel.Evaluate : ConsoleModel.Calculate
92
93
        },
        // --
94
        Kirigami.Action {
95
96
97
98
99
            iconName: "edit-clear-history"
            text: i18n("Clear Log")
            onTriggered: itemModel.clear()
        }
    ]
100
    
101
    Kirigami.CardsListView {
102
103
        id: view
        model: itemModel
104
105

        delegate: Kirigami.Card {
106
            contentItem: QQC2.Label { text: model.result }
107
108
109
110
111
112
113
114
115
116
117
118
119

            hiddenActions: [
                QQC2.Action {
                    readonly property string value: result.replace(/<[^>]*>/g, '');
                    text: i18n("Copy \"%1\"", value)
                    icon.name: "edit-copy"
                    onTriggered: {
                        clipboard.content = value
                    }
                }
            ]
            actions: [
                Kirigami.Action {
Carl Schwan's avatar
Carl Schwan committed
120
                    enabled: App.functionsModel().canAddFunction(expression, 2, App.variables)
121
122
                    text: i18n("2D Plot")
                    onTriggered: {
Carl Schwan's avatar
Carl Schwan committed
123
                        App.functionsModel().addFunction(expression, 2, App.variables)
124
125
126
                    }
                },
                Kirigami.Action {
Carl Schwan's avatar
Carl Schwan committed
127
                    enabled: App.functionsModel().canAddFunction(expression, 4, App.variables)
128
129
                    text: i18n("3D Plot")
                    onTriggered: {
Carl Schwan's avatar
Carl Schwan committed
130
                        App.functionsModel().addFunction(expression, 4, App.variables)
131
132
133
134
                    }
                }
            ]
        }
135
136
137
138
139
140
141
142
143

        anchors {
            top: parent.top
            bottom: input.top
            left: parent.left
            right: parent.right
        }
    }

144
145
146
147
148
    ExpressionInput {
        id: input
        focus: true
        
        Keys.onReturnPressed: {
149
            consoleModel.addOperation(text)
150
151
        }
        
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
152
        anchors {
153
154
155
156
157
            bottom: parent.bottom
            left: parent.left
            right: parent.right
        }
    }
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
158
}