QmlDesigner: Move generateNewId to AbstractView

Change-Id: I8753eae6c4f79967822003709e7cd35dbea4eeba
Reviewed-by: Thomas Hartmann <Thomas.Hartmann@digia.com>
This commit is contained in:
Marco Bubke
2013-07-29 14:57:37 +02:00
committed by Thomas Hartmann
parent a63732e3e0
commit 125eb66b79
5 changed files with 19 additions and 35 deletions

View File

@@ -159,6 +159,7 @@ public:
ModelNode modelNodeForId(const QString &id); ModelNode modelNodeForId(const QString &id);
bool hasId(const QString &id) const; bool hasId(const QString &id) const;
QString generateNewId(const QString prefixName) const;
ModelNode modelNodeForInternalId(qint32 internalId); ModelNode modelNodeForInternalId(qint32 internalId);
bool hasModelNodeForInternalId(qint32 internalId) const; bool hasModelNodeForInternalId(qint32 internalId) const;

View File

@@ -185,7 +185,7 @@ public:
private: // functions private: // functions
Internal::InternalNodePointer internalNode() const; Internal::InternalNodePointer internalNode() const;
QString generateNewId() const;
private: // variables private: // variables
Internal::InternalNodePointer m_internalNode; Internal::InternalNodePointer m_internalNode;

View File

@@ -375,6 +375,20 @@ bool AbstractView::hasId(const QString &id) const
return model()->d->hasId(id); return model()->d->hasId(id);
} }
QString AbstractView::generateNewId(const QString prefixName) const
{
int counter = 1;
QString newId = QString("%1%2").arg(prefixName.toLower()).arg(counter);
while (hasId(newId)) {
counter += 1;
newId = QString("%1%2").arg(prefixName.toLower()).arg(counter);
}
return newId;
}
ModelNode AbstractView::modelNodeForInternalId(qint32 internalId) ModelNode AbstractView::modelNodeForInternalId(qint32 internalId)
{ {
return ModelNode(model()->d->nodeForInternalId(internalId), model(), this); return ModelNode(model()->d->nodeForInternalId(internalId), model(), this);

View File

@@ -125,19 +125,6 @@ ModelNode::~ModelNode()
{ {
} }
QString ModelNode::generateNewId() const
{
int counter = 1;
QString newId = QString("%1%2").arg(QString::fromUtf8(simplifiedTypeName()).toLower()).arg(counter);
while (view()->hasId(newId)) {
counter += 1;
newId = QString("%1%2").arg(QString::fromUtf8(simplifiedTypeName()).toLower()).arg(counter);
}
return newId;
}
/*! \brief returns the name of node which is a short cut to a property like objectName /*! \brief returns the name of node which is a short cut to a property like objectName
\return name of the node \return name of the node
*/ */
@@ -152,7 +139,7 @@ QString ModelNode::id() const
QString ModelNode::validId() QString ModelNode::validId()
{ {
if (id().isEmpty()) if (id().isEmpty())
setId(generateNewId()); setId(view()->generateNewId(QString::fromUtf8(simplifiedTypeName())));
return id(); return id();
} }

View File

@@ -147,17 +147,7 @@ QmlItemNode QmlItemNode::createQmlItemNode(AbstractView *view, const ItemLibrary
if (!newQmlItemNode.isValid()) if (!newQmlItemNode.isValid())
return newQmlItemNode; return newQmlItemNode;
QString id; newQmlItemNode.setId(view->generateNewId("image"));
int i = 1;
QString name(itemLibraryEntry.name().toLower());
//remove forbidden characters
name.replace(QRegExp(QLatin1String("[^a-zA-Z0-9_]")), QLatin1String("_"));
do {
id = name + QString::number(i);
i++;
} while (view->hasId(id)); //If the name already exists count upwards
newQmlItemNode.setId(id);
if (!QmlModelState(view->actualStateNode()).isBaseState()) { if (!QmlModelState(view->actualStateNode()).isBaseState()) {
newQmlItemNode.modelNode().variantProperty("opacity").setValue(0); newQmlItemNode.modelNode().variantProperty("opacity").setValue(0);
@@ -224,16 +214,8 @@ QmlItemNode QmlItemNode::createQmlItemNodeFromImage(AbstractView *view, const QS
Q_ASSERT(newQmlItemNode.isValid()); Q_ASSERT(newQmlItemNode.isValid());
QString id; newQmlItemNode.setId(view->generateNewId("image"));
int i = 1;
QString name("image");
name.remove(QLatin1Char(' '));
do {
id = name + QString::number(i);
i++;
} while (view->hasId(id)); //If the name already exists count upwards
newQmlItemNode.setId(id);
if (!QmlModelState(view->actualStateNode()).isBaseState()) { if (!QmlModelState(view->actualStateNode()).isBaseState()) {
newQmlItemNode.modelNode().variantProperty("opacity").setValue(0); newQmlItemNode.modelNode().variantProperty("opacity").setValue(0);
newQmlItemNode.setVariantProperty("opacity", 1); newQmlItemNode.setVariantProperty("opacity", 1);