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
{
//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),

View File

@ -20,10 +20,12 @@
#include "extendergroup.h"
#include <QAction>
#include <QApplication>
#include <QString>
#include <QList>
#include <QtGui/QGraphicsWidget>
#include <QtGui/QGraphicsLinearLayout>
#include <QtGui/QGraphicsSceneDragDropEvent>
#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<QGraphicsLinearLayout *>(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<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)
: 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"

View File

@ -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;