diff options
author | Richard Moe Gustavsen <richard.gustavsen@theqtcompany.com> | 2016-04-20 11:35:42 +0200 |
---|---|---|
committer | Richard Moe Gustavsen <richard.gustavsen@theqtcompany.com> | 2016-04-21 13:15:18 +0000 |
commit | c598d52561e048319c6c3656bedf4bb665603dc7 (patch) | |
tree | 3f59da3e0aa75ef43591b5f42ba51037eedf8775 /src | |
parent | 597370b36487d2d2ed438db531f2d4aad4e55744 (diff) |
QQuickTextInput: Listen for changes to cursorFlashTime
cursorFlashTime will now change dynamically from QPA while platform
controlled text selection (on mobile) is ongoing. This patch
will therefore update QWidgetLineControl so that it listens to the
cursorFlashTimeChanged signal and changes the blinking rate when
triggered.
The previous code had a function setCursorBlinkPeriod, which is now
changed to setBlinkingCursorEnabled (like in QQuickTextControlPrivate).
This is because all callers of the function did either pass
"QApplication::cursorFlashTime" or "0", which basically means enable
or disable blinking. This moves the control of the blinking rate
fully to updateCursorFlashTime, which simplifies the code a
bit, especially when cursorFlashTime can change.
Note that when setting a blink period to 0, it means "show the
cursor without blinking". AFAICS, the current implementation did
not guarantee that. This is now made more explicit in the code. If
hiding the cursor is needed, a separate function "setCursorVisible"
is already available for controlling that.
Change-Id: Ie3222525474e03b485ff8585fd8d7da6fd5b26e5
Reviewed-by: Jan Arve Sæther <jan-arve.saether@theqtcompany.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/quick/items/qquicktextinput.cpp | 71 | ||||
-rw-r--r-- | src/quick/items/qquicktextinput_p_p.h | 12 |
2 files changed, 42 insertions, 41 deletions
diff --git a/src/quick/items/qquicktextinput.cpp b/src/quick/items/qquicktextinput.cpp index f93857a892..ab7a0f02dd 100644 --- a/src/quick/items/qquicktextinput.cpp +++ b/src/quick/items/qquicktextinput.cpp @@ -760,12 +760,8 @@ void QQuickTextInput::setCursorVisible(bool on) d->cursorVisible = on; if (on && isComponentComplete()) QQuickTextUtil::createCursor(d); - if (!d->cursorItem) { - d->setCursorBlinkPeriod(on ? QGuiApplication::styleHints()->cursorFlashTime() : 0); - d->updateType = QQuickTextInputPrivate::UpdatePaintNode; - polish(); - update(); - } + if (!d->cursorItem) + d->updateCursorBlinking(); emit cursorVisibleChanged(d->cursorVisible); } @@ -1782,7 +1778,7 @@ QSGNode *QQuickTextInput::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData node = new QQuickTextNode(this); d->textNode = node; - const bool showCursor = !isReadOnly() && d->cursorItem == 0 && d->cursorVisible && (d->m_blinkStatus || d->m_blinkPeriod == 0); + const bool showCursor = !isReadOnly() && d->cursorItem == 0 && d->cursorVisible && d->m_blinkStatus; if (!d->textLayoutDirty && oldNode != 0) { if (showCursor) @@ -2518,8 +2514,10 @@ void QQuickTextInputPrivate::handleFocusEvent(QFocusEvent *event) { Q_Q(QQuickTextInput); bool focus = event->gotFocus(); - if (!m_readOnly) + if (!m_readOnly) { q->setCursorVisible(focus); + setBlinkingCursorEnabled(focus); + } if (focus) { q->q_updateAlignment(); #ifndef QT_NO_IM @@ -4219,26 +4217,39 @@ bool QQuickTextInputPrivate::emitCursorPositionChanged() } -void QQuickTextInputPrivate::setCursorBlinkPeriod(int msec) +void QQuickTextInputPrivate::setBlinkingCursorEnabled(bool enable) { - Q_Q(QQuickTextInput); - if (msec == m_blinkPeriod) + if (enable == m_blinkEnabled) return; + + m_blinkEnabled = enable; + updateCursorBlinking(); + + if (enable) + connect(qApp->styleHints(), &QStyleHints::cursorFlashTimeChanged, this, &QQuickTextInputPrivate::updateCursorBlinking); + else + disconnect(qApp->styleHints(), &QStyleHints::cursorFlashTimeChanged, this, &QQuickTextInputPrivate::updateCursorBlinking); +} + +void QQuickTextInputPrivate::updateCursorBlinking() +{ + Q_Q(QQuickTextInput); + if (m_blinkTimer) { q->killTimer(m_blinkTimer); - } - if (msec) { - m_blinkTimer = q->startTimer(msec / 2); - m_blinkStatus = 1; - } else { m_blinkTimer = 0; - if (m_blinkStatus == 1) { - updateType = UpdatePaintNode; - q->polish(); - q->update(); - } } - m_blinkPeriod = msec; + + if (m_blinkEnabled && cursorVisible && !cursorItem && !m_readOnly) { + int flashTime = QGuiApplication::styleHints()->cursorFlashTime(); + if (flashTime >= 2) + m_blinkTimer = q->startTimer(flashTime / 2); + } + + m_blinkStatus = 1; + updateType = UpdatePaintNode; + q->polish(); + q->update(); } void QQuickTextInput::timerEvent(QTimerEvent *event) @@ -4275,20 +4286,8 @@ void QQuickTextInputPrivate::processKeyEvent(QKeyEvent* event) return; } - if (m_blinkPeriod > 0) { - if (m_blinkTimer) - q->killTimer(m_blinkTimer); - - m_blinkTimer = q->startTimer(m_blinkPeriod / 2); - - if (m_blinkStatus == 0) { - m_blinkStatus = 1; - - updateType = UpdatePaintNode; - q->polish(); - q->update(); - } - } + if (m_blinkEnabled) + updateCursorBlinking(); if (m_echoMode == QQuickTextInput::PasswordEchoOnEdit && !m_passwordEchoEditing diff --git a/src/quick/items/qquicktextinput_p_p.h b/src/quick/items/qquicktextinput_p_p.h index 9d3c66f04a..57a9f6413a 100644 --- a/src/quick/items/qquicktextinput_p_p.h +++ b/src/quick/items/qquicktextinput_p_p.h @@ -107,7 +107,7 @@ public: #ifndef QT_NO_IM , m_preeditCursor(0) #endif - , m_blinkPeriod(0) + , m_blinkEnabled(false) , m_blinkTimer(0) , m_maxLength(32767) , m_lastCursorPos(-1) @@ -242,7 +242,7 @@ public: #ifndef QT_NO_IM int m_preeditCursor; #endif - int m_blinkPeriod; // 0 for non-blinking cursor + bool m_blinkEnabled; int m_blinkTimer; int m_maxLength; int m_lastCursorPos; @@ -307,8 +307,9 @@ public: return !tripleClickTimer.hasExpired(QGuiApplication::styleHints()->mouseDoubleClickInterval()); } - void setNativeCursorEnabled(bool enabled) { - setCursorBlinkPeriod(enabled && cursorVisible ? QGuiApplication::styleHints()->cursorFlashTime() : 0); } + void setNativeCursorEnabled(bool) { + updateCursorBlinking(); + } int nextMaskBlank(int pos) { @@ -443,7 +444,8 @@ public: #endif void processKeyEvent(QKeyEvent* ev); - void setCursorBlinkPeriod(int msec); + void setBlinkingCursorEnabled(bool enable); + void updateCursorBlinking(); void updateLayout(); void updateBaselineOffset(); |