Support <pre> in StyleText textFormat
authorMartin Jones <martin.jones@nokia.com>
Wed, 21 Dec 2011 00:19:03 +0000 (10:19 +1000)
committerQt by Nokia <qt-info@nokia.com>
Thu, 5 Jan 2012 05:47:26 +0000 (06:47 +0100)
Task-number: QTBUG-23159

Change-Id: I842d066efb3a78defba61cc31060840f771f9b11
Reviewed-by: Michael Brasser <michael.brasser@nokia.com>
src/quick/items/qquicktext.cpp
src/quick/util/qdeclarativestyledtext.cpp
tests/auto/qtquick2/qdeclarativestyledtext/tst_qdeclarativestyledtext.cpp

index 5072bf1..b96ee71 100644 (file)
@@ -1528,6 +1528,7 @@ void QQuickText::resetMaximumLineCount()
 
     \code
     <b></b> - bold
+    <strong></strong> - bold
     <i></i> - italic
     <br> - new line
     <p> - paragraph
@@ -1536,6 +1537,7 @@ void QQuickText::resetMaximumLineCount()
     <h1> to <h6> - headers
     <a href=""> - anchor
     <ol type="">, <ul type=""> and <li> - ordered and unordered lists
+    <pre></pre> - preformatted
     &gt; &lt; &amp;
     \endcode
 
index d2c2ef8..78add30 100644 (file)
@@ -51,6 +51,7 @@
     QDeclarativeStyledText supports few tags:
 
     <b></b> - bold
+    <strong></strong> - bold
     <i></i> - italic
     <br> - new line
     <p> - paragraph
@@ -59,6 +60,7 @@
     <h1> to <h6> - headers
     <a href=""> - anchor
     <ol type="">, <ul type=""> and <li> - ordered and unordered lists
+    <pre></pre> - preformated
 
     The opening and closing tags must be correctly nested.
 */
@@ -79,6 +81,7 @@ public:
 
     QDeclarativeStyledTextPrivate(const QString &t, QTextLayout &l)
         : text(t), layout(l), baseFont(layout.font()), hasNewLine(false)
+        , preFormat(false)
     {
     }
 
@@ -106,6 +109,7 @@ public:
     QFont baseFont;
     QStack<List> listStack;
     bool hasNewLine;
+    bool preFormat;
 
     static const QChar lessThan;
     static const QChar greaterThan;
@@ -216,6 +220,11 @@ void QDeclarativeStyledTextPrivate::parse()
             parseEntity(ch, text, drawText);
             textStart = ch - text.constData() + 1;
             textLength = 0;
+        } else if (preFormat && ch->isSpace()) {
+            drawText.append(QStringRef(&text, textStart, textLength));
+            drawText.append(QChar(QChar::Nbsp));
+            textStart = ch - text.constData() + 1;
+            textLength = 0;
         } else {
             ++textLength;
         }
@@ -269,6 +278,13 @@ bool QDeclarativeStyledTextPrivate::parseTag(const QChar *&ch, const QString &te
                 if (tagLength == 1) {
                     if (!hasNewLine)
                         textOut.append(QChar::LineSeparator);
+                } else if (tag == QLatin1String("pre")) {
+                    preFormat = true;
+                    if (!hasNewLine)
+                        textOut.append(QChar::LineSeparator);
+                    format.setFontFamily(QString::fromLatin1("Courier New,courier"));
+                    format.setFontFixedPitch(true);
+                    return true;
                 }
             } else if (char0 == QLatin1Char('u')) {
                 if (tagLength == 1) {
@@ -392,6 +408,12 @@ bool QDeclarativeStyledTextPrivate::parseCloseTag(const QChar *&ch, const QStrin
                     textOut.append(QChar::LineSeparator);
                     hasNewLine = true;
                     return false;
+                } else if (tag == QLatin1String("pre")) {
+                    preFormat = false;
+                    if (!hasNewLine)
+                        textOut.append(QChar::LineSeparator);
+                    hasNewLine = true;
+                    return true;
                 }
             } else if (char0 == QLatin1Char('u')) {
                 if (tagLength == 1)
index a17c429..5adc6fc 100644 (file)
@@ -114,6 +114,7 @@ void tst_qdeclarativestyledtext::textOutput_data()
     QTest::newRow("h5") << "<h5>head" << QChar(QChar::LineSeparator) + QLatin1String("head") << (FormatList() << Format(Format::Bold, 0, 5));
     QTest::newRow("h6") << "<h6>head" << QChar(QChar::LineSeparator) + QLatin1String("head") << (FormatList() << Format(Format::Bold, 0, 5));
     QTest::newRow("h7") << "<h7>head" << "head" << FormatList();
+    QTest::newRow("pre") << "normal<pre>pre text</pre>normal" << QLatin1String("normal") + QChar(QChar::LineSeparator) + QLatin1String("pre") + QChar(QChar::Nbsp) + QLatin1String("text") + QChar(QChar::LineSeparator) + QLatin1String("normal") << (FormatList() << Format(0, 6, 9));
 }
 
 void tst_qdeclarativestyledtext::textOutput()