Commit dc8ad830 authored by Maximilien DI DIO's avatar Maximilien DI DIO Committed by Ahmad Samir
Browse files

feat(AppColorSchemeChooser): User is able to change konsole color scheme

Add AppColorSchemeChooser class which use KColorSchemeManager to change
konsole color scheme. Inspired by KateColorSchemeChooser wrote by
Zhigalin Alexander <alexander@zhigalin.tk>.
parent 179b1739
<?xml version="1.0"?>
<!DOCTYPE gui SYSTEM "kpartgui.dtd">
<gui name="konsole" version="18">
<gui name="konsole" version="19">
<MenuBar>
<Menu name="file"><text>File</text>
<Action name="new-window"/>
......@@ -39,6 +39,7 @@
<DefineGroup name="session-settings"/>
<Action name="manage-profiles"/>
<Action name="show-menubar"/>
<Action name="colorscheme-menu"/>
<Separator/>
<Action name="view-full-screen"/>
<Separator/>
......
/*************************************************************************************
* 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()));
}
/*************************************************************************************
* 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
......@@ -109,6 +109,7 @@ add_subdirectory(decoders)
add_subdirectory(plugins)
set(konsoleprivate_SRCS ${windowadaptors_SRCS}
AppColorSchemeChooser.cpp
BookmarkHandler.cpp
BookmarkMenu.cpp
CheckableSessionModel.cpp
......
......@@ -6,6 +6,7 @@
// Own
#include "MainWindow.h"
#include "AppColorSchemeChooser.h"
// Qt
#include <QMenu>
......@@ -336,6 +337,8 @@ void MainWindow::setupActions()
// Settings Menu
_toggleMenuBarAction = KStandardAction::showMenubar(menuBar(), &QMenuBar::setVisible, collection);
collection->setDefaultShortcut(_toggleMenuBarAction, Konsole::ACCEL | Qt::SHIFT | Qt::Key_M);
// Set up themes
actionCollection()->addAction(QStringLiteral("colorscheme-menu"), new AppColorSchemeChooser(actionCollection()));
// Full Screen
menuAction = KStandardAction::fullScreen(this, &MainWindow::viewFullScreen, this, collection);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment