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