Console.qml 6.65 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.Layouts 1.2
22
import QtQuick.Controls 2.5 as QQC2
23
import QtQml.Models 2.10
24
import QtQuick.Dialogs 1.0
25
import org.kde.analitza 1.0
26
import org.kde.kalgebra.mobile 1.0
27

28
Kirigami.ScrollablePage {
29
    id: page
30
31

    title: i18n("Calculator")
32
    ListModel { id: itemModel }
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
    // This content is only available in the desktop version of Kalgebra.
    // Don't put any information here that can't be accessed by another
    // part of Kalgebra.
    readonly property Item drawerContent : ColumnLayout {
        visible: true
        width: 300
        Kirigami.AbstractApplicationHeader {
            Layout.fillWidth: true
            topPadding: Kirigami.Units.smallSpacing
            bottomPadding: Kirigami.Units.largeSpacing
            leftPadding: Kirigami.Units.smallSpacing
            rightPadding: Kirigami.Units.smallSpacing
            Layout.preferredHeight: Kirigami.Units.gridUnit * 2
            Kirigami.Heading {
                level: 1
                text: i18n("Variables")
                Layout.fillWidth: true
            }
        }
        QQC2.ScrollView {
            Layout.fillWidth: true
            Layout.fillHeight: true
            ListView {
                model: VariablesModel { variables: App.variables }
                delegate: Kirigami.BasicListItem {
                    label: model.whatsThis
                }
            }
        }
    }

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

        delegate: Kirigami.Card {
115
            contentItem: QQC2.Label { text: model.result }
116
117
118
119
120
121
122
123
124
125
126
127
128

            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 {
129
                    visible: App.functionsModel().canAddFunction(expression, 2, App.variables)
130
131
                    text: i18n("2D Plot")
                    onTriggered: {
Carl Schwan's avatar
Carl Schwan committed
132
                        App.functionsModel().addFunction(expression, 2, App.variables)
133
                        show2dPlotAction.trigger();
134
135
136
                    }
                },
                Kirigami.Action {
137
                    visible: App.functionsModel().canAddFunction(expression, 4, App.variables)
138
139
                    text: i18n("3D Plot")
                    onTriggered: {
Carl Schwan's avatar
Carl Schwan committed
140
                        App.functionsModel().addFunction(expression, 4, App.variables)
141
                        show3dPlotAction.trigger();
142
143
144
145
                    }
                }
            ]
        }
146
147
148
        Clipboard {
            id: clipboard
        }
149

150
151
152
153
154
155
156
157
158
        ConsoleModel {
            id: consoleModel
            variables: App.variables
            onMessage: {
                itemModel.append({ result: msg, expression: result.toString()  })
                input.selectAll()
                view.currentIndex = view.count-1
                view.positionViewAtIndex(view.currentIndex, ListView.Contain)
            }
159
        }
160
161
162
163
164
165
166
167
168

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

            property var proceed
        }

169
170
    }

171
    footer: ExpressionInput {
172
173
        id: input
        focus: true
174

175
        Keys.onReturnPressed: {
176
            consoleModel.addOperation(text)
177
178
        }
    }
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
179
}