Addition of the hasItem(...) function to Extender. And usage of this function in libplasmaclock. This function allows for checking if items exist, even if that item hasn't been instantiated yet.

svn path=/trunk/KDE/kdelibs/; revision=938688
This commit is contained in:
Rob Scheepmaker 2009-03-12 17:19:45 +00:00
parent ddc3a4b978
commit 766bca2524
4 changed files with 79 additions and 18 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2008 by Rob Scheepmaker <r.scheepmaker@student.utwente.nl>
* Copyright 2008, 2009 by Rob Scheepmaker <r.scheepmaker@student.utwente.nl>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@ -169,6 +169,28 @@ ExtenderItem *Extender::item(const QString &name) const
return 0;
}
bool Extender::hasItem(const QString &name) const
{
if (item(name)) {
return true;
}
//if item(name) returns false, that doesn't mean that the item doesn't exist, just that it has
//not been instantiated yet.
Corona *corona = qobject_cast<Corona*>(scene());
KConfigGroup extenderItemGroup(corona->config(), "DetachedExtenderItems");
foreach (const QString &extenderItemId, extenderItemGroup.groupList()) {
KConfigGroup cg = extenderItemGroup.group(extenderItemId);
if (cg.readEntry("sourceAppletId", 0) == d->applet->id() &&
cg.readEntry("extenderItemName", "") == name &&
cg.readEntry("sourceAppletPluginName", "") == d->applet->pluginName()) {
return true;
}
}
return false;
}
void Extender::setAppearance(Appearance appearance)
{
if (d->appearance == appearance) {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2008 by Rob Scheepmaker <r.scheepmaker@student.utwente.nl>
* Copyright 2008, 2009 by Rob Scheepmaker <r.scheepmaker@student.utwente.nl>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@ -93,6 +93,8 @@ class PLASMA_EXPORT Extender : public QGraphicsWidget
* in an applet's constructor.
* The constructor also takes care of restoring ExtenderItems that were contained in this
* extender before, so ExtenderItems are persistent between sessions.
* Note that a call to extender() in an applet will instantiate an Extender for you if one
* isn't already associated with your applet.
* @param applet The applet this extender is part of. Null is not allowed here.
*/
explicit Extender(Applet *applet);
@ -101,7 +103,6 @@ class PLASMA_EXPORT Extender : public QGraphicsWidget
/**
* @param message The text to be shown whenever the applet's extender is empty.
* Defaults to i18n'ed "no items".
*/
void setEmptyExtenderMessage(const QString &message);
@ -127,20 +128,29 @@ class PLASMA_EXPORT Extender : public QGraphicsWidget
QList<ExtenderItem*> detachedItems() const;
/**
* This function can be used for easily determining if a certain item is already displayed
* in a extender item somewhere, so your applet doesn't duplicate this item. Say the applet
* displays 'jobs', from an engine which add's a source for every job. In sourceAdded you
* could do something like:
* if (!item(source)) {
* //add an extender item monitoring this source.
* }
* This function can be used for obtaining the extender item specified by name. For checking
* whether or not an item already exists, you should use hasItem instead: while plasma is
* starting up, not all detached items might have been instantiated yet. hasItem returns true
* even if the requested item isn't instantiated yet.
* @returns the requested item
*/
ExtenderItem *item(const QString &name) const;
/**
* This function can be used for easily determining if a certain item is already displayed
* in an extender item somewhere, so your applet doesn't duplicate this item. This is needed
* because ExtenderItems are persistent, so you can't blindly add new extender items in all
* cases.
* @returns whether or not this item already exists.
* @since 4.3
*/
bool hasItem(const QString &name) const;
/**
* Use this function to instruct the extender on how to render it's items. Usually you will
* want to call this function in your applet's constraintsEvent, allthough this is already
* done for you when using PopupApplet at base class for your applet. Defaults to NoBorders.
* done for you when using PopupApplet as base class for your applet. Defaults to NoBorders.
* @param appearance the way this extender should look.
*/
void setAppearance(Appearance appearance);

View File

@ -1,5 +1,5 @@
/*
* Copyright 2008 by Rob Scheepmaker <r.scheepmaker@student.utwente.nl>
* Copyright 2008, 2009 by Rob Scheepmaker <r.scheepmaker@student.utwente.nl>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@ -251,6 +251,23 @@ void ExtenderItem::setExtender(Extender *extender, const QPointF &pos)
d->expirationTimer = 0;
}
Corona *corona = qobject_cast<Corona*>(scene());
KConfigGroup extenderItemGroup(corona->config(), "DetachedExtenderItems");
if (isDetached()) {
kDebug() << "detached, adding entry to the global group";
KConfigGroup itemConfig = extenderItemGroup.group(QString::number(d->extenderItemId));
itemConfig.writeEntry("sourceAppletPluginName",
config().readEntry("sourceAppletPluginName", ""));
itemConfig.writeEntry("sourceAppletId",
config().readEntry("sourceAppletId", 0));
itemConfig.writeEntry("extenderItemName",
config().readEntry("extenderItemName", ""));
} else if (extenderItemGroup.hasGroup(QString::number(d->extenderItemId))) {
kDebug() << "no longer detached, removing entry from the global group";
extenderItemGroup.deleteGroup(QString::number(d->extenderItemId));
}
//we might have to enable or disable the returnToSource button.
d->updateToolBox();
}
@ -353,6 +370,13 @@ void ExtenderItem::destroy()
return;
}
//remove global entry if needed.
Corona *corona = qobject_cast<Corona*>(scene());
KConfigGroup extenderItemGroup(corona->config(), "DetachedExtenderItems");
if (extenderItemGroup.hasGroup(QString::number(d->extenderItemId))) {
extenderItemGroup.deleteGroup(QString::number(d->extenderItemId));
}
d->hostApplet()->config("ExtenderItems").deleteGroup(QString::number(d->extenderItemId));
d->extender->d->removeExtenderItem(this);
deleteLater();

View File

@ -1,5 +1,5 @@
/*
* Copyright 2008 by Rob Scheepmaker <r.scheepmaker@student.utwente.nl>
* Copyright 2008, 2009 by Rob Scheepmaker <r.scheepmaker@student.utwente.nl>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@ -46,13 +46,18 @@ class ExtenderItemPrivate;
* Typical usage of ExtenderItems in your applet could look like this:
*
* @code
* ExtenderItem *item = new ExtenderItem(extender());
* //name can be used to later access this item through extender()->item(name):
* item->setName("networkmonitoreth0");
* item->config().writeEntry("device", "eth0");
* initExtenderItem(item);
* if (!extender()->hasItem("networkmonitoreth0")) {
* ExtenderItem *item = new ExtenderItem(extender());
* //name can be used to later access this item through extender()->item(name):
* item->setName("networkmonitoreth0");
* item->config().writeEntry("device", "eth0");
* initExtenderItem(item);
* }
* @endcode
*
* Note that we first check if the item already exists: ExtenderItems are persistent
* between sessions so we can't blindly add items since they might already exist.
*
* You'll then need to implement the initExtenderItem function. Having this function in your applet
* makes sure that detached extender items get restored after plasma is restarted, just like applets
* are. That is also the reason that we write an entry in item->config().