#include <qstylehints.h>
#include <qmetaobject.h>
+#include <private/qqmlglobal_p.h>
+
// ### these should come from QStyleHints
const int textCursorWidth = 1;
const bool fullWidthSelection = true;
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);
// 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
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();
}
}
-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);
: 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()
d->document->clearResources();
d->richText = d->format == RichText || (d->format == AutoText && Qt::mightBeRichText(text));
- if (d->richText) {
+ if (!isComponentComplete()) {
+ d->text = text;
+ } else if (d->richText) {
#ifndef QT_NO_TEXTHTMLPARSER
d->control->setHtml(text);
#else
d->richText = format == RichText || (format == AutoText && (wasRich || Qt::mightBeRichText(text())));
#ifndef QT_NO_TEXTHTMLPARSER
- if (wasRich && !d->richText) {
- d->control->setPlainText(!d->textCached ? d->control->toHtml() : d->text);
- updateSize();
- } else if (!wasRich && d->richText) {
- d->control->setHtml(!d->textCached ? d->control->toPlainText() : d->text);
- updateSize();
+ if (isComponentComplete()) {
+ if (wasRich && !d->richText) {
+ d->control->setPlainText(!d->textCached ? d->control->toHtml() : d->text);
+ updateSize();
+ } else if (!wasRich && d->richText) {
+ d->control->setHtml(!d->textCached ? d->control->toPlainText() : d->text);
+ updateSize();
+ }
}
#endif
QQuickImplicitSizeItem::componentComplete();
d->document->setBaseUrl(baseUrl(), d->richText);
+#ifndef QT_NO_TEXTHTML_PARSER
+ if (d->richText)
+ d->control->setHtml(d->text);
+ else
+#endif
+ if (!d->text.isEmpty())
+ d->control->setPlainText(d->text);
+
if (d->dirty) {
d->determineHorizontalAlignment();
d->updateDefaultTextOption();