fix: re-add lost changes
This commit is contained in:
+151
-6
@@ -5,13 +5,17 @@
|
||||
#include <QtQuick/qquickwindow.h>
|
||||
#include <qcontainerfwd.h>
|
||||
#include <qdir.h>
|
||||
#include <qdiriterator.h>
|
||||
#include <qfileinfo.h>
|
||||
#include <qfuturewatcher.h>
|
||||
#include <qjsprimitivevalue.h>
|
||||
#include <qloggingcategory.h>
|
||||
#include <qregularexpression.h>
|
||||
#include <qqmlengine.h>
|
||||
#include <qfile.h>
|
||||
|
||||
#include "util/metaenum.hpp"
|
||||
|
||||
Q_LOGGING_CATEGORY(lcZUtils, "ZShell.cutils", QtInfoMsg)
|
||||
|
||||
namespace ZShell {
|
||||
@@ -171,13 +175,112 @@ qreal ZUtils::clamp(qreal value, qreal min, qreal max) {
|
||||
return qBound(min, value, max);
|
||||
}
|
||||
|
||||
QString ZUtils::settingsIndex() {
|
||||
QFile file(QStringLiteral(":/qt/qml/ZShell/settings-index.json"));
|
||||
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||
qCWarning(lcZUtils) << "Failed to open embedded settings index";
|
||||
return QString();
|
||||
QString ZUtils::enumToString(
|
||||
QObject* target, const QString& property, const QVariant& value) {
|
||||
if (!target) {
|
||||
qCWarning(lcZUtils) << "enumToString: a target is required";
|
||||
return {};
|
||||
}
|
||||
return QString::fromUtf8(file.readAll());
|
||||
|
||||
const auto* meta = target->metaObject();
|
||||
const auto index = meta->indexOfProperty(property.toUtf8().constData());
|
||||
if (index < 0) {
|
||||
qCWarning(lcZUtils)
|
||||
<< "enumToString:" << target << "has no property" << property;
|
||||
return {};
|
||||
}
|
||||
|
||||
const auto prop = meta->property(index);
|
||||
const auto metaEnum = prop.isEnumType()
|
||||
? prop.enumerator()
|
||||
: util::metaEnumFor(prop.metaType());
|
||||
if (!metaEnum.isValid() || metaEnum.is64Bit()) {
|
||||
qCWarning(lcZUtils) << "enumToString: property" << property << "of"
|
||||
<< target << "is not a supported enum";
|
||||
return {};
|
||||
}
|
||||
|
||||
const auto val = value.isValid() ? value : prop.read(target);
|
||||
const auto* key = util::enumKeyFor(metaEnum, val);
|
||||
if (!key) {
|
||||
qCWarning(
|
||||
lcZUtils,
|
||||
"enumToString: no enumerator of %s::%s has the value %lld",
|
||||
metaEnum.scope(),
|
||||
metaEnum.name(),
|
||||
val.toLongLong());
|
||||
return {};
|
||||
}
|
||||
|
||||
return QString::fromUtf8(key);
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
template <typename Predicate>
|
||||
QQuickItem* findChildDfs(QQuickItem* root, Predicate&& match) {
|
||||
const auto children = root->childItems();
|
||||
for (QQuickItem* const child : children) {
|
||||
if (match(child)) {
|
||||
return child;
|
||||
}
|
||||
if (QQuickItem* const found = findChildDfs(child, match)) {
|
||||
return found;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
template <typename Predicate>
|
||||
void findChildrenDfs(
|
||||
QQuickItem* root, Predicate&& match, QList<QQuickItem*>& out) {
|
||||
const auto children = root->childItems();
|
||||
for (QQuickItem* const child : children) {
|
||||
if (match(child)) {
|
||||
out.append(child);
|
||||
}
|
||||
findChildrenDfs(child, match, out);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
QQuickItem* ZUtils::findChild(QQuickItem* root, const QString& name) {
|
||||
if (!root) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return findChildDfs(root, [&name](const QQuickItem* item) {
|
||||
return item->objectName() == name;
|
||||
});
|
||||
}
|
||||
|
||||
QList<QQuickItem*> ZUtils::findChildren(QQuickItem* root, const QString& name) {
|
||||
QList<QQuickItem*> children;
|
||||
if (root) {
|
||||
findChildrenDfs(
|
||||
root,
|
||||
[&name](const QQuickItem* item) {
|
||||
return item->objectName() == name;
|
||||
},
|
||||
children);
|
||||
}
|
||||
return children;
|
||||
}
|
||||
|
||||
QList<QQuickItem*> ZUtils::findChildrenMatching(
|
||||
QQuickItem* root, const QString& pattern) {
|
||||
QList<QQuickItem*> children;
|
||||
if (root) {
|
||||
const QRegularExpression re(pattern);
|
||||
findChildrenDfs(
|
||||
root,
|
||||
[&re](const QQuickItem* item) {
|
||||
return re.match(item->objectName()).hasMatch();
|
||||
},
|
||||
children);
|
||||
}
|
||||
return children;
|
||||
}
|
||||
|
||||
#ifndef ZSHELL_VERSION
|
||||
@@ -192,4 +295,46 @@ QString ZUtils::qtVersion() const {
|
||||
return QStringLiteral(QT_VERSION_STR);
|
||||
}
|
||||
|
||||
QString ZUtils::gitRevision() {
|
||||
#ifdef GIT_REVISION
|
||||
return QStringLiteral(GIT_REVISION);
|
||||
#else
|
||||
return QString();
|
||||
#endif
|
||||
}
|
||||
|
||||
QString ZUtils::readTextFile(const QString& path) {
|
||||
QFile file(path);
|
||||
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||
return QString();
|
||||
}
|
||||
return QString::fromUtf8(file.readAll());
|
||||
}
|
||||
|
||||
bool ZUtils::writeTextFile(const QString& path, const QString& text) {
|
||||
const QFileInfo info(path);
|
||||
if (!QDir().mkpath(info.absolutePath())) {
|
||||
qCWarning(lcZUtils) << "Failed to create directory for" << path;
|
||||
return false;
|
||||
}
|
||||
QFile file(path);
|
||||
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
||||
qCWarning(lcZUtils) << "Failed to open" << path << "for writing";
|
||||
return false;
|
||||
}
|
||||
return file.write(text.toUtf8()) >= 0;
|
||||
}
|
||||
|
||||
QStringList ZUtils::listFiles(const QString& dir, const QString& suffix) {
|
||||
QStringList out;
|
||||
QDirIterator it(dir, QDirIterator::Subdirectories);
|
||||
while (it.hasNext()) {
|
||||
const QString path = it.next();
|
||||
if (it.fileInfo().isFile() && path.endsWith(suffix)) {
|
||||
out.append(path);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace ZShell
|
||||
|
||||
Reference in New Issue
Block a user