1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 18:56:34 +01:00

Created Menu API

This commit is contained in:
Anmol Gautam 2018-06-22 14:46:33 +05:30
parent 368dcd4811
commit 316bc85eb6
11 changed files with 396 additions and 31 deletions

View File

@ -175,6 +175,9 @@ set(SRCS ${SRCS}
plugins/qml/api/windows/qmlwindowtype.cpp plugins/qml/api/windows/qmlwindowtype.cpp
plugins/qml/api/browseraction/qmlbrowseraction.cpp plugins/qml/api/browseraction/qmlbrowseraction.cpp
plugins/qml/api/sidebar/qmlsidebar.cpp plugins/qml/api/sidebar/qmlsidebar.cpp
plugins/qml/api/menus/qmlmenu.cpp
plugins/qml/api/menus/qmlaction.cpp
plugins/qml/api/menus/qmlwebhittestresult.cpp
popupwindow/popuplocationbar.cpp popupwindow/popuplocationbar.cpp
popupwindow/popupstatusbarmessage.cpp popupwindow/popupstatusbarmessage.cpp
popupwindow/popupwebview.cpp popupwindow/popupwebview.cpp

View File

@ -0,0 +1,50 @@
/* ============================================================
* 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 "qmlaction.h"
#include "qztools.h"
QmlAction::QmlAction(QAction *action, QObject *parent)
: QObject(parent)
, m_action(action)
{
connect(m_action, &QAction::triggered, this, &QmlAction::triggered);
}
void QmlAction::setProperties(const QVariantMap &map)
{
if (!m_action) {
return;
}
for (const QString &key : map.keys()) {
if (key == QSL("icon")) {
QUrl url = map.value(key).toUrl();
QIcon icon(QzTools::getPathFromUrl(url));
m_action->setIcon(icon);
} else if (key == QSL("shortcut")) {
m_action->setShortcut(QKeySequence(map.value(key).toString()));
} else {
m_action->setProperty(key.toUtf8(), map.value(key));
}
}
}
void QmlAction::update(const QVariantMap &map)
{
setProperties(map);
}

View File

@ -0,0 +1,37 @@
/* ============================================================
* 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>
#include <QAction>
#include <QVariantMap>
class QmlAction : public QObject
{
Q_OBJECT
public:
explicit QmlAction(QAction *action, QObject *parent = nullptr);
void setProperties(const QVariantMap &map);
Q_INVOKABLE void update(const QVariantMap &map);
Q_SIGNALS:
void triggered();
private:
QAction *m_action;
};

View File

@ -0,0 +1,70 @@
/* ============================================================
* 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 "qmlmenu.h"
#include "qztools.h"
QmlMenu::QmlMenu(QMenu *menu, QObject *parent)
: QObject(parent)
, m_menu(menu)
{
connect(m_menu, &QMenu::triggered, this, &QmlMenu::triggered);
}
QmlAction *QmlMenu::addAction(const QVariantMap &map)
{
if (!m_menu) {
return nullptr;
}
QAction *action = new QAction();
QmlAction *qmlAction = new QmlAction(action, this);
qmlAction->setProperties(map);
m_menu->addAction(action);
return qmlAction;
}
QmlMenu *QmlMenu::addMenu(const QVariantMap &map)
{
if (!m_menu) {
return nullptr;
}
QMenu *newMenu = new QMenu();
for (const QString &key : map.keys()) {
if (key == QSL("icon")) {
QUrl url = map.value(key).toUrl();
QIcon icon(QzTools::getPathFromUrl(url));
newMenu->setIcon(icon);
continue;
}
newMenu->setProperty(key.toUtf8(), map.value(key));
}
m_menu->addMenu(newMenu);
QmlMenu *newQmlMenu = new QmlMenu(newMenu, this);
return newQmlMenu;
}
void QmlMenu::addSeparator()
{
if (!m_menu) {
return;
}
m_menu->addSeparator();
}

View File

@ -0,0 +1,37 @@
/* ============================================================
* 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 "qmlaction.h"
#include <QMenu>
class QmlMenu : public QObject
{
Q_OBJECT
public:
explicit QmlMenu(QMenu *menu, QObject *parent = nullptr);
Q_INVOKABLE QmlAction *addAction(const QVariantMap &map);
Q_INVOKABLE QmlMenu *addMenu(const QVariantMap &map);
Q_INVOKABLE void addSeparator();
Q_SIGNALS:
void triggered();
private:
QMenu *m_menu;
};

View File

@ -0,0 +1,71 @@
/* ============================================================
* 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 "qmlwebhittestresult.h"
#define NOT !
QmlWebHitTestResult::QmlWebHitTestResult(const WebHitTestResult &webHitTestResult, QObject *parent)
: QObject(parent)
, m_webHitTestResult(webHitTestResult)
{
}
bool QmlWebHitTestResult::isImage() const
{
return NOT m_webHitTestResult.imageUrl().isEmpty();
}
bool QmlWebHitTestResult::isContentEditable() const
{
return m_webHitTestResult.isContentEditable();
}
bool QmlWebHitTestResult::isContentSelected() const
{
return m_webHitTestResult.isContentSelected();
}
bool QmlWebHitTestResult::isNull() const
{
return m_webHitTestResult.isNull();
}
bool QmlWebHitTestResult::isLink() const
{
return NOT m_webHitTestResult.linkUrl().isEmpty();
}
bool QmlWebHitTestResult::isMedia() const
{
return NOT m_webHitTestResult.mediaUrl().isEmpty();
}
bool QmlWebHitTestResult::mediaPaused() const
{
return m_webHitTestResult.mediaPaused();
}
bool QmlWebHitTestResult::mediaMuted() const
{
return m_webHitTestResult.mediaMuted();
}
QString QmlWebHitTestResult::tagName() const
{
return m_webHitTestResult.tagName();
}

View File

@ -0,0 +1,40 @@
/* ============================================================
* 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 "webhittestresult.h"
#include <QObject>
class QmlWebHitTestResult : public QObject
{
Q_OBJECT
public:
explicit QmlWebHitTestResult(const WebHitTestResult &webHitTestResult, QObject *parent = nullptr);
Q_INVOKABLE bool isImage() const;
Q_INVOKABLE bool isContentEditable() const;
Q_INVOKABLE bool isContentSelected() const;
Q_INVOKABLE bool isNull() const;
Q_INVOKABLE bool isLink() const;
Q_INVOKABLE bool isMedia() const;
Q_INVOKABLE bool mediaPaused() const;
Q_INVOKABLE bool mediaMuted() const;
Q_INVOKABLE QString tagName() const;
private:
WebHitTestResult m_webHitTestResult;
};

View File

@ -22,6 +22,8 @@
#include "browserwindow.h" #include "browserwindow.h"
#include "navigationbar.h" #include "navigationbar.h"
#include "sidebar.h" #include "sidebar.h"
#include "api/menus/qmlmenu.h"
#include "api/menus/qmlwebhittestresult.h"
#include <QDebug> #include <QDebug>
QmlPluginInterface::QmlPluginInterface() QmlPluginInterface::QmlPluginInterface()
@ -32,7 +34,7 @@ QmlPluginInterface::QmlPluginInterface()
void QmlPluginInterface::init(InitState state, const QString &settingsPath) void QmlPluginInterface::init(InitState state, const QString &settingsPath)
{ {
if (!m_jsInit.isCallable()) { if (!m_init.isCallable()) {
qWarning() << "Unable to call" << __FUNCTION__ << "on" << m_name << "plugin"; qWarning() << "Unable to call" << __FUNCTION__ << "on" << m_name << "plugin";
return; return;
} }
@ -40,7 +42,7 @@ void QmlPluginInterface::init(InitState state, const QString &settingsPath)
QJSValueList args; QJSValueList args;
args.append(state); args.append(state);
args.append(settingsPath); args.append(settingsPath);
m_jsInit.call(args); m_init.call(args);
if (m_browserAction) { if (m_browserAction) {
for (BrowserWindow *window : mApp->windows()) { for (BrowserWindow *window : mApp->windows()) {
@ -62,12 +64,12 @@ DesktopFile QmlPluginInterface::metaData() const
void QmlPluginInterface::unload() void QmlPluginInterface::unload()
{ {
if (!m_jsUnload.isCallable()) { if (!m_unload.isCallable()) {
qWarning() << "Unable to call" << __FUNCTION__ << "on" << m_name << "plugin"; qWarning() << "Unable to call" << __FUNCTION__ << "on" << m_name << "plugin";
return; return;
} }
m_jsUnload.call(); m_unload.call();
if (m_browserAction) { if (m_browserAction) {
for (BrowserWindow *window : mApp->windows()) { for (BrowserWindow *window : mApp->windows()) {
@ -86,43 +88,69 @@ void QmlPluginInterface::unload()
bool QmlPluginInterface::testPlugin() bool QmlPluginInterface::testPlugin()
{ {
if (!m_jsTestPlugin.isCallable()) { if (!m_testPlugin.isCallable()) {
qWarning() << "Unable to call" << __FUNCTION__ << "on" << m_name << "plugin"; qWarning() << "Unable to call" << __FUNCTION__ << "on" << m_name << "plugin";
return false; return false;
} }
QJSValue ret = m_jsTestPlugin.call(); QJSValue ret = m_testPlugin.call();
return ret.toBool(); return ret.toBool();
} }
QJSValue QmlPluginInterface::jsInit() const void QmlPluginInterface::populateWebViewMenu(QMenu *menu, WebView *webview, const WebHitTestResult &webHitTestResult)
{ {
return m_jsInit; Q_UNUSED(webview)
if (!m_populateWebViewMenu.isCallable()) {
return;
}
QmlMenu *qmlMenu = new QmlMenu(menu);
QmlWebHitTestResult *qmlWebHitTestResult = new QmlWebHitTestResult(webHitTestResult);
QJSValueList args;
args.append(m_engine->newQObject(qmlMenu));
args.append(m_engine->newQObject(qmlWebHitTestResult));
m_populateWebViewMenu.call(args);
menu->addSeparator();
qmlMenu->deleteLater();
qmlWebHitTestResult->deleteLater();
} }
void QmlPluginInterface::setJsInit(const QJSValue &init) QJSValue QmlPluginInterface::readInit() const
{ {
m_jsInit = init; return m_init;
} }
QJSValue QmlPluginInterface::jsUnload() const void QmlPluginInterface::setInit(const QJSValue &init)
{ {
return m_jsUnload; m_init = init;
} }
void QmlPluginInterface::setJsUnload(const QJSValue &unload) QJSValue QmlPluginInterface::readUnload() const
{ {
m_jsUnload = unload; return m_unload;
} }
QJSValue QmlPluginInterface::jsTestPlugin() const void QmlPluginInterface::setUnload(const QJSValue &unload)
{ {
return m_jsTestPlugin; m_unload = unload;
} }
void QmlPluginInterface::setJsTestPlugin(const QJSValue &testPlugin) QJSValue QmlPluginInterface::readTestPlugin() const
{ {
m_jsTestPlugin = testPlugin; return m_testPlugin;
}
void QmlPluginInterface::setTestPlugin(const QJSValue &testPlugin)
{
m_testPlugin = testPlugin;
}
void QmlPluginInterface::setEngine(QQmlEngine *engine)
{
m_engine = engine;
} }
void QmlPluginInterface::setName(const QString &name) void QmlPluginInterface::setName(const QString &name)
@ -150,6 +178,16 @@ void QmlPluginInterface::setSideBar(QmlSideBar *sideBar)
m_sideBar = sideBar; m_sideBar = sideBar;
} }
QJSValue QmlPluginInterface::readPopulateWebViewMenu() const
{
return m_populateWebViewMenu;
}
void QmlPluginInterface::setPopulateWebViewMenu(const QJSValue &value)
{
m_populateWebViewMenu = value;
}
void QmlPluginInterface::addButton(BrowserWindow *window) void QmlPluginInterface::addButton(BrowserWindow *window)
{ {
if (m_browserAction->location().testFlag(QmlBrowserAction::NavigationToolBar)) { if (m_browserAction->location().testFlag(QmlBrowserAction::NavigationToolBar)) {

View File

@ -30,11 +30,12 @@ class QmlPluginInterface : public QObject, public PluginInterface
Q_OBJECT Q_OBJECT
Q_INTERFACES(PluginInterface) Q_INTERFACES(PluginInterface)
Q_ENUMS(InitState) Q_ENUMS(InitState)
Q_PROPERTY(QJSValue init READ jsInit WRITE setJsInit) Q_PROPERTY(QJSValue init READ readInit WRITE setInit)
Q_PROPERTY(QJSValue unload READ jsUnload WRITE setJsUnload) Q_PROPERTY(QJSValue unload READ readUnload WRITE setUnload)
Q_PROPERTY(QJSValue testPlugin READ jsTestPlugin WRITE setJsTestPlugin) Q_PROPERTY(QJSValue testPlugin READ readTestPlugin WRITE setTestPlugin)
Q_PROPERTY(QmlBrowserAction* browserAction READ browserAction WRITE setBrowserAction) Q_PROPERTY(QmlBrowserAction* browserAction READ browserAction WRITE setBrowserAction)
Q_PROPERTY(QmlSideBar* sideBar READ sideBar WRITE setSideBar) Q_PROPERTY(QmlSideBar* sideBar READ sideBar WRITE setSideBar)
Q_PROPERTY(QJSValue populateWebViewMenu READ readPopulateWebViewMenu WRITE setPopulateWebViewMenu)
public: public:
explicit QmlPluginInterface(); explicit QmlPluginInterface();
@ -42,30 +43,35 @@ public:
void init(InitState state, const QString &settingsPath); void init(InitState state, const QString &settingsPath);
void unload(); void unload();
bool testPlugin(); bool testPlugin();
void setEngine(QQmlEngine *engine);
void setName(const QString &name); void setName(const QString &name);
void populateWebViewMenu(QMenu *menu, WebView *webview, const WebHitTestResult &webHitTestResult) override;
Q_SIGNALS: Q_SIGNALS:
void qmlPluginUnloaded(); void qmlPluginUnloaded();
private: private:
QQmlEngine *m_engine;
QString m_name; QString m_name;
QJSValue m_jsInit; QJSValue m_init;
QJSValue m_jsUnload; QJSValue m_unload;
QJSValue m_jsTestPlugin; QJSValue m_testPlugin;
QmlBrowserAction *m_browserAction; QmlBrowserAction *m_browserAction;
QmlSideBar *m_sideBar; QmlSideBar *m_sideBar;
bool loaded; QJSValue m_populateWebViewMenu;
QJSValue jsInit() const; QJSValue readInit() const;
void setJsInit(const QJSValue &init); void setInit(const QJSValue &init);
QJSValue jsUnload() const; QJSValue readUnload() const;
void setJsUnload(const QJSValue &unload); void setUnload(const QJSValue &unload);
QJSValue jsTestPlugin() const; QJSValue readTestPlugin() const;
void setJsTestPlugin(const QJSValue &testPlugin); void setTestPlugin(const QJSValue &testPlugin);
QmlBrowserAction *browserAction() const; QmlBrowserAction *browserAction() const;
void setBrowserAction(QmlBrowserAction *browserAction); void setBrowserAction(QmlBrowserAction *browserAction);
QmlSideBar *sideBar() const; QmlSideBar *sideBar() const;
void setSideBar(QmlSideBar *sideBar); void setSideBar(QmlSideBar *sideBar);
QJSValue readPopulateWebViewMenu() const;
void setPopulateWebViewMenu(const QJSValue &value);
void addButton(BrowserWindow *window); void addButton(BrowserWindow *window);
void removeButton(BrowserWindow *window); void removeButton(BrowserWindow *window);

View File

@ -27,6 +27,7 @@ QmlPluginLoader::QmlPluginLoader(const QString &path)
void QmlPluginLoader::createComponent() void QmlPluginLoader::createComponent()
{ {
m_interface = qobject_cast<QmlPluginInterface*>(m_component->create()); m_interface = qobject_cast<QmlPluginInterface*>(m_component->create());
m_interface->setEngine(m_engine);
connect(m_interface, &QmlPluginInterface::qmlPluginUnloaded, this, [this]{ connect(m_interface, &QmlPluginInterface::qmlPluginUnloaded, this, [this]{
delete m_component; delete m_component;
delete m_engine; delete m_engine;

View File

@ -35,6 +35,9 @@
#include "api/windows/qmlwindowtype.h" #include "api/windows/qmlwindowtype.h"
#include "api/browseraction/qmlbrowseraction.h" #include "api/browseraction/qmlbrowseraction.h"
#include "api/sidebar/qmlsidebar.h" #include "api/sidebar/qmlsidebar.h"
#include "api/menus/qmlmenu.h"
#include "api/menus/qmlaction.h"
#include "api/menus/qmlwebhittestresult.h"
#include <QQmlEngine> #include <QQmlEngine>
@ -137,4 +140,13 @@ void QmlPlugins::registerQmlTypes()
// SideBar // SideBar
qmlRegisterType<QmlSideBar>("org.kde.falkon", 1, 0, "SideBar"); qmlRegisterType<QmlSideBar>("org.kde.falkon", 1, 0, "SideBar");
// Menu
qmlRegisterUncreatableType<QmlMenu>("org.kde.falkon", 1, 0, "Menu", "Unable to register type: Menu");
// Action
qmlRegisterUncreatableType<QmlAction>("org.kde.falkon", 1, 0, "Action", "Unable to register type: Action");
// WebHitTestResult
qmlRegisterUncreatableType<QmlWebHitTestResult>("org.kde.falkon", 1, 0, "WebHitTestResult", "Unable to register type: WebHitTestResult");
} }