diff options
author | Milian Wolff <milian.wolff@kdab.com> | 2014-07-03 19:17:02 +0200 |
---|---|---|
committer | Milian Wolff <milian.wolff@kdab.com> | 2014-07-15 11:31:48 +0200 |
commit | 003596fad52690127afca0d7025b62bad7fd013e (patch) | |
tree | 7b68f95ce3a519018b309990f85bf7e044307fe9 /src/imports | |
parent | 125c5f7dc270ab58e5f876cf8bc8aaf56d9e8f1b (diff) | |
download | qtwebchannel-003596fad52690127afca0d7025b62bad7fd013e.tar.gz |
Make the QWebChannel QML API publically accessible.
This is required for proper QtWebKit/QtWebEngine integration, as
otherwise these modules would have to redo a lot of the QtWebChannel
QML API. Furthermore, without this, we could not use the WebChannel.id
attached property everywhere, independent of the web browser technology.
Change-Id: I032a9326841d505c2f77959a240bbfc71e94b6e8
Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
Diffstat (limited to 'src/imports')
-rw-r--r-- | src/imports/webchannel/plugin.cpp | 5 | ||||
-rw-r--r-- | src/imports/webchannel/qmlwebchannel.cpp | 199 | ||||
-rw-r--r-- | src/imports/webchannel/qmlwebchannel.h | 102 | ||||
-rw-r--r-- | src/imports/webchannel/qmlwebchannelattached.cpp | 68 | ||||
-rw-r--r-- | src/imports/webchannel/qmlwebchannelattached.h | 70 | ||||
-rw-r--r-- | src/imports/webchannel/webchannel.pro | 8 |
6 files changed, 4 insertions, 448 deletions
diff --git a/src/imports/webchannel/plugin.cpp b/src/imports/webchannel/plugin.cpp index 6507112..235f542 100644 --- a/src/imports/webchannel/plugin.cpp +++ b/src/imports/webchannel/plugin.cpp @@ -42,7 +42,8 @@ #include <qqml.h> #include <QtQml/QQmlExtensionPlugin> -#include "qmlwebchannel.h" +#include <qqmlwebchannel.h> +#include <qqmlwebchannelattached_p.h> QT_BEGIN_NAMESPACE @@ -59,7 +60,7 @@ void QWebChannelPlugin::registerTypes(const char *uri) { int major = 1; int minor = 0; - qmlRegisterType<QmlWebChannel>(uri, major, minor, "WebChannel"); + qmlRegisterType<QQmlWebChannel>(uri, major, minor, "WebChannel"); } QT_END_NAMESPACE diff --git a/src/imports/webchannel/qmlwebchannel.cpp b/src/imports/webchannel/qmlwebchannel.cpp deleted file mode 100644 index e61e9c8..0000000 --- a/src/imports/webchannel/qmlwebchannel.cpp +++ /dev/null @@ -1,199 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com> -** Contact: http://www.qt-project.org/legal -** -** This file is part of the QtWebChannel module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "qmlwebchannel.h" - -#include "qwebchannel_p.h" -#include "qmetaobjectpublisher_p.h" -#include "qwebchannelabstracttransport.h" - -#include <QtQml/QQmlContext> - -QT_USE_NAMESPACE - -QmlWebChannel::QmlWebChannel(QObject *parent) - : QWebChannel(parent) -{ -} - -QmlWebChannel::~QmlWebChannel() -{ - -} - -void QmlWebChannel::registerObjects(const QVariantMap &objects) -{ - QMap<QString, QVariant>::const_iterator it = objects.constBegin(); - for (; it != objects.constEnd(); ++it) { - QObject *object = it.value().value<QObject*>(); - if (!object) { - qWarning("Invalid QObject given to register under name %s", qPrintable(it.key())); - continue; - } - d->publisher->registerObject(it.key(), object); - } -} - -bool QmlWebChannel::test_clientIsIdle() const -{ - return d->publisher->clientIsIdle; -} - -void QmlWebChannel::objectIdChanged(const QString &newId) -{ - const QmlWebChannelAttached *const attached = qobject_cast<QmlWebChannelAttached*>(sender()); - Q_ASSERT(attached); - Q_ASSERT(attached->parent()); - Q_ASSERT(m_registeredObjects.contains(attached->parent())); - - QObject *const object = attached->parent(); - const QString &oldId = d->publisher->registeredObjectIds.value(object); - - if (!oldId.isEmpty()) { - deregisterObject(object); - } - - registerObject(newId, object); -} - -QmlWebChannelAttached *QmlWebChannel::qmlAttachedProperties(QObject *obj) -{ - return new QmlWebChannelAttached(obj); -} - -void QmlWebChannel::connectTo(QObject *transport) -{ - if (QWebChannelAbstractTransport *realTransport = qobject_cast<QWebChannelAbstractTransport*>(transport)) { - QWebChannel::connectTo(realTransport); - } else { - qWarning() << "Cannot connect to transport" << transport << " - it is not a QWebChannelAbstractTransport."; - } -} - -void QmlWebChannel::disconnectFrom(QObject *transport) -{ - if (QWebChannelAbstractTransport *realTransport = qobject_cast<QWebChannelAbstractTransport*>(transport)) { - QWebChannel::disconnectFrom(realTransport); - } else { - qWarning() << "Cannot disconnect from transport" << transport << " - it is not a QWebChannelAbstractTransport."; - } -} - -QQmlListProperty<QObject> QmlWebChannel::registeredObjects() -{ - return QQmlListProperty<QObject>(this, 0, - registeredObjects_append, - registeredObjects_count, - registeredObjects_at, - registeredObjects_clear); -} - -void QmlWebChannel::registeredObjects_append(QQmlListProperty<QObject> *prop, QObject *object) -{ - const QmlWebChannelAttached *const attached = qobject_cast<QmlWebChannelAttached*>( - qmlAttachedPropertiesObject<QmlWebChannel>(object, false /* don't create */)); - if (!attached) { - const QQmlContext *const context = qmlContext(object); - qWarning() << "Cannot register object" << context->nameForObject(object) << '(' << object << ") without attached WebChannel.id property. Did you forget to set it?"; - return; - } - QmlWebChannel *channel = static_cast<QmlWebChannel*>(prop->object); - if (!attached->id().isEmpty()) { - // TODO: warning in such cases? - channel->registerObject(attached->id(), object); - } - channel->m_registeredObjects.append(object); - connect(attached, SIGNAL(idChanged(QString)), channel, SLOT(objectIdChanged(QString))); -} - -int QmlWebChannel::registeredObjects_count(QQmlListProperty<QObject> *prop) -{ - return static_cast<QmlWebChannel*>(prop->object)->m_registeredObjects.size(); -} - -QObject *QmlWebChannel::registeredObjects_at(QQmlListProperty<QObject> *prop, int index) -{ - return static_cast<QmlWebChannel*>(prop->object)->m_registeredObjects.at(index); -} - -void QmlWebChannel::registeredObjects_clear(QQmlListProperty<QObject> *prop) -{ - QmlWebChannel *channel = static_cast<QmlWebChannel*>(prop->object); - foreach (QObject *object, channel->m_registeredObjects) { - channel->deregisterObject(object); - } - return channel->m_registeredObjects.clear(); -} - -QQmlListProperty<QObject> QmlWebChannel::transports() -{ - return QQmlListProperty<QObject>(this, 0, - transports_append, - transports_count, - transports_at, - transports_clear); -} - -void QmlWebChannel::transports_append(QQmlListProperty<QObject> *prop, QObject *transport) -{ - QmlWebChannel *channel = static_cast<QmlWebChannel*>(prop->object); - channel->connectTo(transport); -} - -int QmlWebChannel::transports_count(QQmlListProperty<QObject> *prop) -{ - return static_cast<QmlWebChannel*>(prop->object)->d->transports.size(); -} - -QObject *QmlWebChannel::transports_at(QQmlListProperty<QObject> *prop, int index) -{ - QmlWebChannel *channel = static_cast<QmlWebChannel*>(prop->object); - return dynamic_cast<QObject*>(channel->d->transports.at(index)); -} - -void QmlWebChannel::transports_clear(QQmlListProperty<QObject> *prop) -{ - QWebChannel *channel = static_cast<QWebChannel*>(prop->object); - foreach (QWebChannelAbstractTransport *transport, channel->d->transports) { - channel->disconnectFrom(transport); - } - Q_ASSERT(channel->d->transports.isEmpty()); -} diff --git a/src/imports/webchannel/qmlwebchannel.h b/src/imports/webchannel/qmlwebchannel.h deleted file mode 100644 index 6bc7127..0000000 --- a/src/imports/webchannel/qmlwebchannel.h +++ /dev/null @@ -1,102 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com> -** Contact: http://www.qt-project.org/legal -** -** This file is part of the QtWebChannel module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QMLWEBCHANNEL_H -#define QMLWEBCHANNEL_H - -#include <qwebchannel.h> - -#include "qmlwebchannelattached.h" - -#include <QVector> - -#include <QtQml/qqml.h> -#include <QtQml/QQmlListProperty> - -QT_BEGIN_NAMESPACE - -class QmlWebChannel : public QWebChannel -{ - Q_OBJECT - - Q_PROPERTY( QQmlListProperty<QObject> transports READ transports ); - Q_PROPERTY( QQmlListProperty<QObject> registeredObjects READ registeredObjects ) - -public: - explicit QmlWebChannel(QObject *parent = 0); - virtual ~QmlWebChannel(); - - Q_INVOKABLE void registerObjects(const QVariantMap &objects); - QQmlListProperty<QObject> registeredObjects(); - - QQmlListProperty<QObject> transports(); - - // TODO: remove this by replacing QML with C++ tests - Q_INVOKABLE bool test_clientIsIdle() const; - - static QmlWebChannelAttached *qmlAttachedProperties(QObject *obj); - - Q_INVOKABLE void connectTo(QObject *transport); - Q_INVOKABLE void disconnectFrom(QObject *transport); - -private Q_SLOTS: - void objectIdChanged(const QString &newId); - -private: - static void registeredObjects_append(QQmlListProperty<QObject> *prop, QObject *item); - static int registeredObjects_count(QQmlListProperty<QObject> *prop); - static QObject *registeredObjects_at(QQmlListProperty<QObject> *prop, int index); - static void registeredObjects_clear(QQmlListProperty<QObject> *prop); - - static void transports_append(QQmlListProperty<QObject> *prop, QObject *item); - static int transports_count(QQmlListProperty<QObject> *prop); - static QObject *transports_at(QQmlListProperty<QObject> *prop, int index); - static void transports_clear(QQmlListProperty<QObject> *prop); - - QVector<QObject*> m_registeredObjects; -}; - -QML_DECLARE_TYPE( QmlWebChannel ) -QML_DECLARE_TYPEINFO( QmlWebChannel, QML_HAS_ATTACHED_PROPERTIES ) - -QT_END_NAMESPACE - -#endif // QMLWEBCHANNEL_H diff --git a/src/imports/webchannel/qmlwebchannelattached.cpp b/src/imports/webchannel/qmlwebchannelattached.cpp deleted file mode 100644 index 637feb2..0000000 --- a/src/imports/webchannel/qmlwebchannelattached.cpp +++ /dev/null @@ -1,68 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com> -** Contact: http://www.qt-project.org/legal -** -** This file is part of the QtWebChannel module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "qmlwebchannelattached.h" - -QT_USE_NAMESPACE - -QmlWebChannelAttached::QmlWebChannelAttached(QObject *parent) - : QObject(parent) -{ - -} - -QmlWebChannelAttached::~QmlWebChannelAttached() -{ - -} - -QString QmlWebChannelAttached::id() const -{ - return m_id; -} - -void QmlWebChannelAttached::setId(const QString &id) -{ - if (id != m_id) { - m_id = id; - emit idChanged(id); - } -} diff --git a/src/imports/webchannel/qmlwebchannelattached.h b/src/imports/webchannel/qmlwebchannelattached.h deleted file mode 100644 index 3d60122..0000000 --- a/src/imports/webchannel/qmlwebchannelattached.h +++ /dev/null @@ -1,70 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com> -** Contact: http://www.qt-project.org/legal -** -** This file is part of the QtWebChannel module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QMLWEBCHANNELATTACHED_H -#define QMLWEBCHANNELATTACHED_H - -#include <QObject> - -QT_BEGIN_NAMESPACE - -class QmlWebChannelAttached : public QObject -{ - Q_OBJECT - - Q_PROPERTY( QString id READ id WRITE setId NOTIFY idChanged FINAL ) -public: - explicit QmlWebChannelAttached(QObject *parent = 0); - virtual ~QmlWebChannelAttached(); - - QString id() const; - void setId(const QString &id); - -Q_SIGNALS: - void idChanged(const QString &id); - -private: - QString m_id; -}; - -QT_END_NAMESPACE - -#endif // QMLWEBCHANNELATTACHED_H diff --git a/src/imports/webchannel/webchannel.pro b/src/imports/webchannel/webchannel.pro index c1959f5..8ae6ef5 100644 --- a/src/imports/webchannel/webchannel.pro +++ b/src/imports/webchannel/webchannel.pro @@ -4,12 +4,6 @@ INCLUDEPATH += ../../webchannel VPATH += ../../webchannel SOURCES += \ - plugin.cpp \ - qmlwebchannel.cpp \ - qmlwebchannelattached.cpp - -HEADERS += \ - qmlwebchannel.h \ - qmlwebchannelattached.h + plugin.cpp load(qml_plugin) |