Commit a91d137d authored by Marco Bubke's avatar Marco Bubke

QmlDesigner.Model: Add internalId to the ModelNode

There are many int to ModelNode hashes in the views. This patch implement a int to ModelNode hash in the model to make them obsolete.

Reviewed-By: Thomas Hartmann
parent 1f797099
......@@ -385,7 +385,6 @@ ModelNode NavigatorTreeModel::nodeForIndex(const QModelIndex &index) const
{
Q_ASSERT(index.isValid());
uint hash = index.data(NavigatorRole).toUInt();
Q_ASSERT(hash);
Q_ASSERT(containsNodeHash(hash));
return nodeForHash(hash);
}
......
......@@ -97,6 +97,9 @@ public:
ModelNode modelNodeForId(const QString &id);
bool hasId(const QString &id) const;
ModelNode modelNodeForInternalId(qint32 internalId);
bool hasModelNodeForInternalId(qint32 internalId) const;
QList<ModelNode> allModelNodes();
void emitCustomNotification(const QString &identifier);
......
......@@ -165,6 +165,8 @@ public:
void setAuxiliaryData(const QString &name, const QVariant &data);
bool hasAuxiliaryData(const QString &name) const;
qint32 internalId() const;
private: // functions
Internal::InternalNodePointer internalNode() const;
QString generateNewId() const;
......
......@@ -334,6 +334,16 @@ bool AbstractView::hasId(const QString &id) const
return model()->m_d->hasId(id);
}
ModelNode AbstractView::modelNodeForInternalId(qint32 internalId)
{
return ModelNode(model()->m_d->nodeForInternalId(internalId), model(), this);
}
bool AbstractView::hasModelNodeInternalId(qint32 internalId) const
{
return model()->m_d->hasNodeForInternalId(internalId);
}
QmlModelView *AbstractView::toQmlModelView()
{
return qobject_cast<QmlModelView*>(this);
......
......@@ -50,22 +50,24 @@ namespace Internal {
InternalNode::InternalNode() :
m_majorVersion(0),
m_minorVersion(0),
m_valid(false)
m_valid(false),
m_internalId(-1)
{
}
InternalNode::InternalNode(const QString &typeName,int majorVersion, int minorVersion):
InternalNode::InternalNode(const QString &typeName,int majorVersion, int minorVersion, qint32 internalId):
m_typeName(typeName),
m_majorVersion(majorVersion),
m_minorVersion(minorVersion),
m_valid(true)
m_valid(true),
m_internalId(internalId)
{
}
InternalNode::Pointer InternalNode::create(const QString &type,int majorVersion, int minorVersion)
InternalNode::Pointer InternalNode::create(const QString &type,int majorVersion, int minorVersion, qint32 internalId)
{
InternalNode *newPointer(new InternalNode(type, majorVersion, minorVersion));
InternalNode *newPointer(new InternalNode(type, majorVersion, minorVersion, internalId));
InternalNode::Pointer smartPointer(newPointer);
newPointer->setInternalWeakPointer(smartPointer);
......@@ -158,7 +160,10 @@ void InternalNode::setId(const QString& id)
uint qHash(const InternalNodePointer& node)
{
return ::qHash(node.data());
if (node.isNull())
return ::qHash(-1);
return ::qHash(node->internalId());
}
QVariant InternalNode::auxiliaryData(const QString &name) const
......@@ -310,7 +315,13 @@ QList<InternalNode::Pointer> InternalNode::allDirectSubNodes() const
bool operator <(const InternalNode::Pointer &firstNode, const InternalNode::Pointer &secondNode)
{
return firstNode.data() < secondNode.data();
if (firstNode.isNull())
return true;
if (secondNode.isNull())
return false;
return firstNode->internalId() < secondNode->internalId();
}
void InternalNode::setScriptFunctions(const QStringList &scriptFunctionList)
......@@ -323,5 +334,10 @@ QStringList InternalNode::scriptFunctions() const
return m_scriptFunctionList;
}
qint32 InternalNode::internalId() const
{
return m_internalId;
}
}
}
......@@ -64,7 +64,7 @@ public:
explicit InternalNode();
static Pointer create(const QString &typeName, int majorVersion, int minorVersion);
static Pointer create(const QString &typeName, int majorVersion, int minorVersion, qint32 internalId);
QString type() const;
void setType(const QString &newType);
......@@ -116,11 +116,13 @@ public:
void setScriptFunctions(const QStringList &scriptFunctionList);
QStringList scriptFunctions() const;
qint32 internalId() const;
protected:
Pointer internalPointer() const;
void setInternalWeakPointer(const Pointer &pointer);
void removeProperty(const QString &name);
explicit InternalNode(const QString &type, int majorVersion, int minorVersion);
explicit InternalNode(const QString &type, int majorVersion, int minorVersion, qint32 internalId);
private:
QString m_typeName;
......@@ -135,6 +137,7 @@ private:
int m_minorVersion;
bool m_valid;
qint32 m_internalId;
QHash<QString, InternalPropertyPointer> m_namePropertyHash;
QStringList m_scriptFunctionList;
......
......@@ -88,9 +88,10 @@ namespace Internal {
ModelPrivate::ModelPrivate(Model *model) :
m_q(model),
m_writeLock(false)
m_writeLock(false),
m_internalIdCounter(1)
{
m_rootInternalNode = createNode("Qt/Item", 4, 7, PropertyListType());
m_rootInternalNode = createNode("Qt/Item", 4, 7, PropertyListType(), true);
}
ModelPrivate::~ModelPrivate()
......@@ -204,12 +205,18 @@ void ModelPrivate::setFileUrl(const QUrl &fileUrl)
InternalNode::Pointer ModelPrivate::createNode(const QString &typeString,
int majorVersion,
int minorVersion,
const QList<QPair<QString, QVariant> > &propertyList)
const QList<QPair<QString, QVariant> > &propertyList,
bool isRootNode)
{
if (typeString.isEmpty())
throw InvalidArgumentException(__LINE__, __FUNCTION__, __FILE__, tr("invalid type"));
InternalNode::Pointer newInternalNodePointer = InternalNode::create(typeString, majorVersion, minorVersion);
qint32 internalId = 0;
if (!isRootNode)
internalId = m_internalIdCounter++;
InternalNode::Pointer newInternalNodePointer = InternalNode::create(typeString, majorVersion, minorVersion, internalId);
typedef QPair<QString, QVariant> PropertyPair;
......@@ -219,6 +226,7 @@ InternalNode::Pointer ModelPrivate::createNode(const QString &typeString,
}
m_nodeSet.insert(newInternalNodePointer);
m_internalIdNodeHash.insert(newInternalNodePointer->internalId(), newInternalNodePointer);
notifyNodeCreated(newInternalNodePointer);
......@@ -235,6 +243,7 @@ void ModelPrivate::removeNodeFromModel(const InternalNodePointer &node)
m_idNodeHash.remove(node->id());
node->setValid(false);
m_nodeSet.remove(node);
m_internalIdNodeHash.remove(node->internalId());
}
void ModelPrivate::removeAllSubNodes(const InternalNode::Pointer &node)
......@@ -1156,15 +1165,6 @@ void ModelPrivate::changeNodeOrder(const InternalNode::Pointer &internalParentNo
notifyNodeOrderChanged(nodeList, internalNode, from);
}
void ModelPrivate::setRootNode(const InternalNode::Pointer& newRootNode)
{
removeNode(m_rootInternalNode);
m_rootInternalNode = newRootNode;
if (!m_rootInternalNode.isNull() && m_rootInternalNode->isValid())
notifyNodeCreated(m_rootInternalNode);
}
void ModelPrivate::setRewriterView(RewriterView *rewriterView)
{
if (rewriterView == m_rewriterView.data())
......@@ -1215,6 +1215,16 @@ bool ModelPrivate::hasId(const QString &id) const
return m_idNodeHash.contains(id);
}
InternalNodePointer ModelPrivate::nodeForInternalId(qint32 internalId) const
{
return m_internalIdNodeHash.value(internalId);
}
bool ModelPrivate::hasNodeForInternalId(qint32 internalId) const
{
return m_internalIdNodeHash.contains(internalId);
}
QList<InternalNodePointer> ModelPrivate::allNodes() const
{
// the item must be ordered!
......
......@@ -101,7 +101,8 @@ public:
InternalNodePointer createNode(const QString &typeString,
int majorVersion,
int minorVersion,
const QList<QPair<QString, QVariant> > &propertyList);
const QList<QPair<QString, QVariant> > &propertyList,
bool isRootNode = false);
/*factory methods for internal use in model and rewriter*/
......@@ -153,7 +154,6 @@ public:
void changeSelectedNodes(const QList<InternalNodePointer> &newSelectedsNodeList,
const QList<InternalNodePointer> &oldSelectedsNodeList);
void setRootNode(const InternalNodePointer& newRootNode);
void setAuxiliaryData(const InternalNodePointer& node, const QString &name, const QVariant &data);
void resetModelByRewriter(const QString &description);
......@@ -186,6 +186,9 @@ public:
InternalNodePointer nodeForId(const QString &id) const;
bool hasId(const QString &id) const;
InternalNodePointer nodeForInternalId(qint32 internalId) const;
bool hasNodeForInternalId(qint32 internalId) const;
QList<InternalNodePointer> allNodes() const;
bool isWriteLocked() const;
......@@ -212,6 +215,7 @@ private:
QList<QWeakPointer<AbstractView> > m_viewList;
QList<InternalNodePointer> m_selectedNodeList;
QHash<QString,InternalNodePointer> m_idNodeHash;
QHash<qint32, InternalNodePointer> m_internalIdNodeHash;
QSet<InternalNodePointer> m_nodeSet;
InternalNodePointer m_rootInternalNode;
......@@ -221,7 +225,8 @@ private:
QWeakPointer<Model> m_masterModel;
QWeakPointer<RewriterView> m_rewriterView;
QWeakPointer<NodeInstanceView> m_nodeInstanceView;
bool m_writeLock;
bool m_writeLock;
qint32 m_internalIdCounter;
};
}
......
......@@ -629,7 +629,7 @@ bool operator ==(const ModelNode &firstNode, const ModelNode &secondNode)
throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
}
return firstNode.m_internalNode.data() == secondNode.m_internalNode.data();
return firstNode.internalId() == secondNode.internalId();
}
/*!
......@@ -642,7 +642,7 @@ bool operator !=(const ModelNode &firstNode, const ModelNode &secondNode)
throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
}
return firstNode.m_internalNode != secondNode.m_internalNode;
return firstNode.internalId() != secondNode.internalId();
}
bool operator <(const ModelNode &firstNode, const ModelNode &secondNode)
......@@ -652,7 +652,7 @@ bool operator <(const ModelNode &firstNode, const ModelNode &secondNode)
throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
}
return firstNode.internalNode().data() < secondNode.internalNode().data();
return firstNode.internalId() < secondNode.internalId();
}
......@@ -672,7 +672,7 @@ uint qHash(const ModelNode &node)
// Q_ASSERT_X(node.isValid(), Q_FUNC_INFO, "model node is invalid");
// throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
// }
return ::qHash(node.m_internalNode.data());
return ::qHash(node.internalId());
}
/*!
......@@ -925,4 +925,12 @@ QStringList ModelNode::scriptFunctions() const
return internalNode()->scriptFunctions();
}
qint32 ModelNode::internalId() const
{
if (m_internalNode.isNull())
return -1;
return m_internalNode->internalId();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment