diff options
Diffstat (limited to 'src/quick/items')
82 files changed, 465 insertions, 7390 deletions
diff --git a/src/quick/items/context2d/qquickcanvascontext_p.h b/src/quick/items/context2d/qquickcanvascontext_p.h index bfea0e5d5b..d15f5d945d 100644 --- a/src/quick/items/context2d/qquickcanvascontext_p.h +++ b/src/quick/items/context2d/qquickcanvascontext_p.h @@ -46,8 +46,6 @@ #include <private/qv8engine_p.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QQuickCanvasItem; @@ -83,6 +81,4 @@ Q_SIGNALS: QT_END_NAMESPACE -QT_END_HEADER - #endif //QQUICKCANVASCONTEXT_P_H diff --git a/src/quick/items/context2d/qquickcanvasitem_p.h b/src/quick/items/context2d/qquickcanvasitem_p.h index d2a907554e..c53e4f952a 100644 --- a/src/quick/items/context2d/qquickcanvasitem_p.h +++ b/src/quick/items/context2d/qquickcanvasitem_p.h @@ -47,8 +47,6 @@ #include <QtCore/QThread> #include <QtGui/QImage> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QQuickCanvasContext; @@ -207,6 +205,4 @@ QT_END_NAMESPACE QML_DECLARE_TYPE(QQuickCanvasItem) -QT_END_HEADER - #endif //QQUICKCANVASITEM_P_H diff --git a/src/quick/items/context2d/qquickcontext2d.cpp b/src/quick/items/context2d/qquickcontext2d.cpp index 2f37c7f109..17115ae8c4 100644 --- a/src/quick/items/context2d/qquickcontext2d.cpp +++ b/src/quick/items/context2d/qquickcontext2d.cpp @@ -62,7 +62,6 @@ #include <private/qv8domerrors_p.h> #include <QtCore/qnumeric.h> #include <private/qquickwindow_p.h> -#include <private/qquickwindowmanager_p.h> #if defined(Q_OS_QNX) || defined(Q_OS_LINUX_ANDROID) #include <ctype.h> @@ -3348,7 +3347,6 @@ QQuickContext2D::QQuickContext2D(QObject *parent) : QQuickCanvasContext(parent) , m_buffer(new QQuickContext2DCommandBuffer) , m_v8engine(0) - , m_windowManager(0) , m_surface(0) , m_glContext(0) , m_thread(0) @@ -3380,7 +3378,6 @@ void QQuickContext2D::init(QQuickCanvasItem *canvasItem, const QVariantMap &args m_renderTarget = canvasItem->renderTarget(); QQuickWindow *window = canvasItem->window(); - m_windowManager = QQuickWindowPrivate::get(window)->windowManager; m_renderStrategy = canvasItem->renderStrategy(); switch (m_renderTarget) { diff --git a/src/quick/items/context2d/qquickcontext2d_p.h b/src/quick/items/context2d/qquickcontext2d_p.h index 2124c731b2..24f5c44f18 100644 --- a/src/quick/items/context2d/qquickcontext2d_p.h +++ b/src/quick/items/context2d/qquickcontext2d_p.h @@ -62,15 +62,12 @@ #include <QElapsedTimer> #endif -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QQuickContext2DCommandBuffer; class QQuickContext2DTexture; class QQuickPixmap; class QSGTexture; -class QQuickWindowManager; class QSurface; class QOpenGLContext; @@ -240,7 +237,6 @@ public: v8::Local<v8::Value> m_strokeStyle; v8::Handle<v8::Value> m_v8path; QV8Engine *m_v8engine; - QQuickWindowManager *m_windowManager; QSurface *m_surface; QOpenGLContext *m_glContext; v8::Persistent<v8::Object> m_v8value; @@ -259,6 +255,4 @@ public: QT_END_NAMESPACE QML_DECLARE_TYPE(QQuickContext2D) -QT_END_HEADER - #endif // QQUICKCONTEXT2D_P_H diff --git a/src/quick/items/context2d/qquickcontext2dcommandbuffer_p.h b/src/quick/items/context2d/qquickcontext2dcommandbuffer_p.h index 0247a9e286..ff0a3a4e20 100644 --- a/src/quick/items/context2d/qquickcontext2dcommandbuffer_p.h +++ b/src/quick/items/context2d/qquickcontext2dcommandbuffer_p.h @@ -45,8 +45,6 @@ #include <QtCore/qmutex.h> #include "qquickcontext2d_p.h" -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QQuickCanvasItem; @@ -266,8 +264,6 @@ private: QMutex queueLock; }; -QT_END_HEADER - QT_END_NAMESPACE #endif // QQUICKCONTEXT2DCOMMANDBUFFER_P_H diff --git a/src/quick/items/context2d/qquickcontext2dtexture_p.h b/src/quick/items/context2d/qquickcontext2dtexture_p.h index 8fe3168533..df96a0eda1 100644 --- a/src/quick/items/context2d/qquickcontext2dtexture_p.h +++ b/src/quick/items/context2d/qquickcontext2dtexture_p.h @@ -53,8 +53,6 @@ #include <QtCore/QWaitCondition> #include <QtCore/QThread> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QQuickContext2DTile; @@ -180,8 +178,6 @@ private: QSGPlainTexture* m_texture; }; -QT_END_HEADER - QT_END_NAMESPACE #endif // QQUICKCONTEXT2DTEXTURE_P_H diff --git a/src/quick/items/context2d/qquickcontext2dtile_p.h b/src/quick/items/context2d/qquickcontext2dtile_p.h index 60415ee63d..22cbbe6a30 100644 --- a/src/quick/items/context2d/qquickcontext2dtile_p.h +++ b/src/quick/items/context2d/qquickcontext2dtile_p.h @@ -45,8 +45,6 @@ #include "qquickcontext2d_p.h" #include <QOpenGLFramebufferObject> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QQuickContext2DTexture; @@ -103,8 +101,6 @@ public: private: QImage m_image; }; -QT_END_HEADER - QT_END_NAMESPACE #endif // QQUICKCONTEXT2DTILE_P_H diff --git a/src/quick/items/items.pri b/src/quick/items/items.pri index 13abf7b958..2183a2bc63 100644 --- a/src/quick/items/items.pri +++ b/src/quick/items/items.pri @@ -38,10 +38,6 @@ HEADERS += \ $$PWD/qquickflickable_p.h \ $$PWD/qquickflickable_p_p.h \ $$PWD/qquicklistview_p.h \ - $$PWD/qquickvisualadaptormodel_p.h \ - $$PWD/qquickvisualdatamodel_p.h \ - $$PWD/qquickvisualdatamodel_p_p.h \ - $$PWD/qquickvisualitemmodel_p.h \ $$PWD/qquickrepeater_p.h \ $$PWD/qquickrepeater_p_p.h \ $$PWD/qquickgridview_p.h \ @@ -74,9 +70,7 @@ HEADERS += \ $$PWD/qquickitemview_p_p.h \ $$PWD/qquickitemviewtransition_p.h \ $$PWD/qquickscreen_p.h \ - $$PWD/qquickwindowmodule_p.h \ - $$PWD/qquickwindowmanager_p.h \ - $$PWD/qquickthreadedwindowmanager_p.h + $$PWD/qquickwindowmodule_p.h SOURCES += \ $$PWD/qquickevents.cpp \ @@ -101,9 +95,6 @@ SOURCES += \ $$PWD/qquickpincharea.cpp \ $$PWD/qquickflickable.cpp \ $$PWD/qquicklistview.cpp \ - $$PWD/qquickvisualadaptormodel.cpp \ - $$PWD/qquickvisualdatamodel.cpp \ - $$PWD/qquickvisualitemmodel.cpp \ $$PWD/qquickrepeater.cpp \ $$PWD/qquickgridview.cpp \ $$PWD/qquickpathview.cpp \ @@ -128,9 +119,7 @@ SOURCES += \ $$PWD/qquickitemview.cpp \ $$PWD/qquickitemviewtransition.cpp \ $$PWD/qquickwindowmodule.cpp \ - $$PWD/qquickscreen.cpp \ - $$PWD/qquickwindowmanager.cpp \ - $$PWD/qquickthreadedwindowmanager.cpp + $$PWD/qquickscreen.cpp SOURCES += \ $$PWD/qquickshadereffect.cpp \ diff --git a/src/quick/items/qquickaccessibleattached_p.h b/src/quick/items/qquickaccessibleattached_p.h index 15261232b2..2124356a70 100644 --- a/src/quick/items/qquickaccessibleattached_p.h +++ b/src/quick/items/qquickaccessibleattached_p.h @@ -52,8 +52,6 @@ #include <QtGui/qaccessible.h> #include <private/qtquickglobal_p.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE @@ -165,8 +163,6 @@ QT_END_NAMESPACE QML_DECLARE_TYPE(QQuickAccessibleAttached) QML_DECLARE_TYPEINFO(QQuickAccessibleAttached, QML_HAS_ATTACHED_PROPERTIES) -QT_END_HEADER - #endif // QT_NO_ACCESSIBILITY #endif diff --git a/src/quick/items/qquickanchors_p.h b/src/quick/items/qquickanchors_p.h index df3577c42d..1f18961caf 100644 --- a/src/quick/items/qquickanchors_p.h +++ b/src/quick/items/qquickanchors_p.h @@ -48,8 +48,6 @@ #include <private/qtquickglobal_p.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QQuickItem; @@ -200,6 +198,4 @@ QT_END_NAMESPACE QML_DECLARE_TYPE(QQuickAnchors) -QT_END_HEADER - #endif // QQUICKANCHORS_P_H diff --git a/src/quick/items/qquickanimatedimage_p.h b/src/quick/items/qquickanimatedimage_p.h index 83bae59af8..2775023d4e 100644 --- a/src/quick/items/qquickanimatedimage_p.h +++ b/src/quick/items/qquickanimatedimage_p.h @@ -46,8 +46,6 @@ #ifndef QT_NO_MOVIE -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QMovie; @@ -108,8 +106,6 @@ QT_END_NAMESPACE QML_DECLARE_TYPE(QQuickAnimatedImage) -QT_END_HEADER - #endif // QT_NO_MOVIE #endif // QQUICKANIMATEDIMAGE_P_H diff --git a/src/quick/items/qquickanimatedsprite_p.h b/src/quick/items/qquickanimatedsprite_p.h index 47dd51c3ee..6d0a821a47 100644 --- a/src/quick/items/qquickanimatedsprite_p.h +++ b/src/quick/items/qquickanimatedsprite_p.h @@ -46,8 +46,6 @@ #include <private/qquicksprite_p.h> #include <QTime> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QSGContext; @@ -378,6 +376,4 @@ private: QT_END_NAMESPACE -QT_END_HEADER - #endif // QQUICKANIMATEDSPRITE_P_H diff --git a/src/quick/items/qquickborderimage_p.h b/src/quick/items/qquickborderimage_p.h index 1cc86bc832..2ba69a383d 100644 --- a/src/quick/items/qquickborderimage_p.h +++ b/src/quick/items/qquickborderimage_p.h @@ -44,7 +44,6 @@ #include "qquickimagebase_p.h" -QT_BEGIN_HEADER QT_BEGIN_NAMESPACE class QQuickScaleGrid; @@ -102,6 +101,5 @@ private: QT_END_NAMESPACE QML_DECLARE_TYPE(QQuickBorderImage) -QT_END_HEADER #endif // QQUICKBORDERIMAGE_P_H diff --git a/src/quick/items/qquickdrag_p.h b/src/quick/items/qquickdrag_p.h index 5e1448aab6..c1835d9504 100644 --- a/src/quick/items/qquickdrag_p.h +++ b/src/quick/items/qquickdrag_p.h @@ -51,8 +51,6 @@ #ifndef QT_NO_DRAGANDDROP -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QQuickItem; @@ -205,8 +203,6 @@ private: QT_END_NAMESPACE -QT_END_HEADER - #endif // QT_NO_DRAGANDDROP #endif diff --git a/src/quick/items/qquickdroparea_p.h b/src/quick/items/qquickdroparea_p.h index 14921a2d28..bfc3f922b5 100644 --- a/src/quick/items/qquickdroparea_p.h +++ b/src/quick/items/qquickdroparea_p.h @@ -51,8 +51,6 @@ #ifndef QT_NO_DRAGANDDROP -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QQuickDropAreaPrivate; @@ -162,8 +160,6 @@ QT_END_NAMESPACE QML_DECLARE_TYPE(QQuickDropEvent) QML_DECLARE_TYPE(QQuickDropArea) -QT_END_HEADER - #endif // QT_NO_DRAGANDDROP #endif // QQUICKDROPAREA_P_H diff --git a/src/quick/items/qquickflickable_p.h b/src/quick/items/qquickflickable_p.h index 2e16f11882..5d36ea01ac 100644 --- a/src/quick/items/qquickflickable_p.h +++ b/src/quick/items/qquickflickable_p.h @@ -45,8 +45,6 @@ #include "qquickitem.h" #include <private/qtquickglobal_p.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QQuickFlickablePrivate; @@ -278,6 +276,4 @@ QT_END_NAMESPACE QML_DECLARE_TYPE(QQuickFlickable) -QT_END_HEADER - #endif // QQUICKFLICKABLE_P_H diff --git a/src/quick/items/qquickflipable_p.h b/src/quick/items/qquickflipable_p.h index 1bc6f4113d..795dc4f809 100644 --- a/src/quick/items/qquickflipable_p.h +++ b/src/quick/items/qquickflipable_p.h @@ -48,8 +48,6 @@ #include <QtGui/qvector3d.h> #include <QtCore/qobject.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QQuickFlipablePrivate; @@ -96,6 +94,4 @@ QT_END_NAMESPACE QML_DECLARE_TYPE(QQuickFlipable) -QT_END_HEADER - #endif // QQUICKFLIPABLE_P_H diff --git a/src/quick/items/qquickfocusscope_p.h b/src/quick/items/qquickfocusscope_p.h index d72eae0805..965c7538bf 100644 --- a/src/quick/items/qquickfocusscope_p.h +++ b/src/quick/items/qquickfocusscope_p.h @@ -44,8 +44,6 @@ #include "qquickitem.h" -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class Q_AUTOTEST_EXPORT QQuickFocusScope : public QQuickItem @@ -60,6 +58,4 @@ QT_END_NAMESPACE QML_DECLARE_TYPE(QQuickFocusScope) -QT_END_HEADER - #endif // QQUICKFOCUSSCOPE_P_H diff --git a/src/quick/items/qquickgridview.cpp b/src/quick/items/qquickgridview.cpp index fd9ce9ffcd..ea8badb584 100644 --- a/src/quick/items/qquickgridview.cpp +++ b/src/quick/items/qquickgridview.cpp @@ -40,10 +40,10 @@ ****************************************************************************/ #include "qquickgridview_p.h" -#include "qquickvisualitemmodel_p.h" #include "qquickflickable_p_p.h" #include "qquickitemview_p_p.h" +#include <private/qqmlobjectmodel_p.h> #include <private/qquicksmoothedanimation_p_p.h> #include <QtGui/qevent.h> @@ -209,7 +209,7 @@ public: virtual void setPosition(qreal pos); virtual void layoutVisibleItems(int fromModelIndex = 0); - virtual bool applyInsertionChange(const QQuickChangeSet::Insert &insert, ChangeResult *changeResult, QList<FxViewItem *> *addedItems, QList<MovedItem> *movingIntoView); + virtual bool applyInsertionChange(const QQmlChangeSet::Insert &insert, ChangeResult *changeResult, QList<FxViewItem *> *addedItems, QList<MovedItem> *movingIntoView); virtual void translateAndTransitionItemsAfter(int afterModelIndex, const ChangeResult &insertionResult, const ChangeResult &removalResult); virtual bool needsRefillForAddedOrRemovedIndex(int index) const; @@ -2105,13 +2105,16 @@ void QQuickGridView::geometryChanged(const QRectF &newGeometry, const QRectF &ol QQuickItemView::geometryChanged(newGeometry, oldGeometry); } -void QQuickGridView::initItem(int index, QQuickItem *item) +void QQuickGridView::initItem(int index, QObject *obj) { - QQuickItemView::initItem(index, item); - QQuickGridViewAttached *attached = static_cast<QQuickGridViewAttached *>( - qmlAttachedPropertiesObject<QQuickGridView>(item)); - if (attached) - attached->setView(this); + QQuickItemView::initItem(index, obj); + QQuickItem *item = qmlobject_cast<QQuickItem*>(obj); + if (item) { + QQuickGridViewAttached *attached = static_cast<QQuickGridViewAttached *>( + qmlAttachedPropertiesObject<QQuickGridView>(item)); + if (attached) + attached->setView(this); + } } /*! @@ -2286,7 +2289,7 @@ void QQuickGridView::moveCurrentIndexRight() } } -bool QQuickGridViewPrivate::applyInsertionChange(const QQuickChangeSet::Insert &change, ChangeResult *insertResult, QList<FxViewItem *> *addedItems, QList<MovedItem> *movingIntoView) +bool QQuickGridViewPrivate::applyInsertionChange(const QQmlChangeSet::Insert &change, ChangeResult *insertResult, QList<FxViewItem *> *addedItems, QList<MovedItem> *movingIntoView) { Q_Q(QQuickGridView); diff --git a/src/quick/items/qquickgridview_p.h b/src/quick/items/qquickgridview_p.h index 64cb814f8c..d98de1050f 100644 --- a/src/quick/items/qquickgridview_p.h +++ b/src/quick/items/qquickgridview_p.h @@ -46,11 +46,8 @@ #include <private/qqmlguard_p.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE -class QQuickVisualModel; class QQuickGridViewAttached; class QQuickGridViewPrivate; class Q_AUTOTEST_EXPORT QQuickGridView : public QQuickItemView @@ -112,7 +109,7 @@ protected: virtual void viewportMoved(Qt::Orientations); virtual void keyPressEvent(QKeyEvent *); virtual void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry); - virtual void initItem(int index, QQuickItem *item); + virtual void initItem(int index, QObject *item); }; class QQuickGridViewAttached : public QQuickItemViewAttached @@ -145,6 +142,4 @@ QT_END_NAMESPACE QML_DECLARE_TYPE(QQuickGridView) QML_DECLARE_TYPEINFO(QQuickGridView, QML_HAS_ATTACHED_PROPERTIES) -QT_END_HEADER - #endif // QQUICKGRIDVIEW_P_H diff --git a/src/quick/items/qquickimage_p.h b/src/quick/items/qquickimage_p.h index 9437b47635..b1f27a64ba 100644 --- a/src/quick/items/qquickimage_p.h +++ b/src/quick/items/qquickimage_p.h @@ -45,8 +45,6 @@ #include "qquickimagebase_p.h" #include <QtQuick/qsgtextureprovider.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QQuickImagePrivate; @@ -114,6 +112,4 @@ private: QT_END_NAMESPACE QML_DECLARE_TYPE(QQuickImage) -QT_END_HEADER - #endif // QQUICKIMAGE_P_H diff --git a/src/quick/items/qquickimagebase_p.h b/src/quick/items/qquickimagebase_p.h index f5ed69129d..388873b4d3 100644 --- a/src/quick/items/qquickimagebase_p.h +++ b/src/quick/items/qquickimagebase_p.h @@ -45,8 +45,6 @@ #include "qquickimplicitsizeitem_p.h" #include <private/qtquickglobal_p.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QQuickImageBasePrivate; @@ -114,6 +112,4 @@ private: QT_END_NAMESPACE -QT_END_HEADER - #endif // QQUICKIMAGEBASE_P_H diff --git a/src/quick/items/qquickimplicitsizeitem_p.h b/src/quick/items/qquickimplicitsizeitem_p.h index 6e306d8375..54b94dd183 100644 --- a/src/quick/items/qquickimplicitsizeitem_p.h +++ b/src/quick/items/qquickimplicitsizeitem_p.h @@ -45,8 +45,6 @@ #include "qquickpainteditem.h" #include <private/qtquickglobal_p.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QQuickImplicitSizeItemPrivate; @@ -70,6 +68,4 @@ private: QT_END_NAMESPACE -QT_END_HEADER - #endif // QQUICKIMPLICITSIZEITEM_H diff --git a/src/quick/items/qquickitem.cpp b/src/quick/items/qquickitem.cpp index d72a7b1a1d..26af6953e1 100644 --- a/src/quick/items/qquickitem.cpp +++ b/src/quick/items/qquickitem.cpp @@ -739,7 +739,12 @@ void QQuickKeyNavigationAttached::setFocusNavigation(QQuickItem *currentItem, co while (currentItem != initialItem && isNextItem); } -const QQuickKeysAttached::SigMap QQuickKeysAttached::sigMap[] = { +struct SigMap { + int key; + const char *sig; +}; + +const SigMap sigMap[] = { { Qt::Key_Left, "leftPressed" }, { Qt::Key_Right, "rightPressed" }, { Qt::Key_Up, "upPressed" }, @@ -771,6 +776,21 @@ const QQuickKeysAttached::SigMap QQuickKeysAttached::sigMap[] = { { 0, 0 } }; +const QByteArray QQuickKeysAttached::keyToSignal(int key) +{ + QByteArray keySignal; + if (key >= Qt::Key_0 && key <= Qt::Key_9) { + keySignal = "digit0Pressed"; + keySignal[5] = '0' + (key - Qt::Key_0); + } else { + int i = 0; + while (sigMap[i].key && sigMap[i].key != key) + ++i; + keySignal = sigMap[i].sig; + } + return keySignal; +} + bool QQuickKeysAttached::isConnected(const char *signalName) { Q_D(QQuickKeysAttached); @@ -3788,14 +3808,15 @@ void QQuickItem::forceActiveFocus() */ QQuickItem *QQuickItem::childAt(qreal x, qreal y) const { - // XXX todo - should this include transform etc.? const QList<QQuickItem *> children = childItems(); for (int i = children.count()-1; i >= 0; --i) { QQuickItem *child = children.at(i); - if (child->isVisible() && child->x() <= x - && child->x() + child->width() >= x - && child->y() <= y - && child->y() + child->height() >= y) + // Map coordinates to the child element's coordinate space + QPointF point = mapToItem(child, QPointF(x, y)); + if (child->isVisible() && point.x() >= 0 + && child->width() >= point.x() + && point.y() >= 0 + && child->height() >= point.y()) return child; } return 0; diff --git a/src/quick/items/qquickitem.h b/src/quick/items/qquickitem.h index b3ac9339a6..e9c817a580 100644 --- a/src/quick/items/qquickitem.h +++ b/src/quick/items/qquickitem.h @@ -52,8 +52,6 @@ #include <QtGui/qfont.h> #include <QtGui/qaccessible.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QQuickItem; @@ -446,6 +444,4 @@ QT_END_NAMESPACE QML_DECLARE_TYPE(QQuickItem) QML_DECLARE_TYPE(QQuickTransform) -QT_END_HEADER - #endif // QQUICKITEM_H diff --git a/src/quick/items/qquickitem_p.h b/src/quick/items/qquickitem_p.h index c238477d6f..8f6865b40b 100644 --- a/src/quick/items/qquickitem_p.h +++ b/src/quick/items/qquickitem_p.h @@ -819,28 +819,9 @@ private: virtual void inputMethodEvent(QInputMethodEvent *, bool post); virtual QVariant inputMethodQuery(Qt::InputMethodQuery query) const; #endif - const QByteArray keyToSignal(int key) { - QByteArray keySignal; - if (key >= Qt::Key_0 && key <= Qt::Key_9) { - keySignal = "digit0Pressed"; - keySignal[5] = '0' + (key - Qt::Key_0); - } else { - int i = 0; - while (sigMap[i].key && sigMap[i].key != key) - ++i; - keySignal = sigMap[i].sig; - } - return keySignal; - } + const QByteArray keyToSignal(int key); bool isConnected(const char *signalName); - - struct SigMap { - int key; - const char *sig; - }; - - static const SigMap sigMap[]; }; Qt::MouseButtons QQuickItemPrivate::acceptedMouseButtons() const diff --git a/src/quick/items/qquickitemanimation_p.h b/src/quick/items/qquickitemanimation_p.h index 1dfce5233c..796f6c8ade 100644 --- a/src/quick/items/qquickitemanimation_p.h +++ b/src/quick/items/qquickitemanimation_p.h @@ -46,8 +46,6 @@ #include <QtQuick/private/qquickanimation_p.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QQuickParentAnimationPrivate; @@ -198,6 +196,4 @@ QML_DECLARE_TYPE(QQuickParentAnimation) QML_DECLARE_TYPE(QQuickAnchorAnimation) QML_DECLARE_TYPE(QQuickPathAnimation) -QT_END_HEADER - #endif // QQUICKITEMANIMATION_H diff --git a/src/quick/items/qquickitemsmodule.cpp b/src/quick/items/qquickitemsmodule.cpp index c04807168f..71c23111d5 100644 --- a/src/quick/items/qquickitemsmodule.cpp +++ b/src/quick/items/qquickitemsmodule.cpp @@ -57,8 +57,6 @@ #include "qquickflickable_p.h" #include "qquickflickable_p_p.h" #include "qquicklistview_p.h" -#include "qquickvisualitemmodel_p.h" -#include "qquickvisualdatamodel_p.h" #include "qquickgridview_p.h" #include "qquickpathview_p.h" #include "qquickitemviewtransition_p.h" @@ -161,9 +159,6 @@ static void qt_quickitems_defineModule(const char *uri, int major, int minor) qmlRegisterType<QQuickTextEdit>(uri,major,minor,"TextEdit"); qmlRegisterType<QQuickTextInput>(uri,major,minor,"TextInput"); qmlRegisterType<QQuickViewSection>(uri,major,minor,"ViewSection"); - qmlRegisterType<QQuickVisualDataModel>(uri,major,minor,"VisualDataModel"); - qmlRegisterType<QQuickVisualDataGroup>(uri,major,minor,"VisualDataGroup"); - qmlRegisterType<QQuickVisualItemModel>(uri,major,minor,"VisualItemModel"); qmlRegisterType<QQuickItemLayer>(); qmlRegisterType<QQuickAnchors>(); @@ -178,7 +173,6 @@ static void qt_quickitems_defineModule(const char *uri, int major, int minor) #ifndef QT_NO_VALIDATOR qmlRegisterType<QValidator>(); #endif - qmlRegisterType<QQuickVisualModel>(); qmlRegisterType<QQuickPen>(); qmlRegisterType<QQuickFlickableVisibleArea>(); qRegisterMetaType<QQuickAnchorLine>("QQuickAnchorLine"); @@ -228,6 +222,8 @@ static void qt_quickitems_defineModule(const char *uri, int major, int minor) #ifndef QT_NO_ACCESSIBILITY qmlRegisterUncreatableType<QQuickAccessibleAttached>("QtQuick", 2, 0, "Accessible",QQuickAccessibleAttached::tr("Accessible is only available via attached properties")); #endif + + qmlRegisterType<QQuickGrid, 1>(uri, 2, 1, "Grid"); } void QQuickItemsModule::defineModule() diff --git a/src/quick/items/qquickitemsmodule_p.h b/src/quick/items/qquickitemsmodule_p.h index d21f182cc6..bbc7c5c9e2 100644 --- a/src/quick/items/qquickitemsmodule_p.h +++ b/src/quick/items/qquickitemsmodule_p.h @@ -44,8 +44,6 @@ #include <qqml.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QQuickItemsModule @@ -56,7 +54,5 @@ public: QT_END_NAMESPACE -QT_END_HEADER - #endif // QQUICKITEMSMODULE_P_H diff --git a/src/quick/items/qquickitemview.cpp b/src/quick/items/qquickitemview.cpp index b73fb8c5b6..198052b939 100644 --- a/src/quick/items/qquickitemview.cpp +++ b/src/quick/items/qquickitemview.cpp @@ -41,6 +41,7 @@ #include "qquickitemview_p_p.h" #include <QtQuick/private/qquicktransition_p.h> +#include <QtQml/QQmlInfo> #include "qplatformdefs.h" QT_BEGIN_NAMESPACE @@ -147,14 +148,14 @@ bool QQuickItemViewChangeSet::hasPendingChanges() const return !pendingChanges.isEmpty(); } -void QQuickItemViewChangeSet::applyChanges(const QQuickChangeSet &changeSet) +void QQuickItemViewChangeSet::applyChanges(const QQmlChangeSet &changeSet) { pendingChanges.apply(changeSet); int moveId = -1; int moveOffset = 0; - foreach (const QQuickChangeSet::Remove &r, changeSet.removes()) { + foreach (const QQmlChangeSet::Remove &r, changeSet.removes()) { itemCount -= r.count; if (moveId == -1 && newCurrentIndex >= r.index + r.count) { newCurrentIndex -= r.count; @@ -173,7 +174,7 @@ void QQuickItemViewChangeSet::applyChanges(const QQuickChangeSet &changeSet) currentChanged = true; } } - foreach (const QQuickChangeSet::Insert &i, changeSet.inserts()) { + foreach (const QQmlChangeSet::Insert &i, changeSet.inserts()) { if (moveId == -1) { if (itemCount && newCurrentIndex >= i.index) { newCurrentIndex += i.count; @@ -266,14 +267,14 @@ void QQuickItemView::setModel(const QVariant &model) if (d->modelVariant == model) return; if (d->model) { - disconnect(d->model, SIGNAL(modelUpdated(QQuickChangeSet,bool)), - this, SLOT(modelUpdated(QQuickChangeSet,bool))); - disconnect(d->model, SIGNAL(initItem(int,QQuickItem*)), this, SLOT(initItem(int,QQuickItem*))); - disconnect(d->model, SIGNAL(createdItem(int,QQuickItem*)), this, SLOT(createdItem(int,QQuickItem*))); - disconnect(d->model, SIGNAL(destroyingItem(QQuickItem*)), this, SLOT(destroyingItem(QQuickItem*))); + disconnect(d->model, SIGNAL(modelUpdated(QQmlChangeSet,bool)), + this, SLOT(modelUpdated(QQmlChangeSet,bool))); + disconnect(d->model, SIGNAL(initItem(int,QObject*)), this, SLOT(initItem(int,QObject*))); + disconnect(d->model, SIGNAL(createdItem(int,QObject*)), this, SLOT(createdItem(int,QObject*))); + disconnect(d->model, SIGNAL(destroyingItem(QObject*)), this, SLOT(destroyingItem(QObject*))); } - QQuickVisualModel *oldModel = d->model; + QQmlInstanceModel *oldModel = d->model; d->clear(); d->model = 0; @@ -281,8 +282,8 @@ void QQuickItemView::setModel(const QVariant &model) d->modelVariant = model; QObject *object = qvariant_cast<QObject*>(model); - QQuickVisualModel *vim = 0; - if (object && (vim = qobject_cast<QQuickVisualModel *>(object))) { + QQmlInstanceModel *vim = 0; + if (object && (vim = qobject_cast<QQmlInstanceModel *>(object))) { if (d->ownModel) { delete oldModel; d->ownModel = false; @@ -290,22 +291,22 @@ void QQuickItemView::setModel(const QVariant &model) d->model = vim; } else { if (!d->ownModel) { - d->model = new QQuickVisualDataModel(qmlContext(this), this); + d->model = new QQmlDelegateModel(qmlContext(this), this); d->ownModel = true; if (isComponentComplete()) - static_cast<QQuickVisualDataModel *>(d->model.data())->componentComplete(); + static_cast<QQmlDelegateModel *>(d->model.data())->componentComplete(); } else { d->model = oldModel; } - if (QQuickVisualDataModel *dataModel = qobject_cast<QQuickVisualDataModel*>(d->model)) + if (QQmlDelegateModel *dataModel = qobject_cast<QQmlDelegateModel*>(d->model)) dataModel->setModel(model); } if (d->model) { d->bufferMode = QQuickItemViewPrivate::BufferBefore | QQuickItemViewPrivate::BufferAfter; - connect(d->model, SIGNAL(createdItem(int,QQuickItem*)), this, SLOT(createdItem(int,QQuickItem*))); - connect(d->model, SIGNAL(initItem(int,QQuickItem*)), this, SLOT(initItem(int,QQuickItem*))); - connect(d->model, SIGNAL(destroyingItem(QQuickItem*)), this, SLOT(destroyingItem(QQuickItem*))); + connect(d->model, SIGNAL(createdItem(int,QObject*)), this, SLOT(createdItem(int,QObject*))); + connect(d->model, SIGNAL(initItem(int,QObject*)), this, SLOT(initItem(int,QObject*))); + connect(d->model, SIGNAL(destroyingItem(QObject*)), this, SLOT(destroyingItem(QObject*))); if (isComponentComplete()) { d->updateSectionCriteria(); d->refill(); @@ -319,8 +320,8 @@ void QQuickItemView::setModel(const QVariant &model) } } - connect(d->model, SIGNAL(modelUpdated(QQuickChangeSet,bool)), - this, SLOT(modelUpdated(QQuickChangeSet,bool))); + connect(d->model, SIGNAL(modelUpdated(QQmlChangeSet,bool)), + this, SLOT(modelUpdated(QQmlChangeSet,bool))); emit countChanged(); } emit modelChanged(); @@ -330,7 +331,7 @@ QQmlComponent *QQuickItemView::delegate() const { Q_D(const QQuickItemView); if (d->model) { - if (QQuickVisualDataModel *dataModel = qobject_cast<QQuickVisualDataModel*>(d->model)) + if (QQmlDelegateModel *dataModel = qobject_cast<QQmlDelegateModel*>(d->model)) return dataModel->delegate(); } @@ -343,10 +344,10 @@ void QQuickItemView::setDelegate(QQmlComponent *delegate) if (delegate == this->delegate()) return; if (!d->ownModel) { - d->model = new QQuickVisualDataModel(qmlContext(this)); + d->model = new QQmlDelegateModel(qmlContext(this)); d->ownModel = true; } - if (QQuickVisualDataModel *dataModel = qobject_cast<QQuickVisualDataModel*>(d->model)) { + if (QQmlDelegateModel *dataModel = qobject_cast<QQmlDelegateModel*>(d->model)) { int oldCount = dataModel->count(); dataModel->setDelegate(delegate); if (isComponentComplete()) { @@ -371,6 +372,7 @@ void QQuickItemView::setDelegate(QQmlComponent *delegate) emit countChanged(); } emit delegateChanged(); + d->delegateValidated = false; } @@ -972,7 +974,7 @@ void QQuickItemViewPrivate::applyPendingChanges() layout(); } -int QQuickItemViewPrivate::findMoveKeyIndex(QQuickChangeSet::MoveKey key, const QVector<QQuickChangeSet::Remove> &changes) const +int QQuickItemViewPrivate::findMoveKeyIndex(QQmlChangeSet::MoveKey key, const QVector<QQmlChangeSet::Remove> &changes) const { for (int i=0; i<changes.count(); i++) { for (int j=changes[i].index; j<changes[i].index + changes[i].count; j++) { @@ -1152,7 +1154,7 @@ void QQuickItemView::destroyRemoved() d->forceLayoutPolish(); } -void QQuickItemView::modelUpdated(const QQuickChangeSet &changeSet, bool reset) +void QQuickItemView::modelUpdated(const QQmlChangeSet &changeSet, bool reset) { Q_D(QQuickItemView); if (reset) { @@ -1388,7 +1390,7 @@ void QQuickItemView::componentComplete() { Q_D(QQuickItemView); if (d->model && d->ownModel) - static_cast<QQuickVisualDataModel *>(d->model.data())->componentComplete(); + static_cast<QQmlDelegateModel *>(d->model.data())->componentComplete(); QQuickFlickable::componentComplete(); @@ -1440,7 +1442,7 @@ QQuickItemViewPrivate::QQuickItemViewPrivate() , inLayout(false), inViewportMoved(false), forceLayout(false), currentIndexCleared(false) , haveHighlightRange(false), autoHighlight(true), highlightRangeStartValid(false), highlightRangeEndValid(false) , fillCacheBuffer(false), inRequest(false) - , runDelayedRemoveTransition(false) + , runDelayedRemoveTransition(false), delegateValidated(false) { bufferPause.addAnimationChangeListener(this, QAbstractAnimationJob::Completion); bufferPause.setLoopCount(1); @@ -1874,8 +1876,8 @@ bool QQuickItemViewPrivate::applyModelChanges(ChangeResult *totalInsertionResult totalInsertionResult->visiblePos = prevViewPos; totalRemovalResult->visiblePos = prevViewPos; - const QVector<QQuickChangeSet::Remove> &removals = currentChanges.pendingChanges.removes(); - const QVector<QQuickChangeSet::Insert> &insertions = currentChanges.pendingChanges.inserts(); + const QVector<QQmlChangeSet::Remove> &removals = currentChanges.pendingChanges.removes(); + const QVector<QQmlChangeSet::Insert> &insertions = currentChanges.pendingChanges.inserts(); ChangeResult insertionResult(prevViewPos); ChangeResult removalResult(prevViewPos); @@ -1895,7 +1897,7 @@ bool QQuickItemViewPrivate::applyModelChanges(ChangeResult *totalInsertionResult } } if (runDelayedRemoveTransition) { - QQuickChangeSet::Remove removal; + QQmlChangeSet::Remove removal; for (QList<FxViewItem*>::Iterator it = visibleItems.begin(); it != visibleItems.end();) { FxViewItem *item = *it; if (item->index == -1 && !item->attached->delayRemove()) { @@ -1963,7 +1965,7 @@ bool QQuickItemViewPrivate::applyModelChanges(ChangeResult *totalInsertionResult // Whatever removed/moved items remain are no longer visible items. prepareRemoveTransitions(¤tChanges.removedItems); - for (QHash<QQuickChangeSet::MoveKey, FxViewItem *>::Iterator it = currentChanges.removedItems.begin(); + for (QHash<QQmlChangeSet::MoveKey, FxViewItem *>::Iterator it = currentChanges.removedItems.begin(); it != currentChanges.removedItems.end(); ++it) { releaseItem(it.value()); } @@ -1992,7 +1994,7 @@ bool QQuickItemViewPrivate::applyModelChanges(ChangeResult *totalInsertionResult return visibleAffected; } -bool QQuickItemViewPrivate::applyRemovalChange(const QQuickChangeSet::Remove &removal, ChangeResult *removeResult, int *removedCount) +bool QQuickItemViewPrivate::applyRemovalChange(const QQmlChangeSet::Remove &removal, ChangeResult *removeResult, int *removedCount) { Q_Q(QQuickItemView); bool visibleAffected = false; @@ -2042,7 +2044,7 @@ bool QQuickItemViewPrivate::applyRemovalChange(const QQuickChangeSet::Remove &re return visibleAffected; } -void QQuickItemViewPrivate::removeItem(FxViewItem *item, const QQuickChangeSet::Remove &removal, ChangeResult *removeResult) +void QQuickItemViewPrivate::removeItem(FxViewItem *item, const QQmlChangeSet::Remove &removal, ChangeResult *removeResult) { if (removeResult->visiblePos.isValid()) { if (item->position() < removeResult->visiblePos) @@ -2055,7 +2057,7 @@ void QQuickItemViewPrivate::removeItem(FxViewItem *item, const QQuickChangeSet:: item->transitionNextReposition(transitioner, QQuickItemViewTransitioner::MoveTransition, true); } else { // track item so it is released later - currentChanges.removedItems.insertMulti(QQuickChangeSet::MoveKey(), item); + currentChanges.removedItems.insertMulti(QQmlChangeSet::MoveKey(), item); } if (!removeResult->changedFirstItem && item == *visibleItems.constBegin()) removeResult->changedFirstItem = true; @@ -2120,14 +2122,14 @@ void QQuickItemViewPrivate::prepareVisibleItemTransitions() visibleItems[i]->prepareTransition(transitioner, viewBounds); } -void QQuickItemViewPrivate::prepareRemoveTransitions(QHash<QQuickChangeSet::MoveKey, FxViewItem *> *removedItems) +void QQuickItemViewPrivate::prepareRemoveTransitions(QHash<QQmlChangeSet::MoveKey, FxViewItem *> *removedItems) { if (!transitioner) return; if (transitioner->canTransition(QQuickItemViewTransitioner::RemoveTransition, true) || transitioner->canTransition(QQuickItemViewTransitioner::RemoveTransition, false)) { - for (QHash<QQuickChangeSet::MoveKey, FxViewItem *>::Iterator it = removedItems->begin(); + for (QHash<QQmlChangeSet::MoveKey, FxViewItem *>::Iterator it = removedItems->begin(); it != removedItems->end(); ) { bool isRemove = it.key().moveId < 0; if (isRemove) { @@ -2197,7 +2199,20 @@ FxViewItem *QQuickItemViewPrivate::createItem(int modelIndex, bool asynchronous) requestedIndex = modelIndex; inRequest = true; - if (QQuickItem *item = model->item(modelIndex, asynchronous)) { + QObject* object = model->object(modelIndex, asynchronous); + QQuickItem *item = qmlobject_cast<QQuickItem*>(object); + if (!item) { + if (object) { + model->release(object); + if (!delegateValidated) { + delegateValidated = true; + QObject* delegate = q->delegate(); + qmlInfo(delegate ? delegate : q) << q->tr("Delegate must be of Item type"); + } + } + inRequest = false; + return 0; + } else { item->setParentItem(q->contentItem()); if (requestedIndex == modelIndex) requestedIndex = -1; @@ -2212,15 +2227,13 @@ FxViewItem *QQuickItemViewPrivate::createItem(int modelIndex, bool asynchronous) inRequest = false; return viewItem; } - - inRequest = false; - return 0; } -void QQuickItemView::createdItem(int index, QQuickItem *item) +void QQuickItemView::createdItem(int index, QObject* object) { Q_D(QQuickItemView); + QQuickItem* item = qmlobject_cast<QQuickItem*>(object); if (!d->inRequest) { d->unrequestedItems.insert(item, index); d->requestedIndex = -1; @@ -2235,16 +2248,21 @@ void QQuickItemView::createdItem(int index, QQuickItem *item) } } -void QQuickItemView::initItem(int, QQuickItem *item) +void QQuickItemView::initItem(int, QObject *object) { - item->setZ(1); - item->setParentItem(contentItem()); - QQuickItemPrivate::get(item)->setCulled(true); + QQuickItem* item = qmlobject_cast<QQuickItem*>(object); + if (item) { + item->setZ(1); + item->setParentItem(contentItem()); + QQuickItemPrivate::get(item)->setCulled(true); + } } -void QQuickItemView::destroyingItem(QQuickItem *item) +void QQuickItemView::destroyingItem(QObject *object) { Q_D(QQuickItemView); + QQuickItem* item = qmlobject_cast<QQuickItem*>(object); + item->setParentItem(0); d->unrequestedItems.remove(item); } @@ -2257,14 +2275,16 @@ bool QQuickItemViewPrivate::releaseItem(FxViewItem *item) trackedItem = 0; QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(item->item); itemPrivate->removeItemChangeListener(this, QQuickItemPrivate::Geometry); - QQuickVisualModel::ReleaseFlags flags = model->release(item->item); + QQmlInstanceModel::ReleaseFlags flags = model->release(item->item); if (flags == 0) { // item was not destroyed, and we no longer reference it. QQuickItemPrivate::get(item->item)->setCulled(true); unrequestedItems.insert(item->item, model->indexOf(item->item, q)); + } else if (flags & QQmlInstanceModel::Destroyed) { + item->item->setParentItem(0); } delete item; - return flags != QQuickVisualModel::Referenced; + return flags != QQmlInstanceModel::Referenced; } QQuickItem *QQuickItemViewPrivate::createHighlightItem() diff --git a/src/quick/items/qquickitemview_p.h b/src/quick/items/qquickitemview_p.h index 82180a2e13..b0f910680a 100644 --- a/src/quick/items/qquickitemview_p.h +++ b/src/quick/items/qquickitemview_p.h @@ -44,11 +44,9 @@ #include "qquickflickable_p.h" -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE -class QQuickChangeSet; +class QQmlChangeSet; class QQuickItemViewPrivate; @@ -257,10 +255,10 @@ protected: protected slots: void destroyRemoved(); - void createdItem(int index, QQuickItem *item); - virtual void initItem(int index, QQuickItem *item); - void modelUpdated(const QQuickChangeSet &changeSet, bool reset); - void destroyingItem(QQuickItem *item); + void createdItem(int index, QObject *item); + virtual void initItem(int index, QObject *item); + void modelUpdated(const QQmlChangeSet &changeSet, bool reset); + void destroyingItem(QObject *item); void animStopped(); void trackedPositionChanged(); @@ -352,7 +350,5 @@ public: QT_END_NAMESPACE -QT_END_HEADER - #endif // QQUICKITEMVIEW_P_H diff --git a/src/quick/items/qquickitemview_p_p.h b/src/quick/items/qquickitemview_p_p.h index 0efc458c3d..1fa933ebd1 100644 --- a/src/quick/items/qquickitemview_p_p.h +++ b/src/quick/items/qquickitemview_p_p.h @@ -45,13 +45,11 @@ #include "qquickitemview_p.h" #include "qquickitemviewtransition_p.h" #include "qquickflickable_p_p.h" -#include "qquickvisualdatamodel_p.h" -#include "qquickvisualitemmodel_p.h" -#include <private/qquickchangeset_p.h> +#include <QtQml/private/qqmlobjectmodel_p.h> +#include <QtQml/private/qqmldelegatemodel_p.h> +#include <QtQml/private/qqmlchangeset_p.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE @@ -103,14 +101,14 @@ public: void prepare(int currentIndex, int count); void reset(); - void applyChanges(const QQuickChangeSet &changeSet); + void applyChanges(const QQmlChangeSet &changeSet); void applyBufferedChanges(const QQuickItemViewChangeSet &other); int itemCount; int newCurrentIndex; - QQuickChangeSet pendingChanges; - QHash<QQuickChangeSet::MoveKey, FxViewItem *> removedItems; + QQmlChangeSet pendingChanges; + QHash<QQmlChangeSet::MoveKey, FxViewItem *> removedItems; bool active : 1; bool currentChanged : 1; @@ -209,18 +207,18 @@ public: void applyPendingChanges(); bool applyModelChanges(ChangeResult *insertionResult, ChangeResult *removalResult); - bool applyRemovalChange(const QQuickChangeSet::Remove &removal, ChangeResult *changeResult, int *removedCount); - void removeItem(FxViewItem *item, const QQuickChangeSet::Remove &removal, ChangeResult *removeResult); + bool applyRemovalChange(const QQmlChangeSet::Remove &removal, ChangeResult *changeResult, int *removedCount); + void removeItem(FxViewItem *item, const QQmlChangeSet::Remove &removal, ChangeResult *removeResult); void repositionFirstItem(FxViewItem *prevVisibleItemsFirst, qreal prevVisibleItemsFirstPos, FxViewItem *prevFirstVisible, ChangeResult *insertionResult, ChangeResult *removalResult); void createTransitioner(); void prepareVisibleItemTransitions(); - void prepareRemoveTransitions(QHash<QQuickChangeSet::MoveKey, FxViewItem *> *removedItems); + void prepareRemoveTransitions(QHash<QQmlChangeSet::MoveKey, FxViewItem *> *removedItems); bool prepareNonVisibleItemTransition(FxViewItem *item, const QRectF &viewBounds); virtual void viewItemTransitionFinished(QQuickItemViewTransitionableItem *item); - int findMoveKeyIndex(QQuickChangeSet::MoveKey key, const QVector<QQuickChangeSet::Remove> &changes) const; + int findMoveKeyIndex(QQmlChangeSet::MoveKey key, const QVector<QQmlChangeSet::Remove> &changes) const; void checkVisible() const; void showVisibleItems() const; @@ -251,7 +249,7 @@ public: q->polish(); } - QQmlGuard<QQuickVisualModel> model; + QQmlGuard<QQmlInstanceModel> model; QVariant modelVariant; int itemCount; int buffer; @@ -286,8 +284,8 @@ public: struct MovedItem { FxViewItem *item; - QQuickChangeSet::MoveKey moveKey; - MovedItem(FxViewItem *i, QQuickChangeSet::MoveKey k) + QQmlChangeSet::MoveKey moveKey; + MovedItem(FxViewItem *i, QQmlChangeSet::MoveKey k) : item(i), moveKey(k) {} }; QQuickItemViewTransitioner *transitioner; @@ -309,6 +307,7 @@ public: bool fillCacheBuffer : 1; bool inRequest : 1; bool runDelayedRemoveTransition : 1; + bool delegateValidated : 1; protected: virtual Qt::Orientation layoutOrientation() const = 0; @@ -346,7 +345,7 @@ protected: virtual void layoutVisibleItems(int fromModelIndex = 0) = 0; virtual void changedVisibleIndex(int newIndex) = 0; - virtual bool applyInsertionChange(const QQuickChangeSet::Insert &insert, ChangeResult *changeResult, + virtual bool applyInsertionChange(const QQmlChangeSet::Insert &insert, ChangeResult *changeResult, QList<FxViewItem *> *newItems, QList<MovedItem> *movingIntoView) = 0; virtual bool needsRefillForAddedOrRemovedIndex(int) const { return false; } @@ -363,6 +362,4 @@ protected: QT_END_NAMESPACE -QT_END_HEADER - #endif // QQUICKITEMVIEW_P_P_H diff --git a/src/quick/items/qquickitemviewtransition_p.h b/src/quick/items/qquickitemviewtransition_p.h index 2667971fd7..4fb4386c57 100644 --- a/src/quick/items/qquickitemviewtransition_p.h +++ b/src/quick/items/qquickitemviewtransition_p.h @@ -44,8 +44,6 @@ #include <private/qquicktransitionmanager_p_p.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QQuickItem; @@ -211,6 +209,4 @@ QT_END_NAMESPACE QML_DECLARE_TYPE(QQuickViewTransitionAttached) QML_DECLARE_TYPEINFO(QQuickViewTransitionAttached, QML_HAS_ATTACHED_PROPERTIES) -QT_END_HEADER - #endif // QQUICKITEMVIEWTRANSITION_P_P_H diff --git a/src/quick/items/qquicklistview.cpp b/src/quick/items/qquicklistview.cpp index 57f1b99456..297f64176c 100644 --- a/src/quick/items/qquicklistview.cpp +++ b/src/quick/items/qquicklistview.cpp @@ -41,8 +41,8 @@ #include "qquicklistview_p.h" #include "qquickitemview_p_p.h" -#include "qquickvisualitemmodel_p.h" +#include <private/qqmlobjectmodel_p.h> #include <QtQml/qqmlexpression.h> #include <QtQml/qqmlengine.h> #include <QtQml/qqmlinfo.h> @@ -106,7 +106,7 @@ public: virtual void setPosition(qreal pos); virtual void layoutVisibleItems(int fromModelIndex = 0); - virtual bool applyInsertionChange(const QQuickChangeSet::Insert &insert, ChangeResult *changeResult, QList<FxViewItem *> *addedItems, QList<MovedItem> *movingIntoView); + virtual bool applyInsertionChange(const QQmlChangeSet::Insert &insert, ChangeResult *changeResult, QList<FxViewItem *> *addedItems, QList<MovedItem> *movingIntoView); virtual void translateAndTransitionItemsAfter(int afterIndex, const ChangeResult &insertionResult, const ChangeResult &removalResult); virtual void updateSectionCriteria(); @@ -2803,13 +2803,16 @@ void QQuickListView::geometryChanged(const QRectF &newGeometry, const QRectF &ol QQuickItemView::geometryChanged(newGeometry, oldGeometry); } -void QQuickListView::initItem(int index, QQuickItem *item) +void QQuickListView::initItem(int index, QObject *object) { - QQuickItemView::initItem(index, item); - QQuickListViewAttached *attached = static_cast<QQuickListViewAttached *>( - qmlAttachedPropertiesObject<QQuickListView>(item)); - if (attached) - attached->setView(this); + QQuickItemView::initItem(index, object); + QQuickItem *item = qmlobject_cast<QQuickItem*>(object); + if (item) { + QQuickListViewAttached *attached = static_cast<QQuickListViewAttached *>( + qmlAttachedPropertiesObject<QQuickListView>(item)); + if (attached) + attached->setView(this); + } } @@ -2867,7 +2870,7 @@ void QQuickListViewPrivate::updateSectionCriteria() } } -bool QQuickListViewPrivate::applyInsertionChange(const QQuickChangeSet::Insert &change, ChangeResult *insertResult, QList<FxViewItem *> *addedItems, QList<MovedItem> *movingIntoView) +bool QQuickListViewPrivate::applyInsertionChange(const QQmlChangeSet::Insert &change, ChangeResult *insertResult, QList<FxViewItem *> *addedItems, QList<MovedItem> *movingIntoView) { int modelIndex = change.index; int count = change.count; diff --git a/src/quick/items/qquicklistview_p.h b/src/quick/items/qquicklistview_p.h index f6c405e86b..3e766f8068 100644 --- a/src/quick/items/qquicklistview_p.h +++ b/src/quick/items/qquicklistview_p.h @@ -46,8 +46,6 @@ #include <private/qqmlguard_p.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QQuickListView; @@ -96,7 +94,7 @@ private: }; -class QQuickVisualModel; +class QQmlInstanceModel; class QQuickListViewAttached; class Q_AUTOTEST_EXPORT QQuickListView : public QQuickItemView { @@ -169,7 +167,7 @@ protected: virtual void viewportMoved(Qt::Orientations orient); virtual void keyPressEvent(QKeyEvent *); virtual void geometryChanged(const QRectF &newGeometry,const QRectF &oldGeometry); - virtual void initItem(int index, QQuickItem *item); + virtual void initItem(int index, QObject *item); }; class QQuickListViewAttached : public QQuickItemViewAttached @@ -205,6 +203,4 @@ QML_DECLARE_TYPEINFO(QQuickListView, QML_HAS_ATTACHED_PROPERTIES) QML_DECLARE_TYPE(QQuickListView) QML_DECLARE_TYPE(QQuickViewSection) -QT_END_HEADER - #endif // QQUICKLISTVIEW_P_H diff --git a/src/quick/items/qquickloader_p.h b/src/quick/items/qquickloader_p.h index ebf9e16c1e..6a69ccd32f 100644 --- a/src/quick/items/qquickloader_p.h +++ b/src/quick/items/qquickloader_p.h @@ -44,8 +44,6 @@ #include "qquickimplicitsizeitem_p.h" -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QQuickLoaderPrivate; @@ -115,6 +113,4 @@ QT_END_NAMESPACE QML_DECLARE_TYPE(QQuickLoader) -QT_END_HEADER - #endif // QQUICKLOADER_P_H diff --git a/src/quick/items/qquickmousearea_p.h b/src/quick/items/qquickmousearea_p.h index eb49535d95..ad15167b10 100644 --- a/src/quick/items/qquickmousearea_p.h +++ b/src/quick/items/qquickmousearea_p.h @@ -46,8 +46,6 @@ #include <private/qtquickglobal_p.h> #include <QtCore/qstringlist.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QQuickMouseEvent; @@ -252,6 +250,4 @@ QML_DECLARE_TYPEINFO(QQuickDrag, QML_HAS_ATTACHED_PROPERTIES) #endif QML_DECLARE_TYPE(QQuickMouseArea) -QT_END_HEADER - #endif // QQUICKMOUSEAREA_P_H diff --git a/src/quick/items/qquickmultipointtoucharea_p.h b/src/quick/items/qquickmultipointtoucharea_p.h index 1ab934e06d..e2ae5ed24e 100644 --- a/src/quick/items/qquickmultipointtoucharea_p.h +++ b/src/quick/items/qquickmultipointtoucharea_p.h @@ -50,8 +50,6 @@ #include <QtGui/qguiapplication.h> #include <QtGui/qstylehints.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QQuickMultiPointTouchArea; @@ -271,6 +269,4 @@ QML_DECLARE_TYPE(QQuickTouchPoint) QML_DECLARE_TYPE(QQuickGrabGestureEvent) QML_DECLARE_TYPE(QQuickMultiPointTouchArea) -QT_END_HEADER - #endif // QQUICKMULTIPOINTTOUCHAREA_H diff --git a/src/quick/items/qquickpainteditem.h b/src/quick/items/qquickpainteditem.h index d2d7a54467..88924003b8 100644 --- a/src/quick/items/qquickpainteditem.h +++ b/src/quick/items/qquickpainteditem.h @@ -45,8 +45,6 @@ #include <QtQuick/qquickitem.h> #include <QtGui/qcolor.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QQuickPaintedItemPrivate; @@ -125,6 +123,4 @@ Q_DECLARE_OPERATORS_FOR_FLAGS(QQuickPaintedItem::PerformanceHints) QT_END_NAMESPACE -QT_END_HEADER - #endif // QQUICKPAINTEDITEM_P_H diff --git a/src/quick/items/qquickpathview.cpp b/src/quick/items/qquickpathview.cpp index a7be50bc11..e9aa6985fc 100644 --- a/src/quick/items/qquickpathview.cpp +++ b/src/quick/items/qquickpathview.cpp @@ -46,8 +46,9 @@ #include <QtQuick/private/qquickstate_p.h> #include <private/qqmlglobal_p.h> #include <private/qqmlopenmetaobject_p.h> -#include <private/qquickchangeset_p.h> +#include <private/qqmlchangeset_p.h> +#include <QtQml/qqmlinfo.h> #include <QtGui/qevent.h> #include <QtGui/qevent.h> #include <QtGui/qguiapplication.h> @@ -117,7 +118,7 @@ QQuickPathViewPrivate::QQuickPathViewPrivate() , offset(0.0), offsetAdj(0.0), mappedRange(1.0), mappedCache(0.0) , stealMouse(false), ownModel(false), interactive(true), haveHighlightRange(true) , autoHighlight(true), highlightUp(false), layoutScheduled(false) - , moving(false), flicking(false), dragging(false), inRequest(false) + , moving(false), flicking(false), dragging(false), inRequest(false), delegateValidated(false) , dragMargin(0), deceleration(100), maximumFlickVelocity(QML_FLICK_DEFAULTMAXVELOCITY) , moveOffset(this, &QQuickPathViewPrivate::setAdjustedOffset), flickDuration(0) , firstIndex(-1), pathItems(-1), requestedIndex(-1), cacheSize(0), requestedZ(0) @@ -150,8 +151,18 @@ QQuickItem *QQuickPathViewPrivate::getItem(int modelIndex, qreal z, bool async) requestedIndex = modelIndex; requestedZ = z; inRequest = true; - QQuickItem *item = model->item(modelIndex, async); - if (item) { + QObject *object = model->object(modelIndex, async); + QQuickItem *item = qmlobject_cast<QQuickItem*>(object); + if (!item) { + if (object) { + model->release(object); + if (!delegateValidated) { + delegateValidated = true; + QObject* delegate = q->delegate(); + qmlInfo(delegate ? delegate : q) << q->tr("Delegate must be of Item type"); + } + } + } else { item->setParentItem(q); requestedIndex = -1; QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(item); @@ -161,9 +172,10 @@ QQuickItem *QQuickPathViewPrivate::getItem(int modelIndex, qreal z, bool async) return item; } -void QQuickPathView::createdItem(int index, QQuickItem *item) +void QQuickPathView::createdItem(int index, QObject *object) { Q_D(QQuickPathView); + QQuickItem *item = qmlobject_cast<QQuickItem*>(object); if (d->requestedIndex != index) { qPathViewAttachedType = d->attachedType(); QQuickPathViewAttached *att = static_cast<QQuickPathViewAttached *>(qmlAttachedPropertiesObject<QQuickPathView>(item)); @@ -181,10 +193,11 @@ void QQuickPathView::createdItem(int index, QQuickItem *item) } } -void QQuickPathView::initItem(int index, QQuickItem *item) +void QQuickPathView::initItem(int index, QObject *object) { Q_D(QQuickPathView); - if (d->requestedIndex == index) { + QQuickItem *item = qmlobject_cast<QQuickItem*>(object); + if (item && d->requestedIndex == index) { QQuickItemPrivate::get(item)->setCulled(true); item->setParentItem(this); qPathViewAttachedType = d->attachedType(); @@ -209,10 +222,14 @@ void QQuickPathViewPrivate::releaseItem(QQuickItem *item) return; QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(item); itemPrivate->removeItemChangeListener(this, QQuickItemPrivate::Geometry); - if (model->release(item) == 0) { + QQmlInstanceModel::ReleaseFlags flags = model->release(item); + if (!flags) { // item was not destroyed, and we no longer reference it. if (QQuickPathViewAttached *att = attached(item)) att->setOnPath(false); + } else if (flags & QQmlInstanceModel::Destroyed) { + // but we still reference it + item->setParentItem(0); } } @@ -619,19 +636,19 @@ void QQuickPathView::setModel(const QVariant &model) return; if (d->model) { - qmlobject_disconnect(d->model, QQuickVisualModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), - this, QQuickPathView, SLOT(modelUpdated(QQuickChangeSet,bool))); - qmlobject_disconnect(d->model, QQuickVisualModel, SIGNAL(createdItem(int,QQuickItem*)), - this, QQuickPathView, SLOT(createdItem(int,QQuickItem*))); - qmlobject_disconnect(d->model, QQuickVisualModel, SIGNAL(initItem(int,QQuickItem*)), - this, QQuickPathView, SLOT(initItem(int,QQuickItem*))); + qmlobject_disconnect(d->model, QQmlInstanceModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), + this, QQuickPathView, SLOT(modelUpdated(QQmlChangeSet,bool))); + qmlobject_disconnect(d->model, QQmlInstanceModel, SIGNAL(createdItem(int,QObject*)), + this, QQuickPathView, SLOT(createdItem(int,QObject*))); + qmlobject_disconnect(d->model, QQmlInstanceModel, SIGNAL(initItem(int,QObject*)), + this, QQuickPathView, SLOT(initItem(int,QObject*))); d->clear(); } d->modelVariant = model; QObject *object = qvariant_cast<QObject*>(model); - QQuickVisualModel *vim = 0; - if (object && (vim = qobject_cast<QQuickVisualModel *>(object))) { + QQmlInstanceModel *vim = 0; + if (object && (vim = qobject_cast<QQmlInstanceModel *>(object))) { if (d->ownModel) { delete d->model; d->ownModel = false; @@ -639,23 +656,23 @@ void QQuickPathView::setModel(const QVariant &model) d->model = vim; } else { if (!d->ownModel) { - d->model = new QQuickVisualDataModel(qmlContext(this)); + d->model = new QQmlDelegateModel(qmlContext(this)); d->ownModel = true; if (isComponentComplete()) - static_cast<QQuickVisualDataModel *>(d->model.data())->componentComplete(); + static_cast<QQmlDelegateModel *>(d->model.data())->componentComplete(); } - if (QQuickVisualDataModel *dataModel = qobject_cast<QQuickVisualDataModel*>(d->model)) + if (QQmlDelegateModel *dataModel = qobject_cast<QQmlDelegateModel*>(d->model)) dataModel->setModel(model); } int oldModelCount = d->modelCount; d->modelCount = 0; if (d->model) { - qmlobject_connect(d->model, QQuickVisualModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), - this, QQuickPathView, SLOT(modelUpdated(QQuickChangeSet,bool))); - qmlobject_connect(d->model, QQuickVisualModel, SIGNAL(createdItem(int,QQuickItem*)), - this, QQuickPathView, SLOT(createdItem(int,QQuickItem*))); - qmlobject_connect(d->model, QQuickVisualModel, SIGNAL(initItem(int,QQuickItem*)), - this, QQuickPathView, SLOT(initItem(int,QQuickItem*))); + qmlobject_connect(d->model, QQmlInstanceModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), + this, QQuickPathView, SLOT(modelUpdated(QQmlChangeSet,bool))); + qmlobject_connect(d->model, QQmlInstanceModel, SIGNAL(createdItem(int,QObject*)), + this, QQuickPathView, SLOT(createdItem(int,QObject*))); + qmlobject_connect(d->model, QQmlInstanceModel, SIGNAL(initItem(int,QObject*)), + this, QQuickPathView, SLOT(initItem(int,QObject*))); d->modelCount = d->model->count(); } if (isComponentComplete()) { @@ -1210,7 +1227,7 @@ QQmlComponent *QQuickPathView::delegate() const { Q_D(const QQuickPathView); if (d->model) { - if (QQuickVisualDataModel *dataModel = qobject_cast<QQuickVisualDataModel*>(d->model)) + if (QQmlDelegateModel *dataModel = qobject_cast<QQmlDelegateModel*>(d->model)) return dataModel->delegate(); } @@ -1223,10 +1240,10 @@ void QQuickPathView::setDelegate(QQmlComponent *delegate) if (delegate == this->delegate()) return; if (!d->ownModel) { - d->model = new QQuickVisualDataModel(qmlContext(this)); + d->model = new QQmlDelegateModel(qmlContext(this)); d->ownModel = true; } - if (QQuickVisualDataModel *dataModel = qobject_cast<QQuickVisualDataModel*>(d->model)) { + if (QQmlDelegateModel *dataModel = qobject_cast<QQmlDelegateModel*>(d->model)) { int oldCount = dataModel->count(); dataModel->setDelegate(delegate); d->modelCount = dataModel->count(); @@ -1234,6 +1251,7 @@ void QQuickPathView::setDelegate(QQmlComponent *delegate) if (oldCount != dataModel->count()) emit countChanged(); emit delegateChanged(); + d->delegateValidated = false; } } @@ -1821,7 +1839,7 @@ void QQuickPathView::componentComplete() { Q_D(QQuickPathView); if (d->model && d->ownModel) - static_cast<QQuickVisualDataModel *>(d->model.data())->componentComplete(); + static_cast<QQmlDelegateModel *>(d->model.data())->componentComplete(); QQuickItem::componentComplete(); @@ -1983,7 +2001,7 @@ void QQuickPathView::refill() d->releaseItem(d->itemCache.takeLast()); } -void QQuickPathView::modelUpdated(const QQuickChangeSet &changeSet, bool reset) +void QQuickPathView::modelUpdated(const QQmlChangeSet &changeSet, bool reset) { Q_D(QQuickPathView); if (!d->model || !d->model->isValid() || !d->path || !isComponentComplete()) @@ -2004,7 +2022,7 @@ void QQuickPathView::modelUpdated(const QQuickChangeSet &changeSet, bool reset) int moveOffset; bool currentChanged = false; bool changedOffset = false; - foreach (const QQuickChangeSet::Remove &r, changeSet.removes()) { + foreach (const QQmlChangeSet::Remove &r, changeSet.removes()) { if (moveId == -1 && d->currentIndex >= r.index + r.count) { d->currentIndex -= r.count; currentChanged = true; @@ -2030,7 +2048,7 @@ void QQuickPathView::modelUpdated(const QQuickChangeSet &changeSet, bool reset) } d->modelCount -= r.count; } - foreach (const QQuickChangeSet::Insert &i, changeSet.inserts()) { + foreach (const QQmlChangeSet::Insert &i, changeSet.inserts()) { if (d->modelCount) { if (moveId == -1 && i.index <= d->currentIndex) { d->currentIndex += i.count; @@ -2082,7 +2100,7 @@ void QQuickPathView::modelUpdated(const QQuickChangeSet &changeSet, bool reset) emit countChanged(); } -void QQuickPathView::destroyingItem(QQuickItem *item) +void QQuickPathView::destroyingItem(QObject *item) { Q_UNUSED(item); } diff --git a/src/quick/items/qquickpathview_p.h b/src/quick/items/qquickpathview_p.h index 9cb6b881a0..998707aa41 100644 --- a/src/quick/items/qquickpathview_p.h +++ b/src/quick/items/qquickpathview_p.h @@ -46,11 +46,9 @@ #include <private/qquickpath_p.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE -class QQuickChangeSet; +class QQmlChangeSet; class QQuickPathViewPrivate; class QQuickPathViewAttached; @@ -216,10 +214,10 @@ private Q_SLOTS: void refill(); void ticked(); void movementEnding(); - void modelUpdated(const QQuickChangeSet &changeSet, bool reset); - void createdItem(int index, QQuickItem *item); - void initItem(int index, QQuickItem *item); - void destroyingItem(QQuickItem *item); + void modelUpdated(const QQmlChangeSet &changeSet, bool reset); + void createdItem(int index, QObject *item); + void initItem(int index, QObject *item); + void destroyingItem(QObject *item); void pathUpdated(); private: @@ -281,6 +279,5 @@ QT_END_NAMESPACE QML_DECLARE_TYPE(QQuickPathView) QML_DECLARE_TYPEINFO(QQuickPathView, QML_HAS_ATTACHED_PROPERTIES) -QT_END_HEADER #endif // QQUICKPATHVIEW_P_H diff --git a/src/quick/items/qquickpathview_p_p.h b/src/quick/items/qquickpathview_p_p.h index 9f504db22f..26533057d1 100644 --- a/src/quick/items/qquickpathview_p_p.h +++ b/src/quick/items/qquickpathview_p_p.h @@ -55,7 +55,6 @@ #include "qquickpathview_p.h" #include "qquickitem_p.h" -#include "qquickvisualdatamodel_p.h" #include <QtQml/qqml.h> #include <QtCore/qdatetime.h> @@ -63,6 +62,7 @@ #include <private/qquickanimation_p_p.h> #include <private/qqmlguard_p.h> +#include <private/qqmldelegatemodel_p.h> #include <private/qquicktimeline_p_p.h> QT_BEGIN_NAMESPACE @@ -152,6 +152,7 @@ public: bool dragging : 1; bool requestedOnPath : 1; bool inRequest : 1; + bool delegateValidated : 1; QElapsedTimer timer; qint64 lastPosTime; QPointF lastPos; @@ -168,7 +169,7 @@ public: qreal requestedZ; QList<QQuickItem *> items; QList<QQuickItem *> itemCache; - QQmlGuard<QQuickVisualModel> model; + QQmlGuard<QQmlInstanceModel> model; QVariant modelVariant; enum MovementReason { Other, SetIndex, Mouse }; MovementReason moveReason; diff --git a/src/quick/items/qquickpincharea_p.h b/src/quick/items/qquickpincharea_p.h index 2f22b4e4a7..4fc77d7f9c 100644 --- a/src/quick/items/qquickpincharea_p.h +++ b/src/quick/items/qquickpincharea_p.h @@ -44,8 +44,6 @@ #include "qquickitem.h" -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class Q_AUTOTEST_EXPORT QQuickPinch : public QObject @@ -301,7 +299,5 @@ QML_DECLARE_TYPE(QQuickPinch) QML_DECLARE_TYPE(QQuickPinchEvent) QML_DECLARE_TYPE(QQuickPinchArea) -QT_END_HEADER - #endif // QQUICKPINCHAREA_H diff --git a/src/quick/items/qquickpositioners.cpp b/src/quick/items/qquickpositioners.cpp index 2b03ed2dc4..df80e77bfa 100644 --- a/src/quick/items/qquickpositioners.cpp +++ b/src/quick/items/qquickpositioners.cpp @@ -1102,6 +1102,8 @@ QQuickGrid::QQuickGrid(QQuickItem *parent) , m_useRowSpacing(false) , m_useColumnSpacing(false) , m_flow(LeftToRight) + , m_hItemAlign(AlignLeft) + , m_vItemAlign(AlignTop) { } @@ -1248,6 +1250,7 @@ void QQuickGrid::setLayoutDirection(Qt::LayoutDirection layoutDirection) prePositioning(); emit layoutDirectionChanged(); emit effectiveLayoutDirectionChanged(); + emit effectiveHorizontalAlignmentChanged(effectiveHAlign()); } } @@ -1266,6 +1269,97 @@ Qt::LayoutDirection QQuickGrid::effectiveLayoutDirection() const return QQuickBasePositionerPrivate::getEffectiveLayoutDirection(this); } +/*! + \qmlproperty enumeration QtQuick2::Grid::horizontalItmeAlignment + \qmlproperty enumeration QtQuick2::Grid::verticalItemAlignment + \qmlproperty enumeration QtQuick2::Grid::effectiveHorizontalItemAlignment + + Sets the horizontal and vertical alignment of items in the Grid. By default, + the items are vertically aligned to the top. Horizontal + alignment follows the layoutDirection of the Grid, for example when having a layoutDirection + from LeftToRight, the items will be aligned on the left. + + The valid values for \c horizontalItemAlignment are, \c Grid.AlignLeft, \c Grid.AlignRight and + \c Grid.AlignHCenter. + + The valid values for \c verticalItemAlignment are \c Grid.AlignTop, \c Grid.AlignBottom + and \c Grid.AlignVCenter. + + The below images show three examples of how to align items. + + \table + \row + \li + \li \inlineimage gridLayout_aligntopleft.png + \li \inlineimage gridLayout_aligntop.png + \li \inlineimage gridLayout_aligncenter.png + \row + \li Horizontal alignment + \li AlignLeft + \li AlignHCenter + \li AlignHCenter + \row + \li Vertical alignment + \li AlignTop + \li AlignTop + \li AlignVCenter + \endtable + + + When mirroring the layout using either the attached property LayoutMirroring::enabled or + by setting the layoutDirection, the horizontal alignment of items will be mirrored as well. + However, the property \c horizontalItemAlignment will remain unchanged. + To query the effective horizontal alignment of items, use the read-only property + \c effectiveHorizontalItemAlignment. + + \sa Grid::layoutDirection, {LayoutMirroring}{LayoutMirroring} +*/ +QQuickGrid::HAlignment QQuickGrid::hItemAlign() const +{ + return m_hItemAlign; +} +void QQuickGrid::setHItemAlign(HAlignment align) +{ + if (m_hItemAlign != align) { + m_hItemAlign = align; + prePositioning(); + emit horizontalAlignmentChanged(align); + emit effectiveHorizontalAlignmentChanged(effectiveHAlign()); + } +} + +QQuickGrid::HAlignment QQuickGrid::effectiveHAlign() const +{ + HAlignment effectiveAlignment = m_hItemAlign; + if (effectiveLayoutDirection() == Qt::RightToLeft) { + switch (hItemAlign()) { + case AlignLeft: + effectiveAlignment = AlignRight; + break; + case AlignRight: + effectiveAlignment = AlignLeft; + break; + default: + break; + } + } + return effectiveAlignment; +} + + +QQuickGrid::VAlignment QQuickGrid::vItemAlign() const +{ + return m_vItemAlign; +} +void QQuickGrid::setVItemAlign(VAlignment align) +{ + if (m_vItemAlign != align) { + m_vItemAlign = align; + prePositioning(); + emit verticalAlignmentChanged(align); + } +} + void QQuickGrid::doPositioning(QSizeF *contentSize) { //Precondition: All items in the positioned list have a valid item pointer and should be positioned @@ -1362,9 +1456,22 @@ void QQuickGrid::doPositioning(QSizeF *contentSize) for (int i = 0; i < positionedItems.count(); ++i) { PositionedItem &child = positionedItems[i]; qreal childXOffset = xoffset; + + if (effectiveHAlign() == AlignRight) + childXOffset += maxColWidth[curCol] - child.item->width(); + else if (hItemAlign() == AlignHCenter) + childXOffset += (maxColWidth[curCol] - child.item->width())/2.0; + if (!d->isLeftToRight()) - childXOffset -= child.item->width(); - positionItem(childXOffset, yoffset, &child); + childXOffset -= maxColWidth[curCol]; + + qreal alignYOffset = yoffset; + if (m_vItemAlign == AlignVCenter) + alignYOffset += (maxRowHeight[curRow] - child.item->height())/2.0; + else if (m_vItemAlign == AlignBottom) + alignYOffset += maxRowHeight[curRow] - child.item->height(); + + positionItem(childXOffset, alignYOffset, &child); if (m_flow == LeftToRight) { if (d->isLeftToRight()) diff --git a/src/quick/items/qquickpositioners_p.h b/src/quick/items/qquickpositioners_p.h index 784efea510..1ff5a7a74c 100644 --- a/src/quick/items/qquickpositioners_p.h +++ b/src/quick/items/qquickpositioners_p.h @@ -51,8 +51,6 @@ #include <QtCore/qobject.h> #include <QtCore/qstring.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QQuickBasePositionerPrivate; @@ -222,6 +220,9 @@ class Q_AUTOTEST_EXPORT QQuickGrid : public QQuickBasePositioner Q_PROPERTY(Flow flow READ flow WRITE setFlow NOTIFY flowChanged) Q_PROPERTY(Qt::LayoutDirection layoutDirection READ layoutDirection WRITE setLayoutDirection NOTIFY layoutDirectionChanged) Q_PROPERTY(Qt::LayoutDirection effectiveLayoutDirection READ effectiveLayoutDirection NOTIFY effectiveLayoutDirectionChanged) + Q_PROPERTY(HAlignment horizontalItemAlignment READ hItemAlign WRITE setHItemAlign NOTIFY horizontalAlignmentChanged REVISION 1) + Q_PROPERTY(HAlignment effectiveHorizontalItemAlignment READ effectiveHAlign NOTIFY effectiveHorizontalAlignmentChanged REVISION 1) + Q_PROPERTY(VAlignment verticalItemAlignment READ vItemAlign WRITE setVItemAlign NOTIFY verticalAlignmentChanged REVISION 1) public: QQuickGrid(QQuickItem *parent=0); @@ -249,6 +250,22 @@ public: void setLayoutDirection (Qt::LayoutDirection); Qt::LayoutDirection effectiveLayoutDirection() const; + Q_ENUMS(HAlignment) + Q_ENUMS(VAlignment) + enum HAlignment { AlignLeft = Qt::AlignLeft, + AlignRight = Qt::AlignRight, + AlignHCenter = Qt::AlignHCenter}; + enum VAlignment { AlignTop = Qt::AlignTop, + AlignBottom = Qt::AlignBottom, + AlignVCenter = Qt::AlignVCenter }; + + HAlignment hItemAlign() const; + void setHItemAlign(HAlignment align); + HAlignment effectiveHAlign() const; + + VAlignment vItemAlign() const; + void setVItemAlign(VAlignment align); + Q_SIGNALS: void rowsChanged(); void columnsChanged(); @@ -257,6 +274,9 @@ Q_SIGNALS: void effectiveLayoutDirectionChanged(); void rowSpacingChanged(); void columnSpacingChanged(); + Q_REVISION(1) void horizontalAlignmentChanged(HAlignment alignment); + Q_REVISION(1) void effectiveHorizontalAlignmentChanged(HAlignment alignment); + Q_REVISION(1) void verticalAlignmentChanged(VAlignment alignment); protected: virtual void doPositioning(QSizeF *contentSize); @@ -270,6 +290,8 @@ private: bool m_useRowSpacing; bool m_useColumnSpacing; Flow m_flow; + HAlignment m_hItemAlign; + VAlignment m_vItemAlign; Q_DISABLE_COPY(QQuickGrid) }; @@ -318,6 +340,4 @@ QML_DECLARE_TYPE(QQuickFlow) QML_DECLARE_TYPE(QQuickBasePositioner) QML_DECLARE_TYPEINFO(QQuickBasePositioner, QML_HAS_ATTACHED_PROPERTIES) -QT_END_HEADER - #endif // QQUICKPOSITIONERS_P_H diff --git a/src/quick/items/qquickrectangle_p.h b/src/quick/items/qquickrectangle_p.h index 911c3fdf8b..2ad144aafd 100644 --- a/src/quick/items/qquickrectangle_p.h +++ b/src/quick/items/qquickrectangle_p.h @@ -48,8 +48,6 @@ #include <private/qtquickglobal_p.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class Q_AUTOTEST_EXPORT QQuickPen : public QObject @@ -178,6 +176,4 @@ QML_DECLARE_TYPE(QQuickGradientStop) QML_DECLARE_TYPE(QQuickGradient) QML_DECLARE_TYPE(QQuickRectangle) -QT_END_HEADER - #endif // QQUICKRECTANGLE_P_H diff --git a/src/quick/items/qquickrepeater.cpp b/src/quick/items/qquickrepeater.cpp index 4c43b4bf72..35e37d1246 100644 --- a/src/quick/items/qquickrepeater.cpp +++ b/src/quick/items/qquickrepeater.cpp @@ -41,16 +41,18 @@ #include "qquickrepeater_p.h" #include "qquickrepeater_p_p.h" -#include "qquickvisualdatamodel_p.h" #include <private/qqmlglobal_p.h> -#include <private/qquicklistaccessor_p.h> -#include <private/qquickchangeset_p.h> +#include <private/qqmllistaccessor_p.h> +#include <private/qqmlchangeset_p.h> +#include <private/qqmldelegatemodel_p.h> + +#include <QtQml/QQmlInfo> QT_BEGIN_NAMESPACE QQuickRepeaterPrivate::QQuickRepeaterPrivate() - : model(0), ownModel(false), inRequest(false), dataSourceIsObject(false), itemCount(0), createFrom(-1) + : model(0), ownModel(false), inRequest(false), dataSourceIsObject(false), delegateValidated(false), itemCount(0), createFrom(-1) { } @@ -193,18 +195,18 @@ void QQuickRepeater::setModel(const QVariant &model) clear(); if (d->model) { - disconnect(d->model, SIGNAL(modelUpdated(QQuickChangeSet,bool)), - this, SLOT(modelUpdated(QQuickChangeSet,bool))); - disconnect(d->model, SIGNAL(createdItem(int,QQuickItem*)), this, SLOT(createdItem(int,QQuickItem*))); - disconnect(d->model, SIGNAL(initItem(int,QQuickItem*)), this, SLOT(initItem(int,QQuickItem*))); -// disconnect(d->model, SIGNAL(destroyingItem(QQuickItem*)), this, SLOT(destroyingItem(QQuickItem*))); + disconnect(d->model, SIGNAL(modelUpdated(QQmlChangeSet,bool)), + this, SLOT(modelUpdated(QQmlChangeSet,bool))); + disconnect(d->model, SIGNAL(createdItem(int,QObject*)), this, SLOT(createdItem(int,QObject*))); + disconnect(d->model, SIGNAL(initItem(int,QObject*)), this, SLOT(initItem(int,QObject*))); +// disconnect(d->model, SIGNAL(destroyingItem(QObject*)), this, SLOT(destroyingItem(QObject*))); } d->dataSource = model; QObject *object = qvariant_cast<QObject*>(model); d->dataSourceAsObject = object; d->dataSourceIsObject = object != 0; - QQuickVisualModel *vim = 0; - if (object && (vim = qobject_cast<QQuickVisualModel *>(object))) { + QQmlInstanceModel *vim = 0; + if (object && (vim = qobject_cast<QQmlInstanceModel *>(object))) { if (d->ownModel) { delete d->model; d->ownModel = false; @@ -212,20 +214,20 @@ void QQuickRepeater::setModel(const QVariant &model) d->model = vim; } else { if (!d->ownModel) { - d->model = new QQuickVisualDataModel(qmlContext(this)); + d->model = new QQmlDelegateModel(qmlContext(this)); d->ownModel = true; if (isComponentComplete()) - static_cast<QQuickVisualDataModel *>(d->model)->componentComplete(); + static_cast<QQmlDelegateModel *>(d->model)->componentComplete(); } - if (QQuickVisualDataModel *dataModel = qobject_cast<QQuickVisualDataModel*>(d->model)) + if (QQmlDelegateModel *dataModel = qobject_cast<QQmlDelegateModel*>(d->model)) dataModel->setModel(model); } if (d->model) { - connect(d->model, SIGNAL(modelUpdated(QQuickChangeSet,bool)), - this, SLOT(modelUpdated(QQuickChangeSet,bool))); - connect(d->model, SIGNAL(createdItem(int,QQuickItem*)), this, SLOT(createdItem(int,QQuickItem*))); - connect(d->model, SIGNAL(initItem(int,QQuickItem*)), this, SLOT(initItem(int,QQuickItem*))); -// connect(d->model, SIGNAL(destroyingItem(QQuickItem*)), this, SLOT(destroyingItem(QQuickItem*))); + connect(d->model, SIGNAL(modelUpdated(QQmlChangeSet,bool)), + this, SLOT(modelUpdated(QQmlChangeSet,bool))); + connect(d->model, SIGNAL(createdItem(int,QObject*)), this, SLOT(createdItem(int,QObject*))); + connect(d->model, SIGNAL(initItem(int,QObject*)), this, SLOT(initItem(int,QObject*))); +// connect(d->model, SIGNAL(destroyingItem(QObject*)), this, SLOT(destroyingItem(QObject*))); regenerate(); } emit modelChanged(); @@ -269,7 +271,7 @@ QQmlComponent *QQuickRepeater::delegate() const { Q_D(const QQuickRepeater); if (d->model) { - if (QQuickVisualDataModel *dataModel = qobject_cast<QQuickVisualDataModel*>(d->model)) + if (QQmlDelegateModel *dataModel = qobject_cast<QQmlDelegateModel*>(d->model)) return dataModel->delegate(); } @@ -279,18 +281,20 @@ QQmlComponent *QQuickRepeater::delegate() const void QQuickRepeater::setDelegate(QQmlComponent *delegate) { Q_D(QQuickRepeater); - if (QQuickVisualDataModel *dataModel = qobject_cast<QQuickVisualDataModel*>(d->model)) + if (QQmlDelegateModel *dataModel = qobject_cast<QQmlDelegateModel*>(d->model)) if (delegate == dataModel->delegate()) return; if (!d->ownModel) { - d->model = new QQuickVisualDataModel(qmlContext(this)); + d->model = new QQmlDelegateModel(qmlContext(this)); d->ownModel = true; } - if (QQuickVisualDataModel *dataModel = qobject_cast<QQuickVisualDataModel*>(d->model)) { + + if (QQmlDelegateModel *dataModel = qobject_cast<QQmlDelegateModel*>(d->model)) { dataModel->setDelegate(delegate); regenerate(); emit delegateChanged(); + d->delegateValidated = false; } } @@ -325,7 +329,7 @@ void QQuickRepeater::componentComplete() { Q_D(QQuickRepeater); if (d->model && d->ownModel) - static_cast<QQuickVisualDataModel *>(d->model)->componentComplete(); + static_cast<QQmlDelegateModel *>(d->model)->componentComplete(); QQuickItem::componentComplete(); regenerate(); if (d->model && d->model->count()) @@ -350,6 +354,7 @@ void QQuickRepeater::clear() QQuickItem *item = d->deletables.at(i); if (complete) emit itemRemoved(i, item); + item->setParentItem(0); d->model->release(item); } } @@ -382,8 +387,17 @@ void QQuickRepeaterPrivate::createItems() inRequest = true; for (int ii = createFrom; ii < itemCount; ++ii) { if (!deletables.at(ii)) { - QQuickItem *item = model->item(ii, false); + QObject *object = model->object(ii, false); + QQuickItem *item = qmlobject_cast<QQuickItem*>(object); if (!item) { + if (object) { + model->release(object); + if (!delegateValidated) { + delegateValidated = true; + QObject* delegate = q->delegate(); + qmlInfo(delegate ? delegate : q) << q->tr("Delegate must be of Item type"); + } + } createFrom = ii; break; } @@ -407,19 +421,21 @@ void QQuickRepeaterPrivate::createItems() inRequest = false; } -void QQuickRepeater::createdItem(int, QQuickItem *) +void QQuickRepeater::createdItem(int, QObject *) { Q_D(QQuickRepeater); if (!d->inRequest) d->createItems(); } -void QQuickRepeater::initItem(int, QQuickItem *item) +void QQuickRepeater::initItem(int, QObject *object) { - item->setParentItem(parentItem()); + QQuickItem *item = qmlobject_cast<QQuickItem*>(object); + if (item) + item->setParentItem(parentItem()); } -void QQuickRepeater::modelUpdated(const QQuickChangeSet &changeSet, bool reset) +void QQuickRepeater::modelUpdated(const QQmlChangeSet &changeSet, bool reset) { Q_D(QQuickRepeater); @@ -435,7 +451,7 @@ void QQuickRepeater::modelUpdated(const QQuickChangeSet &changeSet, bool reset) int difference = 0; QHash<int, QVector<QPointer<QQuickItem> > > moved; - foreach (const QQuickChangeSet::Remove &remove, changeSet.removes()) { + foreach (const QQmlChangeSet::Remove &remove, changeSet.removes()) { int index = qMin(remove.index, d->deletables.count()); int count = qMin(remove.index + remove.count, d->deletables.count()) - index; if (remove.isMove()) { @@ -447,8 +463,10 @@ void QQuickRepeater::modelUpdated(const QQuickChangeSet &changeSet, bool reset) QQuickItem *item = d->deletables.at(index); d->deletables.remove(index); emit itemRemoved(index, item); - if (item) + if (item) { + item->setParentItem(0); d->model->release(item); + } --d->itemCount; } @@ -456,7 +474,7 @@ void QQuickRepeater::modelUpdated(const QQuickChangeSet &changeSet, bool reset) } d->createFrom = -1; - foreach (const QQuickChangeSet::Insert &insert, changeSet.inserts()) { + foreach (const QQmlChangeSet::Insert &insert, changeSet.inserts()) { int index = qMin(insert.index, d->deletables.count()); if (insert.isMove()) { QVector<QPointer<QQuickItem> > items = moved.value(insert.moveId); diff --git a/src/quick/items/qquickrepeater_p.h b/src/quick/items/qquickrepeater_p.h index 17153694c9..be9ed7266d 100644 --- a/src/quick/items/qquickrepeater_p.h +++ b/src/quick/items/qquickrepeater_p.h @@ -44,11 +44,9 @@ #include "qquickitem.h" -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE -class QQuickChangeSet; +class QQmlChangeSet; class QQuickRepeaterPrivate; class Q_AUTOTEST_EXPORT QQuickRepeater : public QQuickItem @@ -91,9 +89,9 @@ protected: void itemChange(ItemChange change, const ItemChangeData &value); private Q_SLOTS: - void createdItem(int index, QQuickItem *item); - void initItem(int, QQuickItem *item); - void modelUpdated(const QQuickChangeSet &changeSet, bool reset); + void createdItem(int index, QObject *item); + void initItem(int, QObject *item); + void modelUpdated(const QQmlChangeSet &changeSet, bool reset); private: Q_DISABLE_COPY(QQuickRepeater) @@ -104,6 +102,4 @@ QT_END_NAMESPACE QML_DECLARE_TYPE(QQuickRepeater) -QT_END_HEADER - #endif // QQUICKREPEATER_P_H diff --git a/src/quick/items/qquickrepeater_p_p.h b/src/quick/items/qquickrepeater_p_p.h index a187aea39c..f220eb4fcb 100644 --- a/src/quick/items/qquickrepeater_p_p.h +++ b/src/quick/items/qquickrepeater_p_p.h @@ -61,7 +61,7 @@ QT_BEGIN_NAMESPACE class QQmlContext; -class QQuickVisualModel; +class QQmlInstanceModel; class QQuickRepeaterPrivate : public QQuickItemPrivate { Q_DECLARE_PUBLIC(QQuickRepeater) @@ -73,12 +73,13 @@ public: private: void createItems(); - QQuickVisualModel *model; + QQmlInstanceModel *model; QVariant dataSource; QQmlGuard<QObject> dataSourceAsObject; bool ownModel : 1; bool inRequest : 1; bool dataSourceIsObject : 1; + bool delegateValidated : 1; int itemCount; int createFrom; diff --git a/src/quick/items/qquickscalegrid_p_p.h b/src/quick/items/qquickscalegrid_p_p.h index b10785f60e..264c444229 100644 --- a/src/quick/items/qquickscalegrid_p_p.h +++ b/src/quick/items/qquickscalegrid_p_p.h @@ -50,8 +50,6 @@ #include <QtQuick/private/qquickpixmapcache_p.h> #include <private/qtquickglobal_p.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QQuickScaleGrid : public QObject @@ -126,6 +124,4 @@ QT_END_NAMESPACE QML_DECLARE_TYPE(QQuickScaleGrid) -QT_END_HEADER - #endif // QQUICKSCALEGRID_P_P_H diff --git a/src/quick/items/qquickscreen_p.h b/src/quick/items/qquickscreen_p.h index 014fc0709b..98f38b7154 100644 --- a/src/quick/items/qquickscreen_p.h +++ b/src/quick/items/qquickscreen_p.h @@ -47,8 +47,6 @@ #include <QSize> #include <private/qqmlglobal_p.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE @@ -104,6 +102,4 @@ QT_END_NAMESPACE QML_DECLARE_TYPEINFO(QQuickScreen, QML_HAS_ATTACHED_PROPERTIES) -QT_END_HEADER - #endif diff --git a/src/quick/items/qquickshadereffect_p.h b/src/quick/items/qquickshadereffect_p.h index 6abb41b737..5c3dcf4e41 100644 --- a/src/quick/items/qquickshadereffect_p.h +++ b/src/quick/items/qquickshadereffect_p.h @@ -52,8 +52,6 @@ #include <QtCore/qpointer.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE const char *qtPositionAttributeName(); @@ -193,6 +191,4 @@ private: QT_END_NAMESPACE -QT_END_HEADER - #endif // QQUICKSHADEREFFECT_P_H diff --git a/src/quick/items/qquickshadereffectmesh_p.h b/src/quick/items/qquickshadereffectmesh_p.h index 15f4d02c12..9c19b8e6e6 100644 --- a/src/quick/items/qquickshadereffectmesh_p.h +++ b/src/quick/items/qquickshadereffectmesh_p.h @@ -51,8 +51,6 @@ #ifndef QQUICKSHADEREFFECTMESH_P_H #define QQUICKSHADEREFFECTMESH_P_H -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QSGGeometry; @@ -101,6 +99,4 @@ inline QColor qt_premultiply_color(const QColor &c) QT_END_NAMESPACE -QT_END_HEADER - #endif // QQUICKSHADEREFFECTMESH_P_H diff --git a/src/quick/items/qquickshadereffectnode_p.h b/src/quick/items/qquickshadereffectnode_p.h index db21d06f58..a6854526fa 100644 --- a/src/quick/items/qquickshadereffectnode_p.h +++ b/src/quick/items/qquickshadereffectnode_p.h @@ -51,8 +51,6 @@ #include <QtCore/qsharedpointer.h> #include <QtCore/qpointer.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE struct QQuickShaderEffectMaterialKey { @@ -147,6 +145,4 @@ private Q_SLOTS: QT_END_NAMESPACE -QT_END_HEADER - #endif // QQUICKSHADEREFFECTNODE_P_H diff --git a/src/quick/items/qquickshadereffectsource_p.h b/src/quick/items/qquickshadereffectsource_p.h index 5c9427adde..57aba314aa 100644 --- a/src/quick/items/qquickshadereffectsource_p.h +++ b/src/quick/items/qquickshadereffectsource_p.h @@ -55,8 +55,6 @@ #define QSG_DEBUG_FBO_OVERLAY -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QSGNode; @@ -257,6 +255,4 @@ private: QT_END_NAMESPACE -QT_END_HEADER - #endif // QQUICKSHADEREFFECTSOURCE_P_H diff --git a/src/quick/items/qquicksprite_p.h b/src/quick/items/qquicksprite_p.h index dce28e888b..a85059e8d6 100644 --- a/src/quick/items/qquicksprite_p.h +++ b/src/quick/items/qquicksprite_p.h @@ -50,8 +50,6 @@ #include "qquickspriteengine_p.h" #include <QDebug> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QQuickSprite : public QQuickStochasticState @@ -316,5 +314,4 @@ private: }; QT_END_NAMESPACE -QT_END_HEADER #endif // QQUICKSPRITE_P_H diff --git a/src/quick/items/qquickspriteengine.cpp b/src/quick/items/qquickspriteengine.cpp index 6dc4cf4436..4c7be3bce9 100644 --- a/src/quick/items/qquickspriteengine.cpp +++ b/src/quick/items/qquickspriteengine.cpp @@ -429,7 +429,7 @@ QImage QQuickSpriteEngine::assembledImage() } //maxFrames is max number in a line of the texture - QImage image(w, h, QImage::Format_ARGB32); + QImage image(w, h, QImage::Format_ARGB32_Premultiplied); image.fill(0); QPainter p(&image); int y = 0; diff --git a/src/quick/items/qquickspriteengine_p.h b/src/quick/items/qquickspriteengine_p.h index 612a2b4c2d..768b5e4a6e 100644 --- a/src/quick/items/qquickspriteengine_p.h +++ b/src/quick/items/qquickspriteengine_p.h @@ -53,8 +53,6 @@ #include <private/qquickpixmapcache_p.h> #include <private/qtquickglobal_p.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QQuickSprite; @@ -323,6 +321,4 @@ inline int spriteCount(QQmlListProperty<QQuickSprite> *p) QT_END_NAMESPACE -QT_END_HEADER - #endif // QQUICKSPRITEENGINE_P_H diff --git a/src/quick/items/qquickspritesequence_p.h b/src/quick/items/qquickspritesequence_p.h index b07d177745..b954a4464f 100644 --- a/src/quick/items/qquickspritesequence_p.h +++ b/src/quick/items/qquickspritesequence_p.h @@ -45,8 +45,6 @@ #include <QtQuick/QQuickItem> #include <QTime> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QSGContext; @@ -145,6 +143,4 @@ private: QT_END_NAMESPACE -QT_END_HEADER - #endif // QQUICKSPRITESEQUENCE_P_H diff --git a/src/quick/items/qquickstateoperations_p.h b/src/quick/items/qquickstateoperations_p.h index d43420f266..9ee0d591da 100644 --- a/src/quick/items/qquickstateoperations_p.h +++ b/src/quick/items/qquickstateoperations_p.h @@ -49,8 +49,6 @@ #include <QtQml/qqmlscriptstring.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QQuickParentChangePrivate; @@ -213,7 +211,5 @@ QML_DECLARE_TYPE(QQuickParentChange) QML_DECLARE_TYPE(QQuickAnchorSet) QML_DECLARE_TYPE(QQuickAnchorChanges) -QT_END_HEADER - #endif // QQUICKSTATEOPERATIONS_P_H diff --git a/src/quick/items/qquicktext_p.h b/src/quick/items/qquicktext_p.h index ba0260c5c7..03b436b3fb 100644 --- a/src/quick/items/qquicktext_p.h +++ b/src/quick/items/qquicktext_p.h @@ -46,8 +46,6 @@ #include <private/qtquickglobal_p.h> #include <QtGui/qtextoption.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QQuickTextPrivate; @@ -295,6 +293,4 @@ QT_END_NAMESPACE QML_DECLARE_TYPE(QQuickText) QML_DECLARE_TYPE(QQuickTextLine) -QT_END_HEADER - #endif // QQUICKTEXT_P_H diff --git a/src/quick/items/qquicktextcontrol_p.h b/src/quick/items/qquicktextcontrol_p.h index 84d6fcce52..94eae81f4e 100644 --- a/src/quick/items/qquicktextcontrol_p.h +++ b/src/quick/items/qquicktextcontrol_p.h @@ -63,8 +63,6 @@ #include <QtGui/qclipboard.h> #include <QtCore/qmimedata.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE @@ -196,6 +194,4 @@ private: QT_END_NAMESPACE -QT_END_HEADER - #endif // QQuickTextControl_H diff --git a/src/quick/items/qquicktextedit_p.h b/src/quick/items/qquicktextedit_p.h index e88ca16c0e..0538270cd4 100644 --- a/src/quick/items/qquicktextedit_p.h +++ b/src/quick/items/qquicktextedit_p.h @@ -46,8 +46,6 @@ #include <QtGui/qtextoption.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QQuickTextEditPrivate; @@ -351,6 +349,4 @@ QT_END_NAMESPACE QML_DECLARE_TYPE(QQuickTextEdit) -QT_END_HEADER - #endif // QQUICKTEXTEDIT_P_H diff --git a/src/quick/items/qquicktextinput_p.h b/src/quick/items/qquicktextinput_p.h index 83bc125dee..602e9dc87b 100644 --- a/src/quick/items/qquicktextinput_p.h +++ b/src/quick/items/qquicktextinput_p.h @@ -46,8 +46,6 @@ #include <QtGui/qtextoption.h> #include <QtGui/qvalidator.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QQuickTextInputPrivate; @@ -415,6 +413,4 @@ QML_DECLARE_TYPE(QQuickDoubleValidator) QML_DECLARE_TYPE(QRegExpValidator) #endif -QT_END_HEADER - #endif // QQUICKTEXTINPUT_P_H diff --git a/src/quick/items/qquickthreadedwindowmanager.cpp b/src/quick/items/qquickthreadedwindowmanager.cpp deleted file mode 100644 index 6c7b9c0448..0000000000 --- a/src/quick/items/qquickthreadedwindowmanager.cpp +++ /dev/null @@ -1,910 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal -** -** This file is part of the QtQml module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "qquickwindowmanager_p.h" -#include "qquickthreadedwindowmanager_p.h" - -#include <QtCore/QTime> -#include <QtCore/QDebug> - -#include <QtGui/QOpenGLContext> -#include <QtGui/private/qguiapplication_p.h> -#include <qpa/qplatformintegration.h> - -#include <QtQml/private/qqmlglobal_p.h> - -#include <QtQuick/QQuickWindow> -#include <QtQuick/private/qquickwindow_p.h> - -QT_BEGIN_NAMESPACE - -//#define THREAD_DEBUG -extern Q_GUI_EXPORT QImage qt_gl_read_framebuffer(const QSize &size, bool alpha_format, bool include_alpha); - -const QEvent::Type QEvent_Sync = QEvent::Type(QEvent::User); -const QEvent::Type QEvent_DeferredUpdate = QEvent::Type(QEvent::User + 1); - -#define QQUICK_RENDER_TIMING -#ifdef QQUICK_RENDER_TIMING -DEFINE_BOOL_CONFIG_OPTION(qquick_render_timing, QML_RENDER_TIMING) -static QTime threadTimer; -static int syncTime; -static int renderTime; -static int swapTime; -#endif - - -/* - Threaded Rendering - ================== - - The threaded rendering uses a number of different variables to track potential - states used to handle resizing, initial paint, grabbing and driving animations - while ALWAYS keeping the GL context in the rendering thread and keeping the - overhead of normal one-shot paints and vblank driven animations at a minimum. - - Resize, initial show and grab suffer slightly in this model as they are locked - to the rendering in the rendering thread, but this is a necessary evil for - the system to work. - - Variables that are used: - - Private::animationRunning: This is true while the animations are running, and only - written to inside locks. - - RenderThread::isGuiLocked: This is used to indicate that the GUI thread owns the - lock. This variable is an integer to allow for recursive calls to lockInGui() - without using a recursive mutex. See isPostingSyncEvent. - - RenderThread::isPostingSyncEvent: This variable is set in the render thread just - before the sync event is sent to the GUI thread. It is used to avoid deadlocks - in the case where render thread waits while waiting for GUI to pick up the sync - event and GUI thread gets a resizeEvent, the initial paintEvent or a grab. - When this happens, we use the - exhaustSyncEvent() function to do the sync right there and mark the coming - sync event to be discarded. There can only ever be one sync incoming. - - RenderThread::isRenderBlock: This variable is true when animations are not - running and the render thread has gone to sleep, waiting for more to do. - - RenderThread::isExternalUpdatePending: This variable is set to false when - a new render pass is started and to true in maybeUpdate(). It is an - indication to the render thread that another render pass needs to take - place, rather than the render thread going to sleep after completing its swap. - - RenderThread::doGrab: This variable is set by the grab() function and - tells the renderer to do a grab after rendering is complete and before - swapping happens. - - RenderThread::shouldExit: This variable is used to determine if the render - thread should do a nother pass. It is typically set as a result of show() - and unset as a result of hide() or during shutdown() - - RenderThread::hasExited: Used by the GUI thread to synchronize the shutdown - after shouldExit has been set to true. - */ - - -void QQuickRenderThreadSingleContextWindowManager::initialize() -{ - Q_ASSERT(m_rendered_windows.size()); - - QQuickWindow *win = 0; - for (QHash<QQuickWindow *, WindowData *>::const_iterator it = m_rendered_windows.constBegin(); - it != m_rendered_windows.constEnd() && !win; ++it) { - if (QQuickWindowPrivate::get(it.key())->isRenderable()) - win = it.key(); - } - if (!win) - return; - - gl = new QOpenGLContext(); - // Pick up the surface format from one of them - gl->setFormat(win->requestedFormat()); - gl->create(); - if (!gl->makeCurrent(win)) - qWarning("QQuickWindow: makeCurrent() failed..."); - - Q_ASSERT(!sg->isReady()); - sg->initialize(gl); -} - - -/*! - This function is called when the window is created to register the window with - the window manager. - - Called on GUI Thread. - */ - -void QQuickRenderThreadSingleContextWindowManager::show(QQuickWindow *window) -{ -#ifdef THREAD_DEBUG - printf("GUI: Window added to windowing system, %p, %dx%d\n", window, window->width(), window->height()); -#endif - - WindowTracker tracker; - tracker.window = window; - tracker.isVisible = false; - tracker.toBeRemoved = false; - m_tracked_windows << tracker; - - connect(window, SIGNAL(widthChanged(int)), this, SLOT(windowVisibilityChanged()), Qt::DirectConnection); - connect(window, SIGNAL(heightChanged(int)), this, SLOT(windowVisibilityChanged()), Qt::DirectConnection); - - windowVisibilityChanged(); -} - - -void QQuickRenderThreadSingleContextWindowManager::handleAddedWindow(QQuickWindow *window) -{ -#ifdef THREAD_DEBUG - printf(" RenderThread: adding window: %p\n", window); -#endif - - WindowData *data = new WindowData; - data->sizeWasChanged = false; - data->windowSize = window->size(); - data->isVisible = window->isVisible(); - data->isRenderable = QQuickWindowPrivate::get(window)->isRenderable(); - m_rendered_windows[window] = data; - - isExternalUpdatePending = true; -} - - -/*! - Called on Render Thread - */ -void QQuickRenderThreadSingleContextWindowManager::handleAddedWindows() -{ -#ifdef THREAD_DEBUG - printf(" RenderThread: about to add %d\n", m_added_windows.size()); -#endif - - while (m_added_windows.size()) { - QQuickWindow *window = m_added_windows.takeLast(); - handleAddedWindow(window); - } -} - - -/*! - Called on the GUI Thread, from the window' destructor - */ - -void QQuickRenderThreadSingleContextWindowManager::windowDestroyed(QQuickWindow *window) -{ -#ifdef THREAD_DEBUG - printf("GUI: Window destroyed: %p\n", window); -#endif - - hide(window); -} - - -/*! - Called on GUI Thread - */ - -void QQuickRenderThreadSingleContextWindowManager::hide(QQuickWindow *window) -{ -#ifdef THREAD_DEBUG - printf("GUI: Window hidden: %p\n", window); -#endif - - int position = -1; - for (int i=0; i<m_tracked_windows.size(); ++i) { - if (m_tracked_windows.at(i).window == window) { - m_tracked_windows[i].toBeRemoved = true; - position = i; - break; - } - } - - if (position >= 0) { - disconnect(window, SIGNAL(widthChanged(int)), this, SLOT(windowVisibilityChanged())); - disconnect(window, SIGNAL(heightChanged(int)), this, SLOT(windowVisibilityChanged())); - windowVisibilityChanged(); - m_tracked_windows.removeAt(position); - } - -#ifdef THREAD_DEBUG - printf("GUI: Window removal completed... %p\n", window); -#endif -} - -/*! - Called on Render Thread - */ -void QQuickRenderThreadSingleContextWindowManager::handleRemovedWindows(bool clearGLContext) -{ -#ifdef THREAD_DEBUG - printf(" RenderThread: about to remove %d\n", m_removed_windows.size()); -#endif - - bool removedAnything = false; - while (m_removed_windows.size()) { - QQuickWindow *window = m_removed_windows.takeLast(); -#ifdef THREAD_DEBUG - printf(" RenderThread: removing %p\n", window); -#endif - - QQuickWindowPrivate::get(window)->cleanupNodesOnShutdown(); - delete m_rendered_windows.take(window); - removedAnything = true; - } - - // If a window is removed because it has been hidden it will take with it - // the gl context (at least on Mac) if bound, so disconnect the gl context - // from anything - if (removedAnything && clearGLContext) - gl->doneCurrent(); -} - - - -/*! - Called on GUI Thread - */ - -void QQuickRenderThreadSingleContextWindowManager::windowVisibilityChanged() -{ - bool anyoneShowing = false; - QList<QQuickWindow *> toAdd, toRemove; - - // Not optimal, but also not frequently used... - for (int i=0; i<m_tracked_windows.size(); ++i) { - WindowTracker &t = const_cast<WindowTracker &>(m_tracked_windows.at(i)); - QQuickWindow *win = t.window; - - Q_ASSERT(win->isVisible() || QQuickWindowPrivate::get(win)->renderWithoutShowing || t.toBeRemoved); - bool windowVisible = win->width() > 0 && win->height() > 0; - anyoneShowing |= (windowVisible && !t.toBeRemoved); - - if ((!windowVisible && t.isVisible) || t.toBeRemoved) { - toRemove << win; - } else if (windowVisible && !t.isVisible) { - toAdd << win; - } - t.isVisible = windowVisible; - } - - if (isRunning()) { - if (!anyoneShowing) { - stopRendering(); - } else { - lockInGui(); - exhaustSyncEvent(); - m_added_windows << toAdd; - m_removed_windows << toRemove; - while (isRunning() && (m_added_windows.size() || m_removed_windows.size())) { - if (isRenderBlocked) - wake(); - wait(); - } - unlockInGui(); - } - - } else if (anyoneShowing) { - Q_ASSERT(toRemove.isEmpty()); // since loop is not running, nothing is showing now - for (int i=0; i<toAdd.size(); ++i) - handleAddedWindow(toAdd.at(i)); - startRendering(); - } - -} - - -void QQuickRenderThreadSingleContextWindowManager::run() -{ -#ifdef THREAD_DEBUG - printf("QML Rendering Thread Started\n"); -#endif - - lock(); - Q_ASSERT(!gl); - initialize(); - // Wake GUI as it is waiting for the GL context to have appeared, as - // an indication that the render thread is now running. - wake(); - unlock(); - - if (!gl) - return; - - while (!shouldExit) { - lock(); - -#ifdef THREAD_DEBUG - printf(" RenderThread: *** NEW FRAME ***\n"); -#endif - - isExternalUpdatePending = false; - handleAddedWindows(); - - if (!isGuiLocked) { - isPostingSyncEvent = true; - -#ifdef THREAD_DEBUG - printf(" RenderThread: acquired sync lock...\n"); -#endif - QCoreApplication::postEvent(this, new QEvent(QEvent_Sync)); - -#ifdef THREAD_DEBUG - printf(" RenderThread: going to sleep...\n"); -#endif - wake(); // In case the event got through all the way to wait() before this thread got to wait. - wait(); - - - isPostingSyncEvent = false; - } - -#ifdef THREAD_DEBUG - printf(" RenderThread: Doing locked sync\n"); -#endif -#ifdef QQUICK_RENDER_TIMING - if (qquick_render_timing()) - threadTimer.start(); -#endif - inSync = true; - for (QHash<QQuickWindow *, WindowData *>::const_iterator it = m_rendered_windows.constBegin(); - it != m_rendered_windows.constEnd(); ++it) { - QQuickWindow *window = it.key(); - -#ifdef THREAD_DEBUG - printf(" RenderThread: Syncing window: %p\n", window); -#endif - - WindowData *windowData = it.value(); - QQuickWindowPrivate *windowPrivate = QQuickWindowPrivate::get(window); - - windowData->isRenderable = windowPrivate->isRenderable(); - - if (windowData->isRenderable) { - gl->makeCurrent(window); - - if (windowData->viewportSize != windowData->windowSize) { -#ifdef THREAD_DEBUG - printf(" RenderThread: --- window has changed size...\n"); -#endif - windowData->viewportSize = windowData->windowSize; - windowData->sizeWasChanged = true; - glViewport(0, 0, windowData->viewportSize.width(), windowData->viewportSize.height()); - } - - windowPrivate->syncSceneGraph(); - } - } - inSync = false; - - // Wake GUI after sync to let it continue animating and event processing. - wake(); - unlock(); -#ifdef THREAD_DEBUG - printf(" RenderThread: sync done\n"); -#endif -#ifdef QQUICK_RENDER_TIMING - if (qquick_render_timing()) - syncTime = threadTimer.elapsed(); -#endif - - for (QHash<QQuickWindow *, WindowData *>::const_iterator it = m_rendered_windows.constBegin(); - it != m_rendered_windows.constEnd(); ++it) { - QQuickWindow *window = it.key(); - WindowData *windowData = it.value(); - QQuickWindowPrivate *windowPrivate = QQuickWindowPrivate::get(window); - - if (!windowData->isRenderable) - continue; - -#ifdef THREAD_DEBUG - printf(" RenderThread: Rendering window %p\n", window); -#endif - - Q_ASSERT(windowData->windowSize.width() > 0 && windowData->windowSize.height() > 0); - -#ifdef THREAD_DEBUG - printf(" RenderThread: --- rendering at size %dx%d\n", - windowData->viewportSize.width(), windowData->viewportSize.height() - ); -#endif - - // We only need to re-makeCurrent when we have multiple surfaces. - if (m_rendered_windows.size() > 1) - gl->makeCurrent(window); - - windowPrivate->renderSceneGraph(windowData->viewportSize); -#ifdef QQUICK_RENDER_TIMING - if (qquick_render_timing()) - renderTime = threadTimer.elapsed() - syncTime; -#endif - - // The content of the target buffer is undefined after swap() so grab needs - // to happen before swap(); - if (window == windowToGrab) { -#ifdef THREAD_DEBUG - printf(" RenderThread: --- grabbing...\n"); -#endif - grabContent = qt_gl_read_framebuffer(windowData->windowSize, false, false); - windowToGrab = 0; - } - -#ifdef THREAD_DEBUG - printf(" RenderThread: --- wait for swap...\n"); -#endif - - if (windowData->isVisible && window->isExposed()) - gl->swapBuffers(window); - - windowPrivate->fireFrameSwapped(); -#ifdef THREAD_DEBUG - printf(" RenderThread: --- swap complete...\n"); -#endif - - } - -#ifdef QQUICK_RENDER_TIMING - if (qquick_render_timing()) { - static QTime lastFrameTime = QTime::currentTime(); - swapTime = threadTimer.elapsed() - renderTime - syncTime; - qDebug() << "- Breakdown of frame time; sync:" << syncTime - << "ms render:" << renderTime << "ms swap:" << swapTime - << "ms total:" << swapTime + renderTime + syncTime - << "ms time since last frame:" << (lastFrameTime.msecsTo(QTime::currentTime())) - << "ms"; - lastFrameTime = QTime::currentTime(); - } -#endif - - lock(); - - handleRemovedWindows(); - - // Update sizes... - for (QHash<QQuickWindow *, WindowData *>::const_iterator it = m_rendered_windows.constBegin(); - it != m_rendered_windows.constEnd(); ++it) { - WindowData *windowData = it.value(); - if (windowData->sizeWasChanged) { - windowData->renderedSize = windowData->viewportSize; - windowData->sizeWasChanged = false; - } - } - - - // Wake the GUI thread now that rendering is complete, to signal that painting - // is done, resizing is done or grabbing is completed. For grabbing, we're - // signalling this much later than needed (we could have done it before swap) - // but we don't want to lock an extra time. - wake(); - - if (!animationRunning && !isExternalUpdatePending && !shouldExit && !windowToGrab) { -#ifdef THREAD_DEBUG - printf(" RenderThread: nothing to do, going to sleep...\n"); -#endif - isRenderBlocked = true; - wait(); - isRenderBlocked = false; - } - - unlock(); - - QCoreApplication::processEvents(); - - // Process any "deleteLater" objects... - QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete); - } - -#ifdef THREAD_DEBUG - printf(" RenderThread: deleting all outstanding nodes\n"); -#endif - - m_removed_windows << m_rendered_windows.keys(); - handleRemovedWindows(false); - - sg->invalidate(); - - gl->doneCurrent(); - delete gl; - gl = 0; - -#ifdef THREAD_DEBUG - printf(" RenderThread: render loop exited... Good Night!\n"); -#endif - - lock(); - hasExited = true; - -#ifdef THREAD_DEBUG - printf(" RenderThread: waking GUI for final sleep..\n"); -#endif - wake(); - unlock(); - -#ifdef THREAD_DEBUG - printf(" RenderThread: All done...\n"); -#endif -} - -bool QQuickRenderThreadSingleContextWindowManager::event(QEvent *e) -{ - Q_ASSERT(QThread::currentThread() == qApp->thread()); - - if (e->type() == QEvent_Sync) { - - // If all windowes have been hidden, ignore the event - if (!isRunning()) - return true; - - if (!syncAlreadyHappened) - sync(false); - - syncAlreadyHappened = false; - - if (animationRunning) { -#ifdef THREAD_DEBUG - printf("GUI: Advancing animations...\n"); -#endif - - animDriver->advance(); - -#ifdef THREAD_DEBUG - printf("GUI: Animations advanced...\n"); -#endif - } - - return true; - } else if (e->type() == QEvent_DeferredUpdate) { - handleDeferredUpdate(); - - } else if (e->type() == QEvent::Timer) { -#ifdef THREAD_DEBUG - printf("GUI: Animations advanced via timer...\n"); -#endif - animDriver->advance(); - } - - return QThread::event(e); -} - - - -void QQuickRenderThreadSingleContextWindowManager::exhaustSyncEvent() -{ - if (isPostingSyncEvent) { - sync(true); - syncAlreadyHappened = true; - } -} - - - -void QQuickRenderThreadSingleContextWindowManager::sync(bool guiAlreadyLocked) -{ -#ifdef THREAD_DEBUG - printf("GUI: sync - %s\n", guiAlreadyLocked ? "outside event" : "inside event"); -#endif - if (!guiAlreadyLocked) - lockInGui(); - - for (QHash<QQuickWindow *, WindowData *>::const_iterator it = m_rendered_windows.constBegin(); - it != m_rendered_windows.constEnd(); ++it) { - QQuickWindowPrivate::get(it.key())->polishItems(); - } - - wake(); - wait(); - - if (!guiAlreadyLocked) - unlockInGui(); -} - - - - -/*! - Acquires the mutex for the GUI thread. The function uses the isGuiLocked - variable to keep track of how many recursion levels the gui is locked with. - We only actually acquire the mutex for the first level to avoid deadlocking - ourselves. - */ - -void QQuickRenderThreadSingleContextWindowManager::lockInGui() -{ - if (++isGuiLocked == 1) - lock(); - -#ifdef THREAD_DEBUG - printf("GUI: acquired lock... level=%d\n", isGuiLocked); -#endif -} - - - -void QQuickRenderThreadSingleContextWindowManager::unlockInGui() -{ -#ifdef THREAD_DEBUG - printf("GUI: releasing lock... level=%d\n", isGuiLocked); -#endif - - if (--isGuiLocked == 0) - unlock(); -} - - - - -void QQuickRenderThreadSingleContextWindowManager::animationStarted() -{ -#ifdef THREAD_DEBUG - printf("GUI: animationStarted()\n"); -#endif - - if (!isRunning()) { - animationTimer = startTimer(1000/60); - return; - } - - lockInGui(); - - animationRunning = true; - - if (isRenderBlocked) - wake(); - - unlockInGui(); -} - - - -void QQuickRenderThreadSingleContextWindowManager::animationStopped() -{ -#ifdef THREAD_DEBUG - printf("GUI: animationStopped()...\n"); -#endif - - if (!isRunning()) { - killTimer(animationTimer); - animationTimer = -1; - return; - } - - lockInGui(); - animationRunning = false; - unlockInGui(); -} - - -void QQuickRenderThreadSingleContextWindowManager::exposureChanged(QQuickWindow *window) -{ - Q_UNUSED(window); -#ifdef THREAD_DEBUG - printf("GUI: exposure changed: %p\n", window); -#endif - - if (window->isExposed()) - maybeUpdate(window); - -#ifdef THREAD_DEBUG - printf("GUI: exposure changed done: %p\n", window); -#endif -} - - - -void QQuickRenderThreadSingleContextWindowManager::resize(QQuickWindow *window, const QSize &size) -{ -#ifdef THREAD_DEBUG - printf("GUI: Resize Event: %p = %dx%d\n", window, size.width(), size.height()); -#endif - - // If the rendering thread is not running we do not need to do anything. - // Also if the window is being resized to an invalid size, it will be removed - // by the windowVisibilityChanged slot as result of width/heightcChanged() - if (!isRunning() || size.width() <= 0 || size.height() <= 0) - return; - - lockInGui(); - exhaustSyncEvent(); - - WindowData *windowData = m_rendered_windows.value(window); - if (windowData) { - windowData->windowSize = size; - while (isRunning() && windowData->renderedSize != size && size.width() > 0 && size.height() > 0) { - if (isRenderBlocked) - wake(); - wait(); - } - } - unlockInGui(); - -#ifdef THREAD_DEBUG - printf("GUI: Resize done: %p\n", window); -#endif -} - - - -void QQuickRenderThreadSingleContextWindowManager::startRendering() -{ -#ifdef THREAD_DEBUG - printf("GUI: Starting Render Thread\n"); -#endif - hasExited = false; - shouldExit = false; - isGuiLocked = 0; - isPostingSyncEvent = false; - syncAlreadyHappened = false; - inSync = false; - - lockInGui(); - animationRunning = animDriver->isRunning(); - start(); // Start the render thread... - wait(); - unlockInGui(); - - // Animations will now be driven from the rendering thread. - if (animationTimer >= 0) { - killTimer(animationTimer); - animationTimer = -1; - } - - -} - - - -void QQuickRenderThreadSingleContextWindowManager::stopRendering() -{ -#ifdef THREAD_DEBUG - printf("GUI: stopping render thread\n"); -#endif - - lockInGui(); - exhaustSyncEvent(); - shouldExit = true; - - if (isRenderBlocked) { -#ifdef THREAD_DEBUG - printf("GUI: waking up render thread\n"); -#endif - wake(); - } - - while (!hasExited) { -#ifdef THREAD_DEBUG - printf("GUI: waiting for render thread to have exited..\n"); -#endif - wait(); - } - - unlockInGui(); - -#ifdef THREAD_DEBUG - printf("GUI: waiting for render thread to terminate..\n"); -#endif - // Actually wait for the thread to terminate. Otherwise we can delete it - // too early and crash. - QThread::wait(); - -#ifdef THREAD_DEBUG - printf("GUI: thread has terminated and we're all good..\n"); -#endif - - // Activate timer to keep animations running - if (animDriver->isRunning()) - animationTimer = startTimer(1000/60); -} - - - -QImage QQuickRenderThreadSingleContextWindowManager::grab(QQuickWindow *window) -{ - if (!isRunning()) - return QImage(); - - if (QThread::currentThread() != qApp->thread()) { - qWarning("QQuickWindow::grabFrameBuffer: can only be called from the GUI thread"); - return QImage(); - } else if (window->size().width() <= 0 || window->size().height() <= 0 ) { - qWarning("QQuickWindow::grabFrameBuffer: Can't grab a Window with size %dx%d", window->size().width(), window->size().height()); - return QImage(); - } - -#ifdef THREAD_DEBUG - printf("GUI: doing a pixelwise grab..\n"); -#endif - - lockInGui(); - exhaustSyncEvent(); - - windowToGrab = window; - while (isRunning() && windowToGrab) { - if (isRenderBlocked) - wake(); - wait(); - } - - QImage grabbed = grabContent; - grabContent = QImage(); - - unlockInGui(); - - return grabbed; -} - - -void QQuickRenderThreadSingleContextWindowManager::handleDeferredUpdate() -{ -#ifdef THREAD_DEBUG - printf("GUI: handling update to ourselves...\n"); -#endif - - isDeferredUpdatePosted = false; - - lockInGui(); - isExternalUpdatePending = true; - if (isRenderBlocked) - wake(); - unlockInGui(); -} - -void QQuickRenderThreadSingleContextWindowManager::maybeUpdate(QQuickWindow *) -{ - Q_ASSERT_X(QThread::currentThread() == QCoreApplication::instance()->thread() || inSync, - "QQuickWindow::update", - "Function can only be called from GUI thread or during QQuickItem::updatePaintNode()"); - - if (inSync) { - isExternalUpdatePending = true; - - } else if (!isDeferredUpdatePosted) { -#ifdef THREAD_DEBUG - printf("GUI: posting update to ourselves...\n"); -#endif - isDeferredUpdatePosted = true; - QCoreApplication::postEvent(this, new QEvent(QEvent_DeferredUpdate)); - } - -} - -QT_END_NAMESPACE diff --git a/src/quick/items/qquickthreadedwindowmanager_p.h b/src/quick/items/qquickthreadedwindowmanager_p.h deleted file mode 100644 index 76325e2d4f..0000000000 --- a/src/quick/items/qquickthreadedwindowmanager_p.h +++ /dev/null @@ -1,181 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal -** -** This file is part of the QtQml module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QQUICKTHREADEDWINDOWMANAGER_P_H -#define QQUICKTHREADEDWINDOWMANAGER_P_H - -#include "qquickwindowmanager_p.h" - -#include <QtCore/QCoreApplication> -#include <QtCore/QMutex> -#include <QtCore/QWaitCondition> -#include <QtCore/private/qabstractanimation_p.h> - -#include <QtGui/QOpenGLContext> -#include <QtQuick/private/qsgcontext_p.h> -#include <private/qtquickglobal_p.h> - -QT_BEGIN_NAMESPACE - -class QQuickRenderThreadSingleContextWindowManager : public QThread, public QQuickWindowManager -{ - Q_OBJECT -public: - QQuickRenderThreadSingleContextWindowManager() - : sg(QSGContext::createDefaultContext()) - , gl(0) - , animationTimer(-1) - , isGuiLocked(0) - , animationRunning(false) - , isPostingSyncEvent(false) - , isRenderBlocked(false) - , isExternalUpdatePending(false) - , syncAlreadyHappened(false) - , inSync(false) - , shouldExit(false) - , hasExited(false) - , isDeferredUpdatePosted(false) - , windowToGrab(0) - { - sg->moveToThread(this); - - animDriver = sg->createAnimationDriver(this); - animDriver->install(); - connect(animDriver, SIGNAL(started()), this, SLOT(animationStarted())); - connect(animDriver, SIGNAL(stopped()), this, SLOT(animationStopped())); - } - - QSGContext *sceneGraphContext() const { return sg; } - - void releaseResources() { } - - void show(QQuickWindow *window); - void hide(QQuickWindow *window); - - void windowDestroyed(QQuickWindow *window); - - void exposureChanged(QQuickWindow *window); - QImage grab(QQuickWindow *window); - void resize(QQuickWindow *window, const QSize &size); - void handleDeferredUpdate(); - void maybeUpdate(QQuickWindow *window); - void update(QQuickWindow *window) { maybeUpdate(window); } // identical for this implementation - - void startRendering(); - void stopRendering(); - - void exhaustSyncEvent(); - void sync(bool guiAlreadyLocked); - - void initialize(); - - bool event(QEvent *); - - inline void lock() { mutex.lock(); } - inline void unlock() { mutex.unlock(); } - inline void wait() { condition.wait(&mutex); } - inline void wake() { condition.wakeOne(); } - void lockInGui(); - void unlockInGui(); - - void run(); - - QAnimationDriver *animationDriver() const { return animDriver; } - -public slots: - void animationStarted(); - void animationStopped(); - void windowVisibilityChanged(); - -private: - void handleAddedWindows(); - void handleAddedWindow(QQuickWindow *window); - void handleRemovedWindows(bool clearGLContext = true); - - QSGContext *sg; - QOpenGLContext *gl; - QAnimationDriver *animDriver; - int animationTimer; - - QMutex mutex; - QWaitCondition condition; - - int isGuiLocked; - uint animationRunning: 1; - uint isPostingSyncEvent : 1; - uint isRenderBlocked : 1; - uint isExternalUpdatePending : 1; - uint syncAlreadyHappened : 1; - uint inSync : 1; - uint shouldExit : 1; - uint hasExited : 1; - uint isDeferredUpdatePosted : 1; - - QQuickWindow *windowToGrab; - QImage grabContent; - - struct WindowData { - QSize renderedSize; - QSize windowSize; - QSize viewportSize; - - uint sizeWasChanged : 1; - uint isVisible : 1; - uint isRenderable : 1; - }; - - QHash<QQuickWindow *, WindowData *> m_rendered_windows; - - struct WindowTracker { - QQuickWindow *window; - uint isVisible : 1; - uint toBeRemoved : 1; - }; - - QList<WindowTracker> m_tracked_windows; - - QList<QQuickWindow *> m_removed_windows; - QList<QQuickWindow *> m_added_windows; -}; - -QT_END_NAMESPACE - -#endif // QQUICKTHREADEDWINDOWMANAGER_P_H diff --git a/src/quick/items/qquicktranslate_p.h b/src/quick/items/qquicktranslate_p.h index 8c3438c937..4181f5af79 100644 --- a/src/quick/items/qquicktranslate_p.h +++ b/src/quick/items/qquicktranslate_p.h @@ -46,8 +46,6 @@ #include <QtGui/qmatrix4x4.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QQuickTranslatePrivate; @@ -154,6 +152,4 @@ QT_END_NAMESPACE QML_DECLARE_TYPE(QQuickTranslate) -QT_END_HEADER - #endif diff --git a/src/quick/items/qquickview.h b/src/quick/items/qquickview.h index 10bad4c615..3e8883ddfb 100644 --- a/src/quick/items/qquickview.h +++ b/src/quick/items/qquickview.h @@ -46,8 +46,6 @@ #include <QtCore/qurl.h> #include <QtQml/qqmldebug.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QQmlEngine; @@ -115,6 +113,4 @@ private: QT_END_NAMESPACE -QT_END_HEADER - #endif // QQUICKVIEW_H diff --git a/src/quick/items/qquickview_p.h b/src/quick/items/qquickview_p.h index 4b78d8b270..43b17e5bed 100644 --- a/src/quick/items/qquickview_p.h +++ b/src/quick/items/qquickview_p.h @@ -55,8 +55,6 @@ #include "qquickitemchangelistener_p.h" -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QQmlContext; @@ -100,6 +98,4 @@ public: QT_END_NAMESPACE -QT_END_HEADER - #endif // QQUICKVIEW_P_H diff --git a/src/quick/items/qquickvisualadaptormodel.cpp b/src/quick/items/qquickvisualadaptormodel.cpp deleted file mode 100644 index b57f4bbe76..0000000000 --- a/src/quick/items/qquickvisualadaptormodel.cpp +++ /dev/null @@ -1,977 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal -** -** This file is part of the QtQml module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "qquickvisualadaptormodel_p.h" -#include "qquickvisualdatamodel_p_p.h" - -#include <private/qmetaobjectbuilder_p.h> -#include <private/qqmlproperty_p.h> -#include <private/qv8engine_p.h> - -QT_BEGIN_NAMESPACE - -class QQuickVisualAdaptorModelEngineData : public QV8Engine::Deletable -{ -public: - enum - { - Index, - ModelData, - HasModelChildren, - StringCount - }; - - QQuickVisualAdaptorModelEngineData(QV8Engine *engine); - ~QQuickVisualAdaptorModelEngineData(); - - v8::Local<v8::String> index() { return strings->Get(Index)->ToString(); } - v8::Local<v8::String> modelData() { return strings->Get(ModelData)->ToString(); } - v8::Local<v8::String> hasModelChildren() { return strings->Get(HasModelChildren)->ToString(); } - - v8::Persistent<v8::Function> constructorListItem; - v8::Persistent<v8::Array> strings; -}; - -V8_DEFINE_EXTENSION(QQuickVisualAdaptorModelEngineData, engineData) - -static v8::Handle<v8::Value> get_index(v8::Local<v8::String>, const v8::AccessorInfo &info) -{ - QQuickVisualDataModelItem *data = v8_resource_cast<QQuickVisualDataModelItem>(info.This()); - V8ASSERT_TYPE(data, "Not a valid VisualData object"); - - return v8::Int32::New(data->index); -} - -template <typename T, typename M> static void setModelDataType(QMetaObjectBuilder *builder, M *metaType) -{ - builder->setFlags(QMetaObjectBuilder::DynamicMetaObject); - builder->setClassName(T::staticMetaObject.className()); - builder->setSuperClass(&T::staticMetaObject); - metaType->propertyOffset = T::staticMetaObject.propertyCount(); - metaType->signalOffset = T::staticMetaObject.methodCount(); -} - -static void addProperty(QMetaObjectBuilder *builder, int propertyId, const QByteArray &propertyName, const QByteArray &propertyType) -{ - builder->addSignal("__" + QByteArray::number(propertyId) + "()"); - QMetaPropertyBuilder property = builder->addProperty( - propertyName, propertyType, propertyId); - property.setWritable(true); -} - -class VDMModelDelegateDataType; - -class QQuickVDMCachedModelData : public QQuickVisualDataModelItem -{ -public: - QQuickVDMCachedModelData( - QQuickVisualDataModelItemMetaType *metaType, - VDMModelDelegateDataType *dataType, - int index); - - int metaCall(QMetaObject::Call call, int id, void **arguments); - - virtual QVariant value(int role) const = 0; - virtual void setValue(int role, const QVariant &value) = 0; - - void setValue(const QString &role, const QVariant &value); - bool resolveIndex(const QQuickVisualAdaptorModel &model, int idx); - - static v8::Handle<v8::Value> get_property(v8::Local<v8::String>, const v8::AccessorInfo &info); - static void set_property( - v8::Local<v8::String>, v8::Local<v8::Value> value, const v8::AccessorInfo &info); - - VDMModelDelegateDataType *type; - QVector<QVariant> cachedData; -}; - -class VDMModelDelegateDataType - : public QQmlRefCount - , public QQuickVisualAdaptorModel::Accessors - , public QAbstractDynamicMetaObject -{ -public: - VDMModelDelegateDataType(QQuickVisualAdaptorModel *model) - : model(model) - , metaObject(0) - , propertyCache(0) - , propertyOffset(0) - , signalOffset(0) - , hasModelData(false) - { - } - - ~VDMModelDelegateDataType() - { - if (propertyCache) - propertyCache->release(); - free(metaObject); - - qPersistentDispose(constructor); - } - - bool notify( - const QQuickVisualAdaptorModel &, - const QList<QQuickVisualDataModelItem *> &items, - int index, - int count, - const QVector<int> &roles) const - { - bool changed = roles.isEmpty() && !watchedRoles.isEmpty(); - if (!changed && !watchedRoles.isEmpty() && watchedRoleIds.isEmpty()) { - QList<int> roleIds; - foreach (const QByteArray &r, watchedRoles) { - QHash<QByteArray, int>::const_iterator it = roleNames.find(r); - if (it != roleNames.end()) - roleIds << it.value(); - } - const_cast<VDMModelDelegateDataType *>(this)->watchedRoleIds = roleIds; - } - - QVector<int> signalIndexes; - for (int i = 0; i < roles.count(); ++i) { - const int role = roles.at(i); - if (!changed && watchedRoleIds.contains(role)) - changed = true; - - int propertyId = propertyRoles.indexOf(role); - if (propertyId != -1) - signalIndexes.append(propertyId + signalOffset); - } - if (roles.isEmpty()) { - for (int propertyId = 0; propertyId < propertyRoles.count(); ++propertyId) - signalIndexes.append(propertyId + signalOffset); - } - - for (int i = 0, c = items.count(); i < c; ++i) { - QQuickVisualDataModelItem *item = items.at(i); - const int idx = item->modelIndex(); - if (idx >= index && idx < index + count) { - for (int i = 0; i < signalIndexes.count(); ++i) - QMetaObject::activate(item, signalIndexes.at(i), 0); - } - } - return changed; - } - - void replaceWatchedRoles( - QQuickVisualAdaptorModel &, - const QList<QByteArray> &oldRoles, - const QList<QByteArray> &newRoles) const - { - VDMModelDelegateDataType *dataType = const_cast<VDMModelDelegateDataType *>(this); - - dataType->watchedRoleIds.clear(); - foreach (const QByteArray &oldRole, oldRoles) - dataType->watchedRoles.removeOne(oldRole); - dataType->watchedRoles += newRoles; - } - - void initializeConstructor(QQuickVisualAdaptorModelEngineData *const data) - { - constructor = qPersistentNew(v8::ObjectTemplate::New()); - constructor->SetHasExternalResource(true); - constructor->SetAccessor(data->index(), get_index); - - typedef QHash<QByteArray, int>::const_iterator iterator; - for (iterator it = roleNames.constBegin(), end = roleNames.constEnd(); it != end; ++it) { - const int propertyId = propertyRoles.indexOf(it.value()); - const QByteArray &propertyName = it.key(); - - constructor->SetAccessor( - v8::String::New(propertyName.constData(), propertyName.length()), - QQuickVDMCachedModelData::get_property, - QQuickVDMCachedModelData::set_property, - v8::Int32::New(propertyId)); - } - } - - // QAbstractDynamicMetaObject - - void objectDestroyed(QObject *) - { - release(); - } - - int metaCall(QObject *object, QMetaObject::Call call, int id, void **arguments) - { - return static_cast<QQuickVDMCachedModelData *>(object)->metaCall(call, id, arguments); - } - - v8::Persistent<v8::ObjectTemplate> constructor; - QList<int> propertyRoles; - QList<int> watchedRoleIds; - QList<QByteArray> watchedRoles; - QHash<QByteArray, int> roleNames; - QQuickVisualAdaptorModel *model; - QMetaObject *metaObject; - QQmlPropertyCache *propertyCache; - int propertyOffset; - int signalOffset; - bool hasModelData; -}; - -QQuickVDMCachedModelData::QQuickVDMCachedModelData( - QQuickVisualDataModelItemMetaType *metaType, VDMModelDelegateDataType *dataType, int index) - : QQuickVisualDataModelItem(metaType, index) - , type(dataType) -{ - if (index == -1) - cachedData.resize(type->hasModelData ? 1 : type->propertyRoles.count()); - - QObjectPrivate::get(this)->metaObject = type; - - type->addref(); - - QQmlData *qmldata = QQmlData::get(this, true); - qmldata->propertyCache = dataType->propertyCache; - qmldata->propertyCache->addref(); -} - -int QQuickVDMCachedModelData::metaCall(QMetaObject::Call call, int id, void **arguments) -{ - if (call == QMetaObject::ReadProperty && id >= type->propertyOffset) { - const int propertyIndex = id - type->propertyOffset; - if (index == -1) { - if (!cachedData.isEmpty()) { - *static_cast<QVariant *>(arguments[0]) = cachedData.at( - type->hasModelData ? 0 : propertyIndex); - } - } else if (*type->model) { - *static_cast<QVariant *>(arguments[0]) = value(type->propertyRoles.at(propertyIndex)); - } - return -1; - } else if (call == QMetaObject::WriteProperty && id >= type->propertyOffset) { - const int propertyIndex = id - type->propertyOffset; - if (index == -1) { - const QMetaObject *meta = metaObject(); - if (cachedData.count() > 1) { - cachedData[propertyIndex] = *static_cast<QVariant *>(arguments[0]); - QMetaObject::activate(this, meta, propertyIndex, 0); - } else if (cachedData.count() == 1) { - cachedData[0] = *static_cast<QVariant *>(arguments[0]); - QMetaObject::activate(this, meta, 0, 0); - QMetaObject::activate(this, meta, 1, 0); - } - } else if (*type->model) { - setValue(type->propertyRoles.at(propertyIndex), *static_cast<QVariant *>(arguments[0])); - } - return -1; - } else { - return qt_metacall(call, id, arguments); - } -} - -void QQuickVDMCachedModelData::setValue(const QString &role, const QVariant &value) -{ - QHash<QByteArray, int>::iterator it = type->roleNames.find(role.toUtf8()); - if (it != type->roleNames.end()) { - for (int i = 0; i < type->propertyRoles.count(); ++i) { - if (type->propertyRoles.at(i) == *it) { - cachedData[i] = value; - return; - } - } - } -} - -bool QQuickVDMCachedModelData::resolveIndex(const QQuickVisualAdaptorModel &, int idx) -{ - if (index == -1) { - Q_ASSERT(idx >= 0); - index = idx; - cachedData.clear(); - emit modelIndexChanged(); - const QMetaObject *meta = metaObject(); - const int propertyCount = type->propertyRoles.count(); - for (int i = 0; i < propertyCount; ++i) - QMetaObject::activate(this, meta, i, 0); - return true; - } else { - return false; - } -} - -v8::Handle<v8::Value> QQuickVDMCachedModelData::get_property( - v8::Local<v8::String>, const v8::AccessorInfo &info) -{ - QQuickVisualDataModelItem *data = v8_resource_cast<QQuickVisualDataModelItem>(info.This()); - V8ASSERT_TYPE(data, "Not a valid VisualData object"); - - QQuickVDMCachedModelData *modelData = static_cast<QQuickVDMCachedModelData *>(data); - const int propertyId = info.Data()->Int32Value(); - if (data->index == -1) { - if (!modelData->cachedData.isEmpty()) { - return data->engine->fromVariant( - modelData->cachedData.at(modelData->type->hasModelData ? 0 : propertyId)); - } - } else if (*modelData->type->model) { - return data->engine->fromVariant( - modelData->value(modelData->type->propertyRoles.at(propertyId))); - } - return v8::Undefined(); -} - -void QQuickVDMCachedModelData::set_property( - v8::Local<v8::String>, v8::Local<v8::Value> value, const v8::AccessorInfo &info) -{ - QQuickVisualDataModelItem *data = v8_resource_cast<QQuickVisualDataModelItem>(info.This()); - V8ASSERT_TYPE_SETTER(data, "Not a valid VisualData object"); - - const int propertyId = info.Data()->Int32Value(); - if (data->index == -1) { - QQuickVDMCachedModelData *modelData = static_cast<QQuickVDMCachedModelData *>(data); - if (!modelData->cachedData.isEmpty()) { - if (modelData->cachedData.count() > 1) { - modelData->cachedData[propertyId] = data->engine->toVariant(value, QVariant::Invalid); - QMetaObject::activate(data, data->metaObject(), propertyId, 0); - } else if (modelData->cachedData.count() == 1) { - modelData->cachedData[0] = data->engine->toVariant(value, QVariant::Invalid); - QMetaObject::activate(data, data->metaObject(), 0, 0); - QMetaObject::activate(data, data->metaObject(), 1, 0); - } - } - } -} - -//----------------------------------------------------------------- -// QAbstractItemModel -//----------------------------------------------------------------- - -class QQuickVDMAbstractItemModelData : public QQuickVDMCachedModelData -{ - Q_OBJECT - Q_PROPERTY(bool hasModelChildren READ hasModelChildren CONSTANT) -public: - QQuickVDMAbstractItemModelData( - QQuickVisualDataModelItemMetaType *metaType, - VDMModelDelegateDataType *dataType, - int index) - : QQuickVDMCachedModelData(metaType, dataType, index) - { - } - - bool hasModelChildren() const - { - if (index >= 0 && *type->model) { - const QAbstractItemModel * const model = type->model->aim(); - return model->hasChildren(model->index(index, 0, type->model->rootIndex)); - } else { - return false; - } - } - - QVariant value(int role) const - { - return type->model->aim()->index(index, 0, type->model->rootIndex).data(role); - } - - void setValue(int role, const QVariant &value) - { - type->model->aim()->setData( - type->model->aim()->index(index, 0, type->model->rootIndex), value, role); - } - - v8::Handle<v8::Value> get() - { - if (type->constructor.IsEmpty()) { - QQuickVisualAdaptorModelEngineData * const data = engineData(engine); - v8::HandleScope handleScope; - v8::Context::Scope contextScope(engine->context()); - type->initializeConstructor(data); - type->constructor->SetAccessor(data->hasModelChildren(), get_hasModelChildren); - } - v8::Local<v8::Object> data = type->constructor->NewInstance(); - data->SetExternalResource(this); - ++scriptRef; - return data; - } - - static v8::Handle<v8::Value> get_hasModelChildren(v8::Local<v8::String>, const v8::AccessorInfo &info) - { - QQuickVisualDataModelItem *data = v8_resource_cast<QQuickVisualDataModelItem>(info.This()); - V8ASSERT_TYPE(data, "Not a valid VisualData object"); - - const QQuickVisualAdaptorModel *const model = static_cast<QQuickVDMCachedModelData *>(data)->type->model; - if (data->index >= 0 && *model) { - const QAbstractItemModel * const aim = model->aim(); - return v8::Boolean::New(aim->hasChildren(aim->index(data->index, 0, model->rootIndex))); - } else { - return v8::Boolean::New(false); - } - } -}; - -class VDMAbstractItemModelDataType : public VDMModelDelegateDataType -{ -public: - VDMAbstractItemModelDataType(QQuickVisualAdaptorModel *model) - : VDMModelDelegateDataType(model) - { - } - - int count(const QQuickVisualAdaptorModel &model) const - { - return model.aim()->rowCount(model.rootIndex); - } - - void cleanup(QQuickVisualAdaptorModel &model, QQuickVisualDataModel *vdm) const - { - QAbstractItemModel * const aim = model.aim(); - if (aim && vdm) { - QObject::disconnect(aim, SIGNAL(rowsInserted(QModelIndex,int,int)), - vdm, SLOT(_q_rowsInserted(QModelIndex,int,int))); - QObject::disconnect(aim, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), - vdm, SLOT(_q_rowsAboutToBeRemoved(QModelIndex,int,int))); - QObject::disconnect(aim, SIGNAL(rowsRemoved(QModelIndex,int,int)), - vdm, SLOT(_q_rowsRemoved(QModelIndex,int,int))); - QObject::disconnect(aim, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)), - vdm, SLOT(_q_dataChanged(QModelIndex,QModelIndex,QVector<int>))); - QObject::disconnect(aim, SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)), - vdm, SLOT(_q_rowsMoved(QModelIndex,int,int,QModelIndex,int))); - QObject::disconnect(aim, SIGNAL(modelReset()), - vdm, SLOT(_q_modelReset())); - QObject::disconnect(aim, SIGNAL(layoutChanged()), - vdm, SLOT(_q_layoutChanged())); - } - - const_cast<VDMAbstractItemModelDataType *>(this)->release(); - } - - QVariant value(const QQuickVisualAdaptorModel &model, int index, const QString &role) const - { - QHash<QByteArray, int>::const_iterator it = roleNames.find(role.toUtf8()); - if (it != roleNames.end()) { - return model.aim()->index(index, 0, model.rootIndex).data(*it); - } else if (role == QLatin1String("hasModelChildren")) { - return QVariant(model.aim()->hasChildren(model.aim()->index(index, 0, model.rootIndex))); - } else { - return QVariant(); - } - } - - QVariant parentModelIndex(const QQuickVisualAdaptorModel &model) const - { - return model - ? QVariant::fromValue(model.aim()->parent(model.rootIndex)) - : QVariant(); - } - - QVariant modelIndex(const QQuickVisualAdaptorModel &model, int index) const - { - return model - ? QVariant::fromValue(model.aim()->index(index, 0, model.rootIndex)) - : QVariant(); - } - - bool canFetchMore(const QQuickVisualAdaptorModel &model) const - { - return model && model.aim()->canFetchMore(model.rootIndex); - } - - void fetchMore(QQuickVisualAdaptorModel &model) const - { - if (model) - model.aim()->fetchMore(model.rootIndex); - } - - QQuickVisualDataModelItem *createItem( - QQuickVisualAdaptorModel &model, - QQuickVisualDataModelItemMetaType *metaType, - QQmlEngine *engine, - int index) const - { - VDMAbstractItemModelDataType *dataType = const_cast<VDMAbstractItemModelDataType *>(this); - if (!metaObject) - dataType->initializeMetaType(model, engine); - return new QQuickVDMAbstractItemModelData(metaType, dataType, index); - } - - void initializeMetaType(QQuickVisualAdaptorModel &model, QQmlEngine *engine) - { - QMetaObjectBuilder builder; - setModelDataType<QQuickVDMAbstractItemModelData>(&builder, this); - - const QByteArray propertyType = QByteArrayLiteral("QVariant"); - const QHash<int, QByteArray> names = model.aim()->roleNames(); - for (QHash<int, QByteArray>::const_iterator it = names.begin(); it != names.end(); ++it) { - const int propertyId = propertyRoles.count(); - propertyRoles.append(it.key()); - roleNames.insert(it.value(), it.key()); - addProperty(&builder, propertyId, it.value(), propertyType); - } - if (propertyRoles.count() == 1) { - hasModelData = true; - const int role = names.begin().key(); - const QByteArray propertyName = QByteArrayLiteral("modelData"); - - propertyRoles.append(role); - roleNames.insert(propertyName, role); - addProperty(&builder, 1, propertyName, propertyType); - } - - metaObject = builder.toMetaObject(); - *static_cast<QMetaObject *>(this) = *metaObject; - propertyCache = new QQmlPropertyCache(engine, metaObject); - } -}; - -//----------------------------------------------------------------- -// QQuickListAccessor -//----------------------------------------------------------------- - -class QQuickVDMListAccessorData : public QQuickVisualDataModelItem -{ - Q_OBJECT - Q_PROPERTY(QVariant modelData READ modelData WRITE setModelData NOTIFY modelDataChanged) -public: - QQuickVDMListAccessorData(QQuickVisualDataModelItemMetaType *metaType, int index, const QVariant &value) - : QQuickVisualDataModelItem(metaType, index) - , cachedData(value) - { - } - - QVariant modelData() const - { - return cachedData; - } - - void setModelData(const QVariant &data) - { - if (index == -1 && data != cachedData) { - cachedData = data; - emit modelDataChanged(); - } - } - - static v8::Handle<v8::Value> get_modelData(v8::Local<v8::String>, const v8::AccessorInfo &info) - { - QQuickVisualDataModelItem *data = v8_resource_cast<QQuickVisualDataModelItem>(info.This()); - V8ASSERT_TYPE(data, "Not a valid VisualData object"); - - return data->engine->fromVariant(static_cast<QQuickVDMListAccessorData *>(data)->cachedData); - } - - static void set_modelData(v8::Local<v8::String>, v8::Local<v8::Value> value, const v8::AccessorInfo &info) - { - QQuickVisualDataModelItem *data = v8_resource_cast<QQuickVisualDataModelItem>(info.This()); - V8ASSERT_TYPE_SETTER(data, "Not a valid VisualData object"); - - static_cast<QQuickVDMListAccessorData *>(data)->setModelData( - data->engine->toVariant(value, QVariant::Invalid)); - } - - v8::Handle<v8::Value> get() - { - v8::Local<v8::Object> data = engineData(engine)->constructorListItem->NewInstance(); - data->SetExternalResource(this); - ++scriptRef; - return data; - } - - void setValue(const QString &role, const QVariant &value) - { - if (role == QLatin1String("modelData")) - cachedData = value; - } - - bool resolveIndex(const QQuickVisualAdaptorModel &model, int idx) - { - if (index == -1) { - index = idx; - cachedData = model.list.at(idx); - emit modelIndexChanged(); - emit modelDataChanged(); - return true; - } else { - return false; - } - } - - -Q_SIGNALS: - void modelDataChanged(); - -private: - QVariant cachedData; -}; - - -class VDMListDelegateDataType : public QQuickVisualAdaptorModel::Accessors -{ -public: - inline VDMListDelegateDataType() {} - - int count(const QQuickVisualAdaptorModel &model) const - { - return model.list.count(); - } - - QVariant value(const QQuickVisualAdaptorModel &model, int index, const QString &role) const - { - return role == QLatin1String("modelData") - ? model.list.at(index) - : QVariant(); - } - - QQuickVisualDataModelItem *createItem( - QQuickVisualAdaptorModel &model, - QQuickVisualDataModelItemMetaType *metaType, - QQmlEngine *, - int index) const - { - return new QQuickVDMListAccessorData( - metaType, - index, - index >= 0 && index < model.list.count() ? model.list.at(index) : QVariant()); - } -}; - -//----------------------------------------------------------------- -// QObject -//----------------------------------------------------------------- - -class VDMObjectDelegateDataType; -class QQuickVDMObjectData : public QQuickVisualDataModelItem, public QQuickVisualAdaptorModelProxyInterface -{ - Q_OBJECT - Q_PROPERTY(QObject *modelData READ modelData CONSTANT) - Q_INTERFACES(QQuickVisualAdaptorModelProxyInterface) -public: - QQuickVDMObjectData( - QQuickVisualDataModelItemMetaType *metaType, - VDMObjectDelegateDataType *dataType, - int index, - QObject *object); - - QObject *modelData() const { return object; } - QObject *proxiedObject() { return object; } - - QQmlGuard<QObject> object; -}; - -class VDMObjectDelegateDataType : public QQmlRefCount, public QQuickVisualAdaptorModel::Accessors -{ -public: - QMetaObject *metaObject; - int propertyOffset; - int signalOffset; - bool shared; - QMetaObjectBuilder builder; - - VDMObjectDelegateDataType() - : metaObject(0) - , propertyOffset(0) - , signalOffset(0) - , shared(true) - { - } - - VDMObjectDelegateDataType(const VDMObjectDelegateDataType &type) - : QQmlRefCount() - , QQuickVisualAdaptorModel::Accessors() - , metaObject(0) - , propertyOffset(type.propertyOffset) - , signalOffset(type.signalOffset) - , shared(false) - , builder(type.metaObject, QMetaObjectBuilder::Properties - | QMetaObjectBuilder::Signals - | QMetaObjectBuilder::SuperClass - | QMetaObjectBuilder::ClassName) - { - builder.setFlags(QMetaObjectBuilder::DynamicMetaObject); - } - - ~VDMObjectDelegateDataType() - { - free(metaObject); - } - - int count(const QQuickVisualAdaptorModel &model) const - { - return model.list.count(); - } - - QVariant value(const QQuickVisualAdaptorModel &model, int index, const QString &role) const - { - if (QObject *object = model.list.at(index).value<QObject *>()) - return object->property(role.toUtf8()); - return QVariant(); - } - - QQuickVisualDataModelItem *createItem( - QQuickVisualAdaptorModel &model, - QQuickVisualDataModelItemMetaType *metaType, - QQmlEngine *, - int index) const - { - VDMObjectDelegateDataType *dataType = const_cast<VDMObjectDelegateDataType *>(this); - if (!metaObject) - dataType->initializeMetaType(model); - return index >= 0 && index < model.list.count() - ? new QQuickVDMObjectData(metaType, dataType, index, qvariant_cast<QObject *>(model.list.at(index))) - : 0; - } - - void initializeMetaType(QQuickVisualAdaptorModel &) - { - setModelDataType<QQuickVDMObjectData>(&builder, this); - - metaObject = builder.toMetaObject(); - } - - void cleanup(QQuickVisualAdaptorModel &, QQuickVisualDataModel *) const - { - const_cast<VDMObjectDelegateDataType *>(this)->release(); - } -}; - -class QQuickVDMObjectDataMetaObject : public QAbstractDynamicMetaObject -{ -public: - QQuickVDMObjectDataMetaObject(QQuickVDMObjectData *data, VDMObjectDelegateDataType *type) - : m_data(data) - , m_type(type) - { - QObjectPrivate *op = QObjectPrivate::get(m_data); - *static_cast<QMetaObject *>(this) = *type->metaObject; - op->metaObject = this; - m_type->addref(); - } - - ~QQuickVDMObjectDataMetaObject() - { - m_type->release(); - } - - int metaCall(QMetaObject::Call call, int id, void **arguments) - { - static const int objectPropertyOffset = QObject::staticMetaObject.propertyCount(); - if (id >= m_type->propertyOffset - && (call == QMetaObject::ReadProperty - || call == QMetaObject::WriteProperty - || call == QMetaObject::ResetProperty)) { - if (m_data->object) - QMetaObject::metacall(m_data->object, call, id - m_type->propertyOffset + objectPropertyOffset, arguments); - return -1; - } else if (id >= m_type->signalOffset && call == QMetaObject::InvokeMetaMethod) { - QMetaObject::activate(m_data, this, id - m_type->signalOffset, 0); - return -1; - } else { - return m_data->qt_metacall(call, id, arguments); - } - } - - int createProperty(const char *name, const char *) - { - if (!m_data->object) - return -1; - const QMetaObject *metaObject = m_data->object->metaObject(); - static const int objectPropertyOffset = QObject::staticMetaObject.propertyCount(); - - const int previousPropertyCount = propertyCount() - propertyOffset(); - int propertyIndex = metaObject->indexOfProperty(name); - if (propertyIndex == -1) - return -1; - if (previousPropertyCount + objectPropertyOffset == metaObject->propertyCount()) - return propertyIndex + m_type->propertyOffset - objectPropertyOffset; - - if (m_type->shared) { - VDMObjectDelegateDataType *type = m_type; - m_type = new VDMObjectDelegateDataType(*m_type); - type->release(); - } - - const int previousMethodCount = methodCount(); - int notifierId = previousMethodCount - methodOffset(); - for (int propertyId = previousPropertyCount; propertyId < metaObject->propertyCount() - objectPropertyOffset; ++propertyId) { - QMetaProperty property = metaObject->property(propertyId + objectPropertyOffset); - QMetaPropertyBuilder propertyBuilder; - if (property.hasNotifySignal()) { - m_type->builder.addSignal("__" + QByteArray::number(propertyId) + "()"); - propertyBuilder = m_type->builder.addProperty(property.name(), property.typeName(), notifierId); - ++notifierId; - } else { - propertyBuilder = m_type->builder.addProperty(property.name(), property.typeName()); - } - propertyBuilder.setWritable(property.isWritable()); - propertyBuilder.setResettable(property.isResettable()); - propertyBuilder.setConstant(property.isConstant()); - } - - if (m_type->metaObject) - free(m_type->metaObject); - m_type->metaObject = m_type->builder.toMetaObject(); - *static_cast<QMetaObject *>(this) = *m_type->metaObject; - - notifierId = previousMethodCount; - for (int i = previousPropertyCount; i < metaObject->propertyCount() - objectPropertyOffset; ++i) { - QMetaProperty property = metaObject->property(i + objectPropertyOffset); - if (property.hasNotifySignal()) { - QQmlPropertyPrivate::connect( - m_data->object, property.notifySignalIndex(), m_data, notifierId); - ++notifierId; - } - } - return propertyIndex + m_type->propertyOffset - objectPropertyOffset; - } - - QQuickVDMObjectData *m_data; - VDMObjectDelegateDataType *m_type; -}; - -QQuickVDMObjectData::QQuickVDMObjectData( - QQuickVisualDataModelItemMetaType *metaType, - VDMObjectDelegateDataType *dataType, - int index, - QObject *object) - : QQuickVisualDataModelItem(metaType, index) - , object(object) -{ - new QQuickVDMObjectDataMetaObject(this, dataType); -} - -//----------------------------------------------------------------- -// QQuickVisualAdaptorModel -//----------------------------------------------------------------- - -static const QQuickVisualAdaptorModel::Accessors qt_vdm_null_accessors; -static const VDMListDelegateDataType qt_vdm_list_accessors; - -QQuickVisualAdaptorModel::Accessors::~Accessors() -{ -} - -QQuickVisualAdaptorModel::QQuickVisualAdaptorModel() - : accessors(&qt_vdm_null_accessors) -{ -} - -QQuickVisualAdaptorModel::~QQuickVisualAdaptorModel() -{ - accessors->cleanup(*this); -} - -void QQuickVisualAdaptorModel::setModel(const QVariant &variant, QQuickVisualDataModel *vdm, QQmlEngine *engine) -{ - accessors->cleanup(*this, vdm); - - list.setList(variant, engine); - - if (QObject *object = qvariant_cast<QObject *>(variant)) { - setObject(object); - if (QAbstractItemModel *model = qobject_cast<QAbstractItemModel *>(object)) { - accessors = new VDMAbstractItemModelDataType(this); - - qmlobject_connect(model, QAbstractItemModel, SIGNAL(rowsInserted(QModelIndex,int,int)), - vdm, QQuickVisualDataModel, SLOT(_q_rowsInserted(QModelIndex,int,int))); - qmlobject_connect(model, QAbstractItemModel, SIGNAL(rowsRemoved(QModelIndex,int,int)), - vdm, QQuickVisualDataModel, SLOT(_q_rowsRemoved(QModelIndex,int,int))); - qmlobject_connect(model, QAbstractItemModel, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), - vdm, QQuickVisualDataModel, SLOT(_q_rowsAboutToBeRemoved(QModelIndex,int,int))); - qmlobject_connect(model, QAbstractItemModel, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)), - vdm, QQuickVisualDataModel, SLOT(_q_dataChanged(QModelIndex,QModelIndex,QVector<int>))); - qmlobject_connect(model, QAbstractItemModel, SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)), - vdm, QQuickVisualDataModel, SLOT(_q_rowsMoved(QModelIndex,int,int,QModelIndex,int))); - qmlobject_connect(model, QAbstractItemModel, SIGNAL(modelReset()), - vdm, QQuickVisualDataModel, SLOT(_q_modelReset())); - qmlobject_connect(model, QAbstractItemModel, SIGNAL(layoutChanged()), - vdm, QQuickVisualDataModel, SLOT(_q_layoutChanged())); - } else { - accessors = new VDMObjectDelegateDataType; - } - } else if (list.type() == QQuickListAccessor::ListProperty) { - setObject(static_cast<const QQmlListReference *>(variant.constData())->object()); - accessors = new VDMObjectDelegateDataType; - } else if (list.type() != QQuickListAccessor::Invalid) { - Q_ASSERT(list.type() != QQuickListAccessor::Instance); // Should have cast to QObject. - setObject(0); - accessors = &qt_vdm_list_accessors; - } else { - setObject(0); - accessors = &qt_vdm_null_accessors; - } -} - -void QQuickVisualAdaptorModel::invalidateModel(QQuickVisualDataModel *vdm) -{ - accessors->cleanup(*this, vdm); - accessors = &qt_vdm_null_accessors; - // Don't clear the model object as we still need the guard to clear the list variant if the - // object is destroyed. -} - -bool QQuickVisualAdaptorModel::isValid() const -{ - return accessors != &qt_vdm_null_accessors; -} - -void QQuickVisualAdaptorModel::objectDestroyed(QObject *) -{ - setModel(QVariant(), 0, 0); -} - -QQuickVisualAdaptorModelEngineData::QQuickVisualAdaptorModelEngineData(QV8Engine *) -{ - strings = qPersistentNew(v8::Array::New(StringCount)); - strings->Set(Index, v8::String::New("index")); - strings->Set(ModelData, v8::String::New("modelData")); - strings->Set(HasModelChildren, v8::String::New("hasModelChildren")); - - v8::Local<v8::FunctionTemplate> listItem = v8::FunctionTemplate::New(); - listItem->InstanceTemplate()->SetHasExternalResource(true); - listItem->InstanceTemplate()->SetAccessor(index(), get_index); - listItem->InstanceTemplate()->SetAccessor( - modelData(), - QQuickVDMListAccessorData::get_modelData, - QQuickVDMListAccessorData::set_modelData); - constructorListItem = qPersistentNew(listItem->GetFunction()); -} - -QQuickVisualAdaptorModelEngineData::~QQuickVisualAdaptorModelEngineData() -{ - qPersistentDispose(constructorListItem); - qPersistentDispose(strings); -} - -QT_END_NAMESPACE - -#include <qquickvisualadaptormodel.moc> diff --git a/src/quick/items/qquickvisualadaptormodel_p.h b/src/quick/items/qquickvisualadaptormodel_p.h deleted file mode 100644 index 40890f8560..0000000000 --- a/src/quick/items/qquickvisualadaptormodel_p.h +++ /dev/null @@ -1,156 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal -** -** This file is part of the QtQml module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QQUICKVISUALADAPTORMODEL_P_H -#define QQUICKVISUALADAPTORMODEL_P_H - -#include <QtCore/qabstractitemmodel.h> - -#include "private/qquicklistaccessor_p.h" - -#include <private/qqmlguard_p.h> - -QT_BEGIN_HEADER - -QT_BEGIN_NAMESPACE - -class QQmlEngine; - -class QQuickVisualDataModel; -class QQuickVisualDataModelItem; -class QQuickVisualDataModelItemMetaType; - -class QQuickVisualAdaptorModel : public QQmlGuard<QObject> -{ -public: - class Accessors - { - public: - inline Accessors() {} - virtual ~Accessors(); - virtual int count(const QQuickVisualAdaptorModel &) const { return 0; } - virtual void cleanup(QQuickVisualAdaptorModel &, QQuickVisualDataModel * = 0) const {} - - virtual QVariant value(const QQuickVisualAdaptorModel &, int, const QString &) const { - return QVariant(); } - - virtual QQuickVisualDataModelItem *createItem( - QQuickVisualAdaptorModel &, - QQuickVisualDataModelItemMetaType *, - QQmlEngine *, - int) const { return 0; } - - virtual bool notify( - const QQuickVisualAdaptorModel &, - const QList<QQuickVisualDataModelItem *> &, - int, - int, - const QVector<int> &) const { return false; } - virtual void replaceWatchedRoles( - QQuickVisualAdaptorModel &, - const QList<QByteArray> &, - const QList<QByteArray> &) const {} - virtual QVariant parentModelIndex(const QQuickVisualAdaptorModel &) const { - return QVariant(); } - virtual QVariant modelIndex(const QQuickVisualAdaptorModel &, int) const { - return QVariant(); } - virtual bool canFetchMore(const QQuickVisualAdaptorModel &) const { return false; } - virtual void fetchMore(QQuickVisualAdaptorModel &) const {} - }; - - const Accessors *accessors; - QPersistentModelIndex rootIndex; - QQuickListAccessor list; - - QQuickVisualAdaptorModel(); - ~QQuickVisualAdaptorModel(); - - inline QVariant model() const { return list.list(); } - void setModel(const QVariant &variant, QQuickVisualDataModel *vdm, QQmlEngine *engine); - void invalidateModel(QQuickVisualDataModel *vdm); - - bool isValid() const; - - inline QAbstractItemModel *aim() { return static_cast<QAbstractItemModel *>(object()); } - inline const QAbstractItemModel *aim() const { return static_cast<const QAbstractItemModel *>(object()); } - - inline int count() const { return qMax(0, accessors->count(*this)); } - inline QVariant value(int index, const QString &role) const { - return accessors->value(*this, index, role); } - inline QQuickVisualDataModelItem *createItem(QQuickVisualDataModelItemMetaType *metaType, QQmlEngine *engine, int index) { - return accessors->createItem(*this, metaType, engine, index); } - inline bool hasProxyObject() const { - return list.type() == QQuickListAccessor::Instance || list.type() == QQuickListAccessor::ListProperty; } - - inline bool notify( - const QList<QQuickVisualDataModelItem *> &items, - int index, - int count, - const QVector<int> &roles) const { - return accessors->notify(*this, items, index, count, roles); } - inline void replaceWatchedRoles( - const QList<QByteArray> &oldRoles, const QList<QByteArray> &newRoles) { - accessors->replaceWatchedRoles(*this, oldRoles, newRoles); } - - inline QVariant modelIndex(int index) const { return accessors->modelIndex(*this, index); } - inline QVariant parentModelIndex() const { return accessors->parentModelIndex(*this); } - inline bool canFetchMore() const { return accessors->canFetchMore(*this); } - inline void fetchMore() { return accessors->fetchMore(*this); } - -protected: - void objectDestroyed(QObject *); -}; - -class QQuickVisualAdaptorModelProxyInterface -{ -public: - virtual ~QQuickVisualAdaptorModelProxyInterface() {} - - virtual QObject *proxiedObject() = 0; -}; - -#define QQuickVisualAdaptorModelProxyInterface_iid "org.qt-project.Qt.QQuickVisualAdaptorModelProxyInterface" - -Q_DECLARE_INTERFACE(QQuickVisualAdaptorModelProxyInterface, QQuickVisualAdaptorModelProxyInterface_iid) - -QT_END_NAMESPACE - -#endif diff --git a/src/quick/items/qquickvisualdatamodel.cpp b/src/quick/items/qquickvisualdatamodel.cpp deleted file mode 100644 index d2e50e8b8a..0000000000 --- a/src/quick/items/qquickvisualdatamodel.cpp +++ /dev/null @@ -1,3173 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal -** -** This file is part of the QtQml module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "qquickvisualdatamodel_p_p.h" -#include "qquickitem.h" - -#include <QtQml/qqmlinfo.h> - -#include <private/qquickpackage_p.h> -#include <private/qmetaobjectbuilder_p.h> -#include <private/qquickvisualadaptormodel_p.h> -#include <private/qquickchangeset_p.h> -#include <private/qqmlengine_p.h> -#include <private/qqmlcomponent_p.h> -#include <private/qqmlincubator_p.h> -#include <private/qqmlcompiler_p.h> - -QT_BEGIN_NAMESPACE - -class QQuickVisualDataModelEngineData : public QV8Engine::Deletable -{ -public: - enum - { - Model, - Groups, - IsUnresolved, - ItemsIndex, - PersistedItemsIndex, - InItems, - InPersistedItems, - StringCount - }; - - QQuickVisualDataModelEngineData(QV8Engine *engine); - ~QQuickVisualDataModelEngineData(); - - v8::Local<v8::Object> array( - QV8Engine *engine, const QVector<QQuickChangeSet::Remove> &changes); - v8::Local<v8::Object> array( - QV8Engine *engine, const QVector<QQuickChangeSet::Insert> &changes); - v8::Local<v8::Object> array( - QV8Engine *engine, const QVector<QQuickChangeSet::Change> &changes); - - - inline v8::Local<v8::String> model() { return strings->Get(Model)->ToString(); } - inline v8::Local<v8::String> groups() { return strings->Get(Groups)->ToString(); } - inline v8::Local<v8::String> isUnresolved() { return strings->Get(IsUnresolved)->ToString(); } - inline v8::Local<v8::String> itemsIndex() { return strings->Get(ItemsIndex)->ToString(); } - inline v8::Local<v8::String> persistedItemsIndex() { return strings->Get(PersistedItemsIndex)->ToString(); } - inline v8::Local<v8::String> inItems() { return strings->Get(InItems)->ToString(); } - inline v8::Local<v8::String> inPersistedItems() { return strings->Get(InPersistedItems)->ToString(); } - - v8::Persistent<v8::Array> strings; - v8::Persistent<v8::Function> constructorChange; - v8::Persistent<v8::Function> constructorChangeArray; -}; - -V8_DEFINE_EXTENSION(QQuickVisualDataModelEngineData, engineData) - - -void QQuickVisualDataModelPartsMetaObject::propertyCreated(int, QMetaPropertyBuilder &prop) -{ - prop.setWritable(false); -} - -QVariant QQuickVisualDataModelPartsMetaObject::initialValue(int id) -{ - QQuickVisualDataModelParts *parts = static_cast<QQuickVisualDataModelParts *>(object()); - QQuickVisualPartsModel *m = new QQuickVisualPartsModel( - parts->model, QString::fromUtf8(name(id)), parts); - parts->models.append(m); - return QVariant::fromValue(static_cast<QObject *>(m)); -} - -QQuickVisualDataModelParts::QQuickVisualDataModelParts(QQuickVisualDataModel *parent) -: QObject(parent), model(parent) -{ - new QQuickVisualDataModelPartsMetaObject(this); -} - -//--------------------------------------------------------------------------- - -/*! - \qmltype VisualDataModel - \instantiates QQuickVisualDataModel - \inqmlmodule QtQuick 2 - \ingroup qtquick-models - \brief Encapsulates a model and delegate - - The VisualDataModel type encapsulates a model and the delegate that will - be instantiated for items in the model. - - It is usually not necessary to create a VisualDataModel. - However, it can be useful for manipulating and accessing the \l modelIndex - when a QAbstractItemModel subclass is used as the - model. Also, VisualDataModel is used together with \l Package to - provide delegates to multiple views, and with VisualDataGroup to sort and filter - delegate items. - - The example below illustrates using a VisualDataModel with a ListView. - - \snippet qml/visualdatamodel.qml 0 -*/ - -QQuickVisualDataModelPrivate::QQuickVisualDataModelPrivate(QQmlContext *ctxt) - : m_delegate(0) - , m_cacheMetaType(0) - , m_context(ctxt) - , m_parts(0) - , m_filterGroup(QStringLiteral("items")) - , m_count(0) - , m_groupCount(Compositor::MinimumGroupCount) - , m_compositorGroup(Compositor::Cache) - , m_complete(false) - , m_delegateValidated(false) - , m_reset(false) - , m_transaction(false) - , m_incubatorCleanupScheduled(false) - , m_cacheItems(0) - , m_items(0) - , m_persistedItems(0) -{ -} - -QQuickVisualDataModelPrivate::~QQuickVisualDataModelPrivate() -{ - qDeleteAll(m_finishedIncubating); - - if (m_cacheMetaType) - m_cacheMetaType->release(); -} - -void QQuickVisualDataModelPrivate::init() -{ - Q_Q(QQuickVisualDataModel); - m_compositor.setRemoveGroups(Compositor::GroupMask & ~Compositor::PersistedFlag); - - m_items = new QQuickVisualDataGroup(QStringLiteral("items"), q, Compositor::Default, q); - m_items->setDefaultInclude(true); - m_persistedItems = new QQuickVisualDataGroup(QStringLiteral("persistedItems"), q, Compositor::Persisted, q); - QQuickVisualDataGroupPrivate::get(m_items)->emitters.insert(this); -} - -QQuickVisualDataModel::QQuickVisualDataModel() -: QQuickVisualModel(*(new QQuickVisualDataModelPrivate(0))) -{ - Q_D(QQuickVisualDataModel); - d->init(); -} - -QQuickVisualDataModel::QQuickVisualDataModel(QQmlContext *ctxt, QObject *parent) -: QQuickVisualModel(*(new QQuickVisualDataModelPrivate(ctxt)), parent) -{ - Q_D(QQuickVisualDataModel); - d->init(); -} - -QQuickVisualDataModel::~QQuickVisualDataModel() -{ - Q_D(QQuickVisualDataModel); - - foreach (QQuickVisualDataModelItem *cacheItem, d->m_cache) { - if (cacheItem->object) { - delete cacheItem->object; - - cacheItem->object = 0; - cacheItem->contextData->destroy(); - cacheItem->contextData = 0; - cacheItem->scriptRef -= 1; - } - cacheItem->groups &= ~Compositor::UnresolvedFlag; - cacheItem->objectRef = 0; - if (!cacheItem->isReferenced()) - delete cacheItem; - } -} - - -void QQuickVisualDataModel::classBegin() -{ - Q_D(QQuickVisualDataModel); - if (!d->m_context) - d->m_context = qmlContext(this); -} - -void QQuickVisualDataModel::componentComplete() -{ - Q_D(QQuickVisualDataModel); - d->m_complete = true; - - int defaultGroups = 0; - QStringList groupNames; - groupNames.append(QStringLiteral("items")); - groupNames.append(QStringLiteral("persistedItems")); - if (QQuickVisualDataGroupPrivate::get(d->m_items)->defaultInclude) - defaultGroups |= Compositor::DefaultFlag; - if (QQuickVisualDataGroupPrivate::get(d->m_persistedItems)->defaultInclude) - defaultGroups |= Compositor::PersistedFlag; - for (int i = Compositor::MinimumGroupCount; i < d->m_groupCount; ++i) { - QString name = d->m_groups[i]->name(); - if (name.isEmpty()) { - d->m_groups[i] = d->m_groups[d->m_groupCount - 1]; - --d->m_groupCount; - --i; - } else if (name.at(0).isUpper()) { - qmlInfo(d->m_groups[i]) << QQuickVisualDataGroup::tr("Group names must start with a lower case letter"); - d->m_groups[i] = d->m_groups[d->m_groupCount - 1]; - --d->m_groupCount; - --i; - } else { - groupNames.append(name); - - QQuickVisualDataGroupPrivate *group = QQuickVisualDataGroupPrivate::get(d->m_groups[i]); - group->setModel(this, Compositor::Group(i)); - if (group->defaultInclude) - defaultGroups |= (1 << i); - } - } - - d->m_cacheMetaType = new QQuickVisualDataModelItemMetaType( - QQmlEnginePrivate::getV8Engine(d->m_context->engine()), this, groupNames); - - d->m_compositor.setGroupCount(d->m_groupCount); - d->m_compositor.setDefaultGroups(defaultGroups); - d->updateFilterGroup(); - - while (!d->m_pendingParts.isEmpty()) - static_cast<QQuickVisualPartsModel *>(d->m_pendingParts.first())->updateFilterGroup(); - - QVector<Compositor::Insert> inserts; - d->m_count = d->m_adaptorModel.count(); - d->m_compositor.append( - &d->m_adaptorModel, - 0, - d->m_count, - defaultGroups | Compositor::AppendFlag | Compositor::PrependFlag, - &inserts); - d->itemsInserted(inserts); - d->emitChanges(); - - if (d->m_adaptorModel.canFetchMore()) - QCoreApplication::postEvent(this, new QEvent(QEvent::UpdateRequest)); -} - -/*! - \qmlproperty model QtQuick2::VisualDataModel::model - This property holds the model providing data for the VisualDataModel. - - The model provides a set of data that is used to create the items - for a view. For large or dynamic datasets the model is usually - provided by a C++ model object. The C++ model object must be a \l - {QAbstractItemModel} subclass or a simple list. - - Models can also be created directly in QML, using a \l{ListModel} or - \l{XmlListModel}. - - \sa {qml-data-models}{Data Models} -*/ -QVariant QQuickVisualDataModel::model() const -{ - Q_D(const QQuickVisualDataModel); - return d->m_adaptorModel.model(); -} - -void QQuickVisualDataModel::setModel(const QVariant &model) -{ - Q_D(QQuickVisualDataModel); - - if (d->m_complete) - _q_itemsRemoved(0, d->m_count); - - d->m_adaptorModel.setModel(model, this, d->m_context->engine()); - d->m_adaptorModel.replaceWatchedRoles(QList<QByteArray>(), d->m_watchedRoles); - for (int i = 0; d->m_parts && i < d->m_parts->models.count(); ++i) { - d->m_adaptorModel.replaceWatchedRoles( - QList<QByteArray>(), d->m_parts->models.at(i)->watchedRoles()); - } - - if (d->m_complete) { - _q_itemsInserted(0, d->m_adaptorModel.count()); - if (d->m_adaptorModel.canFetchMore()) - QCoreApplication::postEvent(this, new QEvent(QEvent::UpdateRequest)); - } -} - -/*! - \qmlproperty Component QtQuick2::VisualDataModel::delegate - - The delegate provides a template defining each item instantiated by a view. - The index is exposed as an accessible \c index property. Properties of the - model are also available depending upon the type of \l {qml-data-models}{Data Model}. -*/ -QQmlComponent *QQuickVisualDataModel::delegate() const -{ - Q_D(const QQuickVisualDataModel); - return d->m_delegate; -} - -void QQuickVisualDataModel::setDelegate(QQmlComponent *delegate) -{ - Q_D(QQuickVisualDataModel); - if (d->m_transaction) { - qmlInfo(this) << tr("The delegate of a VisualDataModel cannot be changed within onUpdated."); - return; - } - bool wasValid = d->m_delegate != 0; - d->m_delegate = delegate; - d->m_delegateValidated = false; - if (wasValid && d->m_complete) { - for (int i = 1; i < d->m_groupCount; ++i) { - QQuickVisualDataGroupPrivate::get(d->m_groups[i])->changeSet.remove( - 0, d->m_compositor.count(Compositor::Group(i))); - } - } - if (d->m_complete && d->m_delegate) { - for (int i = 1; i < d->m_groupCount; ++i) { - QQuickVisualDataGroupPrivate::get(d->m_groups[i])->changeSet.insert( - 0, d->m_compositor.count(Compositor::Group(i))); - } - } - d->emitChanges(); -} - -/*! - \qmlproperty QModelIndex QtQuick2::VisualDataModel::rootIndex - - QAbstractItemModel provides a hierarchical tree of data, whereas - QML only operates on list data. \c rootIndex allows the children of - any node in a QAbstractItemModel to be provided by this model. - - This property only affects models of type QAbstractItemModel that - are hierarchical (e.g, a tree model). - - For example, here is a simple interactive file system browser. - When a directory name is clicked, the view's \c rootIndex is set to the - QModelIndex node of the clicked directory, thus updating the view to show - the new directory's contents. - - \c main.cpp: - \snippet qml/visualdatamodel_rootindex/main.cpp 0 - - \c view.qml: - \snippet qml/visualdatamodel_rootindex/view.qml 0 - - If the \l model is a QAbstractItemModel subclass, the delegate can also - reference a \c hasModelChildren property (optionally qualified by a - \e model. prefix) that indicates whether the delegate's model item has - any child nodes. - - - \sa modelIndex(), parentModelIndex() -*/ -QVariant QQuickVisualDataModel::rootIndex() const -{ - Q_D(const QQuickVisualDataModel); - return QVariant::fromValue(QModelIndex(d->m_adaptorModel.rootIndex)); -} - -void QQuickVisualDataModel::setRootIndex(const QVariant &root) -{ - Q_D(QQuickVisualDataModel); - - QModelIndex modelIndex = qvariant_cast<QModelIndex>(root); - const bool changed = d->m_adaptorModel.rootIndex != modelIndex; - if (changed || !d->m_adaptorModel.isValid()) { - const int oldCount = d->m_count; - d->m_adaptorModel.rootIndex = modelIndex; - if (!d->m_adaptorModel.isValid() && d->m_adaptorModel.aim()) // The previous root index was invalidated, so we need to reconnect the model. - d->m_adaptorModel.setModel(d->m_adaptorModel.list.list(), this, d->m_context->engine()); - if (d->m_adaptorModel.canFetchMore()) - d->m_adaptorModel.fetchMore(); - if (d->m_complete) { - const int newCount = d->m_adaptorModel.count(); - if (oldCount) - _q_itemsRemoved(0, oldCount); - if (newCount) - _q_itemsInserted(0, newCount); - } - if (changed) - emit rootIndexChanged(); - } -} - -/*! - \qmlmethod QModelIndex QtQuick2::VisualDataModel::modelIndex(int index) - - QAbstractItemModel provides a hierarchical tree of data, whereas - QML only operates on list data. This function assists in using - tree models in QML. - - Returns a QModelIndex for the specified index. - This value can be assigned to rootIndex. - - \sa rootIndex -*/ -QVariant QQuickVisualDataModel::modelIndex(int idx) const -{ - Q_D(const QQuickVisualDataModel); - return d->m_adaptorModel.modelIndex(idx); -} - -/*! - \qmlmethod QModelIndex QtQuick2::VisualDataModel::parentModelIndex() - - QAbstractItemModel provides a hierarchical tree of data, whereas - QML only operates on list data. This function assists in using - tree models in QML. - - Returns a QModelIndex for the parent of the current rootIndex. - This value can be assigned to rootIndex. - - \sa rootIndex -*/ -QVariant QQuickVisualDataModel::parentModelIndex() const -{ - Q_D(const QQuickVisualDataModel); - return d->m_adaptorModel.parentModelIndex(); -} - -/*! - \qmlproperty int QtQuick2::VisualDataModel::count -*/ - -int QQuickVisualDataModel::count() const -{ - Q_D(const QQuickVisualDataModel); - if (!d->m_delegate) - return 0; - return d->m_compositor.count(d->m_compositorGroup); -} - -QQuickVisualDataModel::ReleaseFlags QQuickVisualDataModelPrivate::release(QObject *object) -{ - QQuickVisualDataModel::ReleaseFlags stat = 0; - if (!object) - return stat; - - if (QQuickVisualDataModelItem *cacheItem = QQuickVisualDataModelItem::dataForObject(object)) { - if (cacheItem->releaseObject()) { - cacheItem->destroyObject(); - if (QQuickItem *item = qmlobject_cast<QQuickItem *>(object)) - emitDestroyingItem(item); - if (cacheItem->incubationTask) { - releaseIncubator(cacheItem->incubationTask); - cacheItem->incubationTask = 0; - } - cacheItem->Dispose(); - stat |= QQuickVisualModel::Destroyed; - } else { - stat |= QQuickVisualDataModel::Referenced; - } - } - return stat; -} - -/* - Returns ReleaseStatus flags. -*/ - -QQuickVisualDataModel::ReleaseFlags QQuickVisualDataModel::release(QQuickItem *item) -{ - Q_D(QQuickVisualDataModel); - QQuickVisualModel::ReleaseFlags stat = d->release(item); - if (stat & Destroyed) - item->setParentItem(0); - return stat; -} - -// Cancel a requested async item -void QQuickVisualDataModel::cancel(int index) -{ - Q_D(QQuickVisualDataModel); - if (!d->m_delegate || index < 0 || index >= d->m_compositor.count(d->m_compositorGroup)) { - qWarning() << "VisualDataModel::cancel: index out range" << index << d->m_compositor.count(d->m_compositorGroup); - return; - } - - Compositor::iterator it = d->m_compositor.find(d->m_compositorGroup, index); - QQuickVisualDataModelItem *cacheItem = it->inCache() ? d->m_cache.at(it.cacheIndex) : 0; - if (cacheItem) { - if (cacheItem->incubationTask && !cacheItem->isObjectReferenced()) { - d->releaseIncubator(cacheItem->incubationTask); - cacheItem->incubationTask = 0; - - if (cacheItem->object) { - QObject *object = cacheItem->object; - cacheItem->destroyObject(); - if (QQuickItem *item = qmlobject_cast<QQuickItem *>(object)) - d->emitDestroyingItem(item); - else if (QQuickPackage *package = qmlobject_cast<QQuickPackage *>(object)) - d->emitDestroyingPackage(package); - } - - cacheItem->scriptRef -= 1; - } - if (!cacheItem->isReferenced()) { - d->m_compositor.clearFlags(Compositor::Cache, it.cacheIndex, 1, Compositor::CacheFlag); - d->m_cache.removeAt(it.cacheIndex); - delete cacheItem; - Q_ASSERT(d->m_cache.count() == d->m_compositor.count(Compositor::Cache)); - } - } -} - -void QQuickVisualDataModelPrivate::group_append( - QQmlListProperty<QQuickVisualDataGroup> *property, QQuickVisualDataGroup *group) -{ - QQuickVisualDataModelPrivate *d = static_cast<QQuickVisualDataModelPrivate *>(property->data); - if (d->m_complete) - return; - if (d->m_groupCount == Compositor::MaximumGroupCount) { - qmlInfo(d->q_func()) << QQuickVisualDataModel::tr("The maximum number of supported VisualDataGroups is 8"); - return; - } - d->m_groups[d->m_groupCount] = group; - d->m_groupCount += 1; -} - -int QQuickVisualDataModelPrivate::group_count( - QQmlListProperty<QQuickVisualDataGroup> *property) -{ - QQuickVisualDataModelPrivate *d = static_cast<QQuickVisualDataModelPrivate *>(property->data); - return d->m_groupCount - 1; -} - -QQuickVisualDataGroup *QQuickVisualDataModelPrivate::group_at( - QQmlListProperty<QQuickVisualDataGroup> *property, int index) -{ - QQuickVisualDataModelPrivate *d = static_cast<QQuickVisualDataModelPrivate *>(property->data); - return index >= 0 && index < d->m_groupCount - 1 - ? d->m_groups[index + 1] - : 0; -} - -/*! - \qmlproperty list<VisualDataGroup> QtQuick2::VisualDataModel::groups - - This property holds a visual data model's group definitions. - - Groups define a sub-set of the items in a visual data model and can be used to filter - a model. - - For every group defined in a VisualDataModel two attached properties are added to each - delegate item. The first of the form VisualDataModel.in\e{GroupName} holds whether the - item belongs to the group and the second VisualDataModel.\e{groupName}Index holds the - index of the item in that group. - - The following example illustrates using groups to select items in a model. - - \snippet qml/visualdatagroup.qml 0 -*/ - -QQmlListProperty<QQuickVisualDataGroup> QQuickVisualDataModel::groups() -{ - Q_D(QQuickVisualDataModel); - return QQmlListProperty<QQuickVisualDataGroup>( - this, - d, - QQuickVisualDataModelPrivate::group_append, - QQuickVisualDataModelPrivate::group_count, - QQuickVisualDataModelPrivate::group_at, - 0); -} - -/*! - \qmlproperty VisualDataGroup QtQuick2::VisualDataModel::items - - This property holds visual data model's default group to which all new items are added. -*/ - -QQuickVisualDataGroup *QQuickVisualDataModel::items() -{ - Q_D(QQuickVisualDataModel); - return d->m_items; -} - -/*! - \qmlproperty VisualDataGroup QtQuick2::VisualDataModel::persistedItems - - This property holds visual data model's persisted items group. - - Items in this group are not destroyed when released by a view, instead they are persisted - until removed from the group. - - An item can be removed from the persistedItems group by setting the - VisualDataModel.inPersistedItems property to false. If the item is not referenced by a view - at that time it will be destroyed. Adding an item to this group will not create a new - instance. - - Items returned by the \l QtQuick2::VisualDataGroup::create() function are automatically added - to this group. -*/ - -QQuickVisualDataGroup *QQuickVisualDataModel::persistedItems() -{ - Q_D(QQuickVisualDataModel); - return d->m_persistedItems; -} - -/*! - \qmlproperty string QtQuick2::VisualDataModel::filterOnGroup - - This property holds the name of the group used to filter the visual data model. - - Only items which belong to this group are visible to a view. - - By default this is the \l items group. -*/ - -QString QQuickVisualDataModel::filterGroup() const -{ - Q_D(const QQuickVisualDataModel); - return d->m_filterGroup; -} - -void QQuickVisualDataModel::setFilterGroup(const QString &group) -{ - Q_D(QQuickVisualDataModel); - - if (d->m_transaction) { - qmlInfo(this) << tr("The group of a VisualDataModel cannot be changed within onChanged"); - return; - } - - if (d->m_filterGroup != group) { - d->m_filterGroup = group; - d->updateFilterGroup(); - emit filterGroupChanged(); - } -} - -void QQuickVisualDataModel::resetFilterGroup() -{ - setFilterGroup(QStringLiteral("items")); -} - -void QQuickVisualDataModelPrivate::updateFilterGroup() -{ - Q_Q(QQuickVisualDataModel); - if (!m_cacheMetaType) - return; - - QQuickListCompositor::Group previousGroup = m_compositorGroup; - m_compositorGroup = Compositor::Default; - for (int i = 1; i < m_groupCount; ++i) { - if (m_filterGroup == m_cacheMetaType->groupNames.at(i - 1)) { - m_compositorGroup = Compositor::Group(i); - break; - } - } - - QQuickVisualDataGroupPrivate::get(m_groups[m_compositorGroup])->emitters.insert(this); - if (m_compositorGroup != previousGroup) { - QVector<QQuickChangeSet::Remove> removes; - QVector<QQuickChangeSet::Insert> inserts; - m_compositor.transition(previousGroup, m_compositorGroup, &removes, &inserts); - - QQuickChangeSet changeSet; - changeSet.move(removes, inserts); - emit q->modelUpdated(changeSet, false); - - if (changeSet.difference() != 0) - emit q->countChanged(); - - if (m_parts) { - foreach (QQuickVisualPartsModel *model, m_parts->models) - model->updateFilterGroup(m_compositorGroup, changeSet); - } - } -} - -/*! - \qmlproperty object QtQuick2::VisualDataModel::parts - - The \a parts property selects a VisualDataModel which creates - delegates from the part named. This is used in conjunction with - the \l Package type. - - For example, the code below selects a model which creates - delegates named \e list from a \l Package: - - \code - VisualDataModel { - id: visualModel - delegate: Package { - Item { Package.name: "list" } - } - model: myModel - } - - ListView { - width: 200; height:200 - model: visualModel.parts.list - } - \endcode - - \sa Package -*/ - -QObject *QQuickVisualDataModel::parts() -{ - Q_D(QQuickVisualDataModel); - if (!d->m_parts) - d->m_parts = new QQuickVisualDataModelParts(this); - return d->m_parts; -} - -void QQuickVisualDataModelPrivate::emitCreatedPackage(QVDMIncubationTask *incubationTask, QQuickPackage *package) -{ - for (int i = 1; i < m_groupCount; ++i) - QQuickVisualDataGroupPrivate::get(m_groups[i])->createdPackage(incubationTask->index[i], package); -} - -void QQuickVisualDataModelPrivate::emitInitPackage(QVDMIncubationTask *incubationTask, QQuickPackage *package) -{ - for (int i = 1; i < m_groupCount; ++i) - QQuickVisualDataGroupPrivate::get(m_groups[i])->initPackage(incubationTask->index[i], package); -} - -void QQuickVisualDataModelPrivate::emitDestroyingPackage(QQuickPackage *package) -{ - for (int i = 1; i < m_groupCount; ++i) - QQuickVisualDataGroupPrivate::get(m_groups[i])->destroyingPackage(package); -} - -void QVDMIncubationTask::statusChanged(Status status) -{ - vdm->incubatorStatusChanged(this, status); -} - -void QQuickVisualDataModelPrivate::releaseIncubator(QVDMIncubationTask *incubationTask) -{ - Q_Q(QQuickVisualDataModel); - if (!incubationTask->isError()) - incubationTask->clear(); - m_finishedIncubating.append(incubationTask); - if (!m_incubatorCleanupScheduled) { - m_incubatorCleanupScheduled = true; - QCoreApplication::postEvent(q, new QEvent(QEvent::User)); - } -} - -void QQuickVisualDataModelPrivate::removeCacheItem(QQuickVisualDataModelItem *cacheItem) -{ - int cidx = m_cache.indexOf(cacheItem); - if (cidx >= 0) { - m_compositor.clearFlags(Compositor::Cache, cidx, 1, Compositor::CacheFlag); - m_cache.removeAt(cidx); - } - Q_ASSERT(m_cache.count() == m_compositor.count(Compositor::Cache)); -} - -void QQuickVisualDataModelPrivate::incubatorStatusChanged(QVDMIncubationTask *incubationTask, QQmlIncubator::Status status) -{ - Q_Q(QQuickVisualDataModel); - if (status != QQmlIncubator::Ready && status != QQmlIncubator::Error) - return; - - QQuickVisualDataModelItem *cacheItem = incubationTask->incubating; - cacheItem->incubationTask = 0; - incubationTask->incubating = 0; - releaseIncubator(incubationTask); - - if (status == QQmlIncubator::Ready) { - if (QQuickItem *item = qmlobject_cast<QQuickItem *>(cacheItem->object)) - emitCreatedItem(incubationTask, item); - else if (QQuickPackage *package = qmlobject_cast<QQuickPackage *>(cacheItem->object)) - emitCreatedPackage(incubationTask, package); - } else if (status == QQmlIncubator::Error) { - qmlInfo(q, m_delegate->errors()) << "Error creating delegate"; - } - - if (!cacheItem->isObjectReferenced()) { - if (QQuickItem *item = qmlobject_cast<QQuickItem *>(cacheItem->object)) - emitDestroyingItem(item); - else if (QQuickPackage *package = qmlobject_cast<QQuickPackage *>(cacheItem->object)) - emitDestroyingPackage(package); - delete cacheItem->object; - cacheItem->object = 0; - cacheItem->scriptRef -= 1; - cacheItem->contextData->destroy(); - cacheItem->contextData = 0; - if (!cacheItem->isReferenced()) { - removeCacheItem(cacheItem); - delete cacheItem; - } - } -} - -void QVDMIncubationTask::setInitialState(QObject *o) -{ - vdm->setInitialState(this, o); -} - -void QQuickVisualDataModelPrivate::setInitialState(QVDMIncubationTask *incubationTask, QObject *o) -{ - QQuickVisualDataModelItem *cacheItem = incubationTask->incubating; - cacheItem->object = o; - - if (QQuickItem *item = qmlobject_cast<QQuickItem *>(cacheItem->object)) - emitInitItem(incubationTask, item); - else if (QQuickPackage *package = qmlobject_cast<QQuickPackage *>(cacheItem->object)) - emitInitPackage(incubationTask, package); -} - -QObject *QQuickVisualDataModelPrivate::object(Compositor::Group group, int index, bool asynchronous) -{ - Q_Q(QQuickVisualDataModel); - if (!m_delegate || index < 0 || index >= m_compositor.count(group)) { - qWarning() << "VisualDataModel::item: index out range" << index << m_compositor.count(group); - return 0; - } else if (!m_context->isValid()) { - return 0; - } - - Compositor::iterator it = m_compositor.find(group, index); - - QQuickVisualDataModelItem *cacheItem = it->inCache() ? m_cache.at(it.cacheIndex) : 0; - - if (!cacheItem) { - cacheItem = m_adaptorModel.createItem(m_cacheMetaType, m_context->engine(), it.modelIndex()); - if (!cacheItem) - return 0; - - cacheItem->groups = it->flags; - - m_cache.insert(it.cacheIndex, cacheItem); - m_compositor.setFlags(it, 1, Compositor::CacheFlag); - Q_ASSERT(m_cache.count() == m_compositor.count(Compositor::Cache)); - } - - // Bump the reference counts temporarily so neither the content data or the delegate object - // are deleted if incubatorStatusChanged() is called synchronously. - cacheItem->scriptRef += 1; - cacheItem->referenceObject(); - - if (cacheItem->incubationTask) { - if (!asynchronous && cacheItem->incubationTask->incubationMode() == QQmlIncubator::Asynchronous) { - // previously requested async - now needed immediately - cacheItem->incubationTask->forceCompletion(); - } - } else if (!cacheItem->object) { - QQmlContext *creationContext = m_delegate->creationContext(); - - cacheItem->scriptRef += 1; - - cacheItem->incubationTask = new QVDMIncubationTask(this, asynchronous ? QQmlIncubator::Asynchronous : QQmlIncubator::AsynchronousIfNested); - cacheItem->incubationTask->incubating = cacheItem; - cacheItem->incubationTask->clear(); - - for (int i = 1; i < m_groupCount; ++i) - cacheItem->incubationTask->index[i] = it.index[i]; - - QQmlContextData *ctxt = new QQmlContextData; - ctxt->setParent(QQmlContextData::get(creationContext ? creationContext : m_context)); - ctxt->contextObject = cacheItem; - cacheItem->contextData = ctxt; - - if (m_adaptorModel.hasProxyObject()) { - if (QQuickVisualAdaptorModelProxyInterface *proxy - = qobject_cast<QQuickVisualAdaptorModelProxyInterface *>(cacheItem)) { - ctxt = new QQmlContextData; - ctxt->setParent(cacheItem->contextData, true); - ctxt->contextObject = proxy->proxiedObject(); - } - } - - cacheItem->incubateObject( - m_delegate, - m_context->engine(), - ctxt, - QQmlContextData::get(m_context)); - } - - if (index == m_compositor.count(group) - 1 && m_adaptorModel.canFetchMore()) - QCoreApplication::postEvent(q, new QEvent(QEvent::UpdateRequest)); - - // Remove the temporary reference count. - cacheItem->scriptRef -= 1; - if (cacheItem->object) - return cacheItem->object; - - cacheItem->releaseObject(); - if (!cacheItem->isReferenced()) { - removeCacheItem(cacheItem); - delete cacheItem; - } - - return 0; -} - -/* - If asynchronous is true or the component is being loaded asynchronously due - to an ancestor being loaded asynchronously, item() may return 0. In this - case itemCreated() will be emitted when the item is available. The item - at this stage does not have any references, so item() must be called again - to ensure a reference is held. Any call to item() which returns a valid item - must be matched by a call to release() in order to destroy the item. -*/ -QQuickItem *QQuickVisualDataModel::item(int index, bool asynchronous) -{ - Q_D(QQuickVisualDataModel); - if (!d->m_delegate || index < 0 || index >= d->m_compositor.count(d->m_compositorGroup)) { - qWarning() << "VisualDataModel::item: index out range" << index << d->m_compositor.count(d->m_compositorGroup); - return 0; - } - - QObject *object = d->object(d->m_compositorGroup, index, asynchronous); - if (!object) - return 0; - - if (QQuickItem *item = qmlobject_cast<QQuickItem *>(object)) - return item; - - d->release(object); - if (!d->m_delegateValidated) { - if (object) - qmlInfo(d->m_delegate) << QQuickVisualDataModel::tr("Delegate component must be Item type."); - d->m_delegateValidated = true; - } - return 0; -} - -QString QQuickVisualDataModelPrivate::stringValue(Compositor::Group group, int index, const QString &name) -{ - Compositor::iterator it = m_compositor.find(group, index); - if (QQuickVisualAdaptorModel *model = it.list<QQuickVisualAdaptorModel>()) { - QString role = name; - int dot = name.indexOf(QLatin1Char('.')); - if (dot > 0) - role = name.left(dot); - QVariant value = model->value(it.modelIndex(), role); - while (dot > 0) { - QObject *obj = qvariant_cast<QObject*>(value); - if (!obj) - return QString(); - int from = dot+1; - dot = name.indexOf(QLatin1Char('.'), from); - value = obj->property(name.mid(from, dot-from).toUtf8()); - } - return value.toString(); - } - return QString(); -} - -QString QQuickVisualDataModel::stringValue(int index, const QString &name) -{ - Q_D(QQuickVisualDataModel); - return d->stringValue(d->m_compositorGroup, index, name); -} - -int QQuickVisualDataModel::indexOf(QQuickItem *item, QObject *) const -{ - Q_D(const QQuickVisualDataModel); - if (QQuickVisualDataModelItem *cacheItem = QQuickVisualDataModelItem::dataForObject(item)) - return cacheItem->groupIndex(d->m_compositorGroup); - return -1; -} - -void QQuickVisualDataModel::setWatchedRoles(QList<QByteArray> roles) -{ - Q_D(QQuickVisualDataModel); - d->m_adaptorModel.replaceWatchedRoles(d->m_watchedRoles, roles); - d->m_watchedRoles = roles; -} - -void QQuickVisualDataModelPrivate::addGroups( - Compositor::iterator from, int count, Compositor::Group group, int groupFlags) -{ - QVector<Compositor::Insert> inserts; - m_compositor.setFlags(from, count, group, groupFlags, &inserts); - itemsInserted(inserts); - emitChanges(); -} - -void QQuickVisualDataModelPrivate::removeGroups( - Compositor::iterator from, int count, Compositor::Group group, int groupFlags) -{ - QVector<Compositor::Remove> removes; - m_compositor.clearFlags(from, count, group, groupFlags, &removes); - itemsRemoved(removes); - emitChanges(); -} - -void QQuickVisualDataModelPrivate::setGroups( - Compositor::iterator from, int count, Compositor::Group group, int groupFlags) -{ - QVector<Compositor::Remove> removes; - QVector<Compositor::Insert> inserts; - - m_compositor.setFlags(from, count, group, groupFlags, &inserts); - itemsInserted(inserts); - const int removeFlags = ~groupFlags & Compositor::GroupMask; - - from = m_compositor.find(from.group, from.index[from.group]); - m_compositor.clearFlags(from, count, group, removeFlags, &removes); - itemsRemoved(removes); - emitChanges(); -} - -bool QQuickVisualDataModel::event(QEvent *e) -{ - Q_D(QQuickVisualDataModel); - if (e->type() == QEvent::UpdateRequest) { - d->m_adaptorModel.fetchMore(); - } else if (e->type() == QEvent::User) { - d->m_incubatorCleanupScheduled = false; - qDeleteAll(d->m_finishedIncubating); - d->m_finishedIncubating.clear(); - } - return QQuickVisualModel::event(e); -} - -void QQuickVisualDataModelPrivate::itemsChanged(const QVector<Compositor::Change> &changes) -{ - if (!m_delegate) - return; - - QVarLengthArray<QVector<QQuickChangeSet::Change>, Compositor::MaximumGroupCount> translatedChanges(m_groupCount); - - foreach (const Compositor::Change &change, changes) { - for (int i = 1; i < m_groupCount; ++i) { - if (change.inGroup(i)) { - translatedChanges[i].append(QQuickChangeSet::Change(change.index[i], change.count)); - } - } - } - - for (int i = 1; i < m_groupCount; ++i) - QQuickVisualDataGroupPrivate::get(m_groups[i])->changeSet.change(translatedChanges.at(i)); -} - -void QQuickVisualDataModel::_q_itemsChanged(int index, int count, const QVector<int> &roles) -{ - Q_D(QQuickVisualDataModel); - if (count <= 0 || !d->m_complete) - return; - - if (d->m_adaptorModel.notify(d->m_cache, index, count, roles)) { - QVector<Compositor::Change> changes; - d->m_compositor.listItemsChanged(&d->m_adaptorModel, index, count, &changes); - d->itemsChanged(changes); - d->emitChanges(); - } -} - -static void incrementIndexes(QQuickVisualDataModelItem *cacheItem, int count, const int *deltas) -{ - if (QVDMIncubationTask *incubationTask = cacheItem->incubationTask) { - for (int i = 1; i < count; ++i) - incubationTask->index[i] += deltas[i]; - } - if (QQuickVisualDataModelAttached *attached = cacheItem->attached) { - for (int i = 1; i < count; ++i) - attached->m_currentIndex[i] += deltas[i]; - } -} - -void QQuickVisualDataModelPrivate::itemsInserted( - const QVector<Compositor::Insert> &inserts, - QVarLengthArray<QVector<QQuickChangeSet::Insert>, Compositor::MaximumGroupCount> *translatedInserts, - QHash<int, QList<QQuickVisualDataModelItem *> > *movedItems) -{ - int cacheIndex = 0; - - int inserted[Compositor::MaximumGroupCount]; - for (int i = 1; i < m_groupCount; ++i) - inserted[i] = 0; - - foreach (const Compositor::Insert &insert, inserts) { - for (; cacheIndex < insert.cacheIndex; ++cacheIndex) - incrementIndexes(m_cache.at(cacheIndex), m_groupCount, inserted); - - for (int i = 1; i < m_groupCount; ++i) { - if (insert.inGroup(i)) { - (*translatedInserts)[i].append( - QQuickChangeSet::Insert(insert.index[i], insert.count, insert.moveId)); - inserted[i] += insert.count; - } - } - - if (!insert.inCache()) - continue; - - if (movedItems && insert.isMove()) { - QList<QQuickVisualDataModelItem *> items = movedItems->take(insert.moveId); - Q_ASSERT(items.count() == insert.count); - m_cache = m_cache.mid(0, insert.cacheIndex) + items + m_cache.mid(insert.cacheIndex); - } - if (insert.inGroup()) { - for (int offset = 0; cacheIndex < insert.cacheIndex + insert.count; ++cacheIndex, ++offset) { - QQuickVisualDataModelItem *cacheItem = m_cache.at(cacheIndex); - cacheItem->groups |= insert.flags & Compositor::GroupMask; - - if (QVDMIncubationTask *incubationTask = cacheItem->incubationTask) { - for (int i = 1; i < m_groupCount; ++i) - incubationTask->index[i] = cacheItem->groups & (1 << i) - ? insert.index[i] + offset - : insert.index[i]; - } - if (QQuickVisualDataModelAttached *attached = cacheItem->attached) { - for (int i = 1; i < m_groupCount; ++i) - attached->m_currentIndex[i] = cacheItem->groups & (1 << i) - ? insert.index[i] + offset - : insert.index[i]; - } - } - } else { - cacheIndex = insert.cacheIndex + insert.count; - } - } - for (; cacheIndex < m_cache.count(); ++cacheIndex) - incrementIndexes(m_cache.at(cacheIndex), m_groupCount, inserted); -} - -void QQuickVisualDataModelPrivate::itemsInserted(const QVector<Compositor::Insert> &inserts) -{ - QVarLengthArray<QVector<QQuickChangeSet::Insert>, Compositor::MaximumGroupCount> translatedInserts(m_groupCount); - itemsInserted(inserts, &translatedInserts); - Q_ASSERT(m_cache.count() == m_compositor.count(Compositor::Cache)); - if (!m_delegate) - return; - - for (int i = 1; i < m_groupCount; ++i) - QQuickVisualDataGroupPrivate::get(m_groups[i])->changeSet.insert(translatedInserts.at(i)); -} - -void QQuickVisualDataModel::_q_itemsInserted(int index, int count) -{ - - Q_D(QQuickVisualDataModel); - if (count <= 0 || !d->m_complete) - return; - - d->m_count += count; - - for (int i = 0, c = d->m_cache.count(); i < c; ++i) { - QQuickVisualDataModelItem *item = d->m_cache.at(i); - if (item->modelIndex() >= index) - item->setModelIndex(item->modelIndex() + count); - } - - QVector<Compositor::Insert> inserts; - d->m_compositor.listItemsInserted(&d->m_adaptorModel, index, count, &inserts); - d->itemsInserted(inserts); - d->emitChanges(); -} - -void QQuickVisualDataModelPrivate::itemsRemoved( - const QVector<Compositor::Remove> &removes, - QVarLengthArray<QVector<QQuickChangeSet::Remove>, Compositor::MaximumGroupCount> *translatedRemoves, - QHash<int, QList<QQuickVisualDataModelItem *> > *movedItems) -{ - int cacheIndex = 0; - int removedCache = 0; - - int removed[Compositor::MaximumGroupCount]; - for (int i = 1; i < m_groupCount; ++i) - removed[i] = 0; - - foreach (const Compositor::Remove &remove, removes) { - for (; cacheIndex < remove.cacheIndex; ++cacheIndex) - incrementIndexes(m_cache.at(cacheIndex), m_groupCount, removed); - - for (int i = 1; i < m_groupCount; ++i) { - if (remove.inGroup(i)) { - (*translatedRemoves)[i].append( - QQuickChangeSet::Remove(remove.index[i], remove.count, remove.moveId)); - removed[i] -= remove.count; - } - } - - if (!remove.inCache()) - continue; - - if (movedItems && remove.isMove()) { - movedItems->insert(remove.moveId, m_cache.mid(remove.cacheIndex, remove.count)); - QList<QQuickVisualDataModelItem *>::iterator begin = m_cache.begin() + remove.cacheIndex; - QList<QQuickVisualDataModelItem *>::iterator end = begin + remove.count; - m_cache.erase(begin, end); - } else { - for (; cacheIndex < remove.cacheIndex + remove.count - removedCache; ++cacheIndex) { - QQuickVisualDataModelItem *cacheItem = m_cache.at(cacheIndex); - if (remove.inGroup(Compositor::Persisted) && cacheItem->objectRef == 0 && cacheItem->object) { - QObject *object = cacheItem->object; - cacheItem->destroyObject(); - if (QQuickPackage *package = qmlobject_cast<QQuickPackage *>(object)) - emitDestroyingPackage(package); - else if (QQuickItem *item = qmlobject_cast<QQuickItem *>(object)) - emitDestroyingItem(item); - cacheItem->scriptRef -= 1; - } - if (!cacheItem->isReferenced()) { - m_compositor.clearFlags(Compositor::Cache, cacheIndex, 1, Compositor::CacheFlag); - m_cache.removeAt(cacheIndex); - delete cacheItem; - --cacheIndex; - ++removedCache; - Q_ASSERT(m_cache.count() == m_compositor.count(Compositor::Cache)); - } else if (remove.groups() == cacheItem->groups) { - cacheItem->groups = 0; - if (QVDMIncubationTask *incubationTask = cacheItem->incubationTask) { - for (int i = 1; i < m_groupCount; ++i) - incubationTask->index[i] = -1; - } - if (QQuickVisualDataModelAttached *attached = cacheItem->attached) { - for (int i = 1; i < m_groupCount; ++i) - attached->m_currentIndex[i] = -1; - } - } else { - if (QVDMIncubationTask *incubationTask = cacheItem->incubationTask) { - for (int i = 1; i < m_groupCount; ++i) { - if (remove.inGroup(i)) - incubationTask->index[i] = remove.index[i]; - } - } - if (QQuickVisualDataModelAttached *attached = cacheItem->attached) { - for (int i = 1; i < m_groupCount; ++i) { - if (remove.inGroup(i)) - attached->m_currentIndex[i] = remove.index[i]; - } - } - cacheItem->groups &= ~remove.flags; - } - } - } - } - - for (; cacheIndex < m_cache.count(); ++cacheIndex) - incrementIndexes(m_cache.at(cacheIndex), m_groupCount, removed); -} - -void QQuickVisualDataModelPrivate::itemsRemoved(const QVector<Compositor::Remove> &removes) -{ - QVarLengthArray<QVector<QQuickChangeSet::Remove>, Compositor::MaximumGroupCount> translatedRemoves(m_groupCount); - itemsRemoved(removes, &translatedRemoves); - Q_ASSERT(m_cache.count() == m_compositor.count(Compositor::Cache)); - if (!m_delegate) - return; - - for (int i = 1; i < m_groupCount; ++i) - QQuickVisualDataGroupPrivate::get(m_groups[i])->changeSet.remove(translatedRemoves.at(i)); -} - -void QQuickVisualDataModel::_q_itemsRemoved(int index, int count) -{ - Q_D(QQuickVisualDataModel); - if (count <= 0|| !d->m_complete) - return; - - d->m_count -= count; - - for (int i = 0, c = d->m_cache.count(); i < c; ++i) { - QQuickVisualDataModelItem *item = d->m_cache.at(i); - if (item->modelIndex() >= index + count) - item->setModelIndex(item->modelIndex() - count); - else if (item->modelIndex() >= index) - item->setModelIndex(-1); - } - - QVector<Compositor::Remove> removes; - d->m_compositor.listItemsRemoved(&d->m_adaptorModel, index, count, &removes); - d->itemsRemoved(removes); - - d->emitChanges(); -} - -void QQuickVisualDataModelPrivate::itemsMoved( - const QVector<Compositor::Remove> &removes, const QVector<Compositor::Insert> &inserts) -{ - QHash<int, QList<QQuickVisualDataModelItem *> > movedItems; - - QVarLengthArray<QVector<QQuickChangeSet::Remove>, Compositor::MaximumGroupCount> translatedRemoves(m_groupCount); - itemsRemoved(removes, &translatedRemoves, &movedItems); - - QVarLengthArray<QVector<QQuickChangeSet::Insert>, Compositor::MaximumGroupCount> translatedInserts(m_groupCount); - itemsInserted(inserts, &translatedInserts, &movedItems); - Q_ASSERT(m_cache.count() == m_compositor.count(Compositor::Cache)); - Q_ASSERT(movedItems.isEmpty()); - if (!m_delegate) - return; - - for (int i = 1; i < m_groupCount; ++i) { - QQuickVisualDataGroupPrivate::get(m_groups[i])->changeSet.move( - translatedRemoves.at(i), - translatedInserts.at(i)); - } -} - -void QQuickVisualDataModel::_q_itemsMoved(int from, int to, int count) -{ - Q_D(QQuickVisualDataModel); - if (count <= 0 || !d->m_complete) - return; - - const int minimum = qMin(from, to); - const int maximum = qMax(from, to) + count; - const int difference = from > to ? count : -count; - - for (int i = 0, c = d->m_cache.count(); i < c; ++i) { - QQuickVisualDataModelItem *item = d->m_cache.at(i); - if (item->modelIndex() >= from && item->modelIndex() < from + count) - item->setModelIndex(item->modelIndex() - from + to); - else if (item->modelIndex() >= minimum && item->modelIndex() < maximum) - item->setModelIndex(item->modelIndex() + difference); - } - - QVector<Compositor::Remove> removes; - QVector<Compositor::Insert> inserts; - d->m_compositor.listItemsMoved(&d->m_adaptorModel, from, to, count, &removes, &inserts); - d->itemsMoved(removes, inserts); - d->emitChanges(); -} - -template <typename T> v8::Local<v8::Array> -QQuickVisualDataModelPrivate::buildChangeList(const QVector<T> &changes) -{ - v8::Local<v8::Array> indexes = v8::Array::New(changes.count()); - v8::Local<v8::String> indexKey = v8::String::New("index"); - v8::Local<v8::String> countKey = v8::String::New("count"); - v8::Local<v8::String> moveIdKey = v8::String::New("moveId"); - - for (int i = 0; i < changes.count(); ++i) { - v8::Local<v8::Object> object = v8::Object::New(); - object->Set(indexKey, v8::Integer::New(changes.at(i).index)); - object->Set(countKey, v8::Integer::New(changes.at(i).count)); - object->Set(moveIdKey, changes.at(i).moveId != -1 ? v8::Integer::New(changes.at(i).count) : v8::Undefined()); - indexes->Set(i, object); - } - return indexes; -} - -void QQuickVisualDataModelPrivate::emitModelUpdated(const QQuickChangeSet &changeSet, bool reset) -{ - Q_Q(QQuickVisualDataModel); - emit q->modelUpdated(changeSet, reset); - if (changeSet.difference() != 0) - emit q->countChanged(); -} - -void QQuickVisualDataModelPrivate::emitChanges() -{ - if (m_transaction || !m_complete || !m_context->isValid()) - return; - - m_transaction = true; - QV8Engine *engine = QQmlEnginePrivate::getV8Engine(m_context->engine()); - for (int i = 1; i < m_groupCount; ++i) - QQuickVisualDataGroupPrivate::get(m_groups[i])->emitChanges(engine); - m_transaction = false; - - const bool reset = m_reset; - m_reset = false; - for (int i = 1; i < m_groupCount; ++i) - QQuickVisualDataGroupPrivate::get(m_groups[i])->emitModelUpdated(reset); - - foreach (QQuickVisualDataModelItem *cacheItem, m_cache) { - if (cacheItem->attached) - cacheItem->attached->emitChanges(); - } -} - -void QQuickVisualDataModel::_q_modelReset() -{ - Q_D(QQuickVisualDataModel); - if (!d->m_delegate) - return; - - int oldCount = d->m_count; - d->m_adaptorModel.rootIndex = QModelIndex(); - - if (d->m_complete) { - d->m_count = d->m_adaptorModel.count(); - - for (int i = 0, c = d->m_cache.count(); i < c; ++i) { - QQuickVisualDataModelItem *item = d->m_cache.at(i); - if (item->modelIndex() != -1) - item->setModelIndex(-1); - } - - QVector<Compositor::Remove> removes; - QVector<Compositor::Insert> inserts; - if (oldCount) - d->m_compositor.listItemsRemoved(&d->m_adaptorModel, 0, oldCount, &removes); - if (d->m_count) - d->m_compositor.listItemsInserted(&d->m_adaptorModel, 0, d->m_count, &inserts); - d->itemsMoved(removes, inserts); - d->m_reset = true; - - if (d->m_adaptorModel.canFetchMore()) - d->m_adaptorModel.fetchMore(); - - d->emitChanges(); - } - emit rootIndexChanged(); -} - -void QQuickVisualDataModel::_q_rowsInserted(const QModelIndex &parent, int begin, int end) -{ - Q_D(QQuickVisualDataModel); - if (parent == d->m_adaptorModel.rootIndex) - _q_itemsInserted(begin, end - begin + 1); -} - -void QQuickVisualDataModel::_q_rowsAboutToBeRemoved(const QModelIndex &parent, int begin, int end) -{ - Q_D(QQuickVisualDataModel); - if (!d->m_adaptorModel.rootIndex.isValid()) - return; - const QModelIndex index = d->m_adaptorModel.rootIndex; - if (index.parent() == parent && index.row() >= begin && index.row() <= end) { - const int oldCount = d->m_count; - d->m_count = 0; - d->m_adaptorModel.invalidateModel(this); - - if (d->m_complete && oldCount > 0) { - QVector<Compositor::Remove> removes; - d->m_compositor.listItemsRemoved(&d->m_adaptorModel, 0, oldCount, &removes); - d->itemsRemoved(removes); - d->emitChanges(); - } - } -} - -void QQuickVisualDataModel::_q_rowsRemoved(const QModelIndex &parent, int begin, int end) -{ - Q_D(QQuickVisualDataModel); - if (parent == d->m_adaptorModel.rootIndex) - _q_itemsRemoved(begin, end - begin + 1); -} - -void QQuickVisualDataModel::_q_rowsMoved( - const QModelIndex &sourceParent, int sourceStart, int sourceEnd, - const QModelIndex &destinationParent, int destinationRow) -{ - Q_D(QQuickVisualDataModel); - const int count = sourceEnd - sourceStart + 1; - if (destinationParent == d->m_adaptorModel.rootIndex && sourceParent == d->m_adaptorModel.rootIndex) { - _q_itemsMoved(sourceStart, sourceStart > destinationRow ? destinationRow : destinationRow - count, count); - } else if (sourceParent == d->m_adaptorModel.rootIndex) { - _q_itemsRemoved(sourceStart, count); - } else if (destinationParent == d->m_adaptorModel.rootIndex) { - _q_itemsInserted(destinationRow, count); - } -} - -void QQuickVisualDataModel::_q_dataChanged(const QModelIndex &begin, const QModelIndex &end, const QVector<int> &roles) -{ - Q_D(QQuickVisualDataModel); - if (begin.parent() == d->m_adaptorModel.rootIndex) - _q_itemsChanged(begin.row(), end.row() - begin.row() + 1, roles); -} - -void QQuickVisualDataModel::_q_layoutChanged() -{ - Q_D(QQuickVisualDataModel); - _q_itemsChanged(0, d->m_count, QVector<int>()); -} - -QQuickVisualDataModelAttached *QQuickVisualDataModel::qmlAttachedProperties(QObject *obj) -{ - if (QQuickVisualDataModelItem *cacheItem = QQuickVisualDataModelItem::dataForObject(obj)) { - if (cacheItem->object == obj) { // Don't create attached item for child objects. - cacheItem->attached = new QQuickVisualDataModelAttached(cacheItem, obj); - return cacheItem->attached; - } - } - return new QQuickVisualDataModelAttached(obj); -} - -bool QQuickVisualDataModelPrivate::insert( - Compositor::insert_iterator &before, const v8::Local<v8::Object> &object, int groups) -{ - if (!m_context->isValid()) - return false; - - QQuickVisualDataModelItem *cacheItem = m_adaptorModel.createItem(m_cacheMetaType, m_context->engine(), -1); - if (!cacheItem) - return false; - - v8::Local<v8::Array> propertyNames = object->GetPropertyNames(); - for (uint i = 0; i < propertyNames->Length(); ++i) { - v8::Local<v8::String> propertyName = propertyNames->Get(i)->ToString(); - cacheItem->setValue( - m_cacheMetaType->v8Engine->toString(propertyName), - m_cacheMetaType->v8Engine->toVariant(object->Get(propertyName), QVariant::Invalid)); - } - - cacheItem->groups = groups | Compositor::UnresolvedFlag | Compositor::CacheFlag; - - // Must be before the new object is inserted into the cache or its indexes will be adjusted too. - itemsInserted(QVector<Compositor::Insert>() << Compositor::Insert(before, 1, cacheItem->groups & ~Compositor::CacheFlag)); - - before = m_compositor.insert(before, 0, 0, 1, cacheItem->groups); - m_cache.insert(before.cacheIndex, cacheItem); - - return true; -} - -//============================================================================ - -QQuickVisualDataModelItemMetaType::QQuickVisualDataModelItemMetaType( - QV8Engine *engine, QQuickVisualDataModel *model, const QStringList &groupNames) - : model(model) - , groupCount(groupNames.count() + 1) - , v8Engine(engine) - , metaObject(0) - , groupNames(groupNames) -{ -} - -QQuickVisualDataModelItemMetaType::~QQuickVisualDataModelItemMetaType() -{ - if (metaObject) - metaObject->release(); - qPersistentDispose(constructor); -} - -void QQuickVisualDataModelItemMetaType::initializeMetaObject() -{ - QMetaObjectBuilder builder; - builder.setFlags(QMetaObjectBuilder::DynamicMetaObject); - builder.setClassName(QQuickVisualDataModelAttached::staticMetaObject.className()); - builder.setSuperClass(&QQuickVisualDataModelAttached::staticMetaObject); - - int notifierId = 0; - for (int i = 0; i < groupNames.count(); ++i, ++notifierId) { - QString propertyName = QStringLiteral("in") + groupNames.at(i); - propertyName.replace(2, 1, propertyName.at(2).toUpper()); - builder.addSignal("__" + propertyName.toUtf8() + "Changed()"); - QMetaPropertyBuilder propertyBuilder = builder.addProperty( - propertyName.toUtf8(), "bool", notifierId); - propertyBuilder.setWritable(true); - } - for (int i = 0; i < groupNames.count(); ++i, ++notifierId) { - const QString propertyName = groupNames.at(i) + QStringLiteral("Index"); - builder.addSignal("__" + propertyName.toUtf8() + "Changed()"); - QMetaPropertyBuilder propertyBuilder = builder.addProperty( - propertyName.toUtf8(), "int", notifierId); - propertyBuilder.setWritable(true); - } - - metaObject = new QQuickVisualDataModelAttachedMetaObject(this, builder.toMetaObject()); -} - -void QQuickVisualDataModelItemMetaType::initializeConstructor() -{ - v8::HandleScope handleScope; - v8::Context::Scope contextScope(v8Engine->context()); - - QQuickVisualDataModelEngineData *data = engineData(v8Engine); - - constructor = qPersistentNew(v8::ObjectTemplate::New()); - - constructor->SetHasExternalResource(true); - constructor->SetAccessor(data->model(), get_model); - constructor->SetAccessor(data->groups(), get_groups, set_groups); - constructor->SetAccessor(data->isUnresolved(), get_member, 0, v8::Int32::New(30)); - constructor->SetAccessor(data->inItems(), get_member, set_member, v8::Int32::New(1)); - constructor->SetAccessor(data->inPersistedItems(), get_member, set_member, v8::Int32::New(2)); - constructor->SetAccessor(data->itemsIndex(), get_index, 0, v8::Int32::New(1)); - constructor->SetAccessor(data->persistedItemsIndex(), get_index, 0, v8::Int32::New(2)); - - for (int i = 2; i < groupNames.count(); ++i) { - QString propertyName = QStringLiteral("in") + groupNames.at(i); - propertyName.replace(2, 1, propertyName.at(2).toUpper()); - constructor->SetAccessor( - v8Engine->toString(propertyName), get_member, set_member, v8::Int32::New(i + 1)); - } - for (int i = 2; i < groupNames.count(); ++i) { - const QString propertyName = groupNames.at(i) + QStringLiteral("Index"); - constructor->SetAccessor( - v8Engine->toString(propertyName), get_index, 0, v8::Int32::New(i + 1)); - } -} - -int QQuickVisualDataModelItemMetaType::parseGroups(const QStringList &groups) const -{ - int groupFlags = 0; - foreach (const QString &groupName, groups) { - int index = groupNames.indexOf(groupName); - if (index != -1) - groupFlags |= 2 << index; - } - return groupFlags; -} - -int QQuickVisualDataModelItemMetaType::parseGroups(const v8::Local<v8::Value> &groups) const -{ - int groupFlags = 0; - if (groups->IsString()) { - const QString groupName = v8Engine->toString(groups); - int index = groupNames.indexOf(groupName); - if (index != -1) - groupFlags |= 2 << index; - } else if (groups->IsArray()) { - v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(groups); - for (uint i = 0; i < array->Length(); ++i) { - const QString groupName = v8Engine->toString(array->Get(i)); - int index = groupNames.indexOf(groupName); - if (index != -1) - groupFlags |= 2 << index; - } - } - return groupFlags; -} - -v8::Handle<v8::Value> QQuickVisualDataModelItemMetaType::get_model( - v8::Local<v8::String>, const v8::AccessorInfo &info) -{ - QQuickVisualDataModelItem *cacheItem = v8_resource_cast<QQuickVisualDataModelItem>(info.This()); - V8ASSERT_TYPE(cacheItem, "Not a valid VisualData object"); - if (!cacheItem->metaType->model) - return v8::Undefined(); - - return cacheItem->get(); -} - -v8::Handle<v8::Value> QQuickVisualDataModelItemMetaType::get_groups( - v8::Local<v8::String>, const v8::AccessorInfo &info) -{ - QQuickVisualDataModelItem *cacheItem = v8_resource_cast<QQuickVisualDataModelItem>(info.This()); - V8ASSERT_TYPE(cacheItem, "Not a valid VisualData object"); - - QStringList groups; - for (int i = 1; i < cacheItem->metaType->groupCount; ++i) { - if (cacheItem->groups & (1 << i)) - groups.append(cacheItem->metaType->groupNames.at(i - 1)); - } - - return cacheItem->engine->fromVariant(groups); -} - -void QQuickVisualDataModelItemMetaType::set_groups( - v8::Local<v8::String>, v8::Local<v8::Value> value, const v8::AccessorInfo &info) -{ - QQuickVisualDataModelItem *cacheItem = v8_resource_cast<QQuickVisualDataModelItem>(info.This()); - V8ASSERT_TYPE_SETTER(cacheItem, "Not a valid VisualData object"); - - if (!cacheItem->metaType->model) - return; - QQuickVisualDataModelPrivate *model = QQuickVisualDataModelPrivate::get(cacheItem->metaType->model); - - const int groupFlags = model->m_cacheMetaType->parseGroups(value); - const int cacheIndex = model->m_cache.indexOf(cacheItem); - Compositor::iterator it = model->m_compositor.find(Compositor::Cache, cacheIndex); - model->setGroups(it, 1, Compositor::Cache, groupFlags); -} - -v8::Handle<v8::Value> QQuickVisualDataModelItemMetaType::get_member( - v8::Local<v8::String>, const v8::AccessorInfo &info) -{ - QQuickVisualDataModelItem *cacheItem = v8_resource_cast<QQuickVisualDataModelItem>(info.This()); - V8ASSERT_TYPE(cacheItem, "Not a valid VisualData object"); - - return v8::Boolean::New(cacheItem->groups & (1 << info.Data()->Int32Value())); -} - -void QQuickVisualDataModelItemMetaType::set_member( - v8::Local<v8::String>, v8::Local<v8::Value> value, const v8::AccessorInfo &info) -{ - QQuickVisualDataModelItem *cacheItem = v8_resource_cast<QQuickVisualDataModelItem>(info.This()); - V8ASSERT_TYPE_SETTER(cacheItem, "Not a valid VisualData object"); - - if (!cacheItem->metaType->model) - return; - QQuickVisualDataModelPrivate *model = QQuickVisualDataModelPrivate::get(cacheItem->metaType->model); - - Compositor::Group group = Compositor::Group(info.Data()->Int32Value()); - const bool member = value->BooleanValue(); - const int groupFlag = (1 << group); - if (member == ((cacheItem->groups & groupFlag) != 0)) - return; - - const int cacheIndex = model->m_cache.indexOf(cacheItem); - Compositor::iterator it = model->m_compositor.find(Compositor::Cache, cacheIndex); - if (member) - model->addGroups(it, 1, Compositor::Cache, groupFlag); - else - model->removeGroups(it, 1, Compositor::Cache, groupFlag); -} - -v8::Handle<v8::Value> QQuickVisualDataModelItemMetaType::get_index( - v8::Local<v8::String>, const v8::AccessorInfo &info) -{ - QQuickVisualDataModelItem *cacheItem = v8_resource_cast<QQuickVisualDataModelItem>(info.This()); - V8ASSERT_TYPE(cacheItem, "Not a valid VisualData object"); - - return v8::Integer::New(cacheItem->groupIndex(Compositor::Group(info.Data()->Int32Value()))); -} - - -//--------------------------------------------------------------------------- - -QQuickVisualDataModelItem::QQuickVisualDataModelItem( - QQuickVisualDataModelItemMetaType *metaType, int modelIndex) - : QV8ObjectResource(metaType->v8Engine) - , metaType(metaType) - , contextData(0) - , object(0) - , attached(0) - , incubationTask(0) - , objectRef(0) - , scriptRef(0) - , groups(0) - , index(modelIndex) -{ - metaType->addref(); -} - -QQuickVisualDataModelItem::~QQuickVisualDataModelItem() -{ - Q_ASSERT(scriptRef == 0); - Q_ASSERT(objectRef == 0); - Q_ASSERT(!object); - - if (incubationTask && metaType->model) - QQuickVisualDataModelPrivate::get(metaType->model)->releaseIncubator(incubationTask); - - metaType->release(); - -} - -void QQuickVisualDataModelItem::Dispose() -{ - --scriptRef; - if (isReferenced()) - return; - - if (metaType->model) { - QQuickVisualDataModelPrivate *model = QQuickVisualDataModelPrivate::get(metaType->model); - model->removeCacheItem(this); - } - delete this; -} - -/* - This is essentially a copy of QQmlComponent::create(); except it takes the QQmlContextData - arguments instead of QQmlContext which means we don't have to construct the rather weighty - wrapper class for every delegate item. -*/ -void QQuickVisualDataModelItem::incubateObject( - QQmlComponent *component, - QQmlEngine *engine, - QQmlContextData *context, - QQmlContextData *forContext) -{ - QQmlIncubatorPrivate *incubatorPriv = QQmlIncubatorPrivate::get(incubationTask); - QQmlEnginePrivate *enginePriv = QQmlEnginePrivate::get(engine); - QQmlComponentPrivate *componentPriv = QQmlComponentPrivate::get(component); - - incubatorPriv->compiledData = componentPriv->cc; - incubatorPriv->compiledData->addref(); - incubatorPriv->vme.init( - context, - componentPriv->cc, - componentPriv->start, - componentPriv->creationContext); - - enginePriv->incubate(*incubationTask, forContext); -} - -void QQuickVisualDataModelItem::destroyObject() -{ - Q_ASSERT(object); - Q_ASSERT(contextData); - - QObjectPrivate *p = QObjectPrivate::get(object); - Q_ASSERT(p->declarativeData); - QQmlData *data = static_cast<QQmlData*>(p->declarativeData); - if (data->ownContext && data->context) - data->context->clearContext(); - object->deleteLater(); - - if (attached) { - attached->m_cacheItem = 0; - attached = 0; - } - - contextData->destroy(); - contextData = 0; - object = 0; -} - -QQuickVisualDataModelItem *QQuickVisualDataModelItem::dataForObject(QObject *object) -{ - QObjectPrivate *p = QObjectPrivate::get(object); - QQmlContextData *context = p->declarativeData - ? static_cast<QQmlData *>(p->declarativeData)->context - : 0; - for (context = context ? context->parent : 0; context; context = context->parent) { - if (QQuickVisualDataModelItem *cacheItem = qobject_cast<QQuickVisualDataModelItem *>( - context->contextObject)) { - return cacheItem; - } - } - return 0; -} - -int QQuickVisualDataModelItem::groupIndex(Compositor::Group group) -{ - if (QQuickVisualDataModelPrivate * const model = metaType->model - ? QQuickVisualDataModelPrivate::get(metaType->model) - : 0) { - return model->m_compositor.find(Compositor::Cache, model->m_cache.indexOf(this)).index[group]; - } - return -1; -} - -//--------------------------------------------------------------------------- - -QQuickVisualDataModelAttachedMetaObject::QQuickVisualDataModelAttachedMetaObject( - QQuickVisualDataModelItemMetaType *metaType, QMetaObject *metaObject) - : metaType(metaType) - , metaObject(metaObject) - , memberPropertyOffset(QQuickVisualDataModelAttached::staticMetaObject.propertyCount()) - , indexPropertyOffset(QQuickVisualDataModelAttached::staticMetaObject.propertyCount() + metaType->groupNames.count()) -{ - // Don't reference count the meta-type here as that would create a circular reference. - // Instead we rely the fact that the meta-type's reference count can't reach 0 without first - // destroying all delegates with attached objects. - *static_cast<QMetaObject *>(this) = *metaObject; -} - -QQuickVisualDataModelAttachedMetaObject::~QQuickVisualDataModelAttachedMetaObject() -{ - ::free(metaObject); -} - -void QQuickVisualDataModelAttachedMetaObject::objectDestroyed(QObject *) -{ - release(); -} - -int QQuickVisualDataModelAttachedMetaObject::metaCall(QObject *object, QMetaObject::Call call, int _id, void **arguments) -{ - QQuickVisualDataModelAttached *attached = static_cast<QQuickVisualDataModelAttached *>(object); - if (call == QMetaObject::ReadProperty) { - if (_id >= indexPropertyOffset) { - Compositor::Group group = Compositor::Group(_id - indexPropertyOffset + 1); - *static_cast<int *>(arguments[0]) = attached->m_currentIndex[group]; - return -1; - } else if (_id >= memberPropertyOffset) { - Compositor::Group group = Compositor::Group(_id - memberPropertyOffset + 1); - *static_cast<bool *>(arguments[0]) = attached->m_cacheItem->groups & (1 << group); - return -1; - } - } else if (call == QMetaObject::WriteProperty) { - if (_id >= memberPropertyOffset) { - if (!metaType->model) - return -1; - QQuickVisualDataModelPrivate *model = QQuickVisualDataModelPrivate::get(metaType->model); - Compositor::Group group = Compositor::Group(_id - memberPropertyOffset + 1); - const int groupFlag = 1 << group; - const bool member = attached->m_cacheItem->groups & groupFlag; - if (member && !*static_cast<bool *>(arguments[0])) { - Compositor::iterator it = model->m_compositor.find( - group, attached->m_currentIndex[group]); - model->removeGroups(it, 1, group, groupFlag); - } else if (!member && *static_cast<bool *>(arguments[0])) { - for (int i = 1; i < metaType->groupCount; ++i) { - if (attached->m_cacheItem->groups & (1 << i)) { - Compositor::iterator it = model->m_compositor.find( - Compositor::Group(i), attached->m_currentIndex[i]); - model->addGroups(it, 1, Compositor::Group(i), groupFlag); - break; - } - } - } - return -1; - } - } - return attached->qt_metacall(call, _id, arguments); -} - -QQuickVisualDataModelAttached::QQuickVisualDataModelAttached(QObject *parent) - : m_cacheItem(0) - , m_previousGroups(0) -{ - QQml_setParent_noEvent(this, parent); -} - -QQuickVisualDataModelAttached::QQuickVisualDataModelAttached( - QQuickVisualDataModelItem *cacheItem, QObject *parent) - : m_cacheItem(cacheItem) - , m_previousGroups(cacheItem->groups) -{ - QQml_setParent_noEvent(this, parent); - if (QVDMIncubationTask *incubationTask = m_cacheItem->incubationTask) { - for (int i = 1; i < m_cacheItem->metaType->groupCount; ++i) - m_currentIndex[i] = m_previousIndex[i] = incubationTask->index[i]; - } else { - QQuickVisualDataModelPrivate * const model = QQuickVisualDataModelPrivate::get(m_cacheItem->metaType->model); - Compositor::iterator it = model->m_compositor.find( - Compositor::Cache, model->m_cache.indexOf(m_cacheItem)); - for (int i = 1; i < m_cacheItem->metaType->groupCount; ++i) - m_currentIndex[i] = m_previousIndex[i] = it.index[i]; - } - - if (!cacheItem->metaType->metaObject) - cacheItem->metaType->initializeMetaObject(); - - QObjectPrivate::get(this)->metaObject = cacheItem->metaType->metaObject; - cacheItem->metaType->metaObject->addref(); -} - -/*! - \qmlattachedproperty int QtQuick2::VisualDataModel::model - - This attached property holds the visual data model this delegate instance belongs to. - - It is attached to each instance of the delegate. -*/ - -QQuickVisualDataModel *QQuickVisualDataModelAttached::model() const -{ - return m_cacheItem ? m_cacheItem->metaType->model : 0; -} - -/*! - \qmlattachedproperty stringlist QtQuick2::VisualDataModel::groups - - This attached property holds the name of VisualDataGroups the item belongs to. - - It is attached to each instance of the delegate. -*/ - -QStringList QQuickVisualDataModelAttached::groups() const -{ - QStringList groups; - - if (!m_cacheItem) - return groups; - for (int i = 1; i < m_cacheItem->metaType->groupCount; ++i) { - if (m_cacheItem->groups & (1 << i)) - groups.append(m_cacheItem->metaType->groupNames.at(i - 1)); - } - return groups; -} - -void QQuickVisualDataModelAttached::setGroups(const QStringList &groups) -{ - if (!m_cacheItem) - return; - - QQuickVisualDataModelPrivate *model = QQuickVisualDataModelPrivate::get(m_cacheItem->metaType->model); - - const int groupFlags = model->m_cacheMetaType->parseGroups(groups); - const int cacheIndex = model->m_cache.indexOf(m_cacheItem); - Compositor::iterator it = model->m_compositor.find(Compositor::Cache, cacheIndex); - model->setGroups(it, 1, Compositor::Cache, groupFlags); -} - -/*! - \qmlattachedproperty bool QtQuick2::VisualDataModel::isUnresolved - - This attached property holds whether the visual item is bound to a data model index. - Returns true if the item is not bound to the model, and false if it is. - - An unresolved item can be bound to the data model using the VisualDataGroup::resolve() - function. - - It is attached to each instance of the delegate. -*/ - -bool QQuickVisualDataModelAttached::isUnresolved() const -{ - if (!m_cacheItem) - return false; - - return m_cacheItem->groups & Compositor::UnresolvedFlag; -} - -/*! - \qmlattachedproperty int QtQuick2::VisualDataModel::inItems - - This attached property holds whether the item belongs to the default \l items VisualDataGroup. - - Changing this property will add or remove the item from the items group. - - It is attached to each instance of the delegate. -*/ - -/*! - \qmlattachedproperty int QtQuick2::VisualDataModel::itemsIndex - - This attached property holds the index of the item in the default \l items VisualDataGroup. - - It is attached to each instance of the delegate. -*/ - -/*! - \qmlattachedproperty int QtQuick2::VisualDataModel::inPersistedItems - - This attached property holds whether the item belongs to the \l persistedItems VisualDataGroup. - - Changing this property will add or remove the item from the items group. Change with caution - as removing an item from the persistedItems group will destroy the current instance if it is - not referenced by a model. - - It is attached to each instance of the delegate. -*/ - -/*! - \qmlattachedproperty int QtQuick2::VisualDataModel::persistedItemsIndex - - This attached property holds the index of the item in the \l persistedItems VisualDataGroup. - - It is attached to each instance of the delegate. -*/ - -void QQuickVisualDataModelAttached::emitChanges() -{ - const int groupChanges = m_previousGroups ^ m_cacheItem->groups; - m_previousGroups = m_cacheItem->groups; - - int indexChanges = 0; - for (int i = 1; i < m_cacheItem->metaType->groupCount; ++i) { - if (m_previousIndex[i] != m_currentIndex[i]) { - m_previousIndex[i] = m_currentIndex[i]; - indexChanges |= (1 << i); - } - } - - int notifierId = 0; - const QMetaObject *meta = metaObject(); - for (int i = 1; i < m_cacheItem->metaType->groupCount; ++i, ++notifierId) { - if (groupChanges & (1 << i)) - QMetaObject::activate(this, meta, notifierId, 0); - } - for (int i = 1; i < m_cacheItem->metaType->groupCount; ++i, ++notifierId) { - if (indexChanges & (1 << i)) - QMetaObject::activate(this, meta, notifierId, 0); - } - - if (groupChanges) - emit groupsChanged(); -} - -//============================================================================ - -void QQuickVisualDataGroupPrivate::setModel(QQuickVisualDataModel *m, Compositor::Group g) -{ - Q_ASSERT(!model); - model = m; - group = g; -} - -bool QQuickVisualDataGroupPrivate::isChangedConnected() -{ - Q_Q(QQuickVisualDataGroup); - IS_SIGNAL_CONNECTED(q, QQuickVisualDataGroup, changed, (const QQmlV8Handle &,const QQmlV8Handle &)); -} - -void QQuickVisualDataGroupPrivate::emitChanges(QV8Engine *engine) -{ - Q_Q(QQuickVisualDataGroup); - if (isChangedConnected() && !changeSet.isEmpty()) { - v8::HandleScope handleScope; - v8::Context::Scope contextScope(engine->context()); - v8::Local<v8::Object> removed = engineData(engine)->array(engine, changeSet.removes()); - v8::Local<v8::Object> inserted = engineData(engine)->array(engine, changeSet.inserts()); - emit q->changed(QQmlV8Handle::fromHandle(removed), QQmlV8Handle::fromHandle(inserted)); - } - if (changeSet.difference() != 0) - emit q->countChanged(); -} - -void QQuickVisualDataGroupPrivate::emitModelUpdated(bool reset) -{ - for (QQuickVisualDataGroupEmitterList::iterator it = emitters.begin(); it != emitters.end(); ++it) - it->emitModelUpdated(changeSet, reset); - changeSet.clear(); -} - -void QQuickVisualDataGroupPrivate::createdPackage(int index, QQuickPackage *package) -{ - for (QQuickVisualDataGroupEmitterList::iterator it = emitters.begin(); it != emitters.end(); ++it) - it->createdPackage(index, package); -} - -void QQuickVisualDataGroupPrivate::initPackage(int index, QQuickPackage *package) -{ - for (QQuickVisualDataGroupEmitterList::iterator it = emitters.begin(); it != emitters.end(); ++it) - it->initPackage(index, package); -} - -void QQuickVisualDataGroupPrivate::destroyingPackage(QQuickPackage *package) -{ - for (QQuickVisualDataGroupEmitterList::iterator it = emitters.begin(); it != emitters.end(); ++it) - it->destroyingPackage(package); -} - -/*! - \qmltype VisualDataGroup - \instantiates QQuickVisualDataGroup - \inqmlmodule QtQuick 2 - \ingroup qtquick-models - \brief Encapsulates a filtered set of visual data items - - The VisualDataGroup type provides a means to address the model data of a VisualDataModel's - delegate items, as well as sort and filter these delegate items. - - The initial set of instantiable delegate items in a VisualDataModel is represented - by its \l {QtQuick2::VisualDataModel::items}{items} group, which normally directly reflects - the contents of the model assigned to VisualDataModel::model. This set can be changed to - the contents of any other member of VisualDataModel::groups by assigning the \l name of that - VisualDataGroup to the VisualDataModel::filterOnGroup property. - - The data of an item in a VisualDataGroup can be accessed using the get() function, which returns - information about group membership and indexes as well as model data. In combination - with the move() function this can be used to implement view sorting, with remove() to filter - items out of a view, or with setGroups() and \l Package delegates to categorize items into - different views. - - Data from models can be supplemented by inserting data directly into a VisualDataGroup - with the insert() function. This can be used to introduce mock items into a view, or - placeholder items that are later \l {resolve()}{resolved} to real model data when it becomes - available. - - Delegate items can also be be instantiated directly from a VisualDataGroup using the - create() function, making it possible to use VisualDataModel without an accompanying view - type or to cherry-pick specific items that should be instantiated irregardless of whether - they're currently within a view's visible area. - - \sa {QML Dynamic View Ordering Tutorial} -*/ - -QQuickVisualDataGroup::QQuickVisualDataGroup(QObject *parent) - : QObject(*new QQuickVisualDataGroupPrivate, parent) -{ -} - -QQuickVisualDataGroup::QQuickVisualDataGroup( - const QString &name, QQuickVisualDataModel *model, int index, QObject *parent) - : QObject(*new QQuickVisualDataGroupPrivate, parent) -{ - Q_D(QQuickVisualDataGroup); - d->name = name; - d->setModel(model, Compositor::Group(index)); -} - -QQuickVisualDataGroup::~QQuickVisualDataGroup() -{ -} - -/*! - \qmlproperty string QtQuick2::VisualDataGroup::name - - This property holds the name of the group. - - Each group in a model must have a unique name starting with a lower case letter. -*/ - -QString QQuickVisualDataGroup::name() const -{ - Q_D(const QQuickVisualDataGroup); - return d->name; -} - -void QQuickVisualDataGroup::setName(const QString &name) -{ - Q_D(QQuickVisualDataGroup); - if (d->model) - return; - if (d->name != name) { - d->name = name; - emit nameChanged(); - } -} - -/*! - \qmlproperty int QtQuick2::VisualDataGroup::count - - This property holds the number of items in the group. -*/ - -int QQuickVisualDataGroup::count() const -{ - Q_D(const QQuickVisualDataGroup); - if (!d->model) - return 0; - return QQuickVisualDataModelPrivate::get(d->model)->m_compositor.count(d->group); -} - -/*! - \qmlproperty bool QtQuick2::VisualDataGroup::includeByDefault - - This property holds whether new items are assigned to this group by default. -*/ - -bool QQuickVisualDataGroup::defaultInclude() const -{ - Q_D(const QQuickVisualDataGroup); - return d->defaultInclude; -} - -void QQuickVisualDataGroup::setDefaultInclude(bool include) -{ - Q_D(QQuickVisualDataGroup); - if (d->defaultInclude != include) { - d->defaultInclude = include; - - if (d->model) { - if (include) - QQuickVisualDataModelPrivate::get(d->model)->m_compositor.setDefaultGroup(d->group); - else - QQuickVisualDataModelPrivate::get(d->model)->m_compositor.clearDefaultGroup(d->group); - } - emit defaultIncludeChanged(); - } -} - -/*! - \qmlmethod object QtQuick2::VisualDataGroup::get(int index) - - Returns a javascript object describing the item at \a index in the group. - - The returned object contains the same information that is available to a delegate from the - VisualDataModel attached as well as the model for that item. It has the properties: - - \list - \li \b model The model data of the item. This is the same as the model context property in - a delegate - \li \b groups A list the of names of groups the item is a member of. This property can be - written to change the item's membership. - \li \b inItems Whether the item belongs to the \l {QtQuick2::VisualDataModel::items}{items} group. - Writing to this property will add or remove the item from the group. - \li \b itemsIndex The index of the item within the \l {QtQuick2::VisualDataModel::items}{items} group. - \li \b {in<GroupName>} Whether the item belongs to the dynamic group \e groupName. Writing to - this property will add or remove the item from the group. - \li \b {<groupName>Index} The index of the item within the dynamic group \e groupName. - \li \b isUnresolved Whether the item is bound to an index in the model assigned to - VisualDataModel::model. Returns true if the item is not bound to the model, and false if it is. - \endlist -*/ - -QQmlV8Handle QQuickVisualDataGroup::get(int index) -{ - Q_D(QQuickVisualDataGroup); - if (!d->model) - return QQmlV8Handle::fromHandle(v8::Undefined());; - - QQuickVisualDataModelPrivate *model = QQuickVisualDataModelPrivate::get(d->model); - if (!model->m_context->isValid()) { - return QQmlV8Handle::fromHandle(v8::Undefined()); - } else if (index < 0 || index >= model->m_compositor.count(d->group)) { - qmlInfo(this) << tr("get: index out of range"); - return QQmlV8Handle::fromHandle(v8::Undefined()); - } - - Compositor::iterator it = model->m_compositor.find(d->group, index); - QQuickVisualDataModelItem *cacheItem = it->inCache() - ? model->m_cache.at(it.cacheIndex) - : 0; - - if (!cacheItem) { - cacheItem = model->m_adaptorModel.createItem( - model->m_cacheMetaType, model->m_context->engine(), it.modelIndex()); - if (!cacheItem) - return QQmlV8Handle::fromHandle(v8::Undefined()); - cacheItem->groups = it->flags; - - model->m_cache.insert(it.cacheIndex, cacheItem); - model->m_compositor.setFlags(it, 1, Compositor::CacheFlag); - } - - if (model->m_cacheMetaType->constructor.IsEmpty()) - model->m_cacheMetaType->initializeConstructor(); - v8::Local<v8::Object> handle = model->m_cacheMetaType->constructor->NewInstance(); - handle->SetExternalResource(cacheItem); - ++cacheItem->scriptRef; - - return QQmlV8Handle::fromHandle(handle); -} - -bool QQuickVisualDataGroupPrivate::parseIndex( - const v8::Local<v8::Value> &value, int *index, Compositor::Group *group) const -{ - if (value->IsInt32()) { - *index = value->Int32Value(); - return true; - } else if (value->IsObject()) { - v8::Local<v8::Object> object = value->ToObject(); - QQuickVisualDataModelItem * const cacheItem = v8_resource_cast<QQuickVisualDataModelItem>(object); - if (QQuickVisualDataModelPrivate *model = cacheItem && cacheItem->metaType->model - ? QQuickVisualDataModelPrivate::get(cacheItem->metaType->model) - : 0) { - *index = model->m_cache.indexOf(cacheItem); - *group = Compositor::Cache; - return true; - } - } - return false; -} - -/*! - \qmlmethod QtQuick2::VisualDataGroup::insert(int index, jsdict data, array groups = undefined) - \qmlmethod QtQuick2::VisualDataGroup::insert(jsdict data, var groups = undefined) - - Creates a new entry at \a index in a VisualDataModel with the values from \a data that - correspond to roles in the model assigned to VisualDataModel::model. - - If no index is supplied the data is appended to the model. - - The optional \a groups parameter identifies the groups the new entry should belong to, - if unspecified this is equal to the group insert was called on. - - Data inserted into a VisualDataModel can later be merged with an existing entry in - VisualDataModel::model using the \l resolve() function. This can be used to create placeholder - items that are later replaced by actual data. -*/ - -void QQuickVisualDataGroup::insert(QQmlV8Function *args) -{ - Q_D(QQuickVisualDataGroup); - QQuickVisualDataModelPrivate *model = QQuickVisualDataModelPrivate::get(d->model); - - int index = model->m_compositor.count(d->group); - Compositor::Group group = d->group; - - if (args->Length() == 0) - return; - - int i = 0; - v8::Local<v8::Value> v = (*args)[i]; - if (d->parseIndex(v, &index, &group)) { - if (index < 0 || index > model->m_compositor.count(group)) { - qmlInfo(this) << tr("insert: index out of range"); - return; - } - if (++i == args->Length()) - return; - v = (*args)[i]; - } - - Compositor::insert_iterator before = index < model->m_compositor.count(group) - ? model->m_compositor.findInsertPosition(group, index) - : model->m_compositor.end(); - - int groups = 1 << d->group; - if (++i < args->Length()) - groups |= model->m_cacheMetaType->parseGroups((*args)[i]); - - if (v->IsArray()) { - return; - } else if (v->IsObject()) { - model->insert(before, v->ToObject(), groups); - model->emitChanges(); - } -} - -/*! - \qmlmethod QtQuick2::VisualDataGroup::create(int index) - \qmlmethod QtQuick2::VisualDataGroup::create(int index, jsdict data, array groups = undefined) - \qmlmethod QtQuick2::VisualDataGroup::create(jsdict data, array groups = undefined) - - Returns a reference to the instantiated item at \a index in the group. - - If a \a data object is provided it will be \l {insert}{inserted} at \a index and an item - referencing this new entry will be returned. The optional \a groups parameter identifies - the groups the new entry should belong to, if unspecified this is equal to the group create() - was called on. - - All items returned by create are added to the - \l {QtQuick2::VisualDataModel::persistedItems}{persistedItems} group. Items in this - group remain instantiated when not referenced by any view. -*/ - -void QQuickVisualDataGroup::create(QQmlV8Function *args) -{ - Q_D(QQuickVisualDataGroup); - if (!d->model) - return; - - if (args->Length() == 0) - return; - - QQuickVisualDataModelPrivate *model = QQuickVisualDataModelPrivate::get(d->model); - - int index = model->m_compositor.count(d->group); - Compositor::Group group = d->group; - - int i = 0; - v8::Local<v8::Value> v = (*args)[i]; - if (d->parseIndex(v, &index, &group)) - ++i; - - if (i < args->Length() && index >= 0 && index <= model->m_compositor.count(group)) { - v = (*args)[i]; - if (v->IsObject()) { - int groups = 1 << d->group; - if (++i < args->Length()) - groups |= model->m_cacheMetaType->parseGroups((*args)[i]); - - Compositor::insert_iterator before = index < model->m_compositor.count(group) - ? model->m_compositor.findInsertPosition(group, index) - : model->m_compositor.end(); - - index = before.index[d->group]; - group = d->group; - - if (!model->insert(before, v->ToObject(), groups)) { - return; - } - } - } - if (index < 0 || index >= model->m_compositor.count(group)) { - qmlInfo(this) << tr("create: index out of range"); - return; - } - - QObject *object = model->object(group, index, false); - if (object) { - QVector<Compositor::Insert> inserts; - Compositor::iterator it = model->m_compositor.find(group, index); - model->m_compositor.setFlags(it, 1, d->group, Compositor::PersistedFlag, &inserts); - model->itemsInserted(inserts); - model->m_cache.at(it.cacheIndex)->releaseObject(); - } - - args->returnValue(args->engine()->newQObject(object)); - model->emitChanges(); -} - -/*! - \qmlmethod QtQuick2::VisualDataGroup::resolve(int from, int to) - - Binds an unresolved item at \a from to an item in VisualDataModel::model at index \a to. - - Unresolved items are entries whose data has been \l {insert()}{inserted} into a VisualDataGroup - instead of being derived from a VisualDataModel::model index. Resolving an item will replace - the item at the target index with the unresolved item. A resolved an item will reflect the data - of the source model at its bound index and will move when that index moves like any other item. - - If a new item is replaced in the VisualDataGroup onChanged() handler its insertion and - replacement will be communicated to views as an atomic operation, creating the appearance - that the model contents have not changed, or if the unresolved and model item are not adjacent - that the previously unresolved item has simply moved. - -*/ -void QQuickVisualDataGroup::resolve(QQmlV8Function *args) -{ - Q_D(QQuickVisualDataGroup); - if (!d->model) - return; - - QQuickVisualDataModelPrivate *model = QQuickVisualDataModelPrivate::get(d->model); - - if (args->Length() < 2) - return; - - int from = -1; - int to = -1; - Compositor::Group fromGroup = d->group; - Compositor::Group toGroup = d->group; - - v8::Local<v8::Value> v = (*args)[0]; - if (d->parseIndex(v, &from, &fromGroup)) { - if (from < 0 || from >= model->m_compositor.count(fromGroup)) { - qmlInfo(this) << tr("resolve: from index out of range"); - return; - } - } else { - qmlInfo(this) << tr("resolve: from index invalid"); - return; - } - - v = (*args)[1]; - if (d->parseIndex(v, &to, &toGroup)) { - if (to < 0 || to >= model->m_compositor.count(toGroup)) { - qmlInfo(this) << tr("resolve: to index out of range"); - return; - } - } else { - qmlInfo(this) << tr("resolve: to index invalid"); - return; - } - - Compositor::iterator fromIt = model->m_compositor.find(fromGroup, from); - Compositor::iterator toIt = model->m_compositor.find(toGroup, to); - - if (!fromIt->isUnresolved()) { - qmlInfo(this) << tr("resolve: from is not an unresolved item"); - return; - } - if (!toIt->list) { - qmlInfo(this) << tr("resolve: to is not a model item"); - return; - } - - const int unresolvedFlags = fromIt->flags; - const int resolvedFlags = toIt->flags; - const int resolvedIndex = toIt.modelIndex(); - void * const resolvedList = toIt->list; - - QQuickVisualDataModelItem *cacheItem = model->m_cache.at(fromIt.cacheIndex); - cacheItem->groups &= ~Compositor::UnresolvedFlag; - - if (toIt.cacheIndex > fromIt.cacheIndex) - toIt.decrementIndexes(1, unresolvedFlags); - if (!toIt->inGroup(fromGroup) || toIt.index[fromGroup] > from) - from += 1; - - model->itemsMoved( - QVector<Compositor::Remove>() << Compositor::Remove(fromIt, 1, unresolvedFlags, 0), - QVector<Compositor::Insert>() << Compositor::Insert(toIt, 1, unresolvedFlags, 0)); - model->itemsInserted( - QVector<Compositor::Insert>() << Compositor::Insert(toIt, 1, (resolvedFlags & ~unresolvedFlags) | Compositor::CacheFlag)); - toIt.incrementIndexes(1, resolvedFlags | unresolvedFlags); - model->itemsRemoved(QVector<Compositor::Remove>() << Compositor::Remove(toIt, 1, resolvedFlags)); - - model->m_compositor.setFlags(toGroup, to, 1, unresolvedFlags & ~Compositor::UnresolvedFlag); - model->m_compositor.clearFlags(fromGroup, from, 1, unresolvedFlags); - - if (resolvedFlags & Compositor::CacheFlag) - model->m_compositor.insert(Compositor::Cache, toIt.cacheIndex, resolvedList, resolvedIndex, 1, Compositor::CacheFlag); - - Q_ASSERT(model->m_cache.count() == model->m_compositor.count(Compositor::Cache)); - - if (!cacheItem->isReferenced()) { - Q_ASSERT(toIt.cacheIndex == model->m_cache.indexOf(cacheItem)); - model->m_cache.removeAt(toIt.cacheIndex); - model->m_compositor.clearFlags(Compositor::Cache, toIt.cacheIndex, 1, Compositor::CacheFlag); - delete cacheItem; - Q_ASSERT(model->m_cache.count() == model->m_compositor.count(Compositor::Cache)); - } else { - cacheItem->resolveIndex(model->m_adaptorModel, resolvedIndex); - if (cacheItem->attached) - cacheItem->attached->emitUnresolvedChanged(); - } - - model->emitChanges(); -} - -/*! - \qmlmethod QtQuick2::VisualDataGroup::remove(int index, int count) - - Removes \a count items starting at \a index from the group. -*/ - -void QQuickVisualDataGroup::remove(QQmlV8Function *args) -{ - Q_D(QQuickVisualDataGroup); - if (!d->model) - return; - Compositor::Group group = d->group; - int index = -1; - int count = 1; - - if (args->Length() == 0) - return; - - int i = 0; - v8::Local<v8::Value> v = (*args)[i]; - if (!d->parseIndex(v, &index, &group)) { - qmlInfo(this) << tr("remove: invalid index"); - return; - } - - if (++i < args->Length()) { - v = (*args)[i]; - if (v->IsInt32()) - count = v->Int32Value(); - } - - QQuickVisualDataModelPrivate *model = QQuickVisualDataModelPrivate::get(d->model); - if (index < 0 || index >= model->m_compositor.count(group)) { - qmlInfo(this) << tr("remove: index out of range"); - } else if (count != 0) { - Compositor::iterator it = model->m_compositor.find(group, index); - if (count < 0 || count > model->m_compositor.count(d->group) - it.index[d->group]) { - qmlInfo(this) << tr("remove: invalid count"); - } else { - model->removeGroups(it, count, d->group, 1 << d->group); - } - } -} - -bool QQuickVisualDataGroupPrivate::parseGroupArgs( - QQmlV8Function *args, Compositor::Group *group, int *index, int *count, int *groups) const -{ - if (!model || !QQuickVisualDataModelPrivate::get(model)->m_cacheMetaType) - return false; - - if (args->Length() < 2) - return false; - - int i = 0; - v8::Local<v8::Value> v = (*args)[i]; - if (!parseIndex(v, index, group)) - return false; - - v = (*args)[++i]; - if (v->IsInt32()) { - *count = v->Int32Value(); - - if (++i == args->Length()) - return false; - v = (*args)[i]; - } - - *groups = QQuickVisualDataModelPrivate::get(model)->m_cacheMetaType->parseGroups(v); - - return true; -} - -/*! - \qmlmethod QtQuick2::VisualDataGroup::addGroups(int index, int count, stringlist groups) - - Adds \a count items starting at \a index to \a groups. -*/ - -void QQuickVisualDataGroup::addGroups(QQmlV8Function *args) -{ - Q_D(QQuickVisualDataGroup); - Compositor::Group group = d->group; - int index = -1; - int count = 1; - int groups = 0; - - if (!d->parseGroupArgs(args, &group, &index, &count, &groups)) - return; - - QQuickVisualDataModelPrivate *model = QQuickVisualDataModelPrivate::get(d->model); - if (index < 0 || index >= model->m_compositor.count(group)) { - qmlInfo(this) << tr("addGroups: index out of range"); - } else if (count != 0) { - Compositor::iterator it = model->m_compositor.find(group, index); - if (count < 0 || count > model->m_compositor.count(d->group) - it.index[d->group]) { - qmlInfo(this) << tr("addGroups: invalid count"); - } else { - model->addGroups(it, count, d->group, groups); - } - } -} - -/*! - \qmlmethod QtQuick2::VisualDataGroup::removeGroups(int index, int count, stringlist groups) - - Removes \a count items starting at \a index from \a groups. -*/ - -void QQuickVisualDataGroup::removeGroups(QQmlV8Function *args) -{ - Q_D(QQuickVisualDataGroup); - Compositor::Group group = d->group; - int index = -1; - int count = 1; - int groups = 0; - - if (!d->parseGroupArgs(args, &group, &index, &count, &groups)) - return; - - QQuickVisualDataModelPrivate *model = QQuickVisualDataModelPrivate::get(d->model); - if (index < 0 || index >= model->m_compositor.count(group)) { - qmlInfo(this) << tr("removeGroups: index out of range"); - } else if (count != 0) { - Compositor::iterator it = model->m_compositor.find(group, index); - if (count < 0 || count > model->m_compositor.count(d->group) - it.index[d->group]) { - qmlInfo(this) << tr("removeGroups: invalid count"); - } else { - model->removeGroups(it, count, d->group, groups); - } - } -} - -/*! - \qmlmethod QtQuick2::VisualDataGroup::setGroups(int index, int count, stringlist groups) - - Sets the \a groups \a count items starting at \a index belong to. -*/ - -void QQuickVisualDataGroup::setGroups(QQmlV8Function *args) -{ - Q_D(QQuickVisualDataGroup); - Compositor::Group group = d->group; - int index = -1; - int count = 1; - int groups = 0; - - if (!d->parseGroupArgs(args, &group, &index, &count, &groups)) - return; - - QQuickVisualDataModelPrivate *model = QQuickVisualDataModelPrivate::get(d->model); - if (index < 0 || index >= model->m_compositor.count(group)) { - qmlInfo(this) << tr("setGroups: index out of range"); - } else if (count != 0) { - Compositor::iterator it = model->m_compositor.find(group, index); - if (count < 0 || count > model->m_compositor.count(d->group) - it.index[d->group]) { - qmlInfo(this) << tr("setGroups: invalid count"); - } else { - model->setGroups(it, count, d->group, groups); - } - } -} - -/*! - \qmlmethod QtQuick2::VisualDataGroup::setGroups(int index, int count, stringlist groups) - - Sets the \a groups \a count items starting at \a index belong to. -*/ - -/*! - \qmlmethod QtQuick2::VisualDataGroup::move(var from, var to, int count) - - Moves \a count at \a from in a group \a to a new position. -*/ - -void QQuickVisualDataGroup::move(QQmlV8Function *args) -{ - Q_D(QQuickVisualDataGroup); - - if (args->Length() < 2) - return; - - Compositor::Group fromGroup = d->group; - Compositor::Group toGroup = d->group; - int from = -1; - int to = -1; - int count = 1; - - if (!d->parseIndex((*args)[0], &from, &fromGroup)) { - qmlInfo(this) << tr("move: invalid from index"); - return; - } - - if (!d->parseIndex((*args)[1], &to, &toGroup)) { - qmlInfo(this) << tr("move: invalid to index"); - return; - } - - if (args->Length() > 2) { - v8::Local<v8::Value> v = (*args)[2]; - if (v->IsInt32()) - count = v->Int32Value(); - } - - QQuickVisualDataModelPrivate *model = QQuickVisualDataModelPrivate::get(d->model); - - if (count < 0) { - qmlInfo(this) << tr("move: invalid count"); - } else if (from < 0 || from + count > model->m_compositor.count(fromGroup)) { - qmlInfo(this) << tr("move: from index out of range"); - } else if (!model->m_compositor.verifyMoveTo(fromGroup, from, toGroup, to, count, d->group)) { - qmlInfo(this) << tr("move: to index out of range"); - } else if (count > 0) { - QVector<Compositor::Remove> removes; - QVector<Compositor::Insert> inserts; - - model->m_compositor.move(fromGroup, from, toGroup, to, count, d->group, &removes, &inserts); - model->itemsMoved(removes, inserts); - model->emitChanges(); - } - -} - -/*! - \qmlsignal QtQuick2::VisualDataGroup::onChanged(array removed, array inserted) - - This handler is called when items have been removed from or inserted into the group. - - Each object in the \a removed and \a inserted arrays has two values; the \e index of the first - item inserted or removed and a \e count of the number of consecutive items inserted or removed. - - Each index is adjusted for previous changes with all removed items preceding any inserted - items. -*/ - -//============================================================================ - -QQuickVisualPartsModel::QQuickVisualPartsModel(QQuickVisualDataModel *model, const QString &part, QObject *parent) - : QQuickVisualModel(*new QObjectPrivate, parent) - , m_model(model) - , m_part(part) - , m_compositorGroup(Compositor::Cache) - , m_inheritGroup(true) -{ - QQuickVisualDataModelPrivate *d = QQuickVisualDataModelPrivate::get(m_model); - if (d->m_cacheMetaType) { - QQuickVisualDataGroupPrivate::get(d->m_groups[1])->emitters.insert(this); - m_compositorGroup = Compositor::Default; - } else { - d->m_pendingParts.insert(this); - } -} - -QQuickVisualPartsModel::~QQuickVisualPartsModel() -{ -} - -QString QQuickVisualPartsModel::filterGroup() const -{ - if (m_inheritGroup) - return m_model->filterGroup(); - return m_filterGroup; -} - -void QQuickVisualPartsModel::setFilterGroup(const QString &group) -{ - if (QQuickVisualDataModelPrivate::get(m_model)->m_transaction) { - qmlInfo(this) << tr("The group of a VisualDataModel cannot be changed within onChanged"); - return; - } - - if (m_filterGroup != group || m_inheritGroup) { - m_filterGroup = group; - m_inheritGroup = false; - updateFilterGroup(); - - emit filterGroupChanged(); - } -} - -void QQuickVisualPartsModel::resetFilterGroup() -{ - if (!m_inheritGroup) { - m_inheritGroup = true; - updateFilterGroup(); - emit filterGroupChanged(); - } -} - -void QQuickVisualPartsModel::updateFilterGroup() -{ - QQuickVisualDataModelPrivate *model = QQuickVisualDataModelPrivate::get(m_model); - if (!model->m_cacheMetaType) - return; - - if (m_inheritGroup) { - if (m_filterGroup == model->m_filterGroup) - return; - m_filterGroup = model->m_filterGroup; - } - - QQuickListCompositor::Group previousGroup = m_compositorGroup; - m_compositorGroup = Compositor::Default; - QQuickVisualDataGroupPrivate::get(model->m_groups[Compositor::Default])->emitters.insert(this); - for (int i = 1; i < model->m_groupCount; ++i) { - if (m_filterGroup == model->m_cacheMetaType->groupNames.at(i - 1)) { - m_compositorGroup = Compositor::Group(i); - break; - } - } - - QQuickVisualDataGroupPrivate::get(model->m_groups[m_compositorGroup])->emitters.insert(this); - if (m_compositorGroup != previousGroup) { - QVector<QQuickChangeSet::Remove> removes; - QVector<QQuickChangeSet::Insert> inserts; - model->m_compositor.transition(previousGroup, m_compositorGroup, &removes, &inserts); - - QQuickChangeSet changeSet; - changeSet.move(removes, inserts); - if (!changeSet.isEmpty()) - emit modelUpdated(changeSet, false); - - if (changeSet.difference() != 0) - emit countChanged(); - } -} - -void QQuickVisualPartsModel::updateFilterGroup( - Compositor::Group group, const QQuickChangeSet &changeSet) -{ - if (!m_inheritGroup) - return; - - m_compositorGroup = group; - QQuickVisualDataGroupPrivate::get(QQuickVisualDataModelPrivate::get(m_model)->m_groups[m_compositorGroup])->emitters.insert(this); - - if (!changeSet.isEmpty()) - emit modelUpdated(changeSet, false); - - if (changeSet.difference() != 0) - emit countChanged(); - - emit filterGroupChanged(); -} - -int QQuickVisualPartsModel::count() const -{ - QQuickVisualDataModelPrivate *model = QQuickVisualDataModelPrivate::get(m_model); - return model->m_delegate - ? model->m_compositor.count(m_compositorGroup) - : 0; -} - -bool QQuickVisualPartsModel::isValid() const -{ - return m_model->isValid(); -} - -QQuickItem *QQuickVisualPartsModel::item(int index, bool asynchronous) -{ - QQuickVisualDataModelPrivate *model = QQuickVisualDataModelPrivate::get(m_model); - - if (!model->m_delegate || index < 0 || index >= model->m_compositor.count(m_compositorGroup)) { - qWarning() << "VisualDataModel::item: index out range" << index << model->m_compositor.count(m_compositorGroup); - return 0; - } - - QObject *object = model->object(m_compositorGroup, index, asynchronous); - - if (QQuickPackage *package = qmlobject_cast<QQuickPackage *>(object)) { - QObject *part = package->part(m_part); - if (!part) - return 0; - if (QQuickItem *item = qmlobject_cast<QQuickItem *>(part)) { - m_packaged.insertMulti(item, package); - return item; - } - } - - model->release(object); - if (!model->m_delegateValidated) { - if (object) - qmlInfo(model->m_delegate) << tr("Delegate component must be Package type."); - model->m_delegateValidated = true; - } - - return 0; -} - -QQuickVisualModel::ReleaseFlags QQuickVisualPartsModel::release(QQuickItem *item) -{ - QQuickVisualModel::ReleaseFlags flags = 0; - - QHash<QObject *, QQuickPackage *>::iterator it = m_packaged.find(item); - if (it != m_packaged.end()) { - QQuickPackage *package = *it; - QQuickVisualDataModelPrivate *model = QQuickVisualDataModelPrivate::get(m_model); - flags = model->release(package); - m_packaged.erase(it); - if (!m_packaged.contains(item)) - flags &= ~Referenced; - if (flags & Destroyed) - QQuickVisualDataModelPrivate::get(m_model)->emitDestroyingPackage(package); - } - return flags; -} - -QString QQuickVisualPartsModel::stringValue(int index, const QString &role) -{ - return QQuickVisualDataModelPrivate::get(m_model)->stringValue(m_compositorGroup, index, role); -} - -void QQuickVisualPartsModel::setWatchedRoles(QList<QByteArray> roles) -{ - QQuickVisualDataModelPrivate *model = QQuickVisualDataModelPrivate::get(m_model); - model->m_adaptorModel.replaceWatchedRoles(m_watchedRoles, roles); - m_watchedRoles = roles; -} - -int QQuickVisualPartsModel::indexOf(QQuickItem *item, QObject *) const -{ - QHash<QObject *, QQuickPackage *>::const_iterator it = m_packaged.find(item); - if (it != m_packaged.end()) { - if (QQuickVisualDataModelItem *cacheItem = QQuickVisualDataModelItem::dataForObject(*it)) - return cacheItem->groupIndex(m_compositorGroup); - } - return -1; -} - -void QQuickVisualPartsModel::createdPackage(int index, QQuickPackage *package) -{ - if (QQuickItem *item = qmlobject_cast<QQuickItem *>(package->part(m_part))) - emit createdItem(index, item); -} - -void QQuickVisualPartsModel::initPackage(int index, QQuickPackage *package) -{ - if (QQuickItem *item = qmlobject_cast<QQuickItem *>(package->part(m_part))) - emit initItem(index, item); -} - -void QQuickVisualPartsModel::destroyingPackage(QQuickPackage *package) -{ - if (QQuickItem *item = qmlobject_cast<QQuickItem *>(package->part(m_part))) { - Q_ASSERT(!m_packaged.contains(item)); - emit destroyingItem(item); - item->setParentItem(0); - } -} - -void QQuickVisualPartsModel::emitModelUpdated(const QQuickChangeSet &changeSet, bool reset) -{ - emit modelUpdated(changeSet, reset); - if (changeSet.difference() != 0) - emit countChanged(); -} - -//============================================================================ - -v8::Handle<v8::Value> get_change_index(v8::Local<v8::String>, const v8::AccessorInfo &info) -{ - return info.This()->GetInternalField(0); -} - -v8::Handle<v8::Value> get_change_count(v8::Local<v8::String>, const v8::AccessorInfo &info) -{ - return info.This()->GetInternalField(1); -} - -v8::Handle<v8::Value> get_change_moveId(v8::Local<v8::String>, const v8::AccessorInfo &info) -{ - return info.This()->GetInternalField(2); -} - -class QQuickVisualDataGroupChangeArray : public QV8ObjectResource -{ - V8_RESOURCE_TYPE(ChangeSetArrayType) -public: - QQuickVisualDataGroupChangeArray(QV8Engine *engine) - : QV8ObjectResource(engine) - { - } - - virtual quint32 count() const = 0; - virtual const QQuickChangeSet::Change &at(int index) const = 0; - - static v8::Handle<v8::Value> get_change(quint32 index, const v8::AccessorInfo &info) - { - QQuickVisualDataGroupChangeArray *array = v8_resource_cast<QQuickVisualDataGroupChangeArray>(info.This()); - V8ASSERT_TYPE(array, "Not a valid change array"); - - if (index >= array->count()) - return v8::Undefined(); - - const QQuickChangeSet::Change &change = array->at(index); - - v8::Local<v8::Object> object = engineData(array->engine)->constructorChange->NewInstance(); - object->SetInternalField(0, v8::Int32::New(change.index)); - object->SetInternalField(1, v8::Int32::New(change.count)); - if (change.isMove()) - object->SetInternalField(2, v8::Int32::New(change.moveId)); - - return object; - } - - static v8::Handle<v8::Value> get_length(v8::Local<v8::String>, const v8::AccessorInfo &info) - { - QQuickVisualDataGroupChangeArray *array = v8_resource_cast<QQuickVisualDataGroupChangeArray>(info.This()); - V8ASSERT_TYPE(array, "Not a valid change array"); - - return v8::Integer::New(array->count()); - } - - static v8::Local<v8::Function> constructor() - { - v8::Local<v8::FunctionTemplate> changeArray = v8::FunctionTemplate::New(); - changeArray->InstanceTemplate()->SetHasExternalResource(true); - changeArray->InstanceTemplate()->SetIndexedPropertyHandler(get_change); - changeArray->InstanceTemplate()->SetAccessor(v8::String::New("length"), get_length); - return changeArray->GetFunction(); - } -}; - -class QQuickVisualDataGroupRemoveArray : public QQuickVisualDataGroupChangeArray -{ -public: - QQuickVisualDataGroupRemoveArray(QV8Engine *engine, const QVector<QQuickChangeSet::Remove> &changes) - : QQuickVisualDataGroupChangeArray(engine) - , changes(changes) - { - } - - quint32 count() const { return changes.count(); } - const QQuickChangeSet::Change &at(int index) const { return changes.at(index); } - -private: - QVector<QQuickChangeSet::Remove> changes; -}; - -class QQuickVisualDataGroupInsertArray : public QQuickVisualDataGroupChangeArray -{ -public: - QQuickVisualDataGroupInsertArray(QV8Engine *engine, const QVector<QQuickChangeSet::Insert> &changes) - : QQuickVisualDataGroupChangeArray(engine) - , changes(changes) - { - } - - quint32 count() const { return changes.count(); } - const QQuickChangeSet::Change &at(int index) const { return changes.at(index); } - -private: - QVector<QQuickChangeSet::Insert> changes; -}; - -QQuickVisualDataModelEngineData::QQuickVisualDataModelEngineData(QV8Engine *) -{ - strings = qPersistentNew(v8::Array::New(StringCount)); - strings->Set(Model, v8::String::New("model")); - strings->Set(Groups, v8::String::New("groups")); - strings->Set(IsUnresolved, v8::String::New("isUnresolved")); - strings->Set(ItemsIndex, v8::String::New("itemsIndex")); - strings->Set(PersistedItemsIndex, v8::String::New("persistedItemsIndex")); - strings->Set(InItems, v8::String::New("inItems")); - strings->Set(InPersistedItems, v8::String::New("inPersistedItems")); - - v8::Local<v8::FunctionTemplate> change = v8::FunctionTemplate::New(); - change->InstanceTemplate()->SetAccessor(v8::String::New("index"), get_change_index); - change->InstanceTemplate()->SetAccessor(v8::String::New("count"), get_change_count); - change->InstanceTemplate()->SetAccessor(v8::String::New("moveId"), get_change_moveId); - change->InstanceTemplate()->SetInternalFieldCount(3); - constructorChange = qPersistentNew(change->GetFunction()); - constructorChangeArray = qPersistentNew(QQuickVisualDataGroupChangeArray::constructor()); -} - -QQuickVisualDataModelEngineData::~QQuickVisualDataModelEngineData() -{ - qPersistentDispose(strings); - qPersistentDispose(constructorChange); - qPersistentDispose(constructorChangeArray); -} - -v8::Local<v8::Object> QQuickVisualDataModelEngineData::array( - QV8Engine *engine, const QVector<QQuickChangeSet::Remove> &changes) -{ - v8::Local<v8::Object> array = constructorChangeArray->NewInstance(); - array->SetExternalResource(new QQuickVisualDataGroupRemoveArray(engine, changes)); - return array; -} - -v8::Local<v8::Object> QQuickVisualDataModelEngineData::array( - QV8Engine *engine, const QVector<QQuickChangeSet::Insert> &changes) -{ - v8::Local<v8::Object> array = constructorChangeArray->NewInstance(); - array->SetExternalResource(new QQuickVisualDataGroupInsertArray(engine, changes)); - return array; -} - -QT_END_NAMESPACE - diff --git a/src/quick/items/qquickvisualdatamodel_p.h b/src/quick/items/qquickvisualdatamodel_p.h deleted file mode 100644 index 03e06c82ac..0000000000 --- a/src/quick/items/qquickvisualdatamodel_p.h +++ /dev/null @@ -1,238 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal -** -** This file is part of the QtQml module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QQUICKVISUALDATAMODEL_P_H -#define QQUICKVISUALDATAMODEL_P_H - -#include <private/qtquickglobal_p.h> -#include <private/qquicklistcompositor_p.h> -#include <private/qquickvisualitemmodel_p.h> - -#include <QtCore/qabstractitemmodel.h> -#include <QtCore/qstringlist.h> - -#include <private/qv8engine_p.h> -#include <private/qqmlglobal_p.h> - -QT_BEGIN_HEADER - -Q_DECLARE_METATYPE(QModelIndex) - -QT_BEGIN_NAMESPACE - -class QQuickChangeSet; -class QQmlComponent; -class QQuickPackage; -class QQmlV8Function; -class QQuickVisualDataGroup; -class QQuickVisualDataModelAttached; -class QQuickVisualDataModelPrivate; - - -class Q_QUICK_PRIVATE_EXPORT QQuickVisualDataModel : public QQuickVisualModel, public QQmlParserStatus -{ - Q_OBJECT - Q_DECLARE_PRIVATE(QQuickVisualDataModel) - - Q_PROPERTY(QVariant model READ model WRITE setModel) - Q_PROPERTY(QQmlComponent *delegate READ delegate WRITE setDelegate) - Q_PROPERTY(QString filterOnGroup READ filterGroup WRITE setFilterGroup NOTIFY filterGroupChanged RESET resetFilterGroup) - Q_PROPERTY(QQuickVisualDataGroup *items READ items CONSTANT) - Q_PROPERTY(QQuickVisualDataGroup *persistedItems READ persistedItems CONSTANT) - Q_PROPERTY(QQmlListProperty<QQuickVisualDataGroup> groups READ groups CONSTANT) - Q_PROPERTY(QObject *parts READ parts CONSTANT) - Q_PROPERTY(QVariant rootIndex READ rootIndex WRITE setRootIndex NOTIFY rootIndexChanged) - Q_CLASSINFO("DefaultProperty", "delegate") - Q_INTERFACES(QQmlParserStatus) -public: - QQuickVisualDataModel(); - QQuickVisualDataModel(QQmlContext *, QObject *parent=0); - virtual ~QQuickVisualDataModel(); - - void classBegin(); - void componentComplete(); - - QVariant model() const; - void setModel(const QVariant &); - - QQmlComponent *delegate() const; - void setDelegate(QQmlComponent *); - - QVariant rootIndex() const; - void setRootIndex(const QVariant &root); - - Q_INVOKABLE QVariant modelIndex(int idx) const; - Q_INVOKABLE QVariant parentModelIndex() const; - - int count() const; - bool isValid() const { return delegate() != 0; } - QQuickItem *item(int index, bool asynchronous=false); - ReleaseFlags release(QQuickItem *item); - void cancel(int index); - virtual QString stringValue(int index, const QString &role); - virtual void setWatchedRoles(QList<QByteArray> roles); - - int indexOf(QQuickItem *item, QObject *objectContext) const; - - QString filterGroup() const; - void setFilterGroup(const QString &group); - void resetFilterGroup(); - - QQuickVisualDataGroup *items(); - QQuickVisualDataGroup *persistedItems(); - QQmlListProperty<QQuickVisualDataGroup> groups(); - QObject *parts(); - - bool event(QEvent *); - - static QQuickVisualDataModelAttached *qmlAttachedProperties(QObject *obj); - -Q_SIGNALS: - void filterGroupChanged(); - void defaultGroupsChanged(); - void rootIndexChanged(); - -private Q_SLOTS: - void _q_itemsChanged(int index, int count, const QVector<int> &roles); - void _q_itemsInserted(int index, int count); - void _q_itemsRemoved(int index, int count); - void _q_itemsMoved(int from, int to, int count); - void _q_modelReset(); - void _q_rowsInserted(const QModelIndex &,int,int); - void _q_rowsAboutToBeRemoved(const QModelIndex &parent, int begin, int end); - void _q_rowsRemoved(const QModelIndex &,int,int); - void _q_rowsMoved(const QModelIndex &, int, int, const QModelIndex &, int); - void _q_dataChanged(const QModelIndex&,const QModelIndex&,const QVector<int> &); - void _q_layoutChanged(); - -private: - Q_DISABLE_COPY(QQuickVisualDataModel) -}; - -class QQuickVisualDataGroupPrivate; -class Q_AUTOTEST_EXPORT QQuickVisualDataGroup : public QObject -{ - Q_OBJECT - Q_PROPERTY(int count READ count NOTIFY countChanged) - Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) - Q_PROPERTY(bool includeByDefault READ defaultInclude WRITE setDefaultInclude NOTIFY defaultIncludeChanged) -public: - QQuickVisualDataGroup(QObject *parent = 0); - QQuickVisualDataGroup(const QString &name, QQuickVisualDataModel *model, int compositorType, QObject *parent = 0); - ~QQuickVisualDataGroup(); - - QString name() const; - void setName(const QString &name); - - int count() const; - - bool defaultInclude() const; - void setDefaultInclude(bool include); - - Q_INVOKABLE QQmlV8Handle get(int index); - -public Q_SLOTS: - void insert(QQmlV8Function *); - void create(QQmlV8Function *); - void resolve(QQmlV8Function *); - void remove(QQmlV8Function *); - void addGroups(QQmlV8Function *); - void removeGroups(QQmlV8Function *); - void setGroups(QQmlV8Function *); - void move(QQmlV8Function *); - -Q_SIGNALS: - void countChanged(); - void nameChanged(); - void defaultIncludeChanged(); - void changed(const QQmlV8Handle &removed, const QQmlV8Handle &inserted); -private: - Q_DECLARE_PRIVATE(QQuickVisualDataGroup) -}; - -class QQuickVisualDataModelItem; -class QQuickVisualDataModelAttachedMetaObject; -class QQuickVisualDataModelAttached : public QObject -{ - Q_OBJECT - Q_PROPERTY(QQuickVisualDataModel *model READ model CONSTANT) - Q_PROPERTY(QStringList groups READ groups WRITE setGroups NOTIFY groupsChanged) - Q_PROPERTY(bool isUnresolved READ isUnresolved NOTIFY unresolvedChanged) -public: - QQuickVisualDataModelAttached(QObject *parent); - QQuickVisualDataModelAttached(QQuickVisualDataModelItem *cacheItem, QObject *parent); - ~QQuickVisualDataModelAttached() {} - - void setCacheItem(QQuickVisualDataModelItem *item); - - QQuickVisualDataModel *model() const; - - QStringList groups() const; - void setGroups(const QStringList &groups); - - bool isUnresolved() const; - - void emitChanges(); - - void emitUnresolvedChanged() { emit unresolvedChanged(); } - -Q_SIGNALS: - void groupsChanged(); - void unresolvedChanged(); - -public: - QQuickVisualDataModelItem *m_cacheItem; - int m_previousGroups; - int m_currentIndex[QQuickListCompositor::MaximumGroupCount]; - int m_previousIndex[QQuickListCompositor::MaximumGroupCount]; - - friend class QQuickVisualDataModelAttachedMetaObject; -}; - -QT_END_NAMESPACE - -QML_DECLARE_TYPE(QQuickVisualDataModel) -QML_DECLARE_TYPEINFO(QQuickVisualDataModel, QML_HAS_ATTACHED_PROPERTIES) -QML_DECLARE_TYPE(QQuickVisualDataGroup) - -QT_END_HEADER - -#endif // QQUICKVISUALDATAMODEL_P_H diff --git a/src/quick/items/qquickvisualdatamodel_p_p.h b/src/quick/items/qquickvisualdatamodel_p_p.h deleted file mode 100644 index 89ecece064..0000000000 --- a/src/quick/items/qquickvisualdatamodel_p_p.h +++ /dev/null @@ -1,411 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal -** -** This file is part of the QtQml module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QQUICKVISUALDATAMODEL_P_P_H -#define QQUICKVISUALDATAMODEL_P_P_H - -#include "qquickvisualdatamodel_p.h" - -#include "qquickvisualadaptormodel_p.h" - -#include <QtQml/qqmlcontext.h> -#include <QtQml/qqmlincubator.h> - -#include <private/qqmlopenmetaobject_p.h> - -// -// W A R N I N G -// ------------- -// -// This file is not part of the Qt API. It exists purely as an -// implementation detail. This header file may change from version to -// version without notice, or even be removed. -// -// We mean it. -// - -QT_BEGIN_NAMESPACE - -typedef QQuickListCompositor Compositor; - -class QQuickVisualDataModelAttachedMetaObject; - -class QQuickVisualDataModelItemMetaType : public QQmlRefCount -{ -public: - QQuickVisualDataModelItemMetaType(QV8Engine *engine, QQuickVisualDataModel *model, const QStringList &groupNames); - ~QQuickVisualDataModelItemMetaType(); - - void initializeMetaObject(); - void initializeConstructor(); - - int parseGroups(const QStringList &groupNames) const; - int parseGroups(const v8::Local<v8::Value> &groupNames) const; - - static void release_index(v8::Persistent<v8::Value> object, void *parameter); - static void release_model(v8::Persistent<v8::Value> object, void *parameter); - - static v8::Handle<v8::Value> get_model(v8::Local<v8::String>, const v8::AccessorInfo &info); - static v8::Handle<v8::Value> get_groups(v8::Local<v8::String>, const v8::AccessorInfo &info); - static void set_groups( - v8::Local<v8::String>, v8::Local<v8::Value> value, const v8::AccessorInfo &info); - static v8::Handle<v8::Value> get_member(v8::Local<v8::String>, const v8::AccessorInfo &info); - static void set_member( - v8::Local<v8::String>, v8::Local<v8::Value> value, const v8::AccessorInfo &info); - static v8::Handle<v8::Value> get_index(v8::Local<v8::String>, const v8::AccessorInfo &info); - - QQmlGuard<QQuickVisualDataModel> model; - const int groupCount; - QV8Engine * const v8Engine; - QQuickVisualDataModelAttachedMetaObject *metaObject; - const QStringList groupNames; - v8::Persistent<v8::ObjectTemplate> constructor; -}; - -class QQuickVisualAdaptorModel; -class QVDMIncubationTask; - -class QQuickVisualDataModelItem : public QObject, public QV8ObjectResource -{ - Q_OBJECT - Q_PROPERTY(int index READ modelIndex NOTIFY modelIndexChanged) - Q_PROPERTY(QObject *model READ modelObject CONSTANT) - V8_RESOURCE_TYPE(VisualDataItemType) -public: - QQuickVisualDataModelItem(QQuickVisualDataModelItemMetaType *metaType, int modelIndex); - ~QQuickVisualDataModelItem(); - - void referenceObject() { ++objectRef; } - bool releaseObject() { return --objectRef == 0 && !(groups & Compositor::PersistedFlag); } - bool isObjectReferenced() const { return objectRef != 0 || (groups & Compositor::PersistedFlag); } - - bool isReferenced() const { - return scriptRef - || incubationTask - || ((groups & Compositor::UnresolvedFlag) && (groups & Compositor::GroupMask)); - } - - void Dispose(); - - QObject *modelObject() { return this; } - - void incubateObject( - QQmlComponent *component, - QQmlEngine *engine, - QQmlContextData *context, - QQmlContextData *forContext); - void destroyObject(); - - static QQuickVisualDataModelItem *dataForObject(QObject *object); - - int groupIndex(Compositor::Group group); - - int modelIndex() const { return index; } - void setModelIndex(int idx) { index = idx; emit modelIndexChanged(); } - - virtual v8::Handle<v8::Value> get() { return engine->newQObject(this); } - - virtual void setValue(const QString &role, const QVariant &value) { Q_UNUSED(role); Q_UNUSED(value); } - virtual bool resolveIndex(const QQuickVisualAdaptorModel &, int) { return false; } - - QQuickVisualDataModelItemMetaType * const metaType; - QQmlContextData *contextData; - QObject *object; - QQuickVisualDataModelAttached *attached; - QVDMIncubationTask *incubationTask; - int objectRef; - int scriptRef; - int groups; - int index; - - -Q_SIGNALS: - void modelIndexChanged(); - -protected: - void objectDestroyed(QObject *); -}; - - -class QQuickVisualDataModelPrivate; -class QVDMIncubationTask : public QQmlIncubator -{ -public: - QVDMIncubationTask(QQuickVisualDataModelPrivate *l, IncubationMode mode) - : QQmlIncubator(mode) - , incubating(0) - , vdm(l) {} - - virtual void statusChanged(Status); - virtual void setInitialState(QObject *); - - QQuickVisualDataModelItem *incubating; - QQuickVisualDataModelPrivate *vdm; - int index[QQuickListCompositor::MaximumGroupCount]; -}; - - -class QQuickVisualDataGroupEmitter -{ -public: - virtual ~QQuickVisualDataGroupEmitter() {} - virtual void emitModelUpdated(const QQuickChangeSet &changeSet, bool reset) = 0; - virtual void createdPackage(int, QQuickPackage *) {} - virtual void initPackage(int, QQuickPackage *) {} - virtual void destroyingPackage(QQuickPackage *) {} - - QIntrusiveListNode emitterNode; -}; - -typedef QIntrusiveList<QQuickVisualDataGroupEmitter, &QQuickVisualDataGroupEmitter::emitterNode> QQuickVisualDataGroupEmitterList; - -class QQuickVisualDataGroupPrivate : public QObjectPrivate -{ -public: - Q_DECLARE_PUBLIC(QQuickVisualDataGroup) - - QQuickVisualDataGroupPrivate() : group(Compositor::Cache), defaultInclude(false) {} - - static QQuickVisualDataGroupPrivate *get(QQuickVisualDataGroup *group) { - return static_cast<QQuickVisualDataGroupPrivate *>(QObjectPrivate::get(group)); } - - void setModel(QQuickVisualDataModel *model, Compositor::Group group); - bool isChangedConnected(); - void emitChanges(QV8Engine *engine); - void emitModelUpdated(bool reset); - - void createdPackage(int index, QQuickPackage *package); - void initPackage(int index, QQuickPackage *package); - void destroyingPackage(QQuickPackage *package); - - bool parseIndex(const v8::Local<v8::Value> &value, int *index, Compositor::Group *group) const; - bool parseGroupArgs( - QQmlV8Function *args, Compositor::Group *group, int *index, int *count, int *groups) const; - - Compositor::Group group; - QQmlGuard<QQuickVisualDataModel> model; - QQuickVisualDataGroupEmitterList emitters; - QQuickChangeSet changeSet; - QString name; - bool defaultInclude; -}; - -class QQuickVisualDataModelParts; - -class QQuickVisualDataModelPrivate : public QObjectPrivate, public QQuickVisualDataGroupEmitter -{ - Q_DECLARE_PUBLIC(QQuickVisualDataModel) -public: - QQuickVisualDataModelPrivate(QQmlContext *); - ~QQuickVisualDataModelPrivate(); - - static QQuickVisualDataModelPrivate *get(QQuickVisualDataModel *m) { - return static_cast<QQuickVisualDataModelPrivate *>(QObjectPrivate::get(m)); - } - - void init(); - void connectModel(QQuickVisualAdaptorModel *model); - - QObject *object(Compositor::Group group, int index, bool asynchronous); - QQuickVisualDataModel::ReleaseFlags release(QObject *object); - QString stringValue(Compositor::Group group, int index, const QString &name); - void emitCreatedPackage(QVDMIncubationTask *incubationTask, QQuickPackage *package); - void emitInitPackage(QVDMIncubationTask *incubationTask, QQuickPackage *package); - void emitCreatedItem(QVDMIncubationTask *incubationTask, QQuickItem *item) { - emit q_func()->createdItem(incubationTask->index[m_compositorGroup], item); } - void emitInitItem(QVDMIncubationTask *incubationTask, QQuickItem *item) { - emit q_func()->initItem(incubationTask->index[m_compositorGroup], item); } - void emitDestroyingPackage(QQuickPackage *package); - void emitDestroyingItem(QQuickItem *item) { emit q_func()->destroyingItem(item); } - void removeCacheItem(QQuickVisualDataModelItem *cacheItem); - - void updateFilterGroup(); - - void addGroups(Compositor::iterator from, int count, Compositor::Group group, int groupFlags); - void removeGroups(Compositor::iterator from, int count, Compositor::Group group, int groupFlags); - void setGroups(Compositor::iterator from, int count, Compositor::Group group, int groupFlags); - - void itemsInserted( - const QVector<Compositor::Insert> &inserts, - QVarLengthArray<QVector<QQuickChangeSet::Insert>, Compositor::MaximumGroupCount> *translatedInserts, - QHash<int, QList<QQuickVisualDataModelItem *> > *movedItems = 0); - void itemsInserted(const QVector<Compositor::Insert> &inserts); - void itemsRemoved( - const QVector<Compositor::Remove> &removes, - QVarLengthArray<QVector<QQuickChangeSet::Remove>, Compositor::MaximumGroupCount> *translatedRemoves, - QHash<int, QList<QQuickVisualDataModelItem *> > *movedItems = 0); - void itemsRemoved(const QVector<Compositor::Remove> &removes); - void itemsMoved( - const QVector<Compositor::Remove> &removes, const QVector<Compositor::Insert> &inserts); - void itemsChanged(const QVector<Compositor::Change> &changes); - template <typename T> static v8::Local<v8::Array> buildChangeList(const QVector<T> &changes); - void emitChanges(); - void emitModelUpdated(const QQuickChangeSet &changeSet, bool reset); - - bool insert(Compositor::insert_iterator &before, const v8::Local<v8::Object> &object, int groups); - - static void group_append(QQmlListProperty<QQuickVisualDataGroup> *property, QQuickVisualDataGroup *group); - static int group_count(QQmlListProperty<QQuickVisualDataGroup> *property); - static QQuickVisualDataGroup *group_at(QQmlListProperty<QQuickVisualDataGroup> *property, int index); - - void releaseIncubator(QVDMIncubationTask *incubationTask); - void incubatorStatusChanged(QVDMIncubationTask *incubationTask, QQmlIncubator::Status status); - void setInitialState(QVDMIncubationTask *incubationTask, QObject *o); - - QQuickVisualAdaptorModel m_adaptorModel; - QQuickListCompositor m_compositor; - QQmlComponent *m_delegate; - QQuickVisualDataModelItemMetaType *m_cacheMetaType; - QQmlContext *m_context; - QQuickVisualDataModelParts *m_parts; - QQuickVisualDataGroupEmitterList m_pendingParts; - - QList<QQuickVisualDataModelItem *> m_cache; - QList<QVDMIncubationTask *> m_finishedIncubating; - QList<QByteArray> m_watchedRoles; - - QString m_filterGroup; - - int m_count; - int m_groupCount; - - QQuickListCompositor::Group m_compositorGroup; - bool m_complete : 1; - bool m_delegateValidated : 1; - bool m_reset : 1; - bool m_transaction : 1; - bool m_incubatorCleanupScheduled : 1; - - union { - struct { - QQuickVisualDataGroup *m_cacheItems; - QQuickVisualDataGroup *m_items; - QQuickVisualDataGroup *m_persistedItems; - }; - QQuickVisualDataGroup *m_groups[Compositor::MaximumGroupCount]; - }; -}; - -class QQuickVisualPartsModel : public QQuickVisualModel, public QQuickVisualDataGroupEmitter -{ - Q_OBJECT - Q_PROPERTY(QString filterOnGroup READ filterGroup WRITE setFilterGroup NOTIFY filterGroupChanged RESET resetFilterGroup) -public: - QQuickVisualPartsModel(QQuickVisualDataModel *model, const QString &part, QObject *parent = 0); - ~QQuickVisualPartsModel(); - - QString filterGroup() const; - void setFilterGroup(const QString &group); - void resetFilterGroup(); - void updateFilterGroup(); - void updateFilterGroup(Compositor::Group group, const QQuickChangeSet &changeSet); - - int count() const; - bool isValid() const; - QQuickItem *item(int index, bool asynchronous=false); - ReleaseFlags release(QQuickItem *item); - QString stringValue(int index, const QString &role); - QList<QByteArray> watchedRoles() const { return m_watchedRoles; } - void setWatchedRoles(QList<QByteArray> roles); - - int indexOf(QQuickItem *item, QObject *objectContext) const; - - void emitModelUpdated(const QQuickChangeSet &changeSet, bool reset); - - void createdPackage(int index, QQuickPackage *package); - void initPackage(int index, QQuickPackage *package); - void destroyingPackage(QQuickPackage *package); - -Q_SIGNALS: - void filterGroupChanged(); - -private: - QQuickVisualDataModel *m_model; - QHash<QObject *, QQuickPackage *> m_packaged; - QString m_part; - QString m_filterGroup; - QList<QByteArray> m_watchedRoles; - Compositor::Group m_compositorGroup; - bool m_inheritGroup; -}; - -class QMetaPropertyBuilder; - -class QQuickVisualDataModelPartsMetaObject : public QQmlOpenMetaObject -{ -public: - QQuickVisualDataModelPartsMetaObject(QObject *parent) - : QQmlOpenMetaObject(parent) {} - - virtual void propertyCreated(int, QMetaPropertyBuilder &); - virtual QVariant initialValue(int); -}; - -class QQuickVisualDataModelParts : public QObject -{ -Q_OBJECT -public: - QQuickVisualDataModelParts(QQuickVisualDataModel *parent); - - QQuickVisualDataModel *model; - QList<QQuickVisualPartsModel *> models; -}; - -class QQuickVisualDataModelAttachedMetaObject : public QAbstractDynamicMetaObject, public QQmlRefCount -{ -public: - QQuickVisualDataModelAttachedMetaObject( - QQuickVisualDataModelItemMetaType *metaType, QMetaObject *metaObject); - ~QQuickVisualDataModelAttachedMetaObject(); - - void objectDestroyed(QObject *); - int metaCall(QObject *, QMetaObject::Call, int _id, void **); - -private: - QQuickVisualDataModelItemMetaType * const metaType; - QMetaObject * const metaObject; - const int memberPropertyOffset; - const int indexPropertyOffset; -}; - -QT_END_NAMESPACE - -#endif diff --git a/src/quick/items/qquickvisualitemmodel.cpp b/src/quick/items/qquickvisualitemmodel.cpp deleted file mode 100644 index bdc489e2cf..0000000000 --- a/src/quick/items/qquickvisualitemmodel.cpp +++ /dev/null @@ -1,256 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal -** -** This file is part of the QtQml module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "qquickvisualitemmodel_p.h" -#include "qquickitem.h" - -#include <QtCore/qcoreapplication.h> -#include <QtQml/qqmlcontext.h> -#include <QtQml/qqmlengine.h> - -#include <private/qquickchangeset_p.h> -#include <private/qqmlglobal_p.h> -#include <private/qobject_p.h> - -#include <QtCore/qhash.h> -#include <QtCore/qlist.h> - -QT_BEGIN_NAMESPACE - -QHash<QObject*, QQuickVisualItemModelAttached*> QQuickVisualItemModelAttached::attachedProperties; - - -class QQuickVisualItemModelPrivate : public QObjectPrivate -{ - Q_DECLARE_PUBLIC(QQuickVisualItemModel) -public: - class Item { - public: - Item(QQuickItem *i) : item(i), ref(0) {} - - void addRef() { ++ref; } - bool deref() { return --ref == 0; } - - QQuickItem *item; - int ref; - }; - - QQuickVisualItemModelPrivate() : QObjectPrivate() {} - - static void children_append(QQmlListProperty<QQuickItem> *prop, QQuickItem *item) { - static_cast<QQuickVisualItemModelPrivate *>(prop->data)->children.append(Item(item)); - static_cast<QQuickVisualItemModelPrivate *>(prop->data)->itemAppended(); - static_cast<QQuickVisualItemModelPrivate *>(prop->data)->emitChildrenChanged(); - } - - static int children_count(QQmlListProperty<QQuickItem> *prop) { - return static_cast<QQuickVisualItemModelPrivate *>(prop->data)->children.count(); - } - - static QQuickItem *children_at(QQmlListProperty<QQuickItem> *prop, int index) { - return static_cast<QQuickVisualItemModelPrivate *>(prop->data)->children.at(index).item; - } - - static void children_clear(QQmlListProperty<QQuickItem> *prop) { - static_cast<QQuickVisualItemModelPrivate *>(prop->data)->itemCleared(static_cast<QQuickVisualItemModelPrivate *>(prop->data)->children); - static_cast<QQuickVisualItemModelPrivate *>(prop->data)->children.clear(); - static_cast<QQuickVisualItemModelPrivate *>(prop->data)->emitChildrenChanged(); - } - - void itemAppended() { - Q_Q(QQuickVisualItemModel); - QQuickVisualItemModelAttached *attached = QQuickVisualItemModelAttached::properties(children.last().item); - attached->setIndex(children.count()-1); - QQuickChangeSet changeSet; - changeSet.insert(children.count() - 1, 1); - emit q->modelUpdated(changeSet, false); - emit q->countChanged(); - } - - void itemCleared(const QList<Item> &children) { - Q_Q(QQuickVisualItemModel); - foreach (const Item &child, children) - emit q->destroyingItem(child.item); - emit q->countChanged(); - } - - void emitChildrenChanged() { - Q_Q(QQuickVisualItemModel); - emit q->childrenChanged(); - } - - int indexOf(QQuickItem *item) const { - for (int i = 0; i < children.count(); ++i) - if (children.at(i).item == item) - return i; - return -1; - } - - - QList<Item> children; -}; - - -/*! - \qmltype VisualItemModel - \instantiates QQuickVisualItemModel - \inqmlmodule QtQuick 2 - \ingroup qtquick-models - \brief Defines items to be used added to a view - - A VisualItemModel contains the visual items to be used in a view. - When a VisualItemModel is used in a view, the view does not require - a delegate since the VisualItemModel already contains the visual - delegate (items). - - An item can determine its index within the - model via the \l{VisualItemModel::index}{index} attached property. - - The example below places three colored rectangles in a ListView. - \code - import QtQuick 2.0 - - Rectangle { - VisualItemModel { - id: itemModel - Rectangle { height: 30; width: 80; color: "red" } - Rectangle { height: 30; width: 80; color: "green" } - Rectangle { height: 30; width: 80; color: "blue" } - } - - ListView { - anchors.fill: parent - model: itemModel - } - } - \endcode - - \image visualitemmodel.png - - \sa {quick/modelviews/visualitemmodel}{VisualItemModel example} -*/ -QQuickVisualItemModel::QQuickVisualItemModel(QObject *parent) - : QQuickVisualModel(*(new QQuickVisualItemModelPrivate), parent) -{ -} - -/*! - \qmlattachedproperty int QtQuick2::VisualItemModel::index - This attached property holds the index of this delegate's item within the model. - - It is attached to each instance of the delegate. -*/ - -QQmlListProperty<QQuickItem> QQuickVisualItemModel::children() -{ - Q_D(QQuickVisualItemModel); - return QQmlListProperty<QQuickItem>(this, - d, - d->children_append, - d->children_count, - d->children_at, - d->children_clear); -} - -/*! - \qmlproperty int QtQuick2::VisualItemModel::count - - The number of items in the model. This property is readonly. -*/ -int QQuickVisualItemModel::count() const -{ - Q_D(const QQuickVisualItemModel); - return d->children.count(); -} - -bool QQuickVisualItemModel::isValid() const -{ - return true; -} - -QQuickItem *QQuickVisualItemModel::item(int index, bool) -{ - Q_D(QQuickVisualItemModel); - QQuickVisualItemModelPrivate::Item &item = d->children[index]; - item.addRef(); - if (item.ref == 1) { - emit initItem(index, item.item); - emit createdItem(index, item.item); - } - return item.item; -} - -QQuickVisualModel::ReleaseFlags QQuickVisualItemModel::release(QQuickItem *item) -{ - Q_D(QQuickVisualItemModel); - int idx = d->indexOf(item); - if (idx >= 0) { - if (d->children[idx].deref()) { - // XXX todo - the original did item->scene()->removeItem(). Why? - item->setParentItem(0); - } else { - return QQuickVisualModel::Referenced; - } - } - return 0; -} - -QString QQuickVisualItemModel::stringValue(int index, const QString &name) -{ - Q_D(QQuickVisualItemModel); - if (index < 0 || index >= d->children.count()) - return QString(); - return QQmlEngine::contextForObject(d->children.at(index).item)->contextProperty(name).toString(); -} - -int QQuickVisualItemModel::indexOf(QQuickItem *item, QObject *) const -{ - Q_D(const QQuickVisualItemModel); - return d->indexOf(item); -} - -QQuickVisualItemModelAttached *QQuickVisualItemModel::qmlAttachedProperties(QObject *obj) -{ - return QQuickVisualItemModelAttached::properties(obj); -} - -QT_END_NAMESPACE - diff --git a/src/quick/items/qquickvisualitemmodel_p.h b/src/quick/items/qquickvisualitemmodel_p.h deleted file mode 100644 index a5d50f9d38..0000000000 --- a/src/quick/items/qquickvisualitemmodel_p.h +++ /dev/null @@ -1,174 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal -** -** This file is part of the QtQml module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QQUICKVISUALITEMMODEL_P_H -#define QQUICKVISUALITEMMODEL_P_H - -#include <private/qtquickglobal_p.h> -#include <QtQml/qqml.h> -#include <QtCore/qobject.h> - -QT_BEGIN_HEADER - -QT_BEGIN_NAMESPACE - -class QQuickItem; -class QQuickChangeSet; - -class Q_QUICK_PRIVATE_EXPORT QQuickVisualModel : public QObject -{ - Q_OBJECT - - Q_PROPERTY(int count READ count NOTIFY countChanged) - -public: - virtual ~QQuickVisualModel() {} - - enum ReleaseFlag { Referenced = 0x01, Destroyed = 0x02 }; - Q_DECLARE_FLAGS(ReleaseFlags, ReleaseFlag) - - virtual int count() const = 0; - virtual bool isValid() const = 0; - virtual QQuickItem *item(int index, bool asynchronous=false) = 0; - virtual ReleaseFlags release(QQuickItem *item) = 0; - virtual void cancel(int) {} - virtual QString stringValue(int, const QString &) = 0; - virtual void setWatchedRoles(QList<QByteArray> roles) = 0; - - virtual int indexOf(QQuickItem *item, QObject *objectContext) const = 0; - -Q_SIGNALS: - void countChanged(); - void modelUpdated(const QQuickChangeSet &changeSet, bool reset); - void createdItem(int index, QQuickItem *item); - void initItem(int index, QQuickItem *item); - void destroyingItem(QQuickItem *item); - -protected: - QQuickVisualModel(QObjectPrivate &dd, QObject *parent = 0) - : QObject(dd, parent) {} - -private: - Q_DISABLE_COPY(QQuickVisualModel) -}; - -class QQuickVisualItemModelAttached; -class QQuickVisualItemModelPrivate; -class Q_QUICK_PRIVATE_EXPORT QQuickVisualItemModel : public QQuickVisualModel -{ - Q_OBJECT - Q_DECLARE_PRIVATE(QQuickVisualItemModel) - - Q_PROPERTY(QQmlListProperty<QQuickItem> children READ children NOTIFY childrenChanged DESIGNABLE false) - Q_CLASSINFO("DefaultProperty", "children") - -public: - QQuickVisualItemModel(QObject *parent=0); - virtual ~QQuickVisualItemModel() {} - - virtual int count() const; - virtual bool isValid() const; - virtual QQuickItem *item(int index, bool asynchronous=false); - virtual ReleaseFlags release(QQuickItem *item); - virtual QString stringValue(int index, const QString &role); - virtual void setWatchedRoles(QList<QByteArray>) {} - - virtual int indexOf(QQuickItem *item, QObject *objectContext) const; - - QQmlListProperty<QQuickItem> children(); - - static QQuickVisualItemModelAttached *qmlAttachedProperties(QObject *obj); - -Q_SIGNALS: - void childrenChanged(); - -private: - Q_DISABLE_COPY(QQuickVisualItemModel) -}; - -class QQuickVisualItemModelAttached : public QObject -{ - Q_OBJECT - -public: - QQuickVisualItemModelAttached(QObject *parent) - : QObject(parent), m_index(0) {} - ~QQuickVisualItemModelAttached() { - attachedProperties.remove(parent()); - } - - Q_PROPERTY(int index READ index NOTIFY indexChanged) - int index() const { return m_index; } - void setIndex(int idx) { - if (m_index != idx) { - m_index = idx; - emit indexChanged(); - } - } - - static QQuickVisualItemModelAttached *properties(QObject *obj) { - QQuickVisualItemModelAttached *rv = attachedProperties.value(obj); - if (!rv) { - rv = new QQuickVisualItemModelAttached(obj); - attachedProperties.insert(obj, rv); - } - return rv; - } - -Q_SIGNALS: - void indexChanged(); - -public: - int m_index; - - static QHash<QObject*, QQuickVisualItemModelAttached*> attachedProperties; -}; - - -QT_END_NAMESPACE - -QML_DECLARE_TYPE(QQuickVisualModel) -QML_DECLARE_TYPE(QQuickVisualItemModel) -QML_DECLARE_TYPEINFO(QQuickVisualItemModel, QML_HAS_ATTACHED_PROPERTIES) - -QT_END_HEADER - -#endif // QQUICKVISUALITEMMODEL_P_H diff --git a/src/quick/items/qquickwindow.cpp b/src/quick/items/qquickwindow.cpp index 469ea4225c..9acd24cae3 100644 --- a/src/quick/items/qquickwindow.cpp +++ b/src/quick/items/qquickwindow.cpp @@ -50,7 +50,7 @@ #include <QtQuick/private/qsgtexture_p.h> #include <QtQuick/private/qsgflashnode_p.h> -#include <private/qquickwindowmanager_p.h> +#include <private/qsgrenderloop_p.h> #include <private/qguiapplication_p.h> #include <QtGui/QInputMethod> @@ -366,8 +366,8 @@ QQuickWindowPrivate::QQuickWindowPrivate() , windowManager(0) , clearColor(Qt::white) , clearBeforeRendering(true) - , persistentGLContext(false) - , persistentSceneGraph(false) + , persistentGLContext(true) + , persistentSceneGraph(true) , lastWheelEventAccepted(false) , renderTarget(0) , renderTargetId(0) @@ -392,13 +392,7 @@ void QQuickWindowPrivate::init(QQuickWindow *c) contentItemPrivate->windowRefCount = 1; contentItemPrivate->flags |= QQuickItem::ItemIsFocusScope; - // In the absence of a focus in event on some platforms assume the window will - // be activated immediately and set focus on the contentItem - // ### Remove when QTBUG-22415 is resolved. - //It is important that this call happens after the contentItem has a window.. - contentItem->setFocus(true); - - windowManager = QQuickWindowManager::instance(); + windowManager = QSGRenderLoop::instance(); context = windowManager->sceneGraphContext(); q->setSurfaceType(QWindow::OpenGLSurface); q->setFormat(context->defaultSurfaceFormat()); @@ -676,10 +670,10 @@ void QQuickWindowPrivate::setFocusInScope(QQuickItem *scope, QQuickItem *item, F } if (!(options & DontChangeFocusProperty)) { -// if (item != contentItem || QGuiApplication::focusWindow() == q) { // QTBUG-22415 + if (item != contentItem || QGuiApplication::focusWindow() == q) { itemPrivate->focus = true; changed << item; -// } + } } if (newActiveFocusItem && contentItem->hasFocus()) { @@ -929,6 +923,8 @@ QQuickWindow::QQuickWindow(QWindow *parent) d->init(this); } + + /*! \internal */ @@ -972,20 +968,32 @@ QQuickWindow::~QQuickWindow() void QQuickWindow::releaseResources() { Q_D(QQuickWindow); - d->windowManager->releaseResources(); + d->windowManager->releaseResources(this); QQuickPixmap::purgeCache(); } /*! - Sets whether the OpenGL context can be released as a part of a call to - releaseResources() to \a persistent. + Sets whether the OpenGL context can be released to \a + persistent. The default value is true. - The OpenGL context might still be released when the user makes an explicit - call to hide(). + The OpenGL context can be released to free up graphics resources + when the window is obscured, hidden or not rendering. When this + happens is implementation specific. - \sa setPersistentSceneGraph() + The QOpenGLContext::aboutToBeDestroyed() signal is emitted from + the QQuickWindow::openglContext() when the OpenGL context is about + to be released. The QQuickWindow::sceneGraphInitialized() signal + is emitted when a new OpenGL context is created for this + window. Make a Qt::DirectConnection to these signals to be + notified. + + The OpenGL context is still released when the last QQuickWindow is + deleted. + + \sa setPersistentSceneGraph(), + QOpenGLContext::aboutToBeDestroyed(), sceneGraphInitialized() */ void QQuickWindow::setPersistentOpenGLContext(bool persistent) @@ -995,9 +1003,13 @@ void QQuickWindow::setPersistentOpenGLContext(bool persistent) } + /*! - Returns whether the OpenGL context can be released as a part of a call to - releaseResources(). + Returns whether the OpenGL context can be released during the + lifetime of the QQuickWindow. + + \note This is a hint. When and how this happens is implementation + specific. */ bool QQuickWindow::isPersistentOpenGLContext() const @@ -1009,13 +1021,24 @@ bool QQuickWindow::isPersistentOpenGLContext() const /*! - Sets whether the scene graph nodes and resources can be released as a - part of a call to releaseResources() to \a persistent. + Sets whether the scene graph nodes and resources can be released + to \a persistent. The default value is true. - The scene graph nodes and resources might still be released when the user - makes an explicit call to hide(). + The scene graph nodes and resources can be released to free up + graphics resources when the window is obscured, hidden or not + rendering. When this happens is implementation specific. - \sa setPersistentOpenGLContext() + The QQuickWindow::sceneGraphInvalidated() signal is emitted when + cleanup occurs. The QQuickWindow::sceneGraphInitialized() signal + is emitted when a new scene graph is recreated for this + window. Make a Qt::DirectConnection to these signals to be + notified. + + The scene graph nodes and resources are still released when the + last QQuickWindow is deleted. + + \sa setPersistentOpenGLContext(), + sceneGraphInvalidated(), sceneGraphInitialized() */ void QQuickWindow::setPersistentSceneGraph(bool persistent) @@ -1027,8 +1050,11 @@ void QQuickWindow::setPersistentSceneGraph(bool persistent) /*! - Returns whether the scene graph nodes and resources can be released as a part - of a call to releaseResources(). + Returns whether the scene graph nodes and resources can be + released during the lifetime of this QQuickWindow. + + \note This is a hint. When and how this happens is implementation + specific. */ bool QQuickWindow::isPersistentSceneGraph() const @@ -1161,6 +1187,12 @@ void QQuickWindow::keyPressEvent(QKeyEvent *e) { Q_D(QQuickWindow); +#ifndef QT_NO_SHORTCUT + // Try looking for a Shortcut before sending key events + if (QGuiApplicationPrivate::instance()->shortcutMap.tryShortcutEvent(this, e)) + return; +#endif + if (d->activeFocusItem) sendEvent(d->activeFocusItem, e); } @@ -2783,6 +2815,14 @@ void QQuickWindow::setColor(const QColor &color) if (color == d->clearColor) return; + if (color.alpha() != d->clearColor.alpha()) { + QSurfaceFormat fmt = format(); + if (color.alpha() < 255) + fmt.setAlphaBufferSize(8); + else + fmt.setAlphaBufferSize(-1); + setFormat(fmt); + } d->clearColor = color; emit colorChanged(color); d->dirtyItem(contentItem()); diff --git a/src/quick/items/qquickwindow.h b/src/quick/items/qquickwindow.h index a98f3a695f..959c52b37c 100644 --- a/src/quick/items/qquickwindow.h +++ b/src/quick/items/qquickwindow.h @@ -48,8 +48,6 @@ #include <QtGui/qwindow.h> #include <QtGui/qevent.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE class QQuickItem; @@ -173,7 +171,5 @@ QT_END_NAMESPACE Q_DECLARE_METATYPE(QQuickWindow *) -QT_END_HEADER - #endif // QQUICKWINDOW_H diff --git a/src/quick/items/qquickwindow_p.h b/src/quick/items/qquickwindow_p.h index da2ae8284d..f272f30f8e 100644 --- a/src/quick/items/qquickwindow_p.h +++ b/src/quick/items/qquickwindow_p.h @@ -73,7 +73,7 @@ QT_BEGIN_NAMESPACE //Make it easy to identify and customize the root item if needed -class QQuickWindowManager; +class QSGRenderLoop; class QQuickRootItem : public QQuickItem { @@ -200,7 +200,7 @@ public: QSGContext *context; QSGRenderer *renderer; - QQuickWindowManager *windowManager; + QSGRenderLoop *windowManager; QColor clearColor; diff --git a/src/quick/items/qquickwindowmanager.cpp b/src/quick/items/qquickwindowmanager.cpp deleted file mode 100644 index 69aa63c278..0000000000 --- a/src/quick/items/qquickwindowmanager.cpp +++ /dev/null @@ -1,363 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal -** -** This file is part of the QtQml module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "qquickwindowmanager_p.h" -#include "qquickthreadedwindowmanager_p.h" - -#include <QtCore/QCoreApplication> -#include <QtCore/QTime> -#include <QtCore/private/qabstractanimation_p.h> - -#include <QtGui/QOpenGLContext> -#include <QtGui/private/qguiapplication_p.h> -#include <qpa/qplatformintegration.h> - -#include <QtQml/private/qqmlglobal_p.h> - -#include <QtQuick/QQuickWindow> -#include <QtQuick/private/qquickwindow_p.h> -#include <QtQuick/private/qsgcontext_p.h> - -QT_BEGIN_NAMESPACE - -DEFINE_BOOL_CONFIG_OPTION(qquick_render_timing, QML_RENDER_TIMING) - -extern Q_GUI_EXPORT QImage qt_gl_read_framebuffer(const QSize &size, bool alpha_format, bool include_alpha); - -/*! - expectations for this manager to work: - - one opengl context to render multiple windows - - OpenGL pipeline will not block for vsync in swap - - OpenGL pipeline will block based on a full buffer queue. - - Multiple screens can share the OpenGL context - - Animations are advanced for all windows once per swap - */ - -DEFINE_BOOL_CONFIG_OPTION(qmlNoThreadedRenderer, QML_BAD_GUI_RENDER_LOOP); -DEFINE_BOOL_CONFIG_OPTION(qmlForceThreadedRenderer, QML_FORCE_THREADED_RENDERER); // Might trigger graphics driver threading bugs, use at own risk - -QQuickWindowManager *QQuickWindowManager::s_instance = 0; - -QQuickWindowManager::~QQuickWindowManager() -{ -} - -class QQuickTrivialWindowManager : public QObject, public QQuickWindowManager -{ - Q_OBJECT -public: - QQuickTrivialWindowManager(); - - void show(QQuickWindow *window); - void hide(QQuickWindow *window); - - void windowDestroyed(QQuickWindow *window); - - void initializeGL(); - void renderWindow(QQuickWindow *window); - void exposureChanged(QQuickWindow *window); - QImage grab(QQuickWindow *window); - void resize(QQuickWindow *window, const QSize &size); - - void maybeUpdate(QQuickWindow *window); - void update(QQuickWindow *window) { maybeUpdate(window); } // identical for this implementation. - - void releaseResources() { } - - QAnimationDriver *animationDriver() const { return 0; } - - QSGContext *sceneGraphContext() const; - - bool event(QEvent *); - - struct WindowData { - bool updatePending : 1; - bool grabOnly : 1; - }; - - QHash<QQuickWindow *, WindowData> m_windows; - - QOpenGLContext *gl; - QSGContext *sg; - - QImage grabContent; - - bool eventPending; -}; - - -QQuickWindowManager *QQuickWindowManager::instance() -{ - if (!s_instance) { - - s_instance = QSGContext::createWindowManager(); - - bool bufferQueuing = QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::BufferQueueingOpenGL); -#ifdef Q_OS_WIN - bool fancy = false; // QTBUG-28037 -#else - bool fancy = QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::ThreadedOpenGL); -#endif - if (qmlNoThreadedRenderer()) - fancy = false; - else if (qmlForceThreadedRenderer()) - fancy = true; - - // Enable fixed animation steps... - QByteArray fixed = qgetenv("QML_FIXED_ANIMATION_STEP"); - bool fixedAnimationSteps = bufferQueuing; - if (fixed == "no") - fixedAnimationSteps = false; - else if (fixed.length()) - fixedAnimationSteps = true; - if (fixedAnimationSteps) - QUnifiedTimer::instance(true)->setConsistentTiming(true); - - if (!s_instance) { - s_instance = fancy - ? (QQuickWindowManager*) new QQuickRenderThreadSingleContextWindowManager - : (QQuickWindowManager*) new QQuickTrivialWindowManager; - } - } - return s_instance; -} - -void QQuickWindowManager::setInstance(QQuickWindowManager *instance) -{ - Q_ASSERT(!s_instance); - s_instance = instance; -} - -QQuickTrivialWindowManager::QQuickTrivialWindowManager() - : gl(0) - , eventPending(false) -{ - sg = QSGContext::createDefaultContext(); -} - - -void QQuickTrivialWindowManager::show(QQuickWindow *window) -{ - WindowData data; - data.updatePending = false; - data.grabOnly = false; - m_windows[window] = data; - - maybeUpdate(window); -} - -void QQuickTrivialWindowManager::hide(QQuickWindow *window) -{ - if (!m_windows.contains(window)) - return; - - m_windows.remove(window); - QQuickWindowPrivate *cd = QQuickWindowPrivate::get(window); - cd->cleanupNodesOnShutdown(); - - if (m_windows.size() == 0) { - sg->invalidate(); - delete gl; - gl = 0; - } -} - -void QQuickTrivialWindowManager::windowDestroyed(QQuickWindow *window) -{ - hide(window); -} - -void QQuickTrivialWindowManager::renderWindow(QQuickWindow *window) -{ - bool renderWithoutShowing = QQuickWindowPrivate::get(window)->renderWithoutShowing; - if ((!window->isExposed() && !renderWithoutShowing) || !m_windows.contains(window)) - return; - - WindowData &data = const_cast<WindowData &>(m_windows[window]); - - QQuickWindow *masterWindow = 0; - if (!window->isVisible() && !renderWithoutShowing) { - // Find a "proper surface" to bind... - for (QHash<QQuickWindow *, WindowData>::const_iterator it = m_windows.constBegin(); - it != m_windows.constEnd() && !masterWindow; ++it) { - if (it.key()->isVisible()) - masterWindow = it.key(); - } - } else { - masterWindow = window; - } - - if (!masterWindow) - return; - - if (!QQuickWindowPrivate::get(masterWindow)->isRenderable()) { - qWarning().nospace() - << "Unable to find a renderable master window " - << masterWindow << "when trying to render" - << window << " (" << window->geometry() << ")."; - return; - } - - if (!gl) { - gl = new QOpenGLContext(); - gl->setFormat(masterWindow->requestedFormat()); - gl->create(); - if (!gl->makeCurrent(masterWindow)) - qWarning("QQuickWindow: makeCurrent() failed..."); - sg->initialize(gl); - } else { - gl->makeCurrent(masterWindow); - } - - bool alsoSwap = data.updatePending; - data.updatePending = false; - - QQuickWindowPrivate *cd = QQuickWindowPrivate::get(window); - cd->polishItems(); - - int renderTime = 0, syncTime = 0; - QTime renderTimer; - if (qquick_render_timing()) - renderTimer.start(); - - cd->syncSceneGraph(); - - if (qquick_render_timing()) - syncTime = renderTimer.elapsed(); - - cd->renderSceneGraph(window->size()); - - if (qquick_render_timing()) - renderTime = renderTimer.elapsed() - syncTime; - - if (data.grabOnly) { - grabContent = qt_gl_read_framebuffer(window->size(), false, false); - data.grabOnly = false; - } - - if (alsoSwap && window->isVisible()) { - gl->swapBuffers(window); - cd->fireFrameSwapped(); - } - - if (qquick_render_timing()) { - static QTime lastFrameTime = QTime::currentTime(); - const int swapTime = renderTimer.elapsed() - renderTime - syncTime; - qDebug() << "- Breakdown of frame time; sync:" << syncTime - << "ms render:" << renderTime << "ms swap:" << swapTime - << "ms total:" << swapTime + renderTime + syncTime - << "ms time since last frame:" << (lastFrameTime.msecsTo(QTime::currentTime())) - << "ms"; - lastFrameTime = QTime::currentTime(); - } - - // Might have been set during syncSceneGraph() - if (data.updatePending) - maybeUpdate(window); -} - -void QQuickTrivialWindowManager::exposureChanged(QQuickWindow *window) -{ - if (window->isExposed()) - maybeUpdate(window); -} - -QImage QQuickTrivialWindowManager::grab(QQuickWindow *window) -{ - if (!m_windows.contains(window)) - return QImage(); - - m_windows[window].grabOnly = true; - - renderWindow(window); - - QImage grabbed = grabContent; - grabContent = QImage(); - return grabbed; -} - - - -void QQuickTrivialWindowManager::resize(QQuickWindow *, const QSize &) -{ -} - - - -void QQuickTrivialWindowManager::maybeUpdate(QQuickWindow *window) -{ - if (!m_windows.contains(window)) - return; - - m_windows[window].updatePending = true; - - if (!eventPending) { - QCoreApplication::postEvent(this, new QEvent(QEvent::User)); - eventPending = true; - } -} - - - -QSGContext *QQuickTrivialWindowManager::sceneGraphContext() const -{ - return sg; -} - - -bool QQuickTrivialWindowManager::event(QEvent *e) -{ - if (e->type() == QEvent::User) { - eventPending = false; - for (QHash<QQuickWindow *, WindowData>::const_iterator it = m_windows.constBegin(); - it != m_windows.constEnd(); ++it) { - const WindowData &data = it.value(); - if (data.updatePending) - renderWindow(it.key()); - } - return true; - } - return QObject::event(e); -} - -#include "qquickwindowmanager.moc" - -QT_END_NAMESPACE diff --git a/src/quick/items/qquickwindowmanager_p.h b/src/quick/items/qquickwindowmanager_p.h deleted file mode 100644 index 94142a9d90..0000000000 --- a/src/quick/items/qquickwindowmanager_p.h +++ /dev/null @@ -1,87 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal -** -** This file is part of the QtQml module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QQUICKWINDOWMANAGER_P_H -#define QQUICKWINDOWMANAGER_P_H - -#include <QtGui/QImage> -#include <private/qtquickglobal_p.h> - -QT_BEGIN_NAMESPACE - -class QQuickWindow; -class QSGContext; -class QAnimationDriver; - -class Q_QUICK_PRIVATE_EXPORT QQuickWindowManager -{ -public: - virtual ~QQuickWindowManager(); - - virtual void show(QQuickWindow *window) = 0; - virtual void hide(QQuickWindow *window) = 0; - - virtual void windowDestroyed(QQuickWindow *window) = 0; - - virtual void exposureChanged(QQuickWindow *window) = 0; - virtual QImage grab(QQuickWindow *window) = 0; - virtual void resize(QQuickWindow *window, const QSize &size) = 0; - - virtual void update(QQuickWindow *window) = 0; - virtual void maybeUpdate(QQuickWindow *window) = 0; - - virtual QAnimationDriver *animationDriver() const = 0; - - virtual QSGContext *sceneGraphContext() const = 0; - - virtual void releaseResources() = 0; - - // ### make this less of a singleton - static QQuickWindowManager *instance(); - static void setInstance(QQuickWindowManager *instance); - -private: - static QQuickWindowManager *s_instance; -}; - -QT_END_NAMESPACE - -#endif // QQUICKWINDOWMANAGER_P_H diff --git a/src/quick/items/qquickwindowmodule_p.h b/src/quick/items/qquickwindowmodule_p.h index 10f0c630b9..4afc45751f 100644 --- a/src/quick/items/qquickwindowmodule_p.h +++ b/src/quick/items/qquickwindowmodule_p.h @@ -44,8 +44,6 @@ #include <private/qtquickglobal_p.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE @@ -57,6 +55,4 @@ public: QT_END_NAMESPACE -QT_END_HEADER - #endif |