-
Lars Knoll authored
... and rename it to QQmlV4Function Change-Id: Iad72347babf62691e26306877d4f229fda127eb7 Reviewed-by:
Simon Hausmann <simon.hausmann@digia.com>
995d65f3
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtQml module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qquicktextinput_p.h"
#include "qquicktextinput_p_p.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>
#include "qquicktextnode_p.h"
#include <QtQuick/qsgsimplerectnode.h>
#include <QtGui/qstylehints.h>
#include <QtGui/qinputmethod.h>
#include <QtCore/qmath.h>
#ifndef QT_NO_ACCESSIBILITY
#include "qaccessible.h"
#include "qquickaccessibleattached_p.h"
#endif
QT_BEGIN_NAMESPACE
DEFINE_BOOL_CONFIG_OPTION(qmlDisableDistanceField, QML_DISABLE_DISTANCEFIELD)
/*!
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
\qmltype TextInput
\instantiates QQuickTextInput
\inqmlmodule QtQuick 2
\ingroup qtquick-visual
\ingroup qtquick-input
\inherits Item
\brief Displays an editable line of 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),
and setting \l echoMode to an appropriate value enables TextInput to be used for
a password input field.
On Mac OS X, the Up/Down key bindings for Home/End are explicitly disabled.
If you want such bindings (on any platform), you will need to construct them in QML.
\sa TextEdit, Text, {declarative/text/textselection}{Text Selection example}
*/
QQuickTextInput::QQuickTextInput(QQuickItem* parent)
: QQuickImplicitSizeItem(*(new QQuickTextInputPrivate), parent)
{
Q_D(QQuickTextInput);
d->init();
}
QQuickTextInput::~QQuickTextInput()
{
}
void QQuickTextInput::componentComplete()
{
Q_D(QQuickTextInput);
QQuickImplicitSizeItem::componentComplete();
d->checkIsValid();
d->updateLayout();
updateCursorRectangle();
if (d->cursorComponent && isCursorVisible())
QQuickTextUtil::createCursor(d);
}
/*!
\qmlproperty string QtQuick2::TextInput::text
The text in the TextInput.
*/
QString QQuickTextInput::text() const
{
Q_D(const QQuickTextInput);
QString content = d->m_text;
QString res = d->m_maskData ? d->stripString(content) : content;
return (res.isNull() ? QString::fromLatin1("") : res);
}
void QQuickTextInput::setText(const QString &s)
{
Q_D(QQuickTextInput);
if (s == text())
return;
#ifndef QT_NO_IM
d->cancelPreedit();
#endif
d->internalSetText(s, -1, false);
}
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
/*!
\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.
On HighDpi "retina" displays this property is ignored and QtRendering is always used.
*/
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
Returns the total number of characters in the TextInput item.
If the TextInput has an inputMask the length will include mask characters and may differ
from the length of the string returned by the \l text property.
This property can be faster than querying the length the \l text property as it doesn't
require any copying or conversion of the TextInput's internal string data.
*/
int QQuickTextInput::length() const
{
Q_D(const QQuickTextInput);
return d->m_text.length();
}
/*!
\qmlmethod string QtQuick2::TextInput::getText(int start, int end)
Returns the section of text that is between the \a start and \a end positions.
If the TextInput has an inputMask the length will include mask characters.
*/
QString QQuickTextInput::getText(int start, int end) const
{
Q_D(const QQuickTextInput);
if (start > end)
qSwap(start, end);
211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
return d->m_text.mid(start, end - start);
}
QString QQuickTextInputPrivate::realText() const
{
QString res = m_maskData ? stripString(m_text) : m_text;
return (res.isNull() ? QString::fromLatin1("") : res);
}
/*!
\qmlproperty string QtQuick2::TextInput::font.family
Sets the family name of the font.
The family name is case insensitive and may optionally include a foundry name, e.g. "Helvetica [Cronyx]".
If the family is available from more than one foundry and the foundry isn't specified, an arbitrary foundry is chosen.
If the family isn't available a family will be set using the font matching algorithm.
*/
/*!
\qmlproperty bool QtQuick2::TextInput::font.bold
Sets whether the font weight is bold.
*/
/*!
\qmlproperty enumeration QtQuick2::TextInput::font.weight
Sets the font's weight.
The weight can be one of:
\list
\li Font.Light
\li Font.Normal - the default
\li Font.DemiBold
\li Font.Bold
\li Font.Black
\endlist
\qml
TextInput { text: "Hello"; font.weight: Font.DemiBold }
\endqml
*/
/*!
\qmlproperty bool QtQuick2::TextInput::font.italic
Sets whether the font has an italic style.
*/
/*!
\qmlproperty bool QtQuick2::TextInput::font.underline
Sets whether the text is underlined.
*/
/*!
\qmlproperty bool QtQuick2::TextInput::font.strikeout
Sets whether the font has a strikeout style.
*/
/*!
\qmlproperty real QtQuick2::TextInput::font.pointSize
Sets the font size in points. The point size must be greater than zero.
*/
/*!
281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
\qmlproperty int QtQuick2::TextInput::font.pixelSize
Sets the font size in pixels.
Using this function makes the font device dependent.
Use \c pointSize to set the size of the font in a device independent manner.
*/
/*!
\qmlproperty real QtQuick2::TextInput::font.letterSpacing
Sets the letter spacing for the font.
Letter spacing changes the default spacing between individual letters in the font.
A positive value increases the letter spacing by the corresponding pixels; a negative value decreases the spacing.
*/
/*!
\qmlproperty real QtQuick2::TextInput::font.wordSpacing
Sets the word spacing for the font.
Word spacing changes the default spacing between individual words.
A positive value increases the word spacing by a corresponding amount of pixels,
while a negative value decreases the inter-word spacing accordingly.
*/
/*!
\qmlproperty enumeration QtQuick2::TextInput::font.capitalization
Sets the capitalization for the text.
\list
\li Font.MixedCase - This is the normal text rendering option where no capitalization change is applied.
\li Font.AllUppercase - This alters the text to be rendered in all uppercase type.
\li Font.AllLowercase - This alters the text to be rendered in all lowercase type.
\li Font.SmallCaps - This alters the text to be rendered in small-caps type.
\li Font.Capitalize - This alters the text to be rendered with the first character of each word as an uppercase character.
\endlist
\qml
TextInput { text: "Hello"; font.capitalization: Font.AllLowercase }
\endqml
*/
QFont QQuickTextInput::font() const
{
Q_D(const QQuickTextInput);
return d->sourceFont;
}
void QQuickTextInput::setFont(const QFont &font)
{
Q_D(QQuickTextInput);
if (d->sourceFont == font)
return;
d->sourceFont = font;
QFont oldFont = d->font;
d->font = font;
if (d->font.pointSizeF() != -1) {
// 0.5pt resolution
qreal size = qRound(d->font.pointSizeF()*2.0);
d->font.setPointSizeF(size/2.0);
}
if (oldFont != d->font) {
d->updateLayout();
updateCursorRectangle();
#ifndef QT_NO_IM
updateInputMethod(Qt::ImCursorRectangle | Qt::ImFont);
351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
#endif
}
emit fontChanged(d->sourceFont);
}
/*!
\qmlproperty color QtQuick2::TextInput::color
The text color.
*/
QColor QQuickTextInput::color() const
{
Q_D(const QQuickTextInput);
return d->color;
}
void QQuickTextInput::setColor(const QColor &c)
{
Q_D(QQuickTextInput);
if (c != d->color) {
d->color = c;
d->textLayoutDirty = true;
d->updateType = QQuickTextInputPrivate::UpdatePaintNode;
update();
emit colorChanged();
}
}
/*!
\qmlproperty color QtQuick2::TextInput::selectionColor
The text highlight color, used behind selections.
*/
QColor QQuickTextInput::selectionColor() const
{
Q_D(const QQuickTextInput);
return d->selectionColor;
}
void QQuickTextInput::setSelectionColor(const QColor &color)
{
Q_D(QQuickTextInput);
if (d->selectionColor == color)
return;
d->selectionColor = color;
if (d->hasSelectedText()) {
d->textLayoutDirty = true;
d->updateType = QQuickTextInputPrivate::UpdatePaintNode;
update();
}
emit selectionColorChanged();
}
/*!
\qmlproperty color QtQuick2::TextInput::selectedTextColor
The highlighted text color, used in selections.
*/
QColor QQuickTextInput::selectedTextColor() const
{
Q_D(const QQuickTextInput);
return d->selectedTextColor;
}
void QQuickTextInput::setSelectedTextColor(const QColor &color)
{
Q_D(QQuickTextInput);
if (d->selectedTextColor == color)
return;
421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
d->selectedTextColor = color;
if (d->hasSelectedText()) {
d->textLayoutDirty = true;
d->updateType = QQuickTextInputPrivate::UpdatePaintNode;
update();
}
emit selectedTextColorChanged();
}
/*!
\qmlproperty enumeration QtQuick2::TextInput::horizontalAlignment
\qmlproperty enumeration QtQuick2::TextInput::effectiveHorizontalAlignment
\qmlproperty enumeration QtQuick2::TextInput::verticalAlignment
Sets the horizontal alignment of the text within the TextInput item's
width and height. By default, the text alignment follows the natural alignment
of the text, for example text that is read from left to right will be aligned to
the left.
TextInput does not have vertical alignment, as the natural height is
exactly the height of the single line of text. If you set the height
manually to something larger, TextInput will always be top aligned
vertically. You can use anchors to align it however you want within
another item.
The valid values for \c horizontalAlignment are \c TextInput.AlignLeft, \c TextInput.AlignRight and
\c TextInput.AlignHCenter.
Valid values for \c verticalAlignment are \c TextInput.AlignTop (default),
\c TextInput.AlignBottom \c TextInput.AlignVCenter.
When using the attached property LayoutMirroring::enabled to mirror application
layouts, the horizontal alignment of text will also be mirrored. However, the property
\c horizontalAlignment will remain unchanged. To query the effective horizontal alignment
of TextInput, use the read-only property \c effectiveHorizontalAlignment.
*/
QQuickTextInput::HAlignment QQuickTextInput::hAlign() const
{
Q_D(const QQuickTextInput);
return d->hAlign;
}
void QQuickTextInput::setHAlign(HAlignment align)
{
Q_D(QQuickTextInput);
bool forceAlign = d->hAlignImplicit && d->effectiveLayoutMirror;
d->hAlignImplicit = false;
if (d->setHAlign(align, forceAlign) && isComponentComplete()) {
d->updateLayout();
updateCursorRectangle();
}
}
void QQuickTextInput::resetHAlign()
{
Q_D(QQuickTextInput);
d->hAlignImplicit = true;
if (d->determineHorizontalAlignment() && isComponentComplete()) {
d->updateLayout();
updateCursorRectangle();
}
}
QQuickTextInput::HAlignment QQuickTextInput::effectiveHAlign() const
{
Q_D(const QQuickTextInput);
QQuickTextInput::HAlignment effectiveAlignment = d->hAlign;
if (!d->hAlignImplicit && d->effectiveLayoutMirror) {
switch (d->hAlign) {
491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
case QQuickTextInput::AlignLeft:
effectiveAlignment = QQuickTextInput::AlignRight;
break;
case QQuickTextInput::AlignRight:
effectiveAlignment = QQuickTextInput::AlignLeft;
break;
default:
break;
}
}
return effectiveAlignment;
}
bool QQuickTextInputPrivate::setHAlign(QQuickTextInput::HAlignment alignment, bool forceAlign)
{
Q_Q(QQuickTextInput);
if ((hAlign != alignment || forceAlign) && alignment <= QQuickTextInput::AlignHCenter) { // justify not supported
QQuickTextInput::HAlignment oldEffectiveHAlign = q->effectiveHAlign();
hAlign = alignment;
emit q->horizontalAlignmentChanged(alignment);
if (oldEffectiveHAlign != q->effectiveHAlign())
emit q->effectiveHorizontalAlignmentChanged();
return true;
}
return false;
}
Qt::LayoutDirection QQuickTextInputPrivate::textDirection() const
{
QString text = m_text;
#ifndef QT_NO_IM
if (text.isEmpty())
text = m_textLayout.preeditAreaText();
#endif
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();
#ifndef QT_NO_IM
if (direction == Qt::LayoutDirectionAuto)
direction = qApp->inputMethod()->inputDirection();
#endif
}
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
Qt::LayoutDirection direction = textDirection();
561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
#ifndef QT_NO_IM
if (direction == Qt::LayoutDirectionAuto)
direction = qApp->inputMethod()->inputDirection();
#endif
return setHAlign(direction == Qt::RightToLeft ? QQuickTextInput::AlignRight : QQuickTextInput::AlignLeft);
}
return false;
}
QQuickTextInput::VAlignment QQuickTextInput::vAlign() const
{
Q_D(const QQuickTextInput);
return d->vAlign;
}
void QQuickTextInput::setVAlign(QQuickTextInput::VAlignment alignment)
{
Q_D(QQuickTextInput);
if (alignment == d->vAlign)
return;
d->vAlign = alignment;
emit verticalAlignmentChanged(d->vAlign);
if (isComponentComplete()) {
updateCursorRectangle();
}
}
/*!
\qmlproperty enumeration QtQuick2::TextInput::wrapMode
Set this property to wrap the text to the TextInput item's width.
The text will only wrap if an explicit width has been set.
\list
\li TextInput.NoWrap - no wrapping will be performed. If the text contains insufficient newlines, then implicitWidth will exceed a set width.
\li TextInput.WordWrap - wrapping is done on word boundaries only. If a word is too long, implicitWidth will exceed a set width.
\li TextInput.WrapAnywhere - wrapping is done at any point on a line, even if it occurs in the middle of a word.
\li TextInput.Wrap - if possible, wrapping occurs at a word boundary; otherwise it will occur at the appropriate point on the line, even in the middle of a word.
\endlist
The default is TextInput.NoWrap. If you set a width, consider using TextInput.Wrap.
*/
QQuickTextInput::WrapMode QQuickTextInput::wrapMode() const
{
Q_D(const QQuickTextInput);
return d->wrapMode;
}
void QQuickTextInput::setWrapMode(WrapMode mode)
{
Q_D(QQuickTextInput);
if (mode == d->wrapMode)
return;
d->wrapMode = mode;
d->updateLayout();
updateCursorRectangle();
emit wrapModeChanged();
}
void QQuickTextInputPrivate::mirrorChange()
{
Q_Q(QQuickTextInput);
if (q->isComponentComplete()) {
if (!hAlignImplicit && (hAlign == QQuickTextInput::AlignRight || hAlign == QQuickTextInput::AlignLeft)) {
q->updateCursorRectangle();
emit q->effectiveHorizontalAlignmentChanged();
}
}
}
631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
/*!
\qmlproperty bool QtQuick2::TextInput::readOnly
Sets whether user input can modify the contents of the TextInput.
If readOnly is set to true, then user input will not affect the text
property. Any bindings or attempts to set the text property will still
work.
*/
bool QQuickTextInput::isReadOnly() const
{
Q_D(const QQuickTextInput);
return d->m_readOnly;
}
void QQuickTextInput::setReadOnly(bool ro)
{
Q_D(QQuickTextInput);
if (d->m_readOnly == ro)
return;
#ifndef QT_NO_IM
setFlag(QQuickItem::ItemAcceptsInputMethod, !ro);
#endif
d->m_readOnly = ro;
if (!ro)
d->setCursorPosition(d->end());
#ifndef QT_NO_IM
updateInputMethod(Qt::ImEnabled);
#endif
q_canPasteChanged();
d->emitUndoRedoChanged();
emit readOnlyChanged(ro);
}
/*!
\qmlproperty int QtQuick2::TextInput::maximumLength
The maximum permitted length of the text in the TextInput.
If the text is too long, it is truncated at the limit.
By default, this property contains a value of 32767.
*/
int QQuickTextInput::maxLength() const
{
Q_D(const QQuickTextInput);
return d->m_maxLength;
}
void QQuickTextInput::setMaxLength(int ml)
{
Q_D(QQuickTextInput);
if (d->m_maxLength == ml || d->m_maskData)
return;
d->m_maxLength = ml;
d->internalSetText(d->m_text, -1, false);
emit maximumLengthChanged(ml);
}
/*!
\qmlproperty bool QtQuick2::TextInput::cursorVisible
Set to true when the TextInput shows a cursor.
This property is set and unset when the TextInput gets active focus, so that other
properties can be bound to whether the cursor is currently showing. As it
gets set and unset automatically, when you set the value yourself you must
keep in mind that your value may be overwritten.
701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
It can be set directly in script, for example if a KeyProxy might
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 item, like in the below QML,
as the specified value will be overridden an lost on focus changes.
\code
TextInput {
text: "Text"
cursorVisible: false
}
\endcode
In the above snippet the cursor will still become visible when the
TextInput gains active focus.
*/
bool QQuickTextInput::isCursorVisible() const
{
Q_D(const QQuickTextInput);
return d->cursorVisible;
}
void QQuickTextInput::setCursorVisible(bool on)
{
Q_D(QQuickTextInput);
if (d->cursorVisible == on)
return;
d->cursorVisible = on;
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);
}
/*!
\qmlproperty int QtQuick2::TextInput::cursorPosition
The position of the cursor in the TextInput.
*/
int QQuickTextInput::cursorPosition() const
{
Q_D(const QQuickTextInput);
return d->m_cursor;
}
void QQuickTextInput::setCursorPosition(int cp)
{
Q_D(QQuickTextInput);
if (cp < 0 || cp > text().length())
return;
d->moveCursor(cp);
}
/*!
\qmlproperty rectangle QtQuick2::TextInput::cursorRectangle
The rectangle where the standard text cursor is rendered within the text input. Read only.
The position and height of a custom cursorDelegate are updated to follow the cursorRectangle
automatically when it changes. The width of the delegate is unaffected by changes in the
cursor rectangle.
*/
QRectF QQuickTextInput::cursorRectangle() const
{
Q_D(const QQuickTextInput);
771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
int c = d->m_cursor;
#ifndef QT_NO_IM
c += d->m_preeditCursor;
#endif
if (d->m_echoMode == NoEcho)
c = 0;
QTextLine l = d->m_textLayout.lineForTextPosition(c);
if (!l.isValid())
return QRectF();
return QRectF(l.cursorToX(c) - d->hscroll, l.y() - d->vscroll, 1, l.height());
}
/*!
\qmlproperty int QtQuick2::TextInput::selectionStart
The cursor position before the first character in the current selection.
This property is read-only. To change the selection, use select(start,end),
selectAll(), or selectWord().
\sa selectionEnd, cursorPosition, selectedText
*/
int QQuickTextInput::selectionStart() const
{
Q_D(const QQuickTextInput);
return d->lastSelectionStart;
}
/*!
\qmlproperty int QtQuick2::TextInput::selectionEnd
The cursor position after the last character in the current selection.
This property is read-only. To change the selection, use select(start,end),
selectAll(), or selectWord().
\sa selectionStart, cursorPosition, selectedText
*/
int QQuickTextInput::selectionEnd() const
{
Q_D(const QQuickTextInput);
return d->lastSelectionEnd;
}
/*!
\qmlmethod QtQuick2::TextInput::select(int start, int end)
Causes the text from \a start to \a end to be selected.
If either start or end is out of range, the selection is not changed.
After calling this, selectionStart will become the lesser
and selectionEnd will become the greater (regardless of the order passed
to this method).
\sa selectionStart, selectionEnd
*/
void QQuickTextInput::select(int start, int end)
{
Q_D(QQuickTextInput);
if (start < 0 || end < 0 || start > d->m_text.length() || end > d->m_text.length())
return;
d->setSelection(start, end-start);
}
/*!
\qmlproperty string QtQuick2::TextInput::selectedText
This read-only property provides the text currently selected in the
text input.
841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
It is equivalent to the following snippet, but is faster and easier
to use.
\js
myTextInput.text.toString().substring(myTextInput.selectionStart,
myTextInput.selectionEnd);
\endjs
*/
QString QQuickTextInput::selectedText() const
{
Q_D(const QQuickTextInput);
return d->selectedText();
}
/*!
\qmlproperty bool QtQuick2::TextInput::activeFocusOnPress
Whether the TextInput should gain active focus on a mouse press. By default this is
set to true.
*/
bool QQuickTextInput::focusOnPress() const
{
Q_D(const QQuickTextInput);
return d->focusOnPress;
}
void QQuickTextInput::setFocusOnPress(bool b)
{
Q_D(QQuickTextInput);
if (d->focusOnPress == b)
return;
d->focusOnPress = b;
emit activeFocusOnPressChanged(d->focusOnPress);
}
/*!
\qmlproperty bool QtQuick2::TextInput::autoScroll
Whether the TextInput should scroll when the text is longer than the width. By default this is
set to true.
*/
bool QQuickTextInput::autoScroll() const
{
Q_D(const QQuickTextInput);
return d->autoScroll;
}
void QQuickTextInput::setAutoScroll(bool b)
{
Q_D(QQuickTextInput);
if (d->autoScroll == b)
return;
d->autoScroll = b;
//We need to repaint so that the scrolling is taking into account.
updateCursorRectangle();
emit autoScrollChanged(d->autoScroll);
}
#ifndef QT_NO_VALIDATOR
/*!
\qmltype IntValidator
\instantiates QIntValidator
\inqmlmodule QtQuick 2
\ingroup qtquick-text-utility
\brief Defines a validator for integer values
The IntValidator type provides a validator for integer values.
911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
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
and negative signs. In addition, IntValidator is always guaranteed to accept a number
formatted according to the "C" locale.
*/
QQuickIntValidator::QQuickIntValidator(QObject *parent)
: QIntValidator(parent)
{
}
/*!
\qmlproperty string QtQuick2::IntValidator::locale
This property holds the name of the locale used to interpret the number.
\sa QML:Qt::locale()
*/
QString QQuickIntValidator::localeName() const
{
return locale().name();
}
void QQuickIntValidator::setLocaleName(const QString &name)
{
if (locale().name() != name) {
setLocale(QLocale(name));
emit localeNameChanged();
}
}
void QQuickIntValidator::resetLocaleName()
{
QLocale defaultLocale;
if (locale() != defaultLocale) {
setLocale(defaultLocale);
emit localeNameChanged();
}
}
/*!
\qmlproperty int QtQuick2::IntValidator::top
This property holds the validator's highest acceptable value.
By default, this property's value is derived from the highest signed integer available (typically 2147483647).
*/
/*!
\qmlproperty int QtQuick2::IntValidator::bottom
This property holds the validator's lowest acceptable value.
By default, this property's value is derived from the lowest signed integer available (typically -2147483647).
*/
/*!
\qmltype DoubleValidator
\instantiates QDoubleValidator
\inqmlmodule QtQuick 2
\ingroup qtquick-text-utility
\brief Defines 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.
Input is accepected but invalid if it contains a double that is outside
the range or is in the wrong format; e.g. with too many digits after the
981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
decimal point or is empty.
Input is rejected if it is not a double.
Note: If the valid range consists of just positive doubles (e.g. 0.0 to
100.0) and input is a negative double then it is rejected. If \l notation
is set to DoubleValidator.StandardNotation, and the input contains more
digits before the decimal point than a double in the valid range may have,
it is also rejected. If \l notation is DoubleValidator.ScientificNotation,
and the input is not in the valid range, it is accecpted but invalid. The
value may yet become valid by changing the exponent.
*/
QQuickDoubleValidator::QQuickDoubleValidator(QObject *parent)
: QDoubleValidator(parent)
{
}
/*!
\qmlproperty string QtQuick2::DoubleValidator::locale
This property holds the name of the locale used to interpret the number.
\sa QML:Qt::locale()
*/
QString QQuickDoubleValidator::localeName() const
{
return locale().name();
}
void QQuickDoubleValidator::setLocaleName(const QString &name)
{
if (locale().name() != name) {
setLocale(QLocale(name));
emit localeNameChanged();
}
}
void QQuickDoubleValidator::resetLocaleName()
{
QLocale defaultLocale;
if (locale() != defaultLocale) {
setLocale(defaultLocale);
emit localeNameChanged();
}
}
/*!
\qmlproperty real QtQuick2::DoubleValidator::top
This property holds the validator's maximum acceptable value.
By default, this property contains a value of infinity.
*/
/*!
\qmlproperty real QtQuick2::DoubleValidator::bottom
This property holds the validator's minimum acceptable value.
By default, this property contains a value of -infinity.
*/
/*!
\qmlproperty int QtQuick2::DoubleValidator::decimals
This property holds the validator's maximum number of digits after the decimal point.
By default, this property contains a value of 1000.
*/
/*!
\qmlproperty enumeration QtQuick2::DoubleValidator::notation
This property holds the notation of how a string can describe a number.
1051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
The possible values for this property are:
\list
\li DoubleValidator.StandardNotation
\li DoubleValidator.ScientificNotation (default)
\endlist
If this property is set to DoubleValidator.ScientificNotation, the written number may have an exponent part (e.g. 1.5E-2).
*/
/*!
\qmltype RegExpValidator
\instantiates QRegExpValidator
\inqmlmodule QtQuick 2
\ingroup qtquick-text-utility
\brief Provides a string validator
The RegExpValidator type provides a validator, which counts as valid any string which
matches a specified regular expression.
*/
/*!
\qmlproperty regExp QtQuick2::RegExpValidator::regExp
This property holds the regular expression used for validation.
Note that this property should be a regular expression in JS syntax, e.g /a/ for the regular expression
matching "a".
By default, this property contains a regular expression with the pattern .* that matches any string.
*/
/*!
\qmlproperty Validator QtQuick2::TextInput::validator
Allows you to set a validator on the TextInput. When a validator is set
the TextInput will only accept input which leaves the text property in
an acceptable or intermediate state. The accepted signal will only be sent
if the text is in an acceptable state when enter is pressed.
Currently supported validators are IntValidator, DoubleValidator and
RegExpValidator. An example of using validators is shown below, which allows
input of integers between 11 and 31 into the text input:
\code
import QtQuick 2.0
TextInput{
validator: IntValidator{bottom: 11; top: 31;}
focus: true
}
\endcode
\sa acceptableInput, inputMask
*/
QValidator* QQuickTextInput::validator() const
{
Q_D(const QQuickTextInput);
return d->m_validator;
}
void QQuickTextInput::setValidator(QValidator* v)
{
Q_D(QQuickTextInput);
if (d->m_validator == v)
return;
if (d->m_validator) {
qmlobject_disconnect(
d->m_validator, QValidator, SIGNAL(changed()),
this, QQuickTextInput, SLOT(q_validatorChanged()));
1121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
}
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()
{
Q_Q(QQuickTextInput);
ValidatorState state = hasAcceptableInput(m_text);
m_validInput = state != InvalidInput;
if (state != AcceptableInput) {
if (m_acceptableInput) {
m_acceptableInput = false;
emit q->acceptableInputChanged();
}
} else if (!m_acceptableInput) {
m_acceptableInput = true;
emit q->acceptableInputChanged();
}
}
/*!
\qmlproperty string QtQuick2::TextInput::inputMask
Allows you to set an input mask on the TextInput, restricting the allowable
text inputs. See QLineEdit::inputMask for further details, as the exact
same mask strings are used by TextInput.
\sa acceptableInput, validator
*/
QString QQuickTextInput::inputMask() const
{
Q_D(const QQuickTextInput);
return d->inputMask();
}
void QQuickTextInput::setInputMask(const QString &im)
{
Q_D(QQuickTextInput);
if (d->inputMask() == im)
return;
d->setInputMask(im);
emit inputMaskChanged(d->inputMask());
}
/*!
\qmlproperty bool QtQuick2::TextInput::acceptableInput
This property is always true unless a validator or input mask has been set.
1191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
If a validator or input mask has been set, this property will only be true
if the current text is acceptable to the validator or input mask as a final
string (not as an intermediate string).
*/
bool QQuickTextInput::hasAcceptableInput() const
{
Q_D(const QQuickTextInput);
return d->hasAcceptableInput(d->m_text) == QQuickTextInputPrivate::AcceptableInput;
}
/*!
\qmlsignal QtQuick2::TextInput::onAccepted()
This handler is called when the Return or Enter key is pressed.
Note that if there is a \l validator or \l inputMask set on the text
input, the handler will only be emitted if the input is in an acceptable
state.
*/
#ifndef QT_NO_IM
Qt::InputMethodHints QQuickTextInputPrivate::effectiveInputMethodHints() const
{
Qt::InputMethodHints hints = inputMethodHints;
if (m_echoMode == QQuickTextInput::Password || m_echoMode == QQuickTextInput::NoEcho)
hints |= Qt::ImhHiddenText;
else if (m_echoMode == QQuickTextInput::PasswordEchoOnEdit)
hints &= ~Qt::ImhHiddenText;
if (m_echoMode != QQuickTextInput::Normal)
hints |= (Qt::ImhNoAutoUppercase | Qt::ImhNoPredictiveText | Qt::ImhSensitiveData);
return hints;
}
#endif
/*!
\qmlproperty enumeration QtQuick2::TextInput::echoMode
Specifies how the text should be displayed in the TextInput.
\list
\li TextInput.Normal - Displays the text as it is. (Default)
\li TextInput.Password - Displays asterisks instead of characters.
\li TextInput.NoEcho - Displays nothing.
\li TextInput.PasswordEchoOnEdit - Displays characters as they are entered
while editing, otherwise displays asterisks.
\endlist
*/
QQuickTextInput::EchoMode QQuickTextInput::echoMode() const
{
Q_D(const QQuickTextInput);
return QQuickTextInput::EchoMode(d->m_echoMode);
}
void QQuickTextInput::setEchoMode(QQuickTextInput::EchoMode echo)
{
Q_D(QQuickTextInput);
if (echoMode() == echo)
return;
d->cancelPasswordEchoTimer();
d->m_echoMode = echo;
d->m_passwordEchoEditing = false;
#ifndef QT_NO_IM
updateInputMethod(Qt::ImHints);
#endif
d->updateDisplayText();
updateCursorRectangle();
emit echoModeChanged(echoMode());
}
#ifndef QT_NO_IM
/*!
1261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
\qmlproperty enumeration QtQuick2::TextInput::inputMethodHints
Provides hints to the input method about the expected content of the text input and how it
should operate.
The value is a bit-wise combination of flags, or Qt.ImhNone if no hints are set.
Flags that alter behaviour are:
\list
\li Qt.ImhHiddenText - Characters should be hidden, as is typically used when entering passwords.
This is automatically set when setting echoMode to \c TextInput.Password.
\li Qt.ImhSensitiveData - Typed text should not be stored by the active input method
in any persistent storage like predictive user dictionary.
\li Qt.ImhNoAutoUppercase - The input method should not try to automatically switch to upper case
when a sentence ends.
\li Qt.ImhPreferNumbers - Numbers are preferred (but not required).
\li Qt.ImhPreferUppercase - Upper case letters are preferred (but not required).
\li Qt.ImhPreferLowercase - Lower case letters are preferred (but not required).
\li Qt.ImhNoPredictiveText - Do not use predictive text (i.e. dictionary lookup) while typing.
\li Qt.ImhDate - The text editor functions as a date field.
\li Qt.ImhTime - The text editor functions as a time field.
\endlist
Flags that restrict input (exclusive flags) are:
\list
\li Qt.ImhDigitsOnly - Only digits are allowed.
\li Qt.ImhFormattedNumbersOnly - Only number input is allowed. This includes decimal point and minus sign.
\li Qt.ImhUppercaseOnly - Only upper case letter input is allowed.
\li Qt.ImhLowercaseOnly - Only lower case letter input is allowed.
\li Qt.ImhDialableCharactersOnly - Only characters suitable for phone dialing are allowed.
\li Qt.ImhEmailCharactersOnly - Only characters suitable for email addresses are allowed.
\li Qt.ImhUrlCharactersOnly - Only characters suitable for URLs are allowed.
\endlist
Masks:
\list
\li Qt.ImhExclusiveInputMask - This mask yields nonzero if any of the exclusive flags are used.
\endlist
*/
Qt::InputMethodHints QQuickTextInput::inputMethodHints() const
{
Q_D(const QQuickTextInput);
return d->inputMethodHints;
}
void QQuickTextInput::setInputMethodHints(Qt::InputMethodHints hints)
{
Q_D(QQuickTextInput);
if (hints == d->inputMethodHints)
return;
d->inputMethodHints = hints;
updateInputMethod(Qt::ImHints);
emit inputMethodHintsChanged();
}
#endif // QT_NO_IM
/*!
\qmlproperty Component QtQuick2::TextInput::cursorDelegate
The delegate for the cursor in the TextInput.
If you set a cursorDelegate for a TextInput, this delegate will be used for
drawing the cursor instead of the standard cursor. An instance of the
delegate will be created and managed by the TextInput when a cursor is
1331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
needed, and the x property of delegate instance will be set so as
to be one pixel before the top left of the current character.
Note that the root item of the delegate component must be a QQuickItem or
QQuickItem derived item.
*/
QQmlComponent* QQuickTextInput::cursorDelegate() const
{
Q_D(const QQuickTextInput);
return d->cursorComponent;
}
void QQuickTextInput::setCursorDelegate(QQmlComponent* c)
{
Q_D(QQuickTextInput);
QQuickTextUtil::setCursorDelegate(d, c);
}
void QQuickTextInput::createCursor()
{
Q_D(QQuickTextInput);
d->cursorPending = true;
QQuickTextUtil::createCursor(d);
}
/*!
\qmlmethod rect QtQuick2::TextInput::positionToRectangle(int pos)
This function takes a character position and returns the rectangle that the
cursor would occupy, if it was placed at that character position.
This is similar to setting the cursorPosition, and then querying the cursor
rectangle, but the cursorPosition is not changed.
*/
QRectF QQuickTextInput::positionToRectangle(int pos) const
{
Q_D(const QQuickTextInput);
if (d->m_echoMode == NoEcho)
pos = 0;
#ifndef QT_NO_IM
else if (pos > d->m_cursor)
pos += d->preeditAreaText().length();
#endif
QTextLine l = d->m_textLayout.lineForTextPosition(pos);
return l.isValid()
? QRectF(l.cursorToX(pos) - d->hscroll, l.y() - d->vscroll, 1, l.height())
: QRectF();
}
/*!
\qmlmethod int QtQuick2::TextInput::positionAt(real x, real y, CursorPosition position = CursorBetweenCharacters)
This function returns the character position at
x and y pixels from the top left of the textInput. Position 0 is before the
first character, position 1 is after the first character but before the second,
and so on until position text.length, which is after all characters.
This means that for all x values before the first character this function returns 0,
and for all x values after the last character this function returns text.length. If
the y value is above the text the position will be that of the nearest character on
the first line line and if it is below the text the position of the nearest character
on the last line will be returned.
The cursor position type specifies how the cursor position should be resolved.
\list
\li TextInput.CursorBetweenCharacters - Returns the position between characters that is nearest x.
\li TextInput.CursorOnCharacter - Returns the position before the character that is nearest x.
\endlist
*/
1401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470
void QQuickTextInput::positionAt(QQmlV4Function *args) const
{
Q_D(const QQuickTextInput);
qreal x = 0;
qreal y = 0;
QTextLine::CursorPosition position = QTextLine::CursorBetweenCharacters;
if (args->length() < 1)
return;
int i = 0;
v8::Handle<v8::Value> arg = (*args)[i];
x = arg->NumberValue();
if (++i < args->length()) {
arg = (*args)[i];
y = arg->NumberValue();
}
if (++i < args->length()) {
arg = (*args)[i];
position = QTextLine::CursorPosition(arg->Int32Value());
}
int pos = d->positionAt(x, y, position);
const int cursor = d->m_cursor;
if (pos > cursor) {
#ifndef QT_NO_IM
const int preeditLength = d->preeditAreaText().length();
pos = pos > cursor + preeditLength
? pos - preeditLength
: cursor;
#else
pos = cursor;
#endif
}
args->setReturnValue(QV4::Value::fromInt32(pos));
}
int QQuickTextInputPrivate::positionAt(qreal x, qreal y, QTextLine::CursorPosition position) const
{
x += hscroll;
y += vscroll;
QTextLine line = m_textLayout.lineAt(0);
for (int i = 1; i < m_textLayout.lineCount(); ++i) {
QTextLine nextLine = m_textLayout.lineAt(i);
if (y < (line.rect().bottom() + nextLine.y()) / 2)
break;
line = nextLine;
}
return line.isValid() ? line.xToCursor(x, position) : 0;
}
void QQuickTextInput::keyPressEvent(QKeyEvent* ev)
{
Q_D(QQuickTextInput);
// Don't allow MacOSX up/down support, and we don't allow a completer.
bool ignore = (ev->key() == Qt::Key_Up || ev->key() == Qt::Key_Down) && ev->modifiers() == Qt::NoModifier;
if (!ignore && (d->lastSelectionStart == d->lastSelectionEnd) && (ev->key() == Qt::Key_Right || ev->key() == Qt::Key_Left)) {
// Ignore when moving off the end unless there is a selection,
// because then moving will do something (deselect).
int cursorPosition = d->m_cursor;
if (cursorPosition == 0)
ignore = ev->key() == (d->layoutDirection() == Qt::LeftToRight ? Qt::Key_Left : Qt::Key_Right);
if (!ignore && cursorPosition == text().length())
ignore = ev->key() == (d->layoutDirection() == Qt::LeftToRight ? Qt::Key_Right : Qt::Key_Left);
}
1471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
if (ignore) {
ev->ignore();
} else {
d->processKeyEvent(ev);
}
if (!ev->isAccepted())
QQuickImplicitSizeItem::keyPressEvent(ev);
}
#ifndef QT_NO_IM
void QQuickTextInput::inputMethodEvent(QInputMethodEvent *ev)
{
Q_D(QQuickTextInput);
const bool wasComposing = d->hasImState;
if (d->m_readOnly) {
ev->ignore();
} else {
d->processInputMethodEvent(ev);
}
if (!ev->isAccepted())
QQuickImplicitSizeItem::inputMethodEvent(ev);
if (wasComposing != d->hasImState)
emit inputMethodComposingChanged();
}
#endif
void QQuickTextInput::mouseDoubleClickEvent(QMouseEvent *event)
{
Q_D(QQuickTextInput);
if (d->selectByMouse && event->button() == Qt::LeftButton) {
#ifndef QT_NO_IM
d->commitPreedit();
#endif
int cursor = d->positionAt(event->localPos());
d->selectWordAtPos(cursor);
event->setAccepted(true);
if (!d->hasPendingTripleClick()) {
d->tripleClickStartPoint = event->localPos();
d->tripleClickTimer.start();
}
} else {
if (d->sendMouseEventToInputContext(event))
return;
QQuickImplicitSizeItem::mouseDoubleClickEvent(event);
}
}
void QQuickTextInput::mousePressEvent(QMouseEvent *event)
{
Q_D(QQuickTextInput);
d->pressPos = event->localPos();
if (d->sendMouseEventToInputContext(event))
return;
if (d->selectByMouse) {
setKeepMouseGrab(false);
d->selectPressed = true;
QPointF distanceVector = d->pressPos - d->tripleClickStartPoint;
if (d->hasPendingTripleClick()
&& distanceVector.manhattanLength() < qApp->styleHints()->startDragDistance()) {
event->setAccepted(true);
selectAll();
return;
}
}
1541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610
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();
#ifndef QT_NO_IM
// re-open input panel on press if already focused
if (hasActiveFocus() && hadActiveFocus && !d->m_readOnly)
qGuiApp->inputMethod()->show();
#endif
}
event->setAccepted(true);
}
void QQuickTextInput::mouseMoveEvent(QMouseEvent *event)
{
Q_D(QQuickTextInput);
if (d->selectPressed) {
if (qAbs(int(event->localPos().x() - d->pressPos.x())) > qApp->styleHints()->startDragDistance())
setKeepMouseGrab(true);
#ifndef QT_NO_IM
if (d->composeMode()) {
// start selection
int startPos = d->positionAt(d->pressPos);
int currentPos = d->positionAt(event->localPos());
if (startPos != currentPos)
d->setSelection(startPos, currentPos - startPos);
} else
#endif
{
moveCursorSelection(d->positionAt(event->localPos()), d->mouseSelectionMode);
}
event->setAccepted(true);
} else {
QQuickImplicitSizeItem::mouseMoveEvent(event);
}
}
void QQuickTextInput::mouseReleaseEvent(QMouseEvent *event)
{
Q_D(QQuickTextInput);
if (d->sendMouseEventToInputContext(event))
return;
if (d->selectPressed) {
d->selectPressed = false;
setKeepMouseGrab(false);
}
#ifndef QT_NO_CLIPBOARD
if (QGuiApplication::clipboard()->supportsSelection()) {
if (event->button() == Qt::LeftButton) {
d->copy(QClipboard::Selection);
} else if (!d->m_readOnly && event->button() == Qt::MidButton) {
d->deselect();
d->insert(QGuiApplication::clipboard()->text(QClipboard::Selection));
}
}
#endif
if (!event->isAccepted())
QQuickImplicitSizeItem::mouseReleaseEvent(event);
}
bool QQuickTextInputPrivate::sendMouseEventToInputContext(QMouseEvent *event)
{
#if !defined QT_NO_IM
if (composeMode()) {
1611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680
int tmp_cursor = positionAt(event->localPos());
int mousePos = tmp_cursor - m_cursor;
if (mousePos >= 0 && mousePos <= m_textLayout.preeditAreaText().length()) {
if (event->type() == QEvent::MouseButtonRelease) {
qApp->inputMethod()->invokeAction(QInputMethod::Click, mousePos);
}
return true;
}
}
#else
Q_UNUSED(event);
#endif
return false;
}
void QQuickTextInput::mouseUngrabEvent()
{
Q_D(QQuickTextInput);
d->selectPressed = false;
setKeepMouseGrab(false);
}
bool QQuickTextInput::event(QEvent* ev)
{
#ifndef QT_NO_SHORTCUT
Q_D(QQuickTextInput);
if (ev->type() == QEvent::ShortcutOverride) {
if (d->m_readOnly)
return false;
QKeyEvent* ke = static_cast<QKeyEvent*>(ev);
if (ke == QKeySequence::Copy
|| ke == QKeySequence::Paste
|| ke == QKeySequence::Cut
|| ke == QKeySequence::Redo
|| ke == QKeySequence::Undo
|| ke == QKeySequence::MoveToNextWord
|| ke == QKeySequence::MoveToPreviousWord
|| ke == QKeySequence::MoveToStartOfDocument
|| ke == QKeySequence::MoveToEndOfDocument
|| ke == QKeySequence::SelectNextWord
|| ke == QKeySequence::SelectPreviousWord
|| ke == QKeySequence::SelectStartOfLine
|| ke == QKeySequence::SelectEndOfLine
|| ke == QKeySequence::SelectStartOfBlock
|| ke == QKeySequence::SelectEndOfBlock
|| ke == QKeySequence::SelectStartOfDocument
|| ke == QKeySequence::SelectAll
|| ke == QKeySequence::SelectEndOfDocument) {
ke->accept();
} else if (ke->modifiers() == Qt::NoModifier || ke->modifiers() == Qt::ShiftModifier
|| ke->modifiers() == Qt::KeypadModifier) {
if (ke->key() < Qt::Key_Escape) {
ke->accept();
return true;
} else {
switch (ke->key()) {
case Qt::Key_Delete:
case Qt::Key_Home:
case Qt::Key_End:
case Qt::Key_Backspace:
case Qt::Key_Left:
case Qt::Key_Right:
return true;
default:
break;
}
}
}
}
1681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750
#endif
return QQuickImplicitSizeItem::event(ev);
}
void QQuickTextInput::geometryChanged(const QRectF &newGeometry,
const QRectF &oldGeometry)
{
Q_D(QQuickTextInput);
if (!d->inLayout) {
if (newGeometry.width() != oldGeometry.width())
d->updateLayout();
updateCursorRectangle();
}
QQuickImplicitSizeItem::geometryChanged(newGeometry, oldGeometry);
}
void QQuickTextInputPrivate::updateHorizontalScroll()
{
Q_Q(QQuickTextInput);
#ifndef QT_NO_IM
QTextLine currentLine = m_textLayout.lineForTextPosition(m_cursor + m_preeditCursor);
const int preeditLength = m_textLayout.preeditAreaText().length();
#else
QTextLine currentLine = m_textLayout.lineForTextPosition(m_cursor);
#endif
const qreal width = qMax<qreal>(0, q->width());
qreal cix = 0;
qreal widthUsed = 0;
if (currentLine.isValid()) {
#ifndef QT_NO_IM
cix = currentLine.cursorToX(m_cursor + preeditLength);
#else
cix = currentLine.cursorToX(m_cursor);
#endif
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());
if (cix - hscroll >= width) {
// text doesn't fit, cursor is to the right of br (scroll right)
hscroll = cix - width;
} else if (cix - hscroll < 0 && hscroll < widthUsed) {
// text doesn't fit, cursor is to the left of br (scroll left)
hscroll = cix;
} else if (widthUsed - hscroll < 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;
}
#ifndef QT_NO_IM
if (preeditLength > 0) {
// check to ensure long pre-edit text doesn't push the cursor
// off to the left
cix = currentLine.cursorToX(m_cursor + qMax(0, m_preeditCursor - 1));
if (cix < hscroll)
hscroll = cix;
}
#endif
}
if (previousScroll != hscroll)
1751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820
textLayoutDirty = true;
}
void QQuickTextInputPrivate::updateVerticalScroll()
{
Q_Q(QQuickTextInput);
#ifndef QT_NO_IM
const int preeditLength = m_textLayout.preeditAreaText().length();
#endif
const qreal height = qMax<qreal>(0, q->height());
qreal heightUsed = contentSize.height();
qreal previousScroll = vscroll;
if (!autoScroll || heightUsed <= height) {
// text fits in br; use vscroll for alignment
vscroll = -QQuickTextUtil::alignedY(
heightUsed, height, vAlign & ~(Qt::AlignAbsolute|Qt::AlignHorizontal_Mask));
} else {
#ifndef QT_NO_IM
QTextLine currentLine = m_textLayout.lineForTextPosition(m_cursor + preeditLength);
#else
QTextLine currentLine = m_textLayout.lineForTextPosition(m_cursor);
#endif
QRectF r = currentLine.isValid() ? currentLine.rect() : QRectF();
qreal top = r.top();
int bottom = r.bottom();
if (bottom - vscroll >= height) {
// text doesn't fit, cursor is to the below the br (scroll down)
vscroll = bottom - height;
} else if (top - vscroll < 0 && vscroll < heightUsed) {
// text doesn't fit, cursor is above br (scroll up)
vscroll = top;
} else if (heightUsed - vscroll < height) {
// text doesn't fit, text document is to the left of br; align
// right
vscroll = heightUsed - height;
}
#ifndef QT_NO_IM
if (preeditLength > 0) {
// check to ensure long pre-edit text doesn't push the cursor
// off the top
currentLine = m_textLayout.lineForTextPosition(m_cursor + qMax(0, m_preeditCursor - 1));
top = currentLine.isValid() ? currentLine.rect().top() : 0;
if (top < vscroll)
vscroll = top;
}
#endif
}
if (previousScroll != vscroll)
textLayoutDirty = true;
}
void QQuickTextInput::triggerPreprocess()
{
Q_D(QQuickTextInput);
if (d->updateType == QQuickTextInputPrivate::UpdateNone)
d->updateType = QQuickTextInputPrivate::UpdateOnlyPreprocess;
update();
}
QSGNode *QQuickTextInput::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *data)
{
Q_UNUSED(data);
Q_D(QQuickTextInput);
if (d->updateType != QQuickTextInputPrivate::UpdatePaintNode && oldNode != 0) {
// Update done in preprocess() in the nodes
d->updateType = QQuickTextInputPrivate::UpdateNone;
return oldNode;
1821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890
}
d->updateType = QQuickTextInputPrivate::UpdateNone;
QQuickTextNode *node = static_cast<QQuickTextNode *>(oldNode);
if (node == 0)
node = new QQuickTextNode(QQuickItemPrivate::get(this)->sceneGraphContext(), this);
d->textNode = node;
if (!d->textLayoutDirty && oldNode != 0) {
QSGSimpleRectNode *cursorNode = node->cursorNode();
if (cursorNode != 0 && !isReadOnly()) {
cursorNode->setRect(cursorRectangle());
if (!d->cursorVisible || d->cursorItem || (!d->m_blinkStatus && d->m_blinkPeriod > 0)) {
d->hideCursor();
} else {
d->showCursor();
}
}
} else {
node->setUseNativeRenderer(d->renderType == NativeRendering && d->window->devicePixelRatio() <= 1);
node->deleteContent();
node->setMatrix(QMatrix4x4());
QPointF offset(0, 0);
if (d->autoScroll && d->m_textLayout.lineCount() > 0) {
QFontMetricsF fm(d->font);
// the y offset is there to keep the baseline constant in case we have script changes in the text.
offset = -QPoint(d->hscroll, d->vscroll + d->m_textLayout.lineAt(0).ascent() - fm.ascent());
} else {
offset = -QPoint(d->hscroll, d->vscroll);
}
if (!d->m_textLayout.text().isEmpty()
#ifndef QT_NO_IM
|| !d->m_textLayout.preeditAreaText().isEmpty()
#endif
) {
node->addTextLayout(offset, &d->m_textLayout, d->color,
QQuickText::Normal, QColor(), QColor(),
d->selectionColor, d->selectedTextColor,
d->selectionStart(),
d->selectionEnd() - 1); // selectionEnd() returns first char after
// selection
}
if (!isReadOnly() && d->cursorItem == 0) {
node->setCursor(cursorRectangle(), d->color);
if (!d->cursorVisible || (!d->m_blinkStatus && d->m_blinkPeriod > 0)) {
d->hideCursor();
} else {
d->showCursor();
}
}
d->textLayoutDirty = false;
}
return node;
}
#ifndef QT_NO_IM
QVariant QQuickTextInput::inputMethodQuery(Qt::InputMethodQuery property) const
{
Q_D(const QQuickTextInput);
switch (property) {
case Qt::ImEnabled:
return QVariant((bool)(flags() & ItemAcceptsInputMethod));
case Qt::ImHints:
1891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960
return QVariant((int) d->effectiveInputMethodHints());
case Qt::ImCursorRectangle:
return cursorRectangle();
case Qt::ImFont:
return font();
case Qt::ImCursorPosition:
return QVariant(d->m_cursor);
case Qt::ImSurroundingText:
if (d->m_echoMode == PasswordEchoOnEdit && !d->m_passwordEchoEditing) {
return QVariant(displayText());
} else {
return QVariant(d->realText());
}
case Qt::ImCurrentSelection:
return QVariant(selectedText());
case Qt::ImMaximumTextLength:
return QVariant(maxLength());
case Qt::ImAnchorPosition:
if (d->selectionStart() == d->selectionEnd())
return QVariant(d->m_cursor);
else if (d->selectionStart() == d->m_cursor)
return QVariant(d->selectionEnd());
else
return QVariant(d->selectionStart());
default:
return QQuickItem::inputMethodQuery(property);
}
}
#endif // QT_NO_IM
/*!
\qmlmethod QtQuick2::TextInput::deselect()
Removes active text selection.
*/
void QQuickTextInput::deselect()
{
Q_D(QQuickTextInput);
d->deselect();
}
/*!
\qmlmethod QtQuick2::TextInput::selectAll()
Causes all text to be selected.
*/
void QQuickTextInput::selectAll()
{
Q_D(QQuickTextInput);
d->setSelection(0, text().length());
}
/*!
\qmlmethod QtQuick2::TextInput::isRightToLeft(int start, int end)
Returns true if the natural reading direction of the editor text
found between positions \a start and \a end is right to left.
*/
bool QQuickTextInput::isRightToLeft(int start, int end)
{
if (start > end) {
qmlInfo(this) << "isRightToLeft(start, end) called with the end property being smaller than the start.";
return false;
} else {
return text().mid(start, end - start).isRightToLeft();
}
}
#ifndef QT_NO_CLIPBOARD
/*!
1961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030
\qmlmethod QtQuick2::TextInput::cut()
Moves the currently selected text to the system clipboard.
*/
void QQuickTextInput::cut()
{
Q_D(QQuickTextInput);
if (!d->m_readOnly) {
d->copy();
d->del();
}
}
/*!
\qmlmethod QtQuick2::TextInput::copy()
Copies the currently selected text to the system clipboard.
*/
void QQuickTextInput::copy()
{
Q_D(QQuickTextInput);
d->copy();
}
/*!
\qmlmethod QtQuick2::TextInput::paste()
Replaces the currently selected text by the contents of the system clipboard.
*/
void QQuickTextInput::paste()
{
Q_D(QQuickTextInput);
if (!d->m_readOnly)
d->paste();
}
#endif // QT_NO_CLIPBOARD
/*!
\qmlmethod QtQuick2::TextInput::undo()
Undoes the last operation if undo is \l {canUndo}{available}. Deselects any
current selection, and updates the selection start to the current cursor
position.
*/
void QQuickTextInput::undo()
{
Q_D(QQuickTextInput);
if (!d->m_readOnly) {
d->internalUndo();
d->finishChange(-1, true);
}
}
/*!
\qmlmethod QtQuick2::TextInput::redo()
Redoes the last operation if redo is \l {canRedo}{available}.
*/
void QQuickTextInput::redo()
{
Q_D(QQuickTextInput);
if (!d->m_readOnly) {
d->internalRedo();
d->finishChange();
}
}
/*!
2031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100
\qmlmethod QtQuick2::TextInput::insert(int position, string text)
Inserts \a text into the TextInput at position.
*/
void QQuickTextInput::insert(int position, const QString &text)
{
Q_D(QQuickTextInput);
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;
const int priorState = d->m_undoState;
QString insertText = text;
if (d->hasSelectedText()) {
d->addCommand(QQuickTextInputPrivate::Command(
QQuickTextInputPrivate::SetSelection, d->m_cursor, 0, d->m_selstart, d->m_selend));
}
if (d->m_maskData) {
insertText = d->maskString(position, insertText);
for (int i = 0; i < insertText.length(); ++i) {
d->addCommand(QQuickTextInputPrivate::Command(
QQuickTextInputPrivate::DeleteSelection, position + i, d->m_text.at(position + i), -1, -1));
d->addCommand(QQuickTextInputPrivate::Command(
QQuickTextInputPrivate::Insert, position + i, insertText.at(i), -1, -1));
}
d->m_text.replace(position, insertText.length(), insertText);
if (!insertText.isEmpty())
d->m_textDirty = true;
if (position < d->m_selend && position + insertText.length() > d->m_selstart)
d->m_selDirty = true;
} else {
int remaining = d->m_maxLength - d->m_text.length();
if (remaining != 0) {
insertText = insertText.left(remaining);
d->m_text.insert(position, insertText);
for (int i = 0; i < insertText.length(); ++i)
d->addCommand(QQuickTextInputPrivate::Command(
QQuickTextInputPrivate::Insert, position + i, insertText.at(i), -1, -1));
if (d->m_cursor >= position)
d->m_cursor += insertText.length();
if (d->m_selstart >= position)
d->m_selstart += insertText.length();
if (d->m_selend >= position)
d->m_selend += insertText.length();
d->m_textDirty = true;
if (position >= d->m_selstart && position <= d->m_selend)
d->m_selDirty = true;
}
}
d->addCommand(QQuickTextInputPrivate::Command(
QQuickTextInputPrivate::SetSelection, d->m_cursor, 0, d->m_selstart, d->m_selend));
d->finishChange(priorState);
if (d->lastSelectionStart != d->lastSelectionEnd) {
if (d->m_selstart != d->lastSelectionStart) {
d->lastSelectionStart = d->m_selstart;
emit selectionStartChanged();
}
if (d->m_selend != d->lastSelectionEnd) {
d->lastSelectionEnd = d->m_selend;
emit selectionEndChanged();
}
2101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170
}
}
/*!
\qmlmethod QtQuick2::TextInput::remove(int start, int end)
Removes the section of text that is between the \a start and \a end positions from the TextInput.
*/
void QQuickTextInput::remove(int start, int end)
{
Q_D(QQuickTextInput);
start = qBound(0, start, d->m_text.length());
end = qBound(0, end, d->m_text.length());
if (start > end)
qSwap(start, end);
else if (start == end)
return;
if (start < d->m_selend && end > d->m_selstart)
d->m_selDirty = true;
const int priorState = d->m_undoState;
d->addCommand(QQuickTextInputPrivate::Command(
QQuickTextInputPrivate::SetSelection, d->m_cursor, 0, d->m_selstart, d->m_selend));
if (start <= d->m_cursor && d->m_cursor < end) {
// cursor is within the selection. Split up the commands
// to be able to restore the correct cursor position
for (int i = d->m_cursor; i >= start; --i) {
d->addCommand(QQuickTextInputPrivate::Command(
QQuickTextInputPrivate::DeleteSelection, i, d->m_text.at(i), -1, 1));
}
for (int i = end - 1; i > d->m_cursor; --i) {
d->addCommand(QQuickTextInputPrivate::Command(
QQuickTextInputPrivate::DeleteSelection, i - d->m_cursor + start - 1, d->m_text.at(i), -1, -1));
}
} else {
for (int i = end - 1; i >= start; --i) {
d->addCommand(QQuickTextInputPrivate::Command(
QQuickTextInputPrivate::RemoveSelection, i, d->m_text.at(i), -1, -1));
}
}
if (d->m_maskData) {
d->m_text.replace(start, end - start, d->clearString(start, end - start));
for (int i = 0; i < end - start; ++i) {
d->addCommand(QQuickTextInputPrivate::Command(
QQuickTextInputPrivate::Insert, start + i, d->m_text.at(start + i), -1, -1));
}
} else {
d->m_text.remove(start, end - start);
if (d->m_cursor > start)
d->m_cursor -= qMin(d->m_cursor, end) - start;
if (d->m_selstart > start)
d->m_selstart -= qMin(d->m_selstart, end) - start;
if (d->m_selend > end)
d->m_selend -= qMin(d->m_selend, end) - start;
}
d->addCommand(QQuickTextInputPrivate::Command(
QQuickTextInputPrivate::SetSelection, d->m_cursor, 0, d->m_selstart, d->m_selend));
d->m_textDirty = true;
d->finishChange(priorState);
if (d->lastSelectionStart != d->lastSelectionEnd) {
if (d->m_selstart != d->lastSelectionStart) {
2171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240
d->lastSelectionStart = d->m_selstart;
emit selectionStartChanged();
}
if (d->m_selend != d->lastSelectionEnd) {
d->lastSelectionEnd = d->m_selend;
emit selectionEndChanged();
}
}
}
/*!
\qmlmethod QtQuick2::TextInput::selectWord()
Causes the word closest to the current cursor position to be selected.
*/
void QQuickTextInput::selectWord()
{
Q_D(QQuickTextInput);
d->selectWordAtPos(d->m_cursor);
}
/*!
\qmlproperty string QtQuick2::TextInput::passwordCharacter
This is the character displayed when echoMode is set to Password or
PasswordEchoOnEdit. By default it is an asterisk.
If this property is set to a string with more than one character,
the first character is used. If the string is empty, the value
is ignored and the property is not set.
*/
QString QQuickTextInput::passwordCharacter() const
{
Q_D(const QQuickTextInput);
return QString(d->m_passwordCharacter);
}
void QQuickTextInput::setPasswordCharacter(const QString &str)
{
Q_D(QQuickTextInput);
if (str.length() < 1)
return;
d->m_passwordCharacter = str.constData()[0];
if (d->m_echoMode == Password || d->m_echoMode == PasswordEchoOnEdit)
d->updateDisplayText();
emit passwordCharacterChanged();
}
/*!
\qmlproperty string QtQuick2::TextInput::displayText
This is the text displayed in the TextInput.
If \l echoMode is set to TextInput::Normal, this holds the
same value as the TextInput::text property. Otherwise,
this property holds the text visible to the user, while
the \l text property holds the actual entered text.
*/
QString QQuickTextInput::displayText() const
{
Q_D(const QQuickTextInput);
return d->m_textLayout.text();
}
/*!
\qmlproperty bool QtQuick2::TextInput::selectByMouse
Defaults to false.
2241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310
If true, the user can use the mouse to select text in some
platform-specific way. Note that for some platforms this may
not be an appropriate interaction (eg. may conflict with how
the text needs to behave inside a Flickable.
*/
bool QQuickTextInput::selectByMouse() const
{
Q_D(const QQuickTextInput);
return d->selectByMouse;
}
void QQuickTextInput::setSelectByMouse(bool on)
{
Q_D(QQuickTextInput);
if (d->selectByMouse != on) {
d->selectByMouse = on;
emit selectByMouseChanged(on);
}
}
/*!
\qmlproperty enumeration QtQuick2::TextInput::mouseSelectionMode
Specifies how text should be selected using a mouse.
\list
\li TextInput.SelectCharacters - The selection is updated with individual characters. (Default)
\li TextInput.SelectWords - The selection is updated with whole words.
\endlist
This property only applies when \l selectByMouse is true.
*/
QQuickTextInput::SelectionMode QQuickTextInput::mouseSelectionMode() const
{
Q_D(const QQuickTextInput);
return d->mouseSelectionMode;
}
void QQuickTextInput::setMouseSelectionMode(SelectionMode mode)
{
Q_D(QQuickTextInput);
if (d->mouseSelectionMode != mode) {
d->mouseSelectionMode = mode;
emit mouseSelectionModeChanged(mode);
}
}
/*!
\qmlproperty bool QtQuick2::TextInput::persistentSelection
Whether the TextInput should keep its selection when it loses active focus to another
item in the scene. By default this is set to false;
*/
bool QQuickTextInput::persistentSelection() const
{
Q_D(const QQuickTextInput);
return d->persistentSelection;
}
void QQuickTextInput::setPersistentSelection(bool on)
{
Q_D(QQuickTextInput);
if (d->persistentSelection == on)
return;
d->persistentSelection = on;
emit persistentSelectionChanged();
}