From a5c48ddc4feb1223cd0a6740edbdb5580d756a74 Mon Sep 17 00:00:00 2001 From: Rob Scheepmaker Date: Sun, 26 Apr 2009 17:03:15 +0000 Subject: [PATCH] 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 --- extender.cpp | 12 +++++++++++- extender.h | 7 +++++++ extendergroup.cpp | 32 +++++++++----------------------- extenderitem.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++-- extenderitem.h | 3 ++- 5 files changed, 72 insertions(+), 27 deletions(-) diff --git a/extender.cpp b/extender.cpp index e45acc791..54ace0e3f 100644 --- a/extender.cpp +++ b/extender.cpp @@ -170,6 +170,11 @@ ExtenderItem *Extender::item(const QString &name) const return 0; } +ExtenderGroup *Extender::group(const QString &name) const +{ + return qobject_cast(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(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 { diff --git a/extender.h b/extender.h index 5c0900fe4..8b798ff2b 100644 --- a/extender.h +++ b/extender.h @@ -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 diff --git a/extendergroup.cpp b/extendergroup.cpp index 4955204cd..d5e1e1ad9 100644 --- a/extendergroup.cpp +++ b/extendergroup.cpp @@ -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(); - } } } diff --git a/extenderitem.cpp b/extenderitem.cpp index 33075594e..b77e02d60 100644 --- a/extenderitem.cpp +++ b/extenderitem.cpp @@ -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(this); + QList 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; } diff --git a/extenderitem.h b/extenderitem.h index 12b047b34..182b853f0 100644 --- a/extenderitem.h +++ b/extenderitem.h @@ -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);