42 #include "private/qdeclarativevisualitemmodel_p.h" 64 #include <QtCore/qdebug.h> 66 #include <private/qobject_p.h> 99 emit q->countChanged();
104 emit q->childrenChanged();
188 d->children_count, d->children_at);
202 return d->children.count();
221 int idx =
d->indexOf(item);
223 if (
d->children[idx].deref()) {
245 if (index < 0 || index >=
d->children.count())
253 return d->indexOf(item);
295 if (m_listModelInterface) {
296 m_roles = m_listModelInterface->
roles();
297 for (
int ii = 0; ii < m_roles.
count(); ++ii)
298 m_roleNames.
insert(m_listModelInterface->
toString(m_roles.
at(ii)).toUtf8(), m_roles.
at(ii));
299 }
else if (m_abstractItemModel) {
306 m_roleNames.
insert(
"hasModelChildren", -1);
307 }
else if (m_listAccessor) {
308 m_roleNames.
insert(
"modelData", 0);
311 int count =
object->metaObject()->propertyCount();
312 for (
int ii = 1; ii <
count; ++ii) {
313 const QMetaProperty &prop =
object->metaObject()->property(ii);
325 if (!m_metaDataCreated) {
327 if (m_roleNames.
count()) {
329 while (it != m_roleNames.
end()) {
330 int propId = m_delegateDataType->createProperty(it.
key()) - m_delegateDataType->propertyOffset();
331 m_roleToPropId.
insert(*it, propId);
335 if (m_roles.
count() == 1)
336 m_modelDataPropId = m_delegateDataType->createProperty(
"modelData") - m_delegateDataType->propertyOffset();
337 m_metaDataCreated =
true;
370 for (; it !=
end(); ++
it) {
372 if (objRef.
obj == obj) {
373 if (--objRef.
ref == 0) {
385 if (m_visualItemModel)
386 return m_visualItemModel->
count();
387 if (m_listModelInterface)
388 return m_listModelInterface->
count();
389 if (m_abstractItemModel)
390 return m_abstractItemModel->
rowCount(m_root);
392 return m_listAccessor->count();
400 friend class QDeclarativeVisualItemParts;
426 virtual int createProperty(
const char *,
const char *);
443 int propForRole(
int)
const;
449 void setValue(
int,
const QVariant &);
451 return m_meta->hasValue(
id);
454 void ensureProperties();
478 m_meta->setValue(
id, val);
496 if (
qstrcmp(name,
"modelData") == 0)
513 if (propName ==
"modelData") {
530 }
else if (model->
m_roles.
count() == 1 && propName ==
"modelData") {
538 if (propName ==
"hasModelChildren") {
544 }
else if (model->
m_roles.
count() == 1 && propName ==
"modelData") {
556 : m_index(index), m_model(model),
629 :
QObject(parent), model(parent)
635 : m_listModelInterface(0), m_abstractItemModel(0), m_visualItemModel(0), m_delegate(0)
636 , m_context(ctxt), m_modelDataPropId(-1), m_parts(0), m_delegateDataType(0), m_metaDataCreated(false)
637 , m_metaDataCacheable(false), m_delegateValidated(false), m_completePending(false), m_listAccessor(0)
686 if (
d->m_listAccessor)
687 delete d->m_listAccessor;
688 if (
d->m_delegateDataType)
689 d->m_delegateDataType->release();
711 return d->m_modelVariant;
717 delete d->m_listAccessor;
718 d->m_listAccessor = 0;
719 d->m_modelVariant =
model;
720 if (
d->m_listModelInterface) {
730 d->m_listModelInterface = 0;
731 }
else if (
d->m_abstractItemModel) {
742 d->m_abstractItemModel = 0;
743 }
else if (
d->m_visualItemModel) {
754 d->m_visualItemModel = 0;
758 d->m_roleNames.clear();
759 if (
d->m_delegateDataType)
760 d->m_delegateDataType->release();
761 d->m_metaDataCreated = 0;
762 d->m_metaDataCacheable =
false;
766 if (
object && (
d->m_listModelInterface = qobject_cast<QListModelInterface *>(
object))) {
775 d->m_metaDataCacheable =
true;
776 if (
d->m_delegate &&
d->m_listModelInterface->count())
779 }
else if (
object && (
d->m_abstractItemModel = qobject_cast<QAbstractItemModel *>(
object))) {
790 d->m_metaDataCacheable =
true;
791 if (
d->m_abstractItemModel->canFetchMore(
d->m_root))
792 d->m_abstractItemModel->fetchMore(
d->m_root);
795 if ((
d->m_visualItemModel = qvariant_cast<QDeclarativeVisualDataModel *>(model))) {
809 d->m_listAccessor->setList(model,
d->m_context?
d->m_context->engine():
qmlEngine(
this));
811 d->m_metaDataCacheable =
true;
812 if (
d->m_delegate &&
d->modelCount()) {
831 if (
d->m_visualItemModel)
832 return d->m_visualItemModel->delegate();
833 return d->m_delegate;
839 bool wasValid =
d->m_delegate != 0;
841 d->m_delegateValidated =
false;
842 if (!wasValid &&
d->modelCount() &&
d->m_delegate) {
846 if (wasValid && !
d->m_delegate &&
d->modelCount()) {
894 if (
d->m_root != modelIndex) {
895 int oldCount =
d->modelCount();
897 if (
d->m_abstractItemModel &&
d->m_abstractItemModel->canFetchMore(modelIndex))
898 d->m_abstractItemModel->fetchMore(modelIndex);
899 int newCount =
d->modelCount();
900 if (
d->m_delegate && oldCount)
902 if (
d->m_delegate && newCount)
904 if (newCount != oldCount)
929 if (
d->m_abstractItemModel)
952 if (
d->m_abstractItemModel)
972 if (
d->m_visualItemModel)
973 return d->m_visualItemModel->count();
976 return d->modelCount();
982 if (
d->m_visualItemModel)
983 return d->m_visualItemModel->item(index,
d->m_part.toUtf8(), complete);
993 if (
d->m_visualItemModel)
994 return d->m_visualItemModel->release(item);
996 ReleaseFlags
stat = 0;
998 bool inPackage =
false;
1001 if (it !=
d->m_packaged.end()) {
1003 d->m_packaged.erase(it);
1004 if (
d->m_packaged.contains(item))
1010 if (
d->m_cache.releaseItem(obj)) {
1026 }
else if (!inPackage) {
1074 if (
d->m_visualItemModel)
1075 return d->m_visualItemModel->item(index, viewId, complete);
1077 if (
d->modelCount() <= 0 || !
d->m_delegate)
1079 QObject *nobj =
d->m_cache.getItem(index);
1080 bool needComplete =
false;
1086 if ((!
d->m_listModelInterface || !
d->m_abstractItemModel) &&
d->m_listAccessor
1095 d->m_completePending =
false;
1096 nobj =
d->m_delegate->beginCreate(ctxt);
1098 d->m_delegate->completeCreate();
1100 d->m_completePending =
true;
1101 needComplete =
true;
1106 d->m_cache.insertItem(index, nobj);
1112 qmlInfo(
this,
d->m_delegate->errors()) <<
"Error creating delegate";
1122 d->m_packaged.insertMulti(item, package);
1127 d->m_delegate->completeCreate();
1128 d->m_cache.releaseItem(nobj);
1129 if (!
d->m_delegateValidated) {
1131 d->m_delegateValidated =
true;
1134 if (
d->modelCount()-1 == index &&
d->m_abstractItemModel &&
d->m_abstractItemModel->canFetchMore(
d->m_root))
1135 d->m_abstractItemModel->fetchMore(
d->m_root);
1143 if (
d->m_visualItemModel)
1144 return d->m_visualItemModel->completePending();
1145 return d->m_completePending;
1151 if (
d->m_visualItemModel) {
1152 d->m_visualItemModel->completeItem();
1156 d->m_delegate->completeCreate();
1157 d->m_completePending =
false;
1163 if (
d->m_visualItemModel)
1164 return d->m_visualItemModel->stringValue(index, name);
1166 if ((!
d->m_listModelInterface || !
d->m_abstractItemModel) &&
d->m_listAccessor) {
1171 if ((!
d->m_listModelInterface && !
d->m_abstractItemModel) || !
d->m_delegate)
1176 bool tempData =
false;
1178 if (
QObject *nobj =
d->m_cache.item(index))
1179 data =
d->data(nobj);
1190 void *args[] = { &val, 0 };
1192 }
else if (prop->
propType == qMetaTypeId<QVariant>()) {
1194 void *args[] = { &v, 0 };
1221 d->watchedRoles = roles;
1222 d->watchedRoleIds.clear();
1229 bool changed =
false;
1230 if (!
d->watchedRoles.isEmpty() &&
d->watchedRoleIds.isEmpty()) {
1232 if (
d->m_roleNames.contains(r))
1233 d->watchedRoleIds <<
d->m_roleNames.value(r);
1238 iter !=
d->m_cache.end(); ++iter) {
1239 const int idx = iter.key();
1241 if (idx >= index && idx < index+count) {
1244 for (
int roleIdx = 0; roleIdx < roles.
count(); ++roleIdx) {
1245 int role = roles.
at(roleIdx);
1246 if (!changed && !
d->watchedRoleIds.isEmpty() &&
d->watchedRoleIds.contains(role))
1251 if (
d->m_listModelInterface) {
1252 data->
setValue(propId,
d->m_listModelInterface->data(idx, role));
1253 }
else if (
d->m_abstractItemModel) {
1254 QModelIndex index =
d->m_abstractItemModel->index(idx, 0,
d->m_root);
1255 data->
setValue(propId,
d->m_abstractItemModel->data(index, role));
1260 if (
d->m_listModelInterface)
1261 roleName =
d->m_listModelInterface->toString(role);
1262 else if (
d->m_abstractItemModel)
1264 qmlInfo(
this) <<
"Changing role not present in item: " << roleName;
1267 if (
d->m_roles.count() == 1) {
1271 int role =
d->m_roles.at(0);
1272 if (
d->m_listModelInterface) {
1273 data->
setValue(propId,
d->m_listModelInterface->data(idx, role));
1274 }
else if (
d->m_abstractItemModel) {
1275 QModelIndex index =
d->m_abstractItemModel->index(idx, 0,
d->m_root);
1276 data->
setValue(propId,
d->m_abstractItemModel->data(index, role));
1294 iter !=
d->m_cache.end(); ) {
1296 if (iter.key() >=
index) {
1298 int index = iter.key() +
count;
1299 iter =
d->m_cache.erase(iter);
1301 items.
insert(index, objRef);
1309 d->m_cache.unite(items);
1323 iter !=
d->m_cache.end(); ) {
1324 if (iter.key() >= index && iter.key() < index +
count) {
1326 iter =
d->m_cache.erase(iter);
1330 }
else if (iter.key() >= index +
count) {
1332 int index = iter.key() -
count;
1333 iter =
d->m_cache.erase(iter);
1334 items.
insert(index, objRef);
1342 d->m_cache.unite(items);
1353 iter !=
d->m_cache.end(); ) {
1355 if (iter.key() >= from && iter.key() < from +
count) {
1357 int index = iter.key() - from + to;
1358 iter =
d->m_cache.erase(iter);
1360 items.
insert(index, objRef);
1369 iter !=
d->m_cache.end(); ) {
1372 if (iter.key() >=
qMin(from,to) && iter.key() <
qMax(from+count,to+count)) {
1374 int index = iter.key() +
diff;
1375 iter =
d->m_cache.erase(iter);
1377 items.
insert(index, objRef);
1385 d->m_cache.unite(items);
1393 if (parent ==
d->m_root)
1400 if (parent ==
d->m_root)
1407 const int count = sourceEnd - sourceStart + 1;
1408 if (destinationParent ==
d->m_root && sourceParent ==
d->m_root) {
1409 _q_itemsMoved(sourceStart, sourceStart > destinationRow ? destinationRow : destinationRow-count, count);
1410 }
else if (sourceParent ==
d->m_root) {
1412 }
else if (destinationParent ==
d->m_root) {
1420 if (begin.
parent() ==
d->m_root)
1436 if (
d->m_abstractItemModel &&
d->m_abstractItemModel->canFetchMore(
d->m_root))
1437 d->m_abstractItemModel->fetchMore(
d->m_root);
1456 #include <qdeclarativevisualitemmodel.moc> The QVariant class acts like a union for the most common Qt data types.
virtual QVariant data(int index, int role) const =0
Returns the data at the given index for the specified roles.
QDeclarativeContextData * context
QDeclarativeVisualDataModelData * data(QObject *item)
QDeclarativePropertyCache * propertyCache
void itemsMoved(int from, int to, int count)
QDeclarativeComponent * m_delegate
void destroyingItem(QDeclarativeItem *item)
QDeclarativeComponent * delegate() const
static QDeclarativeData * get(const QObject *object, bool create=false)
The QHash::const_iterator class provides an STL-style const iterator for QHash and QMultiHash...
void itemsInserted(int index, int count)
void _q_itemsRemoved(int index, int count)
QDeclarativeListAccessor * m_listAccessor
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Item(QDeclarativeItem *i)
#define QT_END_NAMESPACE
This macro expands to.
void _q_rowsInserted(const QModelIndex &, int, int)
QDeclarativeVisualDataModel * model
#define QML_DECLARE_TYPE(TYPE)
void _q_rowsMoved(const QModelIndex &, int, int, const QModelIndex &, int)
QDeclarativeGuard< QDeclarativeContext > m_context
#define it(className, varName)
int modelDataPropertyId() const
virtual int count() const =0
Returns the number of data entries in the model.
~QDeclarativeVisualDataModelData()
QByteArray toUtf8() const Q_REQUIRED_RESULT
Returns a UTF-8 representation of the string as a QByteArray.
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const =0
Returns the number of rows under the given parent.
int indexOf(QDeclarativeItem *item, QObject *objectContext) const
The QByteArray class provides an array of bytes.
VDMDelegateDataType * m_delegateDataType
QString toString() const
Returns the variant as a QString if the variant has type() String , Bool , ByteArray ...
QDeclarativeVisualDataModelParts(QDeclarativeVisualDataModel *parent)
static const QMetaObject staticMetaObject
This variable stores the meta-object for the class.
ReleaseFlags release(QDeclarativeItem *item)
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
int count(const T &t) const
Returns the number of occurrences of value in the list.
void _q_itemsMoved(int from, int to, int count)
void _q_createdPackage(int index, QDeclarativePackage *package)
virtual QString toString(int role) const =0
Returns a string description of the specified role.
virtual int indexOf(QDeclarativeItem *item, QObject *objectContext) const
The QString class provides a Unicode character string.
T * qobject_cast(QObject *object)
The QHash class is a template class that provides a hash-table-based dictionary.
The QObject class is the base class of all Qt objects.
Q_DECLARATIVE_EXPORT QDeclarativeContext * qmlContext(const QObject *)
virtual bool completePending() const
static QObjectPrivate * get(QObject *o)
QModelIndex parent() const
Returns the parent of the model index, or QModelIndex() if it has no parent.
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
static QDeclarativeVisualDataModelPrivate * get(QDeclarativeVisualDataModel *m)
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
void setParent(QObject *)
Makes the object a child of parent.
QHash< QByteArray, int > m_roleNames
QDeclarativeGuard< QDeclarativeVisualDataModel > m_visualItemModel
int toInt(bool *ok=0) const
Returns the variant as an int if the variant has type() Int , Bool , ByteArray , Char ...
Q_INVOKABLE QVariant parentModelIndex() const
QDeclarativeVisualDataModel()
virtual ReleaseFlags release(QDeclarativeItem *item)
void setContextObject(QObject *)
Set the context object.
virtual QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const =0
Returns the index of the item in the model specified by the given row, column and parent index...
static QDeclarativeItem * children_at(QDeclarativeListProperty< QDeclarativeItem > *prop, int index)
static QString toString(Register *reg, int type, bool *ok=0)
void append(const T &t)
Inserts value at the end of the list.
QDeclarativeItem * item(int index, bool complete=true)
Q_INVOKABLE QVariant modelIndex(int idx) const
#define QT_BEGIN_NAMESPACE
This macro expands to.
VDMDelegateDataType(const QMetaObject *base, QDeclarativeEngine *engine)
int indexOf(QDeclarativeItem *item) const
bool completePending() const
The QDeclarativeComponent class encapsulates a QML component definition.
The QDeclarativeItem class provides the most basic of all visual items in QML.
void createdPackage(int index, QDeclarativePackage *package)
virtual bool hasChildren(const QModelIndex &parent=QModelIndex()) const
Returns true if parent has any children; otherwise returns false.
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
void removeItem(QGraphicsItem *item)
Removes the item item and all its children from the scene.
virtual int count() const
int row() const
Returns the row this model index refers to.
const T & at(int i) const
Returns the item at index position i in the list.
QList< int > watchedRoleIds
Data * property(const QScriptDeclarativeClass::Identifier &id) const
static QString fromUtf8(const char *, int size=-1)
Returns a QString initialized with the first size bytes of the UTF-8 string str.
bool isEmpty() const
Returns true if the hash contains no items; otherwise returns false.
static const char * data(const QByteArray &arr)
QDeclarativeGuard< QAbstractItemModel > m_abstractItemModel
T findChild(const QString &aName=QString()) const
Returns the child of this object that can be cast into type T and that is called name, or 0 if there is no such object.
void _q_dataChanged(const QModelIndex &, const QModelIndex &)
static void children_append(QDeclarativeListProperty< QDeclarativeItem > *prop, QDeclarativeItem *item)
The QListModelInterface class can be subclassed to provide C++ models to QDeclarativeGraphics Views...
QVariant contextProperty(const QString &) const
Returns the value of the name property for this context as a QVariant.
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const =0
Returns the data stored under the given role for the item referred to by the index.
QObject * part(const QString &=QString())
friend class QDeclarativeVisualDataModelPartsMetaObject
static QVariant fromValue(const T &value)
Returns a QVariant containing a copy of value.
QObject * item(int index)
int propForRole(int) const
virtual QString stringValue(int index, const QString &role)
QAbstractDeclarativeData * declarativeData
virtual bool isValid() const
static QHash< QObject *, QDeclarativeVisualItemModelAttached * > attachedProperties
bool hasValue(int id) const
virtual QDeclarativeItem * item(int index, bool complete=true)
static QDeclarativeVisualItemModelAttached * qmlAttachedProperties(QObject *obj)
void setContextProperty(const QString &, QObject *)
Set the value of the name property on this context.
T & first()
Returns a reference to the first item in the list.
void emitChildrenChanged()
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
Disconnects signal in object sender from method in object receiver.
QDeclarativeVisualItemModelPrivate()
QDeclarativeVisualItemModel(QObject *parent=0)
void setRootIndex(const QVariant &root)
const QHash< int, QByteArray > & roleNames() const
Returns the model's role names.
#define Q_DECLARE_PUBLIC(Class)
QDeclarativeGuard< QDeclarativeVisualDataModel > m_model
static int children_count(QDeclarativeListProperty< QDeclarativeItem > *prop)
virtual void setWatchedRoles(QList< QByteArray > roles)
QHash< QObject *, QDeclarativePackage * > m_packaged
QDeclarativeVisualDataModelDataMetaObject * m_meta
Q_DECLARATIVE_EXPORT QDeclarativeEngine * qmlEngine(const QObject *)
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the hash...
void setValue(int, const QVariant &)
QDeclarativeGuard< QListModelInterface > m_listModelInterface
The QDeclarativeEngine class provides an environment for instantiating QML components.
void destroyingPackage(QDeclarativePackage *package)
void _q_itemsChanged(int, int, const QList< int > &)
void QDeclarative_setParent_noEvent(QObject *object, QObject *parent)
Makes the object a child of parent.
virtual QList< int > roles() const =0
Returns the list of roles for which the list model interface provides data.
const Key key(const T &value) const
Returns the first key mapped to value.
QObject * parent() const
Returns a pointer to the parent object.
T & last()
Returns a reference to the last item in the list.
The QHash::iterator class provides an STL-style non-const iterator for QHash and QMultiHash.
The QModelIndex class is used to locate data in a data model.
if(void) toggleToolbarShown
void _q_rowsRemoved(const QModelIndex &, int, int)
iterator begin()
Returns an STL-style iterator pointing to the first item in the hash.
QDeclarativeListProperty< QDeclarativeItem > children()
static const char * get(QDBusError::ErrorType code)
T qvariant_cast(const QVariant &)
static void setIndex(int *index, int candidate, int min, int max, bool isIncreasing)
The QDeclarativeContext class defines a context within a QML engine.
QDeclarativeVisualDataModelParts * m_parts
virtual void completeItem()
bool releaseItem(QObject *obj)
QVariant property(const char *name) const
Returns the value of the object's name property.
int count(const Key &key) const
Returns the number of items associated with the key.
void setModel(const QVariant &)
int qstrcmp(const QByteArray &str1, const char *str2)
QGraphicsScene * scene() const
Returns the current scene for the item, or 0 if the item is not stored in a scene.
void setDelegate(QDeclarativeComponent *)
void _q_destroyingPackage(QDeclarativePackage *package)
QHash< int, int > m_roleToPropId
static QDeclarativeVisualItemModelAttached * properties(QObject *obj)
static QDeclarativeContext * contextForObject(const QObject *)
Returns the QDeclarativeContext for the object, or 0 if no context has been set.
iterator find(const Key &key)
Returns an iterator pointing to the item with the key in the hash.
void itemsChanged(int index, int count)
iterator insertMulti(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
QDeclarativeInfo qmlInfo(const QObject *me)
T value() const
Returns the stored value converted to the template type T.
virtual ~QDeclarativeVisualDataModel()
QDeclarativeVisualDataModelData(int index, QDeclarativeVisualDataModel *model)
virtual QString stringValue(int index, const QString &role)
static const KeyPair *const end
void _q_itemsInserted(int index, int count)
QObject * getItem(int index)
void setPart(const QString &)
void insertItem(int index, QObject *obj)
void createdItem(int index, QDeclarativeItem *item)
void deleteLater()
Schedules this object for deletion.
void propertyCreated(int, QMetaPropertyBuilder &prop)
QVariant rootIndex() const
void itemsRemoved(int index, int count)
virtual const QMetaObject * metaObject() const
Returns a pointer to the meta-object of this object.
QDeclarativeVisualDataModelPrivate(QDeclarativeContext *)
QList< QByteArray > watchedRoles
ObjectRef(QObject *object=0)