Falkon Develop
Cross-platform Qt-based web browser
qmlplugininterface.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "qmlplugininterface.h"
19#include "mainapplication.h"
20#include "pluginproxy.h"
21#include "statusbar.h"
22#include "browserwindow.h"
23#include "navigationbar.h"
24#include "sidebar.h"
25#include "api/menus/qmlmenu.h"
31#include "api/tabs/qmltab.h"
32#include "webpage.h"
33#include "qztools.h"
34#include "qml/qmlengine.h"
35#include <QDebug>
36#include <QQuickWidget>
37#include <QDialog>
38#include <QVBoxLayout>
39
41 : m_qmlReusableTab(new QmlTab())
42{
43}
44
46{
47 m_qmlReusableTab->deleteLater();
48}
49
50void QmlPluginInterface::init(InitState state, const QString &settingsPath)
51{
52 if (!m_init.isCallable()) {
53 qWarning() << "Unable to call" << __FUNCTION__ << "on" << m_name << "plugin";
54 return;
55 }
56
57 QJSValueList args;
58 args.append(state);
59 args.append(settingsPath);
60 m_init.call(args);
61}
62
64{
65 if (!m_unload.isCallable()) {
66 qWarning() << "Unable to call" << __FUNCTION__ << "on" << m_name << "plugin";
67 return;
68 }
69
70 m_unload.call();
71
72 for (QObject *childItem : std::as_const(m_childItems)) {
73 childItem->deleteLater();
74 }
75
76 Q_EMIT qmlPluginUnloaded();
77}
78
80{
81 if (!m_testPlugin.isCallable()) {
82 qWarning() << "Unable to call" << __FUNCTION__ << "on" << m_name << "plugin";
83 return false;
84 }
85
86 QJSValue ret = m_testPlugin.call();
87 return ret.toBool();
88}
89
90void QmlPluginInterface::populateWebViewMenu(QMenu *menu, WebView *webview, const WebHitTestResult &webHitTestResult)
91{
92 Q_UNUSED(webview)
93
94 if (!m_populateWebViewMenu.isCallable()) {
95 return;
96 }
97
98 auto *qmlMenu = new QmlMenu(menu, m_engine);
99 auto *qmlWebHitTestResult = new QmlWebHitTestResult(webHitTestResult);
100 QJSValueList args;
101 args.append(m_engine->newQObject(qmlMenu));
102 args.append(m_engine->newQObject(qmlWebHitTestResult));
103 m_populateWebViewMenu.call(args);
104 menu->addSeparator();
105}
106
108{
109 if (!m_settingsWindow) {
110 qWarning() << "No dialog to show";
111 return;
112 }
113
114 auto *widget = new QQuickWidget();
115 widget->setContent(m_settingsWindow->url(), m_settingsWindow, m_settingsWindow->create(m_settingsWindow->creationContext()));
116 widget->show();
117
118 QzTools::centerWidgetToParent(widget, parent);
119}
120
121bool QmlPluginInterface::mouseDoubleClick(Qz::ObjectName type, QObject *obj, QMouseEvent *event)
122{
123 Q_UNUSED(obj)
124 if (!m_mouseDoubleClick.isCallable()) {
125 return false;
126 }
127 auto qmlMouseEvent = new QmlMouseEvent(event);
128 QJSValueList args;
129 args.append(QmlQzObjects::ObjectName(type));
130 args.append(m_engine->newQObject(qmlMouseEvent));
131 m_mouseDoubleClick.call(args);
132 qmlMouseEvent->clear();
133 return false;
134}
135
136bool QmlPluginInterface::mousePress(Qz::ObjectName type, QObject *obj, QMouseEvent *event)
137{
138 Q_UNUSED(obj)
139 if (!m_mousePress.isCallable()) {
140 return false;
141 }
142 auto qmlMouseEvent = new QmlMouseEvent(event);
143 QJSValueList args;
144 args.append(QmlQzObjects::ObjectName(type));
145 args.append(m_engine->newQObject(qmlMouseEvent));
146 m_mousePress.call(args);
147 qmlMouseEvent->clear();
148 return false;
149}
150
151bool QmlPluginInterface::mouseRelease(Qz::ObjectName type, QObject *obj, QMouseEvent *event)
152{
153 Q_UNUSED(obj)
154 if (!m_mouseRelease.isCallable()) {
155 return false;
156 }
157 auto qmlMouseEvent = new QmlMouseEvent(event);
158 QJSValueList args;
159 args.append(QmlQzObjects::ObjectName(type));
160 args.append(m_engine->newQObject(qmlMouseEvent));
161 m_mouseRelease.call(args);
162 qmlMouseEvent->clear();
163 return false;
164}
165
166bool QmlPluginInterface::mouseMove(Qz::ObjectName type, QObject *obj, QMouseEvent *event)
167{
168 Q_UNUSED(obj)
169 if (!m_mouseMove.isCallable()) {
170 return false;
171 }
172 auto qmlMouseEvent = new QmlMouseEvent(event);
173 QJSValueList args;
174 args.append(QmlQzObjects::ObjectName(type));
175 args.append(m_engine->newQObject(qmlMouseEvent));
176 m_mouseMove.call(args);
177 qmlMouseEvent->clear();
178 return false;
179}
180
181bool QmlPluginInterface::wheelEvent(Qz::ObjectName type, QObject *obj, QWheelEvent *event)
182{
183 Q_UNUSED(obj)
184 if (!m_wheelEvent.isCallable()) {
185 return false;
186 }
187 auto qmlWheelEvent = new QmlWheelEvent(event);
188 QJSValueList args;
189 args.append(QmlQzObjects::ObjectName(type));
190 args.append(m_engine->newQObject(qmlWheelEvent));
191 m_wheelEvent.call(args);
192 qmlWheelEvent->clear();
193 return false;
194}
195
196bool QmlPluginInterface::keyPress(Qz::ObjectName type, QObject *obj, QKeyEvent *event)
197{
198 Q_UNUSED(obj)
199 if (!m_keyPress.isCallable()) {
200 return false;
201 }
202 auto qmlKeyEvent = new QmlKeyEvent(event);
203 QJSValueList args;
204 args.append(QmlQzObjects::ObjectName(type));
205 args.append(m_engine->newQObject(qmlKeyEvent));
206 m_keyPress.call(args);
207 qmlKeyEvent->clear();
208 return false;
209}
210
211bool QmlPluginInterface::keyRelease(Qz::ObjectName type, QObject *obj, QKeyEvent *event)
212{
213 Q_UNUSED(obj)
214 if (!m_keyRelease.isCallable()) {
215 return false;
216 }
217 auto qmlKeyEvent = new QmlKeyEvent(event);
218 QJSValueList args;
219 args.append(QmlQzObjects::ObjectName(type));
220 args.append(m_engine->newQObject(qmlKeyEvent));
221 m_keyRelease.call(args);
222 qmlKeyEvent->clear();
223 return false;
224}
225
226bool QmlPluginInterface::acceptNavigationRequest(WebPage *page, const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame)
227{
228 if (!m_acceptNavigationRequest.isCallable()) {
229 return true;
230 }
231 m_qmlReusableTab->setWebPage(page);
232 QJSValueList args;
233 args.append(m_engine->newQObject(m_qmlReusableTab));
234 args.append(QString::fromUtf8(url.toEncoded()));
235 args.append(type);
236 args.append(isMainFrame);
237 return m_acceptNavigationRequest.call(args).toBool();
238}
239
240QJSValue QmlPluginInterface::readInit() const
241{
242 return m_init;
243}
244
245void QmlPluginInterface::setInit(const QJSValue &init)
246{
247 m_init = init;
248}
249
250QJSValue QmlPluginInterface::readUnload() const
251{
252 return m_unload;
253}
254
255void QmlPluginInterface::setUnload(const QJSValue &unload)
256{
257 m_unload = unload;
258}
259
260QJSValue QmlPluginInterface::readTestPlugin() const
261{
262 return m_testPlugin;
263}
264
265void QmlPluginInterface::setTestPlugin(const QJSValue &testPlugin)
266{
267 m_testPlugin = testPlugin;
268}
269
270void QmlPluginInterface::setEngine(QQmlEngine *engine)
271{
272 m_engine = engine;
273}
274
275void QmlPluginInterface::setName(const QString &name)
276{
277 m_name = name;
278}
279
280QJSValue QmlPluginInterface::readPopulateWebViewMenu() const
281{
282 return m_populateWebViewMenu;
283}
284
285void QmlPluginInterface::setPopulateWebViewMenu(const QJSValue &value)
286{
287 m_populateWebViewMenu = value;
288}
289
290QQmlComponent *QmlPluginInterface::settingsWindow() const
291{
292 return m_settingsWindow;
293}
294
295void QmlPluginInterface::setSettingsWindow(QQmlComponent *settingsWindow)
296{
297 m_settingsWindow = settingsWindow;
298}
299
300QJSValue QmlPluginInterface::readMouseDoubleClick() const
301{
302 return m_mouseDoubleClick;
303}
304
305void QmlPluginInterface::setMouseDoubleClick(const QJSValue &mouseDoubleClick)
306{
307 m_mouseDoubleClick = mouseDoubleClick;
308 mApp->plugins()->registerAppEventHandler(PluginProxy::MouseDoubleClickHandler, this);
309}
310
311QJSValue QmlPluginInterface::readMousePress() const
312{
313 return m_mousePress;
314}
315
316void QmlPluginInterface::setMousePress(const QJSValue &mousePress)
317{
318 m_mousePress = mousePress;
319 mApp->plugins()->registerAppEventHandler(PluginProxy::MousePressHandler, this);
320}
321
322QJSValue QmlPluginInterface::readMouseRelease() const
323{
324 return m_mouseRelease;
325}
326
327void QmlPluginInterface::setMouseRelease(const QJSValue &mouseRelease)
328{
329 m_mouseRelease = mouseRelease;
330 mApp->plugins()->registerAppEventHandler(PluginProxy::MouseReleaseHandler, this);
331}
332
333QJSValue QmlPluginInterface::readMouseMove() const
334{
335 return m_mouseMove;
336}
337
338void QmlPluginInterface::setMouseMove(const QJSValue &mouseMove)
339{
340 m_mouseMove = mouseMove;
341 mApp->plugins()->registerAppEventHandler(PluginProxy::MouseMoveHandler, this);
342}
343
344QJSValue QmlPluginInterface::readWheelEvent() const
345{
346 return m_wheelEvent;
347}
348
349void QmlPluginInterface::setWheelEvent(const QJSValue &wheelEvent)
350{
351 m_wheelEvent = wheelEvent;
352 mApp->plugins()->registerAppEventHandler(PluginProxy::WheelEventHandler, this);
353}
354
355QJSValue QmlPluginInterface::readKeyPress() const
356{
357 return m_keyPress;
358}
359
360void QmlPluginInterface::setKeyPress(const QJSValue &keyPress)
361{
362 m_keyPress = keyPress;
363 mApp->plugins()->registerAppEventHandler(PluginProxy::KeyPressHandler, this);
364}
365
366QJSValue QmlPluginInterface::readKeyRelease() const
367{
368 return m_keyRelease;
369}
370
371void QmlPluginInterface::setKeyRelease(const QJSValue &keyRelease)
372{
373 m_keyRelease = keyRelease;
374 mApp->plugins()->registerAppEventHandler(PluginProxy::KeyReleaseHandler, this);
375}
376
377QJSValue QmlPluginInterface::readAcceptNavigationRequest() const
378{
379 return m_acceptNavigationRequest;
380}
381
382void QmlPluginInterface::setAcceptNavigationRequest(const QJSValue &acceptNavigationRequest)
383{
384 m_acceptNavigationRequest = acceptNavigationRequest;
385}
386
387QQmlListProperty<QObject> QmlPluginInterface::childItems()
388{
389 return QQmlListProperty<QObject>(this, &m_childItems);
390}
@ WheelEventHandler
Definition: pluginproxy.h:35
@ MouseReleaseHandler
Definition: pluginproxy.h:33
@ MouseDoubleClickHandler
Definition: pluginproxy.h:33
@ KeyReleaseHandler
Definition: pluginproxy.h:34
@ MouseMoveHandler
Definition: pluginproxy.h:34
@ MousePressHandler
Definition: pluginproxy.h:33
The class exposing KeyEvent to QML.
Definition: qmlkeyevent.h:27
The class exposing WebView contextmenu to QML as Menu API.
Definition: qmlmenu.h:30
The class exposing MouseEvent to QML.
Definition: qmlmouseevent.h:26
QQmlListProperty< QObject > childItems
void showSettings(QWidget *parent=nullptr) override
void setName(const QString &name)
void setEngine(QQmlEngine *engine)
QJSValue acceptNavigationRequest
QQmlComponent * settingsWindow
ObjectName
The ObjectName enum.
Definition: qmlqzobjects.h:33
The class exposing a browser tab to QML.
Definition: qmltab.h:32
void setWebPage(WebPage *webPage)
Definition: qmltab.cpp:364
The class exposing result of WebHitTest to QML.
The class exposing WheelEvent to QML.
Definition: qmlwheelevent.h:27
static void centerWidgetToParent(QWidget *w, QWidget *parent)
Definition: qztools.cpp:124
#define mApp
int value(const QColor &c)
Definition: colors.cpp:238
ObjectName
Definition: qzcommon.h:89
State state