summaryrefslogtreecommitdiffstats
path: root/src/declarative/graphicsitems/qdeclarativeitem.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/declarative/graphicsitems/qdeclarativeitem.cpp')
-rw-r--r--src/declarative/graphicsitems/qdeclarativeitem.cpp286
1 files changed, 221 insertions, 65 deletions
diff --git a/src/declarative/graphicsitems/qdeclarativeitem.cpp b/src/declarative/graphicsitems/qdeclarativeitem.cpp
index 2e3a5a2..ffef61b 100644
--- a/src/declarative/graphicsitems/qdeclarativeitem.cpp
+++ b/src/declarative/graphicsitems/qdeclarativeitem.cpp
@@ -444,6 +444,11 @@ void QDeclarativeItemKeyFilter::componentComplete()
\c KeyNavigation.BeforeItem allows the event to be used for key navigation
before the item, rather than after.
+ If item to which the focus is switching is not enabled or visible, an attempt will
+ be made to skip this item and focus on the next. This is possible if there are
+ a chain of items with the same KeyNavigation handler. If multiple items in a row are not enabled
+ or visible, they will also be skipped.
+
\sa {Keys}{Keys attached property}
*/
@@ -452,10 +457,12 @@ void QDeclarativeItemKeyFilter::componentComplete()
\qmlproperty Item KeyNavigation::right
\qmlproperty Item KeyNavigation::up
\qmlproperty Item KeyNavigation::down
+ \qmlproperty Item KeyNavigation::tab
+ \qmlproperty Item KeyNavigation::backtab
These properties hold the item to assign focus to
- when the left, right, up or down cursor keys are
- pressed.
+ when the left, right, up or down cursor keys, or the
+ tab key are pressed.
*/
/*!
@@ -611,37 +618,37 @@ void QDeclarativeKeyNavigationAttached::keyPressed(QKeyEvent *event, bool post)
switch(event->key()) {
case Qt::Key_Left:
if (d->left) {
- d->left->setFocus(true);
+ setFocusNavigation(d->left, "left");
event->accept();
}
break;
case Qt::Key_Right:
if (d->right) {
- d->right->setFocus(true);
+ setFocusNavigation(d->right, "right");
event->accept();
}
break;
case Qt::Key_Up:
if (d->up) {
- d->up->setFocus(true);
+ setFocusNavigation(d->up, "up");
event->accept();
}
break;
case Qt::Key_Down:
if (d->down) {
- d->down->setFocus(true);
+ setFocusNavigation(d->down, "down");
event->accept();
}
break;
case Qt::Key_Tab:
if (d->tab) {
- d->tab->setFocus(true);
+ setFocusNavigation(d->tab, "tab");
event->accept();
}
break;
case Qt::Key_Backtab:
if (d->backtab) {
- d->backtab->setFocus(true);
+ setFocusNavigation(d->backtab, "backtab");
event->accept();
}
break;
@@ -700,6 +707,29 @@ void QDeclarativeKeyNavigationAttached::keyReleased(QKeyEvent *event, bool post)
if (!event->isAccepted()) QDeclarativeItemKeyFilter::keyReleased(event, post);
}
+void QDeclarativeKeyNavigationAttached::setFocusNavigation(QDeclarativeItem *currentItem, const char *dir)
+{
+ QDeclarativeItem *initialItem = currentItem;
+ bool isNextItem = false;
+ do {
+ isNextItem = false;
+ if (currentItem->isVisible() && currentItem->isEnabled()) {
+ currentItem->setFocus(true);
+ } else {
+ QObject *attached =
+ qmlAttachedPropertiesObject<QDeclarativeKeyNavigationAttached>(currentItem, false);
+ if (attached) {
+ QDeclarativeItem *tempItem = qvariant_cast<QDeclarativeItem*>(attached->property(dir));
+ if (tempItem) {
+ currentItem = tempItem;
+ isNextItem = true;
+ }
+ }
+ }
+ }
+ while (currentItem != initialItem && isNextItem);
+}
+
/*!
\qmlclass Keys QDeclarativeKeysAttached
\ingroup qml-basic-interaction-elements
@@ -799,10 +829,18 @@ void QDeclarativeKeyNavigationAttached::keyReleased(QKeyEvent *event, bool post)
This example forwards key events to two lists:
\qml
- ListView { id: list1 ... }
- ListView { id: list2 ... }
- Keys.forwardTo: [list1, list2]
- focus: true
+ Item {
+ ListView {
+ id: list1
+ // ...
+ }
+ ListView {
+ id: list2
+ // ...
+ }
+ Keys.forwardTo: [list1, list2]
+ focus: true
+ }
\endqml
*/
@@ -1881,11 +1919,26 @@ void QDeclarativeItem::setClip(bool c)
/*!
\qmlproperty bool Item::visible
- Whether the item is visible. By default this is true.
+ This property holds whether the item is visible. By default this is true.
+
+ Setting this property directly affects the \c visible value of child
+ items. When set to \c false, the \c visible values of all child items also
+ become \c false. When set to \c true, the \c visible values of child items
+ are returned to \c true, unless they have explicitly been set to \c false.
- \note visible is not linked to actual visibility; if an item
- moves off screen, or the opacity changes to 0, this will
- not affect the visible property.
+ (Because of this flow-on behavior, using the \c visible property may not
+ have the intended effect if a property binding should only respond to
+ explicit property changes. In such cases it may be better to use the
+ \l opacity property instead.)
+
+ Setting this property to \c false automatically causes \l focus to be set
+ to \c false, and this item will longer receive mouse and keyboard events.
+ (In contrast, setting the \l opacity to 0 does not affect the \l focus
+ property and the receiving of key events.)
+
+ \note This property's value is only affected by changes to this property or
+ the parent's \c visible property. It does not change, for example, if this
+ item moves off-screen, or if the \l opacity changes to 0.
*/
@@ -2116,13 +2169,18 @@ QDeclarativeAnchorLine QDeclarativeItemPrivate::baseline() const
\o \image declarative-anchors_example.png
\o Text anchored to Image, horizontally centered and vertically below, with a margin.
\qml
- Image { id: pic; ... }
- Text {
- id: label
- anchors.horizontalCenter: pic.horizontalCenter
- anchors.top: pic.bottom
- anchors.topMargin: 5
- ...
+ Item {
+ Image {
+ id: pic
+ // ...
+ }
+ Text {
+ id: label
+ anchors.horizontalCenter: pic.horizontalCenter
+ anchors.top: pic.bottom
+ anchors.topMargin: 5
+ // ...
+ }
}
\endqml
\row
@@ -2132,13 +2190,18 @@ QDeclarativeAnchorLine QDeclarativeItemPrivate::baseline() const
property of both defaults to 0.
\qml
- Image { id: pic; ... }
- Text {
- id: label
- anchors.left: pic.right
- anchors.leftMargin: 5
- ...
- }
+ Item {
+ Image {
+ id: pic
+ // ...
+ }
+ Text {
+ id: label
+ anchors.left: pic.right
+ anchors.leftMargin: 5
+ // ...
+ }
+ }
\endqml
\endtable
@@ -2259,13 +2322,15 @@ void QDeclarativeItem::setBaselineOffset(qreal offset)
/*!
\qmlproperty real Item::opacity
- The opacity of the item. Opacity is specified as a number between 0
- (fully transparent) and 1 (fully opaque). The default is 1.
+ This property holds the opacity of the item. Opacity is specified as a
+ number between 0 (fully transparent) and 1 (fully opaque). The default is 1.
- Opacity is an \e inherited attribute. That is, the opacity is
- also applied individually to child items. In almost all cases this
- is what you want, but in some cases (like the following example)
- it may produce undesired results.
+ When this property is set, the specified opacity is also applied
+ individually to child items. In almost all cases this is what you want,
+ but in some cases it may produce undesired results. For example in the
+ second set of rectangles below, the red rectangle has specified an opacity
+ of 0.5, which affects the opacity of its blue child rectangle even though
+ the child has not specified an opacity.
\table
\row
@@ -2300,6 +2365,12 @@ void QDeclarativeItem::setBaselineOffset(qreal offset)
}
\endqml
\endtable
+
+ If an item's opacity is set to 0, the item will no longer receive mouse
+ events, but will continue to receive key events and will retain the keyboard
+ \l focus if it has been set. (In contrast, setting the \l visible property
+ to \c false stops both mouse and keyboard events, and also removes focus
+ from the item.)
*/
/*!
@@ -2452,11 +2523,15 @@ QDeclarativeListProperty<QObject> QDeclarativeItemPrivate::resources()
\qml
Item {
- states: [
- State { ... },
- State { ... }
- ...
- ]
+ states: [
+ State {
+ // ...
+ },
+ State {
+ // ...
+ }
+ // ...
+ ]
}
\endqml
@@ -2474,11 +2549,15 @@ QDeclarativeListProperty<QDeclarativeState> QDeclarativeItemPrivate::states()
\qml
Item {
- transitions: [
- Transition { ... },
- Transition { ... }
- ...
- ]
+ transitions: [
+ Transition {
+ // ...
+ },
+ Transition {
+ // ...
+ }
+ // ...
+ ]
}
\endqml
@@ -2503,11 +2582,15 @@ QDeclarativeListProperty<QDeclarativeTransition> QDeclarativeItemPrivate::transi
\qml
Item {
- filter: [
- Blur { ... },
- Reflection { ... }
- ...
- ]
+ filter: [
+ Blur {
+ // ...
+ },
+ Reflection {
+ // ...
+ }
+ // ...
+ ]
}
\endqml
*/
@@ -2542,14 +2625,14 @@ QDeclarativeListProperty<QDeclarativeTransition> QDeclarativeItemPrivate::transi
This property is often used in scripts to change between states. For
example:
- \qml
- function toggle() {
- if (button.state == 'On')
- button.state = 'Off';
- else
- button.state = 'On';
- }
- \endqml
+ \js
+ function toggle() {
+ if (button.state == 'On')
+ button.state = 'Off';
+ else
+ button.state = 'On';
+ }
+ \endjs
If the item is in its base state (i.e. no explicit state has been
set), \c state will be a blank string. Likewise, you can return an
@@ -3026,13 +3109,24 @@ void QDeclarativeItemPrivate::resetWidth()
q->setImplicitWidth(q->implicitWidth());
}
+void QDeclarativeItemPrivate::implicitWidthChanged()
+{
+ Q_Q(QDeclarativeItem);
+ emit q->implicitWidthChanged();
+}
+
+qreal QDeclarativeItemPrivate::implicitWidth() const
+{
+ return mImplicitWidth;
+}
+
/*!
Returns the width of the item that is implied by other properties that determine the content.
*/
qreal QDeclarativeItem::implicitWidth() const
{
Q_D(const QDeclarativeItem);
- return d->implicitWidth;
+ return d->implicitWidth();
}
/*!
@@ -3042,9 +3136,13 @@ qreal QDeclarativeItem::implicitWidth() const
void QDeclarativeItem::setImplicitWidth(qreal w)
{
Q_D(QDeclarativeItem);
- d->implicitWidth = w;
- if (d->mWidth == w || widthValid())
+ bool changed = w != d->mImplicitWidth;
+ d->mImplicitWidth = w;
+ if (d->mWidth == w || widthValid()) {
+ if (changed)
+ d->implicitWidthChanged();
return;
+ }
qreal oldWidth = d->mWidth;
@@ -3053,6 +3151,9 @@ void QDeclarativeItem::setImplicitWidth(qreal w)
geometryChanged(QRectF(x(), y(), width(), height()),
QRectF(x(), y(), oldWidth, height()));
+
+ if (changed)
+ d->implicitWidthChanged();
}
/*!
@@ -3134,25 +3235,77 @@ void QDeclarativeItemPrivate::resetHeight()
q->setImplicitHeight(q->implicitHeight());
}
+void QDeclarativeItemPrivate::implicitHeightChanged()
+{
+ Q_Q(QDeclarativeItem);
+ emit q->implicitHeightChanged();
+}
+
+qreal QDeclarativeItemPrivate::implicitHeight() const
+{
+ return mImplicitHeight;
+}
+
/*!
Returns the height of the item that is implied by other properties that determine the content.
*/
qreal QDeclarativeItem::implicitHeight() const
{
Q_D(const QDeclarativeItem);
- return d->implicitHeight;
+ return d->implicitHeight();
}
/*!
+ \qmlproperty real Item::implicitWidth
+ \qmlproperty real Item::implicitHeight
+ \since Quick 1.1
+
+ Defines the natural width or height of the Item if no \l width or \l height is specified.
+
+ The default implicit size for most items is 0x0, however some elements have an inherent
+ implicit size which cannot be overridden, e.g. Image, Text.
+
+ Setting the implicit size is useful for defining components that have a preferred size
+ based on their content, for example:
+
+ \code
+ // Label.qml
+ import QtQuick 1.1
+
+ Item {
+ property alias icon: image.source
+ property alias label: text.text
+ implicitWidth: text.implicitWidth + image.implicitWidth
+ implicitHeight: Math.max(text.implicitHeight, image.implicitHeight)
+ Image { id: image }
+ Text {
+ id: text
+ wrapMode: Text.Wrap
+ anchors.left: image.right; anchors.right: parent.right
+ anchors.verticalCenter: parent.verticalCenter
+ }
+ }
+ \endcode
+
+ \bold Note: using implicitWidth of Text or TextEdit and setting the width explicitly
+ incurs a performance penalty as the text must be laid out twice.
+*/
+
+
+/*!
Sets the implied height of the item to \a h.
This is the height implied by other properties that determine the content.
*/
void QDeclarativeItem::setImplicitHeight(qreal h)
{
Q_D(QDeclarativeItem);
- d->implicitHeight = h;
- if (d->mHeight == h || heightValid())
+ bool changed = h != d->mImplicitHeight;
+ d->mImplicitHeight = h;
+ if (d->mHeight == h || heightValid()) {
+ if (changed)
+ d->implicitHeightChanged();
return;
+ }
qreal oldHeight = d->mHeight;
@@ -3161,6 +3314,9 @@ void QDeclarativeItem::setImplicitHeight(qreal h)
geometryChanged(QRectF(x(), y(), width(), height()),
QRectF(x(), y(), width(), oldHeight));
+
+ if (changed)
+ d->implicitHeightChanged();
}
/*!