convenience method for accurately determining where to pop something up

svn path=/trunk/KDE/kdelibs/; revision=1173191
This commit is contained in:
Aaron J. Seigo 2010-09-08 22:30:21 +00:00
parent d436911241
commit 5bddabe5d8
2 changed files with 95 additions and 0 deletions

View File

@ -299,6 +299,88 @@ void ContainmentActions::paste(QPointF scenePos, QPoint screenPos)
}
}
QPoint screenPosFromEvent(QEvent *event)
{
switch (event->type()) {
case QEvent::GraphicsSceneMousePress:
case QEvent::GraphicsSceneMouseRelease:
case QEvent::GraphicsSceneMouseDoubleClick:
return static_cast<QGraphicsSceneMouseEvent*>(event)->screenPos();
break;
case QEvent::GraphicsSceneWheel:
return static_cast<QGraphicsSceneWheelEvent*>(event)->screenPos();
break;
case QEvent::GraphicsSceneContextMenu:
return static_cast<QGraphicsSceneContextMenuEvent*>(event)->screenPos();
break;
default:
break;
}
return QPoint();
}
QPointF scenePosFromEvent(QEvent *event)
{
switch (event->type()) {
case QEvent::GraphicsSceneMousePress:
case QEvent::GraphicsSceneMouseRelease:
case QEvent::GraphicsSceneMouseDoubleClick:
return static_cast<QGraphicsSceneMouseEvent*>(event)->scenePos();
break;
case QEvent::GraphicsSceneWheel:
return static_cast<QGraphicsSceneWheelEvent*>(event)->scenePos();
break;
case QEvent::GraphicsSceneContextMenu:
return static_cast<QGraphicsSceneContextMenuEvent*>(event)->scenePos();
break;
default:
break;
}
return QPoint();
}
bool isNonSceneEvent(QEvent *event)
{
return dynamic_cast<QGraphicsSceneEvent *>(event) == 0;
}
QPoint ContainmentActions::popupPosition(const QSize &s, QEvent *event)
{
if (isNonSceneEvent(event)) {
return screenPosFromEvent(event);
}
Containment *c = containment();
if (!c) {
return screenPosFromEvent(event);
}
Applet *applet = c->d->appletAt(scenePosFromEvent(event));
QPoint screenPos = screenPosFromEvent(event);
QPoint pos = screenPos;
if (applet && containment()->d->isPanelContainment()) {
pos = applet->popupPosition(s);
if (event->type() != QEvent::GraphicsSceneContextMenu ||
static_cast<QGraphicsSceneContextMenuEvent *>(event)->reason() == QGraphicsSceneContextMenuEvent::Mouse) {
// if the menu pops up way away from the mouse press, then move it
// to the mouse press
if (c->formFactor() == Vertical) {
if (pos.y() + s.height() < screenPos.y()) {
pos.setY(screenPos.y());
}
} else if (c->formFactor() == Horizontal) {
if (pos.x() + s.width() < screenPos.x()) {
pos.setX(screenPos.x());
}
}
}
}
return pos;
}
bool ContainmentActions::event(QEvent *e)
{
if (e->type() == QEvent::ParentChange) {

View File

@ -201,6 +201,19 @@ class PLASMA_EXPORT ContainmentActions : public QObject
*/
static QString eventToString(QEvent *event);
/**
* Returns a popup position appropriate to the event and the size.
*
* @arg s size of the popup
* @arg event a pointer to the event that triggered the popup
* @return the preferred top-left position for the popup
* @since 4.6
*/
QPoint popupPosition(const QSize &s, QEvent *event);
/**
* @reimplemented
*/
bool event(QEvent *e);
protected: