tidy&format(all): all files formatted and relevant warnings resolved
C++ / build (pull_request) Failing after 15s
Lint & Format (JS/TS) / lint-format (pull_request) Successful in 28s
Python / lint-format (pull_request) Successful in 45s
Python / test (pull_request) Successful in 38s
Lint & Format (Rust) / lint-format (pull_request) Successful in 1m45s
C++ / build (pull_request) Failing after 15s
Lint & Format (JS/TS) / lint-format (pull_request) Successful in 28s
Python / lint-format (pull_request) Successful in 45s
Python / test (pull_request) Successful in 38s
Lint & Format (Rust) / lint-format (pull_request) Successful in 1m45s
This commit is contained in:
+125
-137
@@ -20,15 +20,12 @@
|
||||
|
||||
namespace ZShell {
|
||||
|
||||
// ============================================================
|
||||
// saveItem
|
||||
// ============================================================
|
||||
|
||||
void ZShellIo::saveItem(QQuickItem* target, const QUrl& path) {
|
||||
this->saveItem(target, path, QRect(), QJSValue(), QJSValue());
|
||||
}
|
||||
|
||||
void ZShellIo::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect) {
|
||||
void ZShellIo::saveItem(
|
||||
QQuickItem* target, const QUrl& path, const QRect& rect) {
|
||||
this->saveItem(target, path, rect, QJSValue(), QJSValue());
|
||||
}
|
||||
|
||||
@@ -36,11 +33,13 @@ void ZShellIo::saveItem(QQuickItem* target, const QUrl& path, QJSValue onSaved)
|
||||
this->saveItem(target, path, QRect(), onSaved, QJSValue());
|
||||
}
|
||||
|
||||
void ZShellIo::saveItem(QQuickItem* target, const QUrl& path, QJSValue onSaved, QJSValue onFailed) {
|
||||
void ZShellIo::saveItem(
|
||||
QQuickItem* target, const QUrl& path, QJSValue onSaved, QJSValue onFailed) {
|
||||
this->saveItem(target, path, QRect(), onSaved, onFailed);
|
||||
}
|
||||
|
||||
void ZShellIo::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, QJSValue onSaved) {
|
||||
void ZShellIo::saveItem(
|
||||
QQuickItem* target, const QUrl& path, const QRect& rect, QJSValue onSaved) {
|
||||
this->saveItem(target, path, rect, onSaved, QJSValue());
|
||||
}
|
||||
|
||||
@@ -49,8 +48,7 @@ void ZShellIo::saveItem(
|
||||
const QUrl& path,
|
||||
const QRect& rect,
|
||||
QJSValue onSaved,
|
||||
QJSValue onFailed
|
||||
) {
|
||||
QJSValue onFailed) {
|
||||
if (!target) {
|
||||
qWarning() << "ZShellIo::saveItem: a target is required";
|
||||
return;
|
||||
@@ -62,9 +60,8 @@ void ZShellIo::saveItem(
|
||||
}
|
||||
|
||||
if (!target->window()) {
|
||||
qWarning() << "ZShellIo::saveItem: unable to save target"
|
||||
<< target
|
||||
<< "without a window";
|
||||
qWarning() << "ZShellIo::saveItem: unable to save target" << target
|
||||
<< "without a window";
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -74,11 +71,11 @@ void ZShellIo::saveItem(
|
||||
|
||||
if (rect.isValid() && !qFuzzyCompare(scale + 1.0, 2.0)) {
|
||||
scaledRect = QRectF(
|
||||
rect.left() * scale,
|
||||
rect.top() * scale,
|
||||
rect.width() * scale,
|
||||
rect.height() * scale
|
||||
).toRect();
|
||||
rect.left() * scale,
|
||||
rect.top() * scale,
|
||||
rect.width() * scale,
|
||||
rect.height() * scale)
|
||||
.toRect();
|
||||
}
|
||||
|
||||
const QSharedPointer<const QQuickItemGrabResult> grabResult =
|
||||
@@ -89,66 +86,62 @@ void ZShellIo::saveItem(
|
||||
&QQuickItemGrabResult::ready,
|
||||
this,
|
||||
[grabResult, scaledRect, path, onSaved, onFailed, this]() {
|
||||
const auto future = QtConcurrent::run([grabResult, scaledRect, path]() {
|
||||
QImage image = grabResult->image();
|
||||
const auto future =
|
||||
QtConcurrent::run([grabResult, scaledRect, path]() {
|
||||
QImage image = grabResult->image();
|
||||
|
||||
if (scaledRect.isValid()) {
|
||||
image = image.copy(scaledRect);
|
||||
}
|
||||
if (scaledRect.isValid()) {
|
||||
image = image.copy(scaledRect);
|
||||
}
|
||||
|
||||
const QString file = path.toLocalFile();
|
||||
const QString parent = QFileInfo(file).absolutePath();
|
||||
const QString file = path.toLocalFile();
|
||||
const QString parent = QFileInfo(file).absolutePath();
|
||||
|
||||
QDir().mkpath(parent);
|
||||
QDir().mkpath(parent);
|
||||
|
||||
QSaveFile out(file);
|
||||
if (!out.open(QIODevice::WriteOnly)) {
|
||||
return false;
|
||||
}
|
||||
QSaveFile out(file);
|
||||
if (!out.open(QIODevice::WriteOnly)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!image.save(&out, "PNG")) {
|
||||
return false;
|
||||
}
|
||||
if (!image.save(&out, "PNG")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return out.commit();
|
||||
});
|
||||
return out.commit();
|
||||
});
|
||||
|
||||
auto* watcher = new QFutureWatcher<bool>(this);
|
||||
auto* engine = qmlEngine(this);
|
||||
|
||||
QObject::connect(watcher, &QFutureWatcher<bool>::finished, this, [=]() {
|
||||
if (watcher->result()) {
|
||||
if (onSaved.isCallable() && engine) {
|
||||
onSaved.call({
|
||||
engine->toScriptValue(path.toLocalFile()),
|
||||
engine->toScriptValue(path)
|
||||
});
|
||||
QObject::connect(
|
||||
watcher, &QFutureWatcher<bool>::finished, this, [=]() {
|
||||
if (watcher->result()) {
|
||||
if (onSaved.isCallable() && engine) {
|
||||
onSaved.call(
|
||||
{engine->toScriptValue(path.toLocalFile()),
|
||||
engine->toScriptValue(path)});
|
||||
}
|
||||
} else {
|
||||
qWarning()
|
||||
<< "ZShellIo::saveItem: failed to save" << path;
|
||||
if (onFailed.isCallable() && engine) {
|
||||
onFailed.call({engine->toScriptValue(path)});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
qWarning() << "ZShellIo::saveItem: failed to save" << path;
|
||||
if (onFailed.isCallable() && engine) {
|
||||
onFailed.call({
|
||||
engine->toScriptValue(path)
|
||||
});
|
||||
}
|
||||
}
|
||||
watcher->deleteLater();
|
||||
});
|
||||
watcher->deleteLater();
|
||||
});
|
||||
|
||||
watcher->setFuture(future);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// cacheImage
|
||||
// ============================================================
|
||||
|
||||
void ZShellIo::cacheImage(const QUrl& source, const QString& cacheDir) {
|
||||
this->cacheImage(source, cacheDir, QJSValue(), QJSValue());
|
||||
}
|
||||
|
||||
void ZShellIo::cacheImage(const QUrl& source, const QString& cacheDir, QJSValue onSaved) {
|
||||
void ZShellIo::cacheImage(
|
||||
const QUrl& source, const QString& cacheDir, QJSValue onSaved) {
|
||||
this->cacheImage(source, cacheDir, onSaved, QJSValue());
|
||||
}
|
||||
|
||||
@@ -156,8 +149,7 @@ void ZShellIo::cacheImage(
|
||||
const QUrl& source,
|
||||
const QString& cacheDir,
|
||||
QJSValue onSaved,
|
||||
QJSValue onFailed
|
||||
) {
|
||||
QJSValue onFailed) {
|
||||
if (cacheDir.isEmpty()) {
|
||||
qWarning() << "ZShellIo::cacheImage: cacheDir is empty";
|
||||
return;
|
||||
@@ -165,92 +157,87 @@ void ZShellIo::cacheImage(
|
||||
|
||||
QImage image;
|
||||
if (!loadSourceImage(source, image)) {
|
||||
qWarning() << "ZShellIo::cacheImage: failed to load source image" << source;
|
||||
qWarning() << "ZShellIo::cacheImage: failed to load source image"
|
||||
<< source;
|
||||
auto* engine = qmlEngine(this);
|
||||
if (onFailed.isCallable() && engine) {
|
||||
onFailed.call({
|
||||
engine->toScriptValue(source),
|
||||
engine->toScriptValue(cacheDir)
|
||||
});
|
||||
onFailed.call(
|
||||
{engine->toScriptValue(source),
|
||||
engine->toScriptValue(cacheDir)});
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const auto future = QtConcurrent::run([image, cacheDir]() -> QString {
|
||||
if (image.isNull()) {
|
||||
return QString();
|
||||
}
|
||||
if (image.isNull()) {
|
||||
return QString();
|
||||
}
|
||||
|
||||
const QImage normalized = image.convertToFormat(QImage::Format_RGBA8888);
|
||||
const QImage normalized =
|
||||
image.convertToFormat(QImage::Format_RGBA8888);
|
||||
|
||||
const QByteArray bytes(
|
||||
reinterpret_cast<const char*>(normalized.constBits()),
|
||||
qsizetype(normalized.sizeInBytes())
|
||||
);
|
||||
const QByteArray bytes(
|
||||
reinterpret_cast<const char*>(normalized.constBits()),
|
||||
normalized.sizeInBytes());
|
||||
|
||||
const QByteArray digest =
|
||||
QCryptographicHash::hash(bytes, QCryptographicHash::Sha256).toHex();
|
||||
const QByteArray digest =
|
||||
QCryptographicHash::hash(bytes, QCryptographicHash::Sha256).toHex();
|
||||
|
||||
QDir dir(cacheDir);
|
||||
if (!dir.exists() && !QDir().mkpath(cacheDir)) {
|
||||
return QString();
|
||||
}
|
||||
QDir dir(cacheDir);
|
||||
if (!dir.exists() && !QDir().mkpath(cacheDir)) {
|
||||
return QString();
|
||||
}
|
||||
|
||||
const QString finalPath = dir.filePath(QString::fromLatin1(digest) + ".png");
|
||||
|
||||
if (QFile::exists(finalPath)) {
|
||||
return finalPath;
|
||||
}
|
||||
|
||||
QSaveFile out(finalPath);
|
||||
if (!out.open(QIODevice::WriteOnly)) {
|
||||
return QString();
|
||||
}
|
||||
|
||||
if (!normalized.save(&out, "PNG")) {
|
||||
return QString();
|
||||
}
|
||||
|
||||
if (!out.commit()) {
|
||||
return QString();
|
||||
}
|
||||
const QString finalPath =
|
||||
dir.filePath(QString::fromLatin1(digest) + ".png");
|
||||
|
||||
if (QFile::exists(finalPath)) {
|
||||
return finalPath;
|
||||
});
|
||||
}
|
||||
|
||||
QSaveFile out(finalPath);
|
||||
if (!out.open(QIODevice::WriteOnly)) {
|
||||
return QString();
|
||||
}
|
||||
|
||||
if (!normalized.save(&out, "PNG")) {
|
||||
return QString();
|
||||
}
|
||||
|
||||
if (!out.commit()) {
|
||||
return QString();
|
||||
}
|
||||
|
||||
return finalPath;
|
||||
});
|
||||
|
||||
auto* watcher = new QFutureWatcher<QString>(this);
|
||||
auto* engine = qmlEngine(this);
|
||||
|
||||
QObject::connect(watcher, &QFutureWatcher<QString>::finished, this, [=]() {
|
||||
const QString finalPath = watcher->result();
|
||||
const QString finalPath = watcher->result();
|
||||
|
||||
if (!finalPath.isEmpty()) {
|
||||
if (onSaved.isCallable() && engine) {
|
||||
onSaved.call({
|
||||
engine->toScriptValue(finalPath),
|
||||
engine->toScriptValue(QUrl::fromLocalFile(finalPath))
|
||||
});
|
||||
}
|
||||
} else {
|
||||
qWarning() << "ZShellIo::cacheImage: failed to cache" << source;
|
||||
if (onFailed.isCallable() && engine) {
|
||||
onFailed.call({
|
||||
engine->toScriptValue(source),
|
||||
engine->toScriptValue(cacheDir)
|
||||
});
|
||||
}
|
||||
if (!finalPath.isEmpty()) {
|
||||
if (onSaved.isCallable() && engine) {
|
||||
onSaved.call(
|
||||
{engine->toScriptValue(finalPath),
|
||||
engine->toScriptValue(QUrl::fromLocalFile(finalPath))});
|
||||
}
|
||||
} else {
|
||||
qWarning() << "ZShellIo::cacheImage: failed to cache" << source;
|
||||
if (onFailed.isCallable() && engine) {
|
||||
onFailed.call(
|
||||
{engine->toScriptValue(source),
|
||||
engine->toScriptValue(cacheDir)});
|
||||
}
|
||||
}
|
||||
|
||||
watcher->deleteLater();
|
||||
});
|
||||
watcher->deleteLater();
|
||||
});
|
||||
|
||||
watcher->setFuture(future);
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// loadSourceImage
|
||||
// ============================================================
|
||||
|
||||
bool ZShellIo::loadSourceImage(const QUrl& source, QImage& image) const {
|
||||
image = QImage();
|
||||
|
||||
@@ -270,22 +257,22 @@ bool ZShellIo::loadSourceImage(const QUrl& source, QImage& image) const {
|
||||
|
||||
const QString providerId = source.host();
|
||||
|
||||
const QString imageId =
|
||||
source.path().startsWith('/')
|
||||
? source.path().mid(1)
|
||||
: source.path();
|
||||
const QString imageId = source.path().startsWith('/')
|
||||
? source.path().mid(1)
|
||||
: source.path();
|
||||
|
||||
auto* providerBase = engine->imageProvider(providerId);
|
||||
if (!providerBase) {
|
||||
qWarning() << "ZShellIo::loadSourceImage: provider not found"
|
||||
<< providerId;
|
||||
<< providerId;
|
||||
return false;
|
||||
}
|
||||
|
||||
auto* provider = dynamic_cast<QQuickImageProvider*>(providerBase);
|
||||
if (!provider) {
|
||||
qWarning() << "ZShellIo::loadSourceImage: provider is not a QQuickImageProvider"
|
||||
<< providerId;
|
||||
qWarning() << "ZShellIo::loadSourceImage: provider is not a "
|
||||
"QQuickImageProvider"
|
||||
<< providerId;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -302,7 +289,7 @@ bool ZShellIo::loadSourceImage(const QUrl& source, QImage& image) const {
|
||||
|
||||
default:
|
||||
qWarning() << "ZShellIo::loadSourceImage: unsupported provider type"
|
||||
<< providerId;
|
||||
<< providerId;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -313,17 +300,16 @@ bool ZShellIo::loadSourceImage(const QUrl& source, QImage& image) const {
|
||||
return false;
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// File ops
|
||||
// ============================================================
|
||||
|
||||
bool ZShellIo::copyFile(const QUrl& source, const QUrl& target, bool overwrite) const {
|
||||
bool ZShellIo::copyFile(
|
||||
const QUrl& source, const QUrl& target, bool overwrite) const {
|
||||
if (!source.isLocalFile()) {
|
||||
qWarning() << "ZShellIo::copyFile: source" << source << "is not a local file";
|
||||
qWarning() << "ZShellIo::copyFile: source" << source
|
||||
<< "is not a local file";
|
||||
return false;
|
||||
}
|
||||
if (!target.isLocalFile()) {
|
||||
qWarning() << "ZShellIo::copyFile: target" << target << "is not a local file";
|
||||
qWarning() << "ZShellIo::copyFile: target" << target
|
||||
<< "is not a local file";
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -336,7 +322,8 @@ bool ZShellIo::copyFile(const QUrl& source, const QUrl& target, bool overwrite)
|
||||
|
||||
bool ZShellIo::deleteFile(const QUrl& path) const {
|
||||
if (!path.isLocalFile()) {
|
||||
qWarning() << "ZShellIo::deleteFile: path" << path << "is not a local file";
|
||||
qWarning() << "ZShellIo::deleteFile: path" << path
|
||||
<< "is not a local file";
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -345,7 +332,8 @@ bool ZShellIo::deleteFile(const QUrl& path) const {
|
||||
|
||||
QString ZShellIo::toLocalFile(const QUrl& url) const {
|
||||
if (!url.isLocalFile()) {
|
||||
qWarning() << "ZShellIo::toLocalFile: given url is not a local file" << url;
|
||||
qWarning() << "ZShellIo::toLocalFile: given url is not a local file"
|
||||
<< url;
|
||||
return QString();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user