Qt 4.8
Public Functions | Public Variables | Properties | Friends | List of all members
NestedListModel Class Reference

#include <qdeclarativelistmodel_p_p.h>

Public Functions

void checkRoles () const
 
void clear ()
 
int count () const
 
QHash< int, QVariantdata (int index, const QList< int > &roles, bool *hasNested=0) const
 
QVariant data (int index, int role) const
 
QScriptValue get (int index) const
 
bool insert (int index, const QScriptValue &)
 
void move (int from, int to, int count)
 
 NestedListModel (QDeclarativeListModel *base)
 
void remove (int index)
 
QList< int > roles () const
 
void set (int index, const QScriptValue &, QList< int > *roles)
 
void setProperty (int index, const QString &property, const QVariant &value, QList< int > *roles)
 
QString toString (int role) const
 
QVariant valueForNode (ModelNode *, bool *hasNested=0) const
 
 ~NestedListModel ()
 

Public Variables

ModelNode_root
 
QDeclarativeListModelm_listModel
 
bool m_ownsRoot
 

Properties

bool _rolesOk
 
QStringList roleStrings
 

Friends

struct ModelNode
 

Detailed Description

Definition at line 175 of file qdeclarativelistmodel_p_p.h.

Constructors and Destructors

◆ NestedListModel()

NestedListModel::NestedListModel ( QDeclarativeListModel base)

Definition at line 1239 of file qdeclarativelistmodel.cpp.

1240  : _root(0), m_ownsRoot(false), m_listModel(base), _rolesOk(false)
1241 {
1242 }
QDeclarativeListModel * m_listModel

◆ ~NestedListModel()

NestedListModel::~NestedListModel ( )

Definition at line 1244 of file qdeclarativelistmodel.cpp.

1245 {
1246  if (m_ownsRoot)
1247  delete _root;
1248 }

Functions

◆ checkRoles()

void NestedListModel::checkRoles ( ) const

Definition at line 1437 of file qdeclarativelistmodel.cpp.

Referenced by ModelNode::changedProperty(), data(), roles(), and toString().

1438 {
1439  if (_rolesOk || !_root)
1440  return;
1441 
1442  for (int i = 0; i<_root->values.count(); ++i) {
1443  ModelNode *node = qvariant_cast<ModelNode *>(_root->values.at(i));
1444  if (node) {
1445  foreach (const QString &role, node->properties.keys()) {
1446  if (!roleStrings.contains(role))
1447  roleStrings.append(role);
1448  }
1449  }
1450  }
1451 
1452  _rolesOk = true;
1453 }
QHash< QString, ModelNode * > properties
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
The QString class provides a Unicode character string.
Definition: qstring.h:83
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
QList< QVariant > values
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
QBool contains(const QString &str, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the list contains the string str; otherwise returns false.
Definition: qstringlist.h:172
T qvariant_cast(const QVariant &)
Definition: qvariant.h:571
QList< Key > keys() const
Returns a list containing all the keys in the hash, in an arbitrary order.
Definition: qhash.h:648

◆ clear()

void NestedListModel::clear ( )

Definition at line 1342 of file qdeclarativelistmodel.cpp.

Referenced by QDeclarativeListModel::clear().

1343 {
1344  if (_root)
1345  _root->clear();
1346 }

◆ count()

int NestedListModel::count ( ) const

Definition at line 1336 of file qdeclarativelistmodel.cpp.

Referenced by QDeclarativeListModel::data(), QDeclarativeListModel::flatten(), get(), set(), and setProperty().

1337 {
1338  if (!_root) return 0;
1339  return _root->values.count();
1340 }
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
QList< QVariant > values

◆ data() [1/2]

QHash< int, QVariant > NestedListModel::data ( int  index,
const QList< int > &  roles,
bool *  hasNested = 0 
) const

Definition at line 1290 of file qdeclarativelistmodel.cpp.

Referenced by QDeclarativeListModel::data(), and QDeclarativeListModel::flatten().

1291 {
1292  Q_ASSERT(_root && index >= 0 && index < _root->values.count());
1293  checkRoles();
1295 
1297  if (!node)
1298  return rv;
1299 
1300  for (int ii = 0; ii < roles.count(); ++ii) {
1301  const QString &roleString = roleStrings.at(roles.at(ii));
1302 
1304  if (iter != node->properties.end()) {
1305  ModelNode *row = *iter;
1306  rv.insert(roles.at(ii), valueForNode(row, hasNested));
1307  }
1308  }
1309 
1310  return rv;
1311 }
The QHash::const_iterator class provides an STL-style const iterator for QHash and QMultiHash...
Definition: qhash.h:395
QHash< QString, ModelNode * > properties
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qdatastream.h:66
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition: qhash.h:753
QList< QVariant > values
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
QVariant valueForNode(ModelNode *, bool *hasNested=0) const
quint16 values[128]
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the hash...
Definition: qhash.h:467
T qvariant_cast(const QVariant &)
Definition: qvariant.h:571
quint16 index
iterator find(const Key &key)
Returns an iterator pointing to the item with the key in the hash.
Definition: qhash.h:865

◆ data() [2/2]

QVariant NestedListModel::data ( int  index,
int  role 
) const

Definition at line 1313 of file qdeclarativelistmodel.cpp.

1314 {
1315  Q_ASSERT(_root && index >= 0 && index < _root->values.count());
1316  checkRoles();
1317  QVariant rv;
1318  if (roleStrings.count() < role)
1319  return rv;
1320 
1322  if (!node)
1323  return rv;
1324 
1325  const QString &roleString = roleStrings.at(role);
1326 
1328  if (iter != node->properties.end()) {
1329  ModelNode *row = *iter;
1330  rv = valueForNode(row);
1331  }
1332 
1333  return rv;
1334 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
The QHash::const_iterator class provides an STL-style const iterator for QHash and QMultiHash...
Definition: qhash.h:395
QHash< QString, ModelNode * > properties
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QList< QVariant > values
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
QVariant valueForNode(ModelNode *, bool *hasNested=0) const
quint16 values[128]
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the hash...
Definition: qhash.h:467
T qvariant_cast(const QVariant &)
Definition: qvariant.h:571
quint16 index
iterator find(const Key &key)
Returns an iterator pointing to the item with the key in the hash.
Definition: qhash.h:865

◆ get()

QScriptValue NestedListModel::get ( int  index) const

Definition at line 1379 of file qdeclarativelistmodel.cpp.

Referenced by QDeclarativeListModel::get().

1380 {
1382  if (!eng)
1383  return 0;
1384 
1385  if (index < 0 || index >= count()) {
1387  if (seng)
1388  return seng->undefinedValue();
1389  return 0;
1390  }
1391 
1393  if (!node)
1394  return 0;
1395 
1396  return QDeclarativeEnginePrivate::qmlScriptObject(node->object(this), eng);
1397 }
static QScriptEngine * getScriptEngine(QDeclarativeEngine *e)
QDeclarativeListModel * m_listModel
The QScriptEngine class provides an environment for evaluating Qt Script code.
QList< QVariant > values
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
static QScriptValue qmlScriptObject(QObject *, QDeclarativeEngine *)
Creates a QScriptValue allowing you to use object in QML script.
ModelObject * object(const NestedListModel *model)
Q_DECLARATIVE_EXPORT QDeclarativeEngine * qmlEngine(const QObject *)
The QDeclarativeEngine class provides an environment for instantiating QML components.
T qvariant_cast(const QVariant &)
Definition: qvariant.h:571
quint16 index
QScriptValue undefinedValue()
Returns a QScriptValue of the primitive type Undefined.

◆ insert()

bool NestedListModel::insert ( int  index,
const QScriptValue valuemap 
)

Definition at line 1358 of file qdeclarativelistmodel.cpp.

Referenced by QDeclarativeListModel::insert().

1359 {
1360  if (!_root) {
1361  _root = new ModelNode(this);
1362  m_ownsRoot = true;
1363  }
1364 
1365  ModelNode *mn = new ModelNode(this);
1366  mn->listIndex = index;
1367  mn->setObjectValue(valuemap);
1369  return true;
1370 }
void insert(int i, const T &t)
Inserts value at index position i in the list.
Definition: qlist.h:575
bool setObjectValue(const QScriptValue &valuemap, bool writeToCache=true)
QList< QVariant > values
static QVariant fromValue(const T &value)
Returns a QVariant containing a copy of value.
Definition: qvariant.h:336
quint16 index

◆ move()

void NestedListModel::move ( int  from,
int  to,
int  count 
)

Definition at line 1372 of file qdeclarativelistmodel.cpp.

Referenced by QDeclarativeListModel::move().

1373 {
1374  if (!_root)
1375  return;
1376  qdeclarativelistmodel_move<QVariantList>(from, to, n, &_root->values);
1377 }
QList< QVariant > values

◆ remove()

void NestedListModel::remove ( int  index)

Definition at line 1348 of file qdeclarativelistmodel.cpp.

Referenced by QDeclarativeListModel::remove().

1349 {
1350  if (!_root)
1351  return;
1354  if (node)
1355  delete node;
1356 }
QList< QVariant > values
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
T qvariant_cast(const QVariant &)
Definition: qvariant.h:571
quint16 index
void removeAt(int i)
Removes the item at index position i.
Definition: qlist.h:480

◆ roles()

QList< int > NestedListModel::roles ( ) const

Definition at line 1455 of file qdeclarativelistmodel.cpp.

Referenced by ModelNode::changedProperty(), QDeclarativeListModel::flatten(), and QDeclarativeListModel::roles().

1456 {
1457  checkRoles();
1458  QList<int> rv;
1459  for (int ii = 0; ii < roleStrings.count(); ++ii)
1460  rv << ii;
1461  return rv;
1462 }
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891

◆ set()

void NestedListModel::set ( int  index,
const QScriptValue valuemap,
QList< int > *  roles 
)

Definition at line 1399 of file qdeclarativelistmodel.cpp.

Referenced by QDeclarativeListModel::set().

1400 {
1401  Q_ASSERT(index >=0 && index < count());
1402 
1404  bool emitItemsChanged = node->setObjectValue(valuemap);
1405  if (!emitItemsChanged)
1406  return;
1407 
1408  QScriptValueIterator it(valuemap);
1409  while (it.hasNext()) {
1410  it.next();
1411  int r = roleStrings.indexOf(it.name());
1412  if (r < 0) {
1413  r = roleStrings.count();
1414  roleStrings << it.name();
1415  }
1416  roles->append(r);
1417  }
1418 }
#define it(className, varName)
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
bool setObjectValue(const QScriptValue &valuemap, bool writeToCache=true)
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
int indexOf(const QRegExp &rx, int from=0) const
Returns the index position of the first exact match of rx in the list, searching forward from index p...
Definition: qstringlist.h:195
QList< QVariant > values
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QScriptValueIterator class provides a Java-style iterator for QScriptValue.
T qvariant_cast(const QVariant &)
Definition: qvariant.h:571
quint16 index

◆ setProperty()

void NestedListModel::setProperty ( int  index,
const QString property,
const QVariant value,
QList< int > *  roles 
)

Definition at line 1420 of file qdeclarativelistmodel.cpp.

Referenced by QDeclarativeListModel::setProperty().

1421 {
1422  Q_ASSERT(index >=0 && index < count());
1423 
1425  bool emitItemsChanged = node->setProperty(property, value);
1426  if (!emitItemsChanged)
1427  return;
1428 
1429  int r = roleStrings.indexOf(property);
1430  if (r < 0) {
1431  r = roleStrings.count();
1432  roleStrings << property;
1433  }
1434  roles->append(r);
1435 }
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
int indexOf(const QRegExp &rx, int from=0) const
Returns the index position of the first exact match of rx in the list, searching forward from index p...
Definition: qstringlist.h:195
QList< QVariant > values
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
const char * property
Definition: qwizard.cpp:138
T qvariant_cast(const QVariant &)
Definition: qvariant.h:571
quint16 index
bool setProperty(const QString &prop, const QVariant &val)

◆ toString()

QString NestedListModel::toString ( int  role) const

Definition at line 1464 of file qdeclarativelistmodel.cpp.

Referenced by QDeclarativeListModel::flatten(), and QDeclarativeListModel::toString().

1465 {
1466  checkRoles();
1467  if (role < roleStrings.count())
1468  return roleStrings.at(role);
1469  else
1470  return QString();
1471 }
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
The QString class provides a Unicode character string.
Definition: qstring.h:83
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468

◆ valueForNode()

QVariant NestedListModel::valueForNode ( ModelNode node,
bool *  hasNested = 0 
) const

Definition at line 1250 of file qdeclarativelistmodel.cpp.

Referenced by data(), and ModelNode::object().

1251 {
1252  QObject *rv = 0;
1253  if (hasNested)
1254  *hasNested = false;
1255 
1256  if (node->isArray) {
1257  // List
1258  rv = node->model(this);
1259  if (hasNested)
1260  *hasNested = true;
1261  } else {
1262  if (!node->properties.isEmpty()) {
1263  // Object
1264  rv = node->object(this);
1265  } else if (node->values.count() == 0) {
1266  // Invalid
1267  return QVariant();
1268  } else if (node->values.count() == 1) {
1269  // Value
1270  QVariant &var = node->values[0];
1271  ModelNode *valueNode = qvariant_cast<ModelNode *>(var);
1272  if (valueNode) {
1273  if (!valueNode->properties.isEmpty())
1274  rv = valueNode->object(this);
1275  else
1276  rv = valueNode->model(this);
1277  } else {
1278  return var;
1279  }
1280  }
1281  }
1282 
1283  if (rv) {
1284  return QVariant::fromValue(rv);
1285  } else {
1286  return QVariant();
1287  }
1288 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QHash< QString, ModelNode * > properties
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
QList< QVariant > values
bool isEmpty() const
Returns true if the hash contains no items; otherwise returns false.
Definition: qhash.h:297
static QVariant fromValue(const T &value)
Returns a QVariant containing a copy of value.
Definition: qvariant.h:336
QDeclarativeListModel * model(const NestedListModel *model)
ModelObject * object(const NestedListModel *model)
T qvariant_cast(const QVariant &)
Definition: qvariant.h:571

Friends and Related Functions

◆ ModelNode

friend struct ModelNode
friend

Definition at line 204 of file qdeclarativelistmodel_p_p.h.

Referenced by insert().

Properties

◆ _rolesOk

bool NestedListModel::_rolesOk
mutableprivate

Definition at line 206 of file qdeclarativelistmodel_p_p.h.

Referenced by checkRoles().

◆ _root

ModelNode* NestedListModel::_root

◆ m_listModel

QDeclarativeListModel* NestedListModel::m_listModel

◆ m_ownsRoot

bool NestedListModel::m_ownsRoot

◆ roleStrings

QStringList NestedListModel::roleStrings
mutableprivate

The documentation for this class was generated from the following files: