* get rid of ToolTipManager::getItemItsApplet

* introduce Plasma::viewFor and Plasma::popupPosition

generally nicer code all around.

svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=833534
This commit is contained in:
Aaron J. Seigo 2008-07-16 23:48:41 +00:00
parent c1fb3655cd
commit 19b3401af9
5 changed files with 110 additions and 99 deletions

View File

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

View File

@ -17,7 +17,14 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <plasma.h>
#include <plasma/plasma.h>
#include <QDesktopWidget>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <plasma/containment.h>
#include <plasma/view.h>
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<Plasma::View *>(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

View File

@ -25,6 +25,8 @@
#include <plasma/plasma_export.h>
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)

View File

@ -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<const Applet *>(item);
if (!applet) {
const QGraphicsItem * currentItem = item->parentItem();
while(currentItem->parentItem() && !dynamic_cast<const Applet *>(currentItem))
{
currentItem=currentItem->parentItem();
}
applet = dynamic_cast<const Applet *>(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<Plasma::View *>(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"

View File

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