Warning, /multimedia/kid3/src/qml/script/QmlConsole.qml is written in an unsupported language. File is not indexed.

0001 /**
0002  * \file QmlConsole.qml
0003  * Simple console to play with Kid3's QML API.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 01 Mar 2015
0008  *
0009  * Copyright (C) 2015  Urs Fleisch
0010  *
0011  * This program is free software; you can redistribute it and/or modify
0012  * it under the terms of the GNU Lesser General Public License as published by
0013  * the Free Software Foundation; version 3.
0014  *
0015  * This program is distributed in the hope that it will be useful,
0016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018  * GNU Lesser General Public License for more details.
0019  *
0020  * You should have received a copy of the GNU Lesser General Public License
0021  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0022  */
0023 
0024 import QtQuick 2.2
0025 import QtQml 2.2
0026 import Kid3 1.0
0027 
0028 Rectangle {
0029   id: root
0030 
0031   /**
0032    * tagv1, tagv2, tagv2v1 can be used for the tag version with both
0033    * QtQuick 1 (Qt 4) and QtQuick 2 (Qt 5).
0034    * QtQuick 1 needs the enum types from C++, so a helper function is used.
0035    */
0036   //property variant tagv1: script.toTagVersion(Frame.TagV1) //@QtQuick1
0037   //property variant tagv2: script.toTagVersion(Frame.TagV2) //@QtQuick1
0038   //property variant tagv2v1: script.toTagVersion(Frame.TagV2V1) //@QtQuick1
0039   readonly property int tagv1: Frame.TagV1 //@QtQuick2
0040   readonly property int tagv2: Frame.TagV2 //@QtQuick2
0041   readonly property int tagv2v1: Frame.TagV2V1 //@QtQuick2
0042 
0043   // An empty context object for interactive variable storage.
0044   property var ctx: Object.create(null) //@QtQuick2
0045 
0046   property SystemPalette palette: SystemPalette {}
0047   property string help:
0048       ".quit    - quit console\n" +
0049       ".help    - show help\n" +
0050       ".clear   - clear output\n" +
0051       "dir(obj) - dump script properties of object\n" +
0052       "script.properties(obj) - dump Qt properties of object\n"
0053 
0054   width: 400
0055   height: 300
0056   color: palette.window
0057 
0058   ScriptUtils {
0059     id: script
0060   }
0061 
0062   ConfigObjects {
0063     id: configs
0064   }
0065 
0066   Rectangle {
0067     anchors {
0068       top: parent.top
0069       bottom: inputRect.top
0070       left: parent.left
0071       right: parent.right
0072       margins: 8
0073     }
0074     border.width: 1
0075     color: palette.base
0076 
0077     Flickable {
0078       id: flick
0079       anchors.fill: parent
0080       anchors.margins: 8
0081       contentWidth: output.paintedWidth
0082       contentHeight: output.paintedHeight
0083       flickableDirection: Flickable.VerticalFlick
0084       clip: true
0085 
0086       function ensureVisible(r) {
0087         if (contentX >= r.x)
0088           contentX = r.x;
0089         else if (contentX + width <= r.x + r.width)
0090           contentX = r.x + r.width - width;
0091         if (contentY >= r.y)
0092           contentY = r.y;
0093         else if (contentY + height <= r.y + r.height)
0094           contentY = r.y + r.height - height;
0095       }
0096 
0097       TextEdit {
0098         id: output
0099         color: palette.text
0100         width: flick.width
0101         height: flick.height
0102         wrapMode: TextEdit.Wrap
0103         textFormat: TextEdit.PlainText
0104         readOnly: true
0105         selectByMouse: true
0106         onCursorRectangleChanged: flick.ensureVisible(cursorRectangle)
0107         text: help
0108       }
0109     }
0110   }
0111 
0112   Rectangle {
0113     id: inputRect
0114     anchors {
0115       left: parent.left
0116       right: parent.right
0117       bottom: parent.bottom
0118       margins: 8
0119     }
0120     height: input.implicitHeight + 2 * input.anchors.margins
0121     border.width: 1
0122     color: palette.base
0123 
0124     TextInput {
0125       id: input
0126       color: palette.text
0127 
0128       property variant history: []
0129       property int historyIndex: 0
0130 
0131       /**
0132        * Get string with JavaScript properties of object.
0133        */
0134       function dir(obj) {
0135         var lines = []
0136         for (var p in obj) {
0137           lines.push(p + " = " +
0138                      (typeof obj[p] !== "function" ? obj[p] : "function"))
0139         }
0140         // Sort and remove duplicates.
0141         lines.sort()
0142         lines = lines.filter(function(val, idx, arr) {
0143           return idx === 0 || val !== arr[idx - 1];
0144         })
0145         return lines.join("\n")
0146       }
0147 
0148       anchors.fill: parent
0149       anchors.margins: 8
0150       focus: true
0151       selectByMouse: true
0152       clip: true
0153       onAccepted: {
0154         if (!text)
0155           return
0156         if (text === ".quit") {
0157           Qt.quit()
0158         } else if (text === ".clear") {
0159           output.text = ""
0160           history = []
0161         } else if (text === ".help") {
0162           output.text += help
0163         } else if (text === "dir") {
0164           output.text +=
0165               "Try dir(obj), with obj e.g. app, script, configs\n"
0166         } else {
0167           var result
0168           try {
0169             result = eval(text)
0170           } catch (ex) {
0171             result = ex.message
0172           }
0173           if (typeof result === "undefined") {
0174             result = "undefined"
0175           } else if (result === null) {
0176             result = "null"
0177           }
0178           output.text += "> " + text + "\n" + result.toString() + "\n"
0179         }
0180         output.cursorPosition = output.text.length
0181         var hist = history
0182         hist.push(text)
0183         history = hist
0184         text = ""
0185       }
0186       Keys.onPressed: {
0187         var histLen
0188         if (event.key === Qt.Key_Up) {
0189           histLen = history.length
0190           if (historyIndex < histLen) {
0191             text = history[histLen - 1 - historyIndex]
0192             ++historyIndex
0193           } else {
0194             text = ""
0195           }
0196         } else if (event.key === Qt.Key_Down) {
0197           histLen = history.length
0198           if (historyIndex > 0 && historyIndex <= histLen) {
0199             --historyIndex
0200             text = history[histLen - 1 - historyIndex]
0201           } else {
0202             text = ""
0203           }
0204         } else {
0205           historyIndex = 0
0206         }
0207       }
0208     }
0209   }
0210   Component.onCompleted: {
0211     if (typeof args === "undefined") {
0212       // Started as a QML script outside of Kid3.
0213       app.selectedFilesUpdated.connect(app.tagsToFrameModels)
0214       app.selectedFilesChanged.connect(app.tagsToFrameModels)
0215       app.readConfig()
0216     }
0217   }
0218 }