Make PlainText the default textFormat of a TextEdit.
authorAndrew den Exter <andrew.den-exter@nokia.com>
Tue, 13 Dec 2011 03:56:42 +0000 (13:56 +1000)
committerQt by Nokia <qt-info@nokia.com>
Fri, 16 Dec 2011 01:48:00 +0000 (02:48 +0100)
With AutoText as the default it's easy to assume that a TextEdit accepts
plain text or rich text only which can lead to unexpected behaviour
when user supplied text is does not have the expected formatting and
implicitly changes the text format.

Task-number: QTBUG-23109
Change-Id: I08900fb23545672fd0e181058633bafeb826a746
Reviewed-by: Martin Jones <martin.jones@nokia.com>
doc/src/declarative/whatsnew.qdoc
examples/declarative/righttoleft/textalignment/textalignment.qml
examples/declarative/text/textselection/textselection.qml
src/quick/items/qquicktextedit.cpp
src/quick/items/qquicktextedit_p_p.h
tests/auto/qtquick2/qquicktextedit/data/horizontalAlignment_RightToLeft.qml
tests/auto/qtquick2/qquicktextedit/tst_qquicktextedit.cpp

index 622566c..2ae4671 100644 (file)
@@ -124,6 +124,9 @@ Text improvements:
  - a \c doLayout method was added to trigger the layout from Javascript.
  - now automatically switch to StyledText instead of RichText if textFormat is set to AutoText.
 
+TextEdit:
+ - the default value of the textFormat property is now PlainText instead of AutoText.
+
 PathView now has a \c currentItem property
 
 ListView and GridView:
index 13abca1..a7af6f1 100644 (file)
@@ -361,6 +361,7 @@ Rectangle {
             font.pixelSize: 24
             cursorVisible: true
             wrapMode: TextEdit.WordWrap
+            textFormat: TextEdit.RichText
             horizontalAlignment: root.horizontalAlignment
             LayoutMirroring.enabled: root.mirror
             Rectangle {
index a25d0df..2e8268d 100644 (file)
@@ -95,6 +95,7 @@ Rectangle {
                 height: flick.height
                 focus: true
                 wrapMode: TextEdit.Wrap
+                textFormat: TextEdit.RichText
 
                 onCursorRectangleChanged: flick.ensureVisible(cursorRectangle)
 
index 9e76c21..107da16 100644 (file)
@@ -280,7 +280,7 @@ void QQuickTextEdit::setText(const QString &text)
     \o TextEdit.RichText
     \endlist
 
-    The default is TextEdit.AutoText.  If the text format is TextEdit.AutoText the text edit
+    The default is TextEdit.PlainText.  If the text format is TextEdit.AutoText the text edit
     will automatically determine whether the text should be treated as
     rich text.  This determination is made using Qt::mightBeRichText().
 
@@ -1633,6 +1633,7 @@ void QQuickTextEditPrivate::init()
     control = new QQuickTextControl(document, q);
     control->setView(q);
     control->setTextInteractionFlags(Qt::LinksAccessibleByMouse | Qt::TextSelectableByKeyboard | Qt::TextEditable);
+    control->setAcceptRichText(false);
 
     // QQuickTextControl follows the default text color
     // defined by the platform, declarative text
index 909fd95..9886aba 100644 (file)
@@ -75,7 +75,7 @@ public:
       persistentSelection(true), requireImplicitWidth(false), selectByMouse(false), canPaste(false),
       hAlignImplicit(true), rightToLeftText(false), useImageFallback(false),
       textMargin(0.0), lastSelectionStart(0), lastSelectionEnd(0), cursorComponent(0), cursor(0),
-      format(QQuickTextEdit::AutoText), document(0), wrapMode(QQuickTextEdit::NoWrap),
+      format(QQuickTextEdit::PlainText), document(0), wrapMode(QQuickTextEdit::NoWrap),
       mouseSelectionMode(QQuickTextEdit::SelectCharacters),
       lineCount(0), yoff(0), nodeType(NodeIsNull), texture(0)
     {
index 4cd9236..2163838 100644 (file)
@@ -19,6 +19,7 @@ Rectangle {
             anchors.fill: parent
             text: top.text
             focus: true
+            textFormat: TextEdit.AutoText
         }
     }
 }
index 9cc904e..fc7b032 100644 (file)
@@ -336,6 +336,71 @@ void tst_qquicktextedit::text()
         QString componentStr = "import QtQuick 2.0\nTextEdit { text: \"" + richText.at(i) + "\" }";
         QDeclarativeComponent texteditComponent(&engine);
         texteditComponent.setData(componentStr.toLatin1(), QUrl());
+
+        QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
+
+        QVERIFY(textEditObject != 0);
+
+        QString expected = richText.at(i);
+        expected.replace(QRegExp("\\\\(.)"),"\\1");
+        QCOMPARE(textEditObject->text(), expected);
+        QCOMPARE(textEditObject->length(), expected.length());
+    }
+
+    for (int i = 0; i < standard.size(); i++)
+    {
+        QString componentStr = "import QtQuick 2.0\nTextEdit { textFormat: TextEdit.RichText; text: \"" + standard.at(i) + "\" }";
+        QDeclarativeComponent texteditComponent(&engine);
+        texteditComponent.setData(componentStr.toLatin1(), QUrl());
+        QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
+
+        QVERIFY(textEditObject != 0);
+
+        QString actual = textEditObject->text();
+        QString expected = standard.at(i);
+        actual.remove(QRegExp(".*<body[^>]*>"));
+        actual.remove(QRegExp("(<[^>]*>)+"));
+        expected.remove("\n");
+        QCOMPARE(actual.simplified(), expected);
+        QCOMPARE(textEditObject->length(), expected.length());
+    }
+
+    for (int i = 0; i < richText.size(); i++)
+    {
+        QString componentStr = "import QtQuick 2.0\nTextEdit { textFormat: TextEdit.RichText; text: \"" + richText.at(i) + "\" }";
+        QDeclarativeComponent texteditComponent(&engine);
+        texteditComponent.setData(componentStr.toLatin1(), QUrl());
+        QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
+
+        QVERIFY(textEditObject != 0);
+        QString actual = textEditObject->text();
+        QString expected = richText.at(i);
+        actual.replace(QRegExp(".*<body[^>]*>"),"");
+        actual.replace(QRegExp("(<[^>]*>)+"),"<>");
+        expected.replace(QRegExp("(<[^>]*>)+"),"<>");
+        QCOMPARE(actual.simplified(),expected.simplified());
+
+        expected.replace("<>", " ");
+        QCOMPARE(textEditObject->length(), expected.simplified().length());
+    }
+
+    for (int i = 0; i < standard.size(); i++)
+    {
+        QString componentStr = "import QtQuick 2.0\nTextEdit { textFormat: TextEdit.AutoText; text: \"" + standard.at(i) + "\" }";
+        QDeclarativeComponent texteditComponent(&engine);
+        texteditComponent.setData(componentStr.toLatin1(), QUrl());
+        QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
+
+        QVERIFY(textEditObject != 0);
+        QCOMPARE(textEditObject->text(), standard.at(i));
+        QCOMPARE(textEditObject->length(), standard.at(i).length());
+    }
+
+    for (int i = 0; i < richText.size(); i++)
+    {
+        QString componentStr = "import QtQuick 2.0\nTextEdit { textFormat: TextEdit.AutoText; text: \"" + richText.at(i) + "\" }";
+        QDeclarativeComponent texteditComponent(&engine);
+        texteditComponent.setData(componentStr.toLatin1(), QUrl());
         QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
 
         QVERIFY(textEditObject != 0);
@@ -409,7 +474,7 @@ void tst_qquicktextedit::width()
 
         int documentWidth = ceil(document.idealWidth());
 
-        QString componentStr = "import QtQuick 2.0\nTextEdit { text: \"" + richText.at(i) + "\" }";
+        QString componentStr = "import QtQuick 2.0\nTextEdit { textFormat: TextEdit.RichText; text: \"" + richText.at(i) + "\" }";
         QDeclarativeComponent texteditComponent(&engine);
         texteditComponent.setData(componentStr.toLatin1(), QUrl());
         QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
@@ -2467,7 +2532,7 @@ void tst_qquicktextedit::getText()
     QFETCH(int, end);
     QFETCH(QString, expectedText);
 
-    QString componentStr = "import QtQuick 2.0\nTextEdit { text: \"" + text + "\" }";
+    QString componentStr = "import QtQuick 2.0\nTextEdit { textFormat: TextEdit.AutoText; text: \"" + text + "\" }";
     QDeclarativeComponent textEditComponent(&engine);
     textEditComponent.setData(componentStr.toLatin1(), QUrl());
     QQuickTextEdit *textEdit = qobject_cast<QQuickTextEdit*>(textEditComponent.create());