KConfigWidgets

kcolorschememenu.cpp
1/*
2 SPDX-FileCopyrightText: 2013 Martin Gräßlin <mgraesslin@kde.org>
3 SPDX-FileCopyrightText: 2023 David Redondo <kde@david-redondo.de>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "kcolorschememenu.h"
9
10#include <KActionMenu>
11#include <KColorSchemeManager>
12#include <KColorSchemeModel>
13#include <KLocalizedString>
14
15#include <QActionGroup>
16#include <QIcon>
17#include <QMenu>
18
19constexpr int defaultSchemeRow = 0;
20
22{
23 // Be careful here when connecting to signals. The menu can outlive the manager
24 KActionMenu *menu = new KActionMenu(QIcon::fromTheme(QStringLiteral("preferences-desktop-color")), i18n("Color Scheme"), parent);
25 QActionGroup *group = new QActionGroup(menu);
26 QObject::connect(group, &QActionGroup::triggered, manager, [manager](QAction *action) {
27 const QString schemePath = action->data().toString();
28 if (schemePath.isEmpty()) {
29 // Reset to default
30 manager->activateScheme(QModelIndex());
31 } else {
32 // Software linking KXmlGui gets its static KCheckAccelerators object deploy
33 // KAcceleratorManager on the whole UI automatically, which adds accelerators
34 // also to the texts of this menu's actions.
35 // So they have to be remove here in case
36 // (hoping that no original names have them also in case no accelerators were added)
37 // See also KColorSchemeManager::saveSchemeToConfigFile(const QString &schemeName) const.
38 const QString schemeName = KLocalizedString::removeAcceleratorMarker(action->text());
39 manager->activateScheme(manager->indexForScheme(schemeName));
40 }
41 });
42 const auto model = manager->model();
43 for (int i = 0; i < model->rowCount(); ++i) {
44 QModelIndex index = model->index(i, 0);
45 QAction *action = new QAction(index.data(KColorSchemeModel::NameRole).toString(), menu);
46 action->setData(index.data(KColorSchemeModel::PathRole));
47 action->setActionGroup(group);
48 action->setCheckable(true);
49 if (index.data(KColorSchemeModel::IdRole).toString() == manager->activeSchemeId()) {
50 action->setChecked(true);
51 }
52 menu->addAction(action);
53 QObject::connect(menu->menu(), &QMenu::aboutToShow, model, [action, index] {
54 if (action->icon().isNull()) {
55 action->setIcon(index.data(KColorSchemeModel::IconRole).value<QIcon>());
56 }
57 });
58 }
59 const auto groupActions = group->actions();
60 if (!group->checkedAction()) {
61 // If no (valid) color scheme has been selected we select the default one
62 groupActions[defaultSchemeRow]->setChecked(true);
63 }
64
65 return menu;
66}
void addAction(QAction *action)
QAbstractItemModel * model() const
QString activeSchemeId() const
QString i18n(const char *text, const TYPE &arg...)
KActionMenu * createMenu(KColorSchemeManager *manager, QObject *parent=nullptr)
Creates a KActionMenu populated with all the available color schemes.
void setCheckable(bool)
void setChecked(bool)
QVariant data() const const
QMenu * menu() const const
void setActionGroup(QActionGroup *group)
void setData(const QVariant &data)
QIcon fromTheme(const QString &name)
void aboutToShow()
QVariant data(int role) const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool isEmpty() const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 3 2024 11:43:44 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.