Commit 27e89b79 authored by Julius Künzel's avatar Julius Künzel 💬
Browse files

Refactor color scheme handeling to fix bugs

Fix the active scheme was not reflected in the menu

Fix [system] "Default" entry was always reset to breeze dark on next
start

BUG: 430580
parent c67783c3
Pipeline #200649 passed with stage
in 7 minutes and 37 seconds
......@@ -201,6 +201,11 @@ int main(int argc, char *argv[])
}
}
}
KConfigGroup uicg(config, "UiSettings");
if (!uicg.exists()) {
uicg.writeEntry("ColorSchemePath", "BreezeDark.colors");
uicg.sync();
}
#ifndef NODBUS
// Init DBus services
......
......@@ -149,7 +149,7 @@ void MainWindow::init(const QString &mltPath)
QString desktopStyle = QApplication::style()->objectName();
// Load themes
auto themeManager = new ThemeManager(actionCollection());
actionCollection()->addAction(QStringLiteral("themes_menu"), themeManager);
actionCollection()->addAction(QStringLiteral("themes_menu"), themeManager->menu());
connect(themeManager, &ThemeManager::themeChanged, this, &MainWindow::slotThemeChanged);
emit pCore->updatePalette();
......@@ -4239,7 +4239,7 @@ void MainWindow::slotEditSubtitle(const QMap<QString, QString> &subProperties)
getMainTimeline()->model()->setSubModel(subtitleModel);
pCore->currentDoc()->initializeSubtitles(subtitleModel);
pCore->subtitleWidget()->setModel(subtitleModel);
const QString subPath = pCore->currentDoc()->subTitlePath(true);
const QString subPath = pCore->currentDoc()->subTitlePath(true);
const QString workPath = pCore->currentDoc()->subTitlePath(false);
QFile subFile(subPath);
if (subFile.exists()) {
......
......@@ -16,58 +16,39 @@
#include <QStringList>
#include <KActionMenu>
#include <KColorSchemeModel>
#include <KConfigGroup>
#include <KLocalizedString>
#include <KSharedConfig>
ThemeManager::ThemeManager(QObject *parent)
: QAction(parent)
: KColorSchemeManager(parent)
{
auto manager = new KColorSchemeManager(parent);
setAutosaveChanges(false);
const auto schemePath(loadCurrentPath());
auto selectionMenu = manager->createSchemeSelectionMenu(QString(), this);
auto themesMenu = selectionMenu->menu();
// KColorSchemeManager includes a system color scheme option that reacts to system scheme changes.
// This scheme will be activated if we pass an empty string to KColorSchemeManager (if "scheme" is empty)
QString scheme;
// Check for duplicates
QList<QAction *> actions = themesMenu->actions();
QStringList existing;
QList<QAction *> duplicates;
for (QAction *ac : qAsConst(actions)) {
if (existing.contains(ac->text())) {
duplicates << ac;
} else {
existing << ac->text();
if (schemePath.isEmpty()) {
if (ac->data().toString().endsWith(QLatin1String("BreezeDark.colors"))) {
themesMenu->setActiveAction(ac);
scheme = ac->text();
}
} else if (ac->data().toString().endsWith(schemePath)) {
themesMenu->setActiveAction(ac);
scheme = ac->text();
if (!schemePath.isEmpty()) {
for (int i = 1; i < model()->rowCount(); ++i) {
QModelIndex index = model()->index(i, 0);
if (index.data(KColorSchemeModel::PathRole).toString().endsWith(schemePath)) {
scheme = index.data(KColorSchemeModel::NameRole).toString();
}
}
}
for (QAction *ac : qAsConst(duplicates)) {
themesMenu->removeAction(ac);
}
qDeleteAll(duplicates);
// KColorSchemeManager includes a system color scheme option that reacts to system scheme changes.
// This scheme will be activated if we pass an empty string to KColorSchemeManager (if "scheme" is empty)
m_menu = createSchemeSelectionMenu(scheme, this);
connect(themesMenu, &QMenu::triggered, this, [this, manager](QAction *action) {
QModelIndex schemeIndex = manager->indexForScheme(KLocalizedString::removeAcceleratorMarker(action->text()));
const QString path = manager->model()->data(schemeIndex, Qt::UserRole).toString();
connect(m_menu->menu(), &QMenu::triggered, this, [this](QAction *action) {
QModelIndex schemeIndex = indexForScheme(KLocalizedString::removeAcceleratorMarker(action->text()));
const QString path = model()->data(schemeIndex, Qt::UserRole).toString();
slotSchemeChanged(path);
});
manager->activateScheme(manager->indexForScheme(scheme));
setMenu(themesMenu);
menu()->setIcon(QIcon::fromTheme(QStringLiteral("preferences-desktop-color")));
menu()->setTitle(i18n("&Color Theme"));
activateScheme(indexForScheme(scheme));
}
QString ThemeManager::loadCurrentPath() const
......
......@@ -17,23 +17,23 @@
// KDE includes
#include <KColorSchemeManager>
class ThemeManager : public QAction
class ThemeManager : public KColorSchemeManager
{
Q_OBJECT
public:
ThemeManager(QObject *parent);
QString currentSchemeName() const;
KActionMenu *menu() { return m_menu; };
private Q_SLOTS:
void slotSchemeChanged(const QString &path);
private:
KActionMenu *m_menu;
QString loadCurrentScheme() const;
QString loadCurrentPath() const;
void saveCurrentScheme(const QString & path);
#if KCONFIGWIDGETS_VERSION < QT_VERSION_CHECK(5, 67, 0)
QString currentDesktopDefaultScheme() const;
#endif
QString currentSchemeName() const;
signals:
void themeChanged(const QString &name);
......
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