#include "qquicktextinput_p.h"
#include "qquicktextinput_p_p.h"
-#include "qquickcanvas.h"
+#include "qquickwindow.h"
+#include "qquicktextutil_p.h"
#include <private/qqmlglobal_p.h>
+
+#include <QtCore/qcoreapplication.h>
#include <QtQml/qqmlinfo.h>
#include <QtGui/qevent.h>
#include <QTextBoundaryFinder>
DEFINE_BOOL_CONFIG_OPTION(qmlDisableDistanceField, QML_DISABLE_DISTANCEFIELD)
-#ifdef QT_GUI_PASSWORD_ECHO_DELAY
-static const int qt_passwordEchoDelay = QT_GUI_PASSWORD_ECHO_DELAY;
-#endif
-
/*!
\qmlclass TextInput QQuickTextInput
\inqmlmodule QtQuick 2
- \ingroup qml-basic-visual-elements
- \brief The TextInput item displays an editable line of text.
+ \ingroup qtquick-visual
+ \ingroup qtquick-input
\inherits Item
+ \brief Displays an editable line of text
- The TextInput element displays a single line of editable plain text.
+ The TextInput type displays a single line of editable plain text.
TextInput is used to accept a line of text input. Input constraints
can be placed on a TextInput item (for example, through a \l validator or \l inputMask),
d->checkIsValid();
d->updateLayout();
updateCursorRectangle();
- if (d->cursorComponent && d->cursorComponent->isReady())
- createCursor();
+ if (d->cursorComponent && isCursorVisible())
+ QQuickTextUtil::createCursor(d);
}
/*!
Q_D(const QQuickTextInput);
QString content = d->m_text;
- if (!d->m_tentativeCommit.isEmpty())
- content.insert(d->m_cursor, d->m_tentativeCommit);
QString res = d->m_maskData ? d->stripString(content) : content;
return (res.isNull() ? QString::fromLatin1("") : res);
}
Q_D(QQuickTextInput);
if (s == text())
return;
- if (d->composeMode())
- qApp->inputMethod()->reset();
- d->m_tentativeCommit.clear();
+
+ d->cancelPreedit();
d->internalSetText(s, -1, false);
}
+
+/*!
+ \qmlproperty enumeration QtQuick2::TextInput::renderType
+
+ Override the default rendering type for this component.
+
+ Supported render types are:
+ \list
+ \li Text.QtRendering - the default
+ \li Text.NativeRendering
+ \endlist
+
+ Select Text.NativeRendering if you prefer text to look native on the target platform and do
+ not require advanced features such as transformation of the text. Using such features in
+ combination with the NativeRendering render type will lend poor and sometimes pixelated
+ results.
+*/
+QQuickTextInput::RenderType QQuickTextInput::renderType() const
+{
+ Q_D(const QQuickTextInput);
+ return d->renderType;
+}
+
+void QQuickTextInput::setRenderType(QQuickTextInput::RenderType renderType)
+{
+ Q_D(QQuickTextInput);
+ if (d->renderType == renderType)
+ return;
+
+ d->renderType = renderType;
+ emit renderTypeChanged();
+
+ if (isComponentComplete())
+ d->updateLayout();
+}
+
/*!
\qmlproperty int QtQuick2::TextInput::length
return false;
}
+Qt::LayoutDirection QQuickTextInputPrivate::textDirection() const
+{
+ QString text = m_text;
+ if (text.isEmpty())
+ text = m_textLayout.preeditAreaText();
+
+ const QChar *character = text.constData();
+ while (!character->isNull()) {
+ switch (character->direction()) {
+ case QChar::DirL:
+ return Qt::LeftToRight;
+ case QChar::DirR:
+ case QChar::DirAL:
+ case QChar::DirAN:
+ return Qt::RightToLeft;
+ default:
+ break;
+ }
+ character++;
+ }
+ return Qt::LayoutDirectionAuto;
+}
+
+Qt::LayoutDirection QQuickTextInputPrivate::layoutDirection() const
+{
+ Qt::LayoutDirection direction = m_layoutDirection;
+ if (direction == Qt::LayoutDirectionAuto) {
+ direction = textDirection();
+ if (direction == Qt::LayoutDirectionAuto)
+ direction = qApp->inputMethod()->inputDirection();
+ }
+ return (direction == Qt::LayoutDirectionAuto) ? Qt::LeftToRight : direction;
+}
+
bool QQuickTextInputPrivate::determineHorizontalAlignment()
{
if (hAlignImplicit) {
// if no explicit alignment has been set, follow the natural layout direction of the text
- QString text = q_func()->text();
- if (text.isEmpty())
- text = m_textLayout.preeditAreaText();
- bool isRightToLeft = text.isEmpty() ? qApp->inputMethod()->inputDirection() == Qt::RightToLeft
- : text.isRightToLeft();
- return setHAlign(isRightToLeft ? QQuickTextInput::AlignRight : QQuickTextInput::AlignLeft);
+ Qt::LayoutDirection direction = textDirection();
+ if (direction == Qt::LayoutDirectionAuto)
+ direction = qApp->inputMethod()->inputDirection();
+ return setHAlign(direction == Qt::RightToLeft ? QQuickTextInput::AlignRight : QQuickTextInput::AlignLeft);
}
return false;
}
forward keys to it and you desire it to look active when this happens
(but without actually giving it active focus).
- It should not be set directly on the element, like in the below QML,
+ It should not be set directly on the item, like in the below QML,
as the specified value will be overridden an lost on focus changes.
\code
if (d->cursorVisible == on)
return;
d->cursorVisible = on;
- d->setCursorBlinkPeriod(on ? qApp->styleHints()->cursorFlashTime() : 0);
- d->updateType = QQuickTextInputPrivate::UpdatePaintNode;
- update();
+ if (on && isComponentComplete())
+ QQuickTextUtil::createCursor(d);
+ if (!d->cursorItem) {
+ d->setCursorBlinkPeriod(on ? qApp->styleHints()->cursorFlashTime() : 0);
+ d->updateType = QQuickTextInputPrivate::UpdatePaintNode;
+ update();
+ }
emit cursorVisibleChanged(d->cursorVisible);
}
{
Q_D(const QQuickTextInput);
- int c = d->m_cursor;
- if (d->m_preeditCursor != -1)
- c += d->m_preeditCursor;
+ int c = d->m_cursor + d->m_preeditCursor;
if (d->m_echoMode == NoEcho)
c = 0;
QTextLine l = d->m_textLayout.lineForTextPosition(c);
/*!
\qmlclass IntValidator QIntValidator
\inqmlmodule QtQuick 2
- \ingroup qml-basic-visual-elements
+ \ingroup qtquick-text-utility
+ \brief Defines a validator for integer values
- This element provides a validator for integer values.
+ The IntValidator type provides a validator for integer values.
If no \l locale is set IntValidator uses the \l {QLocale::setDefault()}{default locale} to
interpret the number and will accept locale specific digits, group separators, and positive
/*!
\qmlclass DoubleValidator QDoubleValidator
\inqmlmodule QtQuick 2
- \ingroup qml-basic-visual-elements
+ \ingroup qtquick-text-utility
+ \brief Defines a validator for non-integer numbers
- This element provides a validator for non-integer numbers.
+ The DoubleValidator type provides a validator for non-integer numbers.
Input is accepted if it contains a double that is within the valid range
and is in the correct format.
/*!
\qmlclass RegExpValidator QRegExpValidator
\inqmlmodule QtQuick 2
- \ingroup qml-basic-visual-elements
+ \ingroup qtquick-text-utility
+ \brief Provides a string validator
- This element provides a validator, which counts as valid any string which
+ The RegExpValidator type provides a validator, which counts as valid any string which
matches a specified regular expression.
*/
/*!
if (d->m_validator == v)
return;
+ if (d->m_validator) {
+ qmlobject_disconnect(
+ d->m_validator, QValidator, SIGNAL(changed()),
+ this, QQuickTextInput, SLOT(q_validatorChanged()));
+ }
+
d->m_validator = v;
+ if (d->m_validator) {
+ qmlobject_connect(
+ d->m_validator, QValidator, SIGNAL(changed()),
+ this, QQuickTextInput, SLOT(q_validatorChanged()));
+ }
+
if (isComponentComplete())
d->checkIsValid();
emit validatorChanged();
}
+void QQuickTextInput::q_validatorChanged()
+{
+ Q_D(QQuickTextInput);
+ d->checkIsValid();
+}
+
#endif // QT_NO_VALIDATOR
void QQuickTextInputPrivate::checkIsValid()
void QQuickTextInput::setCursorDelegate(QQmlComponent* c)
{
Q_D(QQuickTextInput);
- if (d->cursorComponent == c)
- return;
-
- d->cursorComponent = c;
- if (!c) {
- //note that the components are owned by something else
- delete d->cursorItem;
- d->cursorItem = 0;
- } else {
- d->startCreatingCursor();
- }
-
- emit cursorDelegateChanged();
-}
-
-void QQuickTextInputPrivate::startCreatingCursor()
-{
- Q_Q(QQuickTextInput);
- if (cursorComponent->isReady()) {
- q->createCursor();
- } else if (cursorComponent->isLoading()) {
- q->connect(cursorComponent, SIGNAL(statusChanged(int)),
- q, SLOT(createCursor()));
- } else { // isError
- qmlInfo(q, cursorComponent->errors()) << QQuickTextInput::tr("Could not load cursor delegate");
- }
+ QQuickTextUtil::setCursorDelegate(d, c);
}
void QQuickTextInput::createCursor()
{
Q_D(QQuickTextInput);
- if (!isComponentComplete())
- return;
-
- if (d->cursorComponent->isError()) {
- qmlInfo(this, d->cursorComponent->errors()) << tr("Could not load cursor delegate");
- return;
- }
-
- if (!d->cursorComponent->isReady())
- return;
-
- if (d->cursorItem)
- delete d->cursorItem;
- QQmlContext *creationContext = d->cursorComponent->creationContext();
- QObject *object = d->cursorComponent->create(creationContext ? creationContext : qmlContext(this));
- d->cursorItem = qobject_cast<QQuickItem*>(object);
- if (!d->cursorItem) {
- delete object;
- qmlInfo(this, d->cursorComponent->errors()) << tr("Could not instantiate cursor delegate");
- return;
- }
-
- QRectF r = cursorRectangle();
-
- QQml_setParent_noEvent(d->cursorItem, this);
- d->cursorItem->setParentItem(this);
- d->cursorItem->setPos(r.topLeft());
- d->cursorItem->setHeight(r.height());
+ d->cursorPending = true;
+ QQuickTextUtil::createCursor(d);
}
/*!
int cursorPosition = d->m_cursor;
if (cursorPosition == 0)
ignore = ev->key() == (d->layoutDirection() == Qt::LeftToRight ? Qt::Key_Left : Qt::Key_Right);
- if (cursorPosition == text().length())
+ if (!ignore && cursorPosition == text().length())
ignore = ev->key() == (d->layoutDirection() == Qt::LeftToRight ? Qt::Key_Right : Qt::Key_Left);
}
if (ignore) {
void QQuickTextInput::inputMethodEvent(QInputMethodEvent *ev)
{
Q_D(QQuickTextInput);
- const bool wasComposing = d->preeditAreaText().length() > 0;
+ const bool wasComposing = d->hasImState;
if (d->m_readOnly) {
ev->ignore();
} else {
if (!ev->isAccepted())
QQuickImplicitSizeItem::inputMethodEvent(ev);
- if (wasComposing != (d->m_textLayout.preeditAreaText().length() > 0))
+ if (wasComposing != d->hasImState)
emit inputMethodComposingChanged();
}
d->pressPos = event->localPos();
- if (d->focusOnPress) {
- bool hadActiveFocus = hasActiveFocus();
- forceActiveFocus();
- // re-open input panel on press if already focused
- if (hasActiveFocus() && hadActiveFocus && !d->m_readOnly)
- openSoftwareInputPanel();
- }
+ if (d->sendMouseEventToInputContext(event))
+ return;
+
if (d->selectByMouse) {
setKeepMouseGrab(false);
d->selectPressed = true;
}
}
- if (d->sendMouseEventToInputContext(event))
- return;
-
bool mark = (event->modifiers() & Qt::ShiftModifier) && d->selectByMouse;
int cursor = d->positionAt(event->localPos());
d->moveCursor(cursor, mark);
+
+ if (d->focusOnPress) {
+ bool hadActiveFocus = hasActiveFocus();
+ forceActiveFocus();
+ // re-open input panel on press if already focused
+ if (hasActiveFocus() && hadActiveFocus && !d->m_readOnly)
+ qGuiApp->inputMethod()->show();
+ }
+
event->setAccepted(true);
}
const QRectF &oldGeometry)
{
Q_D(QQuickTextInput);
- if (newGeometry.width() != oldGeometry.width())
- d->updateLayout();
- updateCursorRectangle();
+ if (!d->inLayout) {
+ if (newGeometry.width() != oldGeometry.width())
+ d->updateLayout();
+ updateCursorRectangle();
+ }
QQuickImplicitSizeItem::geometryChanged(newGeometry, oldGeometry);
}
QTextLine currentLine = m_textLayout.lineForTextPosition(m_cursor + m_preeditCursor);
const int preeditLength = m_textLayout.preeditAreaText().length();
const qreal width = qMax<qreal>(0, q->width());
- qreal widthUsed = currentLine.isValid() ? currentLine.naturalTextWidth() : 0;
+ qreal cix = 0;
+ qreal widthUsed = 0;
+ if (currentLine.isValid()) {
+ cix = currentLine.cursorToX(m_cursor + preeditLength);
+ const qreal cursorWidth = cix >= 0 ? cix : width - cix;
+ widthUsed = qMax(currentLine.naturalTextWidth(), cursorWidth);
+ }
int previousScroll = hscroll;
if (!autoScroll || widthUsed <= width || m_echoMode == QQuickTextInput::NoEcho) {
hscroll = 0;
} else {
Q_ASSERT(currentLine.isValid());
- qreal cix = currentLine.cursorToX(m_cursor + preeditLength);
if (cix - hscroll >= width) {
// text doesn't fit, cursor is to the right of br (scroll right)
hscroll = cix - width;
// text doesn't fit, text document is to the left of br; align
// right
hscroll = widthUsed - width;
+ } else if (width - hscroll > widthUsed) {
+ // text doesn't fit, text document is to the right of br; align
+ // left
+ hscroll = width - widthUsed;
}
if (preeditLength > 0) {
// check to ensure long pre-edit text doesn't push the cursor
Q_Q(QQuickTextInput);
const int preeditLength = m_textLayout.preeditAreaText().length();
const qreal height = qMax<qreal>(0, q->height());
- qreal heightUsed = boundingRect.height();
+ qreal heightUsed = contentSize.height();
qreal previousScroll = vscroll;
if (!autoScroll || heightUsed <= height) {
// text fits in br; use vscroll for alignment
- switch (vAlign & ~(Qt::AlignAbsolute|Qt::AlignHorizontal_Mask)) {
- case Qt::AlignBottom:
- vscroll = heightUsed - height;
- break;
- case Qt::AlignVCenter:
- vscroll = (heightUsed - height) / 2;
- break;
- default:
- // Top
- vscroll = 0;
- break;
- }
+ vscroll = -QQuickTextUtil::alignedY(
+ heightUsed, height, vAlign & ~(Qt::AlignAbsolute|Qt::AlignHorizontal_Mask));
} else {
QTextLine currentLine = m_textLayout.lineForTextPosition(m_cursor + preeditLength);
QRectF r = currentLine.isValid() ? currentLine.rect() : QRectF();
node = new QQuickTextNode(QQuickItemPrivate::get(this)->sceneGraphContext(), this);
d->textNode = node;
- if (!d->textLayoutDirty) {
+ if (!d->textLayoutDirty && oldNode != 0) {
QSGSimpleRectNode *cursorNode = node->cursorNode();
if (cursorNode != 0 && !isReadOnly()) {
cursorNode->setRect(cursorRectangle());
- if (!d->cursorVisible || (!d->m_blinkStatus && d->m_blinkPeriod > 0)) {
+ if (!d->cursorVisible || d->cursorItem || (!d->m_blinkStatus && d->m_blinkPeriod > 0)) {
d->hideCursor();
} else {
d->showCursor();
}
}
} else {
+ node->setUseNativeRenderer(d->renderType == QQuickTextInput::NativeRendering);
node->deleteContent();
node->setMatrix(QMatrix4x4());
void QQuickTextInput::insert(int position, const QString &text)
{
Q_D(QQuickTextInput);
-#ifdef QT_GUI_PASSWORD_ECHO_DELAY
- if (d->m_echoMode == QQuickTextInput::Password)
- d->m_passwordEchoTimer.start(qt_passwordEchoDelay, this);
-#endif
-
+ if (d->m_echoMode == QQuickTextInput::Password) {
+ int delay = qGuiApp->styleHints()->passwordMaskDelay();
+ if (delay > 0)
+ d->m_passwordEchoTimer.start(delay, this);
+ }
if (position < 0 || position > d->m_text.length())
return;
}
/*!
- \qmlproperty enum QtQuick2::TextInput::mouseSelectionMode
+ \qmlproperty enumeration QtQuick2::TextInput::mouseSelectionMode
Specifies how text should be selected using a mouse.
emit persistentSelectionChanged();
}
+#ifndef QT_NO_CLIPBOARD
/*!
\qmlproperty bool QtQuick2::TextInput::canPaste
}
return d->canPaste;
}
+#endif
/*!
\qmlproperty bool QtQuick2::TextInput::canUndo
qreal QQuickTextInput::contentWidth() const
{
Q_D(const QQuickTextInput);
- return d->boundingRect.width();
+ return d->contentSize.width();
}
/*!
qreal QQuickTextInput::contentHeight() const
{
Q_D(const QQuickTextInput);
- return d->boundingRect.height();
+ return d->contentSize.height();
}
void QQuickTextInput::moveCursorSelection(int position)
}
}
-/*!
- \qmlmethod void QtQuick2::TextInput::openSoftwareInputPanel()
-
- Opens software input panels like virtual keyboards for typing, useful for
- customizing when you want the input keyboard to be shown and hidden in
- your application.
-
- By default the opening of input panels follows the platform style. Input panels are
- always closed if no editor has active focus.
-
- You can disable the automatic behavior by setting the property \c activeFocusOnPress to false
- and use functions openSoftwareInputPanel() and closeSoftwareInputPanel() to implement
- the behavior you want.
-
- Only relevant on platforms, which provide virtual keyboards.
-
- \qml
- import QtQuick 2.0
- TextInput {
- id: textInput
- text: "Hello world!"
- activeFocusOnPress: false
- MouseArea {
- anchors.fill: parent
- onClicked: {
- if (!textInput.activeFocus) {
- textInput.forceActiveFocus()
- textInput.openSoftwareInputPanel();
- } else {
- textInput.focus = false;
- }
- }
- onPressAndHold: textInput.closeSoftwareInputPanel();
- }
- }
- \endqml
-*/
-void QQuickTextInput::openSoftwareInputPanel()
-{
- if (qGuiApp)
- qGuiApp->inputMethod()->show();
-}
-
-/*!
- \qmlmethod void QtQuick2::TextInput::closeSoftwareInputPanel()
-
- Closes a software input panel like a virtual keyboard shown on the screen, useful
- for customizing when you want the input keyboard to be shown and hidden in
- your application.
-
- By default the opening of input panels follows the platform style. Input panels are
- always closed if no editor has active focus.
-
- You can disable the automatic behavior by setting the property \c activeFocusOnPress to false
- and use functions openSoftwareInputPanel() and closeSoftwareInputPanel() to implement
- the behavior you want.
-
- Only relevant on platforms, which provide virtual keyboards.
-
- \qml
- import QtQuick 2.0
- TextInput {
- id: textInput
- text: "Hello world!"
- activeFocusOnPress: false
- MouseArea {
- anchors.fill: parent
- onClicked: {
- if (!textInput.activeFocus) {
- textInput.forceActiveFocus();
- textInput.openSoftwareInputPanel();
- } else {
- textInput.focus = false;
- }
- }
- onPressAndHold: textInput.closeSoftwareInputPanel();
- }
- }
- \endqml
-*/
-void QQuickTextInput::closeSoftwareInputPanel()
-{
- if (qGuiApp)
- qGuiApp->inputMethod()->hide();
-}
-
void QQuickTextInput::focusInEvent(QFocusEvent *event)
{
Q_D(const QQuickTextInput);
if (d->focusOnPress && !d->m_readOnly)
- openSoftwareInputPanel();
+ qGuiApp->inputMethod()->show();
QQuickImplicitSizeItem::focusInEvent(event);
}
Q_D(QQuickTextInput);
if (change == ItemActiveFocusHasChanged) {
bool hasFocus = value.boolValue;
- setCursorVisible(hasFocus); // ### refactor: && d->canvas && d->canvas->hasFocus()
-#ifdef QT_GUI_PASSWORD_ECHO_DELAY
+ setCursorVisible(hasFocus);
if (!hasFocus && (d->m_passwordEchoEditing || d->m_passwordEchoTimer.isActive())) {
-#else
- if (!hasFocus && d->m_passwordEchoEditing) {
-#endif
d->updatePasswordEchoEditing(false);//QQuickTextInputPrivate sets it on key events, but doesn't deal with focus events
}
if (!hasFocus) {
- d->commitPreedit();
if (!d->persistentSelection)
d->deselect();
disconnect(qApp->inputMethod(), SIGNAL(inputDirectionChanged(Qt::LayoutDirection)),
bool QQuickTextInput::isInputMethodComposing() const
{
Q_D(const QQuickTextInput);
- return d->preeditAreaText().length() > 0;
+ return d->hasImState;
}
void QQuickTextInputPrivate::init()
if (!qmlDisableDistanceField()) {
QTextOption option = m_textLayout.textOption();
- option.setUseDesignMetrics(true);
+ option.setUseDesignMetrics(renderType != QQuickTextInput::NativeRendering);
m_textLayout.setTextOption(option);
}
}
{
Q_D(const QQuickTextInput);
+ int cursorWidth = d->cursorItem ? 0 : 1;
+
+ qreal hscroll = d->hscroll;
+ if (!d->autoScroll || d->contentSize.width() < width())
+ hscroll -= QQuickTextUtil::alignedX(d->contentSize.width(), width(), effectiveHAlign());
+
+ // Could include font max left/right bearings to either side of rectangle.
+ QRectF r(-hscroll, -d->vscroll, d->contentSize.width(), d->contentSize.height());
+ r.setRight(r.right() + cursorWidth);
+ return r;
+}
+
+QRectF QQuickTextInput::clipRect() const
+{
+ Q_D(const QQuickTextInput);
+
int cursorWidth = d->cursorItem ? d->cursorItem->width() : 1;
// Could include font max left/right bearings to either side of rectangle.
- QRectF r = QQuickImplicitSizeItem::boundingRect();
+ QRectF r = QQuickImplicitSizeItem::clipRect();
r.setRight(r.right() + cursorWidth);
return r;
}
if (m_echoMode == QQuickTextInput::Password) {
str.fill(m_passwordCharacter);
-#ifdef QT_GUI_PASSWORD_ECHO_DELAY
if (m_passwordEchoTimer.isActive() && m_cursor > 0 && m_cursor <= m_text.length()) {
int cursor = m_cursor - 1;
QChar uc = m_text.at(cursor);
str[cursor - 1] = uc;
}
}
-#endif
} else if (m_echoMode == QQuickTextInput::PasswordEchoOnEdit && !m_passwordEchoEditing) {
str.fill(m_passwordCharacter);
}
}
}
+qreal QQuickTextInputPrivate::getImplicitWidth() const
+{
+ Q_Q(const QQuickTextInput);
+ if (!requireImplicitWidth) {
+ QQuickTextInputPrivate *d = const_cast<QQuickTextInputPrivate *>(this);
+ d->requireImplicitWidth = true;
+
+ if (q->isComponentComplete()) {
+ // One time cost, only incurred if implicitWidth is first requested after
+ // componentComplete.
+ QTextLayout layout(m_text);
+
+ QTextOption option = m_textLayout.textOption();
+ option.setTextDirection(m_layoutDirection);
+ option.setFlags(QTextOption::IncludeTrailingSpaces);
+ option.setWrapMode(QTextOption::WrapMode(wrapMode));
+ option.setAlignment(Qt::Alignment(q->effectiveHAlign()));
+ layout.setTextOption(option);
+ layout.setFont(font);
+ layout.setPreeditArea(m_textLayout.preeditAreaPosition(), m_textLayout.preeditAreaText());
+ layout.beginLayout();
+
+ QTextLine line = layout.createLine();
+ line.setLineWidth(INT_MAX);
+ d->implicitWidth = qCeil(line.naturalTextWidth());
+
+ layout.endLayout();
+ }
+ }
+ return implicitWidth;
+}
+
void QQuickTextInputPrivate::updateLayout()
{
Q_Q(QQuickTextInput);
if (!q->isComponentComplete())
return;
- const QRectF previousRect = boundingRect;
QTextOption option = m_textLayout.textOption();
option.setTextDirection(layoutDirection());
- option.setFlags(QTextOption::IncludeTrailingSpaces);
option.setWrapMode(QTextOption::WrapMode(wrapMode));
option.setAlignment(Qt::Alignment(q->effectiveHAlign()));
m_textLayout.setTextOption(option);
m_textLayout.setFont(font);
- boundingRect = QRectF();
m_textLayout.beginLayout();
+
QTextLine line = m_textLayout.createLine();
+ if (requireImplicitWidth) {
+ line.setLineWidth(INT_MAX);
+ const bool wasInLayout = inLayout;
+ inLayout = true;
+ q->setImplicitWidth(qCeil(line.naturalTextWidth()));
+ inLayout = wasInLayout;
+ if (inLayout) // probably the result of a binding loop, but by letting it
+ return; // get this far we'll get a warning to that effect.
+ }
qreal lineWidth = q->widthValid() ? q->width() : INT_MAX;
qreal height = 0;
- QTextLine firstLine = line;
+ qreal width = 0;
do {
line.setLineWidth(lineWidth);
- line.setPosition(QPointF(line.position().x(), height));
- boundingRect = boundingRect.united(line.naturalTextRect());
+ line.setPosition(QPointF(0, height));
height += line.height();
+ width = qMax(width, line.naturalTextWidth());
+
line = m_textLayout.createLine();
} while (line.isValid());
m_textLayout.endLayout();
textLayoutDirty = true;
+ const QSizeF previousSize = contentSize;
+ contentSize = QSizeF(width, height);
+
updateType = UpdatePaintNode;
q->update();
- q->setImplicitSize(boundingRect.width(), boundingRect.height());
- if (previousRect != boundingRect)
+ if (!requireImplicitWidth && !q->widthValid())
+ q->setImplicitSize(width, height);
+ else
+ q->setImplicitHeight(height);
+
+ if (previousSize != contentSize)
emit q->contentSizeChanged();
}
/*!
\internal
-
- Exits preedit mode and commits parts marked as tentative commit
*/
void QQuickTextInputPrivate::commitPreedit()
{
- if (!composeMode())
+ Q_Q(QQuickTextInput);
+
+ if (!hasImState)
return;
- qApp->inputMethod()->reset();
+ qApp->inputMethod()->commit();
- if (!m_tentativeCommit.isEmpty()) {
- internalInsert(m_tentativeCommit);
- m_tentativeCommit.clear();
- finishChange(-1, true/*not used, not documented*/, false);
- }
+ if (!hasImState)
+ return;
- m_preeditCursor = 0;
- m_textLayout.setPreeditArea(-1, QString());
- m_textLayout.clearAdditionalFormats();
- updateLayout();
+ QInputMethodEvent ev;
+ QCoreApplication::sendEvent(q, &ev);
+}
+
+void QQuickTextInputPrivate::cancelPreedit()
+{
+ Q_Q(QQuickTextInput);
+
+ if (!hasImState)
+ return;
+
+ qApp->inputMethod()->reset();
+
+ QInputMethodEvent ev;
+ QCoreApplication::sendEvent(q, &ev);
}
/*!
#endif //QT_NO_IM
const int oldPreeditCursor = m_preeditCursor;
m_preeditCursor = event->preeditString().length();
- m_hideCursor = false;
+ hasImState = !event->preeditString().isEmpty();
+ bool cursorVisible = true;
QList<QTextLayout::FormatRange> formats;
for (int i = 0; i < event->attributes().size(); ++i) {
const QInputMethodEvent::Attribute &a = event->attributes().at(i);
if (a.type == QInputMethodEvent::Cursor) {
+ hasImState = true;
m_preeditCursor = a.start;
- m_hideCursor = !a.length;
+ cursorVisible = a.length != 0;
} else if (a.type == QInputMethodEvent::TextFormat) {
+ hasImState = true;
QTextCharFormat f = qvariant_cast<QTextFormat>(a.value).toCharFormat();
if (f.isValid()) {
QTextLayout::FormatRange o;
m_textLayout.setAdditionalFormats(formats);
updateDisplayText(/*force*/ true);
- if (cursorPositionChanged) {
- emitCursorPositionChanged();
- } else if (m_preeditCursor != oldPreeditCursor) {
+ if ((cursorPositionChanged && !emitCursorPositionChanged())
+ || m_preeditCursor != oldPreeditCursor
+ || isGettingInput) {
q->updateCursorRectangle();
}
- bool tentativeCommitChanged = m_tentativeCommit != event->tentativeCommitString();
-
- if (tentativeCommitChanged) {
- m_textDirty = true;
- m_tentativeCommit = event->tentativeCommitString();
- }
-
- if (isGettingInput || tentativeCommitChanged)
+ if (isGettingInput)
finishChange(priorState);
+ q->setCursorVisible(cursorVisible);
+
if (selectionChange) {
emit q->selectionChanged();
q->updateInputMethod(Qt::ImCursorRectangle | Qt::ImAnchorPosition
return true;
}
m_cursor = cursorCopy;
-
- if (!m_tentativeCommit.isEmpty()) {
- textCopy.insert(m_cursor, m_tentativeCommit);
- bool validInput = m_validator->validate(textCopy, cursorCopy) != QValidator::Invalid;
- if (!validInput)
- m_tentativeCommit.clear();
- }
- } else {
- m_tentativeCommit.clear();
}
}
#endif
emit q->selectionChanged();
}
- inputMethodAttributesChanged |= (m_cursor == m_lastCursorPos);
+ inputMethodAttributesChanged |= (m_cursor != m_lastCursorPos);
if (inputMethodAttributesChanged)
q->updateInputMethod();
emitUndoRedoChanged();
m_textDirty = (oldText != m_text);
bool changed = finishChange(-1, true, edited);
+#ifdef QT_NO_ACCESSIBILITY
Q_UNUSED(changed)
+#else
+ if (changed) {
+ QAccessibleTextUpdateEvent ev(q, 0, oldText, m_text);
+ QAccessible::updateAccessibility(&ev);
+ }
+#endif
}
*/
void QQuickTextInputPrivate::internalInsert(const QString &s)
{
-#ifdef QT_GUI_PASSWORD_ECHO_DELAY
Q_Q(QQuickTextInput);
- if (m_echoMode == QQuickTextInput::Password)
- m_passwordEchoTimer.start(qt_passwordEchoDelay, q);
-#endif
+ if (m_echoMode == QQuickTextInput::Password) {
+ int delay = qGuiApp->styleHints()->passwordMaskDelay();
+ if (delay > 0)
+ m_passwordEchoTimer.start(delay, q);
+ }
if (hasSelectedText())
addCommand(Command(SetSelection, m_cursor, 0, m_selstart, m_selend));
if (m_maskData) {
}
}
+#ifndef QT_NO_ACCESSIBILITY
+ QAccessibleTextCursorEvent ev(q, m_cursor);
+ QAccessible::updateAccessibility(&ev);
+#endif
+
return true;
}
return false;
d->m_blinkStatus = !d->m_blinkStatus;
d->updateType = QQuickTextInputPrivate::UpdatePaintNode;
update();
-#ifdef QT_GUI_PASSWORD_ECHO_DELAY
} else if (event->timerId() == d->m_passwordEchoTimer.timerId()) {
d->m_passwordEchoTimer.stop();
d->updateDisplayText();
-#endif
+ updateCursorRectangle();
}
}
}
#ifndef QT_NO_SHORTCUT
else if (event == QKeySequence::Undo) {
- if (!m_readOnly)
- q->undo();
+ q->undo();
}
else if (event == QKeySequence::Redo) {
- if (!m_readOnly)
- q->redo();
+ q->redo();
}
else if (event == QKeySequence::SelectAll) {
selectAll();