Commit 5597fbf2 authored by Urs Fleisch's avatar Urs Fleisch
Browse files

QML: Split scripts into asynchronous chunks to keep GUI responsive.

parent d9546fe8
......@@ -136,7 +136,37 @@ Timer {
}
}
interval: 1
onTriggered: main()
Component.onCompleted: start()
/**
* Invoke a callback with delay in ms and optional arguments.
* This can be used to keep the GUI responsive by splitting the code
* into asynchronous functions and call them after a small delay.
*/
function setTimeout(callback, delay) {
var argv = Array.prototype.slice.call(arguments, 2)
function timeoutHandler() {
triggered.disconnect(timeoutHandler)
callback.apply(null, argv)
}
triggered.connect(timeoutHandler)
interval = delay
if (!running) {
start()
} else {
// QtQuick 1 cannot restart a timer from a handler, see QTBUG-22004,
// http://permalink.gmane.org/gmane.comp.lib.qt.qml/3213
// This workaround starts it when it is possible again.
function restartHandler() {
runningChanged.disconnect(restartHandler)
if (!running) {
start()
}
}
runningChanged.connect(restartHandler)
}
}
Component.onCompleted: setTimeout(main, 1)
}
......@@ -26,33 +26,39 @@ import Kid3 1.0
Kid3Script {
onRun: {
app.firstFile()
do {
function doWork() {
if (app.selectionInfo.tagFormatV2) {
var rgStr = app.getFrame(tagv2, "replaygain_track_gain")
if (rgStr) {
var rg = parseFloat(rgStr)
if (isNaN(rg)) {
if (!isNaN(rg)) {
// 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)
} else {
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()
if (!app.nextFile()) {
if (isStandalone()) {
// Save the changes if the script is started stand-alone, not from Kid3.
app.saveDirectory()
}
Qt.quit()
} else {
setTimeout(doWork, 1)
}
}
Qt.quit()
app.firstFile()
doWork()
}
}
......@@ -29,8 +29,8 @@ Kid3Script {
var tempPath = script.tempPath()
var picPath = tempPath + "/kid3img"
var maxPixels = 500
app.firstFile()
do {
function doWork() {
if (app.selectionInfo.tagFormatV2) {
var frames = app.getAllFrames(tagv2)
if ("Picture" in frames) {
......@@ -59,12 +59,19 @@ Kid3Script {
}
}
}
} while (app.nextFile())
script.removeFile(picPath)
if (isStandalone()) {
// Save the changes if the script is started stand-alone, not from Kid3.
app.saveDirectory()
if (!app.nextFile()) {
script.removeFile(picPath)
if (isStandalone()) {
// Save the changes if the script is started stand-alone, not from Kid3.
app.saveDirectory()
}
Qt.quit()
} else {
setTimeout(doWork, 1)
}
}
Qt.quit()
app.firstFile()
doWork()
}
}
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