Move children together with their group when the group gets moved, and add a group convenience function to extender to avoid needing all those casts from extenderitem to extendergroup. Plus some minor code cleanups.

svn path=/trunk/KDE/kdelibs/; revision=959604
This commit is contained in:
Rob Scheepmaker 2009-04-26 17:03:15 +00:00
parent ebcc06e25f
commit a5c48ddc4f
5 changed files with 72 additions and 27 deletions

View File

@ -170,6 +170,11 @@ ExtenderItem *Extender::item(const QString &name) const
return 0;
}
ExtenderGroup *Extender::group(const QString &name) const
{
return qobject_cast<ExtenderGroup*>(item(name));
}
bool Extender::hasItem(const QString &name) const
{
if (item(name)) {
@ -177,7 +182,8 @@ bool Extender::hasItem(const QString &name) const
}
//if item(name) returns false, that doesn't mean that the item doesn't exist, just that it has
//not been instantiated yet.
//not been instantiated yet. check to see if there's mention of this item existing in the
//plasma config's section DetachedExtenderItems
Corona *corona = qobject_cast<Corona*>(scene());
KConfigGroup extenderItemGroup(corona->config(), "DetachedExtenderItems");
foreach (const QString &extenderItemId, extenderItemGroup.groupList()) {
@ -340,13 +346,17 @@ void Extender::dropEvent(QGraphicsSceneDragDropEvent *event)
void Extender::itemAddedEvent(ExtenderItem *item, const QPointF &pos)
{
if (pos == QPointF(-1, -1)) {
//if just plain adding an item, add it at a sane position:
if (!item->group()) {
if (appearance() == BottomUpStacked) {
//at the top
d->layout->insertItem(0, item);
} else {
//at the bottom
d->layout->addItem(item);
}
} else {
//at the top in the group it belongs to
d->layout->insertItem(d->insertIndexFromPos(item->group()->pos()) + 1, item);
}
} else {

View File

@ -138,6 +138,13 @@ class PLASMA_EXPORT Extender : public QGraphicsWidget
*/
ExtenderItem *item(const QString &name) const;
/**
* Extra convenience function for obtaining groups specified by name. This will avoid needed
* to call item and casting to ExtenderGroup, which is otherwise quite common.
* @returns the requested group
*/
ExtenderGroup *group(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

View File

@ -68,8 +68,8 @@ ExtenderGroup::ExtenderGroup(Extender *parent, uint groupId)
}
if (items().isEmpty() && d->autoHide && !isDetached()) {
hide();
extender()->itemRemovedEvent(this);
hide();
}
connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()),
@ -101,9 +101,9 @@ bool ExtenderGroup::autoHide() const
void ExtenderGroup::setAutoHide(bool autoHide)
{
d->autoHide = autoHide;
if (autoHide && items().count() < 2) {
hide();
if (autoHide && items().isEmpty()) {
extender()->itemRemovedEvent(this);
hide();
} else if (!autoHide && !isVisible()) {
extender()->itemAddedEvent(this);
show();
@ -151,41 +151,27 @@ ExtenderGroupPrivate::~ExtenderGroupPrivate()
void ExtenderGroupPrivate::addItemToGroup(Plasma::ExtenderItem *item)
{
if (item->group() == q) {
int itemCount = q->items().count();
if (collapsed && !(autoHide && itemCount == 1)) {
//the group is collapsed, so hide the new item unless there's only one item and autohide
//is true, in which case we hide this group, and not the item in it.
if (!q->isVisible() && !q->items().isEmpty()) {
q->extender()->itemAddedEvent(q);
q->show();
}
if (collapsed) {
q->extender()->itemRemovedEvent(item);
item->hide();
} else {
//the group isn't collapsed so show and readd this item to the extender, which takes
//care of placing the new item directly under the group widget.
q->extender()->itemAddedEvent(item);
item->show();
}
if (!q->isVisible() && (itemCount > 1 || !autoHide)) {
//show the group if needed, depending on autoHide policy.
q->extender()->itemAddedEvent(q);
q->show();
if (collapsed) {
q->extender()->itemRemovedEvent(q->items().first());
q->items().first()->hide();
}
}
}
}
void ExtenderGroupPrivate::removeItemFromGroup(Plasma::ExtenderItem *item)
{
if (item->group() == q) {
if (q->items().count() < 2 && autoHide && !q->isDetached()) {
if (q->items().isEmpty() && autoHide && !q->isDetached()) {
q->extender()->itemRemovedEvent(q);
q->hide();
}
if (q->items().count() == 1 && autoHide) {
q->extender()->itemAddedEvent(q->items().first());
q->items().first()->show();
}
}
}

View File

@ -188,6 +188,12 @@ void ExtenderItem::setWidget(QGraphicsItem *widget)
widget->setPos(QPointF(d->bgLeft + d->dragLeft, panelSize.height() + d->bgTop));
d->widget = widget;
d->updateSizeHints();
/**
if (isGroup() && !isVisible()) {
widget->hide();
}
*/
}
QGraphicsItem *ExtenderItem::widget() const
@ -284,10 +290,24 @@ Extender *ExtenderItem::extender() const
void ExtenderItem::setGroup(ExtenderGroup *group)
{
if (isGroup()) {
//nesting extender groups is just insane. I don't think we'd even want to support that.
kWarning() << "Nesting ExtenderGroups is not supported";
return;
}
d->group = group;
config().writeEntry("group", group->name());
if (!group->isDetached()) {
if (group) {
config().writeEntry("group", group->name());
//TODO: move to another extender if the group we set is actually detached.
if (group->extender() != extender()) {
kDebug() << "moving to another extender because we're joining a detached group.";
setExtender(group->extender());
}
group->d->addItemToGroup(this);
} else {
config().deleteEntry("group");
}
}
@ -563,6 +583,15 @@ void ExtenderItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
}
}
ExtenderGroup *group = qobject_cast<ExtenderGroup*>(this);
QList<ExtenderItem*> childItems;
bool collapsedGroup;
if (isGroup()) {
collapsedGroup = group->d->collapsed;
group->collapseGroup();
childItems = group->items();
}
//and execute the drag.
QWidget *dragParent = extender()->d->applet->view();
QDrag *drag = new QDrag(dragParent);
@ -578,6 +607,18 @@ void ExtenderItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
d->extender->itemAddedEvent(this, curPos);
}
//invoke setGroup on all items belonging to this group, to make sure all children move to the
//new extender together with the group.
if (isGroup()) {
foreach (ExtenderItem *item, childItems) {
item->setGroup(group);
}
}
if (isGroup() && !collapsedGroup) {
group->expandGroup();
}
d->dragStarted = false;
}

View File

@ -173,7 +173,8 @@ class PLASMA_EXPORT ExtenderItem : public QGraphicsWidget
Extender *extender() const;
/**
* @param group the group you want this item to belong to.
* @param group the group you want this item to belong to. Note that you can't nest
* ExtenderGroups.
* @since 4.3
*/
void setGroup(ExtenderGroup *group);