aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick/items/qquicktextcontrol.cpp
diff options
context:
space:
mode:
authorAndrew den Exter <andrew.den-exter@nokia.com>2012-07-05 10:18:43 +1000
committerQt by Nokia <qt-info@nokia.com>2012-07-09 09:05:32 +0200
commit4598939ee2fad1238609ca43717199fd4e98c75f (patch)
tree9ffdcf451ad585f14b1eda8050e48d3298dec7f2 /src/quick/items/qquicktextcontrol.cpp
parent19c0a31319148d4ac716f7cb3295891b5a3b20d9 (diff)
Speed up TextEdit construction time.
Defer setting content on the QTextDocument until componentComplete() to avoid unnecessary layouts and move one time setup for the text document from setContent to the constructor. Reduces the construction time of a TextEdit with RichText textFormat by about a third. Change-Id: Idde0772063bf769cde984efddd68589c55a7431a Reviewed-by: Martin Jones <martin.jones@nokia.com>
Diffstat (limited to 'src/quick/items/qquicktextcontrol.cpp')
-rw-r--r--src/quick/items/qquicktextcontrol.cpp77
1 files changed, 26 insertions, 51 deletions
diff --git a/src/quick/items/qquicktextcontrol.cpp b/src/quick/items/qquicktextcontrol.cpp
index 23bf2a779e..d2ff4dc649 100644
--- a/src/quick/items/qquicktextcontrol.cpp
+++ b/src/quick/items/qquicktextcontrol.cpp
@@ -71,6 +71,8 @@
#include <qstylehints.h>
#include <qmetaobject.h>
+#include <private/qqmlglobal_p.h>
+
// ### these should come from QStyleHints
const int textCursorWidth = 1;
const bool fullWidthSelection = true;
@@ -279,16 +281,7 @@ void QQuickTextControlPrivate::updateCurrentCharFormat()
cursorRectangleChanged = true;
}
-void QQuickTextControlPrivate::init(Qt::TextFormat format, const QString &text, QTextDocument *document)
-{
- Q_Q(QQuickTextControl);
- setContent(format, text, document);
-
- doc->setUndoRedoEnabled(interactionFlags & Qt::TextEditable);
- q->setCursorWidth(-1);
-}
-
-void QQuickTextControlPrivate::setContent(Qt::TextFormat format, const QString &text, QTextDocument *document)
+void QQuickTextControlPrivate::setContent(Qt::TextFormat format, const QString &text)
{
Q_Q(QQuickTextControl);
@@ -298,33 +291,11 @@ void QQuickTextControlPrivate::setContent(Qt::TextFormat format, const QString &
// set char format then.
const QTextCharFormat charFormatForInsertion = cursor.charFormat();
- bool clearDocument = true;
- if (!doc) {
- if (document) {
- doc = document;
- clearDocument = false;
- } else {
- doc = new QTextDocument(q);
- }
- _q_documentLayoutChanged();
- cursor = QTextCursor(doc);
-
-// #### doc->documentLayout()->setPaintDevice(viewport);
-
- QObject::connect(doc, SIGNAL(contentsChanged()), q, SLOT(_q_updateCurrentCharFormatAndSelection()));
- QObject::connect(doc, SIGNAL(cursorPositionChanged(QTextCursor)), q, SLOT(_q_emitCursorPosChanged(QTextCursor)));
- QObject::connect(doc, SIGNAL(documentLayoutChanged()), q, SLOT(_q_documentLayoutChanged()));
- }
-
bool previousUndoRedoState = doc->isUndoRedoEnabled();
- if (!document)
- doc->setUndoRedoEnabled(false);
+ doc->setUndoRedoEnabled(false);
- //Saving the index save some time.
- static int contentsChangedIndex = QMetaMethod::fromSignal(&QTextDocument::contentsChanged).methodIndex();
- static int textChangedIndex = QMetaMethod::fromSignal(&QQuickTextControl::textChanged).methodIndex();
// avoid multiple textChanged() signals being emitted
- QMetaObject::disconnect(doc, contentsChangedIndex, q, textChangedIndex);
+ qmlobject_disconnect(doc, QTextDocument, SIGNAL(contentsChanged()), q, QQuickTextControl, SIGNAL(textChanged()));
if (!text.isEmpty()) {
// clear 'our' cursor for insertion to prevent
@@ -354,18 +325,16 @@ void QQuickTextControlPrivate::setContent(Qt::TextFormat format, const QString &
doc->setUndoRedoEnabled(false);
}
cursor = QTextCursor(doc);
- } else if (clearDocument) {
+ } else {
doc->clear();
}
cursor.setCharFormat(charFormatForInsertion);
- QMetaObject::connect(doc, contentsChangedIndex, q, textChangedIndex);
+ qmlobject_connect(doc, QTextDocument, SIGNAL(contentsChanged()), q, QQuickTextControl, SIGNAL(textChanged()));
emit q->textChanged();
- if (!document)
- doc->setUndoRedoEnabled(previousUndoRedoState);
+ doc->setUndoRedoEnabled(previousUndoRedoState);
_q_updateCurrentCharFormatAndSelection();
- if (!document)
- doc->setModified(false);
+ doc->setModified(false);
q->updateCursorRectangle(true);
emit q->cursorPositionChanged();
@@ -474,16 +443,6 @@ void QQuickTextControlPrivate::_q_emitCursorPosChanged(const QTextCursor &someCu
}
}
-void QQuickTextControlPrivate::_q_documentLayoutChanged()
-{
- Q_Q(QQuickTextControl);
- QAbstractTextDocumentLayout *layout = doc->documentLayout();
- QObject::connect(layout, SIGNAL(update(QRectF)), q, SIGNAL(updateRequest()));
- QObject::connect(layout, SIGNAL(updateBlock(QTextBlock)), q, SIGNAL(updateRequest()));
- QObject::connect(layout, SIGNAL(documentSizeChanged(QSizeF)), q, SIGNAL(documentSizeChanged(QSizeF)));
-
-}
-
void QQuickTextControlPrivate::setBlinkingCursorEnabled(bool enable)
{
Q_Q(QQuickTextControl);
@@ -611,7 +570,23 @@ QQuickTextControl::QQuickTextControl(QTextDocument *doc, QObject *parent)
: QObject(*new QQuickTextControlPrivate, parent)
{
Q_D(QQuickTextControl);
- d->init(Qt::PlainText, QString(), doc);
+ Q_ASSERT(doc);
+
+ QAbstractTextDocumentLayout *layout = doc->documentLayout();
+ qmlobject_connect(layout, QAbstractTextDocumentLayout, SIGNAL(update(QRectF)), this, QQuickTextControl, SIGNAL(updateRequest()));
+ qmlobject_connect(layout, QAbstractTextDocumentLayout, SIGNAL(updateBlock(QTextBlock)), this, QQuickTextControl, SIGNAL(updateRequest()));
+ qmlobject_connect(doc, QTextDocument, SIGNAL(contentsChanged()), this, QQuickTextControl, SIGNAL(textChanged()));
+ qmlobject_connect(doc, QTextDocument, SIGNAL(contentsChanged()), this, QQuickTextControl, SLOT(_q_updateCurrentCharFormatAndSelection()));
+ qmlobject_connect(doc, QTextDocument, SIGNAL(cursorPositionChanged(QTextCursor)), this, QQuickTextControl, SLOT(_q_emitCursorPosChanged(QTextCursor)));
+
+ layout->setProperty("cursorWidth", textCursorWidth);
+
+ d->doc = doc;
+ d->cursor = QTextCursor(doc);
+ d->lastCharFormat = d->cursor.charFormat();
+ doc->setPageSize(QSizeF(0, 0));
+ doc->setModified(false);
+ doc->setUndoRedoEnabled(true);
}
QQuickTextControl::~QQuickTextControl()