diff options
author | Romain Pokrzywka <romain.pokrzywka@bluescape.com> | 2018-05-01 17:05:52 -0500 |
---|---|---|
committer | Romain Pokrzywka <romain.pokrzywka@gmail.com> | 2018-06-18 14:09:03 +0000 |
commit | 5fe78f9ef2d093cccfb52f810416dc5fb56d97fa (patch) | |
tree | c674bea7c83d53f932a3876de19cc12091e39cff /src/webengine/ui_delegates_manager.cpp | |
parent | 5ccb387e489b0f82760e66d4546ba353c00cf630 (diff) |
Add support for loading UI delegates from resource files
Custom UI delegates can be specified for dialogs, context menus, etc. by
placing them in a specific folder accessible from the import path, but
it assumes that all import paths are folders on the os filesystem.
But resource (.qrc) files are also supported as a built-in import path,
so we should support loading UI delegates from there too.
We just need a couple of adjustments for QFileInfo to look them up using
the ":/" prefix, while still loading then using the "qrc:/" protocol.
Also add support for partial controls override, so it's possible
to customize only some UI delegates by prepending an import path with
some delegate files in there, and use the built-in ones for the rest.
Change-Id: I6c791ed0118b396639acd8af8e135e1d68b2c19b
Reviewed-by: Michal Klocek <michal.klocek@qt.io>
Diffstat (limited to 'src/webengine/ui_delegates_manager.cpp')
-rw-r--r-- | src/webengine/ui_delegates_manager.cpp | 49 |
1 files changed, 34 insertions, 15 deletions
diff --git a/src/webengine/ui_delegates_manager.cpp b/src/webengine/ui_delegates_manager.cpp index 5d5e89144..4570172d7 100644 --- a/src/webengine/ui_delegates_manager.cpp +++ b/src/webengine/ui_delegates_manager.cpp @@ -143,16 +143,21 @@ UIDelegatesManager::~UIDelegatesManager() component = &COMPONENT##Component; \ break; -bool UIDelegatesManager::initializeImportDirs(QStringList &dirs, QQmlEngine *engine) { +bool UIDelegatesManager::initializeImportDirs(QStringList &dirs, QQmlEngine *engine) +{ const QStringList paths = engine->importPathList(); for (const QString &path : paths) { - QFileInfo fi(path % QLatin1String("/QtWebEngine/Controls1Delegates/")); - if (fi.exists()) { + QString importPath = path % QLatin1String("/QtWebEngine/Controls1Delegates/"); + + // resource paths have to be tested using the ":/" prefix + if (importPath.startsWith(QLatin1String("qrc:/"))) + importPath.remove(0, 3); + + QFileInfo fi(importPath); + if (fi.exists()) dirs << fi.absolutePath(); - return true; - } } - return false; + return !dirs.isEmpty(); } bool UIDelegatesManager::ensureComponentLoaded(ComponentType type) @@ -179,11 +184,15 @@ bool UIDelegatesManager::ensureComponentLoaded(ComponentType type) return false; for (const QString &importDir : qAsConst(m_importDirs)) { - QFileInfo fi(importDir % QLatin1Char('/') % fileName); - if (!fi.exists()) + const QString componentFilePath = importDir % QLatin1Char('/') % fileName; + + if (!QFileInfo(componentFilePath).exists()) continue; + // FIXME: handle async loading - *component = (new QQmlComponent(engine, QUrl::fromLocalFile(fi.absoluteFilePath()), + *component = (new QQmlComponent(engine, + importDir.startsWith(QLatin1String(":/")) ? QUrl(QLatin1String("qrc") + componentFilePath) + : QUrl::fromLocalFile(componentFilePath), QQmlComponent::PreferSynchronous, m_view)); if ((*component)->status() != QQmlComponent::Ready) { @@ -579,14 +588,24 @@ bool UI2DelegatesManager::initializeImportDirs(QStringList &dirs, QQmlEngine *en { const QStringList paths = engine->importPathList(); for (const QString &path : paths) { - QFileInfo fi1(path % QLatin1String("/QtWebEngine/Controls1Delegates/")); - QFileInfo fi2(path % QLatin1String("/QtWebEngine/Controls2Delegates/")); - if (fi1.exists() && fi2.exists()) { - dirs << fi2.absolutePath() << fi1.absolutePath(); - return true; + QString controls2ImportPath = path % QLatin1String("/QtWebEngine/Controls2Delegates/"); + QString controls1ImportPath = path % QLatin1String("/QtWebEngine/Controls1Delegates/"); + + // resource paths have to be tested using the ":/" prefix + if (controls2ImportPath.startsWith(QLatin1String("qrc:/"))) { + controls2ImportPath.remove(0, 3); + controls1ImportPath.remove(0, 3); } + + QFileInfo fi2(controls2ImportPath); + if (fi2.exists()) + dirs << fi2.absolutePath(); + + QFileInfo fi1(controls1ImportPath); + if (fi1.exists()) + dirs << fi1.absolutePath(); } - return false; + return !dirs.isEmpty(); } QObject *UI2DelegatesManager::addMenu(QObject *parentMenu, const QString &title, const QPoint &pos) |