summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDavid Faure <david.faure@kdab.com>2022-05-25 22:07:36 +0200
committerDavid Faure <david.faure@kdab.com>2022-05-26 16:58:42 +0200
commit09c7457f4ab8021e7637e60e6c3d1f644639aec1 (patch)
treea616cc26d117d741d593542d7dc41a67a79de96f /src
parent2996ca031efcd97ef9866186ae53e96ed75c9fbf (diff)
QTextDocument: add setLayoutEnabled()
This allows to set up everything first - without paying for the layout calculation at every step - and only then trigger the layout once. Results: 0.065 msecs to create a QGraphicsTextItem with some text (layouted) 0.036 msecs to set everything up in a QGraphicsTextItem with 0 width Change-Id: I138bd1d58941d029bc0a36d2730216778f1fbd97 Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/gui/text/qtextdocument.cpp34
-rw-r--r--src/gui/text/qtextdocument.h4
-rw-r--r--src/gui/text/qtextdocument_p.h3
-rw-r--r--src/gui/text/qtextdocumentlayout.cpp8
4 files changed, 45 insertions, 4 deletions
diff --git a/src/gui/text/qtextdocument.cpp b/src/gui/text/qtextdocument.cpp
index c01c858cc5..71511257f5 100644
--- a/src/gui/text/qtextdocument.cpp
+++ b/src/gui/text/qtextdocument.cpp
@@ -635,6 +635,40 @@ bool QTextDocument::useDesignMetrics() const
}
/*!
+ \property QTextDocument::layoutEnabled
+ \since 6.4
+ \brief whether QTextDocument should recalculate the layout after every change
+
+ If this property is set to true, any change to the document triggers a layout,
+ which makes everything work as expected but takes time.
+
+ Temporarily disabling the layout can save time when making multiple changes
+ (not just text content, but also default font, default text option....)
+ so that the document is only laid out once at the end. This can be useful when
+ the text width or page size isn't yet known, for instance.
+
+ By default, this property is \c true.
+
+ \sa setTextWidth
+*/
+
+void QTextDocument::setLayoutEnabled(bool b)
+{
+ Q_D(QTextDocument);
+ if (d->layoutEnabled == b)
+ return;
+ d->layoutEnabled = b;
+ if (b && d->lout)
+ d->lout->documentChanged(0, 0, d->length());
+}
+
+bool QTextDocument::isLayoutEnabled() const
+{
+ Q_D(const QTextDocument);
+ return d->layoutEnabled;
+}
+
+/*!
\since 4.2
Draws the content of the document with painter \a p, clipped to \a rect.
diff --git a/src/gui/text/qtextdocument.h b/src/gui/text/qtextdocument.h
index ba33e410d1..01ed9789ed 100644
--- a/src/gui/text/qtextdocument.h
+++ b/src/gui/text/qtextdocument.h
@@ -62,6 +62,7 @@ class Q_GUI_EXPORT QTextDocument : public QObject
Q_PROPERTY(QSizeF pageSize READ pageSize WRITE setPageSize)
Q_PROPERTY(QFont defaultFont READ defaultFont WRITE setDefaultFont)
Q_PROPERTY(bool useDesignMetrics READ useDesignMetrics WRITE setUseDesignMetrics)
+ Q_PROPERTY(bool layoutEnabled READ isLayoutEnabled WRITE setLayoutEnabled)
Q_PROPERTY(QSizeF size READ size)
Q_PROPERTY(qreal textWidth READ textWidth WRITE setTextWidth)
Q_PROPERTY(int blockCount READ blockCount)
@@ -218,6 +219,9 @@ public:
void setUseDesignMetrics(bool b);
bool useDesignMetrics() const;
+ void setLayoutEnabled(bool b);
+ bool isLayoutEnabled() const;
+
void drawContents(QPainter *painter, const QRectF &rect = QRectF());
void setTextWidth(qreal width);
diff --git a/src/gui/text/qtextdocument_p.h b/src/gui/text/qtextdocument_p.h
index 195006b130..effcfcabed 100644
--- a/src/gui/text/qtextdocument_p.h
+++ b/src/gui/text/qtextdocument_p.h
@@ -291,6 +291,8 @@ public:
return get(object->document());
}
+ bool canLayout() const { return layoutEnabled && !pageSize.isNull(); }
+
private:
QTextDocumentPrivate(const QTextDocumentPrivate& m);
QTextDocumentPrivate& operator= (const QTextDocumentPrivate& m);
@@ -337,6 +339,7 @@ private:
public:
bool inContentsChange;
+ bool layoutEnabled = true;
QTextOption defaultTextOption;
Qt::CursorMoveStyle defaultCursorMoveStyle;
#ifndef QT_NO_CSSPARSER
diff --git a/src/gui/text/qtextdocumentlayout.cpp b/src/gui/text/qtextdocumentlayout.cpp
index 81eb0f49fb..98e4419e40 100644
--- a/src/gui/text/qtextdocumentlayout.cpp
+++ b/src/gui/text/qtextdocumentlayout.cpp
@@ -3754,7 +3754,7 @@ void QTextDocumentLayout::documentChanged(int from, int oldLength, int length)
for (; blockIt.isValid() && blockIt != endIt; blockIt = blockIt.next())
blockIt.clearLayout();
- if (d->docPrivate->pageSize.isNull())
+ if (!d->docPrivate->canLayout())
return;
QRectF updateRect;
@@ -4032,7 +4032,7 @@ QRectF QTextDocumentLayout::tableCellBoundingRect(QTextTable *table, const QText
QRectF QTextDocumentLayout::tableBoundingRect(QTextTable *table) const
{
Q_D(const QTextDocumentLayout);
- if (d->docPrivate->pageSize.isNull())
+ if (!d->docPrivate->canLayout())
return QRectF();
d->ensureLayoutFinished();
@@ -4059,7 +4059,7 @@ QRectF QTextDocumentLayout::tableBoundingRect(QTextTable *table) const
QRectF QTextDocumentLayout::frameBoundingRect(QTextFrame *frame) const
{
Q_D(const QTextDocumentLayout);
- if (d->docPrivate->pageSize.isNull())
+ if (!d->docPrivate->canLayout())
return QRectF();
d->ensureLayoutFinished();
return d->frameBoundingRectInternal(frame);
@@ -4088,7 +4088,7 @@ QRectF QTextDocumentLayoutPrivate::frameBoundingRectInternal(QTextFrame *frame)
QRectF QTextDocumentLayout::blockBoundingRect(const QTextBlock &block) const
{
Q_D(const QTextDocumentLayout);
- if (d->docPrivate->pageSize.isNull() || !block.isValid() || !block.isVisible())
+ if (!d->docPrivate->canLayout() || !block.isValid() || !block.isVisible())
return QRectF();
d->ensureLayoutedByPosition(block.position() + block.length());
QTextFrame *frame = d->document->frameAt(block.position());