mirror of
https://invent.kde.org/network/falkon.git
synced 2024-12-20 18:56:34 +01:00
Created QML API to register external QtQbject
This commit is contained in:
parent
501f3137ce
commit
9877833d15
@ -185,6 +185,7 @@ set(SRCS ${SRCS}
|
|||||||
plugins/qml/api/events/qmlkeyevent.cpp
|
plugins/qml/api/events/qmlkeyevent.cpp
|
||||||
plugins/qml/api/userscript/qmluserscript.cpp
|
plugins/qml/api/userscript/qmluserscript.cpp
|
||||||
plugins/qml/api/userscript/qmluserscripts.cpp
|
plugins/qml/api/userscript/qmluserscripts.cpp
|
||||||
|
plugins/qml/api/userscript/qmlexternaljsobject.cpp
|
||||||
popupwindow/popuplocationbar.cpp
|
popupwindow/popuplocationbar.cpp
|
||||||
popupwindow/popupstatusbarmessage.cpp
|
popupwindow/popupstatusbarmessage.cpp
|
||||||
popupwindow/popupwebview.cpp
|
popupwindow/popupwebview.cpp
|
||||||
|
45
src/lib/plugins/qml/api/userscript/qmlexternaljsobject.cpp
Normal file
45
src/lib/plugins/qml/api/userscript/qmlexternaljsobject.cpp
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
/* ============================================================
|
||||||
|
* Falkon - Qt web browser
|
||||||
|
* Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
* ============================================================ */
|
||||||
|
#include "qmlexternaljsobject.h"
|
||||||
|
#include "javascript/externaljsobject.h"
|
||||||
|
|
||||||
|
QmlExternalJsObject::QmlExternalJsObject(QObject *parent)
|
||||||
|
: QObject(parent)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void QmlExternalJsObject::registerExtraObject(const QVariantMap &map)
|
||||||
|
{
|
||||||
|
if (!map.contains(QSL("id")) || !map.contains(QSL("object"))) {
|
||||||
|
qWarning() << "Unable to call" << __FUNCTION__ << ": unsufficient parameters";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const QString id = map.value(QSL("id")).toString();
|
||||||
|
QObject *object = qvariant_cast<QObject*>(map.value(QSL("object")));
|
||||||
|
if (!object) {
|
||||||
|
qWarning() << "Unable to cast to QObject";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ExternalJsObject::registerExtraObject(id, object);
|
||||||
|
}
|
||||||
|
|
||||||
|
void QmlExternalJsObject::unregisterExtraObject(QObject *object)
|
||||||
|
{
|
||||||
|
ExternalJsObject::unregisterExtraObject(object);
|
||||||
|
}
|
29
src/lib/plugins/qml/api/userscript/qmlexternaljsobject.h
Normal file
29
src/lib/plugins/qml/api/userscript/qmlexternaljsobject.h
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
/* ============================================================
|
||||||
|
* Falkon - Qt web browser
|
||||||
|
* Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
* ============================================================ */
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
|
|
||||||
|
class QmlExternalJsObject : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
explicit QmlExternalJsObject(QObject *parent = nullptr);
|
||||||
|
Q_INVOKABLE void registerExtraObject(const QVariantMap &map);
|
||||||
|
Q_INVOKABLE void unregisterExtraObject(QObject *object);
|
||||||
|
};
|
@ -44,6 +44,7 @@
|
|||||||
#include "api/events/qmlwheelevent.h"
|
#include "api/events/qmlwheelevent.h"
|
||||||
#include "api/userscript/qmluserscript.h"
|
#include "api/userscript/qmluserscript.h"
|
||||||
#include "api/userscript/qmluserscripts.h"
|
#include "api/userscript/qmluserscripts.h"
|
||||||
|
#include "api/userscript/qmlexternaljsobject.h"
|
||||||
|
|
||||||
#ifdef LibIntl_FOUND
|
#ifdef LibIntl_FOUND
|
||||||
#include "qml/api/i18n/qmli18n.h"
|
#include "qml/api/i18n/qmli18n.h"
|
||||||
@ -193,4 +194,12 @@ void QmlPlugins::registerQmlTypes()
|
|||||||
auto *object = new QmlUserScripts();
|
auto *object = new QmlUserScripts();
|
||||||
return object;
|
return object;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
qmlRegisterSingletonType<QmlExternalJsObject>("org.kde.falkon", 1, 0, "ExternalJsObject", [](QQmlEngine *engine, QJSEngine *scriptEngine) -> QObject * {
|
||||||
|
Q_UNUSED(engine)
|
||||||
|
Q_UNUSED(scriptEngine)
|
||||||
|
|
||||||
|
auto *object = new QmlExternalJsObject();
|
||||||
|
return object;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user