it's possible to drop items into extendergroups.

still not possible to chose the position, will come

svn path=/trunk/KDE/kdelibs/; revision=1113368
This commit is contained in:
Marco Martin 2010-04-10 16:15:52 +00:00
parent 37c4df0145
commit 0c989d306f
3 changed files with 129 additions and 35 deletions

View File

@ -48,48 +48,38 @@
namespace Plasma namespace Plasma
{ {
//TODO: copied from panel containment. We'll probably want a spacer widget in libplasma for 4.3. Spacer::Spacer(QGraphicsItem *parent)
class Spacer : public QGraphicsWidget : QGraphicsWidget(parent)
{ {
public: }
Spacer(Extender *parent)
: QGraphicsWidget(parent)
{
}
~Spacer() Spacer::~Spacer()
{ {
} }
void setMargins(qreal left, qreal top, qreal right, qreal bottom) void Spacer::setMargins(qreal left, qreal top, qreal right, qreal bottom)
{ {
m_left = left; m_left = left;
m_top = top; m_top = top;
m_right = right; m_right = right;
m_bottom = bottom; m_bottom = bottom;
} }
protected:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget * widget = 0)
{
Q_UNUSED(option)
Q_UNUSED(widget)
painter->setRenderHint(QPainter::Antialiasing); void Spacer::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
QPainterPath p = Plasma::PaintUtils::roundedRectangle( {
contentsRect().adjusted(m_left, m_top, -m_right, -m_bottom), 4); Q_UNUSED(option)
Q_UNUSED(widget)
QColor c = Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor); painter->setRenderHint(QPainter::Antialiasing);
c.setAlphaF(0.3); QPainterPath p = Plasma::PaintUtils::roundedRectangle(
painter->fillPath(p, c); 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) Extender::Extender(Applet *applet)
: QGraphicsWidget(applet), : QGraphicsWidget(applet),

View File

@ -20,10 +20,12 @@
#include "extendergroup.h" #include "extendergroup.h"
#include <QAction> #include <QAction>
#include <QApplication>
#include <QString> #include <QString>
#include <QList> #include <QList>
#include <QtGui/QGraphicsWidget> #include <QtGui/QGraphicsWidget>
#include <QtGui/QGraphicsLinearLayout> #include <QtGui/QGraphicsLinearLayout>
#include <QtGui/QGraphicsSceneDragDropEvent>
#include "applet.h" #include "applet.h"
#include "extender.h" #include "extender.h"
@ -31,7 +33,9 @@
#include "theme.h" #include "theme.h"
#include "widgets/scrollwidget.h" #include "widgets/scrollwidget.h"
#include "private/extender_p.h"
#include "private/extendergroup_p.h" #include "private/extendergroup_p.h"
#include "private/extenderitemmimedata_p.h"
namespace Plasma namespace Plasma
{ {
@ -47,6 +51,8 @@ ExtenderGroup::ExtenderGroup(Extender *parent, uint groupId)
config().writeEntry("isGroup", true); config().writeEntry("isGroup", true);
setAcceptDrops(true);
QGraphicsLinearLayout *lay = static_cast<QGraphicsLinearLayout *>(layout()); QGraphicsLinearLayout *lay = static_cast<QGraphicsLinearLayout *>(layout());
d->scrollWidget = new ScrollWidget(this); d->scrollWidget = new ScrollWidget(this);
d->scrollWidget->show(); d->scrollWidget->show();
@ -216,8 +222,80 @@ bool ExtenderGroup::eventFilter(QObject *watched, QEvent *event)
return ExtenderItem::eventFilter(watched, 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<const ExtenderItemMimeData*>(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<const ExtenderItemMimeData*>(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) ExtenderGroupPrivate::ExtenderGroupPrivate(ExtenderGroup *group)
: q(group), : q(group),
spacerWidget(0),
svg(new Svg(group)), svg(new Svg(group)),
collapsed(true), collapsed(true),
autoHide(true), autoHide(true),
@ -273,6 +351,28 @@ void ExtenderGroupPrivate::themeChanged()
q->action("collapse")->setIcon(QIcon(svg->pixmap("collapse"))); 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 } // Plasma namespace
#include "extendergroup.moc" #include "extendergroup.moc"

View File

@ -117,6 +117,10 @@ class PLASMA_EXPORT ExtenderGroup : public ExtenderItem
protected: protected:
void resizeEvent(QGraphicsSceneResizeEvent *event); void resizeEvent(QGraphicsSceneResizeEvent *event);
bool eventFilter(QObject *watched, QEvent *event); bool eventFilter(QObject *watched, QEvent *event);
void dragEnterEvent(QGraphicsSceneDragDropEvent *event);
void dragMoveEvent(QGraphicsSceneDragDropEvent *event);
void dragLeaveEvent(QGraphicsSceneDragDropEvent *event);
void dropEvent(QGraphicsSceneDragDropEvent *event);
private: private:
ExtenderGroupPrivate * const d; ExtenderGroupPrivate * const d;