From e8a1aa9c92bc8b88b0e9b0bc53832be49006684a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Fonseca?= Date: Thu, 21 Apr 2011 09:13:23 +0100 Subject: [PATCH] Fix HTML representation of multiline strings. --- gui/apitracecall.cpp | 63 +++++++++++++++++++++++++++++++++++++++++++++++++--- gui/apitracecall.h | 2 +- 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/gui/apitracecall.cpp b/gui/apitracecall.cpp index 2d41e25..8f6f420 100644 --- a/gui/apitracecall.cpp +++ b/gui/apitracecall.cpp @@ -7,6 +7,7 @@ #include #define QT_USE_FAST_OPERATOR_PLUS #include +#include const char * const styleSheet = ".call {\n" @@ -55,7 +56,59 @@ QString ApiPointer::toString() const return QLatin1String("NULL"); } -QString apiVariantToString(const QVariant &variant) +// Qt::convertFromPlainText doesn't do precisely what we want +static QString +plainTextToHTML(const QString & plain, bool multiLine) +{ + int col = 0; + bool quote = false; + QString rich; + for (int i = 0; i < plain.length(); ++i) { + if (plain[i] == QLatin1Char('\n')){ + if (multiLine) { + rich += QLatin1String("
\n"); + } else { + rich += QLatin1String("\\n"); + } + col = 0; + quote = true; + } else { + if (plain[i] == QLatin1Char('\t')){ + if (multiLine) { + rich += QChar(0x00a0U); + ++col; + while (col % 8) { + rich += QChar(0x00a0U); + ++col; + } + } else { + rich += QLatin1String("\\t"); + } + quote = true; + } else if (plain[i].isSpace()) { + rich += QChar(0x00a0U); + quote = true; + } else if (plain[i] == QLatin1Char('<')) { + rich += QLatin1String("<"); + } else if (plain[i] == QLatin1Char('>')) { + rich += QLatin1String(">"); + } else if (plain[i] == QLatin1Char('&')) { + rich += QLatin1String("&"); + } else { + rich += plain[i]; + } + ++col; + } + } + + if (quote) { + return QLatin1Literal("\"") + rich + QLatin1Literal("\""); + } + + return rich; +} + +QString apiVariantToString(const QVariant &variant, bool multiLine) { if (variant.userType() == QVariant::Double) { return QString::number(variant.toFloat()); @@ -70,6 +123,10 @@ QString apiVariantToString(const QVariant &variant) } } + if (variant.userType() == QVariant::String) { + return plainTextToHTML(variant.toString(), multiLine); + } + if (variant.userType() < QVariant::UserType) { return variant.toString(); } @@ -373,7 +430,7 @@ QString ApiTraceCall::toHtml() const QLatin1String("") + QLatin1Literal(" = ") + QLatin1Literal("") + - apiVariantToString(argValues[i]) + + apiVariantToString(argValues[i], true) + QLatin1Literal(""); if (i < m_argNames.count() - 1) m_richText += QLatin1String(", "); @@ -384,7 +441,7 @@ QString ApiTraceCall::toHtml() const m_richText += QLatin1String(" = ") + QLatin1String("") + - apiVariantToString(m_returnValue) + + apiVariantToString(m_returnValue, true) + QLatin1String(""); } m_richText += QLatin1String(""); diff --git a/gui/apitracecall.h b/gui/apitracecall.h index 3afd2ab..b5a3604 100644 --- a/gui/apitracecall.h +++ b/gui/apitracecall.h @@ -125,7 +125,7 @@ private: Q_DECLARE_METATYPE(ApiArray); -QString apiVariantToString(const QVariant &variant); +QString apiVariantToString(const QVariant &variant, bool multiLine = false); class ApiTraceFrame; -- 2.7.4