Defer construction of TextEdit/TextInput delegates.

Don't create instances of the delegate components until the item
gains focus, or the cursorVisible property is set to true.

Cursor delegates are typically small and relatively fast to create and
so won't have a significant cost during a one off focus in event,
but that cost can still add up when creating a number of TextInputs
or TextEdits at once.  If a delegate that is instantiated immeditately
is required it is possible to instead create a child item and bind to
the cursorRectangle and cursorVisible properties.

Change-Id: I5ec2b5b6a30e534aee3dd5a58c6a5ac0686f5ce2
Reviewed-by: Michael Brasser <michael.brasser@nokia.com>
This commit is contained in:
Andrew den Exter 2012-05-04 16:07:32 +10:00 committed by Qt by Nokia
parent 6f9b3893c8
commit bd5e23a00e
15 changed files with 291 additions and 117 deletions

View File

@ -23,6 +23,7 @@ HEADERS += \
$$PWD/qquicktextcontrol_p_p.h \ $$PWD/qquicktextcontrol_p_p.h \
$$PWD/qquicktextedit_p.h \ $$PWD/qquicktextedit_p.h \
$$PWD/qquicktextedit_p_p.h \ $$PWD/qquicktextedit_p_p.h \
$$PWD/qquicktextutil_p.h \
$$PWD/qquickimagebase_p.h \ $$PWD/qquickimagebase_p.h \
$$PWD/qquickimagebase_p_p.h \ $$PWD/qquickimagebase_p_p.h \
$$PWD/qquickimage_p.h \ $$PWD/qquickimage_p.h \
@ -91,6 +92,7 @@ SOURCES += \
$$PWD/qquicktextinput.cpp \ $$PWD/qquicktextinput.cpp \
$$PWD/qquicktextcontrol.cpp \ $$PWD/qquicktextcontrol.cpp \
$$PWD/qquicktextedit.cpp \ $$PWD/qquicktextedit.cpp \
$$PWD/qquicktextutil.cpp \
$$PWD/qquickimagebase.cpp \ $$PWD/qquickimagebase.cpp \
$$PWD/qquickimage.cpp \ $$PWD/qquickimage.cpp \
$$PWD/qquickborderimage.cpp \ $$PWD/qquickborderimage.cpp \

View File

@ -46,6 +46,7 @@
#include "qquickevents_p_p.h" #include "qquickevents_p_p.h"
#include "qquickcanvas.h" #include "qquickcanvas.h"
#include "qquicktextnode_p.h" #include "qquicktextnode_p.h"
#include "qquicktextutil_p.h"
#include <QtQuick/qsgsimplerectnode.h> #include <QtQuick/qsgsimplerectnode.h>
#include <QtQml/qqmlinfo.h> #include <QtQml/qqmlinfo.h>
@ -60,6 +61,7 @@
#include <private/qtextengine_p.h> #include <private/qtextengine_p.h>
#include <private/qsgadaptationlayer_p.h> #include <private/qsgadaptationlayer_p.h>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
/*! /*!
@ -360,8 +362,8 @@ void QQuickTextEdit::setFont(const QFont &font)
if (oldFont != d->font) { if (oldFont != d->font) {
d->document->setDefaultFont(d->font); d->document->setDefaultFont(d->font);
if (d->cursor) { if (d->cursorItem) {
d->cursor->setHeight(QFontMetrics(d->font).height()); d->cursorItem->setHeight(QFontMetrics(d->font).height());
moveCursorDelegate(); moveCursorDelegate();
} }
updateSize(); updateSize();
@ -890,6 +892,8 @@ void QQuickTextEdit::setCursorVisible(bool on)
if (d->cursorVisible == on) if (d->cursorVisible == on)
return; return;
d->cursorVisible = on; d->cursorVisible = on;
if (on && isComponentComplete())
QQuickTextUtil::createCursor(d);
if (!on && !d->persistentSelection) if (!on && !d->persistentSelection)
d->control->setCursorIsFocusIndicator(true); d->control->setCursorIsFocusIndicator(true);
d->control->setCursorVisible(on); d->control->setCursorVisible(on);
@ -941,45 +945,14 @@ QQmlComponent* QQuickTextEdit::cursorDelegate() const
void QQuickTextEdit::setCursorDelegate(QQmlComponent* c) void QQuickTextEdit::setCursorDelegate(QQmlComponent* c)
{ {
Q_D(QQuickTextEdit); Q_D(QQuickTextEdit);
if (d->cursorComponent) { QQuickTextUtil::setCursorDelegate(d, c);
if (d->cursor) {
d->control->setCursorWidth(-1);
updateCursor();
delete d->cursor;
d->cursor = 0;
}
}
d->cursorComponent = c;
if (c && c->isReady()) {
loadCursorDelegate();
} else {
if (c)
connect(c, SIGNAL(statusChanged()),
this, SLOT(loadCursorDelegate()));
}
emit cursorDelegateChanged();
} }
void QQuickTextEdit::loadCursorDelegate() void QQuickTextEdit::createCursor()
{ {
Q_D(QQuickTextEdit); Q_D(QQuickTextEdit);
if (d->cursorComponent->isLoading() || !isComponentComplete()) d->cursorPending = true;
return; QQuickTextUtil::createCursor(d);
QQmlContext *creationContext = d->cursorComponent->creationContext();
QObject *object = d->cursorComponent->create(creationContext ? creationContext : qmlContext(this));
d->cursor = qobject_cast<QQuickItem*>(object);
if (d->cursor) {
d->control->setCursorWidth(0);
updateCursor();
QQml_setParent_noEvent(d->cursor, this);
d->cursor->setParentItem(this);
d->cursor->setHeight(QFontMetrics(d->font).height());
moveCursorDelegate();
}else{
delete object;
qmlInfo(this) << "Error loading cursor delegate.";
}
} }
/*! /*!
@ -1187,8 +1160,8 @@ void QQuickTextEdit::componentComplete()
updateSize(); updateSize();
d->dirty = false; d->dirty = false;
} }
if (d->cursorComponent && d->cursorComponent->isReady()) if (d->cursorComponent && isCursorVisible())
loadCursorDelegate(); QQuickTextUtil::createCursor(d);
} }
/*! /*!
\qmlproperty bool QtQuick2::TextEdit::selectByMouse \qmlproperty bool QtQuick2::TextEdit::selectByMouse
@ -1818,11 +1791,11 @@ void QQuickTextEdit::moveCursorDelegate()
d->determineHorizontalAlignment(); d->determineHorizontalAlignment();
updateInputMethod(); updateInputMethod();
emit cursorRectangleChanged(); emit cursorRectangleChanged();
if (!d->cursor) if (!d->cursorItem)
return; return;
QRectF cursorRect = cursorRectangle(); QRectF cursorRect = cursorRectangle();
d->cursor->setX(cursorRect.x()); d->cursorItem->setX(cursorRect.x());
d->cursor->setY(cursorRect.y()); d->cursorItem->setY(cursorRect.y());
} }
void QQuickTextEdit::updateSelectionMarkers() void QQuickTextEdit::updateSelectionMarkers()
@ -1843,7 +1816,7 @@ QRectF QQuickTextEdit::boundingRect() const
Q_D(const QQuickTextEdit); Q_D(const QQuickTextEdit);
QRectF r(0, -d->yoff, d->contentSize.width(), d->contentSize.height()); QRectF r(0, -d->yoff, d->contentSize.width(), d->contentSize.height());
int cursorWidth = 1; int cursorWidth = 1;
if (d->cursor) if (d->cursorItem)
cursorWidth = 0; cursorWidth = 0;
else if (!d->document->isEmpty()) else if (!d->document->isEmpty())
cursorWidth += 3;// ### Need a better way of accounting for space between char and cursor cursorWidth += 3;// ### Need a better way of accounting for space between char and cursor
@ -1885,8 +1858,8 @@ QRectF QQuickTextEdit::clipRect() const
Q_D(const QQuickTextEdit); Q_D(const QQuickTextEdit);
QRectF r = QQuickImplicitSizeItem::clipRect(); QRectF r = QQuickImplicitSizeItem::clipRect();
int cursorWidth = 1; int cursorWidth = 1;
if (d->cursor) if (d->cursorItem)
cursorWidth = d->cursor->width(); cursorWidth = d->cursorItem->width();
if (!d->document->isEmpty()) if (!d->document->isEmpty())
cursorWidth += 3;// ### Need a better way of accounting for space between char and cursor cursorWidth += 3;// ### Need a better way of accounting for space between char and cursor

View File

@ -290,7 +290,7 @@ private Q_SLOTS:
void q_textChanged(); void q_textChanged();
void updateSelectionMarkers(); void updateSelectionMarkers();
void moveCursorDelegate(); void moveCursorDelegate();
void loadCursorDelegate(); void createCursor();
void q_canPasteChanged(); void q_canPasteChanged();
void updateDocument(); void updateDocument();
void updateCursor(); void updateCursor();
@ -320,6 +320,8 @@ protected:
QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *updatePaintNodeData); QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *updatePaintNodeData);
friend class QQuickTextUtil;
private: private:
Q_DISABLE_COPY(QQuickTextEdit) Q_DISABLE_COPY(QQuickTextEdit)
Q_DECLARE_PRIVATE(QQuickTextEdit) Q_DECLARE_PRIVATE(QQuickTextEdit)

View File

@ -55,6 +55,7 @@
#include "qquicktextedit_p.h" #include "qquicktextedit_p.h"
#include "qquickimplicitsizeitem_p_p.h" #include "qquickimplicitsizeitem_p_p.h"
#include "qquicktextcontrol_p.h"
#include <QtQml/qqml.h> #include <QtQml/qqml.h>
@ -64,19 +65,21 @@ class QQuickTextDocumentWithImageResources;
class QQuickTextControl; class QQuickTextControl;
class QQuickTextEditPrivate : public QQuickImplicitSizeItemPrivate class QQuickTextEditPrivate : public QQuickImplicitSizeItemPrivate
{ {
public:
Q_DECLARE_PUBLIC(QQuickTextEdit) Q_DECLARE_PUBLIC(QQuickTextEdit)
public: typedef QQuickTextEdit Public;
QQuickTextEditPrivate() QQuickTextEditPrivate()
: color(QRgb(0xFF000000)), selectionColor(QRgb(0xFF000080)), selectedTextColor(QRgb(0xFFFFFFFF)) : color(QRgb(0xFF000000)), selectionColor(QRgb(0xFF000080)), selectedTextColor(QRgb(0xFFFFFFFF))
, textMargin(0.0), yoff(0), font(sourceFont), cursorComponent(0), cursor(0), document(0), control(0) , textMargin(0.0), yoff(0), font(sourceFont), cursorComponent(0), cursorItem(0), document(0), control(0)
, lastSelectionStart(0), lastSelectionEnd(0), lineCount(0) , lastSelectionStart(0), lastSelectionEnd(0), lineCount(0)
, hAlign(QQuickTextEdit::AlignLeft), vAlign(QQuickTextEdit::AlignTop) , hAlign(QQuickTextEdit::AlignLeft), vAlign(QQuickTextEdit::AlignTop)
, format(QQuickTextEdit::PlainText), wrapMode(QQuickTextEdit::NoWrap) , format(QQuickTextEdit::PlainText), wrapMode(QQuickTextEdit::NoWrap)
, contentDirection(Qt::LayoutDirectionAuto) , contentDirection(Qt::LayoutDirectionAuto)
, mouseSelectionMode(QQuickTextEdit::SelectCharacters), inputMethodHints(Qt::ImhNone) , mouseSelectionMode(QQuickTextEdit::SelectCharacters), inputMethodHints(Qt::ImhNone)
, updateType(UpdatePaintNode) , updateType(UpdatePaintNode)
, documentDirty(true), dirty(false), richText(false), cursorVisible(false) , documentDirty(true), dirty(false), richText(false), cursorVisible(false), cursorPending(false)
, focusOnPress(true), persistentSelection(false), requireImplicitWidth(false) , focusOnPress(true), persistentSelection(false), requireImplicitWidth(false)
, selectByMouse(false), canPaste(false), canPasteValid(false), hAlignImplicit(true) , selectByMouse(false), canPaste(false), canPasteValid(false), hAlignImplicit(true)
, textCached(false), inLayout(false) , textCached(false), inLayout(false)
@ -96,6 +99,8 @@ public:
qreal getImplicitWidth() const; qreal getImplicitWidth() const;
Qt::LayoutDirection textDirection(const QString &text) const; Qt::LayoutDirection textDirection(const QString &text) const;
void setNativeCursorEnabled(bool enabled) { control->setCursorWidth(enabled ? 1 : 0); }
QColor color; QColor color;
QColor selectionColor; QColor selectionColor;
QColor selectedTextColor; QColor selectedTextColor;
@ -111,7 +116,7 @@ public:
QFont font; QFont font;
QQmlComponent* cursorComponent; QQmlComponent* cursorComponent;
QQuickItem* cursor; QQuickItem* cursorItem;
QQuickTextDocumentWithImageResources *document; QQuickTextDocumentWithImageResources *document;
QQuickTextControl *control; QQuickTextControl *control;
@ -138,6 +143,7 @@ public:
bool dirty : 1; bool dirty : 1;
bool richText : 1; bool richText : 1;
bool cursorVisible : 1; bool cursorVisible : 1;
bool cursorPending : 1;
bool focusOnPress : 1; bool focusOnPress : 1;
bool persistentSelection : 1; bool persistentSelection : 1;
bool requireImplicitWidth:1; bool requireImplicitWidth:1;

View File

@ -42,9 +42,11 @@
#include "qquicktextinput_p.h" #include "qquicktextinput_p.h"
#include "qquicktextinput_p_p.h" #include "qquicktextinput_p_p.h"
#include "qquickcanvas.h" #include "qquickcanvas.h"
#include "qquicktextutil_p.h"
#include <private/qqmlglobal_p.h> #include <private/qqmlglobal_p.h>
#include <QtCore/qcoreapplication.h> #include <QtCore/qcoreapplication.h>
#include <QtQml/qqmlinfo.h> #include <QtQml/qqmlinfo.h>
#include <QtGui/qevent.h> #include <QtGui/qevent.h>
@ -102,8 +104,8 @@ void QQuickTextInput::componentComplete()
d->checkIsValid(); d->checkIsValid();
d->updateLayout(); d->updateLayout();
updateCursorRectangle(); updateCursorRectangle();
if (d->cursorComponent && d->cursorComponent->isReady()) if (d->cursorComponent && isCursorVisible())
createCursor(); QQuickTextUtil::createCursor(d);
} }
/*! /*!
@ -669,9 +671,13 @@ void QQuickTextInput::setCursorVisible(bool on)
if (d->cursorVisible == on) if (d->cursorVisible == on)
return; return;
d->cursorVisible = on; d->cursorVisible = on;
d->setCursorBlinkPeriod(on ? qApp->styleHints()->cursorFlashTime() : 0); if (on && isComponentComplete())
d->updateType = QQuickTextInputPrivate::UpdatePaintNode; QQuickTextUtil::createCursor(d);
update(); if (!d->cursorItem) {
d->setCursorBlinkPeriod(on ? qApp->styleHints()->cursorFlashTime() : 0);
d->updateType = QQuickTextInputPrivate::UpdatePaintNode;
update();
}
emit cursorVisibleChanged(d->cursorVisible); emit cursorVisibleChanged(d->cursorVisible);
} }
@ -1247,65 +1253,14 @@ QQmlComponent* QQuickTextInput::cursorDelegate() const
void QQuickTextInput::setCursorDelegate(QQmlComponent* c) void QQuickTextInput::setCursorDelegate(QQmlComponent* c)
{ {
Q_D(QQuickTextInput); Q_D(QQuickTextInput);
if (d->cursorComponent == c) QQuickTextUtil::setCursorDelegate(d, 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");
}
} }
void QQuickTextInput::createCursor() void QQuickTextInput::createCursor()
{ {
Q_D(QQuickTextInput); Q_D(QQuickTextInput);
if (!isComponentComplete()) d->cursorPending = true;
return; QQuickTextUtil::createCursor(d);
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());
} }
/*! /*!
@ -3189,10 +3144,9 @@ void QQuickTextInputPrivate::processInputMethodEvent(QInputMethodEvent *event)
m_textLayout.setPreeditArea(m_cursor, event->preeditString()); m_textLayout.setPreeditArea(m_cursor, event->preeditString());
#endif //QT_NO_IM #endif //QT_NO_IM
const int oldPreeditCursor = m_preeditCursor; const int oldPreeditCursor = m_preeditCursor;
const bool oldCursorVisible = cursorVisible;
m_preeditCursor = event->preeditString().length(); m_preeditCursor = event->preeditString().length();
hasImState = !event->preeditString().isEmpty(); hasImState = !event->preeditString().isEmpty();
cursorVisible = true; bool cursorVisible = true;
QList<QTextLayout::FormatRange> formats; QList<QTextLayout::FormatRange> formats;
for (int i = 0; i < event->attributes().size(); ++i) { for (int i = 0; i < event->attributes().size(); ++i) {
const QInputMethodEvent::Attribute &a = event->attributes().at(i); const QInputMethodEvent::Attribute &a = event->attributes().at(i);
@ -3224,8 +3178,7 @@ void QQuickTextInputPrivate::processInputMethodEvent(QInputMethodEvent *event)
if (isGettingInput) if (isGettingInput)
finishChange(priorState); finishChange(priorState);
if (cursorVisible != oldCursorVisible) q->setCursorVisible(cursorVisible);
emit q->cursorVisibleChanged(cursorVisible);
if (selectionChange) { if (selectionChange) {
emit q->selectionChanged(); emit q->selectionChanged();

View File

@ -344,6 +344,8 @@ private Q_SLOTS:
void triggerPreprocess(); void triggerPreprocess();
private: private:
friend class QQuickTextUtil;
Q_DECLARE_PRIVATE(QQuickTextInput) Q_DECLARE_PRIVATE(QQuickTextInput)
}; };

View File

@ -74,8 +74,11 @@ class QQuickTextNode;
class Q_AUTOTEST_EXPORT QQuickTextInputPrivate : public QQuickImplicitSizeItemPrivate class Q_AUTOTEST_EXPORT QQuickTextInputPrivate : public QQuickImplicitSizeItemPrivate
{ {
Q_DECLARE_PUBLIC(QQuickTextInput)
public: public:
Q_DECLARE_PUBLIC(QQuickTextInput)
typedef QQuickTextInput Public;
QQuickTextInputPrivate() QQuickTextInputPrivate()
: hscroll(0) : hscroll(0)
, vscroll(0) , vscroll(0)
@ -105,6 +108,7 @@ public:
, m_passwordCharacter(QLatin1Char('*')) , m_passwordCharacter(QLatin1Char('*'))
, focusOnPress(true) , focusOnPress(true)
, cursorVisible(false) , cursorVisible(false)
, cursorPending(false)
, autoScroll(true) , autoScroll(true)
, selectByMouse(false) , selectByMouse(false)
, canPaste(false) , canPaste(false)
@ -235,6 +239,7 @@ public:
bool focusOnPress:1; bool focusOnPress:1;
bool cursorVisible:1; bool cursorVisible:1;
bool cursorPending:1;
bool autoScroll:1; bool autoScroll:1;
bool selectByMouse:1; bool selectByMouse:1;
bool canPaste:1; bool canPaste:1;
@ -265,6 +270,8 @@ public:
return !tripleClickTimer.hasExpired(qApp->styleHints()->mouseDoubleClickInterval()); return !tripleClickTimer.hasExpired(qApp->styleHints()->mouseDoubleClickInterval());
} }
void setNativeCursorEnabled(bool enabled) {
setCursorBlinkPeriod(enabled && cursorVisible ? qApp->styleHints()->cursorFlashTime() : 0); }
int nextMaskBlank(int pos) int nextMaskBlank(int pos)
{ {

View File

@ -0,0 +1,81 @@
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the QtQml module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** 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, Nokia gives you certain additional
** rights. These rights are described in the Nokia 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.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qquicktextutil_p.h"
#include <QtQml/qqmlinfo.h>
#include <private/qqmlglobal_p.h>
QT_BEGIN_NAMESPACE
QQuickItem *QQuickTextUtil::createCursor(
QQmlComponent *component, QQuickItem *parent, const QRectF &rectangle, const char *className)
{
QQuickItem *item = 0;
if (component->isReady()) {
QQmlContext *creationContext = component->creationContext();
if (QObject *object = component->beginCreate(creationContext
? creationContext
: qmlContext(parent))) {
if ((item = qobject_cast<QQuickItem *>(object))) {
QQml_setParent_noEvent(item, parent);
item->setParentItem(parent);
item->setPos(rectangle.topLeft());
item->setHeight(rectangle.height());
} else {
qmlInfo(parent) << tr("%1 does not support loading non-visual cursor delegates.")
.arg(QString::fromUtf8(className));
}
component->completeCreate();
return item;
}
} else if (component->isLoading()) {
QObject::connect(component, SIGNAL(statusChanged(QQmlComponent::Status)),
parent, SLOT(createCursor()), Qt::UniqueConnection);
return item;
}
qmlInfo(parent, component->errors()) << tr("Could not load cursor delegate");
return item;
}
QT_END_NAMESPACE

View File

@ -0,0 +1,126 @@
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the QtQml module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** 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, Nokia gives you certain additional
** rights. These rights are described in the Nokia 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.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef QQUICKTEXTUTIL_P_H
#define QQUICKTEXTUTIL_P_H
//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists purely as an
// implementation detail. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//
#include <QtQml/qqml.h>
#include <QtQml/qqmlincubator.h>
#include <QtQuick/qquickitem.h>
QT_BEGIN_NAMESPACE
class QQuickTextUtil : public QObject // For the benefit of translations.
{
Q_OBJECT
public:
template <typename Private> static void setCursorDelegate(Private *d, QQmlComponent *delegate);
template <typename Private> static void createCursor(Private *d);
private:
static QQuickItem *createCursor(
QQmlComponent *component,
QQuickItem *parent,
const QRectF &cursorRectangle,
const char *className);
};
template <typename Private>
void QQuickTextUtil::setCursorDelegate(Private *d, QQmlComponent *delegate)
{
if (d->cursorComponent == delegate)
return;
typename Private::Public *parent = d->q_func();
if (d->cursorComponent) {
disconnect(d->cursorComponent, SIGNAL(statusChanged(QQmlComponent::Status)),
parent, SLOT(createCursor()));
}
delete d->cursorItem;
d->cursorItem = 0;
d->cursorPending = true;
d->cursorComponent = delegate;
if (parent->isCursorVisible() && parent->isComponentComplete())
createCursor(d);
emit parent->cursorDelegateChanged();
}
template <typename Private>
void QQuickTextUtil::createCursor(Private *d)
{
if (!d->cursorPending)
return;
d->cursorPending = false;
typename Private::Public *parent = d->q_func();
if (d->cursorComponent) {
d->cursorItem = createCursor(
d->cursorComponent,
parent,
parent->cursorRectangle(),
Private::Public::staticMetaObject.className());
}
d->setNativeCursorEnabled(!d->cursorItem);
d->updateType = Private::UpdatePaintNode;
parent->update();
}
QT_END_NAMESPACE
#endif

View File

@ -9,14 +9,18 @@ Rectangle { width: 300; height: 300; color: "white"
] ]
TextEdit { TextEdit {
cursorDelegate: cursorFail cursorDelegate: cursorFail
cursorVisible: true
} }
TextEdit { TextEdit {
cursorDelegate: cursorWait cursorDelegate: cursorWait
cursorVisible: true
} }
TextEdit { TextEdit {
cursorDelegate: cursorNorm cursorDelegate: cursorNorm
cursorVisible: true
} }
TextEdit { TextEdit {
cursorDelegate: cursorErr cursorDelegate: cursorErr
cursorVisible: true
} }
} }

View File

@ -8,11 +8,14 @@ Rectangle { width: 300; height: 300; color: "white"
] ]
TextEdit { TextEdit {
cursorDelegate: cursorFail cursorDelegate: cursorFail
cursorVisible: true
} }
TextEdit { TextEdit {
cursorDelegate: cursorWait cursorDelegate: cursorWait
cursorVisible: true
} }
TextEdit { TextEdit {
cursorDelegate: cursorNorm cursorDelegate: cursorNorm
cursorVisible: true
} }
} }

View File

@ -8,11 +8,14 @@ Rectangle { width: 300; height: 300; color: "white"
] ]
TextEdit { TextEdit {
cursorDelegate: cursorWait cursorDelegate: cursorWait
cursorVisible: true
} }
TextEdit { TextEdit {
cursorDelegate: cursorNorm cursorDelegate: cursorNorm
cursorVisible: true
} }
TextEdit { TextEdit {
cursorDelegate: cursorErr cursorDelegate: cursorErr
cursorVisible: true
} }
} }

View File

@ -8,10 +8,12 @@ Rectangle { width: 300; height: 300; color: "white"
TextEdit { TextEdit {
cursorDelegate: cursorWait cursorDelegate: cursorWait
text: "Hello" text: "Hello"
cursorVisible: true
} }
TextEdit { TextEdit {
objectName: "textEditObject" objectName: "textEditObject"
cursorDelegate: cursorNorm cursorDelegate: cursorNorm
cursorVisible: true
focus: true; focus: true;
text: "Hello" text: "Hello"
} }

View File

@ -1952,9 +1952,12 @@ void tst_qquicktextedit::cursorDelegate()
view.requestActivateWindow(); view.requestActivateWindow();
QQuickTextEdit *textEditObject = view.rootObject()->findChild<QQuickTextEdit*>("textEditObject"); QQuickTextEdit *textEditObject = view.rootObject()->findChild<QQuickTextEdit*>("textEditObject");
QVERIFY(textEditObject != 0); QVERIFY(textEditObject != 0);
QVERIFY(textEditObject->findChild<QQuickItem*>("cursorInstance")); // Delegate creation is deferred until focus in or cursor visiblity is forced.
QVERIFY(!textEditObject->findChild<QQuickItem*>("cursorInstance"));
QVERIFY(!textEditObject->isCursorVisible());
//Test Delegate gets created //Test Delegate gets created
textEditObject->setFocus(true); textEditObject->setFocus(true);
QVERIFY(textEditObject->isCursorVisible());
QQuickItem* delegateObject = textEditObject->findChild<QQuickItem*>("cursorInstance"); QQuickItem* delegateObject = textEditObject->findChild<QQuickItem*>("cursorInstance");
QVERIFY(delegateObject); QVERIFY(delegateObject);
QCOMPARE(delegateObject->property("localProperty").toString(), QString("Hello")); QCOMPARE(delegateObject->property("localProperty").toString(), QString("Hello"));
@ -2716,6 +2719,7 @@ void tst_qquicktextedit::clipRect()
cursorComponent.setData("import QtQuick 2.0\nRectangle { height: 20; width: 8 }", QUrl()); cursorComponent.setData("import QtQuick 2.0\nRectangle { height: 20; width: 8 }", QUrl());
edit->setCursorDelegate(&cursorComponent); edit->setCursorDelegate(&cursorComponent);
edit->setCursorVisible(true);
// If a cursor delegate is used it's size should determine the excess width. // If a cursor delegate is used it's size should determine the excess width.
QCOMPARE(edit->clipRect().x(), qreal(0)); QCOMPARE(edit->clipRect().x(), qreal(0));
@ -2797,6 +2801,7 @@ void tst_qquicktextedit::boundingRect()
cursorComponent.setData("import QtQuick 2.0\nRectangle { height: 20; width: 8 }", QUrl()); cursorComponent.setData("import QtQuick 2.0\nRectangle { height: 20; width: 8 }", QUrl());
edit->setCursorDelegate(&cursorComponent); edit->setCursorDelegate(&cursorComponent);
edit->setCursorVisible(true);
// Don't include the size of a cursor delegate as it has its own bounding rect. // Don't include the size of a cursor delegate as it has its own bounding rect.
QCOMPARE(edit->boundingRect().x(), qreal(0)); QCOMPARE(edit->boundingRect().x(), qreal(0));

View File

@ -1509,6 +1509,7 @@ void tst_qquicktextinput::clipRect()
cursorComponent.setData("import QtQuick 2.0\nRectangle { height: 20; width: 8 }", QUrl()); cursorComponent.setData("import QtQuick 2.0\nRectangle { height: 20; width: 8 }", QUrl());
input->setCursorDelegate(&cursorComponent); input->setCursorDelegate(&cursorComponent);
input->setCursorVisible(true);
// If a cursor delegate is used it's size should determine the excess width. // If a cursor delegate is used it's size should determine the excess width.
QCOMPARE(input->clipRect().x(), qreal(0)); QCOMPARE(input->clipRect().x(), qreal(0));
@ -1596,6 +1597,7 @@ void tst_qquicktextinput::boundingRect()
cursorComponent.setData("import QtQuick 2.0\nRectangle { height: 20; width: 8 }", QUrl()); cursorComponent.setData("import QtQuick 2.0\nRectangle { height: 20; width: 8 }", QUrl());
input->setCursorDelegate(&cursorComponent); input->setCursorDelegate(&cursorComponent);
input->setCursorVisible(true);
// Don't include the size of a cursor delegate as it has its own bounding rect. // Don't include the size of a cursor delegate as it has its own bounding rect.
QCOMPARE(input->boundingRect().x(), input->width() - line.naturalTextWidth()); QCOMPARE(input->boundingRect().x(), input->width() - line.naturalTextWidth());
@ -2481,9 +2483,13 @@ void tst_qquicktextinput::cursorDelegate()
view.requestActivateWindow(); view.requestActivateWindow();
QQuickTextInput *textInputObject = view.rootObject()->findChild<QQuickTextInput*>("textInputObject"); QQuickTextInput *textInputObject = view.rootObject()->findChild<QQuickTextInput*>("textInputObject");
QVERIFY(textInputObject != 0); QVERIFY(textInputObject != 0);
QVERIFY(textInputObject->findChild<QQuickItem*>("cursorInstance")); // Delegate is created on demand, and so won't be available immediately. Focus in or
// setCursorVisible(true) will trigger creation.
QTRY_VERIFY(!textInputObject->findChild<QQuickItem*>("cursorInstance"));
QVERIFY(!textInputObject->isCursorVisible());
//Test Delegate gets created //Test Delegate gets created
textInputObject->setFocus(true); textInputObject->setFocus(true);
QVERIFY(textInputObject->isCursorVisible());
QQuickItem* delegateObject = textInputObject->findChild<QQuickItem*>("cursorInstance"); QQuickItem* delegateObject = textInputObject->findChild<QQuickItem*>("cursorInstance");
QVERIFY(delegateObject); QVERIFY(delegateObject);
QCOMPARE(delegateObject->property("localProperty").toString(), QString("Hello")); QCOMPARE(delegateObject->property("localProperty").toString(), QString("Hello"));
@ -2497,7 +2503,6 @@ void tst_qquicktextinput::cursorDelegate()
QCOMPARE(textInputObject->cursorRectangle().x(), delegateObject->x()); QCOMPARE(textInputObject->cursorRectangle().x(), delegateObject->x());
QCOMPARE(textInputObject->cursorRectangle().y(), delegateObject->y()); QCOMPARE(textInputObject->cursorRectangle().y(), delegateObject->y());
// Test delegate gets moved on mouse press. // Test delegate gets moved on mouse press.
textInputObject->setSelectByMouse(true); textInputObject->setSelectByMouse(true);
textInputObject->setCursorPosition(0); textInputObject->setCursorPosition(0);