diff options
author | Marius Bugge Monsen <mmonsen@trolltech.com> | 2009-07-30 13:52:25 +1000 |
---|---|---|
committer | Marius Bugge Monsen <mmonsen@trolltech.com> | 2009-07-30 13:52:25 +1000 |
commit | 38ff2e6f21b1196948eca97979f4a9840fe071f1 (patch) | |
tree | 9455166ee29a2359d84f7fcd6a23c6bb1e34e981 | |
parent | a36573dbe4961a0289f7fc8ea27b2a5903429ffe (diff) |
Separate the creator out into its own file.
-rw-r--r-- | examples/qmlListView/creator.cpp | 112 | ||||
-rw-r--r-- | examples/qmlListView/creator.h | 33 | ||||
-rw-r--r-- | examples/qmlListView/main.cpp | 96 | ||||
-rw-r--r-- | examples/qmlListView/qmlListView.pro | 3 |
4 files changed, 174 insertions, 70 deletions
diff --git a/examples/qmlListView/creator.cpp b/examples/qmlListView/creator.cpp new file mode 100644 index 0000000..f4e4dcf --- /dev/null +++ b/examples/qmlListView/creator.cpp @@ -0,0 +1,112 @@ +#include "creator.h" + +#include "../../src/qlistmodelinterface.h" // ### FIXME: qml has 3rdparty/qlistmodelinterfache.h + +#include <QmlContext> +#include <QmlEngine> +#include <QmlComponent> + +// context + +class Context : public QmlContext +{ +public: + Context(QmlEngine *parentEngine, QObject *parentObject = 0) + : QmlContext(parentEngine, parentObject), m_index(-1), m_model(0) {} + Context(QmlContext *parentContext, QObject *parentObject = 0) + : QmlContext(parentContext, parentObject), m_index(-1), m_model(0) {} + + inline void setIndex(int index) { m_index = index; } + inline int index() const { return m_index; } + inline void setModel(QtListModelInterface *model) { m_model = model; } + inline QtListModelInterface *model() const { return m_model; } + inline void enableHack() { setHackEnabled(true); } + inline void disableHack() { setHackEnabled(false); } + +protected: + void contextPropertyWrite(const QString &name, const QVariant &value); + QVariant contextPropertyRead(const QString &name) const; + +private: + int m_index; + QtListModelInterface *m_model; +}; + +void Context::contextPropertyWrite(const QString &name, const QVariant &value) +{ + QHash<int,QVariant> data; + int role = m_model->roles().key(name.toLatin1()); // ### FIXME + data.insert(role, value); + m_model->setData(m_index, data); +} + +QVariant Context::contextPropertyRead(const QString &name) const +{ + const int role = m_model->roles().key(name.toLatin1()); // ### FIXME + return m_model->data(m_index).value(role); +} + +// creator + +Creator::Creator(QmlEngine *engine, QmlComponent *component) + : QtGraphicsListViewItemCreatorBase(), m_engine(engine), m_component(component) +{ +} + +Creator::~Creator() +{ +} + +QGraphicsObject *Creator::create(int index, QtGraphicsListView *view) +{ + // setup the context for the item and set the data + QmlContext *parentContext = QmlEngine::contextForObject(view); + Context *context = parentContext ? new Context(parentContext) : new Context(m_engine); + context->setIndex(index); + context->setModel(view->model()); + setContextProperties(index, context, view); + + // use the component to create the item + QGraphicsObject *item = 0; + if (QObject *obj = m_component->create(context)) { + item = static_cast<QGraphicsObject*>(obj); + item->setParentItem(view); + context->setParent(item); + m_contexts.insert(item, context); + context->enableHack(); + } + + if (!context->parent()) + delete context; + return item; +} + +QGraphicsObject *Creator::reassign(int index, QGraphicsObject *item, QtGraphicsListView *view) +{ + m_contexts.value(item)->setIndex(index); + setContextProperties(index, m_contexts.value(item), view); + return item; +} + +void Creator::recycle(QGraphicsObject *item, QtGraphicsListView *view) +{ + Q_UNUSED(view); + m_contexts.remove(item); + delete item; +} + +void Creator::setContextProperties(int index, QmlContext *context, QtGraphicsListView *view) const +{ + if (context && view) { + QHash<int, QByteArray> roles = view->model()->roles(); + QHash<int, QVariant> data = view->model()->data(index); + QHash<int, QVariant>::ConstIterator it = data.constBegin(); + for (;it != data.constEnd(); ++it) { + QByteArray name = roles.value(it.key()); + QVariant value = it.value(); + static_cast<Context*>(context)->disableHack(); + context->setContextProperty(name, value); + static_cast<Context*>(context)->enableHack(); + } + } +} diff --git a/examples/qmlListView/creator.h b/examples/qmlListView/creator.h new file mode 100644 index 0000000..6f081fb --- /dev/null +++ b/examples/qmlListView/creator.h @@ -0,0 +1,33 @@ +#ifndef _CREATOR_H +#define _CREATOR_H + +#include <qgraphicslistview.h> + +class QmlEngine; +class QmlComponent; +class QmlContext; +class Context; + +class Creator : public QtGraphicsListViewItemCreatorBase +{ +public: + Creator(QmlEngine *engine, QmlComponent *component); + ~Creator(); + + // creator api + QGraphicsObject *create(int index, QtGraphicsListView *view); + QGraphicsObject *reassign(int index, QGraphicsObject *item, QtGraphicsListView *view); + void recycle(QGraphicsObject *item, QtGraphicsListView *view); + + // FIXME: make the creator a qobject and connect to itemsChanged + // the creator should keep a list of active objects so it can + // update the data in items that change in the model + +private: + void setContextProperties(int index, QmlContext *context, QtGraphicsListView *view) const; + QmlEngine *m_engine; + QmlComponent *m_component; + QHash<QGraphicsObject*, Context*> m_contexts; +}; + +#endif // _CREATOR_H diff --git a/examples/qmlListView/main.cpp b/examples/qmlListView/main.cpp index 7c391f4..00eebb5 100644 --- a/examples/qmlListView/main.cpp +++ b/examples/qmlListView/main.cpp @@ -1,69 +1,16 @@ -#include <QtGui> -#include <QtDeclarative> +#include <QApplication> -#include <qgraphicslistview.h> +#include "creator.h" #include <qlistwidgetng.h> #include <qlistcontroller.h> #include <qlistdefaultmodel.h> +#include <qgraphicslistview.h> -#include <qdebug.h> - -// creator - -class QmlListViewCreator : public QtGraphicsListViewItemCreatorBase -{ -public: - ~QmlListViewCreator(); - QGraphicsObject *create(int index, QtGraphicsListView *view); - QGraphicsObject *reassign(int index, QGraphicsObject *item, QtGraphicsListView *view); - void recycle(QGraphicsObject *item, QtGraphicsListView *view); - QmlComponent *m_component; - QmlEngine *m_engine; -}; - -QmlListViewCreator::~QmlListViewCreator() -{ -} - -QGraphicsObject *QmlListViewCreator::create(int index, QtGraphicsListView *view) -{ - QGraphicsObject *gobj = 0; - QmlContext *context = new QmlContext(m_engine); - - QHash<int, QByteArray> roles = view->model()->roles(); - QHash<int, QVariant> data = view->model()->data(index); - QHash<int, QVariant>::ConstIterator it = data.constBegin(); - for (;it != data.constEnd(); ++it) { - QByteArray name = roles.value(it.key()); - QVariant value = it.value(); - context->setContextProperty(name, value); - } - - if (QObject *obj = m_component->create(context)) { - gobj = static_cast<QGraphicsObject*>(obj); - gobj->setParentItem(view); - context->setParent(gobj); - } - - if (!context->parent()) - delete context; +#include <QmlEngine> +#include <QmlComponent> - return gobj; -} - -QGraphicsObject *QmlListViewCreator::reassign(int index, QGraphicsObject *item, QtGraphicsListView *view) -{ - delete item; - return create(index, view); - //return item; -} - -void QmlListViewCreator::recycle(QGraphicsObject *item, QtGraphicsListView *view) -{ - Q_UNUSED(view); - delete item; -} +#include <qdebug.h> // main @@ -76,23 +23,34 @@ int main(int argc, char *argv[]) "Rect { \n" " width: 200\n" " height: 50\n" - " color: \"green\"\n" - " Text {\n" - " id: \"text\"\n" - " text: DisplayRole\n" - " x: 20; y: 20\n" + " color: BackgroundRole\n" + " HorizontalLayout {\n" + " Rect {\n" + " width: 50\n" + " height: 50\n" + " color: \"blue\"\n" + " }\n" + " Text {\n" + " color: ForegroundRole\n" + " text: DisplayRole\n" + " x: 20; y: 20\n" + " }\n" " }\n" "}"; - QmlListViewCreator *creator = new QmlListViewCreator; - creator->m_component = new QmlComponent(&engine, qml.toUtf8(), QUrl()); - creator->m_engine = &engine; + Creator *creator = new Creator(&engine, new QmlComponent(&engine, qml.toUtf8(), QUrl())); QtListWidgetNG widget; widget.controller()->view()->setItemCreator(creator); - for (int i = 0; i < 100; ++i) - widget.defaultModel()->appendItem(new QtListDefaultItem(QString("foo ") + QString::number(i))); + for (int i = 0; i < 1000; ++i) { + QtListDefaultItem *item = new QtListDefaultItem(QString("foo ") + QString::number(i)); + item->setBackground(QBrush(qRgb(0, 0xff, 0))); + item->setForeground(QBrush(qRgb(0xff, 0, 0))); + widget.defaultModel()->appendItem(item); + } widget.show(); return app.exec(); } + +#include "main.moc" diff --git a/examples/qmlListView/qmlListView.pro b/examples/qmlListView/qmlListView.pro index 3fbd254..bf9e6cd 100644 --- a/examples/qmlListView/qmlListView.pro +++ b/examples/qmlListView/qmlListView.pro @@ -12,4 +12,5 @@ CONFIG += debug include(../examples.pri) # Input -SOURCES += main.cpp +HEADERS += creator.h +SOURCES += creator.cpp main.cpp |