1 /****************************************************************************
3 ** Copyright (C) 2007-2007 Trolltech ASA. All rights reserved.
5 ** This file is part of the Graphics Widget Items project on Trolltech Labs.
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
);
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
;
56 embeddedWidget
->installEventFilter(this);
61 QRectF
QGraphicsWidgetItem::boundingRect() const
63 return QRectF(0, 0, size
.width(), size
.height());
66 void QGraphicsWidgetItem::paint(QPainter
*painter
, const QStyleOptionGraphicsItem
*, QWidget
*)
71 int QGraphicsWidgetItem::type() const
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()) {
82 case QEvent::ParentChange
:
90 // Adjust the item when the mouse enters or leaves a scene.
91 if (watched
== scene()) {
92 switch (event
->type()) {
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
)
123 // Update the graphics item's geometry if the widget's geometry has
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
132 QList
<QGraphicsView
*> views
= scene
->views();
133 QGraphicsView
*activeView
= 0;
134 if (views
.size() == 1) {
135 activeView
= views
.first();
137 foreach (QGraphicsView
*view
, scene
->views()) {
138 if (view
->viewport()->underMouse()) {
145 activeView
= lastActiveView
;
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
)
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();