correctly show configuration ui

but kded5 goes berserk when it gets closed
This commit is contained in:
Marco Martin 2013-08-26 17:50:17 +02:00
parent 410fac542c
commit 0a3eae6806
7 changed files with 494 additions and 7 deletions

View File

@ -199,7 +199,7 @@ void Containment::restore(KConfigGroup &group)
if (cfg.exists()) {
foreach (const QString &key, cfg.keyList()) {
//qDebug() << "loading" << key;
addContainmentActions(key, cfg.readEntry(key, QString()));
setContainmentActions(key, cfg.readEntry(key, QString()));
}
} else { //shell defaults
KConfigGroup defaultActionsCfg;
@ -212,7 +212,7 @@ void Containment::restore(KConfigGroup &group)
defaultActionsCfg = KConfigGroup(&defaultActionsCfg, "ContainmentActions");
foreach (const QString &key, defaultActionsCfg.keyList()) {
addContainmentActions(key, defaultActionsCfg.readEntry(key, QString()));
setContainmentActions(key, defaultActionsCfg.readEntry(key, QString()));
}
}
@ -478,7 +478,7 @@ QString Containment::wallpaper() const
return d->wallpaper;
}
void Containment::addContainmentActions(const QString &trigger, const QString &pluginName)
void Containment::setContainmentActions(const QString &trigger, const QString &pluginName)
{
KConfigGroup cfg = d->containmentActionsConfig();
ContainmentActions *plugin = 0;

View File

@ -196,7 +196,7 @@ class PLASMA_EXPORT Containment : public Applet
* @param pluginName the name of the plugin to attempt to load. blank = set no plugin.
* @since 4.4
*/
void addContainmentActions(const QString &trigger, const QString &pluginName);
void setContainmentActions(const QString &trigger, const QString &pluginName);
/**
* @return All the loaded containment action plugins, indexed by trigger name

View File

@ -28,6 +28,10 @@
#include <QQmlEngine>
#include <QQmlComponent>
#include <QDialog>
#include <QVBoxLayout>
#include <QDialogButtonBox>
#include <KLocalizedString>
#include <Plasma/Corona>
@ -46,6 +50,8 @@ CurrentContainmentActionsModel::CurrentContainmentActionsModel(Plasma::Containme
setRoleNames(roleNames);
m_baseCfg = KConfigGroup(m_containment->corona()->config(), "ActionPlugins");
QHash<QString, Plasma::ContainmentActions*> actions = cotainment->containmentActions();
QHashIterator<QString, Plasma::ContainmentActions*> i(actions);
@ -56,6 +62,10 @@ CurrentContainmentActionsModel::CurrentContainmentActionsModel(Plasma::Containme
item->setData(i.key(), ActionRole);
item->setData(i.value()->pluginInfo().pluginName(), PluginRole);
appendRow(item);
m_plugins[i.key()] = Plasma::PluginLoader::self()->loadContainmentActions(m_containment, i.value()->pluginInfo().pluginName());
m_plugins[i.key()]->setContainment(m_containment);
KConfigGroup cfg(&m_baseCfg, i.key());
m_plugins[i.key()]->restore(cfg);
}
}
@ -87,7 +97,7 @@ QString CurrentContainmentActionsModel::wheelEventString(const QPointF &delta, i
bool CurrentContainmentActionsModel::append(const QString &action, const QString &plugin)
{
if (!match(index(0,0), ActionRole, action).isEmpty()) {
if (m_plugins.contains(action)) {
return false;
}
@ -95,6 +105,10 @@ bool CurrentContainmentActionsModel::append(const QString &action, const QString
item->setData(action, ActionRole);
item->setData(plugin, PluginRole);
appendRow(item);
m_plugins[action] = Plasma::PluginLoader::self()->loadContainmentActions(m_containment, plugin);
KConfigGroup cfg(&m_baseCfg, action);
m_plugins[action]->setContainment(m_containment);
m_plugins[action]->restore(cfg);
return true;
}
@ -105,17 +119,79 @@ void CurrentContainmentActionsModel::update(int row, const QString &action, cons
if (idx.isValid()) {
setData(idx, action, ActionRole);
setData(idx, plugin, PluginRole);
if (m_plugins.contains(action)) {
delete m_plugins[action];
m_plugins[action] = Plasma::PluginLoader::self()->loadContainmentActions(m_containment, plugin);
}
}
}
void CurrentContainmentActionsModel::remove(int row)
{
const QString action = itemData(index(row, 0)).value(ActionRole).toString();
removeRows(row, 1);
if (m_plugins.contains(action)) {
delete m_plugins[action];
m_plugins.remove(action);
}
}
void CurrentContainmentActionsModel::showConfiguration(int row)
{
const QString action = itemData(index(row, 0)).value(ActionRole).toString();
if (!m_plugins.contains(action)) {
return;
}
QDialog *configDlg = new QDialog();
QLayout *lay = new QVBoxLayout(configDlg);
configDlg->setLayout(lay);
configDlg->setWindowModality(Qt::WindowModal);
//put the config in the dialog
QWidget *w = m_plugins[action]->createConfigurationInterface(configDlg);
QString title;
if (w) {
lay->addWidget(w);
title = w->windowTitle();
}
configDlg->setWindowTitle(title.isEmpty() ? i18n("Configure Plugin") :title);
//put buttons below
QDialogButtonBox *buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
Qt::Horizontal, configDlg);
lay->addWidget(buttons);
connect(buttons, SIGNAL(accepted()), this, SLOT(acceptConfig()));
configDlg->show();
}
void CurrentContainmentActionsModel::save()
{
//TODO: this configuration save is still a stub, not completely "correct" yet
//clean old config, just i case
foreach (const QString &group, m_baseCfg.groupList()) {
KConfigGroup cfg = KConfigGroup(&m_baseCfg, group);
cfg.deleteGroup();
if (m_plugins.contains(group)) {
m_containment->setContainmentActions(group, QString());
}
}
QHashIterator<QString, Plasma::ContainmentActions*> i(m_plugins);
while (i.hasNext()) {
m_containment->setContainmentActions(i.key(), i.value()->pluginInfo().pluginName());
i.next();
KConfigGroup cfg(&m_baseCfg, i.key());
i.value()->save(cfg);
}
}

View File

@ -23,8 +23,11 @@
#include "configview.h"
#include <KConfigGroup>
namespace Plasma {
class Containment;
class ContainmentActions;
}
class ConfigPropertyMap;
@ -47,10 +50,13 @@ public:
Q_INVOKABLE bool append(const QString &action, const QString &plugin);
Q_INVOKABLE void update(int row, const QString &action, const QString &plugin);
Q_INVOKABLE void remove(int row);
Q_INVOKABLE void showConfiguration(int row);
Q_INVOKABLE void save();
private:
Plasma::Containment *m_containment;
QHash<QString, Plasma::ContainmentActions *> m_plugins;
KConfigGroup m_baseCfg;
};
//TODO: is it possible to move this in the shell?

View File

@ -0,0 +1,324 @@
/*
* Copyright (c) 2009 Chani Armitage <chani@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "mousepluginwidget.h"
#include <Plasma/Containment>
#include <KAboutData>
#include <KAboutApplicationDialog>
#include <KDebug>
#include <QApplication>
#include <QComboBox>
#include <QDialog>
#include <QDialogButtonBox>
#include <QGridLayout>
Q_DECLARE_METATYPE(KPluginInfo)
MousePluginWidget::MousePluginWidget(const QString &pluginName, const QString &trigger, QGridLayout *layoutHack, QWidget *parent)
: QObject(parent),
m_configDlg(0),
m_containment(0),
m_lastConfigLocation(trigger),
m_tempConfigParent(QString(), KConfig::SimpleConfig)
{
KPluginInfo::List plugins = Plasma::ContainmentActions::listContainmentActionsInfo();
if (plugins.isEmpty()) {
//panic!!
QLabel *fail = new QLabel(i18n("No plugins found, check your installation."), parent);
layoutHack->addWidget(fail, 0, 0);
return;
}
//make us some widgets
m_pluginList = new QComboBox(parent);
m_aboutButton = new QToolButton(parent);
m_clearButton = new QToolButton(parent);
m_triggerButton = new MouseInputButton(parent);
m_configButton = new QToolButton(parent);
//m_ui.description->setText(plugin.comment());
//plugin list
//FIXME is there some way to share this across all the entries?
foreach (const KPluginInfo& plugin, plugins) {
if (plugin.property("NoDisplay").toBool()) {
continue;
}
m_pluginList->addItem(KIcon(plugin.icon()), plugin.name(), QVariant::fromValue(plugin));
if (plugin.pluginName() == pluginName) {
m_pluginList->setCurrentIndex(m_pluginList->count() - 1);
m_plugin = plugin;
}
}
if (! m_plugin.isValid()) {
//probably an empty string; pick the first one
m_pluginList->setCurrentIndex(0);
m_plugin = plugins.first();
}
//I can haz config?
m_tempConfig = KConfigGroup(&m_tempConfigParent, "test");
if (!m_plugin.property("X-Plasma-HasConfigurationInterface").toBool()) {
m_configButton->setVisible(false);
}
setTrigger(trigger);
//pretty icons for the buttons
m_aboutButton->setIcon(KIcon("dialog-information"));
m_aboutButton->setToolTip(i18nc("About mouse action", "About"));
m_triggerButton->setIcon(KIcon("input-mouse"));
m_configButton->setIcon(KIcon("configure"));
m_configButton->setToolTip(i18nc("Configure mouse action", "Configure"));
m_clearButton->setIcon(KIcon("list-remove"));
m_clearButton->setToolTip(i18nc("Remove mouse action", "Remove"));
//HACK
//FIXME what's the Right Way to do this?
int row = layoutHack->rowCount();
layoutHack->addWidget(m_triggerButton, row, 0);
layoutHack->addWidget(m_pluginList, row, 1);
layoutHack->addWidget(m_configButton, row, 2);
layoutHack->addWidget(m_aboutButton, row, 3);
layoutHack->addWidget(m_clearButton, row, 4);
//connect
connect(m_pluginList, SIGNAL(currentIndexChanged(int)), this, SLOT(setPlugin(int)));
connect(m_triggerButton, SIGNAL(triggerChanged(QString,QString)), this, SLOT(changeTrigger(QString,QString)));
connect(m_configButton, SIGNAL(clicked()), this, SLOT(configure()));
connect(m_clearButton, SIGNAL(clicked()), this, SLOT(clearTrigger()));
connect(m_aboutButton, SIGNAL(clicked()), this, SLOT(showAbout()));
}
MousePluginWidget::~MousePluginWidget()
{
delete m_pluginInstance.data();
}
void MousePluginWidget::setPlugin(int index)
{
m_plugin = m_pluginList->itemData(index).value<KPluginInfo>();
//clear all the old plugin's stuff
if (m_configDlg) {
kDebug() << "can't happen";
m_configDlg->deleteLater();
m_configDlg = 0;
}
if (m_pluginInstance) {
delete m_pluginInstance.data();
kDebug() << "tried to delete instance";
}
//reset config button
bool hasConfig = m_plugin.property("X-Plasma-HasConfigurationInterface").toBool();
m_configButton->setVisible(hasConfig);
m_tempConfig.deleteGroup();
m_lastConfigLocation.clear();
//FIXME a stray mousewheel deleting your config would be no fun.
//perhaps we could mark it for deletion, and then... um.. not delete it if the user goes back to
//that plugin? but then we'd have to record which plugin the config is *for*...
emit configChanged(m_triggerButton->trigger());
}
void MousePluginWidget::setContainment(Plasma::Containment *ctmt)
{
//note: since the old plugin's parent is the old containment,
//we let that containment take care of deleting it
m_containment = ctmt;
}
void MousePluginWidget::setTrigger(const QString &trigger)
{
m_triggerButton->setTrigger(trigger);
updateConfig(trigger);
}
void MousePluginWidget::clearTrigger()
{
QString oldTrigger = m_triggerButton->trigger();
setTrigger(QString());
emit triggerChanged(oldTrigger, QString());
//byebye!
m_pluginList->deleteLater();
m_aboutButton->deleteLater();
m_clearButton->deleteLater();
m_triggerButton->deleteLater();
m_configButton->deleteLater();
deleteLater();
}
void MousePluginWidget::changeTrigger(const QString &oldTrigger, const QString& newTrigger)
{
updateConfig(newTrigger);
emit triggerChanged(oldTrigger, newTrigger);
}
void MousePluginWidget::updateConfig(const QString &trigger)
{
m_configButton->setEnabled(!trigger.isEmpty());
m_pluginList->setEnabled(!trigger.isEmpty());
}
void MousePluginWidget::configure()
{
if (!m_pluginInstance) {
Plasma::ContainmentActions *pluginInstance = Plasma::ContainmentActions::load(m_containment, m_plugin.pluginName());
if (!pluginInstance) {
//FIXME tell user
kDebug() << "failed to load plugin!";
return;
}
m_pluginInstance = pluginInstance;
if (m_lastConfigLocation.isEmpty()) {
pluginInstance->restore(m_tempConfig);
} else {
KConfigGroup cfg = m_containment->containmentActionsConfig();
cfg = KConfigGroup(&cfg, m_lastConfigLocation);
pluginInstance->restore(cfg);
}
}
if (!m_configDlg) {
m_configDlg = new QDialog(qobject_cast<QWidget*>(parent()));
QLayout *lay = new QVBoxLayout(m_configDlg);
m_configDlg->setLayout(lay);
m_configDlg->setWindowModality(Qt::WindowModal);
//put the config in the dialog
QWidget *w = m_pluginInstance.data()->createConfigurationInterface(m_configDlg);
if (w) {
lay->addWidget(w);
}
const QString title = w->windowTitle();
m_configDlg->setWindowTitle(title.isEmpty() ? i18n("Configure Plugin") :title);
//put buttons below
QDialogButtonBox *buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
Qt::Horizontal, m_configDlg);
lay->addWidget(buttons);
//TODO other signals?
connect(buttons, SIGNAL(accepted()), this, SLOT(acceptConfig()));
connect(buttons, SIGNAL(rejected()), this, SLOT(rejectConfig()));
}
m_configDlg->show();
}
void MousePluginWidget::acceptConfig()
{
kDebug() << "accept";
if (m_pluginInstance) {
m_pluginInstance.data()->configurationAccepted();
}
m_configDlg->deleteLater();
m_configDlg = 0;
emit configChanged(m_triggerButton->trigger());
}
void MousePluginWidget::rejectConfig()
{
kDebug() << "reject";
m_configDlg->deleteLater();
m_configDlg = 0;
}
void MousePluginWidget::prepareForSave()
{
if (!m_configButton->isVisible() || m_pluginInstance || m_lastConfigLocation.isEmpty()) {
return;
}
//back up our config because it'll be erased for saving
KConfigGroup cfg = m_containment->containmentActionsConfig();
cfg = KConfigGroup(&cfg, m_lastConfigLocation);
cfg.copyTo(&m_tempConfig);
//kDebug() << "copied to temp";
}
void MousePluginWidget::save()
{
const QString trigger = m_triggerButton->trigger();
if (trigger.isEmpty()) {
m_lastConfigLocation.clear();
return;
}
if (m_pluginInstance || !m_lastConfigLocation.isEmpty()) {
KConfigGroup cfg = m_containment->containmentActionsConfig();
cfg = KConfigGroup(&cfg, trigger);
if (m_pluginInstance) {
m_pluginInstance.data()->save(cfg);
} else {
m_tempConfig.copyTo(&cfg);
}
m_lastConfigLocation = trigger;
}
m_containment->setContainmentActions(trigger, m_plugin.pluginName());
}
//copied from appletbrowser.cpp
//FIXME add a feature to KAboutApplicationDialog to delete the object
/* This is just a wrapper around KAboutApplicationDialog that deletes
the KAboutData object that it is associated with, when it is deleted.
This is required to free memory correctly when KAboutApplicationDialog
is called with a temporary KAboutData that is allocated on the heap.
(see the code below, in AppletBrowserWidget::infoAboutApplet())
*/
class KAboutApplicationDialog2 : public KAboutApplicationDialog
{
public:
KAboutApplicationDialog2(KAboutData *ab, QWidget *parent = 0)
: KAboutApplicationDialog(ab, parent), m_ab(ab) {}
~KAboutApplicationDialog2()
{
delete m_ab;
}
private:
KAboutData *m_ab;
};
void MousePluginWidget::showAbout()
{
KAboutData *aboutData = new KAboutData(m_plugin.name().toUtf8(),
m_plugin.name().toUtf8(),
ki18n(m_plugin.name().toUtf8()),
m_plugin.version().toUtf8(), ki18n(m_plugin.comment().toUtf8()),
m_plugin.fullLicense().key(), ki18n(QByteArray()), ki18n(QByteArray()), m_plugin.website().toLatin1(),
m_plugin.email().toLatin1());
aboutData->setProgramIconName(m_plugin.icon());
aboutData->addAuthor(ki18n(m_plugin.author().toUtf8()), ki18n(QByteArray()), m_plugin.email().toLatin1());
KAboutApplicationDialog *aboutDialog = new KAboutApplicationDialog2(aboutData, qobject_cast<QWidget*>(parent()));
aboutDialog->show();
}
// vim: sw=4 sts=4 et tw=100

View File

@ -0,0 +1,75 @@
/*
* Copyright (c) 2009 Chani Armitage <chani@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef MOUSEPLUGINWIDGET_H
#define MOUSEPLUGINWIDGET_H
#include <QDialog>
#include <QLabel>
#include <QToolButton>
#include <KConfigGroup>
#include <plasma/containmentactions.h>
#include "mouseinputbutton.h"
class QComboBox;
class QGridLayout;
class MousePluginWidget : public QObject
{
Q_OBJECT
public:
MousePluginWidget(const QString &plugin, const QString &trigger, QGridLayout *layoutHack, QWidget *parent = 0);
~MousePluginWidget();
void setTrigger(const QString &trigger);
void prepareForSave();
void save();
signals:
void triggerChanged(const QString &oldTrigger, const QString &newTrigger);
void configChanged(const QString &trigger);
private slots:
void changeTrigger(const QString &oldTrigger, const QString& newTrigger);
void configure();
void acceptConfig();
void rejectConfig();
void showAbout();
private:
void updateConfig(const QString &trigger);
QComboBox *m_pluginList;
QDialog *m_configDlg;
KPluginInfo m_plugin;
QWeakPointer<Plasma::ContainmentActions> m_pluginInstance;
Plasma::Containment *m_containment;
QString m_lastConfigLocation;
KConfigGroup m_tempConfig;
KConfig m_tempConfigParent;
};
#endif

View File

@ -50,16 +50,22 @@ Item {
QtControls.Button {
iconName: "configure"
width: height
onClicked: {
configDialog.currentContainmentActionsModel.showConfiguration(index);
}
}
QtControls.Button {
iconName: "dialog-information"
width: height
onClicked: {
configDialog.currentContainmentActionsModel.showInformation(index);
}
}
QtControls.Button {
iconName: "list-remove"
width: height
onClicked: {
configDialog.currentContainmentActionsModel.remove(index)
configDialog.currentContainmentActionsModel.remove(index);
}
}
}