File indexing completed on 2025-01-05 04:34:48

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 "qmlaction.h"
0019 #include "qztools.h"
0020 #include "qml/api/fileutils/qmlfileutils.h"
0021 #include "qml/qmlengine.h"
0022 #include "qml/qmlstaticdata.h"
0023 #include <QQmlEngine>
0024 
0025 QmlAction::QmlAction(QAction *action, QmlEngine *engine, QObject *parent)
0026     : QObject(parent)
0027     , m_action(action)
0028 {
0029     auto *qmlEngine = qobject_cast<QmlEngine*>(engine);
0030     m_pluginPath = qmlEngine->extensionPath();
0031     connect(m_action, &QAction::triggered, this, &QmlAction::triggered);
0032 }
0033 
0034 void QmlAction::setProperties(const QVariantMap &map)
0035 {
0036     if (!m_action) {
0037         return;
0038     }
0039 
0040     for (auto it = map.cbegin(); it != map.cend(); it++) {
0041         const QString key = it.key();
0042         if (key == QSL("icon")) {
0043             QString iconPath = map.value(key).toString();
0044             QIcon icon = QmlStaticData::instance().getIcon(iconPath, m_pluginPath);
0045             m_action->setIcon(icon);
0046         } else if (key == QSL("shortcut")) {
0047             m_action->setShortcut(QKeySequence(map.value(key).toString()));
0048         } else {
0049             m_action->setProperty(key.toUtf8().constData(), map.value(key));
0050         }
0051     }
0052 }
0053 
0054 void  QmlAction::update(const QVariantMap &map)
0055 {
0056     setProperties(map);
0057 }