QWidgetTextControl: only show cursor when having focus

Commit d3dcc6f introduced a regression to QTextEdit, leaving
it to always show the cursor regardless of focus. The reason
is that QTextArea used to toggle visibilty by setting the
cursor blink rate. What it really wanted to do was to set
cursor visibility explicit.

This patch implements function setCursorVisible, and updates
the places that used to call setBlinkingCursorEnabled to
instead call setCursorVisible (where it makes sense).

Change-Id: I58ea965178d4faaf5b09f6f6a37a37d5e8b99c97
Reviewed-by: Jan Arve Sæther <jan-arve.saether@theqtcompany.com>
This commit is contained in:
Richard Moe Gustavsen 2016-04-29 14:41:08 +02:00
parent 392372392c
commit 3cdc02d382
2 changed files with 18 additions and 6 deletions

View File

@ -112,7 +112,7 @@ static QTextLine currentTextLine(const QTextCursor &cursor)
}
QWidgetTextControlPrivate::QWidgetTextControlPrivate()
: doc(0), cursorOn(false), blinkingEnabled(false), cursorIsFocusIndicator(false),
: doc(0), cursorOn(false), blinkingEnabled(true), cursorVisible(false), cursorIsFocusIndicator(false),
#ifndef Q_OS_ANDROID
interactionFlags(Qt::TextEditorInteraction),
#else
@ -685,6 +685,16 @@ void QWidgetTextControlPrivate::_q_documentLayoutChanged()
}
void QWidgetTextControlPrivate::setCursorVisible(bool visible)
{
if (cursorVisible == visible)
return;
cursorVisible = visible;
updateCursorBlinking();
}
void QWidgetTextControlPrivate::setBlinkingCursorEnabled(bool enable)
{
if (blinkingEnabled == enable)
@ -703,13 +713,13 @@ void QWidgetTextControlPrivate::setBlinkingCursorEnabled(bool enable)
void QWidgetTextControlPrivate::updateCursorBlinking()
{
cursorBlinkTimer.stop();
if (blinkingEnabled) {
if (cursorVisible && blinkingEnabled) {
int flashTime = QGuiApplication::styleHints()->cursorFlashTime();
if (flashTime >= 2)
cursorBlinkTimer.start(flashTime / 2, q_func());
}
cursorOn = true;
cursorOn = cursorVisible;
repaintCursor();
}
@ -2170,13 +2180,13 @@ void QWidgetTextControlPrivate::focusEvent(QFocusEvent *e)
#endif
cursorOn = (interactionFlags & (Qt::TextSelectableByKeyboard | Qt::TextEditable));
if (interactionFlags & Qt::TextEditable) {
setBlinkingCursorEnabled(true);
setCursorVisible(true);
}
#ifdef QT_KEYPAD_NAVIGATION
}
#endif
} else {
setBlinkingCursorEnabled(false);
setCursorVisible(false);
if (cursorIsFocusIndicator
&& e->reason() != Qt::ActiveWindowFocusReason
@ -2985,7 +2995,7 @@ void QWidgetTextControl::setTextInteractionFlags(Qt::TextInteractionFlags flags)
d->interactionFlags = flags;
if (d->hasFocus)
d->setBlinkingCursorEnabled(flags & Qt::TextEditable);
d->setCursorVisible(flags & Qt::TextEditable);
}
Qt::TextInteractionFlags QWidgetTextControl::textInteractionFlags() const

View File

@ -111,6 +111,7 @@ public:
void _q_emitCursorPosChanged(const QTextCursor &someCursor);
void _q_contentsChanged(int from, int charsRemoved, int charsAdded);
void setCursorVisible(bool visible);
void setBlinkingCursorEnabled(bool enable);
void updateCursorBlinking();
@ -177,6 +178,7 @@ public:
QTextDocument *doc;
bool cursorOn;
bool blinkingEnabled;
bool cursorVisible;
QTextCursor cursor;
bool cursorIsFocusIndicator;
QTextCharFormat lastCharFormat;