feat: implement a C++ helper to read contents of a text file

uses `QQmlFileSelector` to choose the correct variant and `QFile` for
the actual reading
This commit is contained in:
Lukáš Tinkl 2023-09-22 12:02:30 +02:00 committed by Lukáš Tinkl
parent b4958689bd
commit 9e13e5f1cf
4 changed files with 44 additions and 21 deletions

View File

@ -11,9 +11,12 @@ class StringUtilsInternal : public QObject
Q_OBJECT
public:
explicit StringUtilsInternal(QObject* parent = nullptr);
explicit StringUtilsInternal(QQmlEngine* engine, QObject* parent = nullptr);
Q_INVOKABLE QString escapeHtml(const QString &unsafe) const;
static QObject* qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine);
Q_INVOKABLE QString readTextFile(const QString& filePath) const;
private:
QQmlEngine *m_engine{nullptr};
};

View File

@ -8,4 +8,8 @@ QtObject {
function escapeHtml(unsafe) {
return Internal.StringUtils.escapeHtml(unsafe)
}
function readTextFile(file) {
return Internal.StringUtils.readTextFile(file)
}
}

View File

@ -1,4 +1,4 @@
#include <QtQml/QQmlExtensionPlugin>
#include <QQmlExtensionPlugin>
#include <QZXing.h>
#include <qqmlsortfilterproxymodeltypes.h>
@ -11,7 +11,8 @@
#include "StatusQ/statuswindow.h"
#include "StatusQ/stringutilsinternal.h"
class StatusQPlugin : public QQmlExtensionPlugin {
class StatusQPlugin : public QQmlExtensionPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID QQmlExtensionInterface_iid)
public:
@ -27,21 +28,20 @@ public:
qmlRegisterSingletonType<ModelUtilsInternal>(
"StatusQ.Internal", 0, 1, "ModelUtils", &ModelUtilsInternal::qmlInstance);
qmlRegisterSingletonType<StringUtilsInternal>(
"StatusQ.Internal", 0, 1, "StringUtils", &StringUtilsInternal::qmlInstance);
qmlRegisterSingletonType<PermissionUtilsInternal>("StatusQ.Internal", 0, 1, "PermissionUtils", [](QQmlEngine *, QJSEngine *) {
return new PermissionUtilsInternal;
});
qmlRegisterSingletonType<StringUtilsInternal>(
"StatusQ.Internal", 0, 1, "StringUtils", [](QQmlEngine* engine, QJSEngine*) {
return new StringUtilsInternal(engine);
});
qmlRegisterSingletonType<PermissionUtilsInternal>(
"StatusQ.Internal", 0, 1, "PermissionUtils", [](QQmlEngine*, QJSEngine*) {
return new PermissionUtilsInternal;
});
QZXing::registerQMLTypes();
qqsfpm::registerTypes();
}
void initializeEngine(QQmlEngine* engine, const char* uri) override
{
QQmlExtensionPlugin::initializeEngine(engine, uri);
}
};
#include "plugin.moc"

View File

@ -1,7 +1,13 @@
#include "StatusQ/stringutilsinternal.h"
StringUtilsInternal::StringUtilsInternal(QObject* parent)
: QObject(parent)
#include <QFile>
#include <QFileSelector>
#include <QQmlEngine>
#include <QQmlFileSelector>
StringUtilsInternal::StringUtilsInternal(QQmlEngine *engine, QObject* parent)
: m_engine(engine)
, QObject(parent)
{
}
@ -10,11 +16,21 @@ QString StringUtilsInternal::escapeHtml(const QString &unsafe) const
return unsafe.toHtmlEscaped();
}
QObject* StringUtilsInternal::qmlInstance(QQmlEngine *engine,
QJSEngine *scriptEngine)
QString StringUtilsInternal::readTextFile(const QString& filePath) const
{
Q_UNUSED(engine);
Q_UNUSED(scriptEngine);
auto selector = QQmlFileSelector::get(m_engine);
if (!selector) {
qWarning() << Q_FUNC_INFO << "No QQmlFileSelector available to load text file:" << filePath;
return {};
}
return new StringUtilsInternal;
const auto resolvedFilePath = selector->selector()->select(filePath);
QFile file(resolvedFilePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qWarning() << Q_FUNC_INFO << "Error opening" << resolvedFilePath << "for reading";
return {};
}
return file.readAll();
}