From c4417c49c7ffbb1f1818b579c488b2e4966ed1f9 Mon Sep 17 00:00:00 2001 From: Thomas Hartmann Date: Tue, 11 Oct 2016 16:21:42 +0200 Subject: [PATCH] QmlDesigner: Write single non ASCII chars as escaped unicode We do not know if the string was escaped or not in the model. But this heuristic at least improves the issue and single characters are escaped. Task-number: QTCREATORBUG-12616 Change-Id: I3dcc251ce111ab3ee377a17876e2cdb64162d6e8 Reviewed-by: Tim Jenssen --- .../designercore/model/qmltextgenerator.cpp | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/plugins/qmldesigner/designercore/model/qmltextgenerator.cpp b/src/plugins/qmldesigner/designercore/model/qmltextgenerator.cpp index c8e5888aea6..3fe89aea5e7 100644 --- a/src/plugins/qmldesigner/designercore/model/qmltextgenerator.cpp +++ b/src/plugins/qmldesigner/designercore/model/qmltextgenerator.cpp @@ -61,6 +61,23 @@ inline static QString doubleToString(double d) return string; } +static QString unicodeEscape(const QString &stringValue) +{ + if (stringValue.count() == 1) { + ushort code = stringValue.at(0).unicode(); + bool isUnicode = code <= 127; + if (isUnicode) { + return stringValue; + } else { + QString escaped; + escaped += "\\u"; + escaped += QString::number(code, 16).rightJustified(4, '0'); + return escaped; + } + } + return stringValue; +} + QmlTextGenerator::QmlTextGenerator(const PropertyNameList &propertyOrder, int indentDepth): m_propertyOrder(propertyOrder), m_indentDepth(indentDepth) @@ -131,7 +148,9 @@ QString QmlTextGenerator::toQml(const AbstractProperty &property, int indentDept case QMetaType::UInt: case QMetaType::ULongLong: return stringValue; - + case QMetaType::QString: + case QMetaType::QChar: + return QStringLiteral("\"%1\"").arg(escape(unicodeEscape(stringValue))); default: return QStringLiteral("\"%1\"").arg(escape(stringValue)); }