Couple of improvements to applet drag & drop:

* when dropping on another containment, the center of the applet is used to determine where 
to drop the applet, instead of the position of the mousepointer.
* movement now works correctly if multiple drag&drops are performed while not removing the 
mouse away from the applet handle.
* now works correctly on rotated applets.
* various code style fixes.


svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=808379
This commit is contained in:
Rob Scheepmaker 2008-05-16 13:28:33 +00:00
parent 9fda5b685d
commit 9f6d033578
3 changed files with 83 additions and 65 deletions

View File

@ -1232,10 +1232,11 @@ QRect Applet::screenRect() const
bottomRight.setX(bottomRight.x() + size().width()); bottomRight.setX(bottomRight.x() + size().width());
bottomRight.setY(bottomRight.y() + size().height()); bottomRight.setY(bottomRight.y() + size().height());
QPoint tL = view()->mapToGlobal(view()->mapFromScene(pos())); QPoint tL = view()->mapToGlobal(containment()->view()->mapFromScene(pos()));
QPoint bR = view()->mapToGlobal(view()->mapFromScene(bottomRight)); QPoint bR = view()->mapToGlobal(containment()->view()->mapFromScene(bottomRight));
kDebug() << "screenRect = " << QPoint(tL.x(), tL.y()), QSize(bR.x() - tL.x(), bR.y() - tL.y());
return QRect(QPoint(0, 0), QSize(bR.x() - tL.x(), bR.y() - tL.y())); return QRect(QPoint(tL.x(), tL.y()), QSize(bR.x() - tL.x(), bR.y() - tL.y()));
} }
void Applet::raise() void Applet::raise()

View File

@ -93,6 +93,16 @@ AppletHandle::AppletHandle(Containment *parent, Applet *applet)
m_hoverTimer->setSingleShot(true); m_hoverTimer->setSingleShot(true);
m_hoverTimer->setInterval(333); m_hoverTimer->setInterval(333);
//This variable contains the bounding rect of the applet in
//screen coordinates. Only set this once: the applet itself
//will only update this after the handle is destroyed, and we
//will track changes to it in the mouseMoveEvent.
m_screenRect = m_applet->screenRect();
//add a small margin to the screenRect so, that when we check
//if we are over another view than the source view, we won't
//detect the top level view.
m_screenRect.adjust(-2, -2, 2, 2);
connect(m_hoverTimer, SIGNAL(timeout()), this, SLOT(fadeIn())); connect(m_hoverTimer, SIGNAL(timeout()), this, SLOT(fadeIn()));
connect(m_applet, SIGNAL(destroyed(QObject*)), this, SLOT(appletDestroyed())); connect(m_applet, SIGNAL(destroyed(QObject*)), this, SLOT(appletDestroyed()));
@ -274,11 +284,18 @@ void AppletHandle::mousePressEvent(QGraphicsSceneMouseEvent *event)
setZValue(m_applet->zValue()); setZValue(m_applet->zValue());
} }
event->accept(); event->accept();
update();
//set mousePos to the position in the applet, in screencoords, so it becomes easy //set mousePos to the position in the applet, in screencoords, so it becomes easy
//to reposition the toplevel view to the correct position. //to reposition the toplevel view to the correct position.
m_mousePos = event->screenPos() - m_applet->view()->mapToGlobal( QPoint localpos = m_applet->containment()->view()
m_applet->view()->mapFromScene(m_applet->pos())); ->mapFromScene(m_applet->pos() + m_pos);
update(); m_mousePos = event->screenPos() - m_applet->containment()->view()
->mapToGlobal(localpos);
//set the starting position
m_pos = pos();
return; return;
} }
@ -349,18 +366,18 @@ void AppletHandle::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
} }
//find out if we were dropped on a panel or something //find out if we were dropped on a panel or something
Plasma::View *v = Plasma::View::topLevelViewAt(event->screenPos()); Plasma::View *v = Plasma::View::topLevelViewAt(m_screenRect.center());
if (v) { if (v) {
Containment *c = v->containment(); Containment *c = v->containment();
QPoint pos = v->mapFromGlobal(event->screenPos() - m_mousePos); //We decide where we've been dropped by looking at the center of the
//applet.
QPoint pos = v->mapFromGlobal(m_screenRect.center());
//XXX the dashboard view won't give us a //XXX the dashboard view won't give us a containment. if it did,
//containment. if it did, this could //this could break shit.
//break shit.
if (c && c != m_containment) { if (c && c != m_containment) {
//we actually have been dropped on another //we actually have been dropped on another containment, so
//containment, so move there //move there: we have a screenpos, we need a scenepos
//we have a screenpos, we need a scenepos
//FIXME how reliable is this transform? //FIXME how reliable is this transform?
switchContainment(c, v->mapToScene(pos)); switchContainment(c, v->mapToScene(pos));
} }
@ -410,34 +427,35 @@ void AppletHandle::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
return; return;
} }
QPointF curPos = transform().map(event->pos()); //Track how much the mouse has moved.
QPointF lastPos = transform().map(event->lastPos()); QPointF deltaScene = event->scenePos() - event->lastScenePos();
QPointF delta = curPos-lastPos;
if (m_pressedButton == MoveButton) { if (m_pressedButton == MoveButton) {
if (m_pos == QPointF(0, 0)) { m_pos += deltaScene;
m_pos = pos();
}
m_pos = m_pos + delta;
QRect screenRect = QRect(event->screenPos() - m_mousePos, //set the screenRect correctly. the screenRect contains the bounding
//rect of the applet in screen coordinates. m_mousePos contains the
//position of the mouse relative to the applet, in screen coords.
m_screenRect = QRect(event->screenPos() - m_mousePos,
m_applet->screenRect().size()); m_applet->screenRect().size());
kDebug() << "screenRect = " << screenRect;
//add a 1 pixelmargin to the screenRect so, that when we check //add a small margin, to avoid detecting the toplevel view when checking
//if we are over another view than the source view, we won't //if we have to move to a toplevel window.
//detect the top level view. m_screenRect.adjust(-2, -2, 2, 2);
screenRect.adjust(-1, -1, 1, 1);
kDebug() << "m_screenRect = " << m_screenRect;
//Are we moving out of the current view? //Are we moving out of the current view?
bool toTopLevel = goTopLevel(screenRect.topLeft()) || bool toTopLevel = goTopLevel(m_screenRect.topLeft()) ||
goTopLevel(screenRect.topRight()) || goTopLevel(m_screenRect.topRight()) ||
goTopLevel(screenRect.bottomLeft()) || goTopLevel(m_screenRect.bottomLeft()) ||
goTopLevel(screenRect.bottomRight()); goTopLevel(m_screenRect.bottomRight());
kDebug() << "toTopLevel = " << toTopLevel;
if (!toTopLevel) { if (!toTopLevel) {
setPos(m_pos); setPos(m_pos);
if (m_topview) { if (m_topview) {
//We were on a toplevel view, but are moving back on the scene
//again. destroy the toplevel view:
m_topview->hide(); m_topview->hide();
delete m_topview; delete m_topview;
m_topview = 0; m_topview = 0;
@ -446,56 +464,54 @@ void AppletHandle::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
} else { } else {
if (!m_topview) { //create a new toplevel view if (!m_topview) { //create a new toplevel view
m_topview = new View(m_applet->containment(), -1, 0); m_topview = new View(m_applet->containment(), -1, 0);
m_topview->setTrackContainmentChanges(false); m_topview->setTrackContainmentChanges(false);
m_topview->setWindowFlags(Qt::ToolTip | Qt::FramelessWindowHint
m_topview->setWindowFlags(Qt::Widget | Qt::FramelessWindowHint
| Qt::WindowStaysOnTopHint); | Qt::WindowStaysOnTopHint);
KWindowSystem::setState(m_topview->winId(), NET::SkipTaskbar | NET::SkipPager);
m_topview->setWallpaperEnabled(false); m_topview->setWallpaperEnabled(false);
m_topview->resize(m_screenRect.size());
m_topview->resize(m_applet->screenRect().size());
kDebug() << "resizing topview to: " << m_applet->screenRect().size();
//TODO: when zoomed out, this doesn't work correctly
m_topview->setSceneRect(m_applet->sceneBoundingRect()); m_topview->setSceneRect(m_applet->sceneBoundingRect());
m_topview->centerOn(m_applet); m_topview->centerOn(m_applet);
//We might have to scale the qgv, because we might be zoomed out.
qreal scale = m_applet->screenRect().width() / m_applet->boundingRect().width(); //We might have to scale the view, because we might be zoomed out.
kDebug() << "scaling topview with: " << scale; qreal scale = m_screenRect.width() / m_applet->boundingRect().width();
m_topview->scale(scale, scale); m_topview->scale(scale, scale);
//Paint a mask based on the applets shape. //Paint a mask based on the applets shape.
//TODO: might be useful to have this in applet? //TODO: I think it's nicer to have this functionality in Applet.
QBitmap bitmap(m_applet->screenRect().size()); //TODO: When the corona tiled background is disabled, disable the
//mask when compositing is enabled.
//FIXME: the mask doesn't function correctly when zoomed out.
QBitmap bitmap(m_screenRect.size());
QPainter * shapePainter = new QPainter(); QPainter * shapePainter = new QPainter();
shapePainter->begin(&bitmap); shapePainter->begin(&bitmap);
shapePainter->fillRect(0, 0, m_applet->screenRect().width(), shapePainter->fillRect(0, 0, m_screenRect.width(),
m_applet->screenRect().height(), m_screenRect.height(),
Qt::white); Qt::white);
shapePainter->setBrush(Qt::black); shapePainter->setBrush(Qt::black);
shapePainter->drawPath(m_applet->shape()); shapePainter->drawPath(m_applet->shape());
shapePainter->end(); shapePainter->end();
delete shapePainter; delete shapePainter;
m_topview->setMask(bitmap); m_topview->setMask(bitmap);
m_topview->show(); m_topview->show();
m_applet->setGhostView(m_applet->containment()->view()); m_applet->setGhostView(m_applet->containment()->view());
//TODO: non compositing users are screwed: masking looks terrible.
//Consider always enabling the applet background. Stuff like the analog clock
//looks absolutely terrible when masked, while the minor rounded corners of most
//themes should look quite ok. I said should, since shape() doesn't really
//function correctly right now for applets drawing standard backgrounds.
} }
//m_topview->move(m_topview.pos() + deltaScreen);
m_topview->move((event->screenPos() - m_mousePos)); m_topview->setGeometry(m_screenRect);
} }
} else if (m_pressedButton == RotateButton || } else if (m_pressedButton == RotateButton ||
m_pressedButton == ResizeButton) { m_pressedButton == ResizeButton) {
if (_k_distanceForPoint(delta) <= 1.0) { if (_k_distanceForPoint(deltaScene) <= 1.0) {
return; return;
} }
@ -741,10 +757,10 @@ void AppletHandle::calculateSize()
((ICON_SIZE + ICON_MARGIN) * 3) + ((ICON_SIZE + ICON_MARGIN) * 3) +
ICON_MARGIN; // that last margin is blank space before the close button ICON_MARGIN; // that last margin is blank space before the close button
if (m_rect.height() < requiredHeight) { if (m_rect.height() < requiredHeight) {
float delta = requiredHeight - m_rect.height(); float deltaScene = requiredHeight - m_rect.height();
delta = delta/2.0; deltaScene = deltaScene/2.0;
if (delta > 0.0) { if (deltaScene > 0.0) {
m_rect.adjust(0.0, -delta, 0.0, delta); m_rect.adjust(0.0, -deltaScene, 0.0, deltaScene);
} }
} }

View File

@ -99,9 +99,10 @@ class AppletHandle : public QObject, public QGraphicsItem
bool m_buttonsOnRight; bool m_buttonsOnRight;
bool m_pendingFade; bool m_pendingFade;
View *m_topview; View *m_topview;
QPoint m_mousePos; //the position of the mousecursor relative to the
//applets position. QPoint m_mousePos; //mousepos relative to applet
QPointF m_pos; QRect m_screenRect; //boudingrect of applet in screencoords
QPointF m_pos; //current position of applet in sceneCoords
}; };
} }