plugin/cutils: qthreadpool -> qtconcurrent
This commit is contained in:
parent
ca1d9f2134
commit
bc56e61ac0
1 changed files with 62 additions and 64 deletions
|
|
@ -1,11 +1,12 @@
|
||||||
#include "cutils.hpp"
|
#include "cutils.hpp"
|
||||||
|
|
||||||
|
#include <QtConcurrent/qtconcurrentrun.h>
|
||||||
#include <QtQuick/qquickitemgrabresult.h>
|
#include <QtQuick/qquickitemgrabresult.h>
|
||||||
#include <QtQuick/qquickwindow.h>
|
#include <QtQuick/qquickwindow.h>
|
||||||
#include <qdir.h>
|
#include <qdir.h>
|
||||||
#include <qfileinfo.h>
|
#include <qfileinfo.h>
|
||||||
|
#include <qfuturewatcher.h>
|
||||||
#include <qqmlengine.h>
|
#include <qqmlengine.h>
|
||||||
#include <qthreadpool.h>
|
|
||||||
|
|
||||||
namespace caelestia {
|
namespace caelestia {
|
||||||
|
|
||||||
|
|
@ -56,7 +57,7 @@ void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, Q
|
||||||
|
|
||||||
QObject::connect(grabResult.data(), &QQuickItemGrabResult::ready, this,
|
QObject::connect(grabResult.data(), &QQuickItemGrabResult::ready, this,
|
||||||
[grabResult, scaledRect, path, onSaved, onFailed, this]() {
|
[grabResult, scaledRect, path, onSaved, onFailed, this]() {
|
||||||
QThreadPool::globalInstance()->start([grabResult, scaledRect, path, onSaved, onFailed, this] {
|
const auto future = QtConcurrent::run([=]() {
|
||||||
QImage image = grabResult->image();
|
QImage image = grabResult->image();
|
||||||
|
|
||||||
if (scaledRect.isValid()) {
|
if (scaledRect.isValid()) {
|
||||||
|
|
@ -65,25 +66,27 @@ 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();
|
||||||
const bool success = QDir().mkpath(parent) && image.save(file);
|
return QDir().mkpath(parent) && image.save(file);
|
||||||
|
});
|
||||||
|
|
||||||
QMetaObject::invokeMethod(
|
auto* watcher = new QFutureWatcher<bool>(this);
|
||||||
this,
|
auto* engine = qmlEngine(this);
|
||||||
[file, success, path, onSaved, onFailed, this]() {
|
|
||||||
if (success) {
|
QObject::connect(watcher, &QFutureWatcher<bool>::finished, this, [=]() {
|
||||||
|
if (watcher->result()) {
|
||||||
if (onSaved.isCallable()) {
|
if (onSaved.isCallable()) {
|
||||||
onSaved.call(
|
onSaved.call(
|
||||||
{ QJSValue(file), qmlEngine(this)->toScriptValue(QVariant::fromValue(path)) });
|
{ QJSValue(path.toLocalFile()), engine->toScriptValue(QVariant::fromValue(path)) });
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
qWarning() << "CUtils::saveItem: failed to save" << path;
|
qWarning() << "CUtils::saveItem: failed to save" << path;
|
||||||
if (onFailed.isCallable()) {
|
if (onFailed.isCallable()) {
|
||||||
onFailed.call({ qmlEngine(this)->toScriptValue(QVariant::fromValue(path)) });
|
onFailed.call({ engine->toScriptValue(QVariant::fromValue(path)) });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
watcher->deleteLater();
|
||||||
Qt::QueuedConnection);
|
|
||||||
});
|
});
|
||||||
|
watcher->setFuture(future);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -132,20 +135,17 @@ void CUtils::getDominantColour(QQuickItem* item, int rescaleSize, QJSValue callb
|
||||||
|
|
||||||
QObject::connect(
|
QObject::connect(
|
||||||
grabResult.data(), &QQuickItemGrabResult::ready, this, [grabResult, rescaleSize, callback, this]() {
|
grabResult.data(), &QQuickItemGrabResult::ready, this, [grabResult, rescaleSize, callback, this]() {
|
||||||
const QImage image = grabResult->image();
|
const auto future = QtConcurrent::run(&CUtils::findDominantColour, this, grabResult->image(), rescaleSize);
|
||||||
|
auto* watcher = new QFutureWatcher<QColor>(this);
|
||||||
QThreadPool::globalInstance()->start([grabResult, image, rescaleSize, callback, this]() {
|
auto* engine = qmlEngine(this);
|
||||||
const QColor color = this->findDominantColour(image, rescaleSize);
|
|
||||||
|
|
||||||
|
QObject::connect(watcher, &QFutureWatcher<QColor>::finished, this, [=]() {
|
||||||
if (callback.isCallable()) {
|
if (callback.isCallable()) {
|
||||||
QMetaObject::invokeMethod(
|
callback.call({ engine->toScriptValue(QVariant::fromValue(watcher->result())) });
|
||||||
this,
|
|
||||||
[color, callback, this]() {
|
|
||||||
callback.call({ qmlEngine(this)->toScriptValue(QVariant::fromValue(color)) });
|
|
||||||
},
|
|
||||||
Qt::QueuedConnection);
|
|
||||||
}
|
}
|
||||||
|
watcher->deleteLater();
|
||||||
});
|
});
|
||||||
|
watcher->setFuture(future);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -159,25 +159,26 @@ void CUtils::getDominantColour(const QString& path, int rescaleSize, QJSValue ca
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
QThreadPool::globalInstance()->start([path, rescaleSize, callback, this]() {
|
const auto future = QtConcurrent::run([=, this]() {
|
||||||
const QImage image(path);
|
const QImage image(path);
|
||||||
|
|
||||||
if (image.isNull()) {
|
if (image.isNull()) {
|
||||||
qWarning() << "CUtils::getDominantColour: failed to load image" << path;
|
qWarning() << "CUtils::getDominantColour: failed to load image" << path;
|
||||||
return;
|
return QColor();
|
||||||
}
|
}
|
||||||
|
|
||||||
const QColor color = this->findDominantColour(image, rescaleSize);
|
return findDominantColour(image, rescaleSize);
|
||||||
|
|
||||||
if (callback.isCallable()) {
|
|
||||||
QMetaObject::invokeMethod(
|
|
||||||
this,
|
|
||||||
[color, callback, this]() {
|
|
||||||
callback.call({ qmlEngine(this)->toScriptValue(QVariant::fromValue(color)) });
|
|
||||||
},
|
|
||||||
Qt::QueuedConnection);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
auto* watcher = new QFutureWatcher<QColor>(this);
|
||||||
|
auto* engine = qmlEngine(this);
|
||||||
|
|
||||||
|
QObject::connect(watcher, &QFutureWatcher<QColor>::finished, this, [=]() {
|
||||||
|
if (watcher->result().isValid() && callback.isCallable()) {
|
||||||
|
callback.call({ engine->toScriptValue(QVariant::fromValue(watcher->result())) });
|
||||||
|
}
|
||||||
|
watcher->deleteLater();
|
||||||
|
});
|
||||||
|
watcher->setFuture(future);
|
||||||
}
|
}
|
||||||
|
|
||||||
QColor CUtils::findDominantColour(const QImage& image, int rescaleSize) const {
|
QColor CUtils::findDominantColour(const QImage& image, int rescaleSize) const {
|
||||||
|
|
@ -251,20 +252,17 @@ void CUtils::getAverageLuminance(QQuickItem* item, int rescaleSize, QJSValue cal
|
||||||
|
|
||||||
QObject::connect(
|
QObject::connect(
|
||||||
grabResult.data(), &QQuickItemGrabResult::ready, this, [grabResult, rescaleSize, callback, this]() {
|
grabResult.data(), &QQuickItemGrabResult::ready, this, [grabResult, rescaleSize, callback, this]() {
|
||||||
const QImage image = grabResult->image();
|
const auto future =
|
||||||
|
QtConcurrent::run(&CUtils::findAverageLuminance, this, grabResult->image(), rescaleSize);
|
||||||
QThreadPool::globalInstance()->start([grabResult, image, rescaleSize, callback, this]() {
|
auto* watcher = new QFutureWatcher<qreal>(this);
|
||||||
const qreal luminance = this->findAverageLuminance(image, rescaleSize);
|
|
||||||
|
|
||||||
|
QObject::connect(watcher, &QFutureWatcher<qreal>::finished, this, [=]() {
|
||||||
if (callback.isCallable()) {
|
if (callback.isCallable()) {
|
||||||
QMetaObject::invokeMethod(
|
callback.call({ QJSValue(watcher->result()) });
|
||||||
this,
|
|
||||||
[luminance, callback]() {
|
|
||||||
callback.call({ QJSValue(luminance) });
|
|
||||||
},
|
|
||||||
Qt::QueuedConnection);
|
|
||||||
}
|
}
|
||||||
|
watcher->deleteLater();
|
||||||
});
|
});
|
||||||
|
watcher->setFuture(future);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -278,25 +276,25 @@ void CUtils::getAverageLuminance(const QString& path, int rescaleSize, QJSValue
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
QThreadPool::globalInstance()->start([path, rescaleSize, callback, this]() {
|
const auto future = QtConcurrent::run([=, this]() {
|
||||||
const QImage image(path);
|
const QImage image(path);
|
||||||
|
|
||||||
if (image.isNull()) {
|
if (image.isNull()) {
|
||||||
qWarning() << "CUtils::getAverageLuminance: failed to load image" << path;
|
qWarning() << "CUtils::getAverageLuminance: failed to load image" << path;
|
||||||
return;
|
return 0.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const qreal luminance = this->findAverageLuminance(image, rescaleSize);
|
return findAverageLuminance(image, rescaleSize);
|
||||||
|
|
||||||
if (callback.isCallable()) {
|
|
||||||
QMetaObject::invokeMethod(
|
|
||||||
this,
|
|
||||||
[luminance, callback]() {
|
|
||||||
callback.call({ QJSValue(luminance) });
|
|
||||||
},
|
|
||||||
Qt::QueuedConnection);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
auto* watcher = new QFutureWatcher<qreal>(this);
|
||||||
|
|
||||||
|
QObject::connect(watcher, &QFutureWatcher<qreal>::finished, this, [=]() {
|
||||||
|
if (callback.isCallable()) {
|
||||||
|
callback.call({ QJSValue(watcher->result()) });
|
||||||
|
}
|
||||||
|
watcher->deleteLater();
|
||||||
|
});
|
||||||
|
watcher->setFuture(future);
|
||||||
}
|
}
|
||||||
|
|
||||||
qreal CUtils::findAverageLuminance(const QImage& image, int rescaleSize) const {
|
qreal CUtils::findAverageLuminance(const QImage& image, int rescaleSize) const {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue