summaryrefslogtreecommitdiffstats
path: root/src/declarative
diff options
context:
space:
mode:
Diffstat (limited to 'src/declarative')
-rw-r--r--src/declarative/debugger/qdeclarativedebugservice.cpp41
-rw-r--r--src/declarative/graphicsitems/qdeclarativeflickable.cpp11
-rw-r--r--src/declarative/graphicsitems/qdeclarativeflickable_p_p.h3
-rw-r--r--src/declarative/graphicsitems/qdeclarativegridview.cpp42
-rw-r--r--src/declarative/graphicsitems/qdeclarativeitem.cpp69
-rw-r--r--src/declarative/graphicsitems/qdeclarativeitem_p.h4
-rw-r--r--src/declarative/graphicsitems/qdeclarativelistview.cpp15
-rw-r--r--src/declarative/graphicsitems/qdeclarativeloader.cpp8
-rw-r--r--src/declarative/graphicsitems/qdeclarativemousearea.cpp100
-rw-r--r--src/declarative/graphicsitems/qdeclarativemousearea_p_p.h6
-rw-r--r--src/declarative/graphicsitems/qdeclarativepathview.cpp195
-rw-r--r--src/declarative/graphicsitems/qdeclarativepathview_p_p.h4
-rw-r--r--src/declarative/graphicsitems/qdeclarativerectangle.cpp179
-rw-r--r--src/declarative/graphicsitems/qdeclarativetext.cpp10
-rw-r--r--src/declarative/graphicsitems/qdeclarativevisualitemmodel.cpp40
-rw-r--r--src/declarative/qml/qdeclarative.h4
-rw-r--r--src/declarative/qml/qdeclarativebinding.cpp13
-rw-r--r--src/declarative/qml/qdeclarativebinding_p.h7
-rw-r--r--src/declarative/qml/qdeclarativecomponent_p.h2
-rw-r--r--src/declarative/qml/qdeclarativecustomparser_p.h2
-rw-r--r--src/declarative/qml/qdeclarativeengine.cpp2
-rw-r--r--src/declarative/qml/qdeclarativeobjectscriptclass.cpp2
-rw-r--r--src/declarative/qml/qdeclarativescriptparser.cpp2
-rw-r--r--src/declarative/qml/qdeclarativetypeloader.cpp8
-rw-r--r--src/declarative/qml/qdeclarativevaluetype.cpp2
-rw-r--r--src/declarative/qml/qdeclarativexmlhttprequest.cpp4
-rw-r--r--src/declarative/util/qdeclarativeanimation.cpp5
-rw-r--r--src/declarative/util/qdeclarativefontloader.cpp197
-rw-r--r--src/declarative/util/qdeclarativefontloader_p.h2
-rw-r--r--src/declarative/util/qdeclarativelistmodel.cpp536
-rw-r--r--src/declarative/util/qdeclarativelistmodel_p.h12
-rw-r--r--src/declarative/util/qdeclarativelistmodel_p_p.h134
-rw-r--r--src/declarative/util/qdeclarativelistmodelworkeragent.cpp33
-rw-r--r--src/declarative/util/qdeclarativelistmodelworkeragent_p.h4
-rw-r--r--src/declarative/util/qdeclarativepixmapcache_p.h2
-rw-r--r--src/declarative/util/qdeclarativepropertychanges.cpp275
-rw-r--r--src/declarative/util/qdeclarativepropertychanges_p.h16
-rw-r--r--src/declarative/util/qdeclarativestate.cpp238
-rw-r--r--src/declarative/util/qdeclarativestate_p.h21
-rw-r--r--src/declarative/util/qdeclarativestate_p_p.h135
-rw-r--r--src/declarative/util/qdeclarativestateoperations.cpp66
-rw-r--r--src/declarative/util/qdeclarativetransitionmanager.cpp4
-rw-r--r--src/declarative/util/qdeclarativexmllistmodel.cpp8
43 files changed, 1946 insertions, 517 deletions
diff --git a/src/declarative/debugger/qdeclarativedebugservice.cpp b/src/declarative/debugger/qdeclarativedebugservice.cpp
index 1bbfcf4a25..1f2bf4fa3b 100644
--- a/src/declarative/debugger/qdeclarativedebugservice.cpp
+++ b/src/declarative/debugger/qdeclarativedebugservice.cpp
@@ -49,6 +49,8 @@
#include <QtCore/qstringlist.h>
#include <private/qobject_p.h>
+#include <private/qapplication_p.h>
+#include <QtGui/qapplication.h>
QT_BEGIN_NAMESPACE
@@ -147,24 +149,41 @@ bool QDeclarativeDebugServer::hasDebuggingClient() const
QDeclarativeDebugServer *QDeclarativeDebugServer::instance()
{
- static bool envTested = false;
+ static bool commandLineTested = false;
static QDeclarativeDebugServer *server = 0;
- if (!envTested) {
- envTested = true;
- QByteArray env = qgetenv("QML_DEBUG_SERVER_PORT");
- QByteArray block = qgetenv("QML_DEBUG_SERVER_BLOCK");
+ if (!commandLineTested) {
+ commandLineTested = true;
+#ifndef QDECLARATIVE_NO_DEBUG_PROTOCOL
+ QApplicationPrivate *appD = static_cast<QApplicationPrivate*>(QObjectPrivate::get(qApp));
+ // ### remove port definition when protocol is changed
+ int port = 0;
+ bool block = false;
bool ok = false;
- int port = env.toInt(&ok);
- if (ok && port > 1024) {
- server = new QDeclarativeDebugServer(port);
- server->listen();
- if (!block.isEmpty()) {
- server->waitForConnection();
+ // format: qmljsdebugger=port:3768[,block]
+ if (!appD->qmljsDebugArguments.isEmpty()) {
+
+ if (appD->qmljsDebugArguments.indexOf(QLatin1String("port:")) == 0) {
+ int separatorIndex = appD->qmljsDebugArguments.indexOf(QLatin1Char(','));
+ port = appD->qmljsDebugArguments.mid(5, separatorIndex - 5).toInt(&ok);
+ }
+ block = appD->qmljsDebugArguments.contains(QLatin1String("block"));
+
+ if (ok) {
+ server = new QDeclarativeDebugServer(port);
+ server->listen();
+ if (block) {
+ server->waitForConnection();
+ }
+ } else {
+ qWarning(QString("QDeclarativeDebugServer: Ignoring \"-qmljsdebugger=%1\". "
+ "Format is -qmljsdebugger=port:<port>[,block]").arg(
+ appD->qmljsDebugArguments).toAscii().constData());
}
}
+#endif
}
return server;
diff --git a/src/declarative/graphicsitems/qdeclarativeflickable.cpp b/src/declarative/graphicsitems/qdeclarativeflickable.cpp
index d4c65aef08..c5077954b0 100644
--- a/src/declarative/graphicsitems/qdeclarativeflickable.cpp
+++ b/src/declarative/graphicsitems/qdeclarativeflickable.cpp
@@ -128,8 +128,8 @@ QDeclarativeFlickablePrivate::QDeclarativeFlickablePrivate()
, flickingHorizontally(false), flickingVertically(false)
, hMoved(false), vMoved(false)
, movingHorizontally(false), movingVertically(false)
- , stealMouse(false), pressed(false)
- , interactive(true), deceleration(500), maxVelocity(2000), reportedVelocitySmoothing(100)
+ , stealMouse(false), pressed(false), interactive(true), calcVelocity(false)
+ , deceleration(500), maxVelocity(2000), reportedVelocitySmoothing(100)
, delayedPressEvent(0), delayedPressTarget(0), pressDelay(0), fixupDuration(600)
, vTime(0), visibleArea(0)
, flickableDirection(QDeclarativeFlickable::AutoFlickDirection)
@@ -372,11 +372,12 @@ void QDeclarativeFlickablePrivate::updateBeginningEnd()
\inlineimage flickable.gif
\endfloat
- The following example shows a large
+ The following example shows a small view onto a large image in which the
+ user can drag or flick the image in order to view different parts of it.
- \clearfloat
\snippet doc/src/snippets/declarative/flickable.qml document
+ \clearfloat
\section1 Limitations
\note Due to an implementation detail, items placed inside a Flickable cannot anchor to it by
@@ -981,7 +982,7 @@ void QDeclarativeFlickable::viewportMoved()
qreal prevY = d->lastFlickablePosition.x();
qreal prevX = d->lastFlickablePosition.y();
d->velocityTimeline.clear();
- if (d->pressed) {
+ if (d->pressed || d->calcVelocity) {
int elapsed = QDeclarativeItemPrivate::restart(d->velocityTime);
if (elapsed > 0) {
qreal horizontalVelocity = (prevX - d->hData.move.value()) * 1000 / elapsed;
diff --git a/src/declarative/graphicsitems/qdeclarativeflickable_p_p.h b/src/declarative/graphicsitems/qdeclarativeflickable_p_p.h
index 261b271ef5..beee741a29 100644
--- a/src/declarative/graphicsitems/qdeclarativeflickable_p_p.h
+++ b/src/declarative/graphicsitems/qdeclarativeflickable_p_p.h
@@ -141,6 +141,7 @@ public:
bool stealMouse : 1;
bool pressed : 1;
bool interactive : 1;
+ bool calcVelocity : 1;
QElapsedTimer lastPosTime;
QPointF lastPos;
QPointF pressPos;
@@ -173,7 +174,7 @@ public:
// flickableData property
static void data_append(QDeclarativeListProperty<QObject> *, QObject *);
static int data_count(QDeclarativeListProperty<QObject> *);
- static QObject * data_at(QDeclarativeListProperty<QObject> *, int);
+ static QObject *data_at(QDeclarativeListProperty<QObject> *, int);
static void data_clear(QDeclarativeListProperty<QObject> *);
};
diff --git a/src/declarative/graphicsitems/qdeclarativegridview.cpp b/src/declarative/graphicsitems/qdeclarativegridview.cpp
index a0faf14a6c..8d08c998d4 100644
--- a/src/declarative/graphicsitems/qdeclarativegridview.cpp
+++ b/src/declarative/graphicsitems/qdeclarativegridview.cpp
@@ -1070,27 +1070,41 @@ void QDeclarativeGridViewPrivate::flick(AxisData &data, qreal minExtent, qreal m
GridView are laid out horizontally or vertically. Grid views are inherently flickable
as GridView inherits from \l Flickable.
- For example, if there is a simple list model defined in a file \c ContactModel.qml like this:
+ \section1 Example Usage
+
+ The following example shows the definition of a simple list model defined
+ in a file called \c ContactModel.qml:
\snippet doc/src/snippets/declarative/gridview/ContactModel.qml 0
- Another component can display this model data in a GridView, like this:
+ \beginfloatright
+ \inlineimage gridview-simple.png
+ \endfloat
+
+ This model can be referenced as \c ContactModel in other QML files. See \l{QML Modules}
+ for more information about creating reusable components like this.
+
+ Another component can display this model data in a GridView, as in the following
+ example, which creates a \c ContactModel component for its model, and a \l Column element
+ (containing \l Image and \l Text elements) for its delegate.
+ \clearfloat
\snippet doc/src/snippets/declarative/gridview/gridview.qml import
\codeline
\snippet doc/src/snippets/declarative/gridview/gridview.qml classdocs simple
- \image gridview-simple.png
- Here, the GridView creates a \c ContactModel component for its model, and a \l Column element
- (containing \l Image and \ Text elements) for its delegate. The view will create a new delegate
- for each item in the model. Notice the delegate is able to access the model's \c name and
- \c portrait data directly.
+ \beginfloatright
+ \inlineimage gridview-highlight.png
+ \endfloat
+
+ The view will create a new delegate for each item in the model. Note that the delegate
+ is able to access the model's \c name and \c portrait data directly.
An improved grid view is shown below. The delegate is visually improved and is moved
into a separate \c contactDelegate component.
-
+
+ \clearfloat
\snippet doc/src/snippets/declarative/gridview/gridview.qml classdocs advanced
- \image gridview-highlight.png
The currently selected item is highlighted with a blue \l Rectangle using the \l highlight property,
and \c focus is set to \c true to enable keyboard navigation for the grid view.
@@ -1099,10 +1113,10 @@ void QDeclarativeGridViewPrivate::flick(AxisData &data, qreal minExtent, qreal m
Delegates are instantiated as needed and may be destroyed at any time.
State should \e never be stored in a delegate.
- \note Views do not enable \e clip automatically. If the view
- is not clipped by another item or the screen, it will be necessary
- to set \e {clip: true} in order to have the out of view items clipped
- nicely.
+ \note Views do not set the \l{Item::}{clip} property automatically.
+ If the view is not clipped by another item or the screen, it will be necessary
+ to set this property to true in order to clip the items that are partially or
+ fully outside the view.
\sa {declarative/modelviews/gridview}{GridView example}
*/
@@ -2241,7 +2255,9 @@ void QDeclarativeGridView::trackedPositionChanged()
}
if (viewPos != pos) {
cancelFlick();
+ d->calcVelocity = true;
d->setPosition(pos);
+ d->calcVelocity = false;
}
}
}
diff --git a/src/declarative/graphicsitems/qdeclarativeitem.cpp b/src/declarative/graphicsitems/qdeclarativeitem.cpp
index 11f9179270..e9da4f774b 100644
--- a/src/declarative/graphicsitems/qdeclarativeitem.cpp
+++ b/src/declarative/graphicsitems/qdeclarativeitem.cpp
@@ -44,6 +44,7 @@
#include "private/qdeclarativeevents_p_p.h"
#include <private/qdeclarativeengine_p.h>
+#include <private/qgraphicsitem_p.h>
#include <qdeclarativeengine.h>
#include <qdeclarativeopenmetaobject_p.h>
@@ -1619,6 +1620,51 @@ void QDeclarativeItemPrivate::data_append(QDeclarativeListProperty<QObject> *pro
}
}
+static inline int children_count_helper(QDeclarativeListProperty<QObject> *prop)
+{
+ QGraphicsItemPrivate *d = QGraphicsItemPrivate::get(static_cast<QGraphicsObject *>(prop->object));
+ return d->children.count();
+}
+
+static inline QObject *children_at_helper(QDeclarativeListProperty<QObject> *prop, int index)
+{
+ QGraphicsItemPrivate *d = QGraphicsItemPrivate::get(static_cast<QGraphicsObject *>(prop->object));
+ if (index >= 0 && index < d->children.count())
+ return d->children.at(index)->toGraphicsObject();
+ else
+ return 0;
+}
+
+static inline void children_clear_helper(QDeclarativeListProperty<QObject> *prop)
+{
+ QGraphicsItemPrivate *d = QGraphicsItemPrivate::get(static_cast<QGraphicsObject *>(prop->object));
+ int childCount = d->children.count();
+ for (int index = 0 ;index < childCount; index++)
+ QGraphicsItemPrivate::get(d->children.at(0))->setParentItemHelper(0, /*newParentVariant=*/0, /*thisPointerVariant=*/0);
+}
+
+int QDeclarativeItemPrivate::data_count(QDeclarativeListProperty<QObject> *prop)
+{
+ return resources_count(prop) + children_count_helper(prop);
+}
+
+QObject *QDeclarativeItemPrivate::data_at(QDeclarativeListProperty<QObject> *prop, int i)
+{
+ int resourcesCount = resources_count(prop);
+ if (i < resourcesCount)
+ return resources_at(prop, i);
+ const int j = i - resourcesCount;
+ if (j < children_count_helper(prop))
+ return children_at_helper(prop, j);
+ return 0;
+}
+
+void QDeclarativeItemPrivate::data_clear(QDeclarativeListProperty<QObject> *prop)
+{
+ resources_clear(prop);
+ children_clear_helper(prop);
+}
+
QObject *QDeclarativeItemPrivate::resources_at(QDeclarativeListProperty<QObject> *prop, int index)
{
const QObjectList children = prop->object->children();
@@ -1638,6 +1684,13 @@ int QDeclarativeItemPrivate::resources_count(QDeclarativeListProperty<QObject> *
return prop->object->children().count();
}
+void QDeclarativeItemPrivate::resources_clear(QDeclarativeListProperty<QObject> *prop)
+{
+ const QObjectList children = prop->object->children();
+ for (int index = 0; index < children.count(); index++)
+ children.at(index)->setParent(0);
+}
+
int QDeclarativeItemPrivate::transform_count(QDeclarativeListProperty<QGraphicsTransform> *list)
{
QGraphicsObject *object = qobject_cast<QGraphicsObject *>(list->object);
@@ -1724,7 +1777,11 @@ void QDeclarativeItemPrivate::parentProperty(QObject *o, void *rv, QDeclarativeN
QDeclarativeListProperty<QObject> QDeclarativeItemPrivate::data()
{
- return QDeclarativeListProperty<QObject>(q_func(), 0, QDeclarativeItemPrivate::data_append);
+ return QDeclarativeListProperty<QObject>(q_func(), 0, QDeclarativeItemPrivate::data_append,
+ QDeclarativeItemPrivate::data_count,
+ QDeclarativeItemPrivate::data_at,
+ QDeclarativeItemPrivate::data_clear
+ );
}
/*!
@@ -2413,7 +2470,9 @@ QDeclarativeListProperty<QObject> QDeclarativeItemPrivate::resources()
{
return QDeclarativeListProperty<QObject>(q_func(), 0, QDeclarativeItemPrivate::resources_append,
QDeclarativeItemPrivate::resources_count,
- QDeclarativeItemPrivate::resources_at);
+ QDeclarativeItemPrivate::resources_at,
+ QDeclarativeItemPrivate::resources_clear
+ );
}
/*!
@@ -2484,7 +2543,7 @@ QDeclarativeListProperty<QDeclarativeTransition> QDeclarativeItemPrivate::transi
/*!
\qmlproperty bool Item::clip
- This property holds whether clipping is enabled.
+ This property holds whether clipping is enabled. The default clip value is \c false.
If clipping is enabled, an item will clip its own painting, as well
as the painting of its children, to its bounding rectangle.
@@ -2494,9 +2553,9 @@ QDeclarativeListProperty<QDeclarativeTransition> QDeclarativeItemPrivate::transi
/*!
\property QDeclarativeItem::clip
- This property holds whether clipping is enabled.
+ This property holds whether clipping is enabled. The default clip value is \c false.
- if clipping is enabled, an item will clip its own painting, as well
+ If clipping is enabled, an item will clip its own painting, as well
as the painting of its children, to its bounding rectangle. If you set
clipping during an item's paint operation, remember to re-set it to
prevent clipping the rest of your scene.
diff --git a/src/declarative/graphicsitems/qdeclarativeitem_p.h b/src/declarative/graphicsitems/qdeclarativeitem_p.h
index fffb4f7dce..f85fa27874 100644
--- a/src/declarative/graphicsitems/qdeclarativeitem_p.h
+++ b/src/declarative/graphicsitems/qdeclarativeitem_p.h
@@ -176,11 +176,15 @@ public:
// data property
static void data_append(QDeclarativeListProperty<QObject> *, QObject *);
+ static int data_count(QDeclarativeListProperty<QObject> *);
+ static QObject *data_at(QDeclarativeListProperty<QObject> *, int);
+ static void data_clear(QDeclarativeListProperty<QObject> *);
// resources property
static QObject *resources_at(QDeclarativeListProperty<QObject> *, int);
static void resources_append(QDeclarativeListProperty<QObject> *, QObject *);
static int resources_count(QDeclarativeListProperty<QObject> *);
+ static void resources_clear(QDeclarativeListProperty<QObject> *);
// transform property
static int transform_count(QDeclarativeListProperty<QGraphicsTransform> *list);
diff --git a/src/declarative/graphicsitems/qdeclarativelistview.cpp b/src/declarative/graphicsitems/qdeclarativelistview.cpp
index 177c5b362f..c1e6aaade5 100644
--- a/src/declarative/graphicsitems/qdeclarativelistview.cpp
+++ b/src/declarative/graphicsitems/qdeclarativelistview.cpp
@@ -1394,11 +1394,14 @@ void QDeclarativeListViewPrivate::flick(AxisData &data, qreal minExtent, qreal m
QAbstractListModel.
A ListView has a \l model, which defines the data to be displayed, and
- a \l delegate, which defines how the data should be displayed. Items in a
- ListView are laid out horizontally or vertically. List views are inherently flickable
- as ListView inherits from \l Flickable.
+ a \l delegate, which defines how the data should be displayed. Items in a
+ ListView are laid out horizontally or vertically. List views are inherently
+ flickable because ListView inherits from \l Flickable.
- For example, if there is a simple list model defined in a file \c ContactModel.qml like this:
+ \section1 Example Usage
+
+ The following example shows the definition of a simple list model defined
+ in a file called \c ContactModel.qml:
\snippet doc/src/snippets/declarative/listview/ContactModel.qml 0
@@ -1416,7 +1419,7 @@ void QDeclarativeListViewPrivate::flick(AxisData &data, qreal minExtent, qreal m
An improved list view is shown below. The delegate is visually improved and is moved
into a separate \c contactDelegate component.
-
+
\snippet doc/src/snippets/declarative/listview/listview.qml classdocs advanced
\image listview-highlight.png
@@ -2732,7 +2735,9 @@ void QDeclarativeListView::trackedPositionChanged()
}
if (viewPos != pos) {
cancelFlick();
+ d->calcVelocity = true;
d->setPosition(pos);
+ d->calcVelocity = false;
}
}
}
diff --git a/src/declarative/graphicsitems/qdeclarativeloader.cpp b/src/declarative/graphicsitems/qdeclarativeloader.cpp
index 5d71625b1c..1066c2bdc1 100644
--- a/src/declarative/graphicsitems/qdeclarativeloader.cpp
+++ b/src/declarative/graphicsitems/qdeclarativeloader.cpp
@@ -212,8 +212,8 @@ QDeclarativeLoader::~QDeclarativeLoader()
\qmlproperty url Loader::source
This property holds the URL of the QML component to instantiate.
- Note the QML component must be an \l Item-based component. Loader cannot
- load non-visual components.
+ Note the QML component must be an \l{Item}-based component. The loader
+ cannot load non-visual components.
To unload the currently loaded item, set this property to an empty string,
or set \l sourceComponent to \c undefined.
@@ -275,8 +275,8 @@ void QDeclarativeLoader::setSource(const QUrl &url)
}
\endqml
- To unload the currently loaded item, set this property to an empty string,
- or set \l sourceComponent to \c undefined.
+ To unload the currently loaded item, set this property to an empty string
+ or \c undefined.
\sa source, progress
*/
diff --git a/src/declarative/graphicsitems/qdeclarativemousearea.cpp b/src/declarative/graphicsitems/qdeclarativemousearea.cpp
index 5516611b0d..a0208efeb3 100644
--- a/src/declarative/graphicsitems/qdeclarativemousearea.cpp
+++ b/src/declarative/graphicsitems/qdeclarativemousearea.cpp
@@ -185,27 +185,57 @@ QDeclarativeMouseAreaPrivate::~QDeclarativeMouseAreaPrivate()
\brief The MouseArea item enables simple mouse handling.
\inherits Item
- A MouseArea is typically used in conjunction with a visible item,
- where the MouseArea effectively 'proxies' mouse handling for that
- item. For example, we can put a MouseArea in a \l Rectangle that changes
+ A MouseArea is an invisible item that is typically used in conjunction with
+ a visible item in order to provide mouse handling for that item.
+ By effectively acting as a proxy, the logic for mouse handling can be
+ contained within a MouseArea item.
+
+ For basic key handling, see the \l{Keys}{Keys attached property}.
+
+ The \l enabled property is used to enable and disable mouse handling for
+ the proxied item. When disabled, the mouse area becomes transparent to
+ mouse events.
+
+ The \l pressed read-only property indicates whether or not the user is
+ holding down a mouse button over the mouse area. This property is often
+ used in bindings between properties in a user interface. The containsMouse
+ read-only property indicates the presence of the mouse cursor over the
+ mouse area but, by default, only when a mouse button is held down; see below
+ for further details.
+
+ Information about the mouse position and button clicks are provided via
+ signals for which event handler properties are defined. The most commonly
+ used involved handling mouse presses and clicks: onClicked, onDoubleClicked,
+ onPressed, onReleased and onPressAndHold.
+
+ By default, MouseArea items only report mouse clicks and not changes to the
+ position of the mouse cursor. Setting the hoverEnabled property ensures that
+ handlers defined for onPositionChanged, onEntered and onExited are used and
+ that the containsMouse property is updated even when no mouse buttons are
+ pressed.
+
+ \section1 Example Usage
+
+ \beginfloatright
+ \inlineimage qml-mousearea-snippet.png
+ \endfloat
+
+ The following example uses a MouseArea in a \l Rectangle that changes
the \l Rectangle color to red when clicked:
- \snippet doc/src/snippets/declarative/mousearea.qml import
+ \snippet doc/src/snippets/declarative/mousearea/mousearea.qml import
\codeline
- \snippet doc/src/snippets/declarative/mousearea.qml intro
+ \snippet doc/src/snippets/declarative/mousearea/mousearea.qml intro
- Many MouseArea signals pass a \l {MouseEvent}{mouse} parameter that contains
+ \clearfloat
+ Many MouseArea signals pass a \l{MouseEvent}{mouse} parameter that contains
additional information about the mouse event, such as the position, button,
and any key modifiers.
Here is an extension of the previous example that produces a different
color when the area is right clicked:
- \snippet doc/src/snippets/declarative/mousearea.qml intro-extended
-
- For basic key handling, see the \l {Keys}{Keys attached property}.
-
- MouseArea is an invisible item: it is never painted.
+ \snippet doc/src/snippets/declarative/mousearea/mousearea.qml intro-extended
\sa MouseEvent, {declarative/touchinteraction/mousearea}{MouseArea example}
*/
@@ -303,18 +333,23 @@ QDeclarativeMouseAreaPrivate::~QDeclarativeMouseAreaPrivate()
The \l {MouseEvent}{mouse} parameter provides information about the click, including the x and y
position of the release of the click, and whether the click was held.
- The \e accepted property of the MouseEvent parameter is ignored in this handler.
+ If the \e accepted property of the \l {MouseEvent}{mouse} parameter is set to false
+ in the handler, the onPressed/onReleased/onClicked handlers will be called for the second
+ click; otherwise they are supressed. The accepted property defaults to true.
*/
/*!
\qmlsignal MouseArea::onCanceled()
This handler is called when mouse events have been canceled, either because an event was not accepted, or
- because another element stole the mouse event handling. This signal is for advanced use: it is useful when
- there is more than one MouseArea that is handling input, or when there is a MouseArea inside a \l Flickable. In the latter
- case, if you execute some logic on the pressed signal and then start dragging, the \l Flickable will steal the mouse handling
- from the MouseArea. In these cases, to reset the logic when the MouseArea has lost the mouse handling to the
- \l Flickable, \c onCanceled should be used in addition to onReleased.
+ because another element stole the mouse event handling.
+
+ This signal is for advanced use: it is useful when there is more than one MouseArea
+ that is handling input, or when there is a MouseArea inside a \l Flickable. In the latter
+ case, if you execute some logic on the pressed signal and then start dragging, the
+ \l Flickable will steal the mouse handling from the MouseArea. In these cases, to reset
+ the logic when the MouseArea has lost the mouse handling to the \l Flickable,
+ \c onCanceled should be used in addition to onReleased.
*/
QDeclarativeMouseArea::QDeclarativeMouseArea(QDeclarativeItem *parent)
@@ -331,11 +366,13 @@ QDeclarativeMouseArea::~QDeclarativeMouseArea()
/*!
\qmlproperty real MouseArea::mouseX
\qmlproperty real MouseArea::mouseY
- These properties hold the coordinates of the mouse.
+ These properties hold the coordinates of the mouse cursor.
If the hoverEnabled property is false then these properties will only be valid
while a button is pressed, and will remain valid as long as the button is held
- even if the mouse is moved outside the area.
+ down even if the mouse is moved outside the area.
+
+ By default, this property is false.
If hoverEnabled is true then these properties will be valid when:
\list
@@ -360,6 +397,8 @@ qreal QDeclarativeMouseArea::mouseY() const
/*!
\qmlproperty bool MouseArea::enabled
This property holds whether the item accepts mouse events.
+
+ By default, this property is true.
*/
bool QDeclarativeMouseArea::isEnabled() const
{
@@ -387,7 +426,8 @@ void QDeclarativeMouseArea::setEnabled(bool a)
\endlist
The code below displays "right" when the right mouse buttons is pressed:
- \snippet doc/src/snippets/declarative/mousearea.qml mousebuttons
+
+ \snippet doc/src/snippets/declarative/mousearea/mousearea.qml mousebuttons
\sa acceptedButtons
*/
@@ -525,12 +565,13 @@ void QDeclarativeMouseArea::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *even
if (!d->absorb) {
QDeclarativeItem::mouseDoubleClickEvent(event);
} else {
- QDeclarativeItem::mouseDoubleClickEvent(event);
- if (event->isAccepted()) {
- // Only deliver the event if we have accepted the press.
- d->saveEvent(event);
- QDeclarativeMouseEvent me(d->lastPos.x(), d->lastPos.y(), d->lastButton, d->lastButtons, d->lastModifiers, true, false);
- emit this->doubleClicked(&me);
+ d->saveEvent(event);
+ QDeclarativeMouseEvent me(d->lastPos.x(), d->lastPos.y(), d->lastButton, d->lastButtons, d->lastModifiers, true, false);
+ me.setAccepted(d->isDoubleClickConnected());
+ emit this->doubleClicked(&me);
+ if (!me.isAccepted()) {
+ // Only deliver the press event if we haven't accepted the double click.
+ QDeclarativeItem::mouseDoubleClickEvent(event);
}
}
}
@@ -704,7 +745,8 @@ QVariant QDeclarativeMouseArea::itemChange(GraphicsItemChange change,
pressed. Hover enables handling of all mouse events even when no mouse button is
pressed.
- This property affects the containsMouse property and the onEntered, onExited and onPositionChanged signals.
+ This property affects the containsMouse property and the onEntered, onExited and
+ onPositionChanged signals.
*/
bool QDeclarativeMouseArea::hoverEnabled() const
{
@@ -847,7 +889,7 @@ QDeclarativeDrag *QDeclarativeMouseArea::drag()
The following example displays a \l Rectangle that can be dragged along the X-axis. The opacity
of the rectangle is reduced when it is dragged to the right.
- \snippet doc/src/snippets/declarative/mousearea.qml drag
+ \snippet doc/src/snippets/declarative/mousearea/mousearea.qml drag
\note Items cannot be dragged if they are anchored for the requested
\c drag.axis. For example, if \c anchors.left or \c anchors.right was set
@@ -858,7 +900,7 @@ QDeclarativeDrag *QDeclarativeMouseArea::drag()
If \c drag.filterChildren is set to true, a drag can override descendant MouseAreas. This
enables a parent MouseArea to handle drags, for example, while descendants handle clicks:
- \snippet doc/src/snippets/declarative/mouseareadragfilter.qml dragfilter
+ \snippet doc/src/snippets/declarative/mousearea/mouseareadragfilter.qml dragfilter
*/
diff --git a/src/declarative/graphicsitems/qdeclarativemousearea_p_p.h b/src/declarative/graphicsitems/qdeclarativemousearea_p_p.h
index cf9dc183fa..48a56d95b3 100644
--- a/src/declarative/graphicsitems/qdeclarativemousearea_p_p.h
+++ b/src/declarative/graphicsitems/qdeclarativemousearea_p_p.h
@@ -95,6 +95,12 @@ public:
return QObjectPrivate::get(q)->isSignalConnected(idx);
}
+ bool isDoubleClickConnected() {
+ Q_Q(QDeclarativeMouseArea);
+ static int idx = QObjectPrivate::get(q)->signalIndex("doubleClicked(QDeclarativeMouseEvent*)");
+ return QObjectPrivate::get(q)->isSignalConnected(idx);
+ }
+
bool absorb : 1;
bool hovered : 1;
bool pressed : 1;
diff --git a/src/declarative/graphicsitems/qdeclarativepathview.cpp b/src/declarative/graphicsitems/qdeclarativepathview.cpp
index de3f9faffd..d13492999c 100644
--- a/src/declarative/graphicsitems/qdeclarativepathview.cpp
+++ b/src/declarative/graphicsitems/qdeclarativepathview.cpp
@@ -141,11 +141,13 @@ void QDeclarativePathViewPrivate::releaseItem(QDeclarativeItem *item)
{
if (!item || !model)
return;
- if (QDeclarativePathViewAttached *att = attached(item))
- att->setOnPath(false);
QDeclarativeItemPrivate *itemPrivate = static_cast<QDeclarativeItemPrivate*>(QGraphicsItemPrivate::get(item));
itemPrivate->removeItemChangeListener(this, QDeclarativeItemPrivate::Geometry);
- model->release(item);
+ if (model->release(item) == 0) {
+ // item was not destroyed, and we no longer reference it.
+ if (QDeclarativePathViewAttached *att = attached(item))
+ att->setOnPath(false);
+ }
}
QDeclarativePathViewAttached *QDeclarativePathViewPrivate::attached(QDeclarativeItem *item)
@@ -404,14 +406,14 @@ QDeclarativePathView::~QDeclarativePathView()
be instantiated, but not considered to be currently on the path.
Usually, these items would be set invisible, for example:
- \code
+ \qml
Component {
Rectangle {
visible: PathView.onPath
...
}
}
- \endcode
+ \endqml
It is attached to each instance of the delegate.
*/
@@ -1033,103 +1035,138 @@ QPointF QDeclarativePathViewPrivate::pointNear(const QPointF &point, qreal *near
return nearPoint;
}
-
void QDeclarativePathView::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
Q_D(QDeclarativePathView);
- if (!d->interactive || !d->items.count())
+ if (d->interactive) {
+ d->handleMousePressEvent(event);
+ event->accept();
+ } else {
+ QDeclarativeItem::mousePressEvent(event);
+ }
+}
+
+void QDeclarativePathViewPrivate::handleMousePressEvent(QGraphicsSceneMouseEvent *event)
+{
+ Q_Q(QDeclarativePathView);
+ if (!interactive || !items.count())
return;
- QPointF scenePoint = mapToScene(event->pos());
+ QPointF scenePoint = q->mapToScene(event->pos());
int idx = 0;
- for (; idx < d->items.count(); ++idx) {
- QRectF rect = d->items.at(idx)->boundingRect();
- rect = d->items.at(idx)->mapToScene(rect).boundingRect();
+ for (; idx < items.count(); ++idx) {
+ QRectF rect = items.at(idx)->boundingRect();
+ rect = items.at(idx)->mapToScene(rect).boundingRect();
if (rect.contains(scenePoint))
break;
}
- if (idx == d->items.count() && d->dragMargin == 0.) // didn't click on an item
+ if (idx == items.count() && dragMargin == 0.) // didn't click on an item
return;
- d->startPoint = d->pointNear(event->pos(), &d->startPc);
- if (idx == d->items.count()) {
- qreal distance = qAbs(event->pos().x() - d->startPoint.x()) + qAbs(event->pos().y() - d->startPoint.y());
- if (distance > d->dragMargin)
+ startPoint = pointNear(event->pos(), &startPc);
+ if (idx == items.count()) {
+ qreal distance = qAbs(event->pos().x() - startPoint.x()) + qAbs(event->pos().y() - startPoint.y());
+ if (distance > dragMargin)
return;
}
- if (d->tl.isActive() && d->flicking)
- d->stealMouse = true; // If we've been flicked then steal the click.
+ if (tl.isActive() && flicking)
+ stealMouse = true; // If we've been flicked then steal the click.
else
- d->stealMouse = false;
+ stealMouse = false;
- d->lastElapsed = 0;
- d->lastDist = 0;
- QDeclarativeItemPrivate::start(d->lastPosTime);
- d->tl.clear();
+ lastElapsed = 0;
+ lastDist = 0;
+ QDeclarativeItemPrivate::start(lastPosTime);
+ tl.clear();
}
void QDeclarativePathView::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
Q_D(QDeclarativePathView);
- if (!d->interactive || !d->lastPosTime.isValid())
+ if (d->interactive) {
+ d->handleMouseMoveEvent(event);
+ if (d->stealMouse)
+ setKeepMouseGrab(true);
+ event->accept();
+ } else {
+ QDeclarativeItem::mouseMoveEvent(event);
+ }
+}
+
+void QDeclarativePathViewPrivate::handleMouseMoveEvent(QGraphicsSceneMouseEvent *event)
+{
+ Q_Q(QDeclarativePathView);
+ if (!interactive || !lastPosTime.isValid())
return;
- if (!d->stealMouse) {
- QPointF delta = event->pos() - d->startPoint;
+ if (!stealMouse) {
+ QPointF delta = event->pos() - startPoint;
if (qAbs(delta.x()) > QApplication::startDragDistance() || qAbs(delta.y()) > QApplication::startDragDistance())
- d->stealMouse = true;
+ stealMouse = true;
}
- if (d->stealMouse) {
- d->moveReason = QDeclarativePathViewPrivate::Mouse;
+ if (stealMouse) {
+ moveReason = QDeclarativePathViewPrivate::Mouse;
qreal newPc;
- d->pointNear(event->pos(), &newPc);
- qreal diff = (newPc - d->startPc)*d->modelCount*d->mappedRange;
+ pointNear(event->pos(), &newPc);
+ qreal diff = (newPc - startPc)*modelCount*mappedRange;
if (diff) {
- setOffset(d->offset + diff);
+ setOffset(offset + diff);
- if (diff > d->modelCount/2)
- diff -= d->modelCount;
- else if (diff < -d->modelCount/2)
- diff += d->modelCount;
+ if (diff > modelCount/2)
+ diff -= modelCount;
+ else if (diff < -modelCount/2)
+ diff += modelCount;
- d->lastElapsed = QDeclarativeItemPrivate::restart(d->lastPosTime);
- d->lastDist = diff;
- d->startPc = newPc;
+ lastElapsed = QDeclarativeItemPrivate::restart(lastPosTime);
+ lastDist = diff;
+ startPc = newPc;
}
- if (!d->moving) {
- d->moving = true;
- emit movingChanged();
- emit movementStarted();
+ if (!moving) {
+ moving = true;
+ emit q->movingChanged();
+ emit q->movementStarted();
}
}
}
-void QDeclarativePathView::mouseReleaseEvent(QGraphicsSceneMouseEvent *)
+void QDeclarativePathView::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
Q_D(QDeclarativePathView);
- d->stealMouse = false;
- setKeepMouseGrab(false);
- if (!d->interactive || !d->lastPosTime.isValid())
+ if (d->interactive) {
+ d->handleMouseReleaseEvent(event);
+ event->accept();
+ ungrabMouse();
+ } else {
+ QDeclarativeItem::mouseReleaseEvent(event);
+ }
+}
+
+void QDeclarativePathViewPrivate::handleMouseReleaseEvent(QGraphicsSceneMouseEvent *)
+{
+ Q_Q(QDeclarativePathView);
+ stealMouse = false;
+ q->setKeepMouseGrab(false);
+ if (!interactive || !lastPosTime.isValid())
return;
- qreal elapsed = qreal(d->lastElapsed + QDeclarativeItemPrivate::elapsed(d->lastPosTime)) / 1000.;
- qreal velocity = elapsed > 0. ? d->lastDist / elapsed : 0;
- if (d->model && d->modelCount && qAbs(velocity) > 1.) {
- qreal count = d->pathItems == -1 ? d->modelCount : d->pathItems;
+ qreal elapsed = qreal(lastElapsed + QDeclarativeItemPrivate::elapsed(lastPosTime)) / 1000.;
+ qreal velocity = elapsed > 0. ? lastDist / elapsed : 0;
+ if (model && modelCount && qAbs(velocity) > 1.) {
+ qreal count = pathItems == -1 ? modelCount : pathItems;
if (qAbs(velocity) > count * 2) // limit velocity
velocity = (velocity > 0 ? count : -count) * 2;
// Calculate the distance to be travelled
qreal v2 = velocity*velocity;
- qreal accel = d->deceleration/10;
+ qreal accel = deceleration/10;
// + 0.25 to encourage moving at least one item in the flick direction
- qreal dist = qMin(qreal(d->modelCount-1), qreal(v2 / (accel * 2.0) + 0.25));
- if (d->haveHighlightRange && d->highlightRangeMode == QDeclarativePathView::StrictlyEnforceRange) {
+ qreal dist = qMin(qreal(modelCount-1), qreal(v2 / (accel * 2.0) + 0.25));
+ if (haveHighlightRange && highlightRangeMode == QDeclarativePathView::StrictlyEnforceRange) {
// round to nearest item.
if (velocity > 0.)
- dist = qRound(dist + d->offset) - d->offset;
+ dist = qRound(dist + offset) - offset;
else
- dist = qRound(dist - d->offset) + d->offset;
+ dist = qRound(dist - offset) + offset;
// Calculate accel required to stop on item boundary
if (dist <= 0.) {
dist = 0.;
@@ -1138,23 +1175,22 @@ void QDeclarativePathView::mouseReleaseEvent(QGraphicsSceneMouseEvent *)
accel = v2 / (2.0f * qAbs(dist));
}
}
- d->offsetAdj = 0.0;
- d->moveOffset.setValue(d->offset);
- d->tl.accel(d->moveOffset, velocity, accel, dist);
- d->tl.callback(QDeclarativeTimeLineCallback(&d->moveOffset, d->fixOffsetCallback, d));
- if (!d->flicking) {
- d->flicking = true;
- emit flickingChanged();
- emit flickStarted();
+ offsetAdj = 0.0;
+ moveOffset.setValue(offset);
+ tl.accel(moveOffset, velocity, accel, dist);
+ tl.callback(QDeclarativeTimeLineCallback(&moveOffset, fixOffsetCallback, this));
+ if (!flicking) {
+ flicking = true;
+ emit q->flickingChanged();
+ emit q->flickStarted();
}
} else {
- d->fixOffset();
+ fixOffset();
}
- d->lastPosTime.invalidate();
- ungrabMouse();
- if (!d->tl.isActive())
- movementEnding();
+ lastPosTime.invalidate();
+ if (!tl.isActive())
+ q->movementEnding();
}
bool QDeclarativePathView::sendMouseEvent(QGraphicsSceneMouseEvent *event)
@@ -1164,7 +1200,8 @@ bool QDeclarativePathView::sendMouseEvent(QGraphicsSceneMouseEvent *event)
QRectF myRect = mapToScene(QRectF(0, 0, width(), height())).boundingRect();
QGraphicsScene *s = scene();
QDeclarativeItem *grabber = s ? qobject_cast<QDeclarativeItem*>(s->mouseGrabberItem()) : 0;
- if ((d->stealMouse || myRect.contains(event->scenePos().toPoint())) && (!grabber || !grabber->keepMouseGrab())) {
+ bool stealThisEvent = d->stealMouse;
+ if ((stealThisEvent || myRect.contains(event->scenePos().toPoint())) && (!grabber || !grabber->keepMouseGrab())) {
mouseEvent.setAccepted(false);
for (int i = 0x1; i <= 0x10; i <<= 1) {
if (event->buttons() & i) {
@@ -1179,25 +1216,28 @@ bool QDeclarativePathView::sendMouseEvent(QGraphicsSceneMouseEvent *event)
switch(mouseEvent.type()) {
case QEvent::GraphicsSceneMouseMove:
- mouseMoveEvent(&mouseEvent);
+ d->handleMouseMoveEvent(&mouseEvent);
break;
case QEvent::GraphicsSceneMousePress:
- mousePressEvent(&mouseEvent);
+ d->handleMousePressEvent(&mouseEvent);
+ stealThisEvent = d->stealMouse; // Update stealThisEvent in case changed by function call above
break;
case QEvent::GraphicsSceneMouseRelease:
- mouseReleaseEvent(&mouseEvent);
+ d->handleMouseReleaseEvent(&mouseEvent);
break;
default:
break;
}
grabber = qobject_cast<QDeclarativeItem*>(s->mouseGrabberItem());
- if (grabber && d->stealMouse && !grabber->keepMouseGrab() && grabber != this)
+ if (grabber && stealThisEvent && !grabber->keepMouseGrab() && grabber != this)
grabMouse();
return d->stealMouse;
} else if (d->lastPosTime.isValid()) {
d->lastPosTime.invalidate();
}
+ if (mouseEvent.type() == QEvent::GraphicsSceneMouseRelease)
+ d->stealMouse = false;
return false;
}
@@ -1211,12 +1251,7 @@ bool QDeclarativePathView::sceneEventFilter(QGraphicsItem *i, QEvent *e)
case QEvent::GraphicsSceneMousePress:
case QEvent::GraphicsSceneMouseMove:
case QEvent::GraphicsSceneMouseRelease:
- {
- bool ret = sendMouseEvent(static_cast<QGraphicsSceneMouseEvent *>(e));
- if (e->type() == QEvent::GraphicsSceneMouseRelease)
- return ret;
- break;
- }
+ return sendMouseEvent(static_cast<QGraphicsSceneMouseEvent *>(e));
default:
break;
}
diff --git a/src/declarative/graphicsitems/qdeclarativepathview_p_p.h b/src/declarative/graphicsitems/qdeclarativepathview_p_p.h
index dfebe359e5..b21721635b 100644
--- a/src/declarative/graphicsitems/qdeclarativepathview_p_p.h
+++ b/src/declarative/graphicsitems/qdeclarativepathview_p_p.h
@@ -123,6 +123,10 @@ public:
return model && model->count() > 0 && model->isValid() && path;
}
+ void handleMousePressEvent(QGraphicsSceneMouseEvent *event);
+ void handleMouseMoveEvent(QGraphicsSceneMouseEvent *event);
+ void handleMouseReleaseEvent(QGraphicsSceneMouseEvent *);
+
int calcCurrentIndex();
void updateCurrent();
static void fixOffsetCallback(void*);
diff --git a/src/declarative/graphicsitems/qdeclarativerectangle.cpp b/src/declarative/graphicsitems/qdeclarativerectangle.cpp
index d027924d07..9831d5fdd2 100644
--- a/src/declarative/graphicsitems/qdeclarativerectangle.cpp
+++ b/src/declarative/graphicsitems/qdeclarativerectangle.cpp
@@ -85,8 +85,8 @@ void QDeclarativePen::setWidth(int w)
/*!
\qmlclass GradientStop QDeclarativeGradientStop
\ingroup qml-basic-visual-elements
- \since 4.7
- \brief The GradientStop item defines the color at a position in a Gradient
+ \since 4.7
+ \brief The GradientStop item defines the color at a position in a Gradient.
\sa Gradient
*/
@@ -95,7 +95,12 @@ void QDeclarativePen::setWidth(int w)
\qmlproperty real GradientStop::position
\qmlproperty color GradientStop::color
- Sets a \e color at a \e position in a gradient.
+ The position and color properties describe the color used at a given
+ position in a gradient, as represented by a gradient stop.
+
+ The default position is 0.0; the default color is black.
+
+ \sa Gradient
*/
void QDeclarativeGradientStop::updateGradient()
@@ -107,20 +112,50 @@ void QDeclarativeGradientStop::updateGradient()
/*!
\qmlclass Gradient QDeclarativeGradient
\ingroup qml-basic-visual-elements
- \since 4.7
+ \since 4.7
\brief The Gradient item defines a gradient fill.
- A gradient is defined by two or more colors, which will be blended seemlessly. The
- colors are specified at their position in the range 0.0 - 1.0 via
- the GradientStop item. For example, the following code paints a
- rectangle with a gradient starting with red, blending to yellow at 1/3 of the
- size of the rectangle, and ending with Green:
+ A gradient is defined by two or more colors, which will be blended seamlessly.
- \snippet doc/src/snippets/declarative/gradient.qml code
+ The colors are specified as a set of GradientStop child items, each of
+ which defines a position on the gradient from 0.0 to 1.0 and a color.
+ The position of each GradientStop is defined by setting its
+ \l{GradientStop::}{position} property; its color is defined using its
+ \l{GradientStop::}{color} property.
+
+ A gradient without any gradient stops is rendered as a solid white fill.
Note that this item is not a visual representation of a gradient. To display a
- gradient use a visual item (like rectangle) which supports having a gradient set
- on it for display.
+ gradient, use a visual element (like \l Rectangle) which supports the use
+ of gradients.
+
+ \section1 Example Usage
+
+ \beginfloatright
+ \inlineimage qml-gradient.png
+ \endfloat
+
+ The following example declares a \l Rectangle item with a gradient starting
+ with red, blending to yellow at one third of the height of the rectangle,
+ and ending with green:
+
+ \snippet doc/src/snippets/declarative/gradient.qml code
+
+ \clearfloat
+ \section1 Performance and Limitations
+
+ Calculating gradients can be computationally expensive compared to the use
+ of solid color fills or images. Consider using gradients for static items
+ in a user interface.
+
+ In Qt 4.7, only vertical, linear gradients can be applied to items. If you
+ need to apply different orientations of gradients, a combination of rotation
+ and clipping will need to be applied to the relevant items. This can
+ introduce additional performance requirements for your application.
+
+ The use of animations involving gradient stops may not give the desired
+ result. An alternative way to animate gradients is to use pre-generated
+ images or SVG drawings containing gradients.
\sa GradientStop
*/
@@ -128,6 +163,10 @@ void QDeclarativeGradientStop::updateGradient()
/*!
\qmlproperty list<GradientStop> Gradient::stops
This property holds the gradient stops describing the gradient.
+
+ By default, this property contains an empty list.
+
+ To set the gradient stops, define them as children of the Gradient element.
*/
const QGradient *QDeclarativeGradient::gradient() const
@@ -155,27 +194,46 @@ void QDeclarativeGradient::doUpdate()
/*!
\qmlclass Rectangle QDeclarativeRectangle
\ingroup qml-basic-visual-elements
- \since 4.7
- \brief The Rectangle item allows you to add rectangles to a scene.
+ \since 4.7
+ \brief The Rectangle item provides a filled rectangle with an optional border.
\inherits Item
- A Rectangle is painted using a solid fill (color) and an optional border.
- You can also create rounded rectangles using the \l radius property.
+ Rectangle items are used to fill areas with solid color or gradients, and are
+ often used to hold other items.
- \qml
- import Qt 4.7
-
- Rectangle {
- width: 100
- height: 100
- color: "red"
- border.color: "black"
- border.width: 5
- radius: 10
- }
- \endqml
+ \section1 Appearance
+
+ Each Rectangle item is painted using either a solid fill color, specified using
+ the \l color property, or a gradient, defined using a Gradient element and set
+ using the \l gradient property. If both a color and a gradient are specified,
+ the gradient is used.
+
+ You can add an optional border to a rectangle with its own color and thickness
+ by settting the \l border.color and \l border.width properties.
+
+ You can also create rounded rectangles using the \l radius property. Since this
+ introduces curved edges to the corners of a rectangle, it may be appropriate to
+ set the \l smooth property to improve its appearance.
- \image declarative-rect.png
+ \section1 Example Usage
+
+ \beginfloatright
+ \inlineimage declarative-rect.png
+ \endfloat
+
+ The following example shows the effects of some of the common properties on a
+ Rectangle item, which in this case is used to create a square:
+
+ \snippet doc/src/snippets/declarative/rectangle/rectangle.qml document
+
+ \clearfloat
+ \section1 Performance
+
+ Using the \l smooth property improves the appearance of a rounded rectangle at
+ the cost of rendering performance. You should consider unsetting this property
+ for rectangles in motion, and only set it when they are stationary.
+
+ \sa Image
*/
int QDeclarativeRectanglePrivate::doUpdateSlotIdx = -1;
@@ -207,13 +265,14 @@ void QDeclarativeRectangle::doUpdate()
rectangle's boundaries, and the spare pixel is rendered to the right and below the
rectangle (as documented for QRect rendering). This can cause unintended effects if
\c border.width is 1 and the rectangle is \l{Item::clip}{clipped} by a parent item:
-
- \table
- \row
- \o \snippet doc/src/snippets/declarative/rect-border-width.qml 0
- \o \image rect-border-width.png
- \endtable
+ \beginfloatright
+ \inlineimage rect-border-width.png
+ \endfloat
+
+ \snippet doc/src/snippets/declarative/rectangle/rect-border-width.qml 0
+
+ \clearfloat
Here, the innermost rectangle's border is clipped on the bottom and right edges by its
parent. To avoid this, the border width can be set to two instead of one.
*/
@@ -231,34 +290,12 @@ QDeclarativePen *QDeclarativeRectangle::border()
This property allows for the construction of simple vertical gradients.
Other gradients may by formed by adding rotation to the rectangle.
- \table
- \row
- \o \image declarative-rect_gradient.png
- \o
- \qml
- Rectangle {
- y: 0; width: 80; height: 80
- color: "lightsteelblue"
- }
-
- Rectangle {
- y: 100; width: 80; height: 80
- gradient: Gradient {
- GradientStop { position: 0.0; color: "lightsteelblue" }
- GradientStop { position: 1.0; color: "blue" }
- }
- }
+ \beginfloatleft
+ \inlineimage declarative-rect_gradient.png
+ \endfloat
- Rectangle {
- y: 200; width: 80; height: 80
- rotation: 90
- gradient: Gradient {
- GradientStop { position: 0.0; color: "lightsteelblue" }
- GradientStop { position: 1.0; color: "blue" }
- }
- }
- \endqml
- \endtable
+ \snippet doc/src/snippets/declarative/rectangle/rectangle-gradient.qml rectangles
+ \clearfloat
If both a gradient and a color are specified, the gradient will be used.
@@ -319,17 +356,21 @@ void QDeclarativeRectangle::setRadius(qreal radius)
\qmlproperty color Rectangle::color
This property holds the color used to fill the rectangle.
- \qml
- // green rectangle using hexidecimal notation
- Rectangle { color: "#00FF00" }
+ The default color is white.
- // steelblue rectangle using SVG color name
- Rectangle { color: "steelblue" }
- \endqml
+ \beginfloatright
+ \inlineimage rect-color.png
+ \endfloat
- The default color is white.
+ The following example shows rectangles with colors specified
+ using hexadecimal and named color notation:
+ \snippet doc/src/snippets/declarative/rectangle/rectangle-colors.qml rectangles
+
+ \clearfloat
If both a gradient and a color are specified, the gradient will be used.
+
+ \sa gradient
*/
QColor QDeclarativeRectangle::color() const
{
diff --git a/src/declarative/graphicsitems/qdeclarativetext.cpp b/src/declarative/graphicsitems/qdeclarativetext.cpp
index 14194a0f4b..1f4c1c7df3 100644
--- a/src/declarative/graphicsitems/qdeclarativetext.cpp
+++ b/src/declarative/graphicsitems/qdeclarativetext.cpp
@@ -1206,6 +1206,16 @@ void QDeclarativeText::mousePressEvent(QGraphicsSceneMouseEvent *event)
\qmlsignal Text::onLinkActivated(string link)
This handler is called when the user clicks on a link embedded in the text.
+ The link must be in rich text or HTML format and the
+ \a link string provides access to the particular link.
+
+ \snippet doc/src/snippets/declarative/text/onLinkActivated.qml 0
+
+ The example code will display the text
+ "The main website is at \l{http://qt.nokia.com}{Nokia Qt DF}."
+
+ Clicking on the highlighted link will output
+ \tt{http://qt.nokia.com link activated} to the console.
*/
/*!
diff --git a/src/declarative/graphicsitems/qdeclarativevisualitemmodel.cpp b/src/declarative/graphicsitems/qdeclarativevisualitemmodel.cpp
index a46ee73d83..a70886e2f4 100644
--- a/src/declarative/graphicsitems/qdeclarativevisualitemmodel.cpp
+++ b/src/declarative/graphicsitems/qdeclarativevisualitemmodel.cpp
@@ -287,16 +287,12 @@ public:
m_roles = m_listModelInterface->roles();
for (int ii = 0; ii < m_roles.count(); ++ii)
m_roleNames.insert(m_listModelInterface->toString(m_roles.at(ii)).toUtf8(), m_roles.at(ii));
- if (m_roles.count() == 1)
- m_roleNames.insert("modelData", m_roles.at(0));
} else if (m_abstractItemModel) {
for (QHash<int,QByteArray>::const_iterator it = m_abstractItemModel->roleNames().begin();
it != m_abstractItemModel->roleNames().end(); ++it) {
m_roles.append(it.key());
m_roleNames.insert(*it, it.key());
}
- if (m_roles.count() == 1)
- m_roleNames.insert("modelData", m_roles.at(0));
if (m_roles.count())
m_roleNames.insert("hasModelChildren", -1);
} else if (m_listAccessor) {
@@ -314,7 +310,8 @@ public:
}
}
- QHash<int,int> roleToPropId;
+ QHash<int,int> m_roleToPropId;
+ int m_modelDataPropId;
void createMetaData() {
if (!m_metaDataCreated) {
ensureRoles();
@@ -322,9 +319,12 @@ public:
QHash<QByteArray, int>::const_iterator it = m_roleNames.begin();
while (it != m_roleNames.end()) {
int propId = m_delegateDataType->createProperty(it.key()) - m_delegateDataType->propertyOffset();
- roleToPropId.insert(*it, propId);
+ m_roleToPropId.insert(*it, propId);
++it;
}
+ // Add modelData property
+ if (m_roles.count() == 1)
+ m_modelDataPropId = m_delegateDataType->createProperty("modelData") - m_delegateDataType->propertyOffset();
m_metaDataCreated = true;
}
}
@@ -430,6 +430,11 @@ public:
void setIndex(int index);
int propForRole(int) const;
+ int modelDataPropertyId() const {
+ QDeclarativeVisualDataModelPrivate *model = QDeclarativeVisualDataModelPrivate::get(m_model);
+ return model->m_modelDataPropId;
+ }
+
void setValue(int, const QVariant &);
bool hasValue(int id) const {
return m_meta->hasValue(id);
@@ -450,8 +455,8 @@ private:
int QDeclarativeVisualDataModelData::propForRole(int id) const
{
QDeclarativeVisualDataModelPrivate *model = QDeclarativeVisualDataModelPrivate::get(m_model);
- QHash<int,int>::const_iterator it = model->roleToPropId.find(id);
- if (it != model->roleToPropId.end())
+ QHash<int,int>::const_iterator it = model->m_roleToPropId.find(id);
+ if (it != model->m_roleToPropId.end())
return *it;
return -1;
@@ -518,14 +523,16 @@ QVariant QDeclarativeVisualDataModelDataMetaObject::initialValue(int propId)
}
} else if (model->m_abstractItemModel) {
model->ensureRoles();
+ QModelIndex index = model->m_abstractItemModel->index(data->m_index, 0, model->m_root);
if (propName == "hasModelChildren") {
- QModelIndex index = model->m_abstractItemModel->index(data->m_index, 0, model->m_root);
return model->m_abstractItemModel->hasChildren(index);
} else {
QHash<QByteArray,int>::const_iterator it = model->m_roleNames.find(propName);
if (it != model->m_roleNames.end()) {
- QModelIndex index = model->m_abstractItemModel->index(data->m_index, 0, model->m_root);
return model->m_abstractItemModel->data(index, *it);
+ } else if (model->m_roles.count() == 1 && propName == "modelData") {
+ //for compatibility with other lists, assign modelData if there is only a single role
+ return model->m_abstractItemModel->data(index, model->m_roles.first());
}
}
}
@@ -1195,6 +1202,19 @@ void QDeclarativeVisualDataModel::_q_itemsChanged(int index, int count,
qmlInfo(this) << "Changing role not present in item: " << roleName;
}
}
+ if (roles.count() == 1) {
+ // Handle the modelData role we add if there is just one role.
+ int propId = data->modelDataPropertyId();
+ if (data->hasValue(propId)) {
+ int role = roles.at(0);
+ if (d->m_listModelInterface) {
+ data->setValue(propId, d->m_listModelInterface->data(idx, QList<int>() << role).value(role));
+ } else if (d->m_abstractItemModel) {
+ QModelIndex index = d->m_abstractItemModel->index(idx, 0, d->m_root);
+ data->setValue(propId, d->m_abstractItemModel->data(index, role));
+ }
+ }
+ }
}
}
}
diff --git a/src/declarative/qml/qdeclarative.h b/src/declarative/qml/qdeclarative.h
index c6b64ae863..985ab72479 100644
--- a/src/declarative/qml/qdeclarative.h
+++ b/src/declarative/qml/qdeclarative.h
@@ -269,7 +269,7 @@ int qmlRegisterInterface(const char *typeName)
QByteArray pointerName(name + '*');
QByteArray listName("QDeclarativeListProperty<" + name + ">");
- QDeclarativePrivate::RegisterInterface interface = {
+ QDeclarativePrivate::RegisterInterface qmlInterface = {
0,
qRegisterMetaType<T *>(pointerName.constData()),
@@ -278,7 +278,7 @@ int qmlRegisterInterface(const char *typeName)
qobject_interface_iid<T *>()
};
- return QDeclarativePrivate::qmlregister(QDeclarativePrivate::InterfaceRegistration, &interface);
+ return QDeclarativePrivate::qmlregister(QDeclarativePrivate::InterfaceRegistration, &qmlInterface);
}
template<typename T>
diff --git a/src/declarative/qml/qdeclarativebinding.cpp b/src/declarative/qml/qdeclarativebinding.cpp
index e096305c97..cb6ad8c76f 100644
--- a/src/declarative/qml/qdeclarativebinding.cpp
+++ b/src/declarative/qml/qdeclarativebinding.cpp
@@ -48,6 +48,7 @@
#include "private/qdeclarativecontext_p.h"
#include "private/qdeclarativedata_p.h"
#include "private/qdeclarativestringconverters_p.h"
+#include "private/qdeclarativestate_p_p.h"
#include <QVariant>
#include <QtCore/qdebug.h>
@@ -373,6 +374,18 @@ void QDeclarativeAbstractBinding::removeFromObject()
}
}
+static void bindingDummyDeleter(QDeclarativeAbstractBinding *)
+{
+}
+
+QDeclarativeAbstractBinding::Pointer QDeclarativeAbstractBinding::weakPointer()
+{
+ if (m_selfPointer.isNull())
+ m_selfPointer = QSharedPointer<QDeclarativeAbstractBinding>(this, bindingDummyDeleter);
+
+ return m_selfPointer.toWeakRef();
+}
+
void QDeclarativeAbstractBinding::clear()
{
if (m_mePtr) {
diff --git a/src/declarative/qml/qdeclarativebinding_p.h b/src/declarative/qml/qdeclarativebinding_p.h
index 598f09faa7..941a1b3bfe 100644
--- a/src/declarative/qml/qdeclarativebinding_p.h
+++ b/src/declarative/qml/qdeclarativebinding_p.h
@@ -67,6 +67,8 @@ QT_BEGIN_NAMESPACE
class Q_DECLARATIVE_EXPORT QDeclarativeAbstractBinding
{
public:
+ typedef QWeakPointer<QDeclarativeAbstractBinding> Pointer;
+
QDeclarativeAbstractBinding();
virtual void destroy();
@@ -86,21 +88,26 @@ public:
void addToObject(QObject *);
void removeFromObject();
+ static Pointer getPointer(QDeclarativeAbstractBinding *p) { return p ? p->weakPointer() : Pointer(); }
+
protected:
virtual ~QDeclarativeAbstractBinding();
void clear();
private:
+ Pointer weakPointer();
friend class QDeclarativeData;
friend class QDeclarativeValueTypeProxyBinding;
friend class QDeclarativePropertyPrivate;
friend class QDeclarativeVME;
+ friend class QtSharedPointer::ExternalRefCount<QDeclarativeAbstractBinding>;
QObject *m_object;
QDeclarativeAbstractBinding **m_mePtr;
QDeclarativeAbstractBinding **m_prevBinding;
QDeclarativeAbstractBinding *m_nextBinding;
+ QSharedPointer<QDeclarativeAbstractBinding> m_selfPointer;
};
class QDeclarativeValueTypeProxyBinding : public QDeclarativeAbstractBinding
diff --git a/src/declarative/qml/qdeclarativecomponent_p.h b/src/declarative/qml/qdeclarativecomponent_p.h
index 1b1454b6bf..a551cc8072 100644
--- a/src/declarative/qml/qdeclarativecomponent_p.h
+++ b/src/declarative/qml/qdeclarativecomponent_p.h
@@ -74,7 +74,7 @@ class QDeclarativeEngine;
class QDeclarativeCompiledData;
class QDeclarativeComponentAttached;
-class QDeclarativeComponentPrivate : public QObjectPrivate, public QDeclarativeTypeData::TypeDataCallback
+class Q_AUTOTEST_EXPORT QDeclarativeComponentPrivate : public QObjectPrivate, public QDeclarativeTypeData::TypeDataCallback
{
Q_DECLARE_PUBLIC(QDeclarativeComponent)
diff --git a/src/declarative/qml/qdeclarativecustomparser_p.h b/src/declarative/qml/qdeclarativecustomparser_p.h
index 509e30a593..c3f9dd2660 100644
--- a/src/declarative/qml/qdeclarativecustomparser_p.h
+++ b/src/declarative/qml/qdeclarativecustomparser_p.h
@@ -117,7 +117,7 @@ public:
NoFlag = 0x00000000,
AcceptsAttachedProperties = 0x00000001
};
- Q_DECLARE_FLAGS(Flags, Flag);
+ Q_DECLARE_FLAGS(Flags, Flag)
QDeclarativeCustomParser() : compiler(0), object(0), m_flags(NoFlag) {}
QDeclarativeCustomParser(Flags f) : compiler(0), object(0), m_flags(f) {}
diff --git a/src/declarative/qml/qdeclarativeengine.cpp b/src/declarative/qml/qdeclarativeengine.cpp
index dd8e9cd4de..06b1ed355a 100644
--- a/src/declarative/qml/qdeclarativeengine.cpp
+++ b/src/declarative/qml/qdeclarativeengine.cpp
@@ -906,7 +906,7 @@ QDeclarativeEngine::ObjectOwnership QDeclarativeEngine::objectOwnership(QObject
return ddata->indestructible?CppOwnership:JavaScriptOwnership;
}
-void qmlExecuteDeferred(QObject *object)
+Q_AUTOTEST_EXPORT void qmlExecuteDeferred(QObject *object)
{
QDeclarativeData *data = QDeclarativeData::get(object);
diff --git a/src/declarative/qml/qdeclarativeobjectscriptclass.cpp b/src/declarative/qml/qdeclarativeobjectscriptclass.cpp
index 9d742381e4..ab6ff74b95 100644
--- a/src/declarative/qml/qdeclarativeobjectscriptclass.cpp
+++ b/src/declarative/qml/qdeclarativeobjectscriptclass.cpp
@@ -843,7 +843,7 @@ QDeclarativeObjectMethodScriptClass::Value QDeclarativeObjectMethodScriptClass::
for (int ii = 0; ii < argTypeNames.count(); ++ii) {
argTypes[ii] = QMetaType::type(argTypeNames.at(ii));
if (argTypes[ii] == QVariant::Invalid)
- argTypes[ii] = enumType(method->object->metaObject(), argTypeNames.at(ii));
+ argTypes[ii] = enumType(method->object->metaObject(), QString::fromLatin1(argTypeNames.at(ii)));
if (argTypes[ii] == QVariant::Invalid)
return Value(ctxt, ctxt->throwError(QString::fromLatin1("Unknown method parameter type: %1").arg(QLatin1String(argTypeNames.at(ii)))));
}
diff --git a/src/declarative/qml/qdeclarativescriptparser.cpp b/src/declarative/qml/qdeclarativescriptparser.cpp
index c956051060..57cc9ab13b 100644
--- a/src/declarative/qml/qdeclarativescriptparser.cpp
+++ b/src/declarative/qml/qdeclarativescriptparser.cpp
@@ -895,7 +895,7 @@ QList<QDeclarativeError> QDeclarativeScriptParser::errors() const
static void replaceWithSpace(QString &str, int idx, int n)
{
QChar *data = str.data() + idx;
- QChar space(' ');
+ const QChar space(QLatin1Char(' '));
for (int ii = 0; ii < n; ++ii)
*data++ = space;
}
diff --git a/src/declarative/qml/qdeclarativetypeloader.cpp b/src/declarative/qml/qdeclarativetypeloader.cpp
index 9b420657e2..061f309476 100644
--- a/src/declarative/qml/qdeclarativetypeloader.cpp
+++ b/src/declarative/qml/qdeclarativetypeloader.cpp
@@ -804,7 +804,7 @@ void QDeclarativeTypeData::done()
error.setUrl(finalUrl());
error.setLine(script.location.line);
error.setColumn(script.location.column);
- error.setDescription(typeLoader()->tr("Script %1 unavailable").arg(script.script->url().toString()));
+ error.setDescription(QDeclarativeTypeLoader::tr("Script %1 unavailable").arg(script.script->url().toString()));
errors.prepend(error);
setError(errors);
}
@@ -822,7 +822,7 @@ void QDeclarativeTypeData::done()
error.setUrl(finalUrl());
error.setLine(type.location.line);
error.setColumn(type.location.column);
- error.setDescription(typeLoader()->tr("Type %1 unavailable").arg(typeName));
+ error.setDescription(QDeclarativeTypeLoader::tr("Type %1 unavailable").arg(typeName));
errors.prepend(error);
setError(errors);
}
@@ -995,9 +995,9 @@ void QDeclarativeTypeData::resolveTypes()
QString userTypeName = parserRef->name;
userTypeName.replace(QLatin1Char('/'),QLatin1Char('.'));
if (typeNamespace)
- error.setDescription(typeLoader()->tr("Namespace %1 cannot be used as a type").arg(userTypeName));
+ error.setDescription(QDeclarativeTypeLoader::tr("Namespace %1 cannot be used as a type").arg(userTypeName));
else
- error.setDescription(typeLoader()->tr("%1 %2").arg(userTypeName).arg(errorString));
+ error.setDescription(QDeclarativeTypeLoader::tr("%1 %2").arg(userTypeName).arg(errorString));
if (!parserRef->refObjects.isEmpty()) {
QDeclarativeParser::Object *obj = parserRef->refObjects.first();
diff --git a/src/declarative/qml/qdeclarativevaluetype.cpp b/src/declarative/qml/qdeclarativevaluetype.cpp
index 98e9a585fb..fda62ee6f7 100644
--- a/src/declarative/qml/qdeclarativevaluetype.cpp
+++ b/src/declarative/qml/qdeclarativevaluetype.cpp
@@ -47,7 +47,7 @@
QT_BEGIN_NAMESPACE
-Q_DECL_IMPORT extern int qt_defaultDpi();
+Q_GUI_EXPORT int qt_defaultDpi();
template<typename T>
int qmlRegisterValueTypeEnums(const char *qmlName)
diff --git a/src/declarative/qml/qdeclarativexmlhttprequest.cpp b/src/declarative/qml/qdeclarativexmlhttprequest.cpp
index 301ea1de44..adfdbb380b 100644
--- a/src/declarative/qml/qdeclarativexmlhttprequest.cpp
+++ b/src/declarative/qml/qdeclarativexmlhttprequest.cpp
@@ -1641,8 +1641,8 @@ static QScriptValue qmlxmlhttprequest_responseXML(QScriptContext *context, QScri
THROW_REFERENCE("Not an XMLHttpRequest object");
if (!request->receivedXml() ||
- request->readyState() != QDeclarativeXMLHttpRequest::Loading &&
- request->readyState() != QDeclarativeXMLHttpRequest::Done)
+ (request->readyState() != QDeclarativeXMLHttpRequest::Loading &&
+ request->readyState() != QDeclarativeXMLHttpRequest::Done))
return engine->nullValue();
else
return Document::load(engine, request->rawResponseBody());
diff --git a/src/declarative/util/qdeclarativeanimation.cpp b/src/declarative/util/qdeclarativeanimation.cpp
index 6637282d77..3a96f98063 100644
--- a/src/declarative/util/qdeclarativeanimation.cpp
+++ b/src/declarative/util/qdeclarativeanimation.cpp
@@ -2699,14 +2699,15 @@ void QDeclarativeParentAnimation::transition(QDeclarativeStateActions &actions,
qreal scale = 1;
qreal rotation = 0;
- if (ok && transform.type() != QTransform::TxRotate) {
+ bool isRotate = (transform.type() == QTransform::TxRotate) || (transform.m11() < 0);
+ if (ok && !isRotate) {
if (transform.m11() == transform.m22())
scale = transform.m11();
else {
qmlInfo(this) << QDeclarativeParentAnimation::tr("Unable to preserve appearance under non-uniform scale");
ok = false;
}
- } else if (ok && transform.type() == QTransform::TxRotate) {
+ } else if (ok && isRotate) {
if (transform.m11() == transform.m22())
scale = qSqrt(transform.m11()*transform.m11() + transform.m12()*transform.m12());
else {
diff --git a/src/declarative/util/qdeclarativefontloader.cpp b/src/declarative/util/qdeclarativefontloader.cpp
index 1755855aae..6879494488 100644
--- a/src/declarative/util/qdeclarativefontloader.cpp
+++ b/src/declarative/util/qdeclarativefontloader.cpp
@@ -58,23 +58,91 @@
QT_BEGIN_NAMESPACE
+#define FONTLOADER_MAXIMUM_REDIRECT_RECURSION 16
+
+class QDeclarativeFontObject : public QObject
+{
+Q_OBJECT
+
+public:
+ QDeclarativeFontObject(int _id);
+
+ void download(const QUrl &url, QNetworkAccessManager *manager);
+
+Q_SIGNALS:
+ void fontDownloaded(const QString&, QDeclarativeFontLoader::Status);
+
+private Q_SLOTS:
+ void replyFinished();
+
+public:
+ int id;
+
+private:
+ QNetworkReply *reply;
+ int redirectCount;
+
+ Q_DISABLE_COPY(QDeclarativeFontObject)
+};
+
+QDeclarativeFontObject::QDeclarativeFontObject(int _id = -1)
+ : QObject(0), id(_id), reply(0), redirectCount(0) {}
+
+
+void QDeclarativeFontObject::download(const QUrl &url, QNetworkAccessManager *manager)
+{
+ QNetworkRequest req(url);
+ req.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true);
+ reply = manager->get(req);
+ QObject::connect(reply, SIGNAL(finished()), this, SLOT(replyFinished()));
+}
+
+void QDeclarativeFontObject::replyFinished()
+{
+ if (reply) {
+ redirectCount++;
+ if (redirectCount < FONTLOADER_MAXIMUM_REDIRECT_RECURSION) {
+ QVariant redirect = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
+ if (redirect.isValid()) {
+ QUrl url = reply->url().resolved(redirect.toUrl());
+ QNetworkAccessManager *manager = reply->manager();
+ reply->deleteLater();
+ reply = 0;
+ download(url, manager);
+ return;
+ }
+ }
+ redirectCount = 0;
+
+ if (!reply->error()) {
+ id = QFontDatabase::addApplicationFontFromData(reply->readAll());
+ if (id != -1)
+ emit fontDownloaded(QFontDatabase::applicationFontFamilies(id).at(0), QDeclarativeFontLoader::Ready);
+ else
+ emit fontDownloaded(QString(), QDeclarativeFontLoader::Error);
+ } else {
+ emit fontDownloaded(QString(), QDeclarativeFontLoader::Error);
+ }
+ reply->deleteLater();
+ reply = 0;
+ }
+}
+
+
class QDeclarativeFontLoaderPrivate : public QObjectPrivate
{
Q_DECLARE_PUBLIC(QDeclarativeFontLoader)
public:
- QDeclarativeFontLoaderPrivate() : reply(0), status(QDeclarativeFontLoader::Null), redirectCount(0) {}
-
- void addFontToDatabase(const QByteArray &);
+ QDeclarativeFontLoaderPrivate() : status(QDeclarativeFontLoader::Null) {}
QUrl url;
QString name;
- QNetworkReply *reply;
QDeclarativeFontLoader::Status status;
- int redirectCount;
+ static QHash<QUrl, QDeclarativeFontObject*> fonts;
};
-
+QHash<QUrl, QDeclarativeFontObject*> QDeclarativeFontLoaderPrivate::fonts;
/*!
\qmlclass FontLoader QDeclarativeFontLoader
@@ -127,30 +195,61 @@ void QDeclarativeFontLoader::setSource(const QUrl &url)
if (url == d->url)
return;
d->url = qmlContext(this)->resolvedUrl(url);
-
- d->status = Loading;
- emit statusChanged();
emit sourceChanged();
+
#ifndef QT_NO_LOCALFILE_OPTIMIZED_QML
- QString lf = QDeclarativeEnginePrivate::urlToLocalFileOrQrc(d->url);
- if (!lf.isEmpty()) {
- int id = QFontDatabase::addApplicationFont(lf);
- if (id != -1) {
- d->name = QFontDatabase::applicationFontFamilies(id).at(0);
- emit nameChanged();
- d->status = QDeclarativeFontLoader::Ready;
+ QString localFile = QDeclarativeEnginePrivate::urlToLocalFileOrQrc(d->url);
+ if (!localFile.isEmpty()) {
+ if (!d->fonts.contains(d->url)) {
+ int id = QFontDatabase::addApplicationFont(localFile);
+ if (id != -1) {
+ updateFontInfo(QFontDatabase::applicationFontFamilies(id).at(0), Ready);
+ QDeclarativeFontObject *fo = new QDeclarativeFontObject(id);
+ d->fonts[d->url] = fo;
+ } else {
+ updateFontInfo(QString(), Error);
+ }
} else {
- d->status = QDeclarativeFontLoader::Error;
- qmlInfo(this) << "Cannot load font: \"" << url.toString() << "\"";
+ updateFontInfo(QFontDatabase::applicationFontFamilies(d->fonts[d->url]->id).at(0), Ready);
}
- emit statusChanged();
} else
#endif
{
- QNetworkRequest req(d->url);
- req.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true);
- d->reply = qmlEngine(this)->networkAccessManager()->get(req);
- QObject::connect(d->reply, SIGNAL(finished()), this, SLOT(replyFinished()));
+ if (!d->fonts.contains(d->url)) {
+ QDeclarativeFontObject *fo = new QDeclarativeFontObject;
+ d->fonts[d->url] = fo;
+ fo->download(d->url, qmlEngine(this)->networkAccessManager());
+ d->status = Loading;
+ emit statusChanged();
+ QObject::connect(fo, SIGNAL(fontDownloaded(QString, QDeclarativeFontLoader::Status)),
+ this, SLOT(updateFontInfo(QString, QDeclarativeFontLoader::Status)));
+ } else {
+ QDeclarativeFontObject *fo = d->fonts[d->url];
+ if (fo->id == -1) {
+ d->status = Loading;
+ emit statusChanged();
+ QObject::connect(fo, SIGNAL(fontDownloaded(QString, QDeclarativeFontLoader::Status)),
+ this, SLOT(updateFontInfo(QString, QDeclarativeFontLoader::Status)));
+ }
+ else
+ updateFontInfo(QFontDatabase::applicationFontFamilies(fo->id).at(0), Ready);
+ }
+ }
+}
+
+void QDeclarativeFontLoader::updateFontInfo(const QString& name, QDeclarativeFontLoader::Status status)
+{
+ Q_D(QDeclarativeFontLoader);
+
+ if (name != d->name) {
+ d->name = name;
+ emit nameChanged();
+ }
+ if (status != d->status) {
+ if (status == Error)
+ qmlInfo(this) << "Cannot load font: \"" << d->url.toString() << "\"";
+ d->status = status;
+ emit statusChanged();
}
}
@@ -177,7 +276,7 @@ QString QDeclarativeFontLoader::name() const
void QDeclarativeFontLoader::setName(const QString &name)
{
Q_D(QDeclarativeFontLoader);
- if (d->name == name )
+ if (d->name == name)
return;
d->name = name;
emit nameChanged();
@@ -223,52 +322,6 @@ QDeclarativeFontLoader::Status QDeclarativeFontLoader::status() const
return d->status;
}
-#define FONTLOADER_MAXIMUM_REDIRECT_RECURSION 16
-
-void QDeclarativeFontLoader::replyFinished()
-{
- Q_D(QDeclarativeFontLoader);
- if (d->reply) {
- d->redirectCount++;
- if (d->redirectCount < FONTLOADER_MAXIMUM_REDIRECT_RECURSION) {
- QVariant redirect = d->reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
- if (redirect.isValid()) {
- QUrl url = d->reply->url().resolved(redirect.toUrl());
- d->reply->deleteLater();
- d->reply = 0;
- setSource(url);
- return;
- }
- }
- d->redirectCount=0;
-
- if (!d->reply->error()) {
- QByteArray ba = d->reply->readAll();
- d->addFontToDatabase(ba);
- } else {
- d->status = Error;
- qmlInfo(this) << "Cannot load font: \"" << d->reply->url().toString() << "\"";
- emit statusChanged();
- }
- d->reply->deleteLater();
- d->reply = 0;
- }
-}
-
-void QDeclarativeFontLoaderPrivate::addFontToDatabase(const QByteArray &ba)
-{
- Q_Q(QDeclarativeFontLoader);
-
- int id = QFontDatabase::addApplicationFontFromData(ba);
- if (id != -1) {
- name = QFontDatabase::applicationFontFamilies(id).at(0);
- emit q->nameChanged();
- status = QDeclarativeFontLoader::Ready;
- } else {
- status = QDeclarativeFontLoader::Error;
- qmlInfo(q) << "Cannot load font: \"" << url.toString() << "\"";
- }
- emit q->statusChanged();
-}
-
QT_END_NAMESPACE
+
+#include <qdeclarativefontloader.moc>
diff --git a/src/declarative/util/qdeclarativefontloader_p.h b/src/declarative/util/qdeclarativefontloader_p.h
index 0344d99b73..bebd5a0623 100644
--- a/src/declarative/util/qdeclarativefontloader_p.h
+++ b/src/declarative/util/qdeclarativefontloader_p.h
@@ -79,7 +79,7 @@ public:
Status status() const;
private Q_SLOTS:
- void replyFinished();
+ void updateFontInfo(const QString&, QDeclarativeFontLoader::Status);
Q_SIGNALS:
void sourceChanged();
diff --git a/src/declarative/util/qdeclarativelistmodel.cpp b/src/declarative/util/qdeclarativelistmodel.cpp
index c7d5024fd4..5ce95e90f4 100644
--- a/src/declarative/util/qdeclarativelistmodel.cpp
+++ b/src/declarative/util/qdeclarativelistmodel.cpp
@@ -58,6 +58,28 @@ Q_DECLARE_METATYPE(QListModelInterface *)
QT_BEGIN_NAMESPACE
+template<typename T>
+void qdeclarativelistmodel_move(int from, int to, int n, T *items)
+{
+ if (n == 1) {
+ items->move(from, to);
+ } else {
+ T replaced;
+ int i=0;
+ typename T::ConstIterator it=items->begin(); it += from+n;
+ for (; i<to-from; ++i,++it)
+ replaced.append(*it);
+ i=0;
+ it=items->begin(); it += from;
+ for (; i<n; ++i,++it)
+ replaced.append(*it);
+ typename T::ConstIterator f=replaced.begin();
+ typename T::Iterator t=items->begin(); t += from;
+ for (; f != replaced.end(); ++f, ++t)
+ *t = *f;
+ }
+}
+
QDeclarativeListModelParser::ListInstruction *QDeclarativeListModelParser::ListModelData::instructions() const
{
return (QDeclarativeListModelParser::ListInstruction *)((char *)this + sizeof(ListModelData));
@@ -69,49 +91,67 @@ QDeclarativeListModelParser::ListInstruction *QDeclarativeListModelParser::ListM
\since 4.7
\brief The ListModel element defines a free-form list data source.
- The ListModel is a simple hierarchy of elements containing data roles. The contents can
- be defined dynamically, or explicitly in QML:
+ The ListModel is a simple container of ListElement definitions, each containing data roles.
+ The contents can be defined dynamically, or explicitly in QML.
- For example:
+ The number of elements in the model can be obtained from its \l count property.
+ A number of familiar methods are also provided to manipulate the contents of the
+ model, including append(), insert(), move(), remove() and set(). These methods
+ accept dictionaries as their arguments; these are translated to ListElement objects
+ by the model.
- \snippet doc/src/snippets/declarative/listmodel.qml 0
+ Elements can be manipulated via the model using the setProperty() method, which
+ allows the roles of the specified element to be set and changed.
- Roles (properties) must begin with a lower-case letter. The above example defines a
- ListModel containing three elements, with the roles "name" and "cost".
+ \section1 Example Usage
- Values must be simple constants - either strings (quoted and optionally within a call to QT_TR_NOOP),
- bools (true, false), numbers, or enum values (like Text.AlignHCenter).
+ The following example shows a ListModel containing three elements, with the roles
+ "name" and "cost".
+
+ \beginfloatright
+ \inlineimage listmodel.png
+ \endfloat
+
+ \snippet doc/src/snippets/declarative/listmodel.qml 0
- The defined model can be used in views such as ListView:
+ \clearfloat
+ Roles (properties) in each element must begin with a lower-case letter and
+ should be common to all elements in a model. The ListElement documentation
+ provides more guidelines for how elements should be defined.
+
+ Since the example model contains an \c id property, it can be referenced
+ by views, such as the ListView in this example:
\snippet doc/src/snippets/declarative/listmodel-simple.qml 0
\dots 8
\snippet doc/src/snippets/declarative/listmodel-simple.qml 1
- \image listmodel.png
- It is possible for roles to contain list data. In the example below we create a list of fruit attributes:
+ It is possible for roles to contain list data. In the following example we
+ create a list of fruit attributes:
\snippet doc/src/snippets/declarative/listmodel-nested.qml model
- The delegate below displays all the fruit attributes:
+ The delegate displays all the fruit attributes:
- \snippet doc/src/snippets/declarative/listmodel-nested.qml delegate
- \image listmodel-nested.png
+ \beginfloatright
+ \inlineimage listmodel-nested.png
+ \endfloat
+ \snippet doc/src/snippets/declarative/listmodel-nested.qml delegate
- \section2 Modifying list models
+ \clearfloat
+ \section1 Modifying List Models
The content of a ListModel may be created and modified using the clear(),
append(), set() and setProperty() methods. For example:
-
- \snippet doc/src/snippets/declarative/listmodel-modify.qml delegate
- Note that when creating content dynamically the set of available properties cannot be changed
- once set. Whatever properties are first added to the model are the
- only permitted properties in the model.
+ \snippet doc/src/snippets/declarative/listmodel-modify.qml delegate
+ Note that when creating content dynamically the set of available properties
+ cannot be changed once set. Whatever properties are first added to the model
+ are the only permitted properties in the model.
- \section2 Using threaded list models with WorkerScript
+ \section1 Using Threaded List Models with WorkerScript
ListModel can be used together with WorkerScript access a list model
from multiple threads. This is useful if list modifications are
@@ -127,19 +167,19 @@ QDeclarativeListModelParser::ListInstruction *QDeclarativeListModelParser::ListM
\snippet examples/declarative/threading/threadedlistmodel/dataloader.js 0
-working-with-data
- worker script by calling \l WorkerScript::sendMessage(). When this message
- is received, \l {WorkerScript::onMessage}{WorkerScript.onMessage()} is invoked in
- \tt dataloader.js, which appends the current time to the list model.
+ The timer in the main example sends messages to the worker script by calling
+ \l WorkerScript::sendMessage(). When this message is received,
+ \l{WorkerScript::onMessage}{WorkerScript.onMessage()} is invoked in \c dataloader.js,
+ which appends the current time to the list model.
- Note the call to sync() from the \l {WorkerScript::onMessage}{WorkerScript.onMessage()} handler.
- You must call sync() or else the changes made to the list from the external
+ Note the call to sync() from the \l{WorkerScript::onMessage}{WorkerScript.onMessage()}
+ handler. You must call sync() or else the changes made to the list from the external
thread will not be reflected in the list model in the main thread.
- \section3 Limitations
+ \section1 Restrictions
- If a list model is to be accessed from a WorkerScript, it \bold cannot
- contain list data. So, the following model cannot be used from a WorkerScript
+ If a list model is to be accessed from a WorkerScript, it cannot
+ contain list-type data. So, the following model cannot be used from a WorkerScript
because of the list contained in the "attributes" property:
\code
@@ -156,7 +196,7 @@ working-with-data
}
\endcode
- In addition, the WorkerScript cannot add any list data to the model.
+ In addition, the WorkerScript cannot add list-type data to the model.
\sa {qmlmodels}{Data Models}, {declarative/threading/threadedlistmodel}{Threaded ListModel example}, QtDeclarative
*/
@@ -177,17 +217,25 @@ working-with-data
*/
QDeclarativeListModel::QDeclarativeListModel(QObject *parent)
-: QListModelInterface(parent), m_agent(0), m_nested(new NestedListModel(this)), m_flat(0), m_isWorkerCopy(false)
+: QListModelInterface(parent), m_agent(0), m_nested(new NestedListModel(this)), m_flat(0)
{
}
-QDeclarativeListModel::QDeclarativeListModel(bool workerCopy, QObject *parent)
-: QListModelInterface(parent), m_agent(0), m_nested(0), m_flat(0), m_isWorkerCopy(workerCopy)
+QDeclarativeListModel::QDeclarativeListModel(const QDeclarativeListModel *orig, QDeclarativeListModelWorkerAgent *parent)
+: QListModelInterface(parent), m_agent(0), m_nested(0), m_flat(0)
{
- if (workerCopy)
- m_flat = new FlatListModel(this);
- else
- m_nested = new NestedListModel(this);
+ m_flat = new FlatListModel(this);
+ m_flat->m_parentAgent = parent;
+
+ if (orig->m_flat) {
+ m_flat->m_roles = orig->m_flat->m_roles;
+ m_flat->m_strings = orig->m_flat->m_strings;
+ m_flat->m_values = orig->m_flat->m_values;
+
+ m_flat->m_nodeData.reserve(m_flat->m_values.count());
+ for (int i=0; i<m_flat->m_values.count(); i++)
+ m_flat->m_nodeData << 0;
+ }
}
QDeclarativeListModel::~QDeclarativeListModel()
@@ -223,19 +271,28 @@ bool QDeclarativeListModel::flatten()
flat->m_strings.insert(s, roles[i]);
}
+ flat->m_nodeData.reserve(flat->m_values.count());
+ for (int i=0; i<flat->m_values.count(); i++)
+ flat->m_nodeData << 0;
+
m_flat = flat;
delete m_nested;
m_nested = 0;
return true;
}
+bool QDeclarativeListModel::inWorkerThread() const
+{
+ return m_flat && m_flat->m_parentAgent;
+}
+
QDeclarativeListModelWorkerAgent *QDeclarativeListModel::agent()
{
if (m_agent)
return m_agent;
if (!flatten()) {
- qmlInfo(this) << "List contains nested list values and cannot be used from a worker script";
+ qmlInfo(this) << "List contains list-type data and cannot be used from a worker script";
return 0;
}
@@ -293,12 +350,43 @@ void QDeclarativeListModel::clear()
else
m_nested->clear();
- if (!m_isWorkerCopy) {
+ if (!inWorkerThread()) {
emit itemsRemoved(0, cleared);
emit countChanged();
}
}
+QDeclarativeListModel *ModelNode::model(const NestedListModel *model)
+{
+ if (!modelCache) {
+ modelCache = new QDeclarativeListModel;
+ QDeclarativeEngine::setContextForObject(modelCache,QDeclarativeEngine::contextForObject(model->m_listModel));
+ modelCache->m_nested->_root = this; // ListModel defaults to nestable model
+
+ for (int i=0; i<values.count(); ++i) {
+ ModelNode *subNode = qvariant_cast<ModelNode *>(values.at(i));
+ if (subNode)
+ subNode->m_model = modelCache->m_nested;
+ }
+ }
+ return modelCache;
+}
+
+ModelObject *ModelNode::object(const NestedListModel *model)
+{
+ if (!objectCache) {
+ objectCache = new ModelObject(this,
+ const_cast<NestedListModel*>(model),
+ QDeclarativeEnginePrivate::getScriptEngine(qmlEngine(model->m_listModel)));
+ QHash<QString, ModelNode *>::iterator it;
+ for (it = properties.begin(); it != properties.end(); ++it) {
+ objectCache->setValue(it.key().toUtf8(), model->valueForNode(*it));
+ }
+ objectCache->setNodeUpdatesEnabled(true);
+ }
+ return objectCache;
+}
+
/*!
\qmlmethod ListModel::remove(int index)
@@ -318,7 +406,7 @@ void QDeclarativeListModel::remove(int index)
else
m_nested->remove(index);
- if (!m_isWorkerCopy) {
+ if (!inWorkerThread()) {
emit itemsRemoved(index, 1);
emit countChanged();
}
@@ -352,7 +440,7 @@ void QDeclarativeListModel::insert(int index, const QScriptValue& valuemap)
}
bool ok = m_flat ? m_flat->insert(index, valuemap) : m_nested->insert(index, valuemap);
- if (ok && !m_isWorkerCopy) {
+ if (ok && !inWorkerThread()) {
emit itemsInserted(index, 1);
emit countChanged();
}
@@ -376,7 +464,7 @@ void QDeclarativeListModel::move(int from, int to, int n)
{
if (n==0 || from==to)
return;
- if (from+n > count() || to+n > count() || from < 0 || to < 0 || n < 0) {
+ if (!canMove(from, to, n)) {
qmlInfo(this) << tr("move: out of range");
return;
}
@@ -398,7 +486,7 @@ void QDeclarativeListModel::move(int from, int to, int n)
else
m_nested->move(from, to, n);
- if (!m_isWorkerCopy)
+ if (!inWorkerThread())
emit itemsMoved(origfrom, origto, orign);
}
@@ -427,11 +515,15 @@ void QDeclarativeListModel::append(const QScriptValue& valuemap)
/*!
\qmlmethod object ListModel::get(int index)
- Returns the item at \a index in the list model.
+ Returns the item at \a index in the list model. This allows the item
+ data to be accessed or modified from JavaScript:
\code
- fruitModel.append({"cost": 5.95, "name":"Jackfruit"})
- fruitModel.get(0).cost
+ Component.onCompleted: {
+ fruitModel.append({"cost": 5.95, "name":"Jackfruit"});
+ console.log(fruitModel.get(0).cost);
+ fruitModel.get(0).cost = 10.95;
+ }
\endcode
The \a index must be an element in the list.
@@ -446,6 +538,9 @@ void QDeclarativeListModel::append(const QScriptValue& valuemap)
fruitModel.get(0).attributes.get(1).value; // == "green"
\endcode
+ \warning The returned object is not guaranteed to remain valid. It
+ should not be used in \l{Property Binding}{property bindings}.
+
\sa append()
*/
QScriptValue QDeclarativeListModel::get(int index) const
@@ -489,7 +584,7 @@ void QDeclarativeListModel::set(int index, const QScriptValue& valuemap)
else
m_nested->set(index, valuemap, &roles);
- if (!m_isWorkerCopy)
+ if (!inWorkerThread())
emit itemsChanged(index, 1, roles);
}
}
@@ -520,7 +615,7 @@ void QDeclarativeListModel::setProperty(int index, const QString& property, cons
else
m_nested->setProperty(index, property, value, &roles);
- if (!m_isWorkerCopy)
+ if (!inWorkerThread())
emit itemsChanged(index, 1, roles);
}
@@ -687,7 +782,7 @@ void QDeclarativeListModelParser::setCustomData(QObject *obj, const QByteArray &
{
QDeclarativeListModel *rv = static_cast<QDeclarativeListModel *>(obj);
- ModelNode *root = new ModelNode;
+ ModelNode *root = new ModelNode(rv->m_nested);
rv->m_nested->_root = root;
QStack<ModelNode *> nodes;
nodes << root;
@@ -704,7 +799,7 @@ void QDeclarativeListModelParser::setCustomData(QObject *obj, const QByteArray &
case ListInstruction::Push:
{
ModelNode *n = nodes.top();
- ModelNode *n2 = new ModelNode;
+ ModelNode *n2 = new ModelNode(rv->m_nested);
n->values << QVariant::fromValue(n2);
nodes.push(n2);
if (processingSet)
@@ -743,7 +838,7 @@ void QDeclarativeListModelParser::setCustomData(QObject *obj, const QByteArray &
case ListInstruction::Set:
{
ModelNode *n = nodes.top();
- ModelNode *n2 = new ModelNode;
+ ModelNode *n2 = new ModelNode(rv->m_nested);
n->properties.insert(QString::fromUtf8(data + instr.dataIdx), n2);
nodes.push(n2);
processingSet = true;
@@ -751,6 +846,13 @@ void QDeclarativeListModelParser::setCustomData(QObject *obj, const QByteArray &
break;
}
}
+
+ ModelNode *rootNode = rv->m_nested->_root;
+ for (int i=0; i<rootNode->values.count(); ++i) {
+ ModelNode *node = qvariant_cast<ModelNode *>(rootNode->values[i]);
+ node->listIndex = i;
+ node->updateListIndexes();
+ }
}
bool QDeclarativeListModelParser::definesEmptyList(const QString &s)
@@ -765,22 +867,57 @@ bool QDeclarativeListModelParser::definesEmptyList(const QString &s)
return false;
}
+
/*!
\qmlclass ListElement QDeclarativeListElement
\ingroup qml-working-with-data
\since 4.7
\brief The ListElement element defines a data item in a ListModel.
+ List elements are defined inside ListModel definitions, and represent items in a
+ list that will be displayed using ListView or \l Repeater items.
+
+ List elements are defined like other QML elements except that they contain
+ a collection of \e role definitions instead of properties. Using the same
+ syntax as property definitions, roles both define how the data is accessed
+ and include the data itself.
+
+ The names used for roles must begin with a lower-case letter and should be
+ common to all elements in a given model. Values must be simple constants; either
+ strings (quoted and optionally within a call to QT_TR_NOOP), boolean values
+ (true, false), numbers, or enumeration values (such as AlignText.AlignHCenter).
+
+ \section1 Referencing Roles
+
+ The role names are used by delegates to obtain data from list elements.
+ Each role name is accessible in the delegate's scope, and refers to the
+ corresponding role in the current element. Where a role name would be
+ ambiguous to use, it can be accessed via the \l{ListView::}{model}
+ property (e.g., \c{model.cost} instead of \c{cost}).
+
+ \section1 Example Usage
+
+ The following model defines a series of list elements, each of which
+ contain "name" and "cost" roles and their associated values.
+
+ \snippet doc/src/snippets/declarative/qml-data-models/listelements.qml model
+
+ The delegate obtains the name and cost for each element by simply referring
+ to \c name and \c cost:
+
+ \snippet doc/src/snippets/declarative/qml-data-models/listelements.qml view
+
\sa ListModel
*/
FlatListModel::FlatListModel(QDeclarativeListModel *base)
- : m_scriptEngine(0), m_listModel(base)
+ : m_scriptEngine(0), m_listModel(base), m_scriptClass(0), m_parentAgent(0)
{
}
FlatListModel::~FlatListModel()
{
+ qDeleteAll(m_nodeData);
}
QHash<int,QVariant> FlatListModel::data(int index, const QList<int> &roles) const
@@ -824,11 +961,15 @@ int FlatListModel::count() const
void FlatListModel::clear()
{
m_values.clear();
+
+ qDeleteAll(m_nodeData);
+ m_nodeData.clear();
}
void FlatListModel::remove(int index)
{
m_values.removeAt(index);
+ removedNode(index);
}
bool FlatListModel::append(const QScriptValue &value)
@@ -845,6 +986,8 @@ bool FlatListModel::insert(int index, const QScriptValue &value)
return false;
m_values.insert(index, row);
+ insertedNode(index);
+
return true;
}
@@ -858,13 +1001,17 @@ QScriptValue FlatListModel::get(int index) const
if (index < 0 || index >= m_values.count())
return scriptEngine->undefinedValue();
- QScriptValue rv = scriptEngine->newObject();
+ FlatListModel *that = const_cast<FlatListModel*>(this);
+ if (!m_scriptClass)
+ that->m_scriptClass = new FlatListScriptClass(that, scriptEngine);
- QHash<int, QVariant> row = m_values.at(index);
- for (QHash<int, QVariant>::ConstIterator iter = row.begin(); iter != row.end(); ++iter)
- rv.setProperty(m_roles.value(iter.key()), scriptEngine->toScriptValue(iter.value()));
+ FlatNodeData *data = m_nodeData.value(index);
+ if (!data) {
+ data = new FlatNodeData(index);
+ that->m_nodeData.replace(index, data);
+ }
- return rv;
+ return QScriptDeclarativeClass::newObject(scriptEngine, m_scriptClass, new FlatNodeObjectData(data));
}
void FlatListModel::set(int index, const QScriptValue &value, QList<int> *roles)
@@ -896,23 +1043,8 @@ void FlatListModel::setProperty(int index, const QString& property, const QVaria
void FlatListModel::move(int from, int to, int n)
{
- if (n == 1) {
- m_values.move(from, to);
- } else {
- QList<QHash<int, QVariant> > replaced;
- int i=0;
- QList<QHash<int, QVariant> >::ConstIterator it=m_values.begin(); it += from+n;
- for (; i<to-from; ++i,++it)
- replaced.append(*it);
- i=0;
- it=m_values.begin(); it += from;
- for (; i<n; ++i,++it)
- replaced.append(*it);
- QList<QHash<int, QVariant> >::ConstIterator f=replaced.begin();
- QList<QHash<int, QVariant> >::Iterator t=m_values.begin(); t += from;
- for (; f != replaced.end(); ++f, ++t)
- *t = *f;
- }
+ qdeclarativelistmodel_move<QList<QHash<int, QVariant> > >(from, to, n, &m_values);
+ moveNodes(from, to, n);
}
bool FlatListModel::addValue(const QScriptValue &value, QHash<int, QVariant> *row, QList<int> *roles)
@@ -922,7 +1054,7 @@ bool FlatListModel::addValue(const QScriptValue &value, QHash<int, QVariant> *ro
it.next();
QScriptValue value = it.value();
if (!value.isVariant() && !value.isRegExp() && !value.isDate() && value.isObject()) {
- qmlInfo(m_listModel) << "Cannot add nested list values when modifying or after modification from a worker script";
+ qmlInfo(m_listModel) << "Cannot add list-type data when modifying or after modification from a worker script";
return false;
}
@@ -942,6 +1074,139 @@ bool FlatListModel::addValue(const QScriptValue &value, QHash<int, QVariant> *ro
return true;
}
+void FlatListModel::insertedNode(int index)
+{
+ if (index >= 0 && index <= m_values.count()) {
+ m_nodeData.insert(index, 0);
+
+ for (int i=index + 1; i<m_nodeData.count(); i++) {
+ if (m_nodeData[i])
+ m_nodeData[i]->index = i;
+ }
+ }
+}
+
+void FlatListModel::removedNode(int index)
+{
+ if (index >= 0 && index < m_nodeData.count()) {
+ delete m_nodeData.takeAt(index);
+
+ for (int i=index; i<m_nodeData.count(); i++) {
+ if (m_nodeData[i])
+ m_nodeData[i]->index = i;
+ }
+ }
+}
+
+void FlatListModel::moveNodes(int from, int to, int n)
+{
+ if (!m_listModel->canMove(from, to, n))
+ return;
+
+ qdeclarativelistmodel_move<QList<FlatNodeData *> >(from, to, n, &m_nodeData);
+
+ for (int i=from; i<from + (to-from); i++) {
+ if (m_nodeData[i])
+ m_nodeData[i]->index = i;
+ }
+}
+
+
+
+FlatNodeData::~FlatNodeData()
+{
+ for (QSet<FlatNodeObjectData *>::Iterator iter = objects.begin(); iter != objects.end(); ++iter) {
+ FlatNodeObjectData *data = *iter;
+ data->nodeData = 0;
+ }
+}
+
+void FlatNodeData::addData(FlatNodeObjectData *data)
+{
+ objects.insert(data);
+}
+
+void FlatNodeData::removeData(FlatNodeObjectData *data)
+{
+ objects.remove(data);
+}
+
+
+FlatListScriptClass::FlatListScriptClass(FlatListModel *model, QScriptEngine *seng)
+ : QScriptDeclarativeClass(seng),
+ m_model(model)
+{
+}
+
+QScriptDeclarativeClass::Value FlatListScriptClass::property(Object *obj, const Identifier &name)
+{
+ FlatNodeObjectData *objData = static_cast<FlatNodeObjectData*>(obj);
+ if (!objData->nodeData) // item at this index has been deleted
+ return QScriptDeclarativeClass::Value(engine(), engine()->undefinedValue());
+
+ int index = objData->nodeData->index;
+ QString propName = toString(name);
+ int role = m_model->m_strings.value(propName, -1);
+
+ if (role >= 0 && index >=0 ) {
+ const QHash<int, QVariant> &row = m_model->m_values[index];
+ QScriptValue sv = engine()->toScriptValue<QVariant>(row[role]);
+ return QScriptDeclarativeClass::Value(engine(), sv);
+ }
+
+ return QScriptDeclarativeClass::Value(engine(), engine()->undefinedValue());
+}
+
+void FlatListScriptClass::setProperty(Object *obj, const Identifier &name, const QScriptValue &value)
+{
+ if (!value.isVariant() && !value.isRegExp() && !value.isDate() && value.isObject()) {
+ qmlInfo(m_model->m_listModel) << "Cannot add list-type data when modifying or after modification from a worker script";
+ return;
+ }
+
+ FlatNodeObjectData *objData = static_cast<FlatNodeObjectData*>(obj);
+ if (!objData->nodeData) // item at this index has been deleted
+ return;
+
+ int index = objData->nodeData->index;
+ QString propName = toString(name);
+
+ int role = m_model->m_strings.value(propName, -1);
+ if (role >= 0 && index >= 0) {
+ QHash<int, QVariant> &row = m_model->m_values[index];
+ row[role] = value.toVariant();
+
+ if (m_model->m_parentAgent) {
+ // This is the list in the worker thread, so tell the agent to
+ // emit itemsChanged() later
+ m_model->m_parentAgent->changedData(index, 1);
+ } else {
+ // This is the list in the main thread, so emit itemsChanged()
+ QList<int> roles;
+ roles << role;
+ emit m_model->m_listModel->itemsChanged(index, 1, roles);
+ }
+ }
+}
+
+QScriptClass::QueryFlags FlatListScriptClass::queryProperty(Object *, const Identifier &, QScriptClass::QueryFlags)
+{
+ return (QScriptClass::HandlesReadAccess | QScriptClass::HandlesWriteAccess);
+}
+
+bool FlatListScriptClass::compare(Object *obj1, Object *obj2)
+{
+ FlatNodeObjectData *data1 = static_cast<FlatNodeObjectData*>(obj1);
+ FlatNodeObjectData *data2 = static_cast<FlatNodeObjectData*>(obj2);
+
+ if (!data1->nodeData || !data2->nodeData)
+ return false;
+
+ return data1->nodeData->index == data2->nodeData->index;
+}
+
+
+
NestedListModel::NestedListModel(QDeclarativeListModel *base)
: _root(0), m_ownsRoot(false), m_listModel(base), _rolesOk(false)
{
@@ -1067,11 +1332,12 @@ void NestedListModel::remove(int index)
bool NestedListModel::insert(int index, const QScriptValue& valuemap)
{
if (!_root) {
- _root = new ModelNode;
+ _root = new ModelNode(this);
m_ownsRoot = true;
}
- ModelNode *mn = new ModelNode;
+ ModelNode *mn = new ModelNode(this);
+ mn->listIndex = index;
mn->setObjectValue(valuemap);
_root->values.insert(index,QVariant::fromValue(mn));
return true;
@@ -1079,34 +1345,19 @@ bool NestedListModel::insert(int index, const QScriptValue& valuemap)
void NestedListModel::move(int from, int to, int n)
{
- if (n==1) {
- _root->values.move(from,to);
- } else {
- QList<QVariant> replaced;
- int i=0;
- QVariantList::const_iterator it=_root->values.begin(); it += from+n;
- for (; i<to-from; ++i,++it)
- replaced.append(*it);
- i=0;
- it=_root->values.begin(); it += from;
- for (; i<n; ++i,++it)
- replaced.append(*it);
- QVariantList::const_iterator f=replaced.begin();
- QVariantList::iterator t=_root->values.begin(); t += from;
- for (; f != replaced.end(); ++f, ++t)
- *t = *f;
- }
+ if (!_root)
+ return;
+ qdeclarativelistmodel_move<QVariantList>(from, to, n, &_root->values);
}
bool NestedListModel::append(const QScriptValue& valuemap)
{
if (!_root) {
- _root = new ModelNode;
+ _root = new ModelNode(this);
m_ownsRoot = true;
}
- ModelNode *mn = new ModelNode;
- mn->setObjectValue(valuemap);
- _root->values << QVariant::fromValue(mn);
+
+ insert(count(), valuemap);
return true;
}
@@ -1201,8 +1452,8 @@ QString NestedListModel::toString(int role) const
}
-ModelNode::ModelNode()
-: modelCache(0), objectCache(0), isArray(false)
+ModelNode::ModelNode(NestedListModel *model)
+: modelCache(0), objectCache(0), isArray(false), m_model(model), listIndex(-1)
{
}
@@ -1226,18 +1477,18 @@ void ModelNode::clear()
properties.clear();
}
-void ModelNode::setObjectValue(const QScriptValue& valuemap) {
+void ModelNode::setObjectValue(const QScriptValue& valuemap, bool writeToCache) {
QScriptValueIterator it(valuemap);
while (it.hasNext()) {
it.next();
- ModelNode *value = new ModelNode;
+ ModelNode *value = new ModelNode(m_model);
QScriptValue v = it.value();
if (v.isArray()) {
value->isArray = true;
value->setListValue(v);
} else {
value->values << v.toVariant();
- if (objectCache)
+ if (writeToCache && objectCache)
objectCache->setValue(it.name().toUtf8(), value->values.last());
}
if (properties.contains(it.name()))
@@ -1250,14 +1501,16 @@ void ModelNode::setListValue(const QScriptValue& valuelist) {
values.clear();
int size = valuelist.property(QLatin1String("length")).toInt32();
for (int i=0; i<size; i++) {
- ModelNode *value = new ModelNode;
+ ModelNode *value = new ModelNode(m_model);
QScriptValue v = valuelist.property(i);
if (v.isArray()) {
value->isArray = true;
value->setListValue(v);
} else if (v.isObject()) {
+ value->listIndex = i;
value->setObjectValue(v);
} else {
+ value->listIndex = i;
value->values << v.toVariant();
}
values.append(QVariant::fromValue(value));
@@ -1269,7 +1522,7 @@ void ModelNode::setProperty(const QString& prop, const QVariant& val) {
if (it != properties.end()) {
(*it)->values[0] = val;
} else {
- ModelNode *n = new ModelNode;
+ ModelNode *n = new ModelNode(m_model);
n->values << val;
properties.insert(prop,n);
}
@@ -1277,6 +1530,40 @@ void ModelNode::setProperty(const QString& prop, const QVariant& val) {
objectCache->setValue(prop.toUtf8(), val);
}
+void ModelNode::updateListIndexes()
+{
+ for (QHash<QString, ModelNode *>::ConstIterator iter = properties.begin(); iter != properties.end(); ++iter) {
+ ModelNode *node = iter.value();
+ if (node->isArray) {
+ for (int i=0; i<node->values.count(); ++i) {
+ ModelNode *subNode = qvariant_cast<ModelNode *>(node->values.at(i));
+ if (subNode)
+ subNode->listIndex = i;
+ }
+ }
+ node->updateListIndexes();
+ }
+}
+
+/*
+ Need to call this to emit itemsChanged() for modifications outside of set()
+ and setProperty(), i.e. if an item returned from get() is modified
+*/
+void ModelNode::changedProperty(const QString &name) const
+{
+ if (listIndex < 0)
+ return;
+
+ m_model->checkRoles();
+ QList<int> roles;
+ int role = m_model->roleStrings.indexOf(name);
+ if (role < 0)
+ roles = m_model->roles();
+ else
+ roles << role;
+ emit m_model->m_listModel->itemsChanged(listIndex, 1, roles);
+}
+
void ModelNode::dump(ModelNode *node, int ind)
{
QByteArray indentBa(ind * 4, ' ');
@@ -1298,16 +1585,47 @@ void ModelNode::dump(ModelNode *node, int ind)
}
}
-ModelObject::ModelObject()
-: _mo(new QDeclarativeOpenMetaObject(this))
+ModelObject::ModelObject(ModelNode *node, NestedListModel *model, QScriptEngine *seng)
+ : m_model(model),
+ m_node(node),
+ m_meta(new ModelNodeMetaObject(seng, this))
{
}
void ModelObject::setValue(const QByteArray &name, const QVariant &val)
{
- _mo->setValue(name, val);
+ m_meta->setValue(name, val);
setProperty(name.constData(), val);
}
+void ModelObject::setNodeUpdatesEnabled(bool enable)
+{
+ m_meta->m_enabled = enable;
+}
+
+
+ModelNodeMetaObject::ModelNodeMetaObject(QScriptEngine *seng, ModelObject *object)
+ : QDeclarativeOpenMetaObject(object),
+ m_enabled(false),
+ m_seng(seng),
+ m_obj(object)
+{
+}
+
+void ModelNodeMetaObject::propertyWritten(int index)
+{
+ if (!m_enabled)
+ return;
+
+ QString propName = QString::fromUtf8(name(index));
+ QVariant value = operator[](index);
+
+ QScriptValue sv = m_seng->newObject();
+ sv.setProperty(propName, m_seng->newVariant(value));
+ m_obj->m_node->setObjectValue(sv, false);
+
+ m_obj->m_node->changedProperty(propName);
+}
+
QT_END_NAMESPACE
diff --git a/src/declarative/util/qdeclarativelistmodel_p.h b/src/declarative/util/qdeclarativelistmodel_p.h
index 6aff9c60cc..fe42ef6586 100644
--- a/src/declarative/util/qdeclarativelistmodel_p.h
+++ b/src/declarative/util/qdeclarativelistmodel_p.h
@@ -63,6 +63,7 @@ class FlatListModel;
class NestedListModel;
class QDeclarativeListModelWorkerAgent;
struct ModelNode;
+class FlatListScriptClass;
class Q_DECLARATIVE_EXPORT QDeclarativeListModel : public QListModelInterface
{
Q_OBJECT
@@ -96,16 +97,21 @@ Q_SIGNALS:
private:
friend class QDeclarativeListModelParser;
friend class QDeclarativeListModelWorkerAgent;
+ friend class FlatListModel;
+ friend class FlatListScriptClass;
friend struct ModelNode;
- QDeclarativeListModel(bool workerCopy, QObject *parent=0);
+ // Constructs a flat list model for a worker agent
+ QDeclarativeListModel(const QDeclarativeListModel *orig, QDeclarativeListModelWorkerAgent *parent);
+
bool flatten();
- bool modifyCheck();
+ bool inWorkerThread() const;
+
+ inline bool canMove(int from, int to, int n) const { return !(from+n > count() || to+n > count() || from < 0 || to < 0 || n < 0); }
QDeclarativeListModelWorkerAgent *m_agent;
NestedListModel *m_nested;
FlatListModel *m_flat;
- bool m_isWorkerCopy;
};
// ### FIXME
diff --git a/src/declarative/util/qdeclarativelistmodel_p_p.h b/src/declarative/util/qdeclarativelistmodel_p_p.h
index 8231414d63..d2d40ee67a 100644
--- a/src/declarative/util/qdeclarativelistmodel_p_p.h
+++ b/src/declarative/util/qdeclarativelistmodel_p_p.h
@@ -54,9 +54,11 @@
//
#include "private/qdeclarativelistmodel_p.h"
-
-#include "qdeclarative.h"
#include "private/qdeclarativeengine_p.h"
+#include "private/qdeclarativeopenmetaobject_p.h"
+#include "qdeclarative.h"
+
+#include <private/qscriptdeclarativeclass_p.h>
QT_BEGIN_HEADER
@@ -68,6 +70,8 @@ class QDeclarativeOpenMetaObject;
class QScriptEngine;
class QDeclarativeListModelWorkerAgent;
struct ModelNode;
+class FlatListScriptClass;
+class FlatNodeData;
class FlatListModel
{
@@ -94,16 +98,82 @@ public:
private:
friend class QDeclarativeListModelWorkerAgent;
friend class QDeclarativeListModel;
+ friend class FlatListScriptClass;
+ friend class FlatNodeData;
bool addValue(const QScriptValue &value, QHash<int, QVariant> *row, QList<int> *roles);
+ void insertedNode(int index);
+ void removedNode(int index);
+ void moveNodes(int from, int to, int n);
QScriptEngine *m_scriptEngine;
QHash<int, QString> m_roles;
QHash<QString, int> m_strings;
QList<QHash<int, QVariant> > m_values;
QDeclarativeListModel *m_listModel;
+
+ FlatListScriptClass *m_scriptClass;
+ QList<FlatNodeData *> m_nodeData;
+ QDeclarativeListModelWorkerAgent *m_parentAgent;
+};
+
+
+/*
+ Created when get() is called on a FlatListModel. This allows changes to the
+ object returned by get() to be tracked, and passed onto the model.
+*/
+class FlatListScriptClass : public QScriptDeclarativeClass
+{
+public:
+ FlatListScriptClass(FlatListModel *model, QScriptEngine *seng);
+
+ Value property(Object *, const Identifier &);
+ void setProperty(Object *, const Identifier &name, const QScriptValue &);
+ QScriptClass::QueryFlags queryProperty(Object *, const Identifier &, QScriptClass::QueryFlags flags);
+ bool compare(Object *, Object *);
+
+private:
+ FlatListModel *m_model;
+};
+
+/*
+ FlatNodeData and FlatNodeObjectData allow objects returned by get() to still
+ point to the correct list index if move(), insert() or remove() are called.
+*/
+struct FlatNodeObjectData;
+class FlatNodeData
+{
+public:
+ FlatNodeData(int i)
+ : index(i) {}
+
+ ~FlatNodeData();
+
+ void addData(FlatNodeObjectData *data);
+ void removeData(FlatNodeObjectData *data);
+
+ int index;
+
+private:
+ QSet<FlatNodeObjectData*> objects;
+};
+
+struct FlatNodeObjectData : public QScriptDeclarativeClass::Object
+{
+ FlatNodeObjectData(FlatNodeData *data) : nodeData(data) {
+ nodeData->addData(this);
+ }
+
+ ~FlatNodeObjectData() {
+ if (nodeData)
+ nodeData->removeData(this);
+ }
+
+ FlatNodeData *nodeData;
};
+
+
class NestedListModel
{
public:
@@ -134,25 +204,50 @@ public:
QDeclarativeListModel *m_listModel;
private:
+ friend struct ModelNode;
mutable QStringList roleStrings;
mutable bool _rolesOk;
};
+class ModelNodeMetaObject;
class ModelObject : public QObject
{
Q_OBJECT
public:
- ModelObject();
+ ModelObject(ModelNode *node, NestedListModel *model, QScriptEngine *seng);
void setValue(const QByteArray &name, const QVariant &val);
+ void setNodeUpdatesEnabled(bool enable);
+
+ NestedListModel *m_model;
+ ModelNode *m_node;
private:
- QDeclarativeOpenMetaObject *_mo;
+ ModelNodeMetaObject *m_meta;
};
+class ModelNodeMetaObject : public QDeclarativeOpenMetaObject
+{
+public:
+ ModelNodeMetaObject(QScriptEngine *seng, ModelObject *object);
+
+ bool m_enabled;
+
+protected:
+ void propertyWritten(int index);
+
+private:
+ QScriptEngine *m_seng;
+ ModelObject *m_obj;
+};
+
+
+/*
+ A ModelNode is created for each item in a NestedListModel.
+*/
struct ModelNode
{
- ModelNode();
+ ModelNode(NestedListModel *model);
~ModelNode();
QList<QVariant> values;
@@ -160,35 +255,22 @@ struct ModelNode
void clear();
- QDeclarativeListModel *model(const NestedListModel *model) {
- if (!modelCache) {
- modelCache = new QDeclarativeListModel;
- QDeclarativeEngine::setContextForObject(modelCache,QDeclarativeEngine::contextForObject(model->m_listModel));
- modelCache->m_nested->_root = this; // ListModel defaults to nestable model
- }
- return modelCache;
- }
-
- ModelObject *object(const NestedListModel *model) {
- if (!objectCache) {
- objectCache = new ModelObject();
- QHash<QString, ModelNode *>::iterator it;
- for (it = properties.begin(); it != properties.end(); ++it) {
- objectCache->setValue(it.key().toUtf8(), model->valueForNode(*it));
- }
- }
- return objectCache;
- }
+ QDeclarativeListModel *model(const NestedListModel *model);
+ ModelObject *object(const NestedListModel *model);
-
- void setObjectValue(const QScriptValue& valuemap);
+ void setObjectValue(const QScriptValue& valuemap, bool writeToCache = true);
void setListValue(const QScriptValue& valuelist);
void setProperty(const QString& prop, const QVariant& val);
+ void changedProperty(const QString &name) const;
+ void updateListIndexes();
static void dump(ModelNode *node, int ind);
QDeclarativeListModel *modelCache;
ModelObject *objectCache;
bool isArray;
+
+ NestedListModel *m_model;
+ int listIndex; // only used for top-level nodes within a list
};
diff --git a/src/declarative/util/qdeclarativelistmodelworkeragent.cpp b/src/declarative/util/qdeclarativelistmodelworkeragent.cpp
index d9df16989f..6804d4af6e 100644
--- a/src/declarative/util/qdeclarativelistmodelworkeragent.cpp
+++ b/src/declarative/util/qdeclarativelistmodelworkeragent.cpp
@@ -83,11 +83,11 @@ void QDeclarativeListModelWorkerAgent::Data::changedChange(int index, int count)
}
QDeclarativeListModelWorkerAgent::QDeclarativeListModelWorkerAgent(QDeclarativeListModel *model)
-: m_engine(0), m_ref(1), m_orig(model), m_copy(new QDeclarativeListModel(true, this))
+ : m_engine(0),
+ m_ref(1),
+ m_orig(model),
+ m_copy(new QDeclarativeListModel(model, this))
{
- m_copy->m_flat->m_roles = m_orig->m_flat->m_roles;
- m_copy->m_flat->m_strings = m_orig->m_flat->m_strings;
- m_copy->m_flat->m_values = m_orig->m_flat->m_values;
}
QDeclarativeListModelWorkerAgent::~QDeclarativeListModelWorkerAgent()
@@ -194,6 +194,11 @@ void QDeclarativeListModelWorkerAgent::sync()
mutex.unlock();
}
+void QDeclarativeListModelWorkerAgent::changedData(int index, int count)
+{
+ data.changedChange(index, count);
+}
+
bool QDeclarativeListModelWorkerAgent::event(QEvent *e)
{
if (e->type() == QEvent::User) {
@@ -216,6 +221,24 @@ bool QDeclarativeListModelWorkerAgent::event(QEvent *e)
orig->m_strings = copy->m_strings;
orig->m_values = copy->m_values;
+ // update the orig->m_nodeData list
+ for (int ii = 0; ii < changes.count(); ++ii) {
+ const Change &change = changes.at(ii);
+ switch (change.type) {
+ case Change::Inserted:
+ orig->insertedNode(change.index);
+ break;
+ case Change::Removed:
+ orig->removedNode(change.index);
+ break;
+ case Change::Moved:
+ orig->moveNodes(change.index, change.to, change.count);
+ break;
+ case Change::Changed:
+ break;
+ }
+ }
+
syncDone.wakeAll();
locker.unlock();
@@ -232,7 +255,7 @@ bool QDeclarativeListModelWorkerAgent::event(QEvent *e)
emit m_orig->itemsMoved(change.index, change.to, change.count);
break;
case Change::Changed:
- emit m_orig->itemsChanged(change.index, change.to, orig->m_roles.keys());
+ emit m_orig->itemsChanged(change.index, change.count, orig->m_roles.keys());
break;
}
}
diff --git a/src/declarative/util/qdeclarativelistmodelworkeragent_p.h b/src/declarative/util/qdeclarativelistmodelworkeragent_p.h
index 01da3741f5..10c3bca549 100644
--- a/src/declarative/util/qdeclarativelistmodelworkeragent_p.h
+++ b/src/declarative/util/qdeclarativelistmodelworkeragent_p.h
@@ -67,6 +67,7 @@ QT_BEGIN_NAMESPACE
QT_MODULE(Declarative)
class QDeclarativeListModel;
+class FlatListScriptClass;
class QDeclarativeListModelWorkerAgent : public QObject
{
@@ -115,6 +116,7 @@ protected:
private:
friend class QDeclarativeWorkerScriptEnginePrivate;
+ friend class FlatListScriptClass;
QScriptEngine *m_engine;
struct Change {
@@ -141,6 +143,8 @@ private:
QDeclarativeListModel *list;
};
+ void changedData(int index, int count);
+
QAtomicInt m_ref;
QDeclarativeListModel *m_orig;
QDeclarativeListModel *m_copy;
diff --git a/src/declarative/util/qdeclarativepixmapcache_p.h b/src/declarative/util/qdeclarativepixmapcache_p.h
index b4d88bdaf4..2e83cc423f 100644
--- a/src/declarative/util/qdeclarativepixmapcache_p.h
+++ b/src/declarative/util/qdeclarativepixmapcache_p.h
@@ -98,7 +98,7 @@ public:
bool connectDownloadProgress(QObject *, int);
private:
- Q_DISABLE_COPY(QDeclarativePixmap);
+ Q_DISABLE_COPY(QDeclarativePixmap)
QDeclarativePixmapData *d;
};
diff --git a/src/declarative/util/qdeclarativepropertychanges.cpp b/src/declarative/util/qdeclarativepropertychanges.cpp
index e897458744..8d01b80af2 100644
--- a/src/declarative/util/qdeclarativepropertychanges.cpp
+++ b/src/declarative/util/qdeclarativepropertychanges.cpp
@@ -52,6 +52,7 @@
#include <qdeclarativeguard_p.h>
#include <qdeclarativeproperty_p.h>
#include <qdeclarativecontext_p.h>
+#include <qdeclarativestate_p_p.h>
#include <QtCore/qdebug.h>
@@ -200,14 +201,14 @@ public:
};
-class QDeclarativePropertyChangesPrivate : public QObjectPrivate
+class QDeclarativePropertyChangesPrivate : public QDeclarativeStateOperationPrivate
{
Q_DECLARE_PUBLIC(QDeclarativePropertyChanges)
public:
- QDeclarativePropertyChangesPrivate() : object(0), decoded(true), restore(true),
+ QDeclarativePropertyChangesPrivate() : decoded(true), restore(true),
isExplicit(false) {}
- QObject *object;
+ QDeclarativeGuard<QObject> object;
QByteArray data;
bool decoded : 1;
@@ -497,4 +498,272 @@ void QDeclarativePropertyChanges::setIsExplicit(bool e)
d->isExplicit = e;
}
+bool QDeclarativePropertyChanges::containsValue(const QByteArray &name) const
+{
+ Q_D(const QDeclarativePropertyChanges);
+ typedef QPair<QByteArray, QVariant> PropertyEntry;
+
+ QListIterator<PropertyEntry> propertyIterator(d->properties);
+ while (propertyIterator.hasNext()) {
+ const PropertyEntry &entry = propertyIterator.next();
+ if (entry.first == name) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+bool QDeclarativePropertyChanges::containsExpression(const QByteArray &name) const
+{
+ Q_D(const QDeclarativePropertyChanges);
+ typedef QPair<QByteArray, QDeclarativeExpression *> ExpressionEntry;
+
+ QListIterator<ExpressionEntry> expressionIterator(d->expressions);
+ while (expressionIterator.hasNext()) {
+ const ExpressionEntry &entry = expressionIterator.next();
+ if (entry.first == name) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+bool QDeclarativePropertyChanges::containsProperty(const QByteArray &name) const
+{
+ return containsValue(name) || containsExpression(name);
+}
+
+void QDeclarativePropertyChanges::changeValue(const QByteArray &name, const QVariant &value)
+{
+ Q_D(QDeclarativePropertyChanges);
+ typedef QPair<QByteArray, QVariant> PropertyEntry;
+ typedef QPair<QByteArray, QDeclarativeExpression *> ExpressionEntry;
+
+ QMutableListIterator<ExpressionEntry> expressionIterator(d->expressions);
+ while (expressionIterator.hasNext()) {
+ const ExpressionEntry &entry = expressionIterator.next();
+ if (entry.first == name) {
+ expressionIterator.remove();
+ if (state() && state()->isStateActive()) {
+ QDeclarativeAbstractBinding *oldBinding = QDeclarativePropertyPrivate::binding(d->property(name));
+ if (oldBinding) {
+ QDeclarativePropertyPrivate::setBinding(d->property(name), 0);
+ oldBinding->destroy();
+ }
+ d->property(name).write(value);
+ }
+
+ d->properties.append(PropertyEntry(name, value));
+ return;
+ }
+ }
+
+ QMutableListIterator<PropertyEntry> propertyIterator(d->properties);
+ while (propertyIterator.hasNext()) {
+ PropertyEntry &entry = propertyIterator.next();
+ if (entry.first == name) {
+ entry.second = value;
+ if (state() && state()->isStateActive())
+ d->property(name).write(value);
+ return;
+ }
+ }
+
+ QDeclarativeAction action;
+ action.restore = restoreEntryValues();
+ action.property = d->property(name);
+ action.fromValue = action.property.read();
+ action.specifiedObject = object();
+ action.specifiedProperty = QString::fromUtf8(name);
+ action.toValue = value;
+
+ propertyIterator.insert(PropertyEntry(name, value));
+ if (state() && state()->isStateActive()) {
+ state()->addEntryToRevertList(action);
+ QDeclarativeAbstractBinding *oldBinding = QDeclarativePropertyPrivate::binding(action.property);
+ if (oldBinding)
+ oldBinding->setEnabled(false, QDeclarativePropertyPrivate::DontRemoveBinding | QDeclarativePropertyPrivate::BypassInterceptor);
+ d->property(name).write(value);
+ }
+}
+
+void QDeclarativePropertyChanges::changeExpression(const QByteArray &name, const QString &expression)
+{
+ Q_D(QDeclarativePropertyChanges);
+ typedef QPair<QByteArray, QVariant> PropertyEntry;
+ typedef QPair<QByteArray, QDeclarativeExpression *> ExpressionEntry;
+
+ bool hadValue = false;
+
+ QMutableListIterator<PropertyEntry> propertyIterator(d->properties);
+ while (propertyIterator.hasNext()) {
+ PropertyEntry &entry = propertyIterator.next();
+ if (entry.first == name) {
+ propertyIterator.remove();
+ hadValue = true;
+ break;
+ }
+ }
+
+ QMutableListIterator<ExpressionEntry> expressionIterator(d->expressions);
+ while (expressionIterator.hasNext()) {
+ const ExpressionEntry &entry = expressionIterator.next();
+ if (entry.first == name) {
+ entry.second->setExpression(expression);
+ if (state() && state()->isStateActive()) {
+ QDeclarativeAbstractBinding *oldBinding = QDeclarativePropertyPrivate::binding(d->property(name));
+ if (oldBinding) {
+ QDeclarativePropertyPrivate::setBinding(d->property(name), 0);
+ oldBinding->destroy();
+ }
+
+ QDeclarativeBinding *newBinding = new QDeclarativeBinding(expression, object(), qmlContext(this));
+ newBinding->setTarget(d->property(name));
+ QDeclarativePropertyPrivate::setBinding(d->property(name), newBinding, QDeclarativePropertyPrivate::DontRemoveBinding | QDeclarativePropertyPrivate::BypassInterceptor);
+ }
+ return;
+ }
+ }
+
+ QDeclarativeExpression *newExpression = new QDeclarativeExpression(qmlContext(this), d->object, expression);
+ expressionIterator.insert(ExpressionEntry(name, newExpression));
+
+ if (state() && state()->isStateActive()) {
+ if (hadValue) {
+ QDeclarativeAbstractBinding *oldBinding = QDeclarativePropertyPrivate::binding(d->property(name));
+ if (oldBinding) {
+ oldBinding->setEnabled(false, QDeclarativePropertyPrivate::DontRemoveBinding | QDeclarativePropertyPrivate::BypassInterceptor);
+ state()->changeBindingInRevertList(object(), name, oldBinding);
+ }
+
+ QDeclarativeBinding *newBinding = new QDeclarativeBinding(expression, object(), qmlContext(this));
+ newBinding->setTarget(d->property(name));
+ QDeclarativePropertyPrivate::setBinding(d->property(name), newBinding, QDeclarativePropertyPrivate::DontRemoveBinding | QDeclarativePropertyPrivate::BypassInterceptor);
+ } else {
+ QDeclarativeAction action;
+ action.restore = restoreEntryValues();
+ action.property = d->property(name);
+ action.fromValue = action.property.read();
+ action.specifiedObject = object();
+ action.specifiedProperty = QString::fromUtf8(name);
+
+
+ if (d->isExplicit) {
+ action.toValue = newExpression->evaluate();
+ } else {
+ QDeclarativeBinding *newBinding = new QDeclarativeBinding(newExpression->expression(), object(), qmlContext(this));
+ newBinding->setTarget(d->property(name));
+ action.toBinding = newBinding;
+ action.deletableToBinding = true;
+
+ state()->addEntryToRevertList(action);
+ QDeclarativeAbstractBinding *oldBinding = QDeclarativePropertyPrivate::binding(action.property);
+ if (oldBinding)
+ oldBinding->setEnabled(false, QDeclarativePropertyPrivate::DontRemoveBinding | QDeclarativePropertyPrivate::BypassInterceptor);
+
+ QDeclarativePropertyPrivate::setBinding(action.property, newBinding, QDeclarativePropertyPrivate::DontRemoveBinding | QDeclarativePropertyPrivate::BypassInterceptor);
+ }
+ }
+ }
+ // what about the signal handler?
+}
+
+QVariant QDeclarativePropertyChanges::property(const QByteArray &name) const
+{
+ Q_D(const QDeclarativePropertyChanges);
+ typedef QPair<QByteArray, QVariant> PropertyEntry;
+ typedef QPair<QByteArray, QDeclarativeExpression *> ExpressionEntry;
+
+ QListIterator<PropertyEntry> propertyIterator(d->properties);
+ while (propertyIterator.hasNext()) {
+ const PropertyEntry &entry = propertyIterator.next();
+ if (entry.first == name) {
+ return entry.second;
+ }
+ }
+
+ QListIterator<ExpressionEntry> expressionIterator(d->expressions);
+ while (expressionIterator.hasNext()) {
+ const ExpressionEntry &entry = expressionIterator.next();
+ if (entry.first == name) {
+ return QVariant(entry.second->expression());
+ }
+ }
+
+ return QVariant();
+}
+
+void QDeclarativePropertyChanges::removeProperty(const QByteArray &name)
+{
+ Q_D(QDeclarativePropertyChanges);
+ typedef QPair<QByteArray, QVariant> PropertyEntry;
+ typedef QPair<QByteArray, QDeclarativeExpression *> ExpressionEntry;
+
+ QMutableListIterator<ExpressionEntry> expressionIterator(d->expressions);
+ while (expressionIterator.hasNext()) {
+ const ExpressionEntry &entry = expressionIterator.next();
+ if (entry.first == name) {
+ expressionIterator.remove();
+ state()->removeEntryFromRevertList(object(), name);
+ return;
+ }
+ }
+
+ QMutableListIterator<PropertyEntry> propertyIterator(d->properties);
+ while (propertyIterator.hasNext()) {
+ const PropertyEntry &entry = propertyIterator.next();
+ if (entry.first == name) {
+ propertyIterator.remove();
+ state()->removeEntryFromRevertList(object(), name);
+ return;
+ }
+ }
+}
+
+QVariant QDeclarativePropertyChanges::value(const QByteArray &name) const
+{
+ Q_D(const QDeclarativePropertyChanges);
+ typedef QPair<QByteArray, QVariant> PropertyEntry;
+
+ QListIterator<PropertyEntry> propertyIterator(d->properties);
+ while (propertyIterator.hasNext()) {
+ const PropertyEntry &entry = propertyIterator.next();
+ if (entry.first == name) {
+ return entry.second;
+ }
+ }
+
+ return QVariant();
+}
+
+QString QDeclarativePropertyChanges::expression(const QByteArray &name) const
+{
+ Q_D(const QDeclarativePropertyChanges);
+ typedef QPair<QByteArray, QDeclarativeExpression *> ExpressionEntry;
+
+ QListIterator<ExpressionEntry> expressionIterator(d->expressions);
+ while (expressionIterator.hasNext()) {
+ const ExpressionEntry &entry = expressionIterator.next();
+ if (entry.first == name) {
+ return entry.second->expression();
+ }
+ }
+
+ return QString();
+}
+
+void QDeclarativePropertyChanges::detachFromState()
+{
+ if (state())
+ state()->removeAllEntriesFromRevertList(object());
+}
+
+void QDeclarativePropertyChanges::attachToState()
+{
+ if (state())
+ state()->addEntriesToRevertList(actions());
+}
+
QT_END_NAMESPACE
diff --git a/src/declarative/util/qdeclarativepropertychanges_p.h b/src/declarative/util/qdeclarativepropertychanges_p.h
index 8578086ef2..199928fef3 100644
--- a/src/declarative/util/qdeclarativepropertychanges_p.h
+++ b/src/declarative/util/qdeclarativepropertychanges_p.h
@@ -52,7 +52,7 @@ QT_BEGIN_NAMESPACE
QT_MODULE(Declarative)
class QDeclarativePropertyChangesPrivate;
-class Q_AUTOTEST_EXPORT QDeclarativePropertyChanges : public QDeclarativeStateOperation
+class Q_DECLARATIVE_EXPORT QDeclarativePropertyChanges : public QDeclarativeStateOperation
{
Q_OBJECT
Q_DECLARE_PRIVATE(QDeclarativePropertyChanges)
@@ -74,6 +74,20 @@ public:
void setIsExplicit(bool);
virtual ActionList actions();
+
+ bool containsProperty(const QByteArray &name) const;
+ bool containsValue(const QByteArray &name) const;
+ bool containsExpression(const QByteArray &name) const;
+ void changeValue(const QByteArray &name, const QVariant &value);
+ void changeExpression(const QByteArray &name, const QString &expression);
+ void removeProperty(const QByteArray &name);
+ QVariant value(const QByteArray &name) const;
+ QString expression(const QByteArray &name) const;
+
+ void detachFromState();
+ void attachToState();
+
+ QVariant property(const QByteArray &name) const;
};
class QDeclarativePropertyChangesParser : public QDeclarativeCustomParser
diff --git a/src/declarative/util/qdeclarativestate.cpp b/src/declarative/util/qdeclarativestate.cpp
index 1ed7923447..0f5413ec46 100644
--- a/src/declarative/util/qdeclarativestate.cpp
+++ b/src/declarative/util/qdeclarativestate.cpp
@@ -304,7 +304,7 @@ void QDeclarativeStatePrivate::complete()
for (int ii = 0; ii < reverting.count(); ++ii) {
for (int jj = 0; jj < revertList.count(); ++jj) {
- if (revertList.at(jj).property == reverting.at(ii)) {
+ if (revertList.at(jj).property() == reverting.at(ii)) {
revertList.removeAt(jj);
break;
}
@@ -370,6 +370,192 @@ void QDeclarativeAction::deleteFromBinding()
}
}
+bool QDeclarativeState::containsPropertyInRevertList(QObject *target, const QByteArray &name) const
+{
+ Q_D(const QDeclarativeState);
+
+ if (isStateActive()) {
+ QListIterator<QDeclarativeSimpleAction> revertListIterator(d->revertList);
+
+ while (revertListIterator.hasNext()) {
+ const QDeclarativeSimpleAction &simpleAction = revertListIterator.next();
+ if (simpleAction.specifiedObject() == target && simpleAction.specifiedProperty().toUtf8() == name)
+ return true;
+ }
+ }
+
+ return false;
+}
+
+bool QDeclarativeState::changeValueInRevertList(QObject *target, const QByteArray &name, const QVariant &revertValue)
+{
+ Q_D(QDeclarativeState);
+
+ if (isStateActive()) {
+ QMutableListIterator<QDeclarativeSimpleAction> revertListIterator(d->revertList);
+
+ while (revertListIterator.hasNext()) {
+ QDeclarativeSimpleAction &simpleAction = revertListIterator.next();
+ if (simpleAction.specifiedObject() == target && simpleAction.specifiedProperty().toUtf8() == name) {
+ simpleAction.setValue(revertValue);
+ return true;
+ }
+ }
+ }
+
+ return false;
+}
+
+bool QDeclarativeState::changeBindingInRevertList(QObject *target, const QByteArray &name, QDeclarativeAbstractBinding *binding)
+{
+ Q_D(QDeclarativeState);
+
+ if (isStateActive()) {
+ QMutableListIterator<QDeclarativeSimpleAction> revertListIterator(d->revertList);
+
+ while (revertListIterator.hasNext()) {
+ QDeclarativeSimpleAction &simpleAction = revertListIterator.next();
+ if (simpleAction.specifiedObject() == target && simpleAction.specifiedProperty().toUtf8() == name) {
+ if (simpleAction.binding())
+ simpleAction.binding()->destroy();
+
+ simpleAction.setBinding(binding);
+ return true;
+ }
+ }
+ }
+
+ return false;
+}
+
+bool QDeclarativeState::removeEntryFromRevertList(QObject *target, const QByteArray &name)
+{
+ Q_D(QDeclarativeState);
+
+ if (isStateActive()) {
+ QMutableListIterator<QDeclarativeSimpleAction> revertListIterator(d->revertList);
+
+ while (revertListIterator.hasNext()) {
+ QDeclarativeSimpleAction &simpleAction = revertListIterator.next();
+ if (simpleAction.property().object() == target && simpleAction.property().name().toUtf8() == name) {
+ QDeclarativeAbstractBinding *oldBinding = QDeclarativePropertyPrivate::binding(simpleAction.property());
+ if (oldBinding) {
+ QDeclarativePropertyPrivate::setBinding(simpleAction.property(), 0);
+ oldBinding->destroy();
+ }
+
+ simpleAction.property().write(simpleAction.value());
+ if (simpleAction.binding())
+ QDeclarativePropertyPrivate::setBinding(simpleAction.property(), simpleAction.binding());
+
+ revertListIterator.remove();
+ return true;
+ }
+ }
+ }
+
+ return false;
+}
+
+void QDeclarativeState::addEntryToRevertList(const QDeclarativeAction &action)
+{
+ Q_D(QDeclarativeState);
+
+ QDeclarativeSimpleAction simpleAction(action);
+
+ d->revertList.append(simpleAction);
+}
+
+void QDeclarativeState::removeAllEntriesFromRevertList(QObject *target)
+{
+ Q_D(QDeclarativeState);
+
+ if (isStateActive()) {
+ QMutableListIterator<QDeclarativeSimpleAction> revertListIterator(d->revertList);
+
+ while (revertListIterator.hasNext()) {
+ QDeclarativeSimpleAction &simpleAction = revertListIterator.next();
+ if (simpleAction.property().object() == target) {
+ QDeclarativeAbstractBinding *oldBinding = QDeclarativePropertyPrivate::binding(simpleAction.property());
+ if (oldBinding) {
+ QDeclarativePropertyPrivate::setBinding(simpleAction.property(), 0);
+ oldBinding->destroy();
+ }
+
+ simpleAction.property().write(simpleAction.value());
+ if (simpleAction.binding())
+ QDeclarativePropertyPrivate::setBinding(simpleAction.property(), simpleAction.binding());
+
+ revertListIterator.remove();
+ }
+ }
+ }
+}
+
+void QDeclarativeState::addEntriesToRevertList(const QList<QDeclarativeAction> &actionList)
+{
+ Q_D(QDeclarativeState);
+ if (isStateActive()) {
+ QList<QDeclarativeSimpleAction> simpleActionList;
+
+ QListIterator<QDeclarativeAction> actionListIterator(actionList);
+ while(actionListIterator.hasNext()) {
+ const QDeclarativeAction &action = actionListIterator.next();
+ QDeclarativeSimpleAction simpleAction(action);
+ action.property.write(action.toValue);
+ if (action.toBinding) {
+ QDeclarativeAbstractBinding *oldBinding = QDeclarativePropertyPrivate::binding(simpleAction.property());
+ if (oldBinding)
+ QDeclarativePropertyPrivate::setBinding(simpleAction.property(), 0);
+ QDeclarativePropertyPrivate::setBinding(simpleAction.property(), action.toBinding, QDeclarativePropertyPrivate::DontRemoveBinding);
+ }
+
+ simpleActionList.append(simpleAction);
+ }
+
+ d->revertList.append(simpleActionList);
+ }
+}
+
+QVariant QDeclarativeState::valueInRevertList(QObject *target, const QByteArray &name) const
+{
+ Q_D(const QDeclarativeState);
+
+ if (isStateActive()) {
+ QListIterator<QDeclarativeSimpleAction> revertListIterator(d->revertList);
+
+ while (revertListIterator.hasNext()) {
+ const QDeclarativeSimpleAction &simpleAction = revertListIterator.next();
+ if (simpleAction.specifiedObject() == target && simpleAction.specifiedProperty().toUtf8() == name)
+ return simpleAction.value();
+ }
+ }
+
+ return QVariant();
+}
+
+QDeclarativeAbstractBinding *QDeclarativeState::bindingInRevertList(QObject *target, const QByteArray &name) const
+{
+ Q_D(const QDeclarativeState);
+
+ if (isStateActive()) {
+ QListIterator<QDeclarativeSimpleAction> revertListIterator(d->revertList);
+
+ while (revertListIterator.hasNext()) {
+ const QDeclarativeSimpleAction &simpleAction = revertListIterator.next();
+ if (simpleAction.specifiedObject() == target && simpleAction.specifiedProperty().toUtf8() == name)
+ return simpleAction.binding();
+ }
+ }
+
+ return 0;
+}
+
+bool QDeclarativeState::isStateActive() const
+{
+ return stateGroup() && stateGroup()->state() == name();
+}
+
void QDeclarativeState::apply(QDeclarativeStateGroup *group, QDeclarativeTransition *trans, QDeclarativeState *revert)
{
Q_D(QDeclarativeState);
@@ -403,13 +589,13 @@ void QDeclarativeState::apply(QDeclarativeStateGroup *group, QDeclarativeTransit
continue;
bool found = false;
for (int jj = 0; jj < d->revertList.count(); ++jj) {
- QDeclarativeActionEvent *event = d->revertList.at(jj).event;
+ QDeclarativeActionEvent *event = d->revertList.at(jj).event();
if (event && event->typeName() == action.event->typeName()) {
if (action.event->override(event)) {
found = true;
- if (action.event != d->revertList.at(jj).event && action.event->needsCopy()) {
- action.event->copyOriginals(d->revertList.at(jj).event);
+ if (action.event != d->revertList.at(jj).event() && action.event->needsCopy()) {
+ action.event->copyOriginals(d->revertList.at(jj).event());
QDeclarativeSimpleAction r(action);
additionalReverts << r;
@@ -434,9 +620,9 @@ void QDeclarativeState::apply(QDeclarativeStateGroup *group, QDeclarativeTransit
action.fromBinding = QDeclarativePropertyPrivate::binding(action.property);
for (int jj = 0; jj < d->revertList.count(); ++jj) {
- if (d->revertList.at(jj).property == action.property) {
+ if (d->revertList.at(jj).property() == action.property) {
found = true;
- if (d->revertList.at(jj).binding != action.fromBinding) {
+ if (d->revertList.at(jj).binding() != action.fromBinding) {
action.deleteFromBinding();
}
break;
@@ -445,7 +631,7 @@ void QDeclarativeState::apply(QDeclarativeStateGroup *group, QDeclarativeTransit
if (!found) {
if (!action.restore) {
- action.deleteFromBinding();
+ action.deleteFromBinding();;
} else {
// Only need to revert the applyList action if the previous
// state doesn't have a higher priority revert already
@@ -460,8 +646,8 @@ void QDeclarativeState::apply(QDeclarativeStateGroup *group, QDeclarativeTransit
// into this state need to be translated into apply actions
for (int ii = 0; ii < d->revertList.count(); ++ii) {
bool found = false;
- if (d->revertList.at(ii).event) {
- QDeclarativeActionEvent *event = d->revertList.at(ii).event;
+ if (d->revertList.at(ii).event()) {
+ QDeclarativeActionEvent *event = d->revertList.at(ii).event();
if (!event->isReversable())
continue;
for (int jj = 0; !found && jj < applyList.count(); ++jj) {
@@ -474,31 +660,31 @@ void QDeclarativeState::apply(QDeclarativeStateGroup *group, QDeclarativeTransit
} else {
for (int jj = 0; !found && jj < applyList.count(); ++jj) {
const QDeclarativeAction &action = applyList.at(jj);
- if (action.property == d->revertList.at(ii).property)
+ if (action.property == d->revertList.at(ii).property())
found = true;
}
}
if (!found) {
- QVariant cur = d->revertList.at(ii).property.read();
+ QVariant cur = d->revertList.at(ii).property().read();
QDeclarativeAbstractBinding *delBinding =
- QDeclarativePropertyPrivate::setBinding(d->revertList.at(ii).property, 0);
+ QDeclarativePropertyPrivate::setBinding(d->revertList.at(ii).property(), 0);
if (delBinding)
delBinding->destroy();
QDeclarativeAction a;
- a.property = d->revertList.at(ii).property;
+ a.property = d->revertList.at(ii).property();
a.fromValue = cur;
- a.toValue = d->revertList.at(ii).value;
- a.toBinding = d->revertList.at(ii).binding;
- a.specifiedObject = d->revertList.at(ii).specifiedObject;
- a.specifiedProperty = d->revertList.at(ii).specifiedProperty;
- a.event = d->revertList.at(ii).event;
- a.reverseEvent = d->revertList.at(ii).reverseEvent;
+ a.toValue = d->revertList.at(ii).value();
+ a.toBinding = d->revertList.at(ii).binding();
+ a.specifiedObject = d->revertList.at(ii).specifiedObject();
+ a.specifiedProperty = d->revertList.at(ii).specifiedProperty();
+ a.event = d->revertList.at(ii).event();
+ a.reverseEvent = d->revertList.at(ii).reverseEvent();
if (a.event && a.event->isRewindable())
a.event->saveCurrentValues();
applyList << a;
// Store these special reverts in the reverting list
- d->reverting << d->revertList.at(ii).property;
+ d->reverting << d->revertList.at(ii).property();
}
}
// All the local reverts now become part of the ongoing revertList
@@ -526,4 +712,16 @@ QDeclarativeStateOperation::ActionList QDeclarativeStateOperation::actions()
return ActionList();
}
+QDeclarativeState *QDeclarativeStateOperation::state() const
+{
+ Q_D(const QDeclarativeStateOperation);
+ return d->m_state;
+}
+
+void QDeclarativeStateOperation::setState(QDeclarativeState *state)
+{
+ Q_D(QDeclarativeStateOperation);
+ d->m_state = state;
+}
+
QT_END_NAMESPACE
diff --git a/src/declarative/util/qdeclarativestate_p.h b/src/declarative/util/qdeclarativestate_p.h
index 2e2ce7b0c9..a0ab11b867 100644
--- a/src/declarative/util/qdeclarativestate_p.h
+++ b/src/declarative/util/qdeclarativestate_p.h
@@ -111,6 +111,8 @@ public:
//### rename to QDeclarativeStateChange?
class QDeclarativeStateGroup;
+class QDeclarativeState;
+class QDeclarativeStateOperationPrivate;
class Q_DECLARATIVE_EXPORT QDeclarativeStateOperation : public QObject
{
Q_OBJECT
@@ -121,8 +123,15 @@ public:
virtual ActionList actions();
+ QDeclarativeState *state() const;
+ void setState(QDeclarativeState *state);
+
protected:
QDeclarativeStateOperation(QObjectPrivate &dd, QObject *parent = 0);
+
+private:
+ Q_DECLARE_PRIVATE(QDeclarativeStateOperation)
+ Q_DISABLE_COPY(QDeclarativeStateOperation)
};
typedef QDeclarativeStateOperation::ActionList QDeclarativeStateActions;
@@ -169,6 +178,18 @@ public:
QDeclarativeStateGroup *stateGroup() const;
void setStateGroup(QDeclarativeStateGroup *);
+ bool containsPropertyInRevertList(QObject *target, const QByteArray &name) const;
+ bool changeValueInRevertList(QObject *target, const QByteArray &name, const QVariant &revertValue);
+ bool changeBindingInRevertList(QObject *target, const QByteArray &name, QDeclarativeAbstractBinding *binding);
+ bool removeEntryFromRevertList(QObject *target, const QByteArray &name);
+ void addEntryToRevertList(const QDeclarativeAction &action);
+ void removeAllEntriesFromRevertList(QObject *target);
+ void addEntriesToRevertList(const QList<QDeclarativeAction> &actions);
+ QVariant valueInRevertList(QObject *target, const QByteArray &name) const;
+ QDeclarativeAbstractBinding *bindingInRevertList(QObject *target, const QByteArray &name) const;
+
+ bool isStateActive() const;
+
Q_SIGNALS:
void completed();
diff --git a/src/declarative/util/qdeclarativestate_p_p.h b/src/declarative/util/qdeclarativestate_p_p.h
index 2ef9bb084d..4fd8f217c9 100644
--- a/src/declarative/util/qdeclarativestate_p_p.h
+++ b/src/declarative/util/qdeclarativestate_p_p.h
@@ -61,6 +61,8 @@
#include <private/qdeclarativeproperty_p.h>
#include <private/qdeclarativeguard_p.h>
+#include <private/qdeclarativebinding_p.h>
+
#include <private/qobject_p.h>
QT_BEGIN_NAMESPACE
@@ -69,30 +71,123 @@ class QDeclarativeSimpleAction
{
public:
enum State { StartState, EndState };
- QDeclarativeSimpleAction(const QDeclarativeAction &a, State state = StartState)
+ QDeclarativeSimpleAction(const QDeclarativeAction &a, State state = StartState)
{
- property = a.property;
- specifiedObject = a.specifiedObject;
- specifiedProperty = a.specifiedProperty;
- event = a.event;
+ m_property = a.property;
+ m_specifiedObject = a.specifiedObject;
+ m_specifiedProperty = a.specifiedProperty;
+ m_event = a.event;
if (state == StartState) {
- value = a.fromValue;
- binding = QDeclarativePropertyPrivate::binding(property);
- reverseEvent = true;
+ m_value = a.fromValue;
+ if (QDeclarativePropertyPrivate::binding(m_property)) {
+ m_binding = QDeclarativeAbstractBinding::getPointer(QDeclarativePropertyPrivate::binding(m_property));
+ }
+ m_reverseEvent = true;
} else {
- value = a.toValue;
- binding = a.toBinding;
- reverseEvent = false;
+ m_value = a.toValue;
+ m_binding = QDeclarativeAbstractBinding::getPointer(a.toBinding);
+ m_reverseEvent = false;
}
}
- QDeclarativeProperty property;
- QVariant value;
- QDeclarativeAbstractBinding *binding;
- QObject *specifiedObject;
- QString specifiedProperty;
- QDeclarativeActionEvent *event;
- bool reverseEvent;
+ ~QDeclarativeSimpleAction()
+ {
+ }
+
+ QDeclarativeSimpleAction(const QDeclarativeSimpleAction &other)
+ : m_property(other.m_property),
+ m_value(other.m_value),
+ m_binding(QDeclarativeAbstractBinding::getPointer(other.binding())),
+ m_specifiedObject(other.m_specifiedObject),
+ m_specifiedProperty(other.m_specifiedProperty),
+ m_event(other.m_event),
+ m_reverseEvent(other.m_reverseEvent)
+ {
+ }
+
+ QDeclarativeSimpleAction &operator =(const QDeclarativeSimpleAction &other)
+ {
+ m_property = other.m_property;
+ m_value = other.m_value;
+ m_binding = QDeclarativeAbstractBinding::getPointer(other.binding());
+ m_specifiedObject = other.m_specifiedObject;
+ m_specifiedProperty = other.m_specifiedProperty;
+ m_event = other.m_event;
+ m_reverseEvent = other.m_reverseEvent;
+
+ return *this;
+ }
+
+ void setProperty(const QDeclarativeProperty &property)
+ {
+ m_property = property;
+ }
+
+ const QDeclarativeProperty &property() const
+ {
+ return m_property;
+ }
+
+ void setValue(const QVariant &value)
+ {
+ m_value = value;
+ }
+
+ const QVariant &value() const
+ {
+ return m_value;
+ }
+
+ void setBinding(QDeclarativeAbstractBinding *binding)
+ {
+ m_binding = QDeclarativeAbstractBinding::getPointer(binding);
+ }
+
+ QDeclarativeAbstractBinding *binding() const
+ {
+ return m_binding.data();
+ }
+
+ QObject *specifiedObject() const
+ {
+ return m_specifiedObject;
+ }
+
+ const QString &specifiedProperty() const
+ {
+ return m_specifiedProperty;
+ }
+
+ QDeclarativeActionEvent *event() const
+ {
+ return m_event;
+ }
+
+ bool reverseEvent() const
+ {
+ return m_reverseEvent;
+ }
+
+private:
+ QDeclarativeProperty m_property;
+ QVariant m_value;
+ QDeclarativeAbstractBinding::Pointer m_binding;
+ QObject *m_specifiedObject;
+ QString m_specifiedProperty;
+ QDeclarativeActionEvent *m_event;
+ bool m_reverseEvent;
+};
+
+class QDeclarativeStateOperationPrivate : public QObjectPrivate
+{
+ Q_DECLARE_PUBLIC(QDeclarativeStateOperation)
+
+public:
+
+ QDeclarativeStateOperationPrivate()
+ : m_state(0) {}
+
+ QDeclarativeState *m_state;
};
class QDeclarativeStatePrivate : public QObjectPrivate
@@ -122,10 +217,14 @@ public:
static void operations_append(QDeclarativeListProperty<QDeclarativeStateOperation> *prop, QDeclarativeStateOperation *op) {
QList<OperationGuard> *list = static_cast<QList<OperationGuard> *>(prop->data);
+ op->setState(qobject_cast<QDeclarativeState*>(prop->object));
list->append(OperationGuard(op, list));
}
static void operations_clear(QDeclarativeListProperty<QDeclarativeStateOperation> *prop) {
QList<OperationGuard> *list = static_cast<QList<OperationGuard> *>(prop->data);
+ QMutableListIterator<OperationGuard> listIterator(*list);
+ while(listIterator.hasNext())
+ listIterator.next()->setState(0);
list->clear();
}
static int operations_count(QDeclarativeListProperty<QDeclarativeStateOperation> *prop) {
diff --git a/src/declarative/util/qdeclarativestateoperations.cpp b/src/declarative/util/qdeclarativestateoperations.cpp
index 845b3da7af..8cb813c675 100644
--- a/src/declarative/util/qdeclarativestateoperations.cpp
+++ b/src/declarative/util/qdeclarativestateoperations.cpp
@@ -52,6 +52,7 @@
#include "private/qdeclarativecontext_p.h"
#include "private/qdeclarativeproperty_p.h"
#include "private/qdeclarativebinding_p.h"
+#include "private/qdeclarativestate_p_p.h"
#include <QtCore/qdebug.h>
#include <QtGui/qgraphicsitem.h>
@@ -61,7 +62,7 @@
QT_BEGIN_NAMESPACE
-class QDeclarativeParentChangePrivate : public QObjectPrivate
+class QDeclarativeParentChangePrivate : public QDeclarativeStateOperationPrivate
{
Q_DECLARE_PUBLIC(QDeclarativeParentChange)
public:
@@ -98,14 +99,15 @@ void QDeclarativeParentChangePrivate::doChange(QDeclarativeItem *targetParent, Q
qreal scale = 1;
qreal rotation = 0;
- if (ok && transform.type() != QTransform::TxRotate) {
+ bool isRotate = (transform.type() == QTransform::TxRotate) || (transform.m11() < 0);
+ if (ok && !isRotate) {
if (transform.m11() == transform.m22())
scale = transform.m11();
else {
qmlInfo(q) << QDeclarativeParentChange::tr("Unable to preserve appearance under non-uniform scale");
ok = false;
}
- } else if (ok && transform.type() == QTransform::TxRotate) {
+ } else if (ok && isRotate) {
if (transform.m11() == transform.m22())
scale = qSqrt(transform.m11()*transform.m11() + transform.m12()*transform.m12());
else {
@@ -579,7 +581,7 @@ void QDeclarativeParentChange::rewind()
d->doChange(d->rewindParent, d->rewindStackBefore);
}
-class QDeclarativeStateChangeScriptPrivate : public QObjectPrivate
+class QDeclarativeStateChangeScriptPrivate : public QDeclarativeStateOperationPrivate
{
public:
QDeclarativeStateChangeScriptPrivate() {}
@@ -964,7 +966,7 @@ void QDeclarativeAnchorSet::resetCenterIn()
}
-class QDeclarativeAnchorChangesPrivate : public QObjectPrivate
+class QDeclarativeAnchorChangesPrivate : public QDeclarativeStateOperationPrivate
{
public:
QDeclarativeAnchorChangesPrivate()
@@ -1029,6 +1031,11 @@ public:
bool applyOrigVCenter;
bool applyOrigBaseline;
+ QDeclarativeNullableValue<qreal> origWidth;
+ QDeclarativeNullableValue<qreal> origHeight;
+ qreal origX;
+ qreal origY;
+
QList<QDeclarativeAbstractBinding*> oldBindings;
QDeclarativeProperty leftProp;
@@ -1320,6 +1327,42 @@ void QDeclarativeAnchorChanges::reverse(Reason reason)
QDeclarativePropertyPrivate::setBinding(d->vCenterProp, d->origVCenterBinding);
if (d->origBaselineBinding)
QDeclarativePropertyPrivate::setBinding(d->baselineProp, d->origBaselineBinding);
+
+ //restore any absolute geometry changed by the state's anchors
+ QDeclarativeAnchors::Anchors stateVAnchors = d->anchorSet->d_func()->usedAnchors & QDeclarativeAnchors::Vertical_Mask;
+ QDeclarativeAnchors::Anchors origVAnchors = targetPrivate->anchors()->usedAnchors() & QDeclarativeAnchors::Vertical_Mask;
+ QDeclarativeAnchors::Anchors stateHAnchors = d->anchorSet->d_func()->usedAnchors & QDeclarativeAnchors::Horizontal_Mask;
+ QDeclarativeAnchors::Anchors origHAnchors = targetPrivate->anchors()->usedAnchors() & QDeclarativeAnchors::Horizontal_Mask;
+
+ bool stateSetWidth = (stateHAnchors &&
+ stateHAnchors != QDeclarativeAnchors::LeftAnchor &&
+ stateHAnchors != QDeclarativeAnchors::RightAnchor &&
+ stateHAnchors != QDeclarativeAnchors::HCenterAnchor);
+ bool origSetWidth = (origHAnchors &&
+ origHAnchors != QDeclarativeAnchors::LeftAnchor &&
+ origHAnchors != QDeclarativeAnchors::RightAnchor &&
+ origHAnchors != QDeclarativeAnchors::HCenterAnchor);
+ if (d->origWidth.isValid() && stateSetWidth && !origSetWidth)
+ d->target->setWidth(d->origWidth.value);
+
+ bool stateSetHeight = (stateVAnchors &&
+ stateVAnchors != QDeclarativeAnchors::TopAnchor &&
+ stateVAnchors != QDeclarativeAnchors::BottomAnchor &&
+ stateVAnchors != QDeclarativeAnchors::VCenterAnchor &&
+ stateVAnchors != QDeclarativeAnchors::BaselineAnchor);
+ bool origSetHeight = (origVAnchors &&
+ origVAnchors != QDeclarativeAnchors::TopAnchor &&
+ origVAnchors != QDeclarativeAnchors::BottomAnchor &&
+ origVAnchors != QDeclarativeAnchors::VCenterAnchor &&
+ origVAnchors != QDeclarativeAnchors::BaselineAnchor);
+ if (d->origHeight.isValid() && stateSetHeight && !origSetHeight)
+ d->target->setHeight(d->origHeight.value);
+
+ if (stateHAnchors && !origHAnchors)
+ d->target->setX(d->origX);
+
+ if (stateVAnchors && !origVAnchors)
+ d->target->setY(d->origY);
}
QString QDeclarativeAnchorChanges::typeName() const
@@ -1382,6 +1425,14 @@ void QDeclarativeAnchorChanges::saveOriginals()
d->origVCenterBinding = QDeclarativePropertyPrivate::binding(d->vCenterProp);
d->origBaselineBinding = QDeclarativePropertyPrivate::binding(d->baselineProp);
+ QDeclarativeItemPrivate *targetPrivate = QDeclarativeItemPrivate::get(d->target);
+ if (targetPrivate->widthValid)
+ d->origWidth = d->target->width();
+ if (targetPrivate->heightValid)
+ d->origHeight = d->target->height();
+ d->origX = d->target->x();
+ d->origY = d->target->y();
+
d->applyOrigLeft = d->applyOrigRight = d->applyOrigHCenter = d->applyOrigTop
= d->applyOrigBottom = d->applyOrigVCenter = d->applyOrigBaseline = false;
@@ -1414,6 +1465,11 @@ void QDeclarativeAnchorChanges::copyOriginals(QDeclarativeActionEvent *other)
d->origVCenterBinding = acp->origVCenterBinding;
d->origBaselineBinding = acp->origBaselineBinding;
+ d->origWidth = acp->origWidth;
+ d->origHeight = acp->origHeight;
+ d->origX = acp->origX;
+ d->origY = acp->origY;
+
d->oldBindings.clear();
d->oldBindings << acp->leftBinding << acp->rightBinding << acp->hCenterBinding
<< acp->topBinding << acp->bottomBinding << acp->baselineBinding;
diff --git a/src/declarative/util/qdeclarativetransitionmanager.cpp b/src/declarative/util/qdeclarativetransitionmanager.cpp
index d82c4bb89f..89b00440a0 100644
--- a/src/declarative/util/qdeclarativetransitionmanager.cpp
+++ b/src/declarative/util/qdeclarativetransitionmanager.cpp
@@ -86,8 +86,8 @@ void QDeclarativeTransitionManager::complete()
d->applyBindings();
for (int ii = 0; ii < d->completeList.count(); ++ii) {
- const QDeclarativeProperty &prop = d->completeList.at(ii).property;
- prop.write(d->completeList.at(ii).value);
+ const QDeclarativeProperty &prop = d->completeList.at(ii).property();
+ prop.write(d->completeList.at(ii).value());
}
d->completeList.clear();
diff --git a/src/declarative/util/qdeclarativexmllistmodel.cpp b/src/declarative/util/qdeclarativexmllistmodel.cpp
index 905ef1eb53..870a523af0 100644
--- a/src/declarative/util/qdeclarativexmllistmodel.cpp
+++ b/src/declarative/util/qdeclarativexmllistmodel.cpp
@@ -209,8 +209,9 @@ Q_SIGNALS:
protected:
void run() {
+ m_mutex.lock();
+
while (!m_quit) {
- m_mutex.lock();
if (!m_jobs.isEmpty())
m_currentJob = m_jobs.dequeue();
m_mutex.unlock();
@@ -230,12 +231,13 @@ protected:
m_mutex.lock();
if (m_currentJob.queryId != -1 && m_abortQueryId != m_currentJob.queryId)
emit queryCompleted(r);
- if (m_jobs.isEmpty())
+ if (m_jobs.isEmpty() && !m_quit)
m_condition.wait(&m_mutex);
m_currentJob.queryId = -1;
m_abortQueryId = -1;
- m_mutex.unlock();
}
+
+ m_mutex.unlock();
}
private: