plugin: make thread safe
This commit is contained in:
parent
f8fa70fe0e
commit
573423742a
3 changed files with 46 additions and 46 deletions
|
|
@ -70,8 +70,11 @@ void CachingImageManager::setPath(const QString& path) {
|
||||||
|
|
||||||
if (!path.isEmpty()) {
|
if (!path.isEmpty()) {
|
||||||
QThreadPool::globalInstance()->start([path, this] {
|
QThreadPool::globalInstance()->start([path, this] {
|
||||||
|
const QString sha = sha256sum(path);
|
||||||
|
|
||||||
|
QMetaObject::invokeMethod(this, [path, sha, this]() {
|
||||||
const QString filename = QString("%1@%2x%3.png")
|
const QString filename = QString("%1@%2x%3.png")
|
||||||
.arg(sha256sum(path))
|
.arg(sha)
|
||||||
.arg(effectiveWidth())
|
.arg(effectiveWidth())
|
||||||
.arg(effectiveHeight());
|
.arg(effectiveHeight());
|
||||||
|
|
||||||
|
|
@ -83,21 +86,17 @@ void CachingImageManager::setPath(const QString& path) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (QFile::exists(m_cachePath.toLocalFile())) {
|
bool cacheExists = QFile::exists(m_cachePath.toLocalFile());
|
||||||
QMetaObject::invokeMethod(m_item, [this]() {
|
|
||||||
|
if (cacheExists) {
|
||||||
m_item->setProperty("source", m_cachePath);
|
m_item->setProperty("source", m_cachePath);
|
||||||
}, Qt::QueuedConnection);
|
|
||||||
|
|
||||||
m_usingCache = true;
|
|
||||||
emit usingCacheChanged();
|
|
||||||
} else {
|
} else {
|
||||||
QMetaObject::invokeMethod(m_item, [path, this]() {
|
|
||||||
m_item->setProperty("source", QUrl::fromLocalFile(path));
|
m_item->setProperty("source", QUrl::fromLocalFile(path));
|
||||||
}, Qt::QueuedConnection);
|
|
||||||
|
|
||||||
m_usingCache = false;
|
|
||||||
emit usingCacheChanged();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m_usingCache = cacheExists;
|
||||||
|
emit usingCacheChanged();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,27 +7,27 @@
|
||||||
#include <QQmlEngine>
|
#include <QQmlEngine>
|
||||||
#include <QDir>
|
#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());
|
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());
|
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());
|
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);
|
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());
|
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) {
|
if (!target) {
|
||||||
qWarning() << "CUtils::saveItem: a target is required";
|
qWarning() << "CUtils::saveItem: a target is required";
|
||||||
return;
|
return;
|
||||||
|
|
@ -40,10 +40,7 @@ void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, Q
|
||||||
|
|
||||||
QSharedPointer<QQuickItemGrabResult> grabResult = target->grabToImage();
|
QSharedPointer<QQuickItemGrabResult> grabResult = target->grabToImage();
|
||||||
|
|
||||||
QObject::connect(
|
QObject::connect(grabResult.data(), &QQuickItemGrabResult::ready, this,
|
||||||
grabResult.data(),
|
|
||||||
&QQuickItemGrabResult::ready,
|
|
||||||
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();
|
||||||
|
|
@ -54,7 +51,10 @@ void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, Q
|
||||||
|
|
||||||
const QString file = path.toLocalFile();
|
const QString file = path.toLocalFile();
|
||||||
const QString parent = QFileInfo(file).absolutePath();
|
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()) {
|
if (onSaved.isCallable()) {
|
||||||
onSaved.call({ QJSValue(file), qmlEngine(this)->toScriptValue(QVariant::fromValue(path)) });
|
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)) });
|
onFailed.call({ qmlEngine(this)->toScriptValue(QVariant::fromValue(path)) });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}, Qt::QueuedConnection);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -10,12 +10,12 @@ class CUtils : public QObject {
|
||||||
QML_SINGLETON;
|
QML_SINGLETON;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path) const;
|
Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path);
|
||||||
Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, const QRect& rect) const;
|
Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, const QRect& rect);
|
||||||
Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, QJSValue onSaved) const;
|
Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, QJSValue onSaved);
|
||||||
Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, QJSValue onSaved, QJSValue onFailed) const;
|
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) 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, QJSValue onFailed) const;
|
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) const;
|
||||||
Q_INVOKABLE bool copyFile(const QUrl& source, const QUrl& target, bool overwrite) const;
|
Q_INVOKABLE bool copyFile(const QUrl& source, const QUrl& target, bool overwrite) const;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue