diff options
author | Martin Jones <martin.jones@nokia.com> | 2009-08-03 05:41:34 (GMT) |
---|---|---|
committer | Martin Jones <martin.jones@nokia.com> | 2009-08-03 05:41:34 (GMT) |
commit | 3c0ea527433dde04ee69c94a3f95f2d2d6b6a02d (patch) | |
tree | 5622cfc3f966892d3192e7a8433f257cf977a7fd | |
parent | 651b7aa72052faa90b3a268f00f82d56460166d3 (diff) | |
download | Qt-3c0ea527433dde04ee69c94a3f95f2d2d6b6a02d.zip Qt-3c0ea527433dde04ee69c94a3f95f2d2d6b6a02d.tar.gz Qt-3c0ea527433dde04ee69c94a3f95f2d2d6b6a02d.tar.bz2 |
Rework VisualItemModel into VisualItemModel & VisualDataModel
QFXVisualModel provides a base class for visual models.
QFxVisualDataModel provides a visual model for Qt item view models.
QFxVisualItemModel provides a model of QFxItems.
-rw-r--r-- | examples/declarative/listview/itemlist.qml | 57 | ||||
-rw-r--r-- | src/declarative/extra/qfxflowview.cpp | 4 | ||||
-rw-r--r-- | src/declarative/extra/qfxflowview.h | 10 | ||||
-rw-r--r-- | src/declarative/fx/qfxgridview.cpp | 30 | ||||
-rw-r--r-- | src/declarative/fx/qfxgridview.h | 2 | ||||
-rw-r--r-- | src/declarative/fx/qfxlistview.cpp | 30 | ||||
-rw-r--r-- | src/declarative/fx/qfxlistview.h | 2 | ||||
-rw-r--r-- | src/declarative/fx/qfxpathview.cpp | 26 | ||||
-rw-r--r-- | src/declarative/fx/qfxpathview_p.h | 4 | ||||
-rw-r--r-- | src/declarative/fx/qfxvisualitemmodel.cpp | 345 | ||||
-rw-r--r-- | src/declarative/fx/qfxvisualitemmodel.h | 94 |
11 files changed, 434 insertions, 170 deletions
diff --git a/examples/declarative/listview/itemlist.qml b/examples/declarative/listview/itemlist.qml new file mode 100644 index 0000000..046321b --- /dev/null +++ b/examples/declarative/listview/itemlist.qml @@ -0,0 +1,57 @@ +// This example demonstrates placing items in a view using +// a VisualItemModel + +import Qt 4.6 + +Rect { + color: "lightgray" + width: 240 + height: 320 + + VisualItemModel { + id: ItemModel + Rect { + height: View.height; width: View.width; color: "#FFFEF0" + Text { text: "Page 1"; font.bold: true; anchors.centerIn: parent } + } + Rect { + height: View.height; width: View.width; color: "#F0FFF7" + Text { text: "Page 2"; font.bold: true; anchors.centerIn: parent } + } + Rect { + height: View.height; width: View.width; color: "#F4F0FF" + Text { text: "Page 3"; font.bold: true; anchors.centerIn: parent } + } + } + + ListView { + id: View + anchors.fill: parent + anchors.bottomMargin: 30 + model: ItemModel + currentItemPositioning: "SnapAuto" + orientation: "Horizontal" + } + + Rect { + color: "gray" + anchors.top: View.bottom + anchors.bottom: parent.bottom + height: 30 + width: 240 + + HorizontalLayout { + anchors.centerIn: parent + spacing: 20 + Repeater { + dataSource: ItemModel.count + Rect { + width: 5; height: 5 + radius: 3 + MouseRegion { width: 20; height: 20; anchors.centerIn: parent; onClicked: View.currentIndex = index } + color: View.currentIndex == index ? "blue" : "white" + } + } + } + } +} diff --git a/src/declarative/extra/qfxflowview.cpp b/src/declarative/extra/qfxflowview.cpp index e9b836d..4af8af9 100644 --- a/src/declarative/extra/qfxflowview.cpp +++ b/src/declarative/extra/qfxflowview.cpp @@ -73,12 +73,12 @@ QFxFlowView::QFxFlowView() setAcceptedMouseButtons(Qt::LeftButton); } -QFxVisualItemModel *QFxFlowView::model() const +QFxVisualModel *QFxFlowView::model() const { return m_model; } -void QFxFlowView::setModel(QFxVisualItemModel *m) +void QFxFlowView::setModel(QFxVisualModel *m) { m_model = m; refresh(); diff --git a/src/declarative/extra/qfxflowview.h b/src/declarative/extra/qfxflowview.h index c2b30a3..2d7a8bd 100644 --- a/src/declarative/extra/qfxflowview.h +++ b/src/declarative/extra/qfxflowview.h @@ -51,21 +51,21 @@ QT_BEGIN_NAMESPACE QT_MODULE(Declarative) -class QFxVisualItemModel; +class QFxVisualModel; class QFxFlowViewValue; class QFxFlowViewAttached; class Q_DECLARATIVE_EXPORT QFxFlowView : public QFxItem { Q_OBJECT - Q_PROPERTY(QFxVisualItemModel *model READ model WRITE setModel) + Q_PROPERTY(QFxVisualModel *model READ model WRITE setModel) Q_PROPERTY(int column READ columns WRITE setColumns) Q_PROPERTY(bool vertical READ vertical WRITE setVertical) public: QFxFlowView(); - QFxVisualItemModel *model() const; - void setModel(QFxVisualItemModel *); + QFxVisualModel *model() const; + void setModel(QFxVisualModel *); int columns() const; void setColumns(int); @@ -86,7 +86,7 @@ private: void reflowDrag(const QPointF &); void clearTimeLine(); int m_columns; - QFxVisualItemModel *m_model; + QFxVisualModel *m_model; QList<QFxItem *> m_items; bool m_vertical; void moveItem(QFxItem *item, const QPointF &); diff --git a/src/declarative/fx/qfxgridview.cpp b/src/declarative/fx/qfxgridview.cpp index eb85c20..f471ef2 100644 --- a/src/declarative/fx/qfxgridview.cpp +++ b/src/declarative/fx/qfxgridview.cpp @@ -213,7 +213,7 @@ public: } bool isValid() const { - return model && model->count() && (!ownModel || model->delegate()); + return model && model->count() && model->isValid(); } int rowSize() const { @@ -293,7 +293,7 @@ public: } } - QFxVisualItemModel *model; + QFxVisualModel *model; QVariant modelVariant; QList<FxGridItem*> visibleItems; QHash<QFxItem*,int> unrequestedItems; @@ -730,8 +730,8 @@ void QFxGridView::setModel(const QVariant &model) d->clear(); d->modelVariant = model; QObject *object = qvariant_cast<QObject*>(model); - QFxVisualItemModel *vim = 0; - if (object && (vim = qobject_cast<QFxVisualItemModel *>(object))) { + QFxVisualModel *vim = 0; + if (object && (vim = qobject_cast<QFxVisualModel *>(object))) { if (d->ownModel) { delete d->model; d->ownModel = false; @@ -739,10 +739,11 @@ void QFxGridView::setModel(const QVariant &model) d->model = vim; } else { if (!d->ownModel) { - d->model = new QFxVisualItemModel(qmlContext(this)); + d->model = new QFxVisualDataModel(qmlContext(this)); d->ownModel = true; } - d->model->setModel(model); + if (QFxVisualDataModel *dataModel = qobject_cast<QFxVisualDataModel*>(d->model)) + dataModel->setModel(model); } if (d->model) { if (d->currentIndex >= d->model->count() || d->currentIndex < 0) @@ -769,19 +770,26 @@ void QFxGridView::setModel(const QVariant &model) QmlComponent *QFxGridView::delegate() const { Q_D(const QFxGridView); - return d->model ? d->model->delegate() : 0; + if (d->model) { + if (QFxVisualDataModel *dataModel = qobject_cast<QFxVisualDataModel*>(d->model)) + return dataModel->delegate(); + } + + return 0; } void QFxGridView::setDelegate(QmlComponent *delegate) { Q_D(QFxGridView); if (!d->ownModel) { - d->model = new QFxVisualItemModel(qmlContext(this)); + d->model = new QFxVisualDataModel(qmlContext(this)); d->ownModel = true; } - d->model->setDelegate(delegate); - d->updateCurrent(d->currentIndex); - refill(); + if (QFxVisualDataModel *dataModel = qobject_cast<QFxVisualDataModel*>(d->model)) { + dataModel->setDelegate(delegate); + d->updateCurrent(d->currentIndex); + refill(); + } } /*! diff --git a/src/declarative/fx/qfxgridview.h b/src/declarative/fx/qfxgridview.h index 2541884..63f93ca 100644 --- a/src/declarative/fx/qfxgridview.h +++ b/src/declarative/fx/qfxgridview.h @@ -49,7 +49,7 @@ QT_BEGIN_HEADER QT_BEGIN_NAMESPACE QT_MODULE(Declarative) -class QFxVisualItemModel; +class QFxVisualModel; class QFxGridViewAttached; class QFxGridViewPrivate; class Q_DECLARATIVE_EXPORT QFxGridView : public QFxFlickable diff --git a/src/declarative/fx/qfxlistview.cpp b/src/declarative/fx/qfxlistview.cpp index 1e75026..860e9e4 100644 --- a/src/declarative/fx/qfxlistview.cpp +++ b/src/declarative/fx/qfxlistview.cpp @@ -265,7 +265,7 @@ public: } bool isValid() const { - return model && model->count() && (!ownModel || model->delegate()); + return model && model->count() && model->isValid(); } int snapIndex() { @@ -357,7 +357,7 @@ public: virtual void fixupY(); virtual void fixupX(); - QFxVisualItemModel *model; + QFxVisualModel *model; QVariant modelVariant; QList<FxListItem*> visibleItems; QHash<QFxItem*,int> unrequestedItems; @@ -910,8 +910,8 @@ void QFxListView::setModel(const QVariant &model) d->clear(); d->modelVariant = model; QObject *object = qvariant_cast<QObject*>(model); - QFxVisualItemModel *vim = 0; - if (object && (vim = qobject_cast<QFxVisualItemModel *>(object))) { + QFxVisualModel *vim = 0; + if (object && (vim = qobject_cast<QFxVisualModel *>(object))) { if (d->ownModel) { delete d->model; d->ownModel = false; @@ -919,10 +919,11 @@ void QFxListView::setModel(const QVariant &model) d->model = vim; } else { if (!d->ownModel) { - d->model = new QFxVisualItemModel(qmlContext(this)); + d->model = new QFxVisualDataModel(qmlContext(this)); d->ownModel = true; } - d->model->setModel(model); + if (QFxVisualDataModel *dataModel = qobject_cast<QFxVisualDataModel*>(d->model)) + dataModel->setModel(model); } if (d->model) { if (d->currentIndex >= d->model->count() || d->currentIndex < 0) @@ -949,19 +950,26 @@ void QFxListView::setModel(const QVariant &model) QmlComponent *QFxListView::delegate() const { Q_D(const QFxListView); - return d->model ? d->model->delegate() : 0; + if (d->model) { + if (QFxVisualDataModel *dataModel = qobject_cast<QFxVisualDataModel*>(d->model)) + return dataModel->delegate(); + } + + return 0; } void QFxListView::setDelegate(QmlComponent *delegate) { Q_D(QFxListView); if (!d->ownModel) { - d->model = new QFxVisualItemModel(qmlContext(this)); + d->model = new QFxVisualDataModel(qmlContext(this)); d->ownModel = true; } - d->model->setDelegate(delegate); - d->updateCurrent(d->currentIndex); - refill(); + if (QFxVisualDataModel *dataModel = qobject_cast<QFxVisualDataModel*>(d->model)) { + dataModel->setDelegate(delegate); + d->updateCurrent(d->currentIndex); + refill(); + } } /*! diff --git a/src/declarative/fx/qfxlistview.h b/src/declarative/fx/qfxlistview.h index 0a0737f..e6898f6 100644 --- a/src/declarative/fx/qfxlistview.h +++ b/src/declarative/fx/qfxlistview.h @@ -50,7 +50,7 @@ QT_BEGIN_NAMESPACE QT_MODULE(Declarative) -class QFxVisualItemModel; +class QFxVisualModel; class QFxListViewAttached; class QFxListViewPrivate; class Q_DECLARATIVE_EXPORT QFxListView : public QFxFlickable diff --git a/src/declarative/fx/qfxpathview.cpp b/src/declarative/fx/qfxpathview.cpp index 17f6dd3..6dcfcd1 100644 --- a/src/declarative/fx/qfxpathview.cpp +++ b/src/declarative/fx/qfxpathview.cpp @@ -144,7 +144,7 @@ QFxPathView::~QFxPathView() \brief the model providing data for the view. The model must be either a \l QListModelInterface or - \l QFxVisualItemModel subclass. + \l QFxVisualModel subclass. */ QVariant QFxPathView::model() const { @@ -168,8 +168,8 @@ void QFxPathView::setModel(const QVariant &model) d->modelVariant = model; QObject *object = qvariant_cast<QObject*>(model); - QFxVisualItemModel *vim = 0; - if (object && (vim = qobject_cast<QFxVisualItemModel *>(object))) { + QFxVisualModel *vim = 0; + if (object && (vim = qobject_cast<QFxVisualModel *>(object))) { if (d->ownModel) { delete d->model; d->ownModel = false; @@ -177,10 +177,11 @@ void QFxPathView::setModel(const QVariant &model) d->model = vim; } else { if (!d->ownModel) { - d->model = new QFxVisualItemModel(qmlContext(this)); + d->model = new QFxVisualDataModel(qmlContext(this)); d->ownModel = true; } - d->model->setModel(model); + if (QFxVisualDataModel *dataModel = qobject_cast<QFxVisualDataModel*>(d->model)) + dataModel->setModel(model); } if (d->model) { connect(d->model, SIGNAL(itemsInserted(int,int)), this, SLOT(itemsInserted(int,int))); @@ -341,18 +342,25 @@ void QFxPathView::setDragMargin(qreal dragMargin) QmlComponent *QFxPathView::delegate() const { Q_D(const QFxPathView); - return d->model ? d->model->delegate() : 0; + if (d->model) { + if (QFxVisualDataModel *dataModel = qobject_cast<QFxVisualDataModel*>(d->model)) + return dataModel->delegate(); + } + + return 0; } void QFxPathView::setDelegate(QmlComponent *c) { Q_D(QFxPathView); if (!d->ownModel) { - d->model = new QFxVisualItemModel(qmlContext(this)); + d->model = new QFxVisualDataModel(qmlContext(this)); d->ownModel = true; } - d->model->setDelegate(c); - d->regenerate(); + if (QFxVisualDataModel *dataModel = qobject_cast<QFxVisualDataModel*>(d->model)) { + dataModel->setDelegate(c); + d->regenerate(); + } } /*! diff --git a/src/declarative/fx/qfxpathview_p.h b/src/declarative/fx/qfxpathview_p.h index eb5a1b1..801bdb8 100644 --- a/src/declarative/fx/qfxpathview_p.h +++ b/src/declarative/fx/qfxpathview_p.h @@ -106,7 +106,7 @@ public: } bool isValid() const { - return model && model->count() > 0 && model->delegate() && path; + return model && model->count() > 0 && model->isValid() && path; } int calcCurrentIndex(); @@ -140,7 +140,7 @@ public: int pathOffset; int requestedIndex; QList<QFxItem *> items; - QFxVisualItemModel *model; + QFxVisualModel *model; QVariant modelVariant; enum MovementReason { Other, Key, Mouse }; MovementReason moveReason; diff --git a/src/declarative/fx/qfxvisualitemmodel.cpp b/src/declarative/fx/qfxvisualitemmodel.cpp index b653057..d61f1ee 100644 --- a/src/declarative/fx/qfxvisualitemmodel.cpp +++ b/src/declarative/fx/qfxvisualitemmodel.cpp @@ -58,16 +58,136 @@ QML_DECLARE_TYPE(QListModelInterface) QT_BEGIN_NAMESPACE -class QFxVisualItemModelParts; -class QFxVisualItemModelData; class QFxVisualItemModelPrivate : public QObjectPrivate { + Q_DECLARE_PUBLIC(QFxVisualItemModel); public: - QFxVisualItemModelPrivate(QmlContext *); + QFxVisualItemModelPrivate() : QObjectPrivate(), children(this) {} + + struct ItemList : public QmlConcreteList<QFxItem *> + { + ItemList(QFxVisualItemModelPrivate *m) : QmlConcreteList<QFxItem *>(), model(m) {} + + void append(QFxItem *item); + + QFxVisualItemModelPrivate *model; + }; + + void itemAppended() { + Q_Q(QFxVisualItemModel); + emit q->itemsInserted(children.count()-1, 1); + emit q->countChanged(); + } + + ItemList children; +}; + + +/*! + \qmlclass VisualItemModel QFxVisualItemModel + \brief The VisualItemModel allows items to be provided to a view. + + The children of the VisualItemModel are provided in a model which + can be used in a view. + + The example below places three colored rectangles in a ListView. + \code + Item { + VisualItemModel { + id: ItemModel + Rect { height: 30; width: 80; color: "red" } + Rect { height: 30; width: 80; color: "green" } + Rect { height: 30; width: 80; color: "blue" } + } + + ListView { + anchors.fill: parent + model: ItemModel + } + } + \endcode +*/ +QFxVisualItemModel::QFxVisualItemModel() + : QFxVisualModel(*(new QFxVisualItemModelPrivate)) +{ +} + +QmlList<QFxItem *> *QFxVisualItemModel::children() +{ + Q_D(QFxVisualItemModel); + return &(d->children); +} + +/*! + \qmlproperty int VisualItemModel::count + + The number of items in the model. This property is readonly. +*/ +int QFxVisualItemModel::count() const +{ + Q_D(const QFxVisualItemModel); + return d->children.count(); +} + +bool QFxVisualItemModel::isValid() const +{ + return true; +} + +QFxItem *QFxVisualItemModel::item(int index, bool) +{ + Q_D(QFxVisualItemModel); + return d->children.at(index); +} + +QFxVisualModel::ReleaseFlags QFxVisualItemModel::release(QFxItem *) +{ + // Nothing to do + return 0; +} + +void QFxVisualItemModel::completeItem() +{ + // Nothing to do +} + +QVariant QFxVisualItemModel::evaluate(int index, const QString &expression, QObject *objectContext) +{ + Q_D(QFxVisualItemModel); + QmlContext *ccontext = qmlContext(this); + QmlContext *ctxt = new QmlContext(ccontext); + ctxt->addDefaultObject(d->children.at(index)); + QmlExpression e(ctxt, expression, objectContext); + e.setTrackChange(false); + QVariant value = e.value(); + delete ctxt; + return value; +} + +int QFxVisualItemModel::indexOf(QFxItem *item, QObject *) const +{ + Q_D(const QFxVisualItemModel); + return d->children.indexOf(item); +} + +void QFxVisualItemModelPrivate::ItemList::append(QFxItem *item) +{ + QmlConcreteList<QFxItem*>::append(item); + item->QObject::setParent(model->q_ptr); + model->itemAppended(); +} + + +class QFxVisualDataModelParts; +class QFxVisualDataModelData; +class QFxVisualDataModelPrivate : public QObjectPrivate +{ +public: + QFxVisualDataModelPrivate(QmlContext *); QGuard<QListModelInterface> m_listModelInterface; QGuard<QAbstractItemModel> m_abstractItemModel; - QGuard<QFxVisualItemModel> m_visualItemModel; + QGuard<QFxVisualDataModel> m_visualItemModel; QString m_part; QmlComponent *m_delegate; @@ -120,10 +240,10 @@ public: Cache m_cache; QHash<QObject *, QmlPackage*> m_packaged; - QFxVisualItemModelParts *m_parts; + QFxVisualDataModelParts *m_parts; friend class QFxVisualItemParts; - QFxVisualItemModelData *data(QObject *item); + QFxVisualDataModelData *data(QObject *item); QVariant m_modelVariant; QmlListAccessor *m_modelList; @@ -141,25 +261,25 @@ public: } }; -class QFxVisualItemModelDataMetaObject : public QmlOpenMetaObject +class QFxVisualDataModelDataMetaObject : public QmlOpenMetaObject { public: - QFxVisualItemModelDataMetaObject(QObject *parent) + QFxVisualDataModelDataMetaObject(QObject *parent) : QmlOpenMetaObject(parent) {} virtual QVariant propertyCreated(int, QMetaPropertyBuilder &); virtual int createProperty(const char *, const char *); private: - friend class QFxVisualItemModelData; + friend class QFxVisualDataModelData; QList<int> roles; }; -class QFxVisualItemModelData : public QObject +class QFxVisualDataModelData : public QObject { Q_OBJECT public: - QFxVisualItemModelData(int index, QFxVisualItemModelPrivate *model); + QFxVisualDataModelData(int index, QFxVisualDataModelPrivate *model); Q_PROPERTY(int index READ index NOTIFY indexChanged) int index() const; @@ -173,32 +293,32 @@ Q_SIGNALS: void indexChanged(); private: - friend class QFxVisualItemModelDataMetaObject; + friend class QFxVisualDataModelDataMetaObject; int m_index; - QFxVisualItemModelPrivate *m_model; - QFxVisualItemModelDataMetaObject *m_meta; + QFxVisualDataModelPrivate *m_model; + QFxVisualDataModelDataMetaObject *m_meta; }; -int QFxVisualItemModelData::count() const +int QFxVisualDataModelData::count() const { return m_meta->count(); } -int QFxVisualItemModelData::role(int id) const +int QFxVisualDataModelData::role(int id) const { Q_ASSERT(id >= 0 && id < count()); return m_meta->roles.at(id); } -void QFxVisualItemModelData::setValue(int id, const QVariant &val) +void QFxVisualDataModelData::setValue(int id, const QVariant &val) { m_meta->setValue(id, val); } -int QFxVisualItemModelDataMetaObject::createProperty(const char *name, const char *type) +int QFxVisualDataModelDataMetaObject::createProperty(const char *name, const char *type) { - QFxVisualItemModelData *data = - static_cast<QFxVisualItemModelData *>(object()); + QFxVisualDataModelData *data = + static_cast<QFxVisualDataModelData *>(object()); if ((!data->m_model->m_listModelInterface || !data->m_model->m_abstractItemModel) && data->m_model->m_modelList) { @@ -217,12 +337,12 @@ int QFxVisualItemModelDataMetaObject::createProperty(const char *name, const cha } QVariant -QFxVisualItemModelDataMetaObject::propertyCreated(int, QMetaPropertyBuilder &prop) +QFxVisualDataModelDataMetaObject::propertyCreated(int, QMetaPropertyBuilder &prop) { prop.setWritable(false); - QFxVisualItemModelData *data = - static_cast<QFxVisualItemModelData *>(object()); + QFxVisualDataModelData *data = + static_cast<QFxVisualDataModelData *>(object()); QString name = QLatin1String(prop.name()); if ((!data->m_model->m_listModelInterface || !data->m_model->m_abstractItemModel) && data->m_model->m_modelList) { @@ -255,105 +375,105 @@ QFxVisualItemModelDataMetaObject::propertyCreated(int, QMetaPropertyBuilder &pro return QVariant(); } -QFxVisualItemModelData::QFxVisualItemModelData(int index, - QFxVisualItemModelPrivate *model) +QFxVisualDataModelData::QFxVisualDataModelData(int index, + QFxVisualDataModelPrivate *model) : m_index(index), m_model(model), - m_meta(new QFxVisualItemModelDataMetaObject(this)) + m_meta(new QFxVisualDataModelDataMetaObject(this)) { } -int QFxVisualItemModelData::index() const +int QFxVisualDataModelData::index() const { return m_index; } // This is internal only - it should not be set from qml -void QFxVisualItemModelData::setIndex(int index) +void QFxVisualDataModelData::setIndex(int index) { m_index = index; emit indexChanged(); } -class QFxVisualItemModelPartsMetaObject : public QmlOpenMetaObject +class QFxVisualDataModelPartsMetaObject : public QmlOpenMetaObject { public: - QFxVisualItemModelPartsMetaObject(QObject *parent) + QFxVisualDataModelPartsMetaObject(QObject *parent) : QmlOpenMetaObject(parent) {} virtual QVariant propertyCreated(int, QMetaPropertyBuilder &); }; -class QFxVisualItemModelParts : public QObject +class QFxVisualDataModelParts : public QObject { Q_OBJECT public: - QFxVisualItemModelParts(QFxVisualItemModel *parent); + QFxVisualDataModelParts(QFxVisualDataModel *parent); private: - friend class QFxVisualItemModelPartsMetaObject; - QFxVisualItemModel *model; + friend class QFxVisualDataModelPartsMetaObject; + QFxVisualDataModel *model; }; QVariant -QFxVisualItemModelPartsMetaObject::propertyCreated(int, QMetaPropertyBuilder &prop) +QFxVisualDataModelPartsMetaObject::propertyCreated(int, QMetaPropertyBuilder &prop) { prop.setWritable(false); - QFxVisualItemModel *m = new QFxVisualItemModel; + QFxVisualDataModel *m = new QFxVisualDataModel; m->setParent(object()); m->setPart(QLatin1String(prop.name())); - m->setModel(QVariant::fromValue(static_cast<QFxVisualItemModelParts *>(object())->model)); + m->setModel(QVariant::fromValue(static_cast<QFxVisualDataModelParts *>(object())->model)); QVariant var = QVariant::fromValue((QObject *)m); return var; } -QFxVisualItemModelParts::QFxVisualItemModelParts(QFxVisualItemModel *parent) +QFxVisualDataModelParts::QFxVisualDataModelParts(QFxVisualDataModel *parent) : QObject(parent), model(parent) { - new QFxVisualItemModelPartsMetaObject(this); + new QFxVisualDataModelPartsMetaObject(this); } -QFxVisualItemModelPrivate::QFxVisualItemModelPrivate(QmlContext *ctxt) +QFxVisualDataModelPrivate::QFxVisualDataModelPrivate(QmlContext *ctxt) : m_listModelInterface(0), m_abstractItemModel(0), m_visualItemModel(0), m_delegate(0) , m_context(ctxt), m_parts(0), m_modelList(0) { } -QFxVisualItemModelData *QFxVisualItemModelPrivate::data(QObject *item) +QFxVisualDataModelData *QFxVisualDataModelPrivate::data(QObject *item) { - QList<QFxVisualItemModelData *> dataList = - item->findChildren<QFxVisualItemModelData *>(); + QList<QFxVisualDataModelData *> dataList = + item->findChildren<QFxVisualDataModelData *>(); Q_ASSERT(dataList.count() == 1); return dataList.first(); } -QFxVisualItemModel::QFxVisualItemModel() -: QObject(*(new QFxVisualItemModelPrivate(0))) +QFxVisualDataModel::QFxVisualDataModel() +: QFxVisualModel(*(new QFxVisualDataModelPrivate(0))) { } -QFxVisualItemModel::QFxVisualItemModel(QmlContext *ctxt) -: QObject(*(new QFxVisualItemModelPrivate(ctxt))) +QFxVisualDataModel::QFxVisualDataModel(QmlContext *ctxt) +: QFxVisualModel(*(new QFxVisualDataModelPrivate(ctxt))) { } -QFxVisualItemModel::~QFxVisualItemModel() +QFxVisualDataModel::~QFxVisualDataModel() { - Q_D(QFxVisualItemModel); + Q_D(QFxVisualDataModel); if (d->m_modelList) delete d->m_modelList; } -QVariant QFxVisualItemModel::model() const +QVariant QFxVisualDataModel::model() const { - Q_D(const QFxVisualItemModel); + Q_D(const QFxVisualDataModel); return d->m_modelVariant; } -void QFxVisualItemModel::setModel(const QVariant &model) +void QFxVisualDataModel::setModel(const QVariant &model) { - Q_D(QFxVisualItemModel); + Q_D(QFxVisualDataModel); d->m_modelVariant = model; if (d->m_listModelInterface) { // Assume caller has released all items. @@ -426,7 +546,7 @@ void QFxVisualItemModel::setModel(const QVariant &model) this, SLOT(_q_dataChanged(const QModelIndex&,const QModelIndex&))); return; } - if ((d->m_visualItemModel = qvariant_cast<QFxVisualItemModel *>(model))) { + if ((d->m_visualItemModel = qvariant_cast<QFxVisualDataModel *>(model))) { QObject::connect(d->m_visualItemModel, SIGNAL(itemsInserted(int,int)), this, SIGNAL(itemsInserted(int,int))); QObject::connect(d->m_visualItemModel, SIGNAL(itemsRemoved(int,int)), @@ -446,43 +566,43 @@ void QFxVisualItemModel::setModel(const QVariant &model) emit itemsInserted(0, d->modelCount()); } -QmlComponent *QFxVisualItemModel::delegate() const +QmlComponent *QFxVisualDataModel::delegate() const { - Q_D(const QFxVisualItemModel); + Q_D(const QFxVisualDataModel); if (d->m_visualItemModel) return d->m_visualItemModel->delegate(); return d->m_delegate; } -void QFxVisualItemModel::setDelegate(QmlComponent *delegate) +void QFxVisualDataModel::setDelegate(QmlComponent *delegate) { - Q_D(QFxVisualItemModel); + Q_D(QFxVisualDataModel); d->m_delegate = delegate; if (d->modelCount()) emit itemsInserted(0, d->modelCount()); } -QString QFxVisualItemModel::part() const +QString QFxVisualDataModel::part() const { - Q_D(const QFxVisualItemModel); + Q_D(const QFxVisualDataModel); return d->m_part; } -void QFxVisualItemModel::setPart(const QString &part) +void QFxVisualDataModel::setPart(const QString &part) { - Q_D(QFxVisualItemModel); + Q_D(QFxVisualDataModel); d->m_part = part; } -int QFxVisualItemModel::count() const +int QFxVisualDataModel::count() const { - Q_D(const QFxVisualItemModel); + Q_D(const QFxVisualDataModel); return d->modelCount(); } -QFxItem *QFxVisualItemModel::item(int index, bool complete) +QFxItem *QFxVisualDataModel::item(int index, bool complete) { - Q_D(QFxVisualItemModel); + Q_D(QFxVisualDataModel); if (d->m_visualItemModel) return d->m_visualItemModel->item(index, d->m_part.toLatin1(), complete); return item(index, QByteArray(), complete); @@ -491,9 +611,9 @@ QFxItem *QFxVisualItemModel::item(int index, bool complete) /* Returns ReleaseStatus flags. */ -QFxVisualItemModel::ReleaseFlags QFxVisualItemModel::release(QFxItem *item) +QFxVisualDataModel::ReleaseFlags QFxVisualDataModel::release(QFxItem *item) { - Q_D(QFxVisualItemModel); + Q_D(QFxVisualDataModel); if (d->m_visualItemModel) return d->m_visualItemModel->release(item); @@ -523,17 +643,17 @@ QFxVisualItemModel::ReleaseFlags QFxVisualItemModel::release(QFxItem *item) return stat; } -QObject *QFxVisualItemModel::parts() +QObject *QFxVisualDataModel::parts() { - Q_D(QFxVisualItemModel); + Q_D(QFxVisualDataModel); if (!d->m_parts) - d->m_parts = new QFxVisualItemModelParts(this); + d->m_parts = new QFxVisualDataModelParts(this); return d->m_parts; } -QFxItem *QFxVisualItemModel::item(int index, const QByteArray &viewId, bool complete) +QFxItem *QFxVisualDataModel::item(int index, const QByteArray &viewId, bool complete) { - Q_D(QFxVisualItemModel); + Q_D(QFxVisualDataModel); if (d->m_visualItemModel) return d->m_visualItemModel->item(index, viewId, complete); @@ -545,7 +665,7 @@ QFxItem *QFxVisualItemModel::item(int index, const QByteArray &viewId, bool comp QmlContext *ccontext = d->m_context; if (!ccontext) ccontext = qmlContext(this); QmlContext *ctxt = new QmlContext(ccontext); - QFxVisualItemModelData *data = new QFxVisualItemModelData(index, d); + QFxVisualDataModelData *data = new QFxVisualDataModelData(index, d); ctxt->setContextProperty(QLatin1String("model"), data); ctxt->addDefaultObject(data); nobj = d->m_delegate->beginCreate(ctxt); @@ -576,9 +696,9 @@ QFxItem *QFxVisualItemModel::item(int index, const QByteArray &viewId, bool comp return item; } -void QFxVisualItemModel::completeItem() +void QFxVisualDataModel::completeItem() { - Q_D(QFxVisualItemModel); + Q_D(QFxVisualDataModel); if (d->m_visualItemModel) { d->m_visualItemModel->completeItem(); return; @@ -587,9 +707,9 @@ void QFxVisualItemModel::completeItem() d->m_delegate->completeCreate(); } -QVariant QFxVisualItemModel::evaluate(int index, const QString &expression, QObject *objectContext) +QVariant QFxVisualDataModel::evaluate(int index, const QString &expression, QObject *objectContext) { - Q_D(QFxVisualItemModel); + Q_D(QFxVisualDataModel); if (d->m_visualItemModel) return d->m_visualItemModel->evaluate(index, expression, objectContext); @@ -609,7 +729,7 @@ QVariant QFxVisualItemModel::evaluate(int index, const QString &expression, QObj QmlContext *ccontext = d->m_context; if (!ccontext) ccontext = qmlContext(this); QmlContext *ctxt = new QmlContext(ccontext); - QFxVisualItemModelData *data = new QFxVisualItemModelData(index, d); + QFxVisualDataModelData *data = new QFxVisualDataModelData(index, d); ctxt->addDefaultObject(data); QmlExpression e(ctxt, expression, objectContext); e.setTrackChange(false); @@ -621,7 +741,7 @@ QVariant QFxVisualItemModel::evaluate(int index, const QString &expression, QObj return value; } -int QFxVisualItemModel::indexOf(QFxItem *item, QObject *objectContext) const +int QFxVisualDataModel::indexOf(QFxItem *item, QObject *objectContext) const { QmlExpression e(qmlContext(item), QLatin1String("index"), objectContext); e.setTrackChange(false); @@ -631,15 +751,15 @@ int QFxVisualItemModel::indexOf(QFxItem *item, QObject *objectContext) const return -1; } -void QFxVisualItemModel::_q_itemsChanged(int index, int count, +void QFxVisualDataModel::_q_itemsChanged(int index, int count, const QList<int> &roles) { - Q_D(QFxVisualItemModel); + Q_D(QFxVisualDataModel); // XXX - highly inefficient for (int ii = index; ii < index + count; ++ii) { if (QObject *item = d->m_cache.item(ii)) { - QFxVisualItemModelData *data = d->data(item); + QFxVisualDataModelData *data = d->data(item); for (int prop = 0; prop < data->count(); ++prop) { @@ -658,22 +778,22 @@ void QFxVisualItemModel::_q_itemsChanged(int index, int count, } } -void QFxVisualItemModel::_q_itemsInserted(int index, int count) +void QFxVisualDataModel::_q_itemsInserted(int index, int count) { - Q_D(QFxVisualItemModel); + Q_D(QFxVisualDataModel); // XXX - highly inefficient - QHash<int,QFxVisualItemModelPrivate::ObjectRef> items; - for (QHash<int,QFxVisualItemModelPrivate::ObjectRef>::Iterator iter = d->m_cache.begin(); + QHash<int,QFxVisualDataModelPrivate::ObjectRef> items; + for (QHash<int,QFxVisualDataModelPrivate::ObjectRef>::Iterator iter = d->m_cache.begin(); iter != d->m_cache.end(); ) { if (iter.key() >= index) { - QFxVisualItemModelPrivate::ObjectRef objRef = *iter; + QFxVisualDataModelPrivate::ObjectRef objRef = *iter; int index = iter.key() + count; iter = d->m_cache.erase(iter); items.insert(index, objRef); - QFxVisualItemModelData *data = d->data(objRef.obj); + QFxVisualDataModelData *data = d->data(objRef.obj); data->setIndex(index); } else { ++iter; @@ -684,25 +804,25 @@ void QFxVisualItemModel::_q_itemsInserted(int index, int count) emit itemsInserted(index, count); } -void QFxVisualItemModel::_q_itemsRemoved(int index, int count) +void QFxVisualDataModel::_q_itemsRemoved(int index, int count) { - Q_D(QFxVisualItemModel); + Q_D(QFxVisualDataModel); // XXX - highly inefficient - QHash<int, QFxVisualItemModelPrivate::ObjectRef> items; - for (QHash<int, QFxVisualItemModelPrivate::ObjectRef>::Iterator iter = d->m_cache.begin(); + QHash<int, QFxVisualDataModelPrivate::ObjectRef> items; + for (QHash<int, QFxVisualDataModelPrivate::ObjectRef>::Iterator iter = d->m_cache.begin(); iter != d->m_cache.end(); ) { if (iter.key() >= index && iter.key() < index + count) { - QFxVisualItemModelPrivate::ObjectRef objRef = *iter; + QFxVisualDataModelPrivate::ObjectRef objRef = *iter; iter = d->m_cache.erase(iter); items.insertMulti(-1, objRef); //XXX perhaps better to maintain separately - QFxVisualItemModelData *data = d->data(objRef.obj); + QFxVisualDataModelData *data = d->data(objRef.obj); data->setIndex(-1); } else if (iter.key() >= index + count) { - QFxVisualItemModelPrivate::ObjectRef objRef = *iter; + QFxVisualDataModelPrivate::ObjectRef objRef = *iter; int index = iter.key() - count; iter = d->m_cache.erase(iter); items.insert(index, objRef); - QFxVisualItemModelData *data = d->data(objRef.obj); + QFxVisualDataModelData *data = d->data(objRef.obj); data->setIndex(index); } else { ++iter; @@ -713,22 +833,22 @@ void QFxVisualItemModel::_q_itemsRemoved(int index, int count) emit itemsRemoved(index, count); } -void QFxVisualItemModel::_q_itemsMoved(int from, int to, int count) +void QFxVisualDataModel::_q_itemsMoved(int from, int to, int count) { - Q_D(QFxVisualItemModel); + Q_D(QFxVisualDataModel); // XXX - highly inefficient - QHash<int,QFxVisualItemModelPrivate::ObjectRef> items; - for (QHash<int,QFxVisualItemModelPrivate::ObjectRef>::Iterator iter = d->m_cache.begin(); + QHash<int,QFxVisualDataModelPrivate::ObjectRef> items; + for (QHash<int,QFxVisualDataModelPrivate::ObjectRef>::Iterator iter = d->m_cache.begin(); iter != d->m_cache.end(); ) { if (iter.key() >= from && iter.key() < from + count) { - QFxVisualItemModelPrivate::ObjectRef objRef = *iter; + QFxVisualDataModelPrivate::ObjectRef objRef = *iter; int index = iter.key() - from + to; iter = d->m_cache.erase(iter); items.insert(index, objRef); - QFxVisualItemModelData *data = d->data(objRef.obj); + QFxVisualDataModelData *data = d->data(objRef.obj); data->setIndex(index); } else { ++iter; @@ -739,35 +859,36 @@ void QFxVisualItemModel::_q_itemsMoved(int from, int to, int count) emit itemsMoved(from, to, count); } -void QFxVisualItemModel::_q_rowsInserted(const QModelIndex &, int begin, int end) +void QFxVisualDataModel::_q_rowsInserted(const QModelIndex &, int begin, int end) { _q_itemsInserted(begin, end - begin + 1); } -void QFxVisualItemModel::_q_rowsRemoved(const QModelIndex &, int begin, int end) +void QFxVisualDataModel::_q_rowsRemoved(const QModelIndex &, int begin, int end) { _q_itemsRemoved(begin, end - begin + 1); } -void QFxVisualItemModel::_q_dataChanged(const QModelIndex &begin, const QModelIndex &end) +void QFxVisualDataModel::_q_dataChanged(const QModelIndex &begin, const QModelIndex &end) { - Q_D(QFxVisualItemModel); + Q_D(QFxVisualDataModel); _q_itemsChanged(begin.row(), end.row() - begin.row() + 1, d->m_roles); } -void QFxVisualItemModel::_q_createdPackage(int index, QmlPackage *package) +void QFxVisualDataModel::_q_createdPackage(int index, QmlPackage *package) { - Q_D(QFxVisualItemModel); + Q_D(QFxVisualDataModel); emit createdItem(index, qobject_cast<QFxItem*>(package->part(d->m_part))); } -void QFxVisualItemModel::_q_destroyingPackage(QmlPackage *package) +void QFxVisualDataModel::_q_destroyingPackage(QmlPackage *package) { - Q_D(QFxVisualItemModel); + Q_D(QFxVisualDataModel); emit destroyingItem(qobject_cast<QFxItem*>(package->part(d->m_part))); } -QML_DEFINE_TYPE(Qt,4,6,(QT_VERSION&0x00ff00)>>8,VisualModel,QFxVisualItemModel) +QML_DEFINE_TYPE(Qt,4,6,(QT_VERSION&0x00ff00)>>8,VisualItemModel,QFxVisualItemModel) +QML_DEFINE_TYPE(Qt,4,6,(QT_VERSION&0x00ff00)>>8,VisualDataModel,QFxVisualDataModel) QT_END_NAMESPACE #include "qfxvisualitemmodel.moc" diff --git a/src/declarative/fx/qfxvisualitemmodel.h b/src/declarative/fx/qfxvisualitemmodel.h index 551c08d..6160068 100644 --- a/src/declarative/fx/qfxvisualitemmodel.h +++ b/src/declarative/fx/qfxvisualitemmodel.h @@ -39,8 +39,8 @@ ** ****************************************************************************/ -#ifndef QFXVISUALITEMMODEL_H -#define QFXVISUALITEMMODEL_H +#ifndef QFXVISUALDATAMODEL_H +#define QFXVISUALDATAMODEL_H #include <QtCore/qobject.h> #include <QtCore/qabstractitemmodel.h> @@ -61,21 +61,89 @@ QT_MODULE(Declarative) class QFxItem; class QmlComponent; class QmlPackage; +class QFxVisualDataModelPrivate; + +class Q_DECLARATIVE_EXPORT QFxVisualModel : public QObject +{ + Q_OBJECT + + Q_PROPERTY(int count READ count NOTIFY countChanged) + +public: + QFxVisualModel() {} + virtual ~QFxVisualModel() {} + + enum ReleaseFlag { Referenced = 0x01, Destroyed = 0x02 }; + Q_DECLARE_FLAGS(ReleaseFlags, ReleaseFlag) + + virtual int count() const = 0; + virtual bool isValid() const = 0; + virtual QFxItem *item(int index, bool complete=true) = 0; + virtual ReleaseFlags release(QFxItem *item) = 0; + virtual void completeItem() = 0; + virtual QVariant evaluate(int index, const QString &expression, QObject *objectContext) = 0; + + virtual int indexOf(QFxItem *item, QObject *objectContext) const = 0; + +Q_SIGNALS: + void countChanged(); + void itemsInserted(int index, int count); + void itemsRemoved(int index, int count); + void itemsMoved(int from, int to, int count); + void createdItem(int index, QFxItem *item); + void destroyingItem(QFxItem *item); + +protected: + QFxVisualModel(QObjectPrivate &dd, QObject *parent = 0) + : QObject(dd, parent) {} + +private: + Q_DISABLE_COPY(QFxVisualModel) +}; + class QFxVisualItemModelPrivate; -class Q_DECLARATIVE_EXPORT QFxVisualItemModel : public QObject +class Q_DECLARATIVE_EXPORT QFxVisualItemModel : public QFxVisualModel { Q_OBJECT Q_DECLARE_PRIVATE(QFxVisualItemModel) + Q_PROPERTY(QmlList<QFxItem *>* children READ children DESIGNABLE false) + Q_CLASSINFO("DefaultProperty", "children") + +public: + QFxVisualItemModel(); + virtual ~QFxVisualItemModel() {} + + virtual int count() const; + virtual bool isValid() const; + virtual QFxItem *item(int index, bool complete=true); + virtual ReleaseFlags release(QFxItem *item); + virtual void completeItem(); + virtual QVariant evaluate(int index, const QString &expression, QObject *objectContext); + + virtual int indexOf(QFxItem *item, QObject *objectContext) const; + + QmlList<QFxItem *> *children(); + +private: + Q_DISABLE_COPY(QFxVisualItemModel) +}; + + +class Q_DECLARATIVE_EXPORT QFxVisualDataModel : public QFxVisualModel +{ + Q_OBJECT + Q_DECLARE_PRIVATE(QFxVisualDataModel) + Q_PROPERTY(QVariant model READ model WRITE setModel) Q_PROPERTY(QmlComponent *delegate READ delegate WRITE setDelegate) Q_PROPERTY(QString part READ part WRITE setPart) Q_PROPERTY(QObject *parts READ parts CONSTANT) Q_CLASSINFO("DefaultProperty", "delegate") public: - QFxVisualItemModel(); - QFxVisualItemModel(QmlContext *); - virtual ~QFxVisualItemModel(); + QFxVisualDataModel(); + QFxVisualDataModel(QmlContext *); + virtual ~QFxVisualDataModel(); QVariant model() const; void setModel(const QVariant &); @@ -86,10 +154,8 @@ public: QString part() const; void setPart(const QString &); - enum ReleaseFlag { Referenced = 0x01, Destroyed = 0x02 }; - Q_DECLARE_FLAGS(ReleaseFlags, ReleaseFlag) - int count() const; + bool isValid() const { return delegate() != 0; } QFxItem *item(int index, bool complete=true); QFxItem *item(int index, const QByteArray &, bool complete=true); ReleaseFlags release(QFxItem *item); @@ -101,12 +167,7 @@ public: QObject *parts(); Q_SIGNALS: - void itemsInserted(int index, int count); - void itemsRemoved(int index, int count); - void itemsMoved(int from, int to, int count); - void createdItem(int index, QFxItem *item); void createdPackage(int index, QmlPackage *package); - void destroyingItem(QFxItem *item); void destroyingPackage(QmlPackage *package); private Q_SLOTS: @@ -121,13 +182,14 @@ private Q_SLOTS: void _q_destroyingPackage(QmlPackage *package); private: - Q_DISABLE_COPY(QFxVisualItemModel) + Q_DISABLE_COPY(QFxVisualDataModel) }; QT_END_NAMESPACE QML_DECLARE_TYPE(QFxVisualItemModel) +QML_DECLARE_TYPE(QFxVisualDataModel) QT_END_HEADER -#endif // QFXVISUALITEMMODEL_H +#endif // QFXVISUALDATAMODEL_H |