From 0c989d306fbcbae0cbc2fc9960d87720c2b5c6f9 Mon Sep 17 00:00:00 2001 From: Marco Martin Date: Sat, 10 Apr 2010 16:15:52 +0000 Subject: [PATCH] it's possible to drop items into extendergroups. still not possible to chose the position, will come svn path=/trunk/KDE/kdelibs/; revision=1113368 --- extenders/extender.cpp | 60 +++++++++------------- extenders/extendergroup.cpp | 100 ++++++++++++++++++++++++++++++++++++ extenders/extendergroup.h | 4 ++ 3 files changed, 129 insertions(+), 35 deletions(-) diff --git a/extenders/extender.cpp b/extenders/extender.cpp index 9ec9f9468..b99587928 100644 --- a/extenders/extender.cpp +++ b/extenders/extender.cpp @@ -48,48 +48,38 @@ namespace Plasma { -//TODO: copied from panel containment. We'll probably want a spacer widget in libplasma for 4.3. -class Spacer : public QGraphicsWidget +Spacer::Spacer(QGraphicsItem *parent) + : QGraphicsWidget(parent) { - public: - Spacer(Extender *parent) - : QGraphicsWidget(parent) - { - } +} - ~Spacer() - { - } +Spacer::~Spacer() +{ +} - void setMargins(qreal left, qreal top, qreal right, qreal bottom) - { - m_left = left; - m_top = top; - m_right = right; - m_bottom = bottom; - } +void Spacer::setMargins(qreal left, qreal top, qreal right, qreal bottom) +{ + m_left = left; + m_top = top; + m_right = right; + m_bottom = bottom; +} - protected: - void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget * widget = 0) - { - Q_UNUSED(option) - Q_UNUSED(widget) - painter->setRenderHint(QPainter::Antialiasing); - QPainterPath p = Plasma::PaintUtils::roundedRectangle( - contentsRect().adjusted(m_left, m_top, -m_right, -m_bottom), 4); +void Spacer::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + Q_UNUSED(option) + Q_UNUSED(widget) - QColor c = Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor); - c.setAlphaF(0.3); - painter->fillPath(p, c); - } + painter->setRenderHint(QPainter::Antialiasing); + QPainterPath p = Plasma::PaintUtils::roundedRectangle( + contentsRect().adjusted(m_left, m_top, -m_right, -m_bottom), 4); + + QColor c = Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor); + c.setAlphaF(0.3); + painter->fillPath(p, c); +} - private: - qreal m_left; - qreal m_top; - qreal m_right; - qreal m_bottom; -}; Extender::Extender(Applet *applet) : QGraphicsWidget(applet), diff --git a/extenders/extendergroup.cpp b/extenders/extendergroup.cpp index 25f190ce4..abe30f1f1 100644 --- a/extenders/extendergroup.cpp +++ b/extenders/extendergroup.cpp @@ -20,10 +20,12 @@ #include "extendergroup.h" #include +#include #include #include #include #include +#include #include "applet.h" #include "extender.h" @@ -31,7 +33,9 @@ #include "theme.h" #include "widgets/scrollwidget.h" +#include "private/extender_p.h" #include "private/extendergroup_p.h" +#include "private/extenderitemmimedata_p.h" namespace Plasma { @@ -47,6 +51,8 @@ ExtenderGroup::ExtenderGroup(Extender *parent, uint groupId) config().writeEntry("isGroup", true); + setAcceptDrops(true); + QGraphicsLinearLayout *lay = static_cast(layout()); d->scrollWidget = new ScrollWidget(this); d->scrollWidget->show(); @@ -216,8 +222,80 @@ bool ExtenderGroup::eventFilter(QObject *watched, QEvent *event) return ExtenderItem::eventFilter(watched, event); } +void ExtenderGroup::dragEnterEvent(QGraphicsSceneDragDropEvent *event) +{ + if (event->mimeData()->hasFormat(ExtenderItemMimeData::mimeType())) { + event->accept(); + + dragMoveEvent(event); + } +} + +void ExtenderGroup::dragMoveEvent(QGraphicsSceneDragDropEvent *event) +{ + if (event->mimeData()->hasFormat(ExtenderItemMimeData::mimeType())) { + const ExtenderItemMimeData *mimeData = + qobject_cast(event->mimeData()); + + if (mimeData) { + QPointF pos(event->pos()); + if (d->spacerWidget && d->spacerWidget->geometry().contains(pos)) { + return; + } + + //Make sure we remove any spacer that might already be in the layout. + if (d->spacerWidget) { + d->layout->removeItem(d->spacerWidget); + } + + int insertIndex = d->insertIndexFromPos(pos); + + //Create a widget that functions as spacer, and add that to the layout. + if (!d->spacerWidget) { + Spacer *widget = new Spacer(this); + ExtenderItem *item = mimeData->extenderItem(); + qreal left, top, right, bottom; + extender()->d->background->getMargins(left, top, right, bottom); + widget->setMargins(left, 4, right, 4); + + widget->setMinimumSize(item->minimumSize()); + widget->setPreferredSize(item->preferredSize()); + widget->setMaximumSize(item->maximumSize()); + widget->setSizePolicy(item->sizePolicy()); + d->spacerWidget = widget; + } + d->layout->insertItem(insertIndex, d->spacerWidget); + } + } +} + +void ExtenderGroup::dropEvent(QGraphicsSceneDragDropEvent *event) +{ + if (event->mimeData()->hasFormat(ExtenderItemMimeData::mimeType())) { + const ExtenderItemMimeData *mimeData = + qobject_cast(event->mimeData()); + + if (mimeData) { + mimeData->extenderItem()->setGroup(this); + QApplication::restoreOverrideCursor(); + d->layout->removeItem(d->spacerWidget); + d->spacerWidget->deleteLater(); + } + } +} + +void ExtenderGroup::dragLeaveEvent(QGraphicsSceneDragDropEvent *event) +{ + if (event->mimeData()->hasFormat(ExtenderItemMimeData::mimeType())) { + if (d->spacerWidget) { + d->layout->removeItem(d->spacerWidget); + } + } +} + ExtenderGroupPrivate::ExtenderGroupPrivate(ExtenderGroup *group) : q(group), + spacerWidget(0), svg(new Svg(group)), collapsed(true), autoHide(true), @@ -273,6 +351,28 @@ void ExtenderGroupPrivate::themeChanged() q->action("collapse")->setIcon(QIcon(svg->pixmap("collapse"))); } +int ExtenderGroupPrivate::insertIndexFromPos(const QPointF &pos) const +{ + int insertIndex = -1; + + //XXX: duplicated from panel + if (pos != QPointF(-1, -1)) { + for (int i = 0; i < layout->count(); ++i) { + QRectF siblingGeometry = layout->itemAt(i)->geometry(); + qreal middle = (siblingGeometry.top() + siblingGeometry.bottom()) / 2.0; + if (pos.y() < middle) { + insertIndex = i; + break; + } else if (pos.y() <= siblingGeometry.bottom()) { + insertIndex = i + 1; + break; + } + } + } + + return insertIndex; +} + } // Plasma namespace #include "extendergroup.moc" diff --git a/extenders/extendergroup.h b/extenders/extendergroup.h index 3385855ac..0048d9430 100644 --- a/extenders/extendergroup.h +++ b/extenders/extendergroup.h @@ -117,6 +117,10 @@ class PLASMA_EXPORT ExtenderGroup : public ExtenderItem protected: void resizeEvent(QGraphicsSceneResizeEvent *event); bool eventFilter(QObject *watched, QEvent *event); + void dragEnterEvent(QGraphicsSceneDragDropEvent *event); + void dragMoveEvent(QGraphicsSceneDragDropEvent *event); + void dragLeaveEvent(QGraphicsSceneDragDropEvent *event); + void dropEvent(QGraphicsSceneDragDropEvent *event); private: ExtenderGroupPrivate * const d;