Qt 4.8
Classes | Public Functions | Private Functions | Properties | List of all members
QDeclarativeListModelParser Class Reference

#include <qdeclarativelistmodel_p.h>

Inheritance diagram for QDeclarativeListModelParser:
QDeclarativeCustomParser

Classes

struct  ListInstruction
 
struct  ListModelData
 

Public Functions

QByteArray compile (const QList< QDeclarativeCustomParserProperty > &)
 
void setCustomData (QObject *, const QByteArray &)
 
- Public Functions inherited from QDeclarativeCustomParser
void clearErrors ()
 
QList< QDeclarativeErrorerrors () const
 
Flags flags () const
 
 QDeclarativeCustomParser ()
 
 QDeclarativeCustomParser (Flags f)
 
virtual ~QDeclarativeCustomParser ()
 

Private Functions

bool compileProperty (const QDeclarativeCustomParserProperty &prop, QList< ListInstruction > &instr, QByteArray &data)
 
bool definesEmptyList (const QString &)
 

Properties

QByteArray listElementTypeName
 

Additional Inherited Members

- Public Types inherited from QDeclarativeCustomParser
enum  Flag { NoFlag = 0x00000000, AcceptsAttachedProperties = 0x00000001 }
 
- Protected Functions inherited from QDeclarativeCustomParser
void error (const QString &description)
 Reports an error with the given description. More...
 
void error (const QDeclarativeCustomParserProperty &, const QString &description)
 Reports an error in parsing prop, with the given description. More...
 
void error (const QDeclarativeCustomParserNode &, const QString &description)
 Reports an error in parsing node, with the given description. More...
 
int evaluateEnum (const QByteArray &) const
 If script is a simply enum expression (eg. More...
 
const QMetaObjectresolveType (const QByteArray &) const
 Resolves name to a type, or 0 if it is not a type. More...
 
QDeclarativeBinding::Identifier rewriteBinding (const QString &, const QByteArray &)
 Rewrites expression and returns an identifier that can be used to construct the binding later. More...
 

Detailed Description

Definition at line 125 of file qdeclarativelistmodel_p.h.

Functions

◆ compile()

QByteArray QDeclarativeListModelParser::compile ( const QList< QDeclarativeCustomParserProperty > &  customProps)
virtual

Implements QDeclarativeCustomParser.

Definition at line 781 of file qdeclarativelistmodel.cpp.

782 {
785  listElementTypeName = QByteArray(); // unknown
786 
787  for(int ii = 0; ii < customProps.count(); ++ii) {
788  const QDeclarativeCustomParserProperty &prop = customProps.at(ii);
789  if(!prop.name().isEmpty()) { // isn't default property
790  error(prop, QDeclarativeListModel::tr("ListModel: undefined property '%1'").arg(QString::fromUtf8(prop.name())));
791  return QByteArray();
792  }
793 
794  if(!compileProperty(prop, instr, data)) {
795  return QByteArray();
796  }
797  }
798 
799  int size = sizeof(ListModelData) +
800  instr.count() * sizeof(ListInstruction) +
801  data.count();
802 
803  QByteArray rv;
804  rv.resize(size);
805 
806  ListModelData *lmd = (ListModelData *)rv.data();
807  lmd->dataOffset = sizeof(ListModelData) +
808  instr.count() * sizeof(ListInstruction);
809  lmd->instrCount = instr.count();
810  for (int ii = 0; ii < instr.count(); ++ii)
811  lmd->instructions()[ii] = instr.at(ii);
812  ::memcpy(rv.data() + lmd->dataOffset, data.constData(), data.count());
813 
814  return rv;
815 }
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
void error(const QString &description)
Reports an error with the given description.
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
static QString fromUtf8(const char *, int size=-1)
Returns a QString initialized with the first size bytes of the UTF-8 string str.
Definition: qstring.cpp:4302
static const char * data(const QByteArray &arr)
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
bool compileProperty(const QDeclarativeCustomParserProperty &prop, QList< ListInstruction > &instr, QByteArray &data)
int count(char c) const
Returns the number of occurrences of character ch in the byte array.
void resize(int size)
Sets the size of the byte array to size bytes.
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
The QList class is a template class that provides lists.
Definition: qdatastream.h:62

◆ compileProperty()

bool QDeclarativeListModelParser::compileProperty ( const QDeclarativeCustomParserProperty prop,
QList< ListInstruction > &  instr,
QByteArray data 
)
private

Definition at line 670 of file qdeclarativelistmodel.cpp.

671 {
673  for(int ii = 0; ii < values.count(); ++ii) {
674  const QVariant &value = values.at(ii);
675 
676  if(value.userType() == qMetaTypeId<QDeclarativeCustomParserNode>()) {
679 
680  if (node.name() != listElementTypeName) {
681  const QMetaObject *mo = resolveType(node.name());
683  error(node, QDeclarativeListModel::tr("ListElement: cannot contain nested elements"));
684  return false;
685  }
686  listElementTypeName = node.name(); // cache right name for next time
687  }
688 
689  {
690  ListInstruction li;
691  li.type = ListInstruction::Push;
692  li.dataIdx = -1;
693  instr << li;
694  }
695 
697  for(int jj = 0; jj < props.count(); ++jj) {
698  const QDeclarativeCustomParserProperty &nodeProp = props.at(jj);
699  if (nodeProp.name().isEmpty()) {
700  error(nodeProp, QDeclarativeListModel::tr("ListElement: cannot contain nested elements"));
701  return false;
702  }
703  if (nodeProp.name() == "id") {
704  error(nodeProp, QDeclarativeListModel::tr("ListElement: cannot use reserved \"id\" property"));
705  return false;
706  }
707 
708  ListInstruction li;
709  int ref = data.count();
710  data.append(nodeProp.name());
711  data.append('\0');
712  li.type = ListInstruction::Set;
713  li.dataIdx = ref;
714  instr << li;
715 
716  if(!compileProperty(nodeProp, instr, data))
717  return false;
718 
719  li.type = ListInstruction::Pop;
720  li.dataIdx = -1;
721  instr << li;
722  }
723 
724  {
725  ListInstruction li;
726  li.type = ListInstruction::Pop;
727  li.dataIdx = -1;
728  instr << li;
729  }
730 
731  } else {
732 
735 
736  int ref = data.count();
737 
738  QByteArray d;
739  d += char(variant.type()); // type tag
740  if (variant.isString()) {
741  d += variant.asString().toUtf8();
742  } else if (variant.isNumber()) {
743  double temp = variant.asNumber();
744  d += QByteArray( reinterpret_cast<const char*>(&temp), sizeof(double));
745  } else if (variant.isBoolean()) {
746  d += char(variant.asBoolean());
747  } else if (variant.isScript()) {
748  if (definesEmptyList(variant.asScript())) {
749  d[0] = char(QDeclarativeParser::Variant::Invalid); // marks empty list
750  } else {
751  QByteArray script = variant.asScript().toUtf8();
752  int v = evaluateEnum(script);
753  if (v<0) {
754  if (script.startsWith("QT_TR_NOOP(\"") && script.endsWith("\")")) {
756  d += script.mid(12,script.length()-14);
757  } else {
758  error(prop, QDeclarativeListModel::tr("ListElement: cannot use script for property value"));
759  return false;
760  }
761  } else {
763  double temp = v;
764  d += QByteArray( reinterpret_cast<const char*>(&temp), sizeof(double));
765  }
766  }
767  }
768  d.append('\0');
769  data.append(d);
770 
771  ListInstruction li;
772  li.type = ListInstruction::Value;
773  li.dataIdx = ref;
774  instr << li;
775  }
776  }
777 
778  return true;
779 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
double d
Definition: qnumeric_p.h:62
The QMetaObject class contains meta-information about Qt objects.
Definition: qobjectdefs.h:304
QList< QDeclarativeCustomParserProperty > properties() const
const QMetaObject * resolveType(const QByteArray &) const
Resolves name to a type, or 0 if it is not a type.
QByteArray & append(char c)
Appends the character ch to this byte array.
QByteArray toUtf8() const Q_REQUIRED_RESULT
Returns a UTF-8 representation of the string as a QByteArray.
Definition: qstring.cpp:4074
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
static const QMetaObject staticMetaObject
This variable stores the meta-object for the class.
Definition: qobject.h:128
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
bool startsWith(const QByteArray &a) const
Returns true if this byte array starts with byte array ba; otherwise returns false.
void error(const QString &description)
Reports an error with the given description.
bool definesEmptyList(const QString &)
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
quint16 values[128]
QByteArray mid(int index, int len=-1) const
Returns a byte array containing len bytes from this byte array, starting at position pos...
Q_CORE_EXPORT int QT_FASTCALL script(uint ucs4)
int length() const
Same as size().
Definition: qbytearray.h:356
int userType() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1913
bool compileProperty(const QDeclarativeCustomParserProperty &prop, QList< ListInstruction > &instr, QByteArray &data)
int count(char c) const
Returns the number of occurrences of character ch in the byte array.
T qvariant_cast(const QVariant &)
Definition: qvariant.h:571
int evaluateEnum(const QByteArray &) const
If script is a simply enum expression (eg.
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
const char * variant
bool endsWith(const QByteArray &a) const
Returns true if this byte array ends with byte array ba; otherwise returns false. ...

◆ definesEmptyList()

bool QDeclarativeListModelParser::definesEmptyList ( const QString s)
private

Definition at line 896 of file qdeclarativelistmodel.cpp.

897 {
898  if (s.startsWith(QLatin1Char('[')) && s.endsWith(QLatin1Char(']'))) {
899  for (int i=1; i<s.length()-1; i++) {
900  if (!s[i].isSpace())
901  return false;
902  }
903  return true;
904  }
905  return false;
906 }
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
bool endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string ends with s; otherwise returns false.
Definition: qstring.cpp:3796
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ setCustomData()

void QDeclarativeListModelParser::setCustomData ( QObject obj,
const QByteArray d 
)
virtual

Implements QDeclarativeCustomParser.

Definition at line 817 of file qdeclarativelistmodel.cpp.

818 {
819  QDeclarativeListModel *rv = static_cast<QDeclarativeListModel *>(obj);
820  ModelNode *root = new ModelNode(rv->m_nested);
821  rv->m_nested->m_ownsRoot = true;
822  rv->m_nested->_root = root;
823  QStack<ModelNode *> nodes;
824  nodes << root;
825 
826  bool processingSet = false;
827 
828  const ListModelData *lmd = (const ListModelData *)d.constData();
829  const char *data = ((const char *)lmd) + lmd->dataOffset;
830 
831  for (int ii = 0; ii < lmd->instrCount; ++ii) {
832  const ListInstruction &instr = lmd->instructions()[ii];
833 
834  switch(instr.type) {
836  {
837  ModelNode *n = nodes.top();
838  ModelNode *n2 = new ModelNode(rv->m_nested);
839  n->values << QVariant::fromValue(n2);
840  nodes.push(n2);
841  if (processingSet)
842  n->isArray = true;
843  }
844  break;
845 
847  nodes.pop();
848  break;
849 
851  {
852  ModelNode *n = nodes.top();
853  switch (QDeclarativeParser::Variant::Type(data[instr.dataIdx])) {
855  n->isArray = true;
856  break;
858  n->values.append(bool(data[1 + instr.dataIdx]));
859  break;
861  double temp;
862  ::memcpy(&temp, data + 1 + instr.dataIdx, sizeof(double));
863  n->values.append(temp);
864  break;
866  n->values.append(QString::fromUtf8(data + 1 + instr.dataIdx));
867  break;
868  default:
869  Q_ASSERT("Format error in ListInstruction");
870  }
871 
872  processingSet = false;
873  }
874  break;
875 
877  {
878  ModelNode *n = nodes.top();
879  ModelNode *n2 = new ModelNode(rv->m_nested);
880  n->properties.insert(QString::fromUtf8(data + instr.dataIdx), n2);
881  nodes.push(n2);
882  processingSet = true;
883  }
884  break;
885  }
886  }
887 
888  ModelNode *rootNode = rv->m_nested->_root;
889  for (int i=0; i<rootNode->values.count(); ++i) {
890  ModelNode *node = qvariant_cast<ModelNode *>(rootNode->values[i]);
891  node->listIndex = i;
892  node->updateListIndexes();
893  }
894 }
QHash< QString, ModelNode * > properties
The QStack class is a template class that provides a stack.
Definition: qcontainerfwd.h:63
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
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition: qhash.h:753
T pop()
Removes the top item from the stack and returns it.
Definition: qstack.h:67
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
QList< QVariant > values
static QString fromUtf8(const char *, int size=-1)
Returns a QString initialized with the first size bytes of the UTF-8 string str.
Definition: qstring.cpp:4302
static const char * data(const QByteArray &arr)
static QVariant fromValue(const T &value)
Returns a QVariant containing a copy of value.
Definition: qvariant.h:336
void push(const T &t)
Adds element t to the top of the stack.
Definition: qstack.h:60
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
T qvariant_cast(const QVariant &)
Definition: qvariant.h:571
T & top()
Returns a reference to the stack&#39;s top item.
Definition: qstack.h:72

Properties

◆ listElementTypeName

QByteArray QDeclarativeListModelParser::listElementTypeName
private

Definition at line 147 of file qdeclarativelistmodel_p.h.


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