File indexing completed on 2024-06-23 05:14:14

0001 /* -*- mode: c++; c-basic-offset:4 -*-
0002     utils/action_data.cpp
0003 
0004     This file is part of Kleopatra, the KDE keymanager
0005     SPDX-FileCopyrightText: 2007 Klarälvdalens Datakonsult AB
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include <config-kleopatra.h>
0011 
0012 #include "action_data.h"
0013 
0014 #include <KActionCollection>
0015 #include <KToggleAction>
0016 #include <QAction>
0017 #include <QIcon>
0018 #include <QKeySequence>
0019 
0020 QAction *Kleo::createAction(const action_data &ad, KActionCollection *coll)
0021 {
0022     QAction *const a = ad.actionType == KFToggleAction ? new KToggleAction(coll) : new QAction(coll);
0023     a->setObjectName(QLatin1StringView(ad.name));
0024     a->setText(ad.text);
0025     if (!ad.tooltip.isEmpty()) {
0026         a->setToolTip(ad.tooltip);
0027     }
0028     if (ad.icon) {
0029         a->setIcon(QIcon::fromTheme(QLatin1StringView(ad.icon)));
0030     }
0031     if (ad.receiver && ad.func) {
0032         if (ad.actionType == KFToggleAction) {
0033             QObject::connect(a, &KToggleAction::toggled, ad.receiver, ad.func);
0034         } else {
0035             QObject::connect(a, &QAction::triggered, ad.receiver, ad.func);
0036         }
0037     }
0038     a->setEnabled(ad.actionState == Enabled);
0039     coll->addAction(QLatin1StringView(ad.name), a);
0040     return a;
0041 }
0042 
0043 QAction *Kleo::make_action_from_data(const action_data &ad, KActionCollection *coll)
0044 {
0045     QAction *const a = createAction(ad, coll);
0046     if (!ad.shortcut.isEmpty()) {
0047         coll->setDefaultShortcut(a, QKeySequence(ad.shortcut));
0048     }
0049     return a;
0050 }
0051 
0052 void Kleo::make_actions_from_data(const std::vector<action_data> &data, KActionCollection *coll)
0053 {
0054     for (const auto &actionData : data) {
0055         coll->addAction(QLatin1StringView(actionData.name), make_action_from_data(actionData, coll));
0056     }
0057 }