diff options
author | Michael Brasser <michael.brasser@nokia.com> | 2009-10-05 03:17:19 (GMT) |
---|---|---|
committer | Michael Brasser <michael.brasser@nokia.com> | 2009-10-05 03:17:19 (GMT) |
commit | d480c23bf54a4dce59b4d29c6276b875307b11ce (patch) | |
tree | 36a3c929f0271205e719a8ca4941809e976aeefb /src/declarative | |
parent | 9d3505cec4749c86ca2782726388c040941ed25c (diff) | |
download | Qt-d480c23bf54a4dce59b4d29c6276b875307b11ce.zip Qt-d480c23bf54a4dce59b4d29c6276b875307b11ce.tar.gz Qt-d480c23bf54a4dce59b4d29c6276b875307b11ce.tar.bz2 |
Experiment with syncing GraphicsObjectContainer and QGraphicsWidget sizes.
Diffstat (limited to 'src/declarative')
-rw-r--r-- | src/declarative/fx/qfxgraphicsobjectcontainer.cpp | 96 | ||||
-rw-r--r-- | src/declarative/fx/qfxgraphicsobjectcontainer.h | 9 |
2 files changed, 103 insertions, 2 deletions
diff --git a/src/declarative/fx/qfxgraphicsobjectcontainer.cpp b/src/declarative/fx/qfxgraphicsobjectcontainer.cpp index 08120fc..c0cac6c 100644 --- a/src/declarative/fx/qfxgraphicsobjectcontainer.cpp +++ b/src/declarative/fx/qfxgraphicsobjectcontainer.cpp @@ -41,6 +41,8 @@ #include "qfxgraphicsobjectcontainer.h" #include <QGraphicsObject> +#include <QGraphicsWidget> +#include <QGraphicsSceneResizeEvent> QT_BEGIN_NAMESPACE @@ -59,7 +61,7 @@ QML_DEFINE_NOCREATE_TYPE(QGraphicsObject) QML_DEFINE_TYPE(Qt,4,6,(QT_VERSION&0x00ff00)>>8,GraphicsObjectContainer,QFxGraphicsObjectContainer) QFxGraphicsObjectContainer::QFxGraphicsObjectContainer(QFxItem *parent) -: QFxItem(parent), _graphicsObject(0) +: QFxItem(parent), _graphicsObject(0), _syncedResize(false) { } @@ -81,9 +83,20 @@ void QFxGraphicsObjectContainer::setGraphicsObject(QGraphicsObject *object) if (object == _graphicsObject) return; + //### what should we do with previously set object? + _graphicsObject = object; - _graphicsObject->setParentItem(this); + if (_graphicsObject) { + _graphicsObject->setParentItem(this); + + if (_syncedResize && _graphicsObject->isWidget()) { + _graphicsObject->installEventFilter(this); + QSizeF newSize = static_cast<QGraphicsWidget*>(_graphicsObject)->size(); //### use sizeHint? + setImplicitWidth(newSize.width()); + setImplicitHeight(newSize.height()); + } + } } QVariant QFxGraphicsObjectContainer::itemChange(GraphicsItemChange change, const QVariant &value) @@ -96,4 +109,83 @@ QVariant QFxGraphicsObjectContainer::itemChange(GraphicsItemChange change, const return QFxItem::itemChange(change, value); } +bool QFxGraphicsObjectContainer::eventFilter(QObject *watched, QEvent *e) +{ + if (watched == _graphicsObject && e->type() == QEvent::GraphicsSceneResize) { + if (_graphicsObject && _graphicsObject->isWidget() && _syncedResize) { + QSizeF newSize = static_cast<QGraphicsWidget*>(_graphicsObject)->size(); + setImplicitWidth(newSize.width()); + setImplicitHeight(newSize.height()); + } + } + return QFxItem::eventFilter(watched, e); +} + +/*! + \qmlproperty bool GraphicsObjectContainer::synchronizedResizing + + This property determines whether or not the container and graphics object will synchronize their + sizes. + + \note This property only applies when wrapping a QGraphicsWidget. + + If synchronizedResizing is enabled, the container and widget will + synchronize their sizes as follows. + \list + \o If a size has been set on the container, the widget will be resized to the container. + Any changes in the container's size will be reflected in the widget. + + \o \e Otherwise, the container will initially be sized to the preferred size of the widget. + Any changes to the container's size will be reflected in the widget, and any changes to the + widget's size will be reflected in the container. + \endlist +*/ +bool QFxGraphicsObjectContainer::synchronizedResizing() const +{ + return _syncedResize; +} + +void QFxGraphicsObjectContainer::setSynchronizedResizing(bool on) +{ + if (on == _syncedResize) + return; + + if (_graphicsObject && _graphicsObject->isWidget()) { + if (!on) { + _graphicsObject->removeEventFilter(this); + disconnect(this, SIGNAL(widthChanged()), this, SLOT(_q_updateSize())); + disconnect(this, SIGNAL(heightChanged()), this, SLOT(_q_updateSize())); + } + } + + _syncedResize = on; + + if (_graphicsObject && _graphicsObject->isWidget()) { + if (on) { + _graphicsObject->installEventFilter(this); + connect(this, SIGNAL(widthChanged()), this, SLOT(_q_updateSize())); + connect(this, SIGNAL(heightChanged()), this, SLOT(_q_updateSize())); + } + } +} + +void QFxGraphicsObjectContainer::_q_updateSize() +{ + if (!_graphicsObject || !_graphicsObject->isWidget() || !_syncedResize) + return; + + QGraphicsWidget *gw = static_cast<QGraphicsWidget*>(_graphicsObject); + const QSizeF newSize(width(), height()); + gw->resize(newSize); + + //### will respecting the widgets min/max ever get us in trouble? (all other items always + // size to exactly what you tell them) + /*QSizeF constrainedSize = newSize.expandedTo(gw->minimumSize()).boundedTo(gw->maximumSize()); + gw->resize(constrainedSize); + if (constrainedSize != newSize) { + setImplicitWidth(constrainedSize.width()); + setImplicitHeight(constrainedSize.height()); + }*/ +} + QT_END_NAMESPACE diff --git a/src/declarative/fx/qfxgraphicsobjectcontainer.h b/src/declarative/fx/qfxgraphicsobjectcontainer.h index 165bc48..a8b7c8c 100644 --- a/src/declarative/fx/qfxgraphicsobjectcontainer.h +++ b/src/declarative/fx/qfxgraphicsobjectcontainer.h @@ -58,6 +58,7 @@ class Q_DECLARATIVE_EXPORT QFxGraphicsObjectContainer : public QFxItem Q_CLASSINFO("DefaultProperty", "graphicsObject") Q_PROPERTY(QGraphicsObject *graphicsObject READ graphicsObject WRITE setGraphicsObject) + Q_PROPERTY(bool synchronizedResizing READ synchronizedResizing WRITE setSynchronizedResizing) public: QFxGraphicsObjectContainer(QFxItem *parent = 0); @@ -66,11 +67,19 @@ public: QGraphicsObject *graphicsObject() const; void setGraphicsObject(QGraphicsObject *); + bool synchronizedResizing() const; + void setSynchronizedResizing(bool on); + protected: QVariant itemChange(GraphicsItemChange change, const QVariant &value); + bool eventFilter(QObject *watched, QEvent *e); + +private Q_SLOTS: + void _q_updateSize(); private: QGraphicsObject *_graphicsObject; + bool _syncedResize; }; QT_END_NAMESPACE |