Changeset View
Changeset View
Standalone View
Standalone View
kstyle/demo/oxygenschemechooser.cpp
- This file was added.
1 | /************************************************************************************* | ||||
---|---|---|---|---|---|
2 | * This file was part of KDevPlatform and now of Oxygen * | ||||
3 | * Copyright 2016 Zhigalin Alexander <alexander@zhigalin.tk> * | ||||
4 | * Copyright 2017 René J.V. Bertin <rjvbertin@gmail.com> * | ||||
5 | * * | ||||
6 | * This library is free software; you can redistribute it and/or * | ||||
7 | * modify it under the terms of the GNU Lesser General Public * | ||||
8 | * License as published by the Free Software Foundation; either * | ||||
9 | * version 2.1 of the License, or (at your option) version 3, or any * | ||||
10 | * later version accepted by the membership of KDE e.V. (or its * | ||||
11 | * successor approved by the membership of KDE e.V.), which shall * | ||||
12 | * act as a proxy defined in Section 6 of version 3 of the license. * | ||||
13 | * * | ||||
14 | * This library is distributed in the hope that it will be useful, * | ||||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * | ||||
17 | * Lesser General Public License for more details. * | ||||
18 | * * | ||||
19 | * You should have received a copy of the GNU Lesser General Public * | ||||
20 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. * | ||||
21 | *************************************************************************************/ | ||||
22 | | ||||
23 | #include "oxygenschemechooser.h" | ||||
24 | | ||||
25 | #include <QStringList> | ||||
26 | #include <QActionGroup> | ||||
27 | #include <QAction> | ||||
28 | #include <QMenu> | ||||
29 | #include <QModelIndex> | ||||
30 | | ||||
31 | #include <KActionMenu> | ||||
32 | #include <KColorSchemeManager> | ||||
33 | #include <KLocalizedString> | ||||
34 | #include <KSharedConfig> | ||||
35 | #include <KConfigGroup> | ||||
36 | | ||||
37 | namespace Oxygen | ||||
38 | { | ||||
39 | | ||||
40 | ColorSchemeChooser::ColorSchemeChooser(QWidget* parent) | ||||
41 | : QPushButton(parent) | ||||
42 | { | ||||
43 | auto manager = new KColorSchemeManager(parent); | ||||
44 | | ||||
45 | const auto scheme(currentSchemeName()); | ||||
46 | | ||||
47 | auto selectionMenu = manager->createSchemeSelectionMenu(scheme, this); | ||||
48 | | ||||
49 | connect(selectionMenu->menu(), &QMenu::triggered, | ||||
50 | this, &ColorSchemeChooser::slotSchemeChanged); | ||||
51 | | ||||
52 | manager->activateScheme(manager->indexForScheme(scheme)); | ||||
53 | | ||||
54 | setMenu(selectionMenu->menu()); | ||||
55 | menu()->setIcon(QIcon::fromTheme(QStringLiteral("preferences-desktop-color"))); | ||||
56 | menu()->setTitle(i18n("&Color Theme")); | ||||
57 | setIcon(menu()->icon()); | ||||
58 | setText(menu()->title()); | ||||
59 | } | ||||
60 | | ||||
61 | QString ColorSchemeChooser::loadCurrentScheme() const | ||||
62 | { | ||||
63 | KSharedConfigPtr config = KSharedConfig::openConfig(); | ||||
64 | #ifdef SUPPORT_THEME_SAVING | ||||
65 | KConfigGroup cg(config, "UiSettings"); | ||||
66 | return cg.readEntry("ColorScheme", currentDesktopDefaultScheme()); | ||||
67 | #else | ||||
68 | return currentDesktopDefaultScheme(); | ||||
69 | #endif | ||||
70 | } | ||||
71 | | ||||
72 | #ifdef SUPPORT_THEME_SAVING | ||||
73 | void ColorSchemeChooser::saveCurrentScheme(const QString &name) | ||||
74 | { | ||||
75 | KSharedConfigPtr config = KSharedConfig::openConfig(); | ||||
76 | KConfigGroup cg(config, "UiSettings"); | ||||
77 | cg.writeEntry("ColorScheme", name); | ||||
78 | cg.sync(); | ||||
79 | } | ||||
80 | #endif | ||||
81 | | ||||
82 | QString ColorSchemeChooser::currentDesktopDefaultScheme() const | ||||
83 | { | ||||
84 | KSharedConfigPtr config = KSharedConfig::openConfig(QLatin1String("kdeglobals")); | ||||
85 | KConfigGroup group(config, "General"); | ||||
86 | return group.readEntry("ColorScheme", QStringLiteral("Breeze")); | ||||
87 | } | ||||
88 | | ||||
89 | QString ColorSchemeChooser::currentSchemeName() const | ||||
90 | { | ||||
91 | if(!menu()) return loadCurrentScheme(); | ||||
92 | | ||||
93 | QAction* const action = menu()->activeAction(); | ||||
94 | | ||||
95 | if(action) return KLocalizedString::removeAcceleratorMarker(action->text()); | ||||
96 | return currentDesktopDefaultScheme(); | ||||
97 | } | ||||
98 | | ||||
99 | void ColorSchemeChooser::slotSchemeChanged(QAction* triggeredAction) | ||||
100 | { | ||||
101 | #ifdef SUPPORT_THEME_SAVING | ||||
102 | saveCurrentScheme(KLocalizedString::removeAcceleratorMarker(triggeredAction->text())); | ||||
103 | #else | ||||
104 | Q_UNUSED(triggeredAction); | ||||
105 | #endif | ||||
106 | } | ||||
107 | | ||||
108 | } // namespace Oxygen |