Remove uneccessary layout in TextInput initialization.
authorAndrew den Exter <andrew.den-exter@nokia.com>
Wed, 21 Dec 2011 07:36:13 +0000 (17:36 +1000)
committerQt by Nokia <qt-info@nokia.com>
Wed, 21 Dec 2011 07:57:12 +0000 (08:57 +0100)
The layout was only done to ensure there was always at least one
QTextLine in the layout to avoid validity checks later, but since
lineForTextPosition can return an invalid QTextLine the checks are still
needed anyway.

Change-Id: Iae65e3460812a60e2aafecd553bf4241bd640d04
Reviewed-by: Alan Alpert <alan.alpert@nokia.com>
src/quick/items/qquicktextinput.cpp
src/quick/items/qquicktextinput_p.h
tests/auto/qtquick2/qquicktextinput/tst_qquicktextinput.cpp

index b58e57e..e6da163 100644 (file)
@@ -602,11 +602,7 @@ void QQuickTextInput::setCursorVisible(bool on)
         return;
     d->cursorVisible = on;
     d->setCursorBlinkPeriod(on ? qApp->styleHints()->cursorFlashTime() : 0);
-    QRect r = cursorRectangle();
-    if (d->inputMask().isEmpty())
-        updateRect(r);
-    else
-        updateRect();
+    update();
     emit cursorVisibleChanged(d->cursorVisible);
 }
 
@@ -639,9 +635,11 @@ QRect QQuickTextInput::cursorRectangle() const
     int c = d->m_cursor;
     if (d->m_preeditCursor != -1)
         c += d->m_preeditCursor;
-    if (d->m_echoMode == NoEcho || !isComponentComplete())
+    if (d->m_echoMode == NoEcho)
         c = 0;
     QTextLine l = d->m_textLayout.lineForTextPosition(c);
+    if (!l.isValid())
+        return QRect();
     return QRect(
             qRound(l.cursorToX(c) - d->hscroll),
             qRound(l.y() - d->vscroll),
@@ -1102,7 +1100,9 @@ QRectF QQuickTextInput::positionToRectangle(int pos) const
     if (pos > d->m_cursor)
         pos += d->preeditAreaText().length();
     QTextLine l = d->m_textLayout.lineAt(0);
-    return QRectF(l.cursorToX(pos) - d->hscroll, 0.0, d->m_cursorWidth, l.height());
+    return l.isValid()
+            ? QRectF(l.cursorToX(pos) - d->hscroll, 0.0, d->m_cursorWidth, l.height())
+            : QRectF();
 }
 
 /*!
@@ -1175,7 +1175,7 @@ int QQuickTextInputPrivate::positionAt(int x, int y, QTextLine::CursorPosition p
             break;
         line = nextLine;
     }
-    return line.xToCursor(x, position);
+    return line.isValid() ? line.xToCursor(x, position) : 0;
 }
 
 void QQuickTextInput::keyPressEvent(QKeyEvent* ev)
@@ -2049,9 +2049,6 @@ void QQuickTextInputPrivate::init()
     q->connect(QGuiApplication::clipboard(), SIGNAL(dataChanged()),
             q, SLOT(q_canPasteChanged()));
 #endif // QT_NO_CLIPBOARD
-    m_textLayout.beginLayout();
-    m_textLayout.createLine();
-    m_textLayout.endLayout();
 
     imHints &= ~Qt::ImhMultiLine;
     oldValidity = hasAcceptableInput(m_text);
@@ -2089,7 +2086,8 @@ void QQuickTextInput::updateCursorRectangle()
 void QQuickTextInput::selectionChanged()
 {
     Q_D(QQuickTextInput);
-    updateRect();//TODO: Only update rect in selection
+    d->textLayoutDirty = true; //TODO: Only update rect in selection
+    update();
     emit selectedTextChanged();
 
     if (d->lastSelectionStart != d->selectionStart()) {
@@ -2118,19 +2116,6 @@ void QQuickTextInputPrivate::hideCursor()
         textNode->cursorNode()->setColor(QColor(0, 0, 0, 0));
 }
 
-void QQuickTextInput::updateRect(const QRect &r)
-{
-    Q_D(QQuickTextInput);
-    if (!isComponentComplete())
-        return;
-
-    if (r.isEmpty()) {
-        d->textLayoutDirty = true;
-    }
-
-    update();
-}
-
 QRectF QQuickTextInput::boundingRect() const
 {
     Q_D(const QQuickTextInput);
@@ -3417,7 +3402,7 @@ void QQuickTextInputPrivate::setCursorBlinkPeriod(int msec)
     } else {
         m_blinkTimer = 0;
         if (m_blinkStatus == 1)
-            emit q->updateRect(inputMask().isEmpty() ? q->cursorRectangle() : QRect());
+            q->update();
     }
     m_blinkPeriod = msec;
 }
@@ -3437,7 +3422,7 @@ void QQuickTextInput::timerEvent(QTimerEvent *event)
     Q_D(QQuickTextInput);
     if (event->timerId() == d->m_blinkTimer) {
         d->m_blinkStatus = !d->m_blinkStatus;
-        updateRect(inputMask().isEmpty() ? cursorRectangle() : QRect());
+        update();
     } else if (event->timerId() == d->m_deleteAllTimer) {
         killTimer(d->m_deleteAllTimer);
         d->m_deleteAllTimer = 0;
index 447f333..0b63e60 100644 (file)
@@ -306,7 +306,6 @@ private Q_SLOTS:
     void selectionChanged();
     void createCursor();
     void updateCursorRectangle();
-    void updateRect(const QRect &r = QRect());
     void q_canPasteChanged();
 
 private:
index 5576345..266b0d4 100644 (file)
@@ -1996,6 +1996,7 @@ void tst_qquicktextinput::cursorVisible()
     QTRY_COMPARE(&view, qGuiApp->focusWindow());
 
     QQuickTextInput input;
+    input.componentComplete();
     QSignalSpy spy(&input, SIGNAL(cursorVisibleChanged(bool)));
 
     QCOMPARE(input.isCursorVisible(), false);
@@ -2045,6 +2046,7 @@ void tst_qquicktextinput::cursorRectangle()
 
     QQuickTextInput input;
     input.setText(text);
+    input.componentComplete();
 
     QTextLayout layout(text);
     layout.setFont(input.font());
@@ -2379,6 +2381,7 @@ void tst_qquicktextinput::openInputPanel()
     // input panel should stay visible if focus is lost to another text inputor
     QSignalSpy inputPanelVisibilitySpy(qApp->inputPanel(), SIGNAL(visibleChanged()));
     QQuickTextInput anotherInput;
+    anotherInput.componentComplete();
     anotherInput.setParentItem(view.rootObject());
     anotherInput.setFocus(true);
     QCOMPARE(qApp->inputPanel()->visible(), true);
@@ -2465,6 +2468,8 @@ void tst_qquicktextinput::focusOutClearSelection()
     input.setFocus(true);
     input2.setParentItem(view.rootItem());
     input.setParentItem(view.rootItem());
+    input.componentComplete();
+    input2.componentComplete();
     view.show();
     view.requestActivateWindow();
     QTest::qWaitForWindowShown(&view);