Warning, /education/kalgebra/mobile/content/ui/Console.qml is written in an unsupported language. File is not indexed.

0001 // SPDX-FileCopyrightText: 2015 by Aleix Pol <aleixpol@kde.org>
0002 // SPDX-License-Identifier: GPL-2.0-or-later
0003 
0004 import QtCore
0005 import QtQuick
0006 import QtQuick.Layouts
0007 import QtQuick.Controls as QQC2
0008 import QtQml.Models
0009 import QtQuick.Dialogs
0010 import org.kde.kirigami as Kirigami
0011 import org.kde.kirigamiaddons.delegates as Delegates
0012 import org.kde.kirigamiaddons.formcard as FormCard
0013 import org.kde.analitza
0014 import org.kde.kalgebra.mobile
0015 
0016 Kirigami.ScrollablePage {
0017     id: page
0018 
0019     title: i18n("Calculator")
0020     ListModel { id: itemModel }
0021 
0022     // This content is only available in the desktop version of Kalgebra.
0023     // Don't put any information here that can't be accessed by another
0024     // part of Kalgebra.
0025     readonly property Item drawerContent : ColumnLayout {
0026         visible: true
0027         width: 300
0028         spacing: 0
0029 
0030         QQC2.ToolBar {
0031             Layout.fillWidth: true
0032             Layout.preferredHeight: applicationWindow().pageStack.globalToolBar.preferredHeight
0033 
0034             leftPadding: Kirigami.Units.smallSpacing
0035             rightPadding: Kirigami.Units.smallSpacing
0036             topPadding: Kirigami.Units.smallSpacing
0037             bottomPadding: Kirigami.Units.smallSpacing
0038 
0039             contentItem: Kirigami.Heading {
0040                 text: i18n("KAlgebra")
0041             }
0042         }
0043 
0044         QQC2.ScrollView {
0045             Layout.fillWidth: true
0046             Layout.fillHeight: true
0047 
0048             ListView {
0049                 model: VariablesModel { variables: App.variables }
0050                 delegate: Delegates.RoundedItemDelegate {
0051                     required property string whatsThis
0052                     required property int index
0053 
0054                     text: whatsThis
0055                 }
0056             }
0057         }
0058     }
0059 
0060     actions: [
0061         Kirigami.Action {
0062             text: i18nc("@action:button", "Load Script")
0063             onTriggered: {
0064                 fileDialog.title = text
0065                 fileDialog.proceed = function() { consoleModel.loadScript(fileDialog.fileUrl) }
0066                 fileDialog.nameFilters = [ i18n("Script (*.kal)") ]
0067                 fileDialog.fileMode = FileDialog.OpenFile
0068                 fileDialog.open()
0069             }
0070         },
0071         Kirigami.Action {
0072             text: i18nc("@action:button", "Save Script")
0073             onTriggered: {
0074                 fileDialog.title = text
0075                 fileDialog.proceed = function() { consoleModel.saveScript(fileDialog.fileUrl) }
0076                 fileDialog.nameFilters = [ i18n("Script (*.kal)") ]
0077                 fileDialog.fileMode = FileDialog.SaveFile
0078                 fileDialog.open()
0079             }
0080         },
0081         //TODO: Recent scripts
0082         Kirigami.Action {
0083             text: i18nc("@action:button", "Export Log")
0084             onTriggered: {
0085                 fileDialog.title = text
0086                 fileDialog.proceed = function() { consoleModel.saveLog(fileDialog.fileUrl) }
0087                 fileDialog.nameFilters = [ i18n("HTML (*.html)") ]
0088                 fileDialog.fileMode = FileDialog.SaveFile
0089                 fileDialog.open()
0090             }
0091         },
0092         // --
0093         Kirigami.Action {
0094             text: consoleModel.mode === 1 ? i18nc("@action:button", "Evaluate") : i18nc("@action:button", "Calculate")
0095             onTriggered: consoleModel.mode = consoleModel.mode === 1 ? 0 : 1
0096         },
0097         // --
0098         Kirigami.Action {
0099             icon.name: "edit-clear-history"
0100             text: i18nc("@action:button", "Clear Log")
0101             onTriggered: itemModel.clear()
0102             enabled: itemModel.count !== 0
0103         }
0104     ]
0105 
0106     ListView {
0107         id: view
0108         model: itemModel
0109 
0110         bottomMargin: Kirigami.Units.largeSpacing
0111         topMargin: Kirigami.Units.largeSpacing
0112         spacing: Kirigami.Units.largeSpacing
0113 
0114         delegate: FormCard.FormCard {
0115             width: ListView.view.width
0116             FormCard.AbstractFormDelegate {
0117                 background: null
0118                 contentItem: RowLayout {
0119                     QQC2.Label {
0120                         text: model.result
0121                         onLinkActivated: {
0122                             input.remove(input.selectionStart, input.selectionEnd)
0123                             input.insert(input.cursorPosition, consoleModel.readContent(link))
0124                         }
0125 
0126                         Layout.fillWidth: true
0127                     }
0128 
0129                     QQC2.ToolButton {
0130                         visible: App.functionsModel().canAddFunction(expression, 2, App.variables)
0131                         text: i18n("2D Plot")
0132                         onClicked: {
0133                             App.functionsModel().addFunction(expression, 2, App.variables)
0134                             show2dPlotAction.trigger();
0135                         }
0136                     }
0137 
0138                     QQC2.ToolButton {
0139                         visible: App.functionsModel().canAddFunction(expression, 4, App.variables)
0140                         text: i18n("3D Plot")
0141                         onClicked: {
0142                             App.functionsModel().addFunction(expression, 4, App.variables)
0143                             show3dPlotAction.trigger();
0144                         }
0145                     }
0146 
0147                     QQC2.ToolButton {
0148                         readonly property string value: result.replace(/<[^>]*>/g, '');
0149                         text: i18n("Copy \"%1\"", value)
0150                         icon.name: "edit-copy"
0151                         display: QQC2.ToolButton.IconOnly
0152                         onClicked: {
0153                             clipboard.content = value
0154                         }
0155 
0156                         QQC2.ToolTip.text: text
0157                         QQC2.ToolTip.visible: hovered
0158                         QQC2.ToolTip.delay: Kirigami.Units.toolTipDelay
0159                     }
0160                 }
0161             }
0162         }
0163         Clipboard {
0164             id: clipboard
0165         }
0166 
0167         ConsoleModel {
0168             id: consoleModel
0169             variables: App.variables
0170             onMessage: (msg, operation, result) => {
0171                 itemModel.append({ result: msg, expression: result.toString()  })
0172                 input.selectAll()
0173                 view.currentIndex = view.count-1
0174                 view.positionViewAtIndex(view.currentIndex, ListView.Contain)
0175             }
0176         }
0177 
0178         FileDialog {
0179             id: fileDialog
0180             currentFolder: StandardPaths.standardLocations(StandardPaths.HomeLocation)[0]
0181             onAccepted: proceed()
0182 
0183             property var proceed
0184         }
0185 
0186         Kirigami.PlaceholderMessage {
0187             text: i18nc("@info", "Empty console history")
0188             visible: view.count === 0
0189             anchors.centerIn: parent
0190             width: parent.width - Kirigami.Units.gridUnit * 4
0191         }
0192     }
0193 
0194     footer: ExpressionInput {
0195         id: input
0196         focus: true
0197 
0198         width: page.width
0199 
0200         onAddOperation: (content) => {
0201             consoleModel.addOperation(content)
0202         }
0203     }
0204 }