aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick
diff options
context:
space:
mode:
authorAndrew den Exter <andrew.den-exter@nokia.com>2012-04-18 17:20:24 +1000
committerQt by Nokia <qt-info@nokia.com>2012-04-19 06:16:28 +0200
commit7bd68f6447c79aa58a9854bb6c44db00ea36a06a (patch)
treefd5145dda805cd19a2aa2541567a280a5d1fb915 /src/quick
parent458eb3cce93162dced2649e44c59f27bb088f07c (diff)
Ensure the cursor delegate position is correct when wrapping.
When inserting text into a TextEdit with implicit width and wrapping the cursor delegate position has to be updated after the width is expanded to the implicit width otherwise it will be positioned in a wrapped location. Change-Id: Ibcb709ec1b4f6827ea8ae919f2e0c932c7372869 Reviewed-by: Martin Jones <martin.jones@nokia.com>
Diffstat (limited to 'src/quick')
-rw-r--r--src/quick/items/qquicktextcontrol.cpp47
-rw-r--r--src/quick/items/qquicktextcontrol_p.h2
-rw-r--r--src/quick/items/qquicktextcontrol_p_p.h1
-rw-r--r--src/quick/items/qquicktextedit.cpp8
4 files changed, 37 insertions, 21 deletions
diff --git a/src/quick/items/qquicktextcontrol.cpp b/src/quick/items/qquicktextcontrol.cpp
index f63e6a7523..7fa990ba9f 100644
--- a/src/quick/items/qquicktextcontrol.cpp
+++ b/src/quick/items/qquicktextcontrol.cpp
@@ -110,7 +110,8 @@ QQuickTextControlPrivate::QQuickTextControlPrivate()
isEnabled(true),
hadSelectionOnMousePress(false),
wordSelectionEnabled(false),
- hasImState(false)
+ hasImState(false),
+ cursorRectangleChanged(false)
{}
bool QQuickTextControlPrivate::cursorMoveKeyEvent(QKeyEvent *e)
@@ -253,7 +254,7 @@ bool QQuickTextControlPrivate::cursorMoveKeyEvent(QKeyEvent *e)
if (moved) {
if (cursor.position() != oldCursorPos)
emit q->cursorPositionChanged();
- emit q->cursorRectangleChanged();
+ q->updateCursorRectangle(true);
} else if (isNavigationEvent && oldSelection.anchor() == cursor.anchor()) {
return false;
}
@@ -275,7 +276,7 @@ void QQuickTextControlPrivate::updateCurrentCharFormat()
lastCharFormat = fmt;
emit q->currentCharFormatChanged(fmt);
- emit q->cursorRectangleChanged();
+ cursorRectangleChanged = true;
}
void QQuickTextControlPrivate::init(Qt::TextFormat format, const QString &text, QTextDocument *document)
@@ -366,7 +367,7 @@ void QQuickTextControlPrivate::setContent(Qt::TextFormat format, const QString &
if (!document)
doc->setModified(false);
- emit q->cursorRectangleChanged();
+ q->updateCursorRectangle(true);
emit q->cursorPositionChanged();
}
@@ -443,7 +444,7 @@ void QQuickTextControlPrivate::selectionChanged(bool forceEmitSelectionChanged /
qGuiApp->inputMethod()->update(Qt::ImCurrentSelection);
emit q->selectionChanged();
}
- emit q->cursorRectangleChanged();
+ q->updateCursorRectangle(true);
}
void QQuickTextControlPrivate::_q_updateCurrentCharFormatAndSelection()
@@ -469,7 +470,7 @@ void QQuickTextControlPrivate::_q_emitCursorPosChanged(const QTextCursor &someCu
Q_Q(QQuickTextControl);
if (someCursor.isCopyOf(cursor)) {
emit q->cursorPositionChanged();
- emit q->cursorRectangleChanged();
+ cursorRectangleChanged = true;
}
}
@@ -592,7 +593,7 @@ void QQuickTextControl::undo()
d->doc->undo(&d->cursor);
if (d->cursor.position() != oldCursorPos)
emit cursorPositionChanged();
- emit cursorRectangleChanged();
+ updateCursorRectangle(true);
}
void QQuickTextControl::redo()
@@ -601,9 +602,9 @@ void QQuickTextControl::redo()
d->repaintSelection();
const int oldCursorPos = d->cursor.position();
d->doc->redo(&d->cursor);
- if (d->cursor.position() != oldCursorPos)
+ if (d->cursor.position() != oldCursorPos)
emit cursorPositionChanged();
- emit cursorRectangleChanged();
+ updateCursorRectangle(true);
}
QQuickTextControl::QQuickTextControl(QTextDocument *doc, QObject *parent)
@@ -623,6 +624,15 @@ QTextDocument *QQuickTextControl::document() const
return d->doc;
}
+void QQuickTextControl::updateCursorRectangle(bool force)
+{
+ Q_D(QQuickTextControl);
+ const bool update = d->cursorRectangleChanged || force;
+ d->cursorRectangleChanged = false;
+ if (update)
+ emit cursorRectangleChanged();
+}
+
void QQuickTextControl::setTextCursor(const QTextCursor &cursor)
{
Q_D(QQuickTextControl);
@@ -633,7 +643,7 @@ void QQuickTextControl::setTextCursor(const QTextCursor &cursor)
d->cursor = cursor;
d->cursorOn = d->hasFocus && (d->interactionFlags & Qt::TextEditable);
d->_q_updateCurrentCharFormatAndSelection();
- emit cursorRectangleChanged();
+ updateCursorRectangle(true);
d->repaintOldAndNewSelection(oldSelection);
if (posChanged)
emit cursorPositionChanged();
@@ -956,8 +966,7 @@ process:
e->accept();
cursorOn = true;
- emit q->cursorRectangleChanged();
-
+ q->updateCursorRectangle(true);
updateCurrentCharFormat();
}
@@ -1198,14 +1207,14 @@ void QQuickTextControlPrivate::mousePressEvent(QMouseEvent *e, const QPointF &po
}
if (interactionFlags & Qt::TextEditable) {
- emit q->cursorRectangleChanged();
+ q->updateCursorRectangle(true);
if (cursor.position() != oldCursorPos)
emit q->cursorPositionChanged();
_q_updateCurrentCharFormatAndSelection();
} else {
if (cursor.position() != oldCursorPos) {
emit q->cursorPositionChanged();
- emit q->cursorRectangleChanged();
+ q->updateCursorRectangle(true);
}
selectionChanged();
}
@@ -1316,7 +1325,7 @@ void QQuickTextControlPrivate::mouseReleaseEvent(QMouseEvent *e, const QPointF &
if (cursor.position() != oldCursorPos) {
emit q->cursorPositionChanged();
- emit q->cursorRectangleChanged();
+ q->updateCursorRectangle(true);
}
if (interactionFlags & Qt::LinksAccessibleByMouse) {
@@ -1435,7 +1444,6 @@ void QQuickTextControlPrivate::inputMethodEvent(QInputMethodEvent *e)
int blockStart = a.start + cursor.block().position();
cursor.setPosition(blockStart, QTextCursor::MoveAnchor);
cursor.setPosition(blockStart + a.length, QTextCursor::KeepAnchor);
- emit q->cursorRectangleChanged();
repaintOldAndNewSelection(oldCursor);
forceSelectionChanged = true;
}
@@ -1475,8 +1483,7 @@ void QQuickTextControlPrivate::inputMethodEvent(QInputMethodEvent *e)
QTextCursorPrivate *cursor_d = QTextCursorPrivate::getPrivate(&cursor);
if (cursor_d)
cursor_d->setX();
- if (oldPreeditCursor != preeditCursor)
- emit q->cursorRectangleChanged();
+ q->updateCursorRectangle(oldPreeditCursor != preeditCursor || forceSelectionChanged);
selectionChanged(forceSelectionChanged);
}
@@ -1609,7 +1616,7 @@ void QQuickTextControl::moveCursor(QTextCursor::MoveOperation op, QTextCursor::M
const QTextCursor oldSelection = d->cursor;
const bool moved = d->cursor.movePosition(op, mode);
d->_q_updateCurrentCharFormatAndSelection();
- emit cursorRectangleChanged();
+ updateCursorRectangle(true);
d->repaintOldAndNewSelection(oldSelection);
if (moved)
emit cursorPositionChanged();
@@ -1690,7 +1697,7 @@ void QQuickTextControl::insertFromMimeData(const QMimeData *source)
if (hasData)
d->cursor.insertFragment(fragment);
- emit cursorRectangleChanged();
+ updateCursorRectangle(true);
}
void QQuickTextControlPrivate::activateLinkUnderCursor(QString href)
diff --git a/src/quick/items/qquicktextcontrol_p.h b/src/quick/items/qquicktextcontrol_p.h
index aae9e12dad..57c26a16f3 100644
--- a/src/quick/items/qquicktextcontrol_p.h
+++ b/src/quick/items/qquicktextcontrol_p.h
@@ -119,6 +119,8 @@ public:
void setCursorIsFocusIndicator(bool b);
void setWordSelectionEnabled(bool enabled);
+ void updateCursorRectangle(bool force);
+
virtual int hitTest(const QPointF &point, Qt::HitTestAccuracy accuracy) const;
virtual QRectF blockBoundingRect(const QTextBlock &block) const;
diff --git a/src/quick/items/qquicktextcontrol_p_p.h b/src/quick/items/qquicktextcontrol_p_p.h
index 3a10f007be..f39bf673d7 100644
--- a/src/quick/items/qquicktextcontrol_p_p.h
+++ b/src/quick/items/qquicktextcontrol_p_p.h
@@ -162,6 +162,7 @@ public:
bool hadSelectionOnMousePress : 1;
bool wordSelectionEnabled : 1;
bool hasImState : 1;
+ bool cursorRectangleChanged : 1;
void _q_copyLink();
void _q_updateBlock(const QTextBlock &);
diff --git a/src/quick/items/qquicktextedit.cpp b/src/quick/items/qquicktextedit.cpp
index f727c54322..6fefab4166 100644
--- a/src/quick/items/qquicktextedit.cpp
+++ b/src/quick/items/qquicktextedit.cpp
@@ -897,6 +897,7 @@ void QQuickTextEdit::setCursorPosition(int pos)
return;
cursor.setPosition(pos);
d->control->setTextCursor(cursor);
+ d->control->updateCursorRectangle(true);
}
/*!
@@ -1143,9 +1144,12 @@ void QQuickTextEdit::geometryChanged(const QRectF &newGeometry,
const QRectF &oldGeometry)
{
Q_D(QQuickTextEdit);
- if (newGeometry.width() != oldGeometry.width() && d->wrapMode != NoWrap && !d->inLayout)
+ if (newGeometry.width() != oldGeometry.width() && d->wrapMode != NoWrap && !d->inLayout) {
updateSize();
+ moveCursorDelegate();
+ }
QQuickImplicitSizeItem::geometryChanged(newGeometry, oldGeometry);
+
}
/*!
@@ -2169,6 +2173,7 @@ void QQuickTextEdit::insert(int position, const QString &text)
} else {
cursor.insertText(text);
}
+ d->control->updateCursorRectangle(false);
}
/*!
@@ -2186,6 +2191,7 @@ void QQuickTextEdit::remove(int start, int end)
cursor.setPosition(start, QTextCursor::MoveAnchor);
cursor.setPosition(end, QTextCursor::KeepAnchor);
cursor.removeSelectedText();
+ d->control->updateCursorRectangle(false);
}
QT_END_NAMESPACE