OSDN Git Service

QuickDesigner: Automatically use alias of namespace in Rewriter
authorKai Koehne <kai.koehne@nokia.com>
Thu, 10 Jun 2010 11:36:45 +0000 (13:36 +0200)
committerKai Koehne <kai.koehne@nokia.com>
Thu, 10 Jun 2010 13:15:31 +0000 (15:15 +0200)
E.g. if the type of the node is 'Qt/Image', version 4, 6, search through
the list of imports to find 'Qt'. If an alias for the namespace has been
defined, prepend this to the element name.

Reviewed-by: Erik Verbruggen
src/plugins/qmldesigner/designercore/model/qmltextgenerator.cpp

index 8ad16ab..f912214 100644 (file)
@@ -35,6 +35,7 @@
 #include "nodelistproperty.h"
 #include "qmltextgenerator.h"
 #include "variantproperty.h"
+#include "model.h"
 
 using namespace QmlDesigner;
 using namespace QmlDesigner::Internal;
@@ -122,11 +123,31 @@ QString QmlTextGenerator::toQml(const AbstractProperty &property, int indentDept
 QString QmlTextGenerator::toQml(const ModelNode &node, int indentDepth) const
 {
     QString type = node.type();
-    int lastSlashIndex = type.lastIndexOf('/');
-    if (lastSlashIndex != -1)
-        type = type.mid(lastSlashIndex + 1);
+    QString url;
+    if (type.contains('/')) {
+        QStringList nameComponents = type.split("/");
+        url = nameComponents.first();
+        type = nameComponents.last();
+    }
+
+    QString alias;
+    if (!url.isEmpty()) {
+        const QString &versionUrl = QString("%1.%2").arg(QString::number(node.majorVersion()), QString::number(node.minorVersion()));
+        foreach (const Import &import, node.model()->imports()) {
+            if (import.url() == url
+                && import.version() == versionUrl) {
+                alias = import.alias();
+                break;
+            }
+        }
+    }
+
+    QString result;
+
+    if (!alias.isEmpty())
+        result = alias + ".";
 
-    QString result = type;
+    result += type;
     result += QLatin1String(" {\n");
 
     const int propertyIndentDepth = indentDepth + 4;