diff options
author | Robin Burchell <robin.burchell@theqtcompany.com> | 2015-03-10 13:44:42 +0100 |
---|---|---|
committer | Robin Burchell <robin.burchell@viroteck.net> | 2015-03-18 13:06:00 +0000 |
commit | e9c82ddc22662ffc768d37f0be5e7e3ac3599bfa (patch) | |
tree | c0eaa3f4000f1fbaee7fbfa32dc2f8705e37843e /src | |
parent | d4ab9273dd1c61e1638f5b0376e65f7c5375ad93 (diff) |
QQuickWebEngineScript: Add a sourceUrl property to complement sourceCode.
QtQuick doesn't have a convenient interface to interact with files, and even if
that were the case, it would either be more awkward (and with increased overhead
-- the price of an extra QObject, property, signal, etc) just to fetch the
contents.
So, we provide a convenience to allow fetching a user script from a file.
Setting the sourceCode directly will reset sourceUrl (if it is set), and setting
sourceUrl will ultimately result in a sourceCode change signal, too.
Change-Id: Iee5abc0d719e2aeeacf1265f695b5a7efee9e0e8
Reviewed-by: Andras Becsi <andras.becsi@theqtcompany.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/webengine/api/qquickwebenginescript.cpp | 35 | ||||
-rw-r--r-- | src/webengine/api/qquickwebenginescript_p.h | 5 | ||||
-rw-r--r-- | src/webengine/api/qquickwebenginescript_p_p.h | 1 |
3 files changed, 41 insertions, 0 deletions
diff --git a/src/webengine/api/qquickwebenginescript.cpp b/src/webengine/api/qquickwebenginescript.cpp index 6ea6d01bc..eb3a41dd5 100644 --- a/src/webengine/api/qquickwebenginescript.cpp +++ b/src/webengine/api/qquickwebenginescript.cpp @@ -37,6 +37,8 @@ #include "qquickwebenginescript_p.h" #include "qquickwebenginescript_p_p.h" +#include <QtCore/QDebug> +#include <QtCore/QFile> #include <QtCore/QStringBuilder> #include <QtCore/QTimerEvent> #include "user_script_controller_host.h" @@ -82,6 +84,11 @@ QString QQuickWebEngineScript::name() const return d->coreScript.name(); } +QUrl QQuickWebEngineScript::sourceUrl() const +{ + Q_D(const QQuickWebEngineScript); + return d->m_sourceUrl; +} QString QQuickWebEngineScript::sourceCode() const { @@ -129,11 +136,39 @@ void QQuickWebEngineScript::setSourceCode(QString arg) Q_D(QQuickWebEngineScript); if (arg == sourceCode()) return; + + // setting the source directly resets the sourceUrl + if (d->m_sourceUrl != QUrl()) { + d->m_sourceUrl = QUrl(); + Q_EMIT sourceUrlChanged(d->m_sourceUrl); + } + d->aboutToUpdateUnderlyingScript(); d->coreScript.setSourceCode(arg); Q_EMIT sourceCodeChanged(arg); } +void QQuickWebEngineScript::setSourceUrl(QUrl arg) +{ + Q_D(QQuickWebEngineScript); + if (arg == sourceUrl()) + return; + + d->m_sourceUrl = arg; + Q_EMIT sourceUrlChanged(d->m_sourceUrl); + + QFile f(arg.toLocalFile()); + if (!f.open(QIODevice::ReadOnly)) { + qWarning() << "Can't open user script " << arg; + return; + } + + d->aboutToUpdateUnderlyingScript(); + QString source = QString::fromUtf8(f.readAll()); + d->coreScript.setSourceCode(source); + Q_EMIT sourceCodeChanged(source); +} + void QQuickWebEngineScript::setInjectionPoint(QQuickWebEngineScript::InjectionPoint arg) { Q_D(QQuickWebEngineScript); diff --git a/src/webengine/api/qquickwebenginescript_p.h b/src/webengine/api/qquickwebenginescript_p.h index 55f0c782d..f1d7a01dc 100644 --- a/src/webengine/api/qquickwebenginescript_p.h +++ b/src/webengine/api/qquickwebenginescript_p.h @@ -39,6 +39,7 @@ #include <private/qtwebengineglobal_p.h> #include <QtCore/QObject> +#include <QtCore/QUrl> QT_BEGIN_NAMESPACE class QQuickWebEngineScriptPrivate; @@ -50,6 +51,7 @@ class Q_WEBENGINE_PRIVATE_EXPORT QQuickWebEngineScript : public QObject Q_ENUMS(InjectionPoint) Q_ENUMS(ScriptWorldId) Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) + Q_PROPERTY(QUrl sourceUrl READ sourceUrl WRITE setSourceUrl NOTIFY sourceUrlChanged) Q_PROPERTY(QString sourceCode READ sourceCode WRITE setSourceCode NOTIFY sourceCodeChanged) Q_PROPERTY(InjectionPoint injectionPoint READ injectionPoint WRITE setInjectionPoint NOTIFY injectionPointChanged) Q_PROPERTY(ScriptWorldId worldId READ worldId WRITE setWorldId NOTIFY worldIdChanged) @@ -74,6 +76,7 @@ public: Q_INVOKABLE QString toString() const; QString name() const; + QUrl sourceUrl() const; QString sourceCode() const; InjectionPoint injectionPoint() const; ScriptWorldId worldId() const; @@ -81,6 +84,7 @@ public: public Q_SLOTS: void setName(QString arg); + void setSourceUrl(QUrl arg); void setSourceCode(QString arg); void setInjectionPoint(InjectionPoint arg); void setWorldId(ScriptWorldId arg); @@ -88,6 +92,7 @@ public Q_SLOTS: Q_SIGNALS: void nameChanged(QString arg); + void sourceUrlChanged(QUrl arg); void sourceCodeChanged(QString arg); void injectionPointChanged(InjectionPoint arg); void worldIdChanged(ScriptWorldId arg); diff --git a/src/webengine/api/qquickwebenginescript_p_p.h b/src/webengine/api/qquickwebenginescript_p_p.h index 7b5626fd1..f8a957579 100644 --- a/src/webengine/api/qquickwebenginescript_p_p.h +++ b/src/webengine/api/qquickwebenginescript_p_p.h @@ -61,6 +61,7 @@ public: QBasicTimer m_basicTimer; QtWebEngineCore::UserScriptControllerHost *m_controllerHost; QtWebEngineCore::WebContentsAdapter *m_adapter; + QUrl m_sourceUrl; private: QQuickWebEngineScript *q_ptr; |