plugin: make thread safe

This commit is contained in:
2 * r + 2 * t 2025-08-27 22:13:21 +10:00
parent f8fa70fe0e
commit 573423742a
3 changed files with 46 additions and 46 deletions

View file

@ -70,8 +70,11 @@ void CachingImageManager::setPath(const QString& path) {
if (!path.isEmpty()) {
QThreadPool::globalInstance()->start([path, this] {
const QString sha = sha256sum(path);
QMetaObject::invokeMethod(this, [path, sha, this]() {
const QString filename = QString("%1@%2x%3.png")
.arg(sha256sum(path))
.arg(sha)
.arg(effectiveWidth())
.arg(effectiveHeight());
@ -83,21 +86,17 @@ void CachingImageManager::setPath(const QString& path) {
return;
}
if (QFile::exists(m_cachePath.toLocalFile())) {
QMetaObject::invokeMethod(m_item, [this]() {
bool cacheExists = QFile::exists(m_cachePath.toLocalFile());
if (cacheExists) {
m_item->setProperty("source", m_cachePath);
}, Qt::QueuedConnection);
m_usingCache = true;
emit usingCacheChanged();
} else {
QMetaObject::invokeMethod(m_item, [path, this]() {
m_item->setProperty("source", QUrl::fromLocalFile(path));
}, Qt::QueuedConnection);
m_usingCache = false;
emit usingCacheChanged();
}
m_usingCache = cacheExists;
emit usingCacheChanged();
});
});
}
}

View file

@ -7,27 +7,27 @@
#include <QQmlEngine>
#include <QDir>
void CUtils::saveItem(QQuickItem* target, const QUrl& path) const {
void CUtils::saveItem(QQuickItem* target, const QUrl& path) {
this->saveItem(target, path, QRect(), QJSValue(), QJSValue());
}
void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect) const {
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, QJSValue onSaved) const {
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, QJSValue onFailed) const {
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, const QRect& rect, QJSValue onSaved) const {
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, QJSValue onFailed) const {
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;
@ -40,10 +40,7 @@ void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, Q
QSharedPointer<QQuickItemGrabResult> grabResult = target->grabToImage();
QObject::connect(
grabResult.data(),
&QQuickItemGrabResult::ready,
this,
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();
@ -54,7 +51,10 @@ void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, Q
const QString file = path.toLocalFile();
const QString parent = QFileInfo(file).absolutePath();
if (QDir().mkpath(parent) && image.save(file)) {
bool success = QDir().mkpath(parent) && image.save(file);
QMetaObject::invokeMethod(this, [file, success, path, onSaved, onFailed, this]() {
if (success) {
if (onSaved.isCallable()) {
onSaved.call({ QJSValue(file), qmlEngine(this)->toScriptValue(QVariant::fromValue(path)) });
}
@ -64,6 +64,7 @@ void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, Q
onFailed.call({ qmlEngine(this)->toScriptValue(QVariant::fromValue(path)) });
}
}
}, Qt::QueuedConnection);
});
}
);

View file

@ -10,12 +10,12 @@ class CUtils : public QObject {
QML_SINGLETON;
public:
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;
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 bool copyFile(const QUrl& source, const QUrl& target) const;
Q_INVOKABLE bool copyFile(const QUrl& source, const QUrl& target, bool overwrite) const;