Add AppColorSchemeChooser class which use KColorSchemeManager to change konsole color scheme. Inspired by KateColorSchemeChooser wrote by Zhigalin Alexander <alexander@zhigalin.tk>.wilder
parent
179b173921
commit
dc8ad83043
5 changed files with 127 additions and 1 deletions
@ -0,0 +1,79 @@ |
||||
/*************************************************************************************
|
||||
* SPDX-FileCopyrightText: 2016 Zhigalin Alexander <alexander@zhigalin.tk> |
||||
* SPDX-FileCopyrightText: 2021 DI DIO Maximilien <maximilien.didio@gmail.com> |
||||
* |
||||
* SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
||||
*************************************************************************************/ |
||||
|
||||
#include "AppColorSchemeChooser.h" |
||||
#include "konsoledebug.h" |
||||
|
||||
#include <QActionGroup> |
||||
#include <QDirIterator> |
||||
#include <QFileInfo> |
||||
#include <QMenu> |
||||
#include <QModelIndex> |
||||
#include <QStandardPaths> |
||||
#include <QStringList> |
||||
#include <QtGlobal> |
||||
|
||||
#include <KActionCollection> |
||||
#include <KActionMenu> |
||||
#include <KConfigGroup> |
||||
#include <KLocalizedString> |
||||
#include <KSharedConfig> |
||||
|
||||
#include "MainWindow.h" |
||||
|
||||
AppColorSchemeChooser::AppColorSchemeChooser(QObject *parent) |
||||
: QAction(parent) |
||||
{ |
||||
auto manager = new KColorSchemeManager(parent); |
||||
|
||||
const auto scheme(currentSchemeName()); |
||||
qCDebug(KonsoleDebug) << "Color scheme : " << scheme; |
||||
|
||||
auto selectionMenu = manager->createSchemeSelectionMenu(scheme, this); |
||||
|
||||
connect(selectionMenu->menu(), &QMenu::triggered, this, &AppColorSchemeChooser::slotSchemeChanged); |
||||
|
||||
manager->activateScheme(manager->indexForScheme(scheme)); |
||||
|
||||
setMenu(selectionMenu->menu()); |
||||
menu()->setIcon(QIcon::fromTheme(QStringLiteral("preferences-desktop-color"))); |
||||
menu()->setTitle(i18n("&Color Scheme")); |
||||
} |
||||
|
||||
QString AppColorSchemeChooser::loadCurrentScheme() const |
||||
{ |
||||
KSharedConfigPtr config = KSharedConfig::openConfig(); |
||||
KConfigGroup cg(config, "UiSettings"); |
||||
return cg.readEntry("ColorScheme", QString()); |
||||
} |
||||
|
||||
void AppColorSchemeChooser::saveCurrentScheme(const QString &name) |
||||
{ |
||||
KSharedConfigPtr config = KSharedConfig::openConfig(); |
||||
KConfigGroup cg(config, "UiSettings"); |
||||
cg.writeEntry("ColorScheme", name); |
||||
cg.sync(); |
||||
} |
||||
|
||||
QString AppColorSchemeChooser::currentSchemeName() const |
||||
{ |
||||
if (!menu()) { |
||||
return loadCurrentScheme(); |
||||
} |
||||
|
||||
QAction *const action = menu()->activeAction(); |
||||
|
||||
if (action) { |
||||
return KLocalizedString::removeAcceleratorMarker(action->text()); |
||||
} |
||||
return QString(); |
||||
} |
||||
|
||||
void AppColorSchemeChooser::slotSchemeChanged(QAction *triggeredAction) |
||||
{ |
||||
saveCurrentScheme(KLocalizedString::removeAcceleratorMarker(triggeredAction->text())); |
||||
} |
||||
@ -0,0 +1,42 @@ |
||||
/*************************************************************************************
|
||||
* SPDX-FileCopyrightText: 2016 Zhigalin Alexander <alexander@zhigalin.tk> |
||||
* SPDX-FileCopyrightText: 2021 DI DIO Maximilien <maximilien.didio@gmail.com> |
||||
* |
||||
* SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
||||
*************************************************************************************/ |
||||
|
||||
#ifndef KONSOLE_COLOR_SCHEME_CHOOSER_H |
||||
#define KONSOLE_COLOR_SCHEME_CHOOSER_H |
||||
|
||||
#include <QAction> |
||||
#include <QApplication> |
||||
#include <QObject> |
||||
#include <QString> |
||||
|
||||
#include <KColorSchemeManager> |
||||
|
||||
// Konsole
|
||||
#include "konsoleprivate_export.h" |
||||
|
||||
class KActionCollection; |
||||
|
||||
/**
|
||||
* Provides a menu that will offer to change the color scheme |
||||
* |
||||
* Furthermore, it will save the selection in the user configuration. |
||||
*/ |
||||
class KONSOLEPRIVATE_EXPORT AppColorSchemeChooser : public QAction |
||||
{ |
||||
public: |
||||
AppColorSchemeChooser(QObject *parent); |
||||
|
||||
QString currentSchemeName() const; |
||||
private Q_SLOTS: |
||||
void slotSchemeChanged(QAction *triggeredAction); |
||||
|
||||
private: |
||||
QString loadCurrentScheme() const; |
||||
void saveCurrentScheme(const QString &name); |
||||
}; |
||||
|
||||
#endif |
||||
Loading…
Reference in new issue