return r;
}
-ModelObject *ListModel::getOrCreateModelObject(QQmlListModel *model, int elementIndex)
+QObject *ListModel::getOrCreateModelObject(QQmlListModel *model, int elementIndex)
{
ListElement *e = elements[elementIndex];
if (e->m_objectCache == 0) {
- e->m_objectCache = new ModelObject(model, elementIndex);
+ e->m_objectCache = new QObject;
+ (void)new ModelNodeMetaObject(e->m_objectCache, model, elementIndex);
}
return e->m_objectCache;
}
// Update values stored in target meta objects
for (int i=0 ; i < target->elements.count() ; ++i) {
ListElement *e = target->elements[i];
- if (e->m_objectCache)
- e->m_objectCache->updateValues();
+ if (ModelNodeMetaObject *mo = e->objectCache())
+ mo->updateValues();
}
}
{
for (int i=0 ; i < elements.count() ; ++i) {
ListElement *e = elements.at(i);
- if (e->m_objectCache) {
- e->m_objectCache->m_elementIndex = i;
- }
+ if (ModelNodeMetaObject *mo = e->objectCache())
+ mo->m_elementIndex = i;
}
}
roles->append(roleIndex);
}
- if (e->m_objectCache) {
- e->m_objectCache->updateValues(*roles);
- }
+ if (ModelNodeMetaObject *mo = e->objectCache())
+ mo->updateValues(*roles);
}
void ListModel::set(int elementIndex, QV4::Object *object)
if (r) {
roleIndex = e->setVariantProperty(*r, data);
- if (roleIndex != -1 && e->m_objectCache) {
+ ModelNodeMetaObject *cache = e->objectCache();
+
+ if (roleIndex != -1 && cache) {
QVector<int> roles;
roles << roleIndex;
- e->m_objectCache->updateValues(roles);
+ cache->updateValues(roles);
}
}
}
return mem;
}
+ModelNodeMetaObject *ListElement::objectCache()
+{
+ if (!m_objectCache)
+ return 0;
+ return ModelNodeMetaObject::get(m_objectCache);
+}
+
QString *ListElement::getStringProperty(const ListLayout::Role &role)
{
char *mem = getPropertyMemory(role);
return roleIndex;
}
-ModelObject::ModelObject(QQmlListModel *model, int elementIndex)
-: m_model(model), m_elementIndex(elementIndex), m_meta(new ModelNodeMetaObject(this))
+ModelNodeMetaObject::ModelNodeMetaObject(QObject *object, QQmlListModel *model, int elementIndex)
+: QQmlOpenMetaObject(object), m_enabled(false), m_model(model), m_elementIndex(elementIndex), m_initialized(false)
+{}
+
+void ModelNodeMetaObject::initialize()
{
+ const int roleCount = m_model->m_listModel->roleCount();
+ QVector<QByteArray> properties;
+ properties.reserve(roleCount);
+ for (int i = 0 ; i < roleCount ; ++i) {
+ const ListLayout::Role &role = m_model->m_listModel->getExistingRole(i);
+ QByteArray name = role.name.toUtf8();
+ properties << name;
+ }
+ type()->createProperties(properties);
updateValues();
- setNodeUpdatesEnabled(true);
+ m_enabled = true;
+}
+
+ModelNodeMetaObject::~ModelNodeMetaObject()
+{
+}
+
+QAbstractDynamicMetaObject *ModelNodeMetaObject::toDynamicMetaObject(QObject *object)
+{
+ if (!m_initialized) {
+ m_initialized = true;
+ initialize();
+ }
+ return QQmlOpenMetaObject::toDynamicMetaObject(object);
+}
+
+ModelNodeMetaObject *ModelNodeMetaObject::get(QObject *obj)
+{
+ QObjectPrivate *op = QObjectPrivate::get(obj);
+ return static_cast<ModelNodeMetaObject*>(op->metaObject);
}
-void ModelObject::updateValues()
+void ModelNodeMetaObject::updateValues()
{
+ if (!m_initialized)
+ return;
int roleCount = m_model->m_listModel->roleCount();
for (int i=0 ; i < roleCount ; ++i) {
const ListLayout::Role &role = m_model->m_listModel->getExistingRole(i);
}
}
-void ModelObject::updateValues(const QVector<int> &roles)
+void ModelNodeMetaObject::updateValues(const QVector<int> &roles)
{
+ if (!m_initialized)
+ return;
int roleCount = roles.count();
for (int i=0 ; i < roleCount ; ++i) {
int roleIndex = roles.at(i);
}
}
-ModelNodeMetaObject::ModelNodeMetaObject(ModelObject *object)
-: QQmlOpenMetaObject(object), m_enabled(false), m_obj(object)
-{
-}
-
-ModelNodeMetaObject::~ModelNodeMetaObject()
-{
-}
-
void ModelNodeMetaObject::propertyWritten(int index)
{
if (!m_enabled)
QString propName = QString::fromUtf8(name(index));
QVariant value = operator[](index);
- QV4::Scope scope(m_obj->m_model->engine());
+ QV4::Scope scope(m_model->engine());
QV4::ScopedValue v(scope, scope.engine->fromVariant(value));
- int roleIndex = m_obj->m_model->m_listModel->setExistingProperty(m_obj->m_elementIndex, propName, v, scope.engine);
+ int roleIndex = m_model->m_listModel->setExistingProperty(m_elementIndex, propName, v, scope.engine);
+ if (roleIndex != -1) {
+ QVector<int> roles;
+ roles << roleIndex;
+ m_model->emitItemsChanged(m_elementIndex, 1, roles);
+ }
+}
+
+namespace QV4 {
+
+void ModelObject::put(Managed *m, String *name, const Value &value)
+{
+ ModelObject *that = static_cast<ModelObject*>(m);
+
+ ExecutionEngine *eng = that->engine();
+ const int elementIndex = that->d()->m_elementIndex;
+ const QString propName = name->toQString();
+ int roleIndex = that->d()->m_model->m_listModel->setExistingProperty(elementIndex, propName, value, eng);
if (roleIndex != -1) {
QVector<int> roles;
roles << roleIndex;
- m_obj->m_model->emitItemsChanged(m_obj->m_elementIndex, 1, roles);
+ that->d()->m_model->emitItemsChanged(elementIndex, 1, roles);
+ }
+
+ ModelNodeMetaObject *mo = ModelNodeMetaObject::get(that->object());
+ if (mo->initialized())
+ mo->emitPropertyNotification(name->toQString().toUtf8());
+}
+
+ReturnedValue ModelObject::get(const Managed *m, String *name, bool *hasProperty)
+{
+ const ModelObject *that = static_cast<const ModelObject*>(m);
+ const ListLayout::Role *role = that->d()->m_model->m_listModel->getExistingRole(name);
+ if (!role)
+ return QObjectWrapper::get(m, name, hasProperty);
+ if (hasProperty)
+ *hasProperty = true;
+ const int elementIndex = that->d()->m_elementIndex;
+ QVariant value = that->d()->m_model->data(elementIndex, role->index);
+ return that->engine()->fromVariant(value);
+}
+
+void ModelObject::advanceIterator(Managed *m, ObjectIterator *it, Value *name, uint *index, Property *p, PropertyAttributes *attributes)
+{
+ ModelObject *that = static_cast<ModelObject*>(m);
+ ExecutionEngine *v4 = that->engine();
+ name->setM(0);
+ *index = UINT_MAX;
+ if (it->arrayIndex < uint(that->d()->m_model->m_listModel->roleCount())) {
+ Scope scope(that->engine());
+ const ListLayout::Role &role = that->d()->m_model->m_listModel->getExistingRole(it->arrayIndex);
+ ++it->arrayIndex;
+ ScopedString roleName(scope, v4->newString(role.name));
+ name->setM(roleName->d());
+ *attributes = QV4::Attr_Data;
+ QVariant value = that->d()->m_model->data(that->d()->m_elementIndex, role.index);
+ p->value = v4->fromVariant(value);
+ return;
}
+ QV4::QObjectWrapper::advanceIterator(m, it, name, index, p, attributes);
}
+DEFINE_OBJECT_VTABLE(ModelObject);
+
+} // namespace QV4
+
DynamicRoleModelNode::DynamicRoleModelNode(QQmlListModel *owner, int uid) : m_owner(owner), m_uid(uid), m_meta(new DynamicRoleModelNodeMetaObject(this))
{
setNodeUpdatesEnabled(true);
DynamicRoleModelNode *object = m_modelObjects[index];
result = QV4::QObjectWrapper::wrap(scope.engine, object);
} else {
- ModelObject *object = m_listModel->getOrCreateModelObject(const_cast<QQmlListModel *>(this), index);
- result = QV4::QObjectWrapper::wrap(scope.engine, object);
+ QObject *object = m_listModel->getOrCreateModelObject(const_cast<QQmlListModel *>(this), index);
+ result = scope.engine->memoryManager->alloc<QV4::ModelObject>(scope.engine, object, const_cast<QQmlListModel *>(this), index);
}
}
friend class DynamicRoleModelNodeMetaObject;
};
-class ModelObject;
-
class ModelNodeMetaObject : public QQmlOpenMetaObject
{
public:
- ModelNodeMetaObject(ModelObject *object);
+ ModelNodeMetaObject(QObject *object, QQmlListModel *model, int elementIndex);
~ModelNodeMetaObject();
+ virtual QAbstractDynamicMetaObject *toDynamicMetaObject(QObject *object);
+
+ static ModelNodeMetaObject *get(QObject *obj);
+
bool m_enabled;
+ QQmlListModel *m_model;
+ int m_elementIndex;
+
+ void updateValues();
+ void updateValues(const QVector<int> &roles);
+
+ bool initialized() const { return m_initialized; }
protected:
void propertyWritten(int index);
private:
-
- ModelObject *m_obj;
-};
-
-class ModelObject : public QObject
-{
- Q_OBJECT
-public:
- ModelObject(QQmlListModel *model, int elementIndex);
-
+ using QQmlOpenMetaObject::setValue;
void setValue(const QByteArray &name, const QVariant &val, bool force)
{
if (force) {
- QVariant existingValue = m_meta->value(name);
+ QVariant existingValue = value(name);
if (existingValue.isValid()) {
- (*m_meta)[name] = QVariant();
+ (*this)[name] = QVariant();
}
}
- m_meta->setValue(name, val);
+ setValue(name, val);
}
- void setNodeUpdatesEnabled(bool enable)
- {
- m_meta->m_enabled = enable;
- }
+ void initialize();
+ bool m_initialized;
+};
- void updateValues();
- void updateValues(const QVector<int> &roles);
+namespace QV4 {
+
+namespace Heap {
+struct ModelObject : public QObjectWrapper {
+ ModelObject(QV4::ExecutionEngine *engine, QObject *object, QQmlListModel *model, int elementIndex)
+ : QObjectWrapper(engine, object)
+ , m_model(model)
+ , m_elementIndex(elementIndex)
+ {}
QQmlListModel *m_model;
int m_elementIndex;
+};
-private:
- ModelNodeMetaObject *m_meta;
+}
+
+struct ModelObject : public QObjectWrapper
+{
+ static void put(Managed *m, String *name, const Value& value);
+ static ReturnedValue get(const Managed *m, String *name, bool *hasProperty);
+ static void advanceIterator(Managed *m, ObjectIterator *it, Value *name, uint *index, Property *p, PropertyAttributes *attributes);
+
+ V4_OBJECT2(ModelObject, QObjectWrapper)
};
+} // namespace QV4
+
class ListLayout
{
public:
enum
{
- BLOCK_SIZE = 64 - sizeof(int) - sizeof(ListElement *) - sizeof(ModelObject *)
+ BLOCK_SIZE = 64 - sizeof(int) - sizeof(ListElement *) - sizeof(ModelNodeMetaObject *)
};
private:
int getUid() const { return uid; }
+ ModelNodeMetaObject *objectCache();
+
char data[BLOCK_SIZE];
ListElement *next;
int uid;
- ModelObject *m_objectCache;
+ QObject *m_objectCache;
friend class ListModel;
};
return m_layout->getExistingRole(index);
}
+ const ListLayout::Role *getExistingRole(QV4::String *key)
+ {
+ return m_layout->getExistingRole(key);
+ }
+
const ListLayout::Role &getOrCreateListRole(const QString &name)
{
return m_layout->getRoleOrCreate(name, ListLayout::Role::List);
static void sync(ListModel *src, ListModel *target, QHash<int, ListModel *> *srcModelHash);
- ModelObject *getOrCreateModelObject(QQmlListModel *model, int elementIndex);
+ QObject *getOrCreateModelObject(QQmlListModel *model, int elementIndex);
private:
QPODVector<ListElement *, 4> elements;