Falkon Develop
Cross-platform Qt-based web browser
qmlmenu.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 "qmlmenu.h"
19#include "qztools.h"
21#include "qml/qmlengine.h"
22#include "qml/qmlstaticdata.h"
23
24QmlMenu::QmlMenu(QMenu *menu, QQmlEngine *engine, QObject *parent)
25 : QObject(parent)
26 , m_menu(menu)
27{
28 QQmlEngine::setObjectOwnership(this, QQmlEngine::JavaScriptOwnership);
29
30 m_engine = qobject_cast<QmlEngine*>(engine);
31 m_pluginPath = m_engine->extensionPath();
32 connect(m_menu, &QMenu::triggered, this, &QmlMenu::triggered);
33}
34
35QmlAction *QmlMenu::addAction(const QVariantMap &map)
36{
37 if (!m_menu) {
38 return nullptr;
39 }
40
41 auto *action = new QAction();
42 auto *qmlAction = new QmlAction(action, m_engine, this);
43 qmlAction->setProperties(map);
44 m_menu->addAction(action);
45
46 return qmlAction;
47}
48
49QmlMenu *QmlMenu::addMenu(const QVariantMap &map)
50{
51 if (!m_menu) {
52 return nullptr;
53 }
54
55 auto *newMenu = new QMenu();
56 for (auto it = map.cbegin(); it != map.cend(); it++) {
57 const QString key = it.key();
58 if (key == QSL("icon")) {
59 const QString iconPath = map.value(key).toString();
60 const QIcon icon = QmlStaticData::instance().getIcon(iconPath, m_pluginPath);
61 newMenu->setIcon(icon);
62 continue;
63 }
64 newMenu->setProperty(key.toUtf8().constData(), map.value(key));
65 }
66 m_menu->addMenu(newMenu);
67 auto *newQmlMenu = new QmlMenu(newMenu, m_engine, this);
68 return newQmlMenu;
69}
70
72{
73 if (!m_menu) {
74 return;
75 }
76
77 m_menu->addSeparator();
78}
The class exposing Action API to QML.
Definition: qmlaction.h:30
QString extensionPath() const
Definition: qmlengine.cpp:35
The class exposing WebView contextmenu to QML as Menu API.
Definition: qmlmenu.h:30
QmlMenu(QMenu *menu, QQmlEngine *engine, QObject *parent=nullptr)
Definition: qmlmenu.cpp:24
void triggered()
This signal is emitted when the menu is triggred.
Q_INVOKABLE void addSeparator()
Adds a separator to menu.
Definition: qmlmenu.cpp:71
Q_INVOKABLE QmlAction * addAction(const QVariantMap &map)
Adds action to menu.
Definition: qmlmenu.cpp:35
Q_INVOKABLE QmlMenu * addMenu(const QVariantMap &map)
Adds sub-menu to menu.
Definition: qmlmenu.cpp:49
static QmlStaticData & instance()
QIcon getIcon(const QString &iconPath, const QString &pluginPath)
#define QSL(x)
Definition: qzcommon.h:40