Commit e34c842e authored by Urs Fleisch's avatar Urs Fleisch
Browse files

QML: Add example scripts.

parent 91d81eef
......@@ -370,6 +370,11 @@ else (WIN32)
set(ZLIB_LIBRARIES -lz)
endif (WIN32)
### Check for QML
if (WITH_QML AND (QT_QTDECLARATIVE_FOUND OR Qt5Qml_FOUND))
set(HAVE_QML 1)
endif (WITH_QML AND (QT_QTDECLARATIVE_FOUND OR Qt5Qml_FOUND))
### Check for libQtDBus
if (WITH_DBUS AND QT_QTDBUS_LIBRARY)
set(HAVE_QTDBUS 1)
......
......@@ -8,6 +8,9 @@
/* Define if you have QtDBus installed */
#cmakedefine HAVE_QTDBUS 1
/* Define if QML is enabled */
#cmakedefine HAVE_QML 1
/* Define if you have Phonon installed */
#cmakedefine HAVE_PHONON 1
......
......@@ -31,8 +31,9 @@
* Current configuration file version.
* 0: <= 3.0.2
* 1: 3.1
* 2: 3.2
*/
static const int CONFIG_VERSION = 1;
static const int CONFIG_VERSION = 2;
/** Version of configuration file read, -1 if not read yet. */
int ConfigStore::s_configVersion = -1;
......
......@@ -27,6 +27,7 @@
#include "useractionsconfig.h"
#include <QStringList>
#include <stdlib.h>
#include "config.h"
int UserActionsConfig::s_index = -1;
......@@ -132,6 +133,16 @@ void UserActionsConfig::readFromConfig(ISettings* config)
m_contextMenuCommands.push_back(
UserActionsConfig::MenuCommand(QLatin1String("LyrDB LRC"), QLatin1String("%{browser} http://lyrdb.com/karaoke/index.htm?q=%u{artist}+%u{title}&action=search")));
}
#ifdef HAVE_QML
if (ConfigStore::getConfigVersion() < 2) {
m_contextMenuCommands.push_back(
UserActionsConfig::MenuCommand(QLatin1String("QML Console"), QLatin1String("@qmlview %{qmlpath}/script/QmlConsole.qml"), false, true));
m_contextMenuCommands.push_back(
UserActionsConfig::MenuCommand(QLatin1String("ReplayGain to SoundCheck"), QLatin1String("@qml %{qmlpath}/script/ReplayGain2SoundCheck.qml"), false, true));
m_contextMenuCommands.push_back(
UserActionsConfig::MenuCommand(QLatin1String("Resize Album Art"), QLatin1String("@qml %{qmlpath}/script/ResizeAlbumArt.qml"), false, true));
}
#endif
}
void UserActionsConfig::setContextMenuCommands(const QList<MenuCommand>& contextMenuCommands)
......
/**
* \file QmlConsole.qml
* Simple console to play with Kid3's QML API.
*
* \b Project: Kid3
* \author Urs Fleisch
* \date 01 Mar 2015
*
* Copyright (C) 2015 Urs Fleisch
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; version 3.
*
* 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import QtQuick 2.2
import Kid3 1.0
Rectangle {
id: root
/**
* tagv1, tagv2, tagv2v1 can be used for the tag version with both
* QtQuick 1 (Qt 4) and QtQuick 2 (Qt 5).
* QtQuick 1 needs the enum types from C++, so a helper function is used.
*/
//property variant tagv1: script.toTagVersion(Frame.TagV1) //@QtQuick1
//property variant tagv2: script.toTagVersion(Frame.TagV2) //@QtQuick1
//property variant tagv2v1: script.toTagVersion(Frame.TagV2V1) //@QtQuick1
readonly property int tagv1: Frame.TagV1 //@QtQuick2
readonly property int tagv2: Frame.TagV2 //@QtQuick2
readonly property int tagv2v1: Frame.TagV2V1 //@QtQuick2
property SystemPalette palette: SystemPalette {}
property string help:
".quit - quit console\n" +
".help - show help\n" +
".clear - clear output\n" +
"dir(obj) - dump script properties of object\n" +
"script.properties(obj) - dump Qt properties of object\n"
width: 400
height: 300
color: palette.window
ScriptUtils {
id: script
}
ConfigObjects {
id: configs
}
Rectangle {
anchors {
top: parent.top
bottom: inputRect.top
left: parent.left
right: parent.right
margins: 8
}
border.width: 1
color: palette.base
Flickable {
id: flick
anchors.fill: parent
anchors.margins: 8
contentWidth: output.paintedWidth
contentHeight: output.paintedHeight
flickableDirection: Flickable.VerticalFlick
clip: true
function ensureVisible(r) {
if (contentX >= r.x)
contentX = r.x;
else if (contentX + width <= r.x + r.width)
contentX = r.x + r.width - width;
if (contentY >= r.y)
contentY = r.y;
else if (contentY + height <= r.y + r.height)
contentY = r.y + r.height - height;
}
TextEdit {
id: output
width: flick.width
height: flick.height
wrapMode: TextEdit.Wrap
textFormat: TextEdit.PlainText
readOnly: true
selectByMouse: true
onCursorRectangleChanged: flick.ensureVisible(cursorRectangle)
text: help
}
}
}
Rectangle {
id: inputRect
anchors {
left: parent.left
right: parent.right
bottom: parent.bottom
margins: 8
}
height: input.implicitHeight + 2 * input.anchors.margins
border.width: 1
color: palette.base
TextInput {
id: input
property variant history: []
property int historyIndex: 0
/**
* Get string with JavaScript properties of object.
*/
function dir(obj) {
var lines = []
for (var p in obj) {
lines.push(p + " = " +
(typeof obj[p] !== "function" ? obj[p] : "function"))
}
// Sort and remove duplicates.
lines.sort()
lines = lines.filter(function(val, idx, arr) {
return idx === 0 || val !== arr[idx - 1];
})
return lines.join("\n")
}
anchors.fill: parent
anchors.margins: 8
focus: true
selectByMouse: true
onAccepted: {
if (!text)
return
if (text === ".quit") {
Qt.quit()
} else if (text === ".clear") {
output.text = ""
history = []
} else if (text === ".help") {
output.text += help
} else if (text === "dir") {
output.text +=
"Try dir(obj), with obj e.g. app, script, configs\n"
} else {
var result
try {
result = eval(text)
} catch (ex) {
result = ex.message
}
if (typeof result === "undefined") {
result = "undefined"
} else if (result === null) {
result = "null"
}
output.text += "> " + text + "\n" + result.toString() + "\n"
}
output.cursorPosition = output.text.length
var hist = history
hist.push(text)
history = hist
text = ""
}
Keys.onPressed: {
var histLen
if (event.key === Qt.Key_Up) {
histLen = history.length
if (historyIndex < histLen) {
text = history[histLen - 1 - historyIndex]
++historyIndex
} else {
text = ""
}
} else if (event.key === Qt.Key_Down) {
histLen = history.length
if (historyIndex > 0 && historyIndex <= histLen) {
--historyIndex
text = history[histLen - 1 - historyIndex]
} else {
text = ""
}
} else {
historyIndex = 0
}
}
}
}
Component.onCompleted: {
if (typeof args === "undefined") {
// Started as a QML script outside of Kid3.
app.selectedFilesUpdated.connect(app.tagsToFrameModels)
app.readConfig()
}
}
}
/**
* \file ReplayGain2SoundCheck.qml
* Create iTunNORM SoundCheck information from replay gain frames.
* Replay gain tags can be generated e.g. using "mp3gain -s i".
*
* \b Project: Kid3
* \author Urs Fleisch
* \date 28 Feb 2015
*
* Copyright (C) 2015 Urs Fleisch
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; version 3.
*
* 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import Kid3 1.0
Kid3Script {
onRun: {
app.firstFile()
do {
if (app.selectionInfo.tagFormatV2) {
var rgStr = app.getFrame(tagv2, "replaygain_track_gain")
if (rgStr) {
var rg = parseFloat(rgStr)
if (isNaN(rg)) {
console.log("Value %1 is not a float in %2".
arg(rgStr).arg(app.selectionInfo.fileName))
continue
}
// Calculate SoundCheck value and insert a sequence of 10
// hex zero-padded to 8 digits values into the iTunNORM frame.
var scVal = parseInt(Math.pow(10, -rg / 10) * 1000).toString(16)
var pad = "00000000"
scVal = " " + (pad + scVal).slice(-pad.length)
var sc = new Array(11).join(scVal)
console.log("Set iTunNORM to %1 in %2".
arg(sc).arg(app.selectionInfo.fileName))
app.setFrame(tagv2, "iTunNORM", sc)
}
}
} while (app.nextFile())
if (isStandalone()) {
// Save the changes if the script is started stand-alone, not from Kid3.
app.saveDirectory()
}
Qt.quit()
}
}
/**
* \file ResizeAlbumArt.qml
* Resize embedded cover art images which are larger than 500x500 pixels.
* The maximum size can be adapted by changing the maxPixels variable.
*
* \b Project: Kid3
* \author Urs Fleisch
* \date 28 Feb 2015
*
* Copyright (C) 2015 Urs Fleisch
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; version 3.
*
* 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import Kid3 1.0
Kid3Script {
onRun: {
var tempPath = script.tempPath()
var picPath = tempPath + "/kid3img"
var maxPixels = 500
app.firstFile()
do {
if (app.selectionInfo.tagFormatV2) {
var frames = app.getAllFrames(tagv2)
if ("Picture" in frames) {
script.removeFile(picPath)
var desc = app.getFrame(tagv2, "Picture:" + picPath)
var img = script.loadImage(picPath)
var imgProps = script.imageProperties(img)
var width = imgProps.width, height = imgProps.height
if (width > maxPixels || height > maxPixels) {
if (width >= height) {
width = maxPixels; height = -1
} else {
width = -1; height = maxPixels
}
img = script.scaleImage(img, width, height)
imgProps = script.imageProperties(img)
if (script.saveImage(img, picPath)) {
app.setFrame(tagv2, "Picture:" + picPath, desc)
console.log("Resized image to %1x%2 in %3".
arg(imgProps.width).arg(imgProps.height).
arg(app.selectionInfo.fileName))
} else {
console.log("Failed to save " + picPath + " for scaled image in "
+ app.selectionInfo.fileName)
}
}
}
}
} while (app.nextFile())
script.removeFile(picPath)
if (isStandalone()) {
// Save the changes if the script is started stand-alone, not from Kid3.
app.saveDirectory()
}
Qt.quit()
}
}
set (script_QML_SRCS
script/ReplayGain2SoundCheck.qml
script/ResizeAlbumArt.qml
script/QmlConsole.qml
)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment