File indexing completed on 2024-04-14 04:47:26

0001 /*
0002     SPDX-FileCopyrightText: 2016 Zhigalin Alexander <alexander@zhigalin.tk>
0003 
0004     SPDX-License-Identifier: LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 // Qt includes
0008 
0009 #include "thememanager.h"
0010 #include "kdenlive_debug.h"
0011 #include "kdenlivesettings.h"
0012 
0013 #include <QFileInfo>
0014 #include <QMenu>
0015 #include <QModelIndex>
0016 #include <QStringList>
0017 
0018 #include <KActionMenu>
0019 #include <KColorSchemeModel>
0020 #if KCONFIGWIDGETS_VERSION >= QT_VERSION_CHECK(5, 107, 0)
0021 #include <KColorSchemeMenu>
0022 #endif
0023 #include <KConfigGroup>
0024 #include <KLocalizedString>
0025 #include <KSharedConfig>
0026 
0027 ThemeManager::ThemeManager(QObject *parent)
0028     : KColorSchemeManager(parent)
0029 {
0030     setAutosaveChanges(false);
0031     const auto schemePath(loadCurrentPath());
0032 
0033     // KColorSchemeManager includes a system color scheme option that reacts to system scheme changes.
0034     // This scheme will be activated if we pass an empty string to KColorSchemeManager (if "scheme" is empty)
0035     QString scheme;
0036 
0037     if (!schemePath.isEmpty()) {
0038         for (int i = 1; i < model()->rowCount(); ++i) {
0039             QModelIndex index = model()->index(i, 0);
0040 #if KCONFIGWIDGETS_VERSION >= QT_VERSION_CHECK(5, 94, 0)
0041             if (index.data(KColorSchemeModel::PathRole).toString().endsWith(schemePath)) {
0042 #else
0043             if (index.data(Qt::UserRole).toString().endsWith(schemePath)) {
0044 #endif
0045                 scheme = index.data(Qt::DisplayRole).toString();
0046             }
0047         }
0048     }
0049 #if KCONFIGWIDGETS_VERSION >= QT_VERSION_CHECK(5, 107, 0)
0050     m_menu = KColorSchemeMenu::createMenu(this, this);
0051     // TODO: select current action?
0052 #else
0053     m_menu = createSchemeSelectionMenu(scheme, this);
0054 #endif
0055 
0056     connect(m_menu->menu(), &QMenu::triggered, this, [this](QAction *action) {
0057         QModelIndex schemeIndex = indexForScheme(KLocalizedString::removeAcceleratorMarker(action->text()));
0058         const QString path = model()->data(schemeIndex, Qt::UserRole).toString();
0059         slotSchemeChanged(path);
0060     });
0061 
0062     activateScheme(indexForScheme(scheme));
0063 }
0064 
0065 QString ThemeManager::loadCurrentPath() const
0066 {
0067     KSharedConfigPtr config = KSharedConfig::openConfig();
0068     KConfigGroup cg(config, "UiSettings");
0069     return cg.readEntry("ColorSchemePath");
0070 }
0071 
0072 void ThemeManager::saveCurrentScheme(const QString &path)
0073 {
0074     KSharedConfigPtr config = KSharedConfig::openConfig();
0075     KConfigGroup cg(config, "UiSettings");
0076     cg.writeEntry("ColorSchemePath", path);
0077     cg.sync();
0078 }
0079 
0080 void ThemeManager::slotSchemeChanged(const QString &path)
0081 {
0082     saveCurrentScheme(QFileInfo(path).fileName());
0083     Q_EMIT themeChanged(path);
0084 }