From 5c6947a4d8808230080c4644689c9a46db4d7da9 Mon Sep 17 00:00:00 2001
From: Joona Petrell <joona.t.petrell@nokia.com>
Date: Wed, 21 Jul 2010 15:40:56 +1000
Subject: [PATCH] Add lineCount property to TextEdit and Text elements

Task-number: QTBUG-12305
Reviewed-by:
---
 src/declarative/graphicsitems/qdeclarativetext.cpp |   21 ++++++++++-
 src/declarative/graphicsitems/qdeclarativetext_p.h |    4 ++
 .../graphicsitems/qdeclarativetextedit.cpp         |   19 +++++++++
 .../graphicsitems/qdeclarativetextedit_p.h         |    4 ++
 .../graphicsitems/qdeclarativetextedit_p_p.h       |    3 +-
 .../qdeclarativetext/tst_qdeclarativetext.cpp      |   41 ++++++++++++++++++++
 .../tst_qdeclarativetextedit.cpp                   |   26 ++++++++++++-
 7 files changed, 115 insertions(+), 3 deletions(-)

diff --git a/src/declarative/graphicsitems/qdeclarativetext.cpp b/src/declarative/graphicsitems/qdeclarativetext.cpp
index a1703c1..1a3ab8b 100644
--- a/src/declarative/graphicsitems/qdeclarativetext.cpp
+++ b/src/declarative/graphicsitems/qdeclarativetext.cpp
@@ -358,7 +358,7 @@ void QDeclarativeText::setText(const QString &n)
     Q_D(QDeclarativeText);
     if (d->text == n)
         return;
-
+    int oldLineCount = lineCount();
     d->richText = d->format == RichText || (d->format == AutoText && Qt::mightBeRichText(n));
     if (d->richText) {
         if (isComponentComplete()) {
@@ -371,6 +371,25 @@ void QDeclarativeText::setText(const QString &n)
     d->updateLayout();
     d->markImgDirty();
     emit textChanged(d->text);
+    if (d->doc && oldLineCount != lineCount()) {
+        emit lineCountChanged();
+    }
+}
+
+/*!
+    \qmlproperty int Text::lineCount
+
+    The number of lines in the text.
+    This property is read-only.
+*/
+int QDeclarativeText::lineCount() const
+{
+    Q_D(const QDeclarativeText);
+    if(d->richText) {
+        return d->doc ? d->doc->lineCount() : 0;
+    } else {
+        return d->layout.lineCount();
+    }
 }
 
 /*!
diff --git a/src/declarative/graphicsitems/qdeclarativetext_p.h b/src/declarative/graphicsitems/qdeclarativetext_p.h
index 2cc4d52..73f69c5 100644
--- a/src/declarative/graphicsitems/qdeclarativetext_p.h
+++ b/src/declarative/graphicsitems/qdeclarativetext_p.h
@@ -64,6 +64,7 @@ class Q_DECLARATIVE_EXPORT QDeclarativeText : public QDeclarativeItem
     Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
     Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged)
     Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
+    Q_PROPERTY(int lineCount READ lineCount NOTIFY lineCountChanged)
     Q_PROPERTY(TextStyle style READ style WRITE setStyle NOTIFY styleChanged)
     Q_PROPERTY(QColor styleColor READ styleColor WRITE setStyleColor NOTIFY styleColorChanged)
     Q_PROPERTY(HAlignment horizontalAlignment READ hAlign WRITE setHAlign NOTIFY horizontalAlignmentChanged)
@@ -145,6 +146,8 @@ public:
 
     QRectF boundingRect() const;
 
+    int lineCount() const;
+
 Q_SIGNALS:
     void textChanged(const QString &text);
     void linkActivated(const QString &link);
@@ -158,6 +161,7 @@ Q_SIGNALS:
     void textFormatChanged(TextFormat textFormat);
     void elideModeChanged(TextElideMode mode);
     void paintedSizeChanged();
+    void lineCountChanged();
 
 protected:
     void mousePressEvent(QGraphicsSceneMouseEvent *event);
diff --git a/src/declarative/graphicsitems/qdeclarativetextedit.cpp b/src/declarative/graphicsitems/qdeclarativetextedit.cpp
index f7b2ebf..617a8a5 100644
--- a/src/declarative/graphicsitems/qdeclarativetextedit.cpp
+++ b/src/declarative/graphicsitems/qdeclarativetextedit.cpp
@@ -271,6 +271,18 @@ void QDeclarativeTextEdit::setText(const QString &text)
 }
 
 /*!
+    \qmlproperty int TextEdit::lineCount
+
+    The number of lines in the text.
+    This property is read-only.
+*/
+int QDeclarativeTextEdit::lineCount() const
+{
+    Q_D(const QDeclarativeTextEdit);
+    return d->document ? d->document->lineCount() : 0;
+}
+
+/*!
     \qmlproperty enumeration TextEdit::textFormat
 
     The way the text property should be displayed.
@@ -1291,9 +1303,16 @@ void QDeclarativeTextEditPrivate::init()
 
 void QDeclarativeTextEdit::q_textChanged()
 {
+    Q_D(QDeclarativeTextEdit);
     updateSize();
     updateMicroFocus();
     emit textChanged(text());
+
+    int lineCount = d->document ? d->document->lineCount() : 0;
+    if (lineCount != d->previousLineCount) {
+        d->previousLineCount = lineCount;
+        emit lineCountChanged();
+    }
 }
 
 void QDeclarativeTextEdit::moveCursorDelegate()
diff --git a/src/declarative/graphicsitems/qdeclarativetextedit_p.h b/src/declarative/graphicsitems/qdeclarativetextedit_p.h
index 68fde3d..1f6c9c9 100644
--- a/src/declarative/graphicsitems/qdeclarativetextedit_p.h
+++ b/src/declarative/graphicsitems/qdeclarativetextedit_p.h
@@ -88,6 +88,7 @@ class Q_AUTOTEST_EXPORT QDeclarativeTextEdit : public QDeclarativePaintedItem
     Q_PROPERTY(bool activeFocusOnPress READ focusOnPress WRITE setFocusOnPress NOTIFY activeFocusOnPressChanged)
     Q_PROPERTY(bool persistentSelection READ persistentSelection WRITE setPersistentSelection NOTIFY persistentSelectionChanged)
     Q_PROPERTY(qreal textMargin READ textMargin WRITE setTextMargin NOTIFY textMarginChanged)
+    Q_PROPERTY(int lineCount READ lineCount NOTIFY lineCountChanged)
     Q_PROPERTY(Qt::InputMethodHints inputMethodHints READ inputMethodHints WRITE setInputMethodHints)
     Q_PROPERTY(bool selectByMouse READ selectByMouse WRITE setSelectByMouse NOTIFY selectByMouseChanged)
 
@@ -172,6 +173,8 @@ public:
     qreal textMargin() const;
     void setTextMargin(qreal margin);
 
+    int lineCount() const;
+
     bool selectByMouse() const;
     void setSelectByMouse(bool);
 
@@ -220,6 +223,7 @@ Q_SIGNALS:
     void persistentSelectionChanged(bool isPersistentSelection);
     void textMarginChanged(qreal textMargin);
     void selectByMouseChanged(bool selectByMouse);
+    void lineCountChanged();
 
 public Q_SLOTS:
     void selectAll();
diff --git a/src/declarative/graphicsitems/qdeclarativetextedit_p_p.h b/src/declarative/graphicsitems/qdeclarativetextedit_p_p.h
index 4092e65..f331b2f 100644
--- a/src/declarative/graphicsitems/qdeclarativetextedit_p_p.h
+++ b/src/declarative/graphicsitems/qdeclarativetextedit_p_p.h
@@ -71,7 +71,7 @@ public:
       : color("black"), hAlign(QDeclarativeTextEdit::AlignLeft), vAlign(QDeclarativeTextEdit::AlignTop),
       imgDirty(true), dirty(false), richText(false), cursorVisible(false), focusOnPress(true),
       showInputPanelOnFocus(true), clickCausedFocus(false), persistentSelection(true), textMargin(0.0),
-      lastSelectionStart(0), lastSelectionEnd(0), cursorComponent(0), cursor(0),
+      lastSelectionStart(0), lastSelectionEnd(0), previousLineCount(1), cursorComponent(0), cursor(0),
       format(QDeclarativeTextEdit::AutoText), document(0), wrapMode(QDeclarativeTextEdit::NoWrap),
       selectByMouse(false),
       yoff(0)
@@ -112,6 +112,7 @@ public:
     qreal textMargin;
     int lastSelectionStart;
     int lastSelectionEnd;
+    int previousLineCount;
     QDeclarativeComponent* cursorComponent;
     QDeclarativeItem* cursor;
     QDeclarativeTextEdit::TextFormat format;
diff --git a/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp b/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp
index 821394d..943dafd 100644
--- a/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp
+++ b/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp
@@ -85,6 +85,7 @@ private slots:
     void style();
     void color();
     void smooth();
+    void lineCount();
 
     // QDeclarativeFontValueType
     void weight();
@@ -699,6 +700,46 @@ void tst_qdeclarativetext::smooth()
     }
 }
 
+void tst_qdeclarativetext::lineCount()
+{
+    QString componentStr = "import Qt 4.7\nText { }";
+    QDeclarativeComponent textComponent(&engine);
+    textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
+    QDeclarativeText *textObject = qobject_cast<QDeclarativeText*>(textComponent.create());
+
+    QVERIFY(textObject != 0);
+
+    QCOMPARE(textObject->lineCount(), 0);
+
+    textObject->setText("Hello world!");
+    QCOMPARE(textObject->lineCount(), 1);
+
+    textObject->setText("Hello world!\nHello world!");
+    QCOMPARE(textObject->lineCount(), 2);
+
+    textObject->setText("Hello world!\nHello world!\nHello world!");
+    QCOMPARE(textObject->lineCount(), 3);
+
+    textObject->setText("");
+    QCOMPARE(textObject->lineCount(), 0);
+
+    textObject->setTextFormat(QDeclarativeText::RichText);
+
+    QCOMPARE(textObject->lineCount(), 1);
+
+    textObject->setText("Hello world!");
+    QCOMPARE(textObject->lineCount(), 1);
+
+    textObject->setText("Hello world!\nHello world!");
+    QCOMPARE(textObject->lineCount(), 2);
+
+    textObject->setText("Hello world!\nHello world!\nHello world!");
+    QCOMPARE(textObject->lineCount(), 3);
+
+    textObject->setText("");
+    QCOMPARE(textObject->lineCount(), 1);
+}
+
 void tst_qdeclarativetext::weight()
 {
     {
diff --git a/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp b/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp
index 93351a8..c7e3610 100644
--- a/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp
+++ b/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp
@@ -102,13 +102,13 @@ private slots:
     void font();
     void color();
     void textMargin();
+    void lineCount();
     void persistentSelection();
     void focusOnPress();
     void selection();
     void mouseSelection_data();
     void mouseSelection();
     void inputMethodHints();
-
     void cursorDelegate();
     void delegateLoading_data();
     void delegateLoading();
@@ -580,6 +580,30 @@ void tst_qdeclarativetextedit::textMargin()
     }
 }
 
+void tst_qdeclarativetextedit::lineCount()
+{
+    QString componentStr = "import Qt 4.7\nTextEdit { }";
+    QDeclarativeComponent textComponent(&engine);
+    textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
+    QDeclarativeTextEdit *textEditObject = qobject_cast<QDeclarativeTextEdit*>(textComponent.create());
+
+    QVERIFY(textEditObject != 0);
+
+    QCOMPARE(textEditObject->lineCount(), 1);
+
+    textEditObject->setText("Hello world!");
+    QCOMPARE(textEditObject->lineCount(), 1);
+
+    textEditObject->setText("Hello world!\nHello world!");
+    QCOMPARE(textEditObject->lineCount(), 2);
+
+    textEditObject->setText("Hello world!\nHello world!\nHello world!");
+    QCOMPARE(textEditObject->lineCount(), 3);
+
+    textEditObject->setText("");
+    QCOMPARE(textEditObject->lineCount(), 1);
+}
+
 void tst_qdeclarativetextedit::persistentSelection()
 {
     {
-- 
1.6.6.1

