1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
#include "cutils.hpp"
#include <qobject.h>
#include <QtQuick/QQuickWindow>
#include <QtQuick/QQuickItem>
#include <QtQuick/QQuickItemGrabResult>
#include <QThreadPool>
#include <QQmlEngine>
#include <QDir>
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) {
this->saveItem(target, path, rect, QJSValue(), QJSValue());
}
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) {
this->saveItem(target, path, QRect(), onSaved, onFailed);
}
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) {
if (!target) {
qWarning() << "CUtils::saveItem: a target is required";
return;
}
if (!path.isLocalFile()) {
qWarning() << "CUtils::saveItem:" << path << "is not a local file";
return;
}
auto scaledRect = rect;
if (rect.isValid()) {
qreal scale = target->window()->devicePixelRatio();
scaledRect = QRect(rect.left() * scale, rect.top() * scale, rect.width() * scale, rect.height() * scale);
}
QSharedPointer<QQuickItemGrabResult> grabResult = target->grabToImage();
QObject::connect(grabResult.data(), &QQuickItemGrabResult::ready, this,
[grabResult, scaledRect, path, onSaved, onFailed, this]() {
QThreadPool::globalInstance()->start([grabResult, scaledRect, path, onSaved, onFailed, this] {
QImage image = grabResult->image();
if (scaledRect.isValid()) {
image = image.copy(scaledRect);
}
const QString file = path.toLocalFile();
const QString parent = QFileInfo(file).absolutePath();
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)) });
}
} else {
qWarning() << "CUtils::saveItem: failed to save" << path;
if (onFailed.isCallable()) {
onFailed.call({ qmlEngine(this)->toScriptValue(QVariant::fromValue(path)) });
}
}
}, Qt::QueuedConnection);
});
}
);
}
bool CUtils::copyFile(const QUrl& source, const QUrl& target) const {
return this->copyFile(source, target, true);
}
bool CUtils::copyFile(const QUrl& source, const QUrl& target, bool overwrite) const {
if (!source.isLocalFile()) {
qWarning() << "CUtils::copyFile: source" << source << "is not a local file";
return false;
}
if (!target.isLocalFile()) {
qWarning() << "CUtils::copyFile: target" << target << "is not a local file";
return false;
}
if (overwrite) {
QFile::remove(target.toLocalFile());
}
return QFile::copy(source.toLocalFile(), target.toLocalFile());
}
void CUtils::getDominantColour(QQuickItem* item, QJSValue callback) const {
this->getDominantColour(item, 128, 128, callback);
}
void CUtils::getDominantColour(QQuickItem* item, int targetWidth, int targetHeight, QJSValue callback) const {
if (!item) {
qWarning() << "CUtils::getDominantColour: an item is required";
return;
}
if (!item->window()) {
// Fail silently to avoid warning
return;
}
QSharedPointer<QQuickItemGrabResult> grabResult = item->grabToImage();
QObject::connect(grabResult.data(), &QQuickItemGrabResult::ready, this,
[grabResult, item, targetWidth, targetHeight, callback, this]() {
QImage image = grabResult->image();
if (image.width() > targetWidth && image.height() > targetHeight) {
image = image.scaled(targetWidth, targetHeight);
} else if (image.width() > targetWidth) {
image = image.scaledToWidth(targetWidth);
} else if (image.height() > targetHeight) {
image = image.scaledToHeight(targetHeight);
}
if (image.format() != QImage::Format_ARGB32) {
image = image.convertToFormat(QImage::Format_ARGB32);
}
std::unordered_map<uint32_t, int> colours;
const uchar* data = image.bits();
int width = image.width();
int height = image.height();
int bytesPerLine = image.bytesPerLine();
for (int y = 0; y < height; ++y) {
const uchar* line = data + y * bytesPerLine;
for (int x = 0; x < width; ++x) {
const uchar* pixel = line + x * 4;
uchar r = pixel[0];
uchar g = pixel[1];
uchar b = pixel[2];
uchar a = pixel[3];
if (a == 0) {
continue;
}
r &= 0xF8;
g &= 0xF8;
b &= 0xF8;
uint32_t colour = (r << 16) | (g << 8) | b;
++colours[colour];
}
}
uint32_t dominantColour = 0;
int maxCount = 0;
for (const auto& [colour, count] : colours) {
if (count > maxCount) {
dominantColour = colour;
maxCount = count;
}
}
const QColor colour = QColor((0xFF << 24) | dominantColour);
if (callback.isCallable()) {
QMetaObject::invokeMethod(item, [item, callback, colour, this]() {
callback.call({ qmlEngine(this)->toScriptValue(QVariant::fromValue(colour)) });
}, Qt::QueuedConnection);
}
}
);
}
|