File indexing completed on 2024-07-21 03:39:35

0001 /*
0002     SPDX-FileCopyrightText: 2013 Martin Gräßlin <mgraesslin@kde.org>
0003     SPDX-FileCopyrightText: 2023 David Redondo <kde@david-redondo.de>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "kcolorschememenu.h"
0009 
0010 #include <KActionMenu>
0011 #include <KColorSchemeManager>
0012 #include <KColorSchemeModel>
0013 #include <KLocalizedString>
0014 
0015 #include <QActionGroup>
0016 #include <QIcon>
0017 #include <QMenu>
0018 
0019 constexpr int defaultSchemeRow = 0;
0020 
0021 KActionMenu *KColorSchemeMenu::createMenu(KColorSchemeManager *manager, QObject *parent)
0022 {
0023     // Be careful here when connecting to signals. The menu can outlive the manager
0024     KActionMenu *menu = new KActionMenu(QIcon::fromTheme(QStringLiteral("preferences-desktop-color")), i18n("Color Scheme"), parent);
0025     QActionGroup *group = new QActionGroup(menu);
0026     QObject::connect(group, &QActionGroup::triggered, manager, [manager](QAction *action) {
0027         const QString schemePath = action->data().toString();
0028         if (schemePath.isEmpty()) {
0029             // Reset to default
0030             manager->activateScheme(QModelIndex());
0031         } else {
0032             // Software linking KXmlGui gets its static KCheckAccelerators object deploy
0033             // KAcceleratorManager on the whole UI automatically, which adds accelerators
0034             // also to the texts of this menu's actions.
0035             // So they have to be remove here in case
0036             // (hoping that no original names have them also in case no accelerators were added)
0037             // See also KColorSchemeManager::saveSchemeToConfigFile(const QString &schemeName) const.
0038             const QString schemeName = KLocalizedString::removeAcceleratorMarker(action->text());
0039             manager->activateScheme(manager->indexForScheme(schemeName));
0040         }
0041     });
0042     const auto model = manager->model();
0043     for (int i = 0; i < model->rowCount(); ++i) {
0044         QModelIndex index = model->index(i, 0);
0045         QAction *action = new QAction(index.data(KColorSchemeModel::NameRole).toString(), menu);
0046         action->setData(index.data(KColorSchemeModel::PathRole));
0047         action->setActionGroup(group);
0048         action->setCheckable(true);
0049         if (index.data(KColorSchemeModel::IdRole).toString() == manager->activeSchemeId()) {
0050             action->setChecked(true);
0051         }
0052         menu->addAction(action);
0053         QObject::connect(menu->menu(), &QMenu::aboutToShow, model, [action, index] {
0054             if (action->icon().isNull()) {
0055                 action->setIcon(index.data(KColorSchemeModel::IconRole).value<QIcon>());
0056             }
0057         });
0058     }
0059     const auto groupActions = group->actions();
0060     if (!group->checkedAction()) {
0061         // If no (valid) color scheme has been selected we select the default one
0062         groupActions[defaultSchemeRow]->setChecked(true);
0063     }
0064 
0065     return menu;
0066 }