Commit bb2b300a authored by Kevin Ottens's avatar Kevin Ottens
Browse files

Expose the settings object to QML

Summary:
This simplifies quite a bit the logic, we don't need to do the
bookkeeping ourselves anymore. Only missing would be a "isDirty" flag on
the settings object which explains the m_currentTheme member variable
which is still around to have something to compare against in
updateNeedsSave().

Reviewers: #plasma, mart

Subscribers: plasma-devel

Tags: #plasma

Differential Revision: https://phabricator.kde.org/D24394
parent 6648a1a4
......@@ -56,6 +56,13 @@ KCMDesktopTheme::KCMDesktopTheme(QObject *parent, const QVariantList &args)
{
// Unfortunately doesn't generate a ctor taking the parent as parameter
m_settings->setParent(this);
m_currentTheme = m_settings->name();
connect(m_settings, &DesktopThemeSettings::configChanged,
this, &KCMDesktopTheme::updateNeedsSave);
connect(m_settings, &DesktopThemeSettings::nameChanged,
this, &KCMDesktopTheme::updateNeedsSave);
qmlRegisterType<DesktopThemeSettings>();
qmlRegisterType<QStandardItemModel>();
KAboutData* about = new KAboutData(QStringLiteral("kcm_desktoptheme"), i18n("Plasma Style"),
......@@ -80,30 +87,19 @@ KCMDesktopTheme::~KCMDesktopTheme()
{
}
QStandardItemModel *KCMDesktopTheme::desktopThemeModel() const
DesktopThemeSettings *KCMDesktopTheme::desktopThemeSettings() const
{
return m_model;
return m_settings;
}
QString KCMDesktopTheme::selectedPlugin() const
{
return m_selectedPlugin;
}
void KCMDesktopTheme::setSelectedPlugin(const QString &plugin)
QStandardItemModel *KCMDesktopTheme::desktopThemeModel() const
{
if (m_selectedPlugin == plugin) {
return;
}
m_selectedPlugin = plugin;
emit selectedPluginChanged(m_selectedPlugin);
emit selectedPluginIndexChanged();
updateNeedsSave();
return m_model;
}
int KCMDesktopTheme::selectedPluginIndex() const
int KCMDesktopTheme::pluginIndex(const QString &pluginName) const
{
const auto results = m_model->match(m_model->index(0, 0), PluginNameRole, m_selectedPlugin);
const auto results = m_model->match(m_model->index(0, 0), PluginNameRole, pluginName);
if (results.count() == 1) {
return results.first().row();
}
......@@ -122,13 +118,11 @@ void KCMDesktopTheme::setPendingDeletion(int index, bool pending)
m_model->setData(idx, pending, PendingDeletionRole);
if (pending && selectedPluginIndex() == index) {
if (pending && pluginIndex(m_settings->name()) == index) {
// move to the next non-pending theme
const auto nonPending = m_model->match(idx, PendingDeletionRole, false);
setSelectedPlugin(nonPending.first().data(PluginNameRole).toString());
m_settings->setName(nonPending.first().data(PluginNameRole).toString());
}
updateNeedsSave();
}
void KCMDesktopTheme::getNewStuff(QQuickItem *ctx)
......@@ -284,28 +278,26 @@ void KCMDesktopTheme::load()
m_model->setSortRole(ThemeNameRole); // FIXME the model should really be just using Qt::DisplayRole
m_model->sort(0 /*column*/);
setSelectedPlugin(m_settings->name());
m_settings->load();
m_currentTheme = m_settings->name();
emit selectedPluginIndexChanged();
updateNeedsSave();
// Model has been cleared so pretend the theme name changed to force view update
emit m_settings->nameChanged();
}
void KCMDesktopTheme::save()
{
if (m_settings->name() != m_selectedPlugin) {
m_settings->setName(m_selectedPlugin);
m_settings->save();
Plasma::Theme().setThemeName(m_settings->name());
}
m_settings->save();
m_currentTheme = m_settings->name();
Plasma::Theme().setThemeName(m_settings->name());
processPendingDeletions();
updateNeedsSave();
}
void KCMDesktopTheme::defaults()
{
setSelectedPlugin(m_settings->defaultNameValue());
m_settings->setDefaults();
m_currentTheme = m_settings->name();
// can this be done more elegantly?
const auto pendingDeletions = m_model->match(m_model->index(0, 0), PendingDeletionRole, true);
......@@ -327,7 +319,7 @@ void KCMDesktopTheme::editTheme(const QString &theme)
void KCMDesktopTheme::updateNeedsSave()
{
setNeedsSave(!m_model->match(m_model->index(0, 0), PendingDeletionRole, true).isEmpty()
|| m_selectedPlugin != m_settings->name());
|| m_currentTheme != m_settings->name());
}
void KCMDesktopTheme::processPendingDeletions()
......@@ -346,7 +338,7 @@ void KCMDesktopTheme::processPendingDeletions()
const QString pluginName = idx.data(PluginNameRole).toString();
const QString displayName = idx.data(Qt::DisplayRole).toString();
Q_ASSERT(pluginName != m_selectedPlugin);
Q_ASSERT(pluginName != m_settings->name());
const QStringList arguments = {QStringLiteral("-t"), QStringLiteral("theme"), QStringLiteral("-r"), pluginName};
......
......@@ -45,9 +45,8 @@ class DesktopThemeSettings;
class KCMDesktopTheme : public KQuickAddons::ConfigModule
{
Q_OBJECT
Q_PROPERTY(DesktopThemeSettings *desktopThemeSettings READ desktopThemeSettings CONSTANT)
Q_PROPERTY(QStandardItemModel *desktopThemeModel READ desktopThemeModel CONSTANT)
Q_PROPERTY(QString selectedPlugin READ selectedPlugin WRITE setSelectedPlugin NOTIFY selectedPluginChanged)
Q_PROPERTY(int selectedPluginIndex READ selectedPluginIndex NOTIFY selectedPluginIndexChanged)
Q_PROPERTY(bool downloadingFile READ downloadingFile NOTIFY downloadingFileChanged)
Q_PROPERTY(bool canEditThemes READ canEditThemes CONSTANT)
......@@ -64,11 +63,10 @@ public:
KCMDesktopTheme(QObject *parent, const QVariantList &args);
~KCMDesktopTheme() override;
DesktopThemeSettings *desktopThemeSettings() const;
QStandardItemModel *desktopThemeModel() const;
QString selectedPlugin() const;
void setSelectedPlugin(const QString &plugin);
int selectedPluginIndex() const;
Q_INVOKABLE int pluginIndex(const QString &pluginName) const;
bool downloadingFile() const;
......@@ -84,8 +82,6 @@ public:
Q_INVOKABLE void editTheme(const QString &themeName);
Q_SIGNALS:
void selectedPluginChanged(const QString &plugin);
void selectedPluginIndexChanged();
void downloadingFileChanged();
void showSuccessMessage(const QString &message);
......@@ -106,7 +102,7 @@ private:
DesktopThemeSettings *m_settings;
QStandardItemModel *m_model;
QString m_selectedPlugin;
QString m_currentTheme;
QStringList m_pendingRemoval;
QHash<QString, Plasma::Theme*> m_themes;
bool m_haveThemeExplorerInstalled;
......
......@@ -2,6 +2,7 @@
Copyright (c) 2014 Marco Martin <mart@kde.org>
Copyright (c) 2016 David Rosca <nowrep@gmail.com>
Copyright (c) 2018 Kai Uwe Broulik <kde@privat.broulik.de>
Copyright (c) 2019 Kevin Ottens <kevin.ottens@enioka.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
......@@ -30,7 +31,7 @@ KCM.GridViewKCM {
KCM.ConfigModule.quickHelp: i18n("This module lets you choose the Plasma style.")
view.model: kcm.desktopThemeModel
view.currentIndex: kcm.selectedPluginIndex
view.currentIndex: kcm.pluginIndex(kcm.desktopThemeSettings.name)
enabled: !kcm.downloadingFile
......@@ -86,7 +87,7 @@ KCM.GridViewKCM {
]
onClicked: {
kcm.selectedPlugin = model.pluginName;
kcm.desktopThemeSettings.name = model.pluginName;
view.forceActiveFocus();
}
}
......
Markdown is supported
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