From bda6a5e7a8a233dc4b4395aa20b41f74f2c0857a Mon Sep 17 00:00:00 2001 From: Eskil Abrahamsen Blomfeldt Date: Mon, 24 Aug 2009 15:55:51 +0200 Subject: Don't float objects into next page unless they can fit in it If you had a floating table which spanned over several pages, this would always float down to the next page, even when it was the only element in the document. The main reason why this mechanism was added to positionFloat() was to handle floating images, since they can't be broken down into more than one page. Tables, however, have logic to ensure they are broken into several pages if they are too large for the current page. There is however no reason to float an object into the next page if it is larger than that page. Since the size of tables are altered by their position as long as they span more than one page, we need to set the sizeDirty flag for floating tables when positioning and then relayout the frame to make sure the page breaks are set correctly. Task-number: 240086 Reviewed-by: Simon Hausmann --- src/gui/text/qtextdocumentlayout.cpp | 14 +++++++++++++- .../qtextdocumentlayout/tst_qtextdocumentlayout.cpp | 21 +++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/gui/text/qtextdocumentlayout.cpp b/src/gui/text/qtextdocumentlayout.cpp index a795c1f..7b36b9d 100644 --- a/src/gui/text/qtextdocumentlayout.cpp +++ b/src/gui/text/qtextdocumentlayout.cpp @@ -1994,9 +1994,12 @@ void QTextDocumentLayoutPrivate::positionFloat(QTextFrame *frame, QTextLine *cur } } - if (y + layoutStruct->frameY + fd->size.height > layoutStruct->pageBottom) { + bool frameSpansIntoNextPage = (y + layoutStruct->frameY + fd->size.height > layoutStruct->pageBottom); + if (frameSpansIntoNextPage && fd->size.height <= layoutStruct->pageHeight) { layoutStruct->newPage(); y = layoutStruct->y; + + frameSpansIntoNextPage = false; } y = findY(y, layoutStruct, fd->size.width); @@ -2017,6 +2020,11 @@ void QTextDocumentLayoutPrivate::positionFloat(QTextFrame *frame, QTextLine *cur // qDebug()<< "float positioned at " << fd->position.x << fd->position.y; fd->layoutDirty = false; + + // If the frame is a table, then positioning it will affect the size if it covers more than + // one page, because of page breaks and repeating the header. + if (qobject_cast(frame) != 0) + fd->sizeDirty = frameSpansIntoNextPage; } QRectF QTextDocumentLayoutPrivate::layoutFrame(QTextFrame *f, int layoutFrom, int layoutTo, QFixed parentY) @@ -2349,6 +2357,10 @@ void QTextDocumentLayoutPrivate::layoutFlow(QTextFrame::Iterator it, QLayoutStru positionFloat(c); + // If the size was made dirty when the position was set, layout again + if (cd->sizeDirty) + updateRect = layoutFrame(c, layoutFrom, layoutTo); + QRectF frameRect(cd->position.toPointF(), cd->size.toSizeF()); if (frameRect == oldFrameRect && updateRect.isValid()) diff --git a/tests/auto/qtextdocumentlayout/tst_qtextdocumentlayout.cpp b/tests/auto/qtextdocumentlayout/tst_qtextdocumentlayout.cpp index d8c4f51..dbba9ef 100644 --- a/tests/auto/qtextdocumentlayout/tst_qtextdocumentlayout.cpp +++ b/tests/auto/qtextdocumentlayout/tst_qtextdocumentlayout.cpp @@ -71,6 +71,7 @@ private slots: void wrapAtWordBoundaryOrAnywhere(); void inlineImage(); void clippedTableCell(); + void floatingTablePageBreak(); private: QTextDocument *doc; @@ -250,5 +251,25 @@ void tst_QTextDocumentLayout::clippedTableCell() QCOMPARE(img, expected); } +void tst_QTextDocumentLayout::floatingTablePageBreak() +{ + doc->clear(); + + QTextCursor cursor(doc); + + QTextTableFormat tableFormat; + tableFormat.setPosition(QTextFrameFormat::FloatLeft); + QTextTable *table = cursor.insertTable(50, 1, tableFormat); + + // Make height of document 2/3 of the table, fitting the table into two pages + QSizeF documentSize = doc->size(); + documentSize.rheight() *= 2.0 / 3.0; + + doc->setPageSize(documentSize); + + QCOMPARE(doc->pageCount(), 2); +} + + QTEST_MAIN(tst_QTextDocumentLayout) #include "tst_qtextdocumentlayout.moc" -- cgit v0.12