create PresenceManager that manage global presence state and message
[makneto-zunavac1.git] / src / ui-kde / qgraphicswidgetitem.cpp
blob0aa4bed79d230fe38074611fb7bb9d658b5276d1
1 /****************************************************************************
2 **
3 ** Copyright (C) 2007-2007 Trolltech ASA. All rights reserved.
4 **
5 ** This file is part of the Graphics Widget Items project on Trolltech Labs.
6 **
7 ** This file may be used under the terms of the GNU General Public
8 ** License version 2.0 as published by the Free Software Foundation
9 ** and appearing in the file LICENSE.GPL included in the packaging of
10 ** this file. Please review the following information to ensure GNU
11 ** General Public Licensing requirements will be met:
12 ** http://www.trolltech.com/products/qt/opensource.html
14 ** If you are unsure which license is appropriate for your use, please
15 ** review the following information:
16 ** http://www.trolltech.com/products/qt/licensing.html or contact the
17 ** sales department at sales@trolltech.com.
19 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
20 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 ****************************************************************************/
24 #include "qgraphicswidgetitem.h"
26 #include <QtGui/qevent.h>
27 #include <QtGui/QGraphicsView>
29 QGraphicsWidgetItem::QGraphicsWidgetItem(QGraphicsWidgetItem *parent)
30 : QGraphicsItem(parent, 0), embeddedWidget(0), lastActiveView(0)
32 setFlag(ItemIgnoresTransformations);
35 QGraphicsWidgetItem::QGraphicsWidgetItem(QWidget *widget, QGraphicsWidgetItem *parent)
36 : QGraphicsItem(parent, 0), embeddedWidget(0), lastActiveView(0)
38 setFlag(ItemIgnoresTransformations);
39 setWidget(widget);
42 QWidget *QGraphicsWidgetItem::widget() const
44 return embeddedWidget;
47 void QGraphicsWidgetItem::setWidget(QWidget *widget)
49 if (embeddedWidget && embeddedWidget->parent() == (QObject *)this) {
50 embeddedWidget->removeEventFilter(this);
51 embeddedWidget->hide();
52 embeddedWidget->setParent(0);
54 embeddedWidget = widget;
55 if (embeddedWidget) {
56 embeddedWidget->installEventFilter(this);
57 adjust();
61 QRectF QGraphicsWidgetItem::boundingRect() const
63 return QRectF(0, 0, size.width(), size.height());
66 void QGraphicsWidgetItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
68 adjust();
71 int QGraphicsWidgetItem::type() const
73 return Type;
76 bool QGraphicsWidgetItem::eventFilter(QObject *watched, QEvent *event)
78 // Adjust the item when it's reparented or resized.
79 if (watched == embeddedWidget) {
80 switch (event->type()) {
81 case QEvent::Resize:
82 case QEvent::ParentChange:
83 adjust();
84 break;
85 default:
86 break;
90 // Adjust the item when the mouse enters or leaves a scene.
91 if (watched == scene()) {
92 switch (event->type()) {
93 case QEvent::Enter:
94 case QEvent::Leave:
95 adjust();
96 break;
97 default:
98 break;
102 return QObject::eventFilter(watched, event);
105 QVariant QGraphicsWidgetItem::itemChange(GraphicsItemChange change, const QVariant &value)
107 if (change == ItemSceneChange) {
108 // Update the scene event filter as the item moves to a new scene.
109 if (QGraphicsScene *oldScene = scene())
110 oldScene->removeEventFilter(this);
111 if (QGraphicsScene *newScene = qVariantValue<QGraphicsScene *>(value))
112 newScene->installEventFilter(this);
114 return QGraphicsItem::itemChange(change, value);
117 void QGraphicsWidgetItem::adjust()
119 QGraphicsScene *scene = this->scene();
120 if (!scene || !embeddedWidget)
121 return;
123 // Update the graphics item's geometry if the widget's geometry has
124 // changed.
125 if (embeddedWidget->size() != size) {
126 prepareGeometryChange();
127 size = embeddedWidget->size();
130 // Find the active view if there's no active view, use the last known
131 // active view.
132 QList<QGraphicsView *> views = scene->views();
133 QGraphicsView *activeView = 0;
134 if (views.size() == 1) {
135 activeView = views.first();
136 } else {
137 foreach (QGraphicsView *view, scene->views()) {
138 if (view->viewport()->underMouse()) {
139 activeView = view;
140 break;
144 if (!activeView)
145 activeView = lastActiveView;
146 if (!activeView)
147 return;
148 lastActiveView = activeView;
150 // Check if the item is visible in the active view.
151 QTransform itemTransform = deviceTransform(activeView->viewportTransform());
152 bool visibleInActiveView = (itemTransform.mapRect(QRect(0, 0, size.width(), size.height())).intersects(activeView->viewport()->rect()));
153 if (!visibleInActiveView)
154 return;
156 // Reparent the widget if necessary.
157 if (embeddedWidget->parentWidget() != activeView->viewport())
158 embeddedWidget->setParent(activeView->viewport());
160 // Move the widget to its new viewport position.
161 QPoint viewportPos = itemTransform.map(QPointF(0, 0)).toPoint();
162 embeddedWidget->move(viewportPos);
163 embeddedWidget->show();