plugin: saveItem ensure parent dir
Also const everything and format
This commit is contained in:
parent
93a779ffbd
commit
800b5e6515
4 changed files with 52 additions and 53 deletions
|
|
@ -30,10 +30,8 @@ Image {
|
|||
onStatusChanged: {
|
||||
if (source == cachePath && status === Image.Error)
|
||||
source = path;
|
||||
else if (source == path && status === Image.Ready) {
|
||||
Paths.mkdir(Paths.imagecache);
|
||||
else if (source == path && status === Image.Ready)
|
||||
CUtils.saveItem(this, cachePath);
|
||||
}
|
||||
}
|
||||
|
||||
Process {
|
||||
|
|
|
|||
|
|
@ -5,61 +5,66 @@
|
|||
#include <QtQuick/QQuickItemGrabResult>
|
||||
#include <QThreadPool>
|
||||
#include <QQmlEngine>
|
||||
#include <QDir>
|
||||
|
||||
void CUtils::saveItem(QQuickItem* target, const QUrl& path) {
|
||||
void CUtils::saveItem(QQuickItem* target, const QUrl& path) const {
|
||||
this->saveItem(target, path, QRect(), QJSValue(), QJSValue());
|
||||
}
|
||||
|
||||
void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect) {
|
||||
this->saveItem(target, path, rect, QJSValue(), QJSValue());
|
||||
void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect) const {
|
||||
this->saveItem(target, path, rect, QJSValue(), QJSValue());
|
||||
}
|
||||
|
||||
void CUtils::saveItem(QQuickItem* target, const QUrl& path, QJSValue onSaved) {
|
||||
this->saveItem(target, path, QRect(), onSaved, QJSValue());
|
||||
void CUtils::saveItem(QQuickItem* target, const QUrl& path, QJSValue onSaved) const {
|
||||
this->saveItem(target, path, QRect(), onSaved, QJSValue());
|
||||
}
|
||||
|
||||
void CUtils::saveItem(QQuickItem* target, const QUrl& path, QJSValue onSaved, QJSValue onFailed) {
|
||||
this->saveItem(target, path, QRect(), onSaved, onFailed);
|
||||
void CUtils::saveItem(QQuickItem* target, const QUrl& path, QJSValue onSaved, QJSValue onFailed) const {
|
||||
this->saveItem(target, path, QRect(), onSaved, onFailed);
|
||||
}
|
||||
|
||||
void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, QJSValue onSaved) {
|
||||
this->saveItem(target, path, rect, onSaved, QJSValue());
|
||||
void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, QJSValue onSaved) const {
|
||||
this->saveItem(target, path, rect, onSaved, QJSValue());
|
||||
}
|
||||
|
||||
void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, QJSValue onSaved, QJSValue onFailed) {
|
||||
if (!target) {
|
||||
qWarning() << "CUtils::saveItem: a target is required";
|
||||
return;
|
||||
}
|
||||
void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, QJSValue onSaved, QJSValue onFailed) const {
|
||||
if (!target) {
|
||||
qWarning() << "CUtils::saveItem: a target is required";
|
||||
return;
|
||||
}
|
||||
|
||||
if (!path.isLocalFile()) {
|
||||
qWarning() << "CUtils::saveItem:" << path << "is not a local file";
|
||||
return;
|
||||
}
|
||||
if (!path.isLocalFile()) {
|
||||
qWarning() << "CUtils::saveItem:" << path << "is not a local file";
|
||||
return;
|
||||
}
|
||||
|
||||
QSharedPointer<QQuickItemGrabResult> grabResult = target->grabToImage();
|
||||
QSharedPointer<QQuickItemGrabResult> grabResult = target->grabToImage();
|
||||
|
||||
QObject::connect(
|
||||
grabResult.data(),
|
||||
&QQuickItemGrabResult::ready,
|
||||
this,
|
||||
[grabResult, rect, path, onSaved, onFailed, this]() {
|
||||
QThreadPool::globalInstance()->start([grabResult, rect, path, onSaved, onFailed, this] {
|
||||
QImage image = grabResult->image();
|
||||
QObject::connect(
|
||||
grabResult.data(),
|
||||
&QQuickItemGrabResult::ready,
|
||||
this,
|
||||
[grabResult, rect, path, onSaved, onFailed, this]() {
|
||||
QThreadPool::globalInstance()->start([grabResult, rect, path, onSaved, onFailed, this] {
|
||||
QImage image = grabResult->image();
|
||||
|
||||
if (!rect.isEmpty()) {
|
||||
image = image.copy(rect);
|
||||
}
|
||||
if (!rect.isEmpty()) {
|
||||
image = image.copy(rect);
|
||||
}
|
||||
|
||||
const QString file = path.toLocalFile();
|
||||
if (image.save(file)) {
|
||||
if (onSaved.isCallable()) {
|
||||
onSaved.call({ QJSValue(file), qmlEngine(this)->toScriptValue(QVariant::fromValue(path)) });
|
||||
}
|
||||
} else if (onFailed.isCallable()) {
|
||||
onFailed.call({ qmlEngine(this)->toScriptValue(QVariant::fromValue(path)) });
|
||||
}
|
||||
});
|
||||
}
|
||||
);
|
||||
const QString file = path.toLocalFile();
|
||||
const QString parent = QFileInfo(file).absolutePath();
|
||||
if (QDir().mkpath(parent) && image.save(file)) {
|
||||
if (onSaved.isCallable()) {
|
||||
onSaved.call({ QJSValue(file), qmlEngine(this)->toScriptValue(QVariant::fromValue(path)) });
|
||||
}
|
||||
} else {
|
||||
qWarning() << "CUtils::saveItem: failed to save" << path;
|
||||
if (onFailed.isCallable()) {
|
||||
onFailed.call({ qmlEngine(this)->toScriptValue(QVariant::fromValue(path)) });
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,10 +9,10 @@ class CUtils : public QObject {
|
|||
QML_SINGLETON;
|
||||
|
||||
public:
|
||||
Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path);
|
||||
Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, const QRect& rect);
|
||||
Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, QJSValue onSaved);
|
||||
Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, QJSValue onSaved, QJSValue onFailed);
|
||||
Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, QJSValue onSaved);
|
||||
Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, QJSValue onSaved, QJSValue onFailed);
|
||||
Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path) const;
|
||||
Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, const QRect& rect) const;
|
||||
Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, QJSValue onSaved) const;
|
||||
Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, QJSValue onSaved, QJSValue onFailed) const;
|
||||
Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, QJSValue onSaved) const;
|
||||
Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, QJSValue onSaved, QJSValue onFailed) const;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -38,10 +38,6 @@ Singleton {
|
|||
return stringify(path).replace("file://", "");
|
||||
}
|
||||
|
||||
function mkdir(path: url): void {
|
||||
Quickshell.execDetached(["mkdir", "-p", strip(path)]);
|
||||
}
|
||||
|
||||
function copy(from: url, to: url): void {
|
||||
Quickshell.execDetached(["cp", strip(from), strip(to)]);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue