23 #include <QApplication>
27 #include <QtGui/QGraphicsWidget>
28 #include <QtGui/QGraphicsLinearLayout>
29 #include <QtGui/QGraphicsSceneDragDropEvent>
37 #include "private/extender_p.h"
38 #include "private/extendergroup_p.h"
39 #include "private/extenderitemmimedata_p.h"
46 d(new ExtenderGroupPrivate(this))
54 d->resizeTimer =
new QTimer(
this);
55 d->resizeTimer->setSingleShot(
true);
57 config().writeEntry(
"isGroup",
true);
61 QGraphicsLinearLayout *lay =
static_cast<QGraphicsLinearLayout *
>(layout());
63 d->childsWidget->installEventFilter(
this);
64 d->layout =
new QGraphicsLinearLayout(
Qt::Vertical, d->childsWidget);
65 d->childsWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
66 lay->addItem(d->childsWidget);
68 QAction *expand =
new QAction(
this);
69 expand->setVisible(
false);
70 expand->setToolTip(i18n(
"Show this group."));
71 connect(expand, SIGNAL(triggered()),
this, SLOT(
expandGroup()));
74 QAction *collapse =
new QAction(
this);
75 collapse->setVisible(
true);
76 collapse->setToolTip(i18n(
"Hide this group."));
77 connect(collapse, SIGNAL(triggered()),
this, SLOT(
collapseGroup()));
84 groupName = item->
config().readEntry(
"group",
"");
85 if (!groupName.isEmpty() && groupName ==
name()) {
95 if (!
config().readEntry(
"groupCollapsed",
true)) {
100 this, SLOT(themeChanged()));
111 QList<ExtenderItem*> result;
113 if (item->
group() ==
this) {
129 if (autoHide &&
items().isEmpty()) {
132 }
else if (!autoHide && !isVisible()) {
140 return d->autoCollapse;
145 d->autoCollapse = collapse;
164 if (d->autoCollapse) {
167 if (d->collapsed ==
false) {
171 d->collapsed =
false;
172 config().writeEntry(
"groupCollapsed", d->collapsed);
173 action(
"collapse")->setVisible(
true);
174 action(
"expand")->setVisible(
false);
176 d->childsWidget->show();
177 static_cast<QGraphicsLinearLayout *
>(layout())->addItem(d->childsWidget);
181 if (item->
group() ==
this) {
192 if (d->autoCollapse) {
195 if (d->collapsed ==
true) {
200 config().writeEntry(
"groupCollapsed", d->collapsed);
201 action(
"collapse")->setVisible(
false);
202 action(
"expand")->setVisible(
true);
204 d->childsWidget->hide();
205 static_cast<QGraphicsLinearLayout *
>(layout())->removeItem(d->childsWidget);
209 if (item !=
this && item->
group() ==
this) {
223 if (watched == d->childsWidget && event->type() == QEvent::GraphicsSceneResize) {
224 static_cast<QGraphicsLayoutItem *
>(
extender()->d->scrollWidget)->updateGeometry();
225 if (!d->resizeTimer->isActive()) {
226 static_cast<QGraphicsLayoutItem *
>(
extender())->updateGeometry();
228 d->resizeTimer->start(0);
232 return ExtenderItem::eventFilter(watched, event);
237 if (event->mimeData()->hasFormat(ExtenderItemMimeData::mimeType())) {
246 if (event->mimeData()->hasFormat(ExtenderItemMimeData::mimeType())) {
247 const ExtenderItemMimeData *mimeData =
248 qobject_cast<
const ExtenderItemMimeData*>(
event->mimeData());
251 QPointF pos(event->pos());
252 if (d->spacerWidget && d->spacerWidget->geometry().contains(pos)) {
257 if (d->spacerWidget) {
258 d->layout->removeItem(d->spacerWidget);
261 int insertIndex = d->insertIndexFromPos(pos);
264 if (!d->spacerWidget) {
265 Spacer *
widget =
new Spacer(
this);
267 qreal left, top, right, bottom;
268 extender()->d->background->getMargins(left, top, right, bottom);
269 widget->setMargins(left, 4, right, 4);
271 widget->setMinimumSize(item->minimumSize());
272 widget->setPreferredSize(item->preferredSize());
273 widget->setMaximumSize(item->maximumSize());
274 widget->setSizePolicy(item->sizePolicy());
277 d->layout->insertItem(insertIndex, d->spacerWidget);
279 extender()->d->setPositionFromDragPosition(event->scenePos());
286 if (event->mimeData()->hasFormat(ExtenderItemMimeData::mimeType())) {
287 const ExtenderItemMimeData *mimeData =
288 qobject_cast<
const ExtenderItemMimeData*>(
event->mimeData());
291 mimeData->extenderItem()->setGroup(
this, event->pos());
292 QApplication::restoreOverrideCursor();
293 d->layout->removeItem(d->spacerWidget);
294 d->spacerWidget->deleteLater();
302 if (event->mimeData()->hasFormat(ExtenderItemMimeData::mimeType())) {
303 if (d->spacerWidget) {
304 d->layout->removeItem(d->spacerWidget);
305 d->spacerWidget->deleteLater();
311 ExtenderGroupPrivate::ExtenderGroupPrivate(
ExtenderGroup *group)
321 ExtenderGroupPrivate::~ExtenderGroupPrivate()
327 if (item->
group() == q) {
328 item->setParentItem(childsWidget);
329 item->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
330 layout->insertItem(insertIndexFromPos(pos), item);
333 if (!q->isVisible() && !q->items().isEmpty()) {
337 childsWidget->setVisible(!q->isGroupCollapsed());
339 if (!q->isGroupCollapsed()) {
340 layout->updateGeometry();
341 static_cast<QGraphicsLayoutItem *
>(q)->updateGeometry();
342 static_cast<QGraphicsLayoutItem *
>(childsWidget)->updateGeometry();
343 static_cast<QGraphicsLayoutItem *
>(q->extender()->d->scrollWidget)->updateGeometry();
346 q->extender()->d->adjustSize();
352 if (item->
group() == q) {
353 if (q->items().isEmpty() && autoHide && !q->isDetached()) {
354 q->extender()->itemRemovedEvent(q);
357 layout->removeItem(item);
358 item->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
360 layout->updateGeometry();
361 static_cast<QGraphicsLayoutItem *
>(q)->updateGeometry();
362 static_cast<QGraphicsLayoutItem *
>(childsWidget)->updateGeometry();
363 static_cast<QGraphicsLayoutItem *
>(q->extender()->d->scrollWidget)->updateGeometry();
369 void ExtenderGroupPrivate::themeChanged()
371 svg->setImagePath(
"widgets/configuration-icons");
374 q->action(
"expand")->setIcon(QIcon(svg->pixmap(
"restore")));
375 q->action(
"collapse")->setIcon(QIcon(svg->pixmap(
"collapse")));
378 int ExtenderGroupPrivate::insertIndexFromPos(
const QPointF &pos)
const
380 int insertIndex = -1;
383 if (pos != QPointF(-1, -1)) {
384 for (
int i = 0; i < layout->count(); ++i) {
385 QRectF siblingGeometry = layout->itemAt(i)->geometry();
386 qreal middle = (siblingGeometry.top() + siblingGeometry.bottom()) / 2.0;
387 if (pos.y() < middle) {
390 }
else if (pos.y() <= siblingGeometry.bottom()) {
402 #include "extendergroup.moc"
The applet is constrained horizontally, but can expand vertically.
void expandGroup()
Expands this group to show all ExtenderItems that are contained in this group.
Extends applets to allow detachable parts.
void setGroup(ExtenderGroup *group)
void setCollapsed(bool collapsed)
Collapse or expand the extender item.
QList< ExtenderItem * > items() const
KConfigGroup config() const
fetch the configuration of this widget.
void addAction(const QString &name, QAction *action)
void resizeEvent(QGraphicsSceneResizeEvent *event)
virtual void itemRemovedEvent(ExtenderItem *item)
Get's called after an item has been removed from this extender.
QGraphicsItem * widget() const
QAction * action(const QString &name) const
bool isGroupCollapsed() const
bool isAutoCollapse() const
void setAutoHide(bool autoHide)
ExtenderGroup * group() const
void setGroupCollapsed(bool collapsed)
expands or collapses this group
bool eventFilter(QObject *watched, QEvent *event)
void resizeEvent(QGraphicsSceneResizeEvent *event)
virtual void itemAddedEvent(ExtenderItem *item, const QPointF &pos=QPointF(-1,-1))
Get's called after an item has been added to this extender.
void dragEnterEvent(QGraphicsSceneDragDropEvent *event)
void dragMoveEvent(QGraphicsSceneDragDropEvent *event)
void dropEvent(QGraphicsSceneDragDropEvent *event)
void dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
ExtenderGroup(Extender *parent, uint groupId=0)
Creates a group.
static Theme * defaultTheme()
Singleton pattern accessor.
Allows for grouping of extender items.
void collapseGroup()
Collapses this group to hide all ExtenderItems that are contained in this group, and shows the summar...
Provides detachable items for an Extender.
A theme aware image-centric SVG class.
Extender * extender() const
void setAutoCollapse(bool collapse)