Commit 27d90761 authored by Kevin Ottens's avatar Kevin Ottens
Browse files

Have the wallpaper combo managed by KConfigDialogManager

Summary:
We use an intermediate property which maps to an index since it's
exactly what KConfigDialogManager expects with a QComboBox.

Also relayouted a bit the ctor and the file to make it easier to
understand (several aspect were weaved together).

Reviewers: #plasma, davidedmundson, crossi, bport

Subscribers: plasma-devel

Tags: #plasma

Differential Revision: https://phabricator.kde.org/D25322
parent e5e8c915
......@@ -193,6 +193,7 @@ PRIVATE
KF5::Notifications
KF5::CoreAddons
KF5::ConfigGui
KF5::Package
KF5::WindowSystem
KF5::XmlGui
${X11_LIBRARIES}
......
......@@ -195,7 +195,7 @@ void UnlockApp::initialize()
m_mainQmlPath = package.fileUrl("lockscreenmainscript");
m_wallpaperIntegration->setConfig(KScreenSaverSettingsBase::self()->sharedConfig());
m_wallpaperIntegration->setPluginName(KScreenSaverSettingsBase::self()->wallpaperPlugin());
m_wallpaperIntegration->setPluginName(KScreenSaverSettingsBase::self()->wallpaperPluginId());
m_wallpaperIntegration->init();
m_lnfIntegration->setPackage(package);
......
......@@ -38,7 +38,7 @@
<label></label>
<whatsthis></whatsthis>
</entry>
<entry key="WallpaperPlugin" type="String">
<entry name="wallpaperPluginId" key="WallpaperPlugin" type="String">
<default>org.kde.image</default>
<label></label>
<whatsthis></whatsthis>
......
......@@ -40,8 +40,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <QQmlContext>
#include <QQuickItem>
static const QString s_defaultWallpaperPackage = QStringLiteral("org.kde.image");
class ScreenLockerKcmForm : public QWidget, public Ui::ScreenLockerKcmForm
{
Q_OBJECT
......@@ -68,56 +66,33 @@ ScreenLockerKcm::ScreenLockerKcm(QWidget *parent, const QVariantList &args)
{
QVBoxLayout* layout = new QVBoxLayout(this);
layout->addWidget(m_ui);
addConfig(m_settings, m_ui);
loadWallpapers();
connect(m_ui->wallpaperCombo, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
for (const auto &pluginInfo : m_settings->availableWallpaperPlugins()) {
m_ui->kcfg_wallpaperPluginIndex->addItem(pluginInfo.name, pluginInfo.id);
}
connect(m_ui->kcfg_wallpaperPluginIndex, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
this, &ScreenLockerKcm::loadWallpaperConfig);
m_ui->wallpaperCombo->installEventFilter(this);
m_ui->kcfg_wallpaperPluginIndex->installEventFilter(this);
m_ui->installEventFilter(this);
auto proxy = new ScreenLockerProxy(this);
m_ui->wallpaperConfigWidget->setClearColor(m_ui->palette().color(QPalette::Active, QPalette::Window));
m_ui->wallpaperConfigWidget->rootContext()->setContextProperty(QStringLiteral("configDialog"), proxy);
m_ui->lnfConfigWidget->setClearColor(m_ui->palette().color(QPalette::Active, QPalette::Window));
m_ui->lnfConfigWidget->rootContext()->setContextProperty(QStringLiteral("configDialog"), proxy);
connect(this, &ScreenLockerKcm::wallpaperConfigurationChanged, proxy, &ScreenLockerProxy::wallpaperConfigurationChanged);
connect(this, &ScreenLockerKcm::currentWallpaperChanged, proxy, &ScreenLockerProxy::currentWallpaperChanged);
m_ui->wallpaperConfigWidget->setSource(QUrl(QStringLiteral("qrc:/kscreenlocker-kcm-resources/wallpaperconfig.qml")));
connect(m_ui->wallpaperConfigWidget->rootObject(), SIGNAL(configurationChanged()), this, SLOT(updateState()));
m_ui->lnfConfigWidget->setClearColor(m_ui->palette().color(QPalette::Active, QPalette::Window));
m_ui->lnfConfigWidget->rootContext()->setContextProperty(QStringLiteral("configDialog"), proxy);
m_ui->lnfConfigWidget->setSource(QUrl(QStringLiteral("qrc:/kscreenlocker-kcm-resources/lnfconfig.qml")));
connect(m_ui->lnfConfigWidget->rootObject(), SIGNAL(configurationChanged()), this, SLOT(updateState()));
m_ui->installEventFilter(this);
addConfig(m_settings, m_ui);
}
void ScreenLockerKcm::load()
{
KCModule::load();
// Because the wallpaper plugin is currently handled wrongly
m_settings->load();
m_package = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("Plasma/LookAndFeel"));
KConfigGroup cg(KSharedConfig::openConfig(QStringLiteral("kdeglobals")), "KDE");
const QString packageName = cg.readEntry("LookAndFeelPackage", QString());
if (!packageName.isEmpty()) {
m_package.setPath(packageName);
}
m_lnfIntegration = new ScreenLocker::LnFIntegration(this);
m_lnfIntegration->setPackage(m_package);
m_lnfIntegration->setConfig(m_settings->sharedConfig());
m_lnfIntegration->init();
m_lnfSettings = m_lnfIntegration->configScheme();
selectWallpaper(m_settings->wallpaperPlugin());
loadWallpaperConfig();
loadLnfConfig();
......@@ -159,7 +134,6 @@ void ScreenLockerKcm::save()
void ScreenLockerKcm::defaults()
{
KCModule::defaults();
selectWallpaper(s_defaultWallpaperPackage);
if (m_lnfSettings) {
m_lnfSettings->setDefaults();
......@@ -193,33 +167,10 @@ void ScreenLockerKcm::updateState()
emit defaulted(isDefaults);
}
void ScreenLockerKcm::loadWallpapers()
{
const auto wallpaperPackages = KPackage::PackageLoader::self()->listPackages(QStringLiteral("Plasma/Wallpaper"));
for (auto &package : wallpaperPackages) {
m_ui->wallpaperCombo->addItem(package.name(), package.pluginId());
}
}
void ScreenLockerKcm::selectWallpaper(const QString &pluginId)
{
const auto index = m_ui->wallpaperCombo->findData(pluginId);
if (index != -1) {
m_ui->wallpaperCombo->setCurrentIndex(index);
} else if (pluginId != s_defaultWallpaperPackage) {
// fall back to default plugin
selectWallpaper(s_defaultWallpaperPackage);
}
}
void ScreenLockerKcm::loadWallpaperConfig()
{
// set the wallpaper config
m_settings->setWallpaperPlugin(m_ui->wallpaperCombo->currentData().toString());
updateState();
if (m_wallpaperIntegration) {
if (m_wallpaperIntegration->pluginName() == m_ui->wallpaperCombo->currentData().toString()) {
if (m_wallpaperIntegration->pluginName() == m_ui->kcfg_wallpaperPluginIndex->currentData().toString()) {
// nothing changed
return;
}
......@@ -229,7 +180,7 @@ void ScreenLockerKcm::loadWallpaperConfig()
m_wallpaperIntegration = new ScreenLocker::WallpaperIntegration(this);
m_wallpaperIntegration->setConfig(m_settings->sharedConfig());
m_wallpaperIntegration->setPluginName(m_ui->wallpaperCombo->currentData().toString());
m_wallpaperIntegration->setPluginName(m_ui->kcfg_wallpaperPluginIndex->currentData().toString());
m_wallpaperIntegration->init();
m_wallpaperSettings = m_wallpaperIntegration->configScheme();
m_ui->wallpaperConfigWidget->rootContext()->setContextProperty(QStringLiteral("wallpaper"), m_wallpaperIntegration);
......@@ -239,6 +190,25 @@ void ScreenLockerKcm::loadWallpaperConfig()
void ScreenLockerKcm::loadLnfConfig()
{
if (m_package.isValid() && m_lnfIntegration) {
return;
}
Q_ASSERT(!m_package.isValid() && !m_lnfIntegration);
m_package = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("Plasma/LookAndFeel"));
KConfigGroup cg(KSharedConfig::openConfig(QStringLiteral("kdeglobals")), "KDE");
const QString packageName = cg.readEntry("LookAndFeelPackage", QString());
if (!packageName.isEmpty()) {
m_package.setPath(packageName);
}
m_lnfIntegration = new ScreenLocker::LnFIntegration(this);
m_lnfIntegration->setPackage(m_package);
m_lnfIntegration->setConfig(m_settings->sharedConfig());
m_lnfIntegration->init();
m_lnfSettings = m_lnfIntegration->configScheme();
auto sourceFile = m_package.fileUrl(QByteArrayLiteral("lockscreen"), QStringLiteral("config.qml"));
if (sourceFile.isEmpty()) {
m_ui->lnfConfigWidget->hide();
......@@ -266,7 +236,7 @@ KDeclarative::ConfigPropertyMap * ScreenLockerKcm::lnfConfiguration() const
QString ScreenLockerKcm::currentWallpaper() const
{
return m_ui->wallpaperCombo->currentData().toString();
return m_ui->kcfg_wallpaperPluginIndex->currentData().toString();
}
bool ScreenLockerKcm::eventFilter(QObject *watched, QEvent *event)
......@@ -277,17 +247,17 @@ bool ScreenLockerKcm::eventFilter(QObject *watched, QEvent *event)
}
return false;
}
if (watched != m_ui->wallpaperCombo) {
if (watched != m_ui->kcfg_wallpaperPluginIndex) {
return false;
}
if (event->type() == QEvent::Move) {
if (auto object = m_ui->wallpaperConfigWidget->rootObject()) {
// QtQuick Layouts have a hardcoded 5 px spacing by default
object->setProperty("formAlignment", m_ui->wallpaperCombo->x() + 5);
object->setProperty("formAlignment", m_ui->kcfg_wallpaperPluginIndex->x() + 5);
}
if (auto object = m_ui->lnfConfigWidget->rootObject()) {
// QtQuick Layouts have a hardcoded 5 px spacing by default
object->setProperty("formAlignment", m_ui->wallpaperCombo->x() + 5);
object->setProperty("formAlignment", m_ui->kcfg_wallpaperPluginIndex->x() + 5);
}
}
......
......@@ -68,7 +68,6 @@ private Q_SLOTS:
void updateState();
private:
void loadWallpapers();
void selectWallpaper(const QString &pluginId);
void loadWallpaperConfig();
void loadLnfConfig();
......@@ -94,6 +93,8 @@ public:
QObject(parent),
q(parent)
{
connect(q, &ScreenLockerKcm::wallpaperConfigurationChanged, this, &ScreenLockerProxy::wallpaperConfigurationChanged);
connect(q, &ScreenLockerKcm::currentWallpaperChanged, this, &ScreenLockerProxy::currentWallpaperChanged);
}
KDeclarative::ConfigPropertyMap *wallpaperConfiguration() const {
......
......@@ -141,12 +141,12 @@
<string>Wallpaper &amp;type:</string>
</property>
<property name="buddy">
<cstring>wallpaperCombo</cstring>
<cstring>kcfg_wallpaperPluginIndex</cstring>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="wallpaperCombo"/>
<widget class="QComboBox" name="kcfg_wallpaperPluginIndex"/>
</item>
</layout>
</item>
......
......@@ -22,6 +22,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <KActionCollection>
#include <KGlobalAccel>
#include <KLocalizedString>
#include <KPackage/Package>
#include <KPackage/PackageLoader>
QList<QKeySequence> KScreenSaverSettings::defaultShortcuts()
{
......@@ -32,6 +34,11 @@ QList<QKeySequence> KScreenSaverSettings::defaultShortcuts()
};
}
QString KScreenSaverSettings::defaultWallpaperPlugin()
{
return QStringLiteral("org.kde.image");
}
KScreenSaverSettings::KScreenSaverSettings(QObject *parent)
: KScreenSaverSettingsBase()
, m_actionCollection(new KActionCollection(this, QStringLiteral("ksmserver")))
......@@ -39,6 +46,11 @@ KScreenSaverSettings::KScreenSaverSettings(QObject *parent)
{
setParent(parent);
const auto wallpaperPackages = KPackage::PackageLoader::self()->listPackages(QStringLiteral("Plasma/Wallpaper"));
for (auto &package : wallpaperPackages) {
m_availableWallpaperPlugins.append({package.name(), package.pluginId()});
}
m_actionCollection->setConfigGlobal(true);
m_lockAction = m_actionCollection->addAction(QStringLiteral("Lock Session"));
m_lockAction->setProperty("isConfigurationAction", true);
......@@ -46,12 +58,35 @@ KScreenSaverSettings::KScreenSaverSettings(QObject *parent)
KGlobalAccel::self()->setShortcut(m_lockAction, defaultShortcuts());
addItem(new KPropertySkeletonItem(this, "shortcut", defaultShortcuts().first()), QStringLiteral("lockscreenShortcut"));
addItem(new KPropertySkeletonItem(this, "wallpaperPluginIndex", indexFromWallpaperPluginId(defaultWallpaperPlugin())),
QStringLiteral("wallpaperPluginIndex"));
}
KScreenSaverSettings::~KScreenSaverSettings()
{
}
QVector<KScreenSaverSettings::WallpaperInfo> KScreenSaverSettings::availableWallpaperPlugins() const
{
return m_availableWallpaperPlugins;
}
int KScreenSaverSettings::wallpaperPluginIndex() const
{
return indexFromWallpaperPluginId(wallpaperPluginId());
}
void KScreenSaverSettings::setWallpaperPluginIndex(int index)
{
Q_ASSERT(index >=0 && index < m_availableWallpaperPlugins.size());
setWallpaperPluginId(m_availableWallpaperPlugins[index].id);
// We get in this function during save, but wallpaperPluginId might
// have been written already, since we're tempering with its value here
// we make sure it really reaches the config object.
findItem(QStringLiteral("wallpaperPluginId"))->writeConfig(config());
}
QKeySequence KScreenSaverSettings::shortcut() const
{
return KGlobalAccel::self()->shortcut(m_lockAction).first();
......@@ -67,3 +102,16 @@ void KScreenSaverSettings::setShortcut(const QKeySequence &sequence)
shortcuts[0] = sequence;
KGlobalAccel::self()->setShortcut(m_lockAction, shortcuts, KGlobalAccel::NoAutoloading);
}
int KScreenSaverSettings::indexFromWallpaperPluginId(const QString &id) const
{
const auto it = std::find_if(m_availableWallpaperPlugins.cbegin(), m_availableWallpaperPlugins.cend(),
[id] (const WallpaperInfo &info) { return info.id == id; });
if (it != m_availableWallpaperPlugins.cend()) {
return it - m_availableWallpaperPlugins.cbegin();
} else if (id != defaultWallpaperPlugin()) {
return indexFromWallpaperPluginId(defaultWallpaperPlugin());
} else {
return -1;
}
}
......@@ -28,16 +28,30 @@ class KScreenSaverSettings : public KScreenSaverSettingsBase
{
Q_OBJECT
Q_PROPERTY(QKeySequence shortcut READ shortcut WRITE setShortcut)
Q_PROPERTY(int wallpaperPluginIndex READ wallpaperPluginIndex WRITE setWallpaperPluginIndex)
public:
struct WallpaperInfo {
QString name;
QString id;
};
static QList<QKeySequence> defaultShortcuts();
static QString defaultWallpaperPlugin();
KScreenSaverSettings(QObject *parent = nullptr);
~KScreenSaverSettings() override;
QVector<WallpaperInfo> availableWallpaperPlugins() const;
int wallpaperPluginIndex() const;
void setWallpaperPluginIndex(int index);
QKeySequence shortcut() const;
void setShortcut(const QKeySequence &sequence);
private:
int indexFromWallpaperPluginId(const QString &id) const;
QVector<WallpaperInfo> m_availableWallpaperPlugins;
KActionCollection *m_actionCollection;
QAction *m_lockAction;
};
......
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