Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Remove uneccessary layout in TextInput initialization.
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>
  • Loading branch information
Andrew den Exter authored and Qt by Nokia committed Dec 21, 2011
1 parent 8def833 commit 71b64a4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 28 deletions.
39 changes: 12 additions & 27 deletions src/quick/items/qquicktextinput.cpp
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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();
}

/*!
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/quick/items/qquicktextinput_p.h
Expand Up @@ -306,7 +306,6 @@ private Q_SLOTS:
void selectionChanged();
void createCursor();
void updateCursorRectangle();
void updateRect(const QRect &r = QRect());
void q_canPasteChanged();

private:
Expand Down
5 changes: 5 additions & 0 deletions tests/auto/qtquick2/qquicktextinput/tst_qquicktextinput.cpp
Expand Up @@ -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);
Expand Down Expand Up @@ -2045,6 +2046,7 @@ void tst_qquicktextinput::cursorRectangle()

QQuickTextInput input;
input.setText(text);
input.componentComplete();

QTextLayout layout(text);
layout.setFont(input.font());
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 71b64a4

Please sign in to comment.