plugin: saveItem ensure parent dir

Also const everything and format
This commit is contained in:
2 * r + 2 * t 2025-08-27 17:17:07 +10:00
parent 93a779ffbd
commit 800b5e6515
4 changed files with 52 additions and 53 deletions

View file

@ -30,10 +30,8 @@ Image {
onStatusChanged: { onStatusChanged: {
if (source == cachePath && status === Image.Error) if (source == cachePath && status === Image.Error)
source = path; source = path;
else if (source == path && status === Image.Ready) { else if (source == path && status === Image.Ready)
Paths.mkdir(Paths.imagecache);
CUtils.saveItem(this, cachePath); CUtils.saveItem(this, cachePath);
}
} }
Process { Process {

View file

@ -5,61 +5,66 @@
#include <QtQuick/QQuickItemGrabResult> #include <QtQuick/QQuickItemGrabResult>
#include <QThreadPool> #include <QThreadPool>
#include <QQmlEngine> #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()); this->saveItem(target, path, QRect(), QJSValue(), QJSValue());
} }
void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect) { void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect) const {
this->saveItem(target, path, rect, QJSValue(), QJSValue()); this->saveItem(target, path, rect, QJSValue(), QJSValue());
} }
void CUtils::saveItem(QQuickItem* target, const QUrl& path, QJSValue onSaved) { void CUtils::saveItem(QQuickItem* target, const QUrl& path, QJSValue onSaved) const {
this->saveItem(target, path, QRect(), onSaved, QJSValue()); this->saveItem(target, path, QRect(), onSaved, QJSValue());
} }
void CUtils::saveItem(QQuickItem* target, const QUrl& path, QJSValue onSaved, QJSValue onFailed) { void CUtils::saveItem(QQuickItem* target, const QUrl& path, QJSValue onSaved, QJSValue onFailed) const {
this->saveItem(target, path, QRect(), onSaved, onFailed); this->saveItem(target, path, QRect(), onSaved, onFailed);
} }
void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, QJSValue onSaved) { void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, QJSValue onSaved) const {
this->saveItem(target, path, rect, onSaved, QJSValue()); this->saveItem(target, path, rect, onSaved, QJSValue());
} }
void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, QJSValue onSaved, QJSValue onFailed) { void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, QJSValue onSaved, QJSValue onFailed) const {
if (!target) { if (!target) {
qWarning() << "CUtils::saveItem: a target is required"; qWarning() << "CUtils::saveItem: a target is required";
return; return;
} }
if (!path.isLocalFile()) { if (!path.isLocalFile()) {
qWarning() << "CUtils::saveItem:" << path << "is not a local file"; qWarning() << "CUtils::saveItem:" << path << "is not a local file";
return; return;
} }
QSharedPointer<QQuickItemGrabResult> grabResult = target->grabToImage(); QSharedPointer<QQuickItemGrabResult> grabResult = target->grabToImage();
QObject::connect( QObject::connect(
grabResult.data(), grabResult.data(),
&QQuickItemGrabResult::ready, &QQuickItemGrabResult::ready,
this, this,
[grabResult, rect, path, onSaved, onFailed, this]() { [grabResult, rect, path, onSaved, onFailed, this]() {
QThreadPool::globalInstance()->start([grabResult, rect, path, onSaved, onFailed, this] { QThreadPool::globalInstance()->start([grabResult, rect, path, onSaved, onFailed, this] {
QImage image = grabResult->image(); QImage image = grabResult->image();
if (!rect.isEmpty()) { if (!rect.isEmpty()) {
image = image.copy(rect); image = image.copy(rect);
} }
const QString file = path.toLocalFile(); const QString file = path.toLocalFile();
if (image.save(file)) { const QString parent = QFileInfo(file).absolutePath();
if (onSaved.isCallable()) { if (QDir().mkpath(parent) && image.save(file)) {
onSaved.call({ QJSValue(file), qmlEngine(this)->toScriptValue(QVariant::fromValue(path)) }); 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)) }); } else {
} qWarning() << "CUtils::saveItem: failed to save" << path;
}); if (onFailed.isCallable()) {
} onFailed.call({ qmlEngine(this)->toScriptValue(QVariant::fromValue(path)) });
); }
}
});
}
);
} }

View file

@ -9,10 +9,10 @@ class CUtils : public QObject {
QML_SINGLETON; QML_SINGLETON;
public: public:
Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path); Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path) const;
Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, const QRect& rect); Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, const QRect& rect) const;
Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, QJSValue onSaved); 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); 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); 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); Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, QJSValue onSaved, QJSValue onFailed) const;
}; };

View file

@ -38,10 +38,6 @@ Singleton {
return stringify(path).replace("file://", ""); return stringify(path).replace("file://", "");
} }
function mkdir(path: url): void {
Quickshell.execDetached(["mkdir", "-p", strip(path)]);
}
function copy(from: url, to: url): void { function copy(from: url, to: url): void {
Quickshell.execDetached(["cp", strip(from), strip(to)]); Quickshell.execDetached(["cp", strip(from), strip(to)]);
} }