diff options
Diffstat (limited to 'src/declarative/graphicsitems/qdeclarativeitem.cpp')
-rw-r--r-- | src/declarative/graphicsitems/qdeclarativeitem.cpp | 370 |
1 files changed, 187 insertions, 183 deletions
diff --git a/src/declarative/graphicsitems/qdeclarativeitem.cpp b/src/declarative/graphicsitems/qdeclarativeitem.cpp index 096e4bf..9433ba0 100644 --- a/src/declarative/graphicsitems/qdeclarativeitem.cpp +++ b/src/declarative/graphicsitems/qdeclarativeitem.cpp @@ -219,46 +219,9 @@ QT_BEGIN_NAMESPACE The angle to rotate, in degrees clockwise. */ - -/*! - \group group_animation - \title Animation -*/ - -/*! - \group group_coreitems - \title Basic Items -*/ - -/*! - \group group_layouts - \title Layouts -*/ - -/*! - \group group_states - \title States and Transitions -*/ - -/*! - \group group_utility - \title Utility -*/ - -/*! - \group group_views - \title Views -*/ - -/*! - \group group_widgets - \title Widgets -*/ - /*! \internal \class QDeclarativeContents - \ingroup group_utility \brief The QDeclarativeContents class gives access to the height and width of an item's contents. */ @@ -267,62 +230,93 @@ QDeclarativeContents::QDeclarativeContents() : m_x(0), m_y(0), m_width(0), m_hei { } +QDeclarativeContents::~QDeclarativeContents() +{ + QList<QGraphicsItem *> children = m_item->childItems(); + for (int i = 0; i < children.count(); ++i) { + QDeclarativeItem *child = qobject_cast<QDeclarativeItem *>(children.at(i)); + if(!child)//### Should this be ignoring non-QDeclarativeItem graphicsobjects? + continue; + QDeclarativeItemPrivate::get(child)->removeItemChangeListener(this, QDeclarativeItemPrivate::Geometry | QDeclarativeItemPrivate::Destroyed); + } +} + QRectF QDeclarativeContents::rectF() const { return QRectF(m_x, m_y, m_width, m_height); } -//TODO: optimization: only check sender(), if there is one -void QDeclarativeContents::calcHeight() +void QDeclarativeContents::calcHeight(QDeclarativeItem *changed) { qreal oldy = m_y; qreal oldheight = m_height; - qreal top = FLT_MAX; - qreal bottom = 0; - - QList<QGraphicsItem *> children = m_item->childItems(); - for (int i = 0; i < children.count(); ++i) { - QDeclarativeItem *child = qobject_cast<QDeclarativeItem *>(children.at(i)); - if(!child)//### Should this be ignoring non-QDeclarativeItem graphicsobjects? - continue; - qreal y = child->y(); - if (y + child->height() > bottom) - bottom = y + child->height(); + if (changed) { + qreal top = oldy; + qreal bottom = oldy + oldheight; + qreal y = changed->y(); + if (y + changed->height() > bottom) + bottom = y + changed->height(); if (y < top) top = y; - } - if (!children.isEmpty()) m_y = top; - m_height = qMax(bottom - top, qreal(0.0)); + m_height = bottom - top; + } else { + qreal top = FLT_MAX; + qreal bottom = 0; + QList<QGraphicsItem *> children = m_item->childItems(); + for (int i = 0; i < children.count(); ++i) { + QDeclarativeItem *child = qobject_cast<QDeclarativeItem *>(children.at(i)); + if(!child)//### Should this be ignoring non-QDeclarativeItem graphicsobjects? + continue; + qreal y = child->y(); + if (y + child->height() > bottom) + bottom = y + child->height(); + if (y < top) + top = y; + } + if (!children.isEmpty()) + m_y = top; + m_height = qMax(bottom - top, qreal(0.0)); + } if (m_height != oldheight || m_y != oldy) emit rectChanged(rectF()); } -//TODO: optimization: only check sender(), if there is one -void QDeclarativeContents::calcWidth() +void QDeclarativeContents::calcWidth(QDeclarativeItem *changed) { qreal oldx = m_x; qreal oldwidth = m_width; - qreal left = FLT_MAX; - qreal right = 0; - - QList<QGraphicsItem *> children = m_item->childItems(); - for (int i = 0; i < children.count(); ++i) { - QDeclarativeItem *child = qobject_cast<QDeclarativeItem *>(children.at(i)); - if(!child)//### Should this be ignoring non-QDeclarativeItem graphicsobjects? - continue; - qreal x = child->x(); - if (x + child->width() > right) - right = x + child->width(); + if (changed) { + qreal left = oldx; + qreal right = oldx + oldwidth; + qreal x = changed->x(); + if (x + changed->width() > right) + right = x + changed->width(); if (x < left) left = x; - } - if (!children.isEmpty()) m_x = left; - m_width = qMax(right - left, qreal(0.0)); + m_width = right - left; + } else { + qreal left = FLT_MAX; + qreal right = 0; + QList<QGraphicsItem *> children = m_item->childItems(); + for (int i = 0; i < children.count(); ++i) { + QDeclarativeItem *child = qobject_cast<QDeclarativeItem *>(children.at(i)); + if(!child)//### Should this be ignoring non-QDeclarativeItem graphicsobjects? + continue; + qreal x = child->x(); + if (x + child->width() > right) + right = x + child->width(); + if (x < left) + left = x; + } + if (!children.isEmpty()) + m_x = left; + m_width = qMax(right - left, qreal(0.0)); + } if (m_width != oldwidth || m_x != oldx) emit rectChanged(rectF()); @@ -331,23 +325,55 @@ void QDeclarativeContents::calcWidth() void QDeclarativeContents::setItem(QDeclarativeItem *item) { m_item = item; + //### optimize + connect(this, SIGNAL(rectChanged(QRectF)), m_item, SIGNAL(childrenRectChanged(QRectF))); QList<QGraphicsItem *> children = m_item->childItems(); for (int i = 0; i < children.count(); ++i) { QDeclarativeItem *child = qobject_cast<QDeclarativeItem *>(children.at(i)); if(!child)//### Should this be ignoring non-QDeclarativeItem graphicsobjects? continue; - connect(child, SIGNAL(heightChanged()), this, SLOT(calcHeight())); - connect(child, SIGNAL(yChanged()), this, SLOT(calcHeight())); - connect(child, SIGNAL(widthChanged()), this, SLOT(calcWidth())); - connect(child, SIGNAL(xChanged()), this, SLOT(calcWidth())); - connect(this, SIGNAL(rectChanged(QRectF)), m_item, SIGNAL(childrenRectChanged(QRectF))); + QDeclarativeItemPrivate::get(child)->addItemChangeListener(this, QDeclarativeItemPrivate::Geometry | QDeclarativeItemPrivate::Destroyed); + //###what about changes to visibility? } + //### defer until componentComplete calcHeight(); calcWidth(); } +void QDeclarativeContents::itemGeometryChanged(QDeclarativeItem *changed, const QRectF &newGeometry, const QRectF &oldGeometry) +{ + if (newGeometry.width() != oldGeometry.width()) + calcWidth(changed); + if (newGeometry.height() != oldGeometry.height()) + calcHeight(changed); +} + +void QDeclarativeContents::itemDestroyed(QDeclarativeItem *item) +{ + if (item) + QDeclarativeItemPrivate::get(item)->removeItemChangeListener(this, QDeclarativeItemPrivate::Geometry | QDeclarativeItemPrivate::Destroyed); + calcWidth(); + calcHeight(); +} + +void QDeclarativeContents::childRemoved(QDeclarativeItem *item) +{ + if (item) + QDeclarativeItemPrivate::get(item)->removeItemChangeListener(this, QDeclarativeItemPrivate::Geometry | QDeclarativeItemPrivate::Destroyed); + calcWidth(); + calcHeight(); +} + +void QDeclarativeContents::childAdded(QDeclarativeItem *item) +{ + if (item) + QDeclarativeItemPrivate::get(item)->addItemChangeListener(this, QDeclarativeItemPrivate::Geometry | QDeclarativeItemPrivate::Destroyed); + calcWidth(item); + calcHeight(item); +} + QDeclarativeItemKeyFilter::QDeclarativeItemKeyFilter(QDeclarativeItem *item) : m_next(0) { @@ -444,7 +470,7 @@ void QDeclarativeItemKeyFilter::componentComplete() If an item has been set for a direction and the KeyNavigation attached property receives the corresponding key press and release events, the events will be accepted by - KeyNaviagtion and will not propagate any further. + KeyNavigation and will not propagate any further. \sa {Keys}{Keys attached property} */ @@ -694,7 +720,7 @@ void QDeclarativeKeyNavigationAttached::keyReleased(QKeyEvent *event) */ /*! - \qmlproperty List<Object> Keys::forwardTo + \qmlproperty list<Object> Keys::forwardTo This property provides a way to forward key presses, key releases, and keyboard input coming from input methods to other items. This can be useful when you want @@ -1258,8 +1284,6 @@ QDeclarativeKeysAttached *QDeclarativeKeysAttached::qmlAttachedProperties(QObjec changes. For many properties in Item or Item derivatives this can be used to add a touch of imperative logic to your application (when absolutely necessary). - - \ingroup group_coreitems */ /*! @@ -1388,6 +1412,7 @@ QDeclarativeItem::~QDeclarativeItem() delete d->_anchorLines; d->_anchorLines = 0; delete d->_anchors; d->_anchors = 0; delete d->_stateGroup; d->_stateGroup = 0; + delete d->_contents; d->_contents = 0; } /*! @@ -1407,7 +1432,7 @@ QDeclarativeItem::~QDeclarativeItem() } \endqml - The default transform origin is \c Center. + The default transform origin is \c Item.Center. */ /*! @@ -1472,11 +1497,6 @@ QDeclarativeItem *QDeclarativeItem::parentItem() const */ /*! - \property QDeclarativeItem::resources - \internal -*/ - -/*! Returns true if construction of the QML component is complete; otherwise returns false. @@ -1491,18 +1511,6 @@ bool QDeclarativeItem::isComponentComplete() const return d->_componentComplete; } -/*! - \property QDeclarativeItem::anchors - \internal -*/ - -/*! \internal */ -QDeclarativeAnchors *QDeclarativeItem::anchors() -{ - Q_D(QDeclarativeItem); - return d->anchors(); -} - void QDeclarativeItemPrivate::data_append(QDeclarativeListProperty<QObject> *prop, QObject *o) { if (!o) @@ -1624,29 +1632,23 @@ void QDeclarativeItemPrivate::parentProperty(QObject *o, void *rv, QDeclarativeN specify it. */ -/*! - \property QDeclarativeItem::data - \internal -*/ - /*! \internal */ -QDeclarativeListProperty<QObject> QDeclarativeItem::data() +QDeclarativeListProperty<QObject> QDeclarativeItemPrivate::data() { - return QDeclarativeListProperty<QObject>(this, 0, QDeclarativeItemPrivate::data_append); + return QDeclarativeListProperty<QObject>(q_func(), 0, QDeclarativeItemPrivate::data_append); } /*! \property QDeclarativeItem::childrenRect \brief The geometry of an item's children. - childrenRect provides an easy way to access the (collective) position and size of the item's children. + This property holds the (collective) position and size of the item's children. */ QRectF QDeclarativeItem::childrenRect() { Q_D(QDeclarativeItem); if (!d->_contents) { d->_contents = new QDeclarativeContents; - QDeclarative_setParent_noEvent(d->_contents, this); d->_contents->setItem(this); } return d->_contents->rectF(); @@ -1860,98 +1862,61 @@ QVariant QDeclarativeItem::inputMethodQuery(Qt::InputMethodQuery query) const /*! \internal */ -QDeclarativeAnchorLine QDeclarativeItem::left() const +QDeclarativeAnchorLine QDeclarativeItemPrivate::left() const { - Q_D(const QDeclarativeItem); - return d->anchorLines()->left; + return anchorLines()->left; } /*! \internal */ -QDeclarativeAnchorLine QDeclarativeItem::right() const +QDeclarativeAnchorLine QDeclarativeItemPrivate::right() const { - Q_D(const QDeclarativeItem); - return d->anchorLines()->right; + return anchorLines()->right; } /*! \internal */ -QDeclarativeAnchorLine QDeclarativeItem::horizontalCenter() const +QDeclarativeAnchorLine QDeclarativeItemPrivate::horizontalCenter() const { - Q_D(const QDeclarativeItem); - return d->anchorLines()->hCenter; + return anchorLines()->hCenter; } /*! \internal */ -QDeclarativeAnchorLine QDeclarativeItem::top() const +QDeclarativeAnchorLine QDeclarativeItemPrivate::top() const { - Q_D(const QDeclarativeItem); - return d->anchorLines()->top; + return anchorLines()->top; } /*! \internal */ -QDeclarativeAnchorLine QDeclarativeItem::bottom() const +QDeclarativeAnchorLine QDeclarativeItemPrivate::bottom() const { - Q_D(const QDeclarativeItem); - return d->anchorLines()->bottom; + return anchorLines()->bottom; } /*! \internal */ -QDeclarativeAnchorLine QDeclarativeItem::verticalCenter() const +QDeclarativeAnchorLine QDeclarativeItemPrivate::verticalCenter() const { - Q_D(const QDeclarativeItem); - return d->anchorLines()->vCenter; + return anchorLines()->vCenter; } /*! \internal */ -QDeclarativeAnchorLine QDeclarativeItem::baseline() const +QDeclarativeAnchorLine QDeclarativeItemPrivate::baseline() const { - Q_D(const QDeclarativeItem); - return d->anchorLines()->baseline; + return anchorLines()->baseline; } /*! - \property QDeclarativeItem::top - \internal -*/ - -/*! - \property QDeclarativeItem::bottom - \internal -*/ - -/*! - \property QDeclarativeItem::left - \internal -*/ - -/*! - \property QDeclarativeItem::right - \internal -*/ - -/*! - \property QDeclarativeItem::horizontalCenter - \internal -*/ - -/*! - \property QDeclarativeItem::verticalCenter - \internal -*/ - -/*! \qmlproperty AnchorLine Item::top \qmlproperty AnchorLine Item::bottom \qmlproperty AnchorLine Item::left @@ -2291,9 +2256,9 @@ void QDeclarativeItemPrivate::focusChanged(bool flag) } /*! \internal */ -QDeclarativeListProperty<QObject> QDeclarativeItem::resources() +QDeclarativeListProperty<QObject> QDeclarativeItemPrivate::resources() { - return QDeclarativeListProperty<QObject>(this, 0, QDeclarativeItemPrivate::resources_append, + return QDeclarativeListProperty<QObject>(q_func(), 0, QDeclarativeItemPrivate::resources_append, QDeclarativeItemPrivate::resources_count, QDeclarativeItemPrivate::resources_at); } @@ -2315,15 +2280,10 @@ QDeclarativeListProperty<QObject> QDeclarativeItem::resources() \sa {qmlstate}{States} */ -/*! - \property QDeclarativeItem::states - \internal -*/ /*! \internal */ -QDeclarativeListProperty<QDeclarativeState> QDeclarativeItem::states() +QDeclarativeListProperty<QDeclarativeState> QDeclarativeItemPrivate::states() { - Q_D(QDeclarativeItem); - return d->states()->statesProperty(); + return _states()->statesProperty(); } /*! @@ -2343,16 +2303,11 @@ QDeclarativeListProperty<QDeclarativeState> QDeclarativeItem::states() \sa {state-transitions}{Transitions} */ -/*! - \property QDeclarativeItem::transitions - \internal -*/ /*! \internal */ -QDeclarativeListProperty<QDeclarativeTransition> QDeclarativeItem::transitions() +QDeclarativeListProperty<QDeclarativeTransition> QDeclarativeItemPrivate::transitions() { - Q_D(QDeclarativeItem); - return d->states()->transitionsProperty(); + return _states()->transitionsProperty(); } /* @@ -2426,20 +2381,18 @@ QDeclarativeListProperty<QDeclarativeTransition> QDeclarativeItem::transitions() */ /*! \internal */ -QString QDeclarativeItem::state() const +QString QDeclarativeItemPrivate::state() const { - Q_D(const QDeclarativeItem); - if (!d->_stateGroup) + if (!_stateGroup) return QString(); else - return d->_stateGroup->state(); + return _stateGroup->state(); } /*! \internal */ -void QDeclarativeItem::setState(const QString &state) +void QDeclarativeItemPrivate::setState(const QString &state) { - Q_D(QDeclarativeItem); - d->states()->setState(state); + _states()->setState(state); } /*! @@ -2502,7 +2455,7 @@ void QDeclarativeItem::componentComplete() d->keyHandler->componentComplete(); } -QDeclarativeStateGroup *QDeclarativeItemPrivate::states() +QDeclarativeStateGroup *QDeclarativeItemPrivate::_states() { Q_Q(QDeclarativeItem); if (!_stateGroup) { @@ -2618,6 +2571,16 @@ QVariant QDeclarativeItem::itemChange(GraphicsItemChange change, } } break; + case ItemChildAddedChange: + if (d->_contents) + d->_contents->childAdded(qobject_cast<QDeclarativeItem*>( + value.value<QGraphicsItem*>())); + break; + case ItemChildRemovedChange: + if (d->_contents) + d->_contents->childRemoved(qobject_cast<QDeclarativeItem*>( + value.value<QGraphicsItem*>())); + break; default: break; } @@ -2723,29 +2686,48 @@ void QDeclarativeItem::setSmooth(bool smooth) update(); } +/*! + \internal + Return the width of the item +*/ qreal QDeclarativeItem::width() const { Q_D(const QDeclarativeItem); return d->width(); } +/*! + \internal + Set the width of the item +*/ void QDeclarativeItem::setWidth(qreal w) { Q_D(QDeclarativeItem); d->setWidth(w); } +/*! + \internal + Reset the width of the item +*/ void QDeclarativeItem::resetWidth() { Q_D(QDeclarativeItem); d->resetWidth(); } +/*! + \internal + Return the width of the item +*/ qreal QDeclarativeItemPrivate::width() const { return mWidth; } +/*! + \internal +*/ void QDeclarativeItemPrivate::setWidth(qreal w) { Q_Q(QDeclarativeItem); @@ -2765,7 +2747,10 @@ void QDeclarativeItemPrivate::setWidth(qreal w) QRectF(q->x(), q->y(), oldWidth, height())); } -void QDeclarativeItemPrivate ::resetWidth() +/*! + \internal +*/ +void QDeclarativeItemPrivate::resetWidth() { Q_Q(QDeclarativeItem); widthValid = false; @@ -2810,29 +2795,47 @@ bool QDeclarativeItem::widthValid() const return d->widthValid; } +/*! + \internal + Return the height of the item +*/ qreal QDeclarativeItem::height() const { Q_D(const QDeclarativeItem); return d->height(); } +/*! + \internal + Set the height of the item +*/ void QDeclarativeItem::setHeight(qreal h) { Q_D(QDeclarativeItem); d->setHeight(h); } +/*! + \internal + Reset the height of the item +*/ void QDeclarativeItem::resetHeight() { Q_D(QDeclarativeItem); d->resetHeight(); } +/*! + \internal +*/ qreal QDeclarativeItemPrivate::height() const { return mHeight; } +/*! + \internal +*/ void QDeclarativeItemPrivate::setHeight(qreal h) { Q_Q(QDeclarativeItem); @@ -2852,6 +2855,9 @@ void QDeclarativeItemPrivate::setHeight(qreal h) QRectF(q->x(), q->y(), width(), oldHeight)); } +/*! + \internal +*/ void QDeclarativeItemPrivate::resetHeight() { Q_Q(QDeclarativeItem); @@ -2956,7 +2962,6 @@ void QDeclarativeItem::setFocus(bool focus) } /*! - \reimp \internal */ void QDeclarativeItem::paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) @@ -2964,7 +2969,6 @@ void QDeclarativeItem::paint(QPainter *, const QStyleOptionGraphicsItem *, QWidg } /*! - \reimp \internal */ bool QDeclarativeItem::event(QEvent *ev) |