31 createDefaultPageStyleSymbol();
36 const auto constMPages = mPages;
39 mLayout->removeItem(
page );
60 return mPageStyleSymbol.get();
65 mPreviousItemPositions.clear();
66 QList< QgsLayoutItem * > items;
74 mPreviousItemPositions.insert( item->uuid(), qMakePair( item->page(), item->pagePositionWithUnits() ) );
80 for (
auto it = mPreviousItemPositions.constBegin(); it != mPreviousItemPositions.constEnd(); ++it )
82 const QString key { it.key() };
85 if ( !mBlockUndoCommands )
86 item->beginCommand( QString() );
88 item->attemptMove( it.value().second,
true,
false, it.value().first );
93 if ( !mBlockUndoCommands )
98 item->cancelCommand();
102 mPreviousItemPositions.clear();
109 const auto constMPages = mPages;
138 double area = pageSize.width() * pageSize.height();
139 if ( area > maxArea )
154 if ( !size.isValid() )
159 || !
qgsDoubleNear( pageSize.height(), size.height(), 0.01 ) )
169 double startNextPageY = 0;
170 const auto constMPages = mPages;
174 if ( startNextPageY > point.y() )
186 if ( mPages.empty() )
190 double startNextPageY = 0;
191 const auto constMPages = mPages;
195 if ( startNextPageY >= point.y() )
200 if ( startNextPageY >= point.y() )
206 double lastPageHeight = mPages.last()->rect().height();
207 while ( startNextPageY < point.y() )
210 if ( startNextPageY >= point.y() )
220 const QList< QGraphicsItem * > items = mLayout->items( point );
221 for ( QGraphicsItem *item : items )
236 if (
page > 0 &&
page < mPages.count() )
238 layoutUnitsPos.ry() += mPages.at(
page )->pos().y();
240 return layoutUnitsPos;
246 if (
page > 0 &&
page < mPages.count() )
256 double startCurrentPageY = 0;
257 double startNextPageY = 0;
259 const auto constMPages = mPages;
262 startCurrentPageY = startNextPageY;
264 if ( startNextPageY > position.y() )
279 y = position.y() - startCurrentPageY;
281 return QPointF( position.x(), y );
298 if ( bounds.isEmpty() )
301 if ( !mBlockUndoCommands )
309 if ( mPages.empty() )
311 std::unique_ptr< QgsLayoutItemPage >
page = std::make_unique< QgsLayoutItemPage >( mLayout );
322 bounds.setWidth( bounds.width() + marginLeft + marginRight );
323 bounds.setHeight( bounds.height() + marginTop + marginBottom );
331 double diffX = marginLeft - bounds.left();
332 double diffY = marginTop - bounds.top();
334 const QList<QGraphicsItem *> itemList = mLayout->items();
335 for ( QGraphicsItem *item : itemList )
343 layoutItem->attemptMoveBy( diffX, diffY );
344 layoutItem->endCommand();
351 const QList< QgsLayoutGuide * > verticalGuides = mLayout->
guides().
guides( Qt::Vertical );
354 guide->setLayoutPosition( guide->layoutPosition() + diffX );
356 const QList< QgsLayoutGuide * > horizontalGuides = mLayout->
guides().
guides( Qt::Horizontal );
359 guide->setLayoutPosition( guide->layoutPosition() + diffY );
363 if ( !mBlockUndoCommands )
369 QDomElement element = document.createElement( QStringLiteral(
"PageCollection" ) );
372 element.appendChild( pageStyleElem );
379 mGuideCollection->writeXml( element, document, context );
381 parentElement.appendChild( element );
387 QDomElement element = e;
388 if ( element.nodeName() != QLatin1String(
"PageCollection" ) )
390 element = element.firstChildElement( QStringLiteral(
"PageCollection" ) );
393 if ( element.nodeName() != QLatin1String(
"PageCollection" ) )
398 mBlockUndoCommands =
true;
404 mLayout->removeItem(
page );
410 QDomElement pageStyleSymbolElem = element.firstChildElement( QStringLiteral(
"symbol" ) );
411 if ( !pageStyleSymbolElem.isNull() )
413 mPageStyleSymbol.reset( QgsSymbolLayerUtils::loadSymbol<QgsFillSymbol>( pageStyleSymbolElem, context ) );
416 QDomNodeList pageList = element.elementsByTagName( QStringLiteral(
"LayoutItem" ) );
417 for (
int i = 0; i < pageList.size(); ++i )
419 QDomElement pageElement = pageList.at( i ).toElement();
421 if ( mPageStyleSymbol )
425 mPages.append(
page.get() );
426 mLayout->addItem(
page.release() );
431 mGuideCollection->readXml( element, document, context );
433 mBlockUndoCommands =
false;
439 return *mGuideCollection;
444 return *mGuideCollection;
449 const auto constMPages = mPages;
468 return mPages.count();
483 return mPages.indexOf(
page );
488 QList<QgsLayoutItemPage *>
pages;
489 const auto constMPages = mPages;
502 const auto constMPages = mPages;
533 QList<QgsLayoutItem *> itemList;
534 const QList<QGraphicsItem *> graphicsItemList = mLayout->items();
535 itemList.reserve( graphicsItemList.size() );
536 for ( QGraphicsItem *graphicsItem : graphicsItemList )
541 itemList.push_back( item );
549 if (
page >= mPages.count() ||
page < 0 )
560 QList<QgsLayoutFrame *> frames;
564 if ( frame->hidePageIfEmpty() && frame->isEmpty() )
575 if ( !mBlockUndoCommands )
577 mPages.append(
page );
578 mLayout->addItem(
page );
580 if ( !mBlockUndoCommands )
586 if ( mPages.empty() )
590 std::unique_ptr< QgsLayoutItemPage > newPage = std::make_unique< QgsLayoutItemPage >( mLayout );
593 return mPages.at( mPages.count() - 1 );
598 if ( !mBlockUndoCommands )
604 if ( beforePage < 0 )
608 if ( beforePage >= mPages.count() )
610 mPages.append(
page );
614 mPages.insert( beforePage,
page );
616 mLayout->addItem(
page );
620 for (
auto it = mPreviousItemPositions.begin(); it != mPreviousItemPositions.end(); ++it )
622 if ( it.value().first < beforePage )
625 it.value().first = it.value().first + 1;
629 if ( ! mBlockUndoCommands )
638 if ( pageNumber < 0 || pageNumber >= mPages.count() )
641 if ( !mBlockUndoCommands )
649 mLayout->removeItem(
page );
654 for (
auto it = mPreviousItemPositions.begin(); it != mPreviousItemPositions.end(); ++it )
659 it.value().first = it.value().first - 1;
663 if ( ! mBlockUndoCommands )
672 if ( !mPages.contains(
page ) )
675 if ( !mBlockUndoCommands )
680 int pageIndex = mPages.indexOf(
page );
683 mPages.removeAll(
page );
687 mLayout->removeItem(
page );
691 for (
auto it = mPreviousItemPositions.begin(); it != mPreviousItemPositions.end(); ++it )
693 if ( it.value().first <= pageIndex )
696 it.value().first = it.value().first - 1;
700 if ( !mBlockUndoCommands )
709 if ( !mBlockUndoCommands )
714 for (
int i = mPages.count() - 1; i >= 0; --i )
717 mPages.takeAt( i )->deleteLater();
720 if ( !mBlockUndoCommands )
729 mPages.removeAll(
page );
733void QgsLayoutPageCollection::createDefaultPageStyleSymbol()
735 QVariantMap properties;
736 properties.insert( QStringLiteral(
"color" ), QStringLiteral(
"white" ) );
737 properties.insert( QStringLiteral(
"style" ), QStringLiteral(
"solid" ) );
738 properties.insert( QStringLiteral(
"style_border" ), QStringLiteral(
"no" ) );
739 properties.insert( QStringLiteral(
"joinstyle" ), QStringLiteral(
"miter" ) );
LayoutUnit
Layout measurement units.
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
static 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.
QList< QgsLayoutGuide * > guides()
Returns a list of all guides contained in the collection.
void update()
Updates the position (and visibility) of all guide line items.
Contains the configuration for a single snap guide used by a layout.
Item representing the paper in a layout.
QRectF boundingRect() const override
void setPageSize(const QgsLayoutSize &size)
Sets the size of the page.
QgsLayoutSize pageSize() const
Returns the size of the page.
void setPageStyleSymbol(QgsFillSymbol *symbol)
Sets the symbol to use for drawing the page background.
Base class for graphical items within a QgsLayout.
bool writeXml(QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context) const
Stores the item state in a DOM element.
QgsLayoutSize sizeWithUnits() const
Returns the item's current size, including units.
void beginCommand(const QString &commandText, UndoCommand command=UndoNone)
Starts new undo command for this item.
int page() const
Returns the page the item is currently on, with the first page returning 0.
virtual void finalizeRestoreFromXml()
Called after all pending items have been restored from XML.
bool readXml(const QDomElement &itemElement, const QDomDocument &document, const QgsReadWriteContext &context)
Sets the item state from a DOM element.
bool shouldDrawItem() const
Returns whether the item should be drawn in the current context.
virtual void attemptMove(const QgsLayoutPoint &point, bool useReferencePoint=true, bool includesFrame=false, int page=-1)
Attempts to move the item to a specified point.
This class provides a method of storing measurements for use in QGIS layouts using a variety of diffe...
double length() const
Returns the length of the measurement.
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
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.
This class provides a method of storing points, consisting of an x and y coordinate,...
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.
This class provides a method of storing sizes, consisting of a width and height, for use in QGIS layo...
void endCommand()
Saves final state of an object and pushes the active command to the undo history.
void beginMacro(const QString &commandText)
Starts a macro command, with the given descriptive commandText.
void beginCommand(QgsLayoutUndoObjectInterface *object, const QString &commandText, int id=0)
Begins a new undo command for the specified object.
void endMacro()
Ends a macro command.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
void updateBounds()
Updates the scene bounds of the layout.
QgsLayoutPageCollection * pageCollection()
Returns a pointer to the layout's page collection, which stores and manages page items in the layout.
void layoutItems(QList< T * > &itemList) const
Returns a list of layout items of a specific type.
QgsLayoutItem * itemByUuid(const QString &uuid, bool includeTemplateUuids=false) const
Returns the layout item with matching uuid unique identifier, or nullptr if a matching item could not...
QgsLayoutGuideCollection & guides()
Returns a reference to the layout's guide collection, which manages page snap guides.
double convertToLayoutUnits(QgsLayoutMeasurement measurement) const
Converts a measurement into the layout's native units.
QgsLayoutMeasurement convertFromLayoutUnits(double length, Qgis::LayoutUnit unit) const
Converts a length measurement from the layout's native units to a specified target unit.
QRectF layoutBounds(bool ignorePages=false, double margin=0.0) const
Calculates the bounds of all non-gui items in the layout.
Qgis::LayoutUnit units() const
Returns the native units for the layout.
QgsLayoutUndoStack * undoStack()
Returns a pointer to the layout's undo stack, which manages undo/redo states for the layout and it's ...
The QgsMargins class 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.
The class is used as a container of context for various read/write operations on other objects.
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)