29#include "moc_qgslayoutpagecollection.cpp"
31using namespace Qt::StringLiterals;
38 createDefaultPageStyleSymbol();
43 const auto constMPages = mPages;
46 mLayout->removeItem(
page );
60 page->setPageStyleSymbol( symbol->
clone() );
67 return mPageStyleSymbol.get();
72 mPreviousItemPositions.clear();
73 QList< QgsLayoutItem * > items;
74 mLayout->layoutItems( items );
81 mPreviousItemPositions.insert( item->uuid(), qMakePair( item->page(), item->pagePositionWithUnits() ) );
87 for (
auto it = mPreviousItemPositions.constBegin(); it != mPreviousItemPositions.constEnd(); ++it )
89 const QString key { it.key() };
92 if ( !mBlockUndoCommands )
93 item->beginCommand( QString() );
95 item->attemptMove( it.value().second,
true,
false, it.value().first );
98 if ( mLayout->itemByUuid( key ) )
100 if ( !mBlockUndoCommands )
105 item->cancelCommand();
109 mPreviousItemPositions.clear();
116 const auto constMPages = mPages;
119 page->attemptMove( p );
123 mLayout->guides().update();
124 mLayout->updateBounds();
133 maxWidth = std::max( maxWidth, mLayout->convertToLayoutUnits(
page->pageSize() ).width() );
144 QSizeF pageSize = mLayout->convertToLayoutUnits(
page->pageSize() );
145 double area = pageSize.width() * pageSize.height();
146 if ( area > maxArea )
160 QSizeF pageSize = mLayout->convertToLayoutUnits(
page->pageSize() );
161 if ( !size.isValid() )
175 double startNextPageY = 0;
176 const auto constMPages = mPages;
180 if ( startNextPageY > point.y() )
192 if ( mPages.empty() )
196 double startNextPageY = 0;
197 const auto constMPages = mPages;
201 if ( startNextPageY >= point.y() )
206 if ( startNextPageY >= point.y() )
212 double lastPageHeight = mPages.last()->rect().height();
213 while ( startNextPageY < point.y() )
216 if ( startNextPageY >= point.y() )
226 const QList< QGraphicsItem * > items = mLayout->items( point );
227 for ( QGraphicsItem *item : items )
232 if (
page->mapToScene(
page->rect() ).boundingRect().contains( point ) )
241 QPointF layoutUnitsPos = mLayout->convertToLayoutUnits( position );
242 if (
page > 0 &&
page < mPages.count() )
244 layoutUnitsPos.ry() += mPages.at(
page )->pos().y();
246 return layoutUnitsPos;
252 if (
page > 0 &&
page < mPages.count() )
254 vDelta = mLayout->convertFromLayoutUnits( mPages.at(
page )->pos().y(), position.
units() ).length();
262 double startCurrentPageY = 0;
263 double startNextPageY = 0;
265 const auto constMPages = mPages;
268 startCurrentPageY = startNextPageY;
270 if ( startNextPageY > position.y() )
285 y = position.y() - startCurrentPageY;
287 return QPointF( position.x(), y );
303 QRectF bounds = mLayout->layoutBounds(
true, 0.0 );
304 if ( bounds.isEmpty() )
307 if ( !mBlockUndoCommands )
308 mLayout->undoStack()->beginCommand(
this, tr(
"Resize to Contents" ) );
315 if ( mPages.empty() )
317 auto page = std::make_unique< QgsLayoutItemPage >( mLayout );
328 bounds.setWidth( bounds.width() + marginLeft + marginRight );
329 bounds.setHeight( bounds.height() + marginTop + marginBottom );
331 QgsLayoutSize newPageSize = mLayout->convertFromLayoutUnits( bounds.size(), mLayout->units() );
332 page->setPageSize( newPageSize );
337 double diffX = marginLeft - bounds.left();
338 double diffY = marginTop - bounds.top();
340 const QList<QGraphicsItem *> itemList = mLayout->items();
341 for ( QGraphicsItem *item : itemList )
348 layoutItem->beginCommand( tr(
"Move Item" ) );
349 layoutItem->attemptMoveBy( diffX, diffY );
350 layoutItem->endCommand();
356 mLayout->undoStack()->beginCommand( &mLayout->guides(), tr(
"Move Guides" ) );
357 const QList< QgsLayoutGuide * > verticalGuides = mLayout->guides().guides( Qt::Vertical );
360 guide->setLayoutPosition( guide->layoutPosition() + diffX );
362 const QList< QgsLayoutGuide * > horizontalGuides = mLayout->guides().guides( Qt::Horizontal );
365 guide->setLayoutPosition( guide->layoutPosition() + diffY );
367 mLayout->undoStack()->endCommand();
369 if ( !mBlockUndoCommands )
370 mLayout->undoStack()->endCommand();
375 QDomElement element = document.createElement( u
"PageCollection"_s );
378 element.appendChild( pageStyleElem );
382 page->writeXml( element, document, context );
385 mGuideCollection->writeXml( element, document, context );
387 parentElement.appendChild( element );
393 QDomElement element = e;
394 if ( element.nodeName() !=
"PageCollection"_L1 )
396 element = element.firstChildElement( u
"PageCollection"_s );
399 if ( element.nodeName() !=
"PageCollection"_L1 )
404 mBlockUndoCommands =
true;
410 mLayout->removeItem(
page );
416 QDomElement pageStyleSymbolElem = element.firstChildElement( u
"symbol"_s );
417 if ( !pageStyleSymbolElem.isNull() )
422 QDomNodeList pageList = element.elementsByTagName( u
"LayoutItem"_s );
423 for (
int i = 0; i < pageList.size(); ++i )
425 QDomElement pageElement = pageList.at( i ).toElement();
426 auto page = std::make_unique<QgsLayoutItemPage>( mLayout );
427 if ( mPageStyleSymbol )
428 page->setPageStyleSymbol( mPageStyleSymbol->clone() );
429 page->readXml( pageElement, document, context );
430 page->finalizeRestoreFromXml();
431 mPages.append(
page.get() );
432 mLayout->addItem(
page.release() );
437 mGuideCollection->readXml( element, document, context );
439 mBlockUndoCommands =
false;
445 return *mGuideCollection;
450 return *mGuideCollection;
456 if ( !referencePage )
461 mLayout->undoStack()->beginCommand(
this, tr(
"Apply page properties" ) );
462 mBlockUndoCommands =
true;
466 if (
page == referencePage )
474 mLayout->undoStack()->endCommand();
475 mBlockUndoCommands =
false;
481 const auto constMPages = mPages;
500 return mPages.count();
515 return mPages.indexOf(
page );
520 QList<QgsLayoutItemPage *>
pages;
521 const auto constMPages = mPages;
524 if (
page->mapToScene(
page->rect() ).boundingRect().intersects( region ) )
534 const auto constMPages = mPages;
537 if (
page->mapToScene(
page->rect() ).boundingRect().intersects( region ) )
547 const QList<QgsLayoutItem *> items = mLayout->pageCollection()->itemsOnPage(
page );
565 QList<QgsLayoutItem *> itemList;
566 const QList<QGraphicsItem *> graphicsItemList = mLayout->items();
567 itemList.reserve( graphicsItemList.size() );
568 for ( QGraphicsItem *graphicsItem : graphicsItemList )
573 itemList.push_back( item );
581 if (
page >= mPages.count() ||
page < 0 )
592 QList<QgsLayoutFrame *> frames;
596 if ( frame->hidePageIfEmpty() && frame->isEmpty() )
607 if ( !mBlockUndoCommands )
608 mLayout->undoStack()->beginCommand(
this, tr(
"Add Page" ) );
609 mPages.append(
page );
610 mLayout->addItem(
page );
612 if ( !mBlockUndoCommands )
613 mLayout->undoStack()->endCommand();
618 if ( mPages.empty() )
622 auto newPage = std::make_unique< QgsLayoutItemPage >( mLayout );
625 return mPages.at( mPages.count() - 1 );
630 if ( !mBlockUndoCommands )
632 mLayout->undoStack()->beginMacro( tr(
"Add Page" ) );
633 mLayout->undoStack()->beginCommand(
this, tr(
"Add Page" ) );
636 if ( beforePage < 0 )
640 if ( beforePage >= mPages.count() )
642 mPages.append(
page );
646 mPages.insert( beforePage,
page );
648 mLayout->addItem(
page );
652 for (
auto it = mPreviousItemPositions.begin(); it != mPreviousItemPositions.end(); ++it )
654 if ( it.value().first < beforePage )
657 it.value().first = it.value().first + 1;
661 if ( !mBlockUndoCommands )
663 mLayout->undoStack()->endCommand();
664 mLayout->undoStack()->endMacro();
673 if ( !mBlockUndoCommands )
675 mLayout->undoStack()->beginMacro( tr(
"Remove Page" ) );
676 mLayout->undoStack()->beginCommand(
this, tr(
"Remove Page" ) );
681 mLayout->removeItem(
page );
686 for (
auto it = mPreviousItemPositions.begin(); it != mPreviousItemPositions.end(); ++it )
691 it.value().first = it.value().first - 1;
695 if ( !mBlockUndoCommands )
697 mLayout->undoStack()->endCommand();
698 mLayout->undoStack()->endMacro();
704 if ( !mPages.contains(
page ) )
707 if ( !mBlockUndoCommands )
709 mLayout->undoStack()->beginMacro( tr(
"Remove Page" ) );
710 mLayout->undoStack()->beginCommand(
this, tr(
"Remove Page" ) );
712 int pageIndex = mPages.indexOf(
page );
715 mPages.removeAll(
page );
719 mLayout->removeItem(
page );
723 for (
auto it = mPreviousItemPositions.begin(); it != mPreviousItemPositions.end(); ++it )
725 if ( it.value().first <= pageIndex )
728 it.value().first = it.value().first - 1;
732 if ( !mBlockUndoCommands )
734 mLayout->undoStack()->endCommand();
735 mLayout->undoStack()->endMacro();
741 if ( !mBlockUndoCommands )
743 mLayout->undoStack()->beginMacro( tr(
"Remove Pages" ) );
744 mLayout->undoStack()->beginCommand(
this, tr(
"Remove Pages" ) );
746 for (
int i = mPages.count() - 1; i >= 0; --i )
749 mPages.takeAt( i )->deleteLater();
752 if ( !mBlockUndoCommands )
754 mLayout->undoStack()->endCommand();
755 mLayout->undoStack()->endMacro();
761 mPages.removeAll(
page );
765void QgsLayoutPageCollection::createDefaultPageStyleSymbol()
767 QVariantMap properties;
768 properties.insert( u
"color"_s, u
"white"_s );
769 properties.insert( u
"style"_s, u
"solid"_s );
770 properties.insert( u
"style_border"_s, u
"no"_s );
771 properties.insert( u
"joinstyle"_s, u
"miter"_s );
LayoutUnit
Layout measurement units.
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
static std::unique_ptr< QgsFillSymbol > createSimple(const QVariantMap &properties)
Create a fill symbol with one symbol layer: SimpleFill with specified properties.
QgsFillSymbol * clone() const override
Returns a deep copy of this symbol.
Base class for frame items, which form a layout multiframe item.
Stores and manages the snap guides used by a layout.
Contains the configuration for a single snap guide used by a layout.
Item representing the paper in a layout.
QgsLayoutSize pageSize() const
Returns the size of the page.
const QgsFillSymbol * pageStyleSymbol() const
Returns the symbol to use for drawing the page background.
Base class for graphical items within a QgsLayout.
QgsLayoutSize sizeWithUnits() const
Returns the item's current size, including units.
int page() const
Returns the page the item is currently on, with the first page returning 0.
bool shouldDrawItem() const
Returns whether the item should be drawn in the current context.
Provides a method of storing measurements for use in QGIS layouts using a variety of different measur...
QList< int > visiblePageNumbers(const QRectF ®ion) const
Returns a list of the page numbers which are visible within the specified region (in layout coordinat...
void deletePage(int pageNumber)
Deletes a page from the collection.
bool pageIsEmpty(int page) const
Returns whether a given page index is empty, ie, it contains no items except for the background paper...
bool readXml(const QDomElement &collectionElement, const QDomDocument &document, const QgsReadWriteContext &context) override
Sets the collection's state from a DOM element.
QPointF pagePositionToLayoutPosition(int page, const QgsLayoutPoint &position) const
Converts a position on a page to an absolute position in layout coordinates.
void addPage(QgsLayoutItemPage *page)
Adds a page to the collection.
void changed()
Emitted when pages are added or removed from the collection.
int pageNumberForPoint(QPointF point) const
Returns the page number corresponding to a point in the layout (in layout units).
QgsLayoutItemPage * takePage(QgsLayoutItemPage *page)
Takes a page from the collection, returning ownership of the page to the caller.
QList< QgsLayoutItemPage * > visiblePages(const QRectF ®ion) const
Returns a list of the pages which are visible within the specified region (in layout coordinates).
Q_DECL_DEPRECATED const QgsFillSymbol * pageStyleSymbol() const
Returns the symbol to use for drawing pages in the collection.
QgsLayoutPageCollection(QgsLayout *layout)
Constructor for QgsLayoutItemPage, with the specified parent layout.
void insertPage(QgsLayoutItemPage *page, int beforePage)
Inserts a page into a specific position in the collection.
void setPageStyleSymbol(QgsFillSymbol *symbol)
Sets the symbol to use for drawing pages in the collection.
void reflow()
Forces the page collection to reflow the arrangement of pages, e.g.
QgsLayoutItemPage * extendByNewPage()
Adds a new page to the end of the collection.
bool writeXml(QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context) const override
Stores the collection's state in a DOM element.
QgsLayout * layout() override
Returns the layout the object belongs to.
double spaceBetweenPages() const
Returns the space between pages, in layout units.
QgsLayoutGuideCollection & guides()
Returns a reference to the collection's guide collection, which manages page snap guides.
int predictPageNumberForPoint(QPointF point) const
Returns the theoretical page number corresponding to a point in the layout (in layout units),...
void endPageSizeChange()
Should be called after changing any page item sizes, and preceded by a call to beginPageSizeChange().
QList< QgsLayoutItem * > itemsOnPage(int page) const
Returns a list of layout items on the specified page index.
QList< QgsLayoutItemPage * > pages()
Returns a list of pages in the collection.
void pageAboutToBeRemoved(int pageNumber)
Emitted just before a page is removed from the collection.
int pageCount() const
Returns the number of pages in the collection.
~QgsLayoutPageCollection() override
void applyPropertiesToAllOtherPages(int sourcePage)
Apply the source page properties (size & background color) to all other pages.
double maximumPageWidth() const
Returns the maximum width of pages in the collection.
QPointF positionOnPage(QPointF point) const
Returns the position within a page of a point in the layout (in layout units).
void resizeToContents(const QgsMargins &margins, Qgis::LayoutUnit marginUnits)
Resizes the layout to a single page which fits the current contents of the layout.
bool shouldExportPage(int page) const
Returns whether the specified page number should be included in exports of the layouts.
QgsLayoutItemPage * page(int pageNumber)
Returns a specific page (by pageNumber) from the collection.
void redraw()
Triggers a redraw for all pages.
QgsLayoutPoint pagePositionToAbsolute(int page, const QgsLayoutPoint &position) const
Converts a position on a page to an absolute position in (maintaining the units from the input positi...
double pageShadowWidth() const
Returns the size of the page shadow, in layout units.
void clear()
Removes all pages from the collection.
int pageNumber(QgsLayoutItemPage *page) const
Returns the page number for the specified page, or -1 if the page is not contained in the collection.
bool hasUniformPageSizes() const
Returns true if the layout has uniform page sizes, e.g.
void beginPageSizeChange()
Should be called before changing any page item sizes, and followed by a call to endPageSizeChange().
QgsLayoutItemPage * pageAtPoint(QPointF point) const
Returns the page at a specified point (in layout coordinates).
QSizeF maximumPageSize() const
Returns the maximum size of any page in the collection, by area.
Provides a method of storing points, consisting of an x and y coordinate, for use in QGIS layouts.
double x() const
Returns x coordinate of point.
double y() const
Returns y coordinate of point.
Qgis::LayoutUnit units() const
Returns the units for the point.
void setY(const double y)
Sets y coordinate of point.
Provides a method of storing sizes, consisting of a width and height, for use in QGIS layouts.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Defines the four margins of a rectangle.
double top() const
Returns the top margin.
double right() const
Returns the right margin.
double bottom() const
Returns the bottom margin.
double left() const
Returns the left margin.
A container for the context for various read/write operations on objects.
static std::unique_ptr< QgsSymbol > loadSymbol(const QDomElement &element, const QgsReadWriteContext &context)
Attempts to load a symbol from a DOM element.
static QDomElement saveSymbol(const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).