File indexing completed on 2024-12-22 04:41:14

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "qmlmenu.h"
0019 #include "qztools.h"
0020 #include "qml/api/fileutils/qmlfileutils.h"
0021 #include "qml/qmlengine.h"
0022 #include "qml/qmlstaticdata.h"
0023 
0024 QmlMenu::QmlMenu(QMenu *menu, QQmlEngine *engine, QObject *parent)
0025     : QObject(parent)
0026     , m_menu(menu)
0027 {
0028     QQmlEngine::setObjectOwnership(this, QQmlEngine::JavaScriptOwnership);
0029 
0030     m_engine = qobject_cast<QmlEngine*>(engine);
0031     m_pluginPath = m_engine->extensionPath();
0032     connect(m_menu, &QMenu::triggered, this, &QmlMenu::triggered);
0033 }
0034 
0035 QmlAction *QmlMenu::addAction(const QVariantMap &map)
0036 {
0037     if (!m_menu) {
0038         return nullptr;
0039     }
0040 
0041     auto *action = new QAction();
0042     auto *qmlAction = new QmlAction(action, m_engine, this);
0043     qmlAction->setProperties(map);
0044     m_menu->addAction(action);
0045 
0046     return qmlAction;
0047 }
0048 
0049 QmlMenu *QmlMenu::addMenu(const QVariantMap &map)
0050 {
0051     if (!m_menu) {
0052         return nullptr;
0053     }
0054 
0055     auto *newMenu = new QMenu();
0056     for (auto it = map.cbegin(); it != map.cend(); it++) {
0057         const QString key = it.key();
0058         if (key == QSL("icon")) {
0059             const QString iconPath = map.value(key).toString();
0060             const QIcon icon = QmlStaticData::instance().getIcon(iconPath, m_pluginPath);
0061             newMenu->setIcon(icon);
0062             continue;
0063         }
0064         newMenu->setProperty(key.toUtf8().constData(), map.value(key));
0065     }
0066     m_menu->addMenu(newMenu);
0067     auto *newQmlMenu = new QmlMenu(newMenu, m_engine, this);
0068     return newQmlMenu;
0069 }
0070 
0071 void QmlMenu::addSeparator()
0072 {
0073     if (!m_menu) {
0074         return;
0075     }
0076 
0077     m_menu->addSeparator();
0078 }