plugin: better getDominantColour
Use callback instead of setting prop
This commit is contained in:
parent
51bcacded4
commit
bec98e1fc5
3 changed files with 14 additions and 13 deletions
|
|
@ -19,12 +19,12 @@ IconImage {
|
||||||
}
|
}
|
||||||
|
|
||||||
layer.onEnabledChanged: {
|
layer.onEnabledChanged: {
|
||||||
if (layer.enabled)
|
if (layer.enabled && status === Image.Ready)
|
||||||
CUtils.getDominantColour(this);
|
CUtils.getDominantColour(this, c => dominantColour = c);
|
||||||
}
|
}
|
||||||
|
|
||||||
onStatusChanged: {
|
onStatusChanged: {
|
||||||
if (layer.enabled && status === Image.Ready)
|
if (layer.enabled && status === Image.Ready)
|
||||||
CUtils.getDominantColour(this);
|
CUtils.getDominantColour(this, c => dominantColour = c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -91,11 +91,11 @@ bool CUtils::copyFile(const QUrl& source, const QUrl& target, bool overwrite) co
|
||||||
return QFile::copy(source.toLocalFile(), target.toLocalFile());
|
return QFile::copy(source.toLocalFile(), target.toLocalFile());
|
||||||
}
|
}
|
||||||
|
|
||||||
void CUtils::getDominantColour(QQuickItem* item) const {
|
void CUtils::getDominantColour(QQuickItem* item, QJSValue callback) const {
|
||||||
this->getDominantColour(item, 128, 128);
|
this->getDominantColour(item, 128, 128, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CUtils::getDominantColour(QQuickItem* item, int width, int height) const {
|
void CUtils::getDominantColour(QQuickItem* item, int width, int height, QJSValue callback) const {
|
||||||
if (!item) {
|
if (!item) {
|
||||||
qWarning() << "CUtils::getDominantColour: an item is required";
|
qWarning() << "CUtils::getDominantColour: an item is required";
|
||||||
return;
|
return;
|
||||||
|
|
@ -109,7 +109,7 @@ void CUtils::getDominantColour(QQuickItem* item, int width, int height) const {
|
||||||
QSharedPointer<QQuickItemGrabResult> grabResult = item->grabToImage(QSize(width, height));
|
QSharedPointer<QQuickItemGrabResult> grabResult = item->grabToImage(QSize(width, height));
|
||||||
|
|
||||||
QObject::connect(grabResult.data(), &QQuickItemGrabResult::ready, this,
|
QObject::connect(grabResult.data(), &QQuickItemGrabResult::ready, this,
|
||||||
[grabResult, item, this]() {
|
[grabResult, item, callback, this]() {
|
||||||
QImage image = grabResult->image();
|
QImage image = grabResult->image();
|
||||||
|
|
||||||
if (image.format() != QImage::Format_ARGB32) {
|
if (image.format() != QImage::Format_ARGB32) {
|
||||||
|
|
@ -154,10 +154,11 @@ void CUtils::getDominantColour(QQuickItem* item, int width, int height) const {
|
||||||
}
|
}
|
||||||
|
|
||||||
const QColor colour = QColor((0xFF << 24) | dominantColour);
|
const QColor colour = QColor((0xFF << 24) | dominantColour);
|
||||||
|
if (callback.isCallable()) {
|
||||||
QMetaObject::invokeMethod(item, [item, colour]() {
|
QMetaObject::invokeMethod(item, [item, callback, colour, this]() {
|
||||||
item->setProperty("dominantColour", colour);
|
callback.call({ qmlEngine(this)->toScriptValue(QVariant::fromValue(colour)) });
|
||||||
}, Qt::QueuedConnection);
|
}, Qt::QueuedConnection);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,6 @@ public:
|
||||||
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;
|
||||||
|
|
||||||
Q_INVOKABLE void getDominantColour(QQuickItem* item) const;
|
Q_INVOKABLE void getDominantColour(QQuickItem* item, QJSValue callback) const;
|
||||||
Q_INVOKABLE void getDominantColour(QQuickItem* item, int width, int height) const;
|
Q_INVOKABLE void getDominantColour(QQuickItem* item, int width, int height, QJSValue callback) const;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue