diff --git a/applet.cpp b/applet.cpp index 1e076d675..59961b59d 100644 --- a/applet.cpp +++ b/applet.cpp @@ -411,13 +411,16 @@ QGraphicsView *Applet::view() const return 0; } + QGraphicsView *found = 0; foreach (QGraphicsView *view, scene()->views()) { if (view->sceneRect().intersects(sceneBoundingRect()) || view->sceneRect().contains(scenePos())) { - return view; + if (!found || view->isActiveWindow()) { + found = view; + } } } - return 0; + return found; } QRectF Applet::mapFromView(const QGraphicsView *view, const QRect &rect) const @@ -434,7 +437,7 @@ QRect Applet::mapToView(const QGraphicsView *view, const QRectF &rect) const QPoint Applet::popupPosition(const QSize &s) const { - return ToolTipManager::popupPosition(this,s); + return Plasma::popupPosition(this, s); } void Applet::updateConstraints(Plasma::Constraints constraints) diff --git a/plasma.cpp b/plasma.cpp index 390bd0e23..45eb2e4ba 100644 --- a/plasma.cpp +++ b/plasma.cpp @@ -17,7 +17,14 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#include +#include + +#include +#include +#include + +#include +#include namespace Plasma { @@ -62,4 +69,78 @@ Direction locationToDirection(Location location) return Down; } +QPoint popupPosition(const QGraphicsItem * item, const QSize &s) +{ + QGraphicsView *v = viewFor(item); + + if (!v) { + return QPoint(0,0); + } + + QPoint pos = v->mapFromScene(item->scenePos()); + pos = v->mapToGlobal(pos); + //kDebug() << "==> position is" << scenePos() << v->mapFromScene(scenePos()) << pos; + Plasma::View *pv = dynamic_cast(v); + + Plasma::Location loc = Floating; + if (pv) { + loc = pv->containment()->location(); + } + + switch (loc) { + case BottomEdge: + pos = QPoint(pos.x(), pos.y() - s.height()); + break; + case TopEdge: + pos = QPoint(pos.x(), pos.y() + (int)item->boundingRect().size().height()); + break; + case LeftEdge: + pos = QPoint(pos.x() + (int)item->boundingRect().size().width(), pos.y()); + break; + case RightEdge: + pos = QPoint(pos.x() - s.width(), pos.y()); + break; + default: + if (pos.y() - s.height() > 0) { + pos = QPoint(pos.x(), pos.y() - s.height()); + } else { + pos = QPoint(pos.x(), pos.y() + (int)item->boundingRect().size().height()); + } + } + + //are we out of screen? + QRect screenRect = QApplication::desktop()->screenGeometry(pv ? pv->containment()->screen() : -1); + //kDebug() << "==> rect for" << (pv ? pv->containment()->screen() : -1) << "is" << screenRect; + + if (pos.rx() + s.width() > screenRect.right()) { + pos.rx() -= ((pos.rx() + s.width()) - screenRect.right()); + } + + if (pos.ry() + s.height() > screenRect.bottom()) { + pos.ry() -= ((pos.ry() + s.height()) - screenRect.bottom()); + } + + pos.rx() = qMax(0, pos.rx()); + return pos; +} + +QGraphicsView* viewFor(const QGraphicsItem * item) +{ + if (!item->scene()) { + return 0; + } + + QGraphicsView *found = 0; + foreach (QGraphicsView *view, item->scene()->views()) { + if (view->sceneRect().intersects(item->sceneBoundingRect()) || + view->sceneRect().contains(item->scenePos())) { + if (!found || view->isActiveWindow()) { + found = view; + } + } + } + + return found; +} + } // Plasma namespace diff --git a/plasma.h b/plasma.h index 08f5954bd..8601759d4 100644 --- a/plasma.h +++ b/plasma.h @@ -25,6 +25,8 @@ #include +class QGraphicsView; + /** * Namespace for everything in libplasma */ @@ -193,6 +195,22 @@ PLASMA_EXPORT qreal scalingFactor(ZoomLevel level); **/ PLASMA_EXPORT Direction locationToDirection(Location location); +/** + * Reccomended position for a popup window like a menu or a tooltip + * given its size + * @param s size of the popup + * @returns reccomended position + */ +PLASMA_EXPORT QPoint popupPosition(const QGraphicsItem *item, const QSize &s); + +/** + * Returns the most appropriate QGraphicsView for the item. + * + * @arg item the QGraphicsItem to locate a view for + * @return pointer to a view, or 0 if none was found + */ +PLASMA_EXPORT QGraphicsView *viewFor(const QGraphicsItem *item); + } // Plasma namespace Q_DECLARE_OPERATORS_FOR_FLAGS(Plasma::Constraints) diff --git a/tooltipmanager.cpp b/tooltipmanager.cpp index 6ad4729df..5592e300a 100644 --- a/tooltipmanager.cpp +++ b/tooltipmanager.cpp @@ -288,7 +288,7 @@ void ToolTipManagerPrivate::showToolTip() ToolTip *tooltip = tooltips.value(currentWidget); if (tooltip) { tooltip->prepareShowing(); - tooltip->move(ToolTipManager::popupPosition(currentWidget,tooltip->size())); + tooltip->move(popupPosition(currentWidget, tooltip->size())); isShown = true; //ToolTip is visible } } @@ -299,6 +299,7 @@ bool ToolTipManager::eventFilter(QObject *watched, QEvent *event) if (!widget) { return QObject::eventFilter(watched,event); } + switch (event->type()) { case QEvent::GraphicsSceneHoverMove: // If the tooltip isn't visible, run through showing the tooltip again @@ -317,12 +318,7 @@ bool ToolTipManager::eventFilter(QObject *watched, QEvent *event) // If the mouse is in the widget's area at the time that it is being // created the widget can receive a hover event before it is fully // initialized, in which case view() will return 0. - const Applet * applet = ToolTipManager::getItemItsApplet(widget); - if (!applet) { - break; - } - - QGraphicsView *parentView = applet->view(); + QGraphicsView *parentView = viewFor(widget); if (parentView) { showToolTip(widget); } @@ -345,77 +341,7 @@ bool ToolTipManager::eventFilter(QObject *watched, QEvent *event) return QObject::eventFilter(watched,event); } -const Applet * ToolTipManager::getItemItsApplet(const QGraphicsItem * item) -{ - const Plasma::Applet * applet = dynamic_cast(item); - if (!applet) { - const QGraphicsItem * currentItem = item->parentItem(); - while(currentItem->parentItem() && !dynamic_cast(currentItem)) - { - currentItem=currentItem->parentItem(); - } - applet = dynamic_cast(currentItem); - if (!applet) return 0; - } - return applet; -} - -QPoint ToolTipManager::popupPosition(const QGraphicsItem * item, const QSize &s) -{ - const Applet * applet = ToolTipManager::getItemItsApplet(item); - if (!applet) return QPoint(0,0); - QGraphicsView *v = applet->view(); - Q_ASSERT(v); - - QPoint pos = v->mapFromScene(item->scenePos()); - pos = v->mapToGlobal(pos); - //kDebug() << "==> position is" << scenePos() << v->mapFromScene(scenePos()) << pos; - Plasma::View *pv = dynamic_cast(v); - - Plasma::Location loc = Floating; - if (pv) { - loc = pv->containment()->location(); - } - - switch (loc) { - case BottomEdge: - pos = QPoint(pos.x(), pos.y() - s.height()); - break; - case TopEdge: - pos = QPoint(pos.x(), pos.y() + (int)item->boundingRect().size().height()); - break; - case LeftEdge: - pos = QPoint(pos.x() + (int)item->boundingRect().size().width(), pos.y()); - break; - case RightEdge: - pos = QPoint(pos.x() - s.width(), pos.y()); - break; - default: - if (pos.y() - s.height() > 0) { - pos = QPoint(pos.x(), pos.y() - s.height()); - } else { - pos = QPoint(pos.x(), pos.y() + (int)item->boundingRect().size().height()); - } - } - - //are we out of screen? - - QRect screenRect = QApplication::desktop()->screenGeometry(pv ? pv->containment()->screen() : -1); - //kDebug() << "==> rect for" << (pv ? pv->containment()->screen() : -1) << "is" << screenRect; - - if (pos.rx() + s.width() > screenRect.right()) { - pos.rx() -= ((pos.rx() + s.width()) - screenRect.right()); - } - - if (pos.ry() + s.height() > screenRect.bottom()) { - pos.ry() -= ((pos.ry() + s.height()) - screenRect.bottom()); - } - pos.rx() = qMax(0, pos.rx()); - - return pos; -} - -} +} // Plasma namespace #include "tooltipmanager.moc" diff --git a/tooltipmanager.h b/tooltipmanager.h index 63d9e3baf..bf486b272 100644 --- a/tooltipmanager.h +++ b/tooltipmanager.h @@ -149,25 +149,8 @@ namespace Plasma */ bool widgetHasToolTip(QGraphicsWidget *widget) const; - /** - * Reccomended position for a popup window like a menu or a tooltip - * given its size - * @param s size of the popup - * @returns reccomended position - */ - static QPoint popupPosition(const QGraphicsItem * item, const QSize &s); - - /** - * Take an item and return its owning applet - * @param item the item on which we search an applet return NULL if no parent plasma applet - * found - */ - static const Applet * getItemItsApplet(const QGraphicsItem * item); - - private: friend class ToolTipManagerSingleton; - bool eventFilter(QObject * watched, QEvent * event); ToolTipManagerPrivate* const d;