Commit 8b299402 authored by Urs Fleisch's avatar Urs Fleisch
Browse files

QML: Add script utility functions.

parent b59b8144
......@@ -28,6 +28,9 @@
#include <QMetaProperty>
#include <QCoreApplication>
#include <QFile>
#include <QDir>
#include <QProcess>
#include <QImage>
#include "pictureframe.h"
#if QT_VERSION < 0x050000
......@@ -236,3 +239,140 @@ QByteArray ScriptUtils::readFile(const QString& filePath)
}
return data;
}
/**
* Remove file.
* @param filePath path to file
* @return true if ok.
*/
bool ScriptUtils::removeFile(const QString& filePath)
{
return QFile::remove(filePath);
}
/**
* Get path of temporary directory.
* @return temporary directory.
*/
QString ScriptUtils::tempPath()
{
return QDir::tempPath();
}
/**
* Synchronously start a system command.
* @param program executable
* @param args arguments
* @param msecs timeout in milliseconds, -1 for no timeout
* @return [exit code, standard output, standard error], empty list on timeout.
*/
QVariantList ScriptUtils::system(
const QString& program, const QStringList& args, int msecs)
{
QProcess proc;
proc.start(program, args);
if (proc.waitForFinished(msecs)) {
return QVariantList()
<< proc.exitCode()
<< QString::fromLocal8Bit(proc.readAllStandardOutput())
<< QString::fromLocal8Bit(proc.readAllStandardError());
}
return QVariantList();
}
/**
* Get value of environment variable.
* @param varName variable name
* @return value.
*/
QByteArray ScriptUtils::getEnv(const QByteArray& varName)
{
return qgetenv(varName.constData());
}
/**
* Set value of environment variable.
* @param varName variable name
* @param value value to set
* @return true if value could be set.
*/
bool ScriptUtils::setEnv(const QByteArray& varName, const QByteArray& value)
{
return qputenv(varName, value);
}
/**
* Load an image from a file.
* @param filePath path to file
* @return image variant.
*/
QVariant ScriptUtils::loadImage(const QString& filePath)
{
QImage img(filePath);
return QVariant::fromValue(img);
}
/**
* Save an image to a file.
* @param var image variant
* @param filePath path to file
* @param format image format, default is "JPG"
* @return true if ok.
*/
bool ScriptUtils::saveImage(const QVariant& var, const QString& filePath,
const QByteArray& format)
{
QImage img(var.value<QImage>());
if (!img.isNull()) {
return img.save(filePath, format.constData());
}
return false;
}
/**
* Get properties of an image.
* @param var image variant
* @return map containing "width", "height", "depth" and "colorCount",
* empty if invalid image.
*/
QVariantMap ScriptUtils::imageProperties(const QVariant& var)
{
QVariantMap map;
QImage img(var.value<QImage>());
if (!img.isNull()) {
map.insert(QLatin1String("width"), img.width());
map.insert(QLatin1String("height"), img.height());
map.insert(QLatin1String("depth"), img.depth());
#if QT_VERSION >= 0x040600
map.insert(QLatin1String("colorCount"), img.colorCount());
#else
map.insert(QLatin1String("colorCount"), img.numColors());
#endif
}
return map;
}
/**
* Scale an image.
* @param var image variant
* @param width scaled width, -1 to keep aspect ratio
* @param height scaled height, -1 to keep aspect ratio
* @return scaled image variant.
*/
QVariant ScriptUtils::scaleImage(const QVariant& var, int width, int height)
{
QImage img(var.value<QImage>());
if (!img.isNull()) {
if (width > 0 && height > 0) {
return QVariant::fromValue(img.scaled(width, height,
Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
} else if (width > 0) {
return QVariant::fromValue(img.scaledToWidth(width,
Qt::SmoothTransformation));
} else if (height > 0) {
return QVariant::fromValue(img.scaledToHeight(height,
Qt::SmoothTransformation));
}
}
return QVariant();
}
......@@ -152,6 +152,83 @@ public:
* @return data read, empty if failed.
*/
Q_INVOKABLE static QByteArray readFile(const QString& filePath);
/**
* Remove file.
* @param filePath path to file
* @return true if ok.
*/
Q_INVOKABLE static bool removeFile(const QString& filePath);
/**
* Get path of temporary directory.
* @return temporary directory.
*/
Q_INVOKABLE static QString tempPath();
/**
* Synchronously start a system command.
* @param program executable
* @param args arguments
* @param msecs timeout in milliseconds, -1 for no timeout
* @return [exit code, standard output, standard error], empty list on timeout.
*/
Q_INVOKABLE static QVariantList system(
const QString& program, const QStringList& args = QStringList(),
int msecs = -1);
/**
* Get value of environment variable.
* @param varName variable name
* @return value.
*/
Q_INVOKABLE static QByteArray getEnv(const QByteArray& varName);
/**
* Set value of environment variable.
* @param varName variable name
* @param value value to set
* @return true if value could be set.
*/
Q_INVOKABLE static bool setEnv(const QByteArray& varName,
const QByteArray& value);
/**
* Load an image from a file.
* @param filePath path to file
* @return image variant.
*/
Q_INVOKABLE static QVariant loadImage(const QString& filePath);
/**
* Save an image to a file.
* @param var image variant
* @param filePath path to file
* @param format image format, default is "JPG"
* @return true if ok.
*/
Q_INVOKABLE static bool saveImage(const QVariant& var,
const QString& filePath,
const QByteArray& format = "JPG");
/**
* Get properties of an image.
* @param var image variant
* @return map containing "width", "height", "depth" and "colorCount",
* empty if invalid image.
*/
Q_INVOKABLE static QVariantMap imageProperties(const QVariant& var);
/**
* Scale an image.
* @param var image variant
* @param width scaled width, -1 to keep aspect ratio
* @param height scaled height, -1 to keep aspect ratio
* @return scaled image variant.
*/
Q_INVOKABLE static QVariant scaleImage(const QVariant& var,
int width, int height = -1);
};
#endif // SCRIPTUTILS_H
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