From ee37d19811dc406ca04d734ea540453c56aebfad Mon Sep 17 00:00:00 2001 From: Marco Martin Date: Mon, 21 Jan 2008 11:42:08 +0000 Subject: [PATCH] better positioning of tooltips in case of panels not in bottom position and a new function in widget called popupPosition(const QSize s) this will be used every time a tooltip or popup menu has to be positioned svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=764299 --- widgets/tooltip.cpp | 4 +++- widgets/widget.cpp | 41 +++++++++++++++++++++++++++++++++++------ widgets/widget.h | 8 ++++++++ 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/widgets/tooltip.cpp b/widgets/tooltip.cpp index 8cd8fd13b..b25ecd262 100644 --- a/widgets/tooltip.cpp +++ b/widgets/tooltip.cpp @@ -82,7 +82,9 @@ void ToolTip::show(const QPoint &location, Plasma::Widget *widget) { d->currentWidget = widget; setData(widget->toolTip()); - move(location.x(), location.y() - sizeHint().height()); + + move(location.x(), location.y()); + if (d->isShown) { // Don't delay if the tooltip is already shown(i.e. moving from one task to another) // Qt doesn't seem to like visible tooltips moving though, so hide it and then diff --git a/widgets/widget.cpp b/widgets/widget.cpp index c2c297db8..c2b91013f 100644 --- a/widgets/widget.cpp +++ b/widgets/widget.cpp @@ -38,6 +38,8 @@ #include "layouts/freelayout.h" #include "plasma/plasma.h" +#include "plasma/view.h" +#include "plasma/containment.h" #include "tooltip_p.h" namespace Plasma @@ -504,9 +506,7 @@ void Widget::setToolTip(const ToolTipData &tip) { d->toolTip = tip; if (ToolTip::self()->currentWidget() == this) { - QPoint viewPos = view()->mapFromScene(scenePos()); - QPoint globalPos = view()->mapToGlobal(viewPos); - ToolTip::self()->show(globalPos, this); + ToolTip::self()->show(popupPosition(ToolTip::self()->sizeHint()), this); } } @@ -543,6 +543,35 @@ void Widget::managingLayoutChanged() } } +QPoint Widget::popupPosition(const QSize s) const +{ + QPoint pos = view()->mapFromScene(scenePos()); + pos = view()->mapToGlobal(pos); + Plasma::View *pv = dynamic_cast(view()); + + Plasma::Location loc = Floating; + if (pv) { + loc = pv->containment()->location(); + } + + switch (loc) { + case BottomEdge: + return QPoint(pos.x(), pos.y() - s.height()); + case TopEdge: + return QPoint(pos.x(), pos.y() + (int)size().height()); + case LeftEdge: + return QPoint(pos.x() + (int)size().width(), pos.y()); + case RightEdge: + return QPoint(pos.x() - s.width(), pos.y()); + default: + if (pos.y() > 0) { + return QPoint(pos.x(), pos.y()-s.height()); + } else { + return QPoint(pos.x(), pos.y()+(int)size().height()); + } + } +} + void Widget::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) { // HACK: QGraphicsItem's documentation says that the event will be passed @@ -578,9 +607,9 @@ bool Widget::sceneEvent(QEvent *event) // initialized, in which case view() will return 0. QGraphicsView *parentView = view(); if (parentView) { - QPoint viewPos = parentView->mapFromScene(scenePos()); - QPoint globalPos = parentView->mapToGlobal(viewPos); - ToolTip::self()->show(globalPos, this); + ToolTip *tip = ToolTip::self(); + tip->adjustSize(); + tip->show(popupPosition(tip->size()), this); } break; diff --git a/widgets/widget.h b/widgets/widget.h index 3cbeaa6ea..7a706e115 100644 --- a/widgets/widget.h +++ b/widgets/widget.h @@ -288,6 +288,14 @@ TODO: implement once we decide how to handle the font system */ void setToolTip( const ToolTipData &dt ); + /** + * Recomended position for a popup window like a menu or a tooltip + * given its size + * @param s size of the popup + * @returns recomended position + */ + QPoint popupPosition(const QSize s) const; + protected: /** * Paints the widget