diff options
author | Michael Brasser <michael.brasser@nokia.com> | 2009-09-08 22:46:10 (GMT) |
---|---|---|
committer | Michael Brasser <michael.brasser@nokia.com> | 2009-09-08 22:46:10 (GMT) |
commit | d29c604b0365dfbc20f8aea8d2cb63f621995219 (patch) | |
tree | 511904026a961cadf5e7e13ff90b7e9baad1efbf /src/declarative | |
parent | cce03d9a3c95dd81c31003e0a96c5bb3dca6f0c0 (diff) | |
parent | 7731e5f7d33d3ec251299c7651e777d7e0054573 (diff) | |
download | Qt-d29c604b0365dfbc20f8aea8d2cb63f621995219.zip Qt-d29c604b0365dfbc20f8aea8d2cb63f621995219.tar.gz Qt-d29c604b0365dfbc20f8aea8d2cb63f621995219.tar.bz2 |
Merge branch 'kinetic-declarativeui' of git@scm.dev.nokia.troll.no:qt/kinetic into kinetic-declarativeui
Diffstat (limited to 'src/declarative')
-rw-r--r-- | src/declarative/QmlChanges.txt | 1 | ||||
-rw-r--r-- | src/declarative/fx/qfxitem.cpp | 8 | ||||
-rw-r--r-- | src/declarative/fx/qfxlistview.cpp | 3 | ||||
-rw-r--r-- | src/declarative/fx/qfxvisualitemmodel.cpp | 19 | ||||
-rw-r--r-- | src/declarative/qml/parser/qmljs.g | 1 | ||||
-rw-r--r-- | src/declarative/qml/parser/qmljsastfwd_p.h | 4 | ||||
-rw-r--r-- | src/declarative/qml/parser/qmljsengine_p.cpp | 6 | ||||
-rw-r--r-- | src/declarative/qml/parser/qmljsengine_p.h | 4 | ||||
-rw-r--r-- | src/declarative/qml/parser/qmljslexer.cpp | 16 | ||||
-rw-r--r-- | src/declarative/qml/parser/qmljslexer_p.h | 3 | ||||
-rw-r--r-- | src/declarative/qml/qmetaobjectbuilder_p.h | 8 | ||||
-rw-r--r-- | src/declarative/util/qmllistmodel.cpp | 271 | ||||
-rw-r--r-- | src/declarative/util/qmllistmodel.h | 8 |
13 files changed, 327 insertions, 25 deletions
diff --git a/src/declarative/QmlChanges.txt b/src/declarative/QmlChanges.txt index e63eb8b..9bf4b10 100644 --- a/src/declarative/QmlChanges.txt +++ b/src/declarative/QmlChanges.txt @@ -31,6 +31,7 @@ ParentChangeAction -> ParentAction VisualModel -> VisualDataModel Renamed properties: +Item: contents -> childrenRect MouseRegion: xmin -> minimumX MouseRegion: xmax -> maximumX MouseRegion: ymin -> minimumY diff --git a/src/declarative/fx/qfxitem.cpp b/src/declarative/fx/qfxitem.cpp index 5363148..3429010 100644 --- a/src/declarative/fx/qfxitem.cpp +++ b/src/declarative/fx/qfxitem.cpp @@ -235,10 +235,10 @@ QFxContents::QFxContents() : m_x(0), m_y(0), m_width(0), m_height(0) } /*! - \qmlproperty qreal Item::childrenRect.x - \qmlproperty qreal Item::childrenRect.y - \qmlproperty qreal Item::childrenRect.width - \qmlproperty qreal Item::childrenRect.height + \qmlproperty real Item::childrenRect.x + \qmlproperty real Item::childrenRect.y + \qmlproperty real Item::childrenRect.width + \qmlproperty real Item::childrenRect.height The childrenRect properties allow an item access to the geometry of its children. This property is useful if you have an item that needs to be diff --git a/src/declarative/fx/qfxlistview.cpp b/src/declarative/fx/qfxlistview.cpp index c24610f..6c0a83e 100644 --- a/src/declarative/fx/qfxlistview.cpp +++ b/src/declarative/fx/qfxlistview.cpp @@ -1507,7 +1507,8 @@ void QFxListView::itemsInserted(int modelIndex, int count) // Update the indexes of the following visible items. for (; index < d->visibleItems.count(); ++index) { FxListItem *listItem = d->visibleItems.at(index); - listItem->setPosition(listItem->position() + (pos - initialPos)); + if (listItem->item != d->currentItem->item) + listItem->setPosition(listItem->position() + (pos - initialPos)); if (listItem->index != -1) listItem->index += count; } diff --git a/src/declarative/fx/qfxvisualitemmodel.cpp b/src/declarative/fx/qfxvisualitemmodel.cpp index 82bec09..cac8b8d 100644 --- a/src/declarative/fx/qfxvisualitemmodel.cpp +++ b/src/declarative/fx/qfxvisualitemmodel.cpp @@ -243,6 +243,16 @@ public: QmlContext *m_context; QList<int> m_roles; QHash<int,QString> m_roleNames; + void ensureRoles() { + if (m_roles.isEmpty()) { + if (m_listModelInterface) { + m_roles = m_listModelInterface->roles(); + for (int ii = 0; ii < m_roles.count(); ++ii) + m_roleNames.insert(m_roles.at(ii), + m_listModelInterface->toString(m_roles.at(ii))); + } + } + } struct ObjectRef { ObjectRef(QObject *object=0) : obj(object), ref(1) {} @@ -375,6 +385,7 @@ int QFxVisualDataModelDataMetaObject::createProperty(const char *name, const cha return QmlOpenMetaObject::createProperty(name, type); } else { const QLatin1String sname(name); + data->m_model->ensureRoles(); for (QHash<int, QString>::ConstIterator iter = data->m_model->m_roleNames.begin(); iter != data->m_model->m_roleNames.end(); ++iter) { @@ -397,6 +408,7 @@ QFxVisualDataModelDataMetaObject::propertyCreated(int, QMetaPropertyBuilder &pro && data->m_model->m_modelList) { return data->m_model->m_modelList->at(data->m_index); } else if (data->m_model->m_listModelInterface) { + data->m_model->ensureRoles(); for (QHash<int, QString>::ConstIterator iter = data->m_model->m_roleNames.begin(); iter != data->m_model->m_roleNames.end(); ++iter) { @@ -410,6 +422,7 @@ QFxVisualDataModelDataMetaObject::propertyCreated(int, QMetaPropertyBuilder &pro } } } else if (data->m_model->m_abstractItemModel) { + data->m_model->ensureRoles(); for (QHash<int, QString>::ConstIterator iter = data->m_model->m_roleNames.begin(); iter != data->m_model->m_roleNames.end(); ++iter) { @@ -560,12 +573,6 @@ void QFxVisualDataModel::setModel(const QVariant &model) if (object && (d->m_listModelInterface = qobject_cast<QListModelInterface *>(object))) { d->m_roles.clear(); d->m_roleNames.clear(); - if (d->m_listModelInterface) { - d->m_roles = d->m_listModelInterface->roles(); - for (int ii = 0; ii < d->m_roles.count(); ++ii) - d->m_roleNames.insert(d->m_roles.at(ii), - d->m_listModelInterface->toString(d->m_roles.at(ii))); - } QObject::connect(d->m_listModelInterface, SIGNAL(itemsChanged(int,int,QList<int>)), this, SLOT(_q_itemsChanged(int,int,QList<int>))); diff --git a/src/declarative/qml/parser/qmljs.g b/src/declarative/qml/parser/qmljs.g index b0ef866..4ed75e8 100644 --- a/src/declarative/qml/parser/qmljs.g +++ b/src/declarative/qml/parser/qmljs.g @@ -80,6 +80,7 @@ %token T_DEBUGGER "debugger" %token T_RESERVED_WORD "reserved word" %token T_MULTILINE_STRING_LITERAL "multiline string literal" +%token T_COMMENT "comment" --- context keywords. %token T_PUBLIC "public" diff --git a/src/declarative/qml/parser/qmljsastfwd_p.h b/src/declarative/qml/parser/qmljsastfwd_p.h index f79cfc2..a6fee1d 100644 --- a/src/declarative/qml/parser/qmljsastfwd_p.h +++ b/src/declarative/qml/parser/qmljsastfwd_p.h @@ -62,9 +62,9 @@ namespace QmlJS { namespace AST { class SourceLocation { public: - SourceLocation(quint32 offset = 0, quint32 length = 0) + SourceLocation(quint32 offset = 0, quint32 length = 0, quint32 line = 0, quint32 column = 0) : offset(offset), length(length), - startLine(0), startColumn(0) + startLine(line), startColumn(column) { } bool isValid() const { return length != 0; } diff --git a/src/declarative/qml/parser/qmljsengine_p.cpp b/src/declarative/qml/parser/qmljsengine_p.cpp index 02d9b9c..eab8944 100644 --- a/src/declarative/qml/parser/qmljsengine_p.cpp +++ b/src/declarative/qml/parser/qmljsengine_p.cpp @@ -178,6 +178,12 @@ Engine::~Engine() QSet<NameId> Engine::literals() const { return _literals; } +void Engine::addComment(int pos, int len, int line, int col) +{ if (len > 0) _comments.append(QmlJS::AST::SourceLocation(pos, len, line, col)); } + +QList<QmlJS::AST::SourceLocation> Engine::comments() const +{ return _comments; } + NameId *Engine::intern(const QChar *u, int s) { return const_cast<NameId *>(&*_literals.insert(NameId(u, s))); } diff --git a/src/declarative/qml/parser/qmljsengine_p.h b/src/declarative/qml/parser/qmljsengine_p.h index 5aea983..877fff2 100644 --- a/src/declarative/qml/parser/qmljsengine_p.h +++ b/src/declarative/qml/parser/qmljsengine_p.h @@ -143,6 +143,7 @@ class Engine Lexer *_lexer; NodePool *_nodePool; QSet<NameId> _literals; + QList<QmlJS::AST::SourceLocation> _comments; public: Engine(); @@ -150,6 +151,9 @@ public: QSet<NameId> literals() const; + void addComment(int pos, int len, int line, int col); + QList<QmlJS::AST::SourceLocation> comments() const; + NameId *intern(const QChar *u, int s); static QString toString(NameId *id); diff --git a/src/declarative/qml/parser/qmljslexer.cpp b/src/declarative/qml/parser/qmljslexer.cpp index 9da6ec0..3be1710 100644 --- a/src/declarative/qml/parser/qmljslexer.cpp +++ b/src/declarative/qml/parser/qmljslexer.cpp @@ -43,6 +43,8 @@ #include "config.h" #endif +#include <QDebug> + #include "qmljsengine_p.h" #include "qmljslexer_p.h" #include "qmljsgrammar_p.h" @@ -71,7 +73,7 @@ extern double integerFromString(const char *buf, int size, int radix); using namespace QmlJS; -Lexer::Lexer(Engine *eng) +Lexer::Lexer(Engine *eng, bool tokenizeComments) : driver(eng), yylineno(0), done(false), @@ -94,8 +96,9 @@ Lexer::Lexer(Engine *eng) check_reserved(true), parenthesesState(IgnoreParentheses), parenthesesCount(0), - prohibitAutomaticSemicolon(false) -{ + prohibitAutomaticSemicolon(false), + tokenizeComments(tokenizeComments) +{qDebug()<<"--- new lexer"; driver->setLexer(this); // allocate space for read buffers buffer8 = new char[size8]; @@ -647,22 +650,29 @@ int Lexer::lex() setDone(Other); } else state = Start; + qDebug() << "--- state is InSingleLineComment @" << startlineno << ":"<<startcolumn; + driver->addComment(startpos, tokenLength(), startlineno, startcolumn); } else if (current == 0) { + driver->addComment(startpos, tokenLength(), startlineno, startcolumn); setDone(Eof); } + break; case InMultiLineComment: if (current == 0) { setDone(Bad); err = UnclosedComment; errmsg = QLatin1String("Unclosed comment at end of file"); + driver->addComment(startpos, tokenLength(), startlineno, startcolumn); } else if (isLineTerminator()) { shiftWindowsLineBreak(); yylineno++; } else if (current == '*' && next1 == '/') { state = Start; shift(1); + driver->addComment(startpos, tokenLength(), startlineno, startcolumn); } + break; case InIdentifier: if (isIdentLetter(current) || isDecimalDigit(current)) { diff --git a/src/declarative/qml/parser/qmljslexer_p.h b/src/declarative/qml/parser/qmljslexer_p.h index 5817868..6cca45d 100644 --- a/src/declarative/qml/parser/qmljslexer_p.h +++ b/src/declarative/qml/parser/qmljslexer_p.h @@ -67,7 +67,7 @@ class NameId; class Lexer { public: - Lexer(Engine *eng); + Lexer(Engine *eng, bool tokenizeComments = false); ~Lexer(); void setCode(const QString &c, int lineno); @@ -239,6 +239,7 @@ private: ParenthesesState parenthesesState; int parenthesesCount; bool prohibitAutomaticSemicolon; + bool tokenizeComments; }; } // namespace QmlJS diff --git a/src/declarative/qml/qmetaobjectbuilder_p.h b/src/declarative/qml/qmetaobjectbuilder_p.h index d503163..c0b7426 100644 --- a/src/declarative/qml/qmetaobjectbuilder_p.h +++ b/src/declarative/qml/qmetaobjectbuilder_p.h @@ -68,7 +68,7 @@ class QMetaPropertyBuilderPrivate; class QMetaEnumBuilder; class QMetaEnumBuilderPrivate; -class Q_CORE_EXPORT QMetaObjectBuilder +class Q_DECLARATIVE_EXPORT QMetaObjectBuilder { public: enum AddMember @@ -189,7 +189,7 @@ private: friend class QMetaEnumBuilder; }; -class Q_CORE_EXPORT QMetaMethodBuilder +class Q_DECLARATIVE_EXPORT QMetaMethodBuilder { public: QMetaMethodBuilder() : _mobj(0), _index(0) {} @@ -227,7 +227,7 @@ private: QMetaMethodBuilderPrivate *d_func() const; }; -class Q_CORE_EXPORT QMetaPropertyBuilder +class Q_DECLARATIVE_EXPORT QMetaPropertyBuilder { public: QMetaPropertyBuilder() : _mobj(0), _index(0) {} @@ -278,7 +278,7 @@ private: QMetaPropertyBuilderPrivate *d_func() const; }; -class Q_CORE_EXPORT QMetaEnumBuilder +class Q_DECLARATIVE_EXPORT QMetaEnumBuilder { public: QMetaEnumBuilder() : _mobj(0), _index(0) {} diff --git a/src/declarative/util/qmllistmodel.cpp b/src/declarative/util/qmllistmodel.cpp index 0d9ea94..5b7d2bb 100644 --- a/src/declarative/util/qmllistmodel.cpp +++ b/src/declarative/util/qmllistmodel.cpp @@ -72,7 +72,9 @@ struct ListModelData \qmlclass ListModel \brief The ListModel element defines a free-form list data source. - The ListModel is a simple hierarchy of elements containing data roles. + The ListModel is a simple hierarchy of elements containing data roles. The contents can + be defined dynamically, or explicitly in QML: + For example: \code @@ -166,6 +168,29 @@ struct ListModelData } \endcode + The content of a ListModel may be created and modified using the clear(), + append(), and set() methods. For example: + + \code + Component { + id: FruitDelegate + Item { + width: 200; height: 50 + Text { text: name } + Text { text: '$'+cost; anchors.right: parent.right } + + // Double the price when clicked. + MouseRegion { + anchors.fill: parent + onClicked: FruitModel.set(index, "cost", cost*2) + } + } + } + \endcode + + When creating content dynamically, note that the set of available properties cannot be changed + except by first clearing the model - whatever properties are first added are then the + only permitted properties in the model. */ class ModelObject : public QObject @@ -189,7 +214,6 @@ struct ModelNode { ModelNode(); ~ModelNode(); - QString className; QList<QVariant> values; QHash<QString, ModelNode *> properties; @@ -214,6 +238,19 @@ struct ModelNode return objectCache; } + void setProperty(const QString& prop, const QVariant& val) { + QHash<QString, ModelNode *>::const_iterator it = properties.find(prop); + if (it != properties.end()) { + (*it)->values[0] = val; + } else { + ModelNode *n = new ModelNode; + n->values << val; + properties.insert(prop,n); + } + if (objectCache) + objectCache->setValue(prop.toLatin1(), val); + } + QmlListModel *modelCache; ModelObject *objectCache; }; @@ -235,7 +272,7 @@ QmlListModel::~QmlListModel() void QmlListModel::checkRoles() const { - if (_rolesOk) + if (_rolesOk || !_root) return; for (int ii = 0; ii < _root->values.count(); ++ii) { @@ -341,6 +378,232 @@ int QmlListModel::count() const return _root->values.count(); } +/*! + \qmlmethod ListModel::clear() + + Deletes all content from the model. The properties are cleared such that + different properties may be set on subsequent additions. + + \sa append() remove() +*/ +void QmlListModel::clear() +{ + int cleared = count(); + _rolesOk = false; + delete _root; + _root = 0; + roleStrings.clear(); + emit itemsRemoved(0,cleared); +} + +/*! + \qmlmethod ListModel::remove(int index) + + Deletes the content at \a index from the model. + + \sa clear() +*/ +void QmlListModel::remove(int index) +{ + if (_root) { + ModelNode *node = qvariant_cast<ModelNode *>(_root->values.at(index)); + _root->values.removeAt(index); + if (node) + delete node; + emit itemsRemoved(index,1); + } +} + +/*! + \qmlmethod ListModel::insert(index,dict) + + Adds a new item to the list model at position \a index, with the + values in \a dict. + + \code + FruitModel.insert(2, {"cost": 5.95, "name":"Pizza"}) + \endcode + + If \a index is not in the list, sufficient empty items are + added to the list. + + \sa set() append() +*/ +void QmlListModel::insert(int index, const QVariantMap& valuemap) +{ + if (!_root) + _root = new ModelNode; + if (index >= _root->values.count()) { + set(index,valuemap); + return; + } + ModelNode *mn = new ModelNode; + for (QVariantMap::const_iterator it=valuemap.begin(); it!=valuemap.end(); ++it) { + addRole(it.key()); + ModelNode *value = new ModelNode; + value->values << it.value(); + mn->properties.insert(it.key(),value); + } + _root->values.insert(index,qVariantFromValue(mn)); + emit itemsInserted(index,1); +} + +/*! + \qmlmethod ListModel::move(from,to,n) + + Moves \a n items \a from one position \a to another. + + The from and to ranges must exist; for example, to move the first 3 items + to the end of the list: + + \code + FruitModel.move(0,FruitModel.count-3,3) + \endcode + + \sa append() +*/ +void QmlListModel::move(int from, int to, int n) +{ + if (from+n > count() || to+n > count() || n==0 || from==to) + return; + if (from > to) { + // Only move forwards - flip if backwards moving + int tfrom = from; + int tto = to; + from = tto; + to = tto+n; + n = tfrom-tto; + } + if (n==1) { + _root->values.move(from,to); + } else { + QList<QVariant> replaced; + int i=0; + QVariantList::const_iterator it=_root->values.begin(); it += from+n; + for (; i<to-from; ++i,++it) + replaced.append(*it); + i=0; + it=_root->values.begin(); it += from; + for (; i<n; ++i,++it) + replaced.append(*it); + QVariantList::const_iterator f=replaced.begin(); + QVariantList::iterator t=_root->values.begin(); t += from; + for (; f != replaced.end(); ++f, ++t) + *t = *f; + } + emit itemsMoved(from,to,n); +} + +/*! + \qmlmethod ListModel::append(dict) + + Adds a new item to the end of the list model, with the + values in \a dict. + + \code + FruitModel.append({"cost": 5.95, "name":"Pizza"}) + \endcode + + \sa set() remove() +*/ +void QmlListModel::append(const QVariantMap& valuemap) +{ + if (!_root) + _root = new ModelNode; + ModelNode *mn = new ModelNode; + for (QVariantMap::const_iterator it=valuemap.begin(); it!=valuemap.end(); ++it) { + addRole(it.key()); + ModelNode *value = new ModelNode; + value->values << it.value(); + mn->properties.insert(it.key(),value); + } + _root->values << qVariantFromValue(mn); + emit itemsInserted(count()-1,1); +} + +/*! + \qmlmethod ListModel::set(index,dict) + + Changes the item at \a index in the list model to the + values in \a dict. + + \code + FruitModel.set(3, {"cost": 5.95, "name":"Pizza"}) + \endcode + + If \a index is not in the list, sufficient empty items are + added to the list. + + \sa append() +*/ +void QmlListModel::set(int index, const QVariantMap& valuemap) +{ + if (!_root) + _root = new ModelNode; + int initialcount = _root->values.count(); + while (index > _root->values.count()) + _root->values.append(qVariantFromValue(new ModelNode)); + if (index == _root->values.count()) + append(valuemap); + else { + ModelNode *node = qvariant_cast<ModelNode *>(_root->values.at(index)); + QList<int> roles; + for (QVariantMap::const_iterator it=valuemap.begin(); it!=valuemap.end(); ++it) { + node->setProperty(it.key(),it.value()); + int r = roleStrings.indexOf(it.key()); + if (r<0) { + r = roleStrings.count(); + roleStrings << it.key(); + } + roles.append(r); + } + if (initialcount < index) { + emit itemsInserted(initialcount,index-initialcount+1); + } else { + emit itemsChanged(index,1,roles); + } + } +} + +/*! + \qmlmethod ListModel::set(index,property,value) + + Changes the \a property of the item at \a index in the list model to \a value. + + \code + FruitModel.set(3, "cost", 5.95) + \endcode + + If \a index is not in the list, sufficient empty items are + added to the list. + + \sa append() +*/ +void QmlListModel::set(int index, const QString& property, const QVariant& value) +{ + if (!_root) + _root = new ModelNode; + int initialcount = _root->values.count(); + while (index >= _root->values.count()) + _root->values.append(qVariantFromValue(new ModelNode)); + ModelNode *node = qvariant_cast<ModelNode *>(_root->values.at(index)); + int r = roleStrings.indexOf(property); + if (r<0) { + r = roleStrings.count(); + roleStrings << property; + } + QList<int> roles; + roles.append(r); + + if (node) + node->setProperty(property,value); + if (initialcount < index) + emit itemsInserted(initialcount,index-initialcount+1); + else + emit itemsChanged(index,1,roles); +} + + class QmlListModelParser : public QmlCustomParser { public: @@ -518,7 +781,7 @@ static void dump(ModelNode *node, int ind) for (int ii = 0; ii < node->values.count(); ++ii) { ModelNode *subNode = qvariant_cast<ModelNode *>(node->values.at(ii)); if (subNode) { - qWarning().nospace() << indent << "Sub-node " << ii << ": class " << subNode->className; + qWarning().nospace() << indent << "Sub-node " << ii; dump(subNode, ind + 1); } else { qWarning().nospace() << indent << "Sub-node " << ii << ": " << node->values.at(ii).toString(); diff --git a/src/declarative/util/qmllistmodel.h b/src/declarative/util/qmllistmodel.h index 39edbe4..8bef347 100644 --- a/src/declarative/util/qmllistmodel.h +++ b/src/declarative/util/qmllistmodel.h @@ -72,6 +72,14 @@ public: virtual int count() const; virtual QHash<int,QVariant> data(int index, const QList<int> &roles = (QList<int>())) const; + Q_INVOKABLE void clear(); + Q_INVOKABLE void remove(int index); + Q_INVOKABLE void append(const QVariantMap& valuemap); + Q_INVOKABLE void insert(int index, const QVariantMap& valuemap); + Q_INVOKABLE void set(int index, const QVariantMap& valuemap); + Q_INVOKABLE void set(int index, const QString& property, const QVariant& value); + Q_INVOKABLE void move(int from, int to, int count); + private: QVariant valueForNode(ModelNode *) const; mutable QStringList roleStrings; |