31 createDefaultPageStyleSymbol();
36 const auto constMPages = mPages;
39 mLayout->removeItem(
page );
49 mPageStyleSymbol.reset( static_cast<QgsFillSymbol *>( symbol->
clone() ) );
60 mPreviousItemPositions.clear();
61 QList< QgsLayoutItem * > items;
69 mPreviousItemPositions.insert( item->uuid(), qMakePair( item->page(), item->pagePositionWithUnits() ) );
75 for (
auto it = mPreviousItemPositions.constBegin(); it != mPreviousItemPositions.constEnd(); ++it )
79 if ( !mBlockUndoCommands )
80 item->beginCommand( QString() );
81 item->attemptMove( it.value().second,
true,
false, it.value().first );
82 if ( !mBlockUndoCommands )
86 mPreviousItemPositions.clear();
93 const auto constMPages = mPages;
122 double area = pageSize.width() * pageSize.height();
123 if ( area > maxArea )
138 if ( !size.isValid() )
143 || !
qgsDoubleNear( pageSize.height(), size.height(), 0.01 ) )
153 double startNextPageY = 0;
154 const auto constMPages = mPages;
158 if ( startNextPageY > point.y() )
163 if ( pageNumber > mPages.count() - 1 )
164 pageNumber = mPages.count() - 1;
170 if ( mPages.empty() )
174 double startNextPageY = 0;
175 const auto constMPages = mPages;
179 if ( startNextPageY >= point.y() )
184 if ( startNextPageY >= point.y() )
190 double lastPageHeight = mPages.last()->rect().height();
191 while ( startNextPageY < point.y() )
194 if ( startNextPageY >= point.y() )
204 const QList< QGraphicsItem * > items = mLayout->items( point );
205 for ( QGraphicsItem *item : items )
210 if ( page->mapToScene( page->rect() ).boundingRect().contains( point ) )
220 if ( page > 0 && page < mPages.count() )
222 layoutUnitsPos.ry() += mPages.at( page )->pos().y();
224 return layoutUnitsPos;
230 if ( page > 0 && page < mPages.count() )
240 double startCurrentPageY = 0;
241 double startNextPageY = 0;
243 const auto constMPages = mPages;
246 startCurrentPageY = startNextPageY;
248 if ( startNextPageY > position.y() )
254 if ( pageNumber == mPages.size() )
263 y = position.y() - startCurrentPageY;
265 return QPointF( position.x(), y );
280 if ( !mBlockUndoCommands )
291 if ( mPages.empty() )
293 std::unique_ptr< QgsLayoutItemPage >
page = qgis::make_unique< QgsLayoutItemPage >( mLayout );
304 bounds.setWidth( bounds.width() + marginLeft + marginRight );
305 bounds.setHeight( bounds.height() + marginTop + marginBottom );
308 page->setPageSize( newPageSize );
313 double diffX = marginLeft - bounds.left();
314 double diffY = marginTop - bounds.top();
316 const QList<QGraphicsItem *> itemList = mLayout->items();
317 for ( QGraphicsItem *item : itemList )
319 if (
QgsLayoutItem *layoutItem = dynamic_cast<QgsLayoutItem *>( item ) )
325 layoutItem->attemptMoveBy( diffX, diffY );
326 layoutItem->endCommand();
333 const QList< QgsLayoutGuide * > verticalGuides = mLayout->
guides().
guides( Qt::Vertical );
336 guide->setLayoutPosition( guide->layoutPosition() + diffX );
338 const QList< QgsLayoutGuide * > horizontalGuides = mLayout->
guides().
guides( Qt::Horizontal );
341 guide->setLayoutPosition( guide->layoutPosition() + diffY );
345 if ( !mBlockUndoCommands )
351 QDomElement element = document.createElement( QStringLiteral(
"PageCollection" ) );
354 element.appendChild( pageStyleElem );
361 mGuideCollection->writeXml( element, document, context );
363 parentElement.appendChild( element );
369 QDomElement element = e;
370 if ( element.nodeName() != QStringLiteral(
"PageCollection" ) )
372 element = element.firstChildElement( QStringLiteral(
"PageCollection" ) );
375 if ( element.nodeName() != QStringLiteral(
"PageCollection" ) )
380 mBlockUndoCommands =
true;
386 mLayout->removeItem(
page );
392 QDomElement pageStyleSymbolElem = element.firstChildElement( QStringLiteral(
"symbol" ) );
393 if ( !pageStyleSymbolElem.isNull() )
395 mPageStyleSymbol.reset( QgsSymbolLayerUtils::loadSymbol<QgsFillSymbol>( pageStyleSymbolElem, context ) );
398 QDomNodeList pageList = element.elementsByTagName( QStringLiteral(
"LayoutItem" ) );
399 for (
int i = 0; i < pageList.size(); ++i )
401 QDomElement pageElement = pageList.at( i ).toElement();
403 page->readXml( pageElement, document, context );
404 page->finalizeRestoreFromXml();
405 mPages.append( page.get() );
406 mLayout->addItem( page.release() );
411 mGuideCollection->readXml( element, document, context );
413 mBlockUndoCommands =
false;
419 return *mGuideCollection;
424 return *mGuideCollection;
429 const auto constMPages = mPages;
448 return mPages.count();
453 return mPages.value( pageNumber );
458 return mPages.value( pageNumber );
463 return mPages.indexOf( page );
468 QList<QgsLayoutItemPage *>
pages;
469 const auto constMPages = mPages;
472 if (
page->mapToScene(
page->rect() ).boundingRect().intersects( region ) )
482 const auto constMPages = mPages;
485 if (
page->mapToScene(
page->rect() ).boundingRect().intersects( region ) )
513 QList<QgsLayoutItem *> itemList;
514 const QList<QGraphicsItem *> graphicsItemList = mLayout->items();
515 itemList.reserve( graphicsItemList.size() );
516 for ( QGraphicsItem *graphicsItem : graphicsItemList )
521 itemList.push_back( item );
529 if ( page >= mPages.count() || page < 0 )
540 QList<QgsLayoutFrame *> frames;
544 if ( frame->hidePageIfEmpty() && frame->isEmpty() )
555 if ( !mBlockUndoCommands )
557 mPages.append( page );
558 mLayout->addItem( page );
560 if ( !mBlockUndoCommands )
566 if ( mPages.empty() )
570 std::unique_ptr< QgsLayoutItemPage > newPage = qgis::make_unique< QgsLayoutItemPage >( mLayout );
573 return mPages.at( mPages.count() - 1 );
578 if ( !mBlockUndoCommands )
584 if ( beforePage < 0 )
588 if ( beforePage >= mPages.count() )
590 mPages.append( page );
594 mPages.insert( beforePage, page );
596 mLayout->addItem( page );
600 for (
auto it = mPreviousItemPositions.begin(); it != mPreviousItemPositions.end(); ++it )
602 if ( it.value().first < beforePage )
605 it.value().first = it.value().first + 1;
609 if ( ! mBlockUndoCommands )
618 if ( pageNumber < 0 || pageNumber >= mPages.count() )
621 if ( !mBlockUndoCommands )
629 mLayout->removeItem( page );
634 for (
auto it = mPreviousItemPositions.begin(); it != mPreviousItemPositions.end(); ++it )
639 it.value().first = it.value().first - 1;
643 if ( ! mBlockUndoCommands )
652 if ( !mPages.contains( page ) )
655 if ( !mBlockUndoCommands )
660 int pageIndex = mPages.indexOf( page );
663 mPages.removeAll( page );
668 for (
auto it = mPreviousItemPositions.begin(); it != mPreviousItemPositions.end(); ++it )
670 if ( it.value().first <= pageIndex )
673 it.value().first = it.value().first - 1;
677 if ( !mBlockUndoCommands )
686 if ( !mBlockUndoCommands )
691 for (
int i = mPages.count() - 1; i >= 0; --i )
694 mPages.takeAt( i )->deleteLater();
697 if ( !mBlockUndoCommands )
706 mPages.removeAll( page );
710 void QgsLayoutPageCollection::createDefaultPageStyleSymbol()
713 properties.insert( QStringLiteral(
"color" ), QStringLiteral(
"white" ) );
714 properties.insert( QStringLiteral(
"style" ), QStringLiteral(
"solid" ) );
715 properties.insert( QStringLiteral(
"style_border" ), QStringLiteral(
"no" ) );
716 properties.insert( QStringLiteral(
"joinstyle" ), QStringLiteral(
"miter" ) );
double right() const
Returns the right margin.
The class is used as a container of context for various read/write operations on other objects...
~QgsLayoutPageCollection() override
bool writeXml(QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context) const override
Stores the collection's state in a DOM element.
void pageAboutToBeRemoved(int pageNumber)
Emitted just before a page is removed from the collection.
void beginCommand(const QString &commandText, UndoCommand command=UndoNone)
Starts new undo command for this item.
QgsLayoutGuideCollection & guides()
Returns a reference to the layout's guide collection, which manages page snap guides.
int pageNumber(QgsLayoutItemPage *page) const
Returns the page number for the specified page, or -1 if the page is not contained in the collection...
QRectF layoutBounds(bool ignorePages=false, double margin=0.0) const
Calculates the bounds of all non-gui items in the layout.
Base class for graphical items within a QgsLayout.
QgsLayoutItemPage * extendByNewPage()
Adds a new page to the end of the collection.
int pageNumberForPoint(QPointF point) const
Returns the page number corresponding to a point in the layout (in layout units). ...
QgsLayoutUndoStack * undoStack()
Returns a pointer to the layout's undo stack, which manages undo/redo states for the layout and it's ...
bool shouldExportPage(int page) const
Returns whether the specified page number should be included in exports of the layouts.
Contains the configuration for a single snap guide used by a layout.
QgsLayoutItemPage * takePage(QgsLayoutItemPage *page)
Takes a page from the collection, returning ownership of the page to the caller.
void resizeToContents(const QgsMargins &margins, QgsUnitTypes::LayoutUnit marginUnits)
Resizes the layout to a single page which fits the current contents of the layout.
QgsUnitTypes::LayoutUnit units() const
Returns the native units for the layout.
static QgsFillSymbol * createSimple(const QgsStringMap &properties)
Create a fill symbol with one symbol layer: SimpleFill with specified properties. ...
QList< QgsLayoutItem * > itemsOnPage(int page) const
Returns a list of layout items on the specified page index.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
QgsLayoutSize sizeWithUnits() const
Returns the item's current size, including units.
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...
void clear()
Removes all pages from the collection.
void setPageStyleSymbol(QgsFillSymbol *symbol)
Sets the symbol to use for drawing pages in the collection.
QgsLayoutGuideCollection & guides()
Returns a reference to the collection's guide collection, which manages page snap guides...
bool hasUniformPageSizes() const
Returns true if the layout has uniform page sizes, e.g.
QList< QgsLayoutItemPage *> pages()
Returns a list of pages in the collection.
void changed()
Emitted when pages are added or removed from the collection.
void updateBounds()
Updates the scene bounds of the layout.
double maximumPageWidth() const
Returns the maximum width of pages in the collection.
QMap< QString, QString > QgsStringMap
QList< int > visiblePageNumbers(const QRectF ®ion) const
Returns a list of the page numbers which are visible within the specified region (in layout coordinat...
double spaceBetweenPages() const
Returns the space between pages, in layout units.
bool pageIsEmpty(int page) const
Returns whether a given page index is empty, ie, it contains no items except for the background paper...
void beginPageSizeChange()
Should be called before changing any page item sizes, and followed by a call to endPageSizeChange().
This class provides a method of storing points, consisting of an x and y coordinate, for use in QGIS layouts.
void layoutItems(QList< T *> &itemList) const
Returns a list of layout items of a specific type.
bool writeXml(QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context) const
Stores the item state in a DOM element.
QgsUnitTypes::LayoutUnit units() const
Returns the units for the point.
QList< QgsLayoutItemPage *> visiblePages(const QRectF ®ion) const
Returns a list of the pages which are visible within the specified region (in layout coordinates)...
void endCommand()
Saves final state of an object and pushes the active command to the undo history. ...
QgsLayoutItemPage * page(int pageNumber)
Returns a specific page (by pageNumber) from the collection.
This class provides a method of storing measurements for use in QGIS layouts using a variety of diffe...
QgsFillSymbol * clone() const override
Returns a deep copy of this symbol.
QgsLayoutPageCollection * pageCollection()
Returns a pointer to the layout's page collection, which stores and manages page items in the layout...
double bottom() const
Returns the bottom margin.
double y() const
Returns y coordinate of point.
QgsLayoutItemPage * pageAtPoint(QPointF point) const
Returns the page at a specified point (in layout coordinates).
Stores and manages the snap guides used by a layout.
double top() const
Returns the top margin.
double x() const
Returns x coordinate of point.
bool readXml(const QDomElement &collectionElement, const QDomDocument &document, const QgsReadWriteContext &context) override
Sets the collection's state from a DOM element.
int page() const
Returns the page the item is currently on, with the first page returning 0.
void redraw()
Triggers a redraw for all pages.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
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.
int pageCount() const
Returns the number 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).
QList< QgsLayoutGuide *> guides()
Returns a list of all guides contained in the collection.
QgsLayoutSize pageSize() const
Returns the size of the page.
QSizeF maximumPageSize() const
Returns the maximum size of any page in the collection, by area.
QPointF pagePositionToLayoutPosition(int page, const QgsLayoutPoint &position) const
Converts a position on a page to an absolute position in layout coordinates.
int predictPageNumberForPoint(QPointF point) const
Returns the theoretical page number corresponding to a point in the layout (in layout units)...
void insertPage(QgsLayoutItemPage *page, int beforePage)
Inserts a page into a specific position in the collection.
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.
void update()
Updates the position (and visibility) of all guide line items.
QgsLayoutMeasurement convertFromLayoutUnits(double length, QgsUnitTypes::LayoutUnit unit) const
Converts a length measurement from the layout's native units to a specified target unit...
static QDomElement saveSymbol(const QString &symbolName, QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
LayoutUnit
Layout measurement units.
void addPage(QgsLayoutItemPage *page)
Adds a page to the collection.
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
void deletePage(int pageNumber)
Deletes a page from the collection.
double convertToLayoutUnits(QgsLayoutMeasurement measurement) const
Converts a measurement into the layout's native units.
void endPageSizeChange()
Should be called after changing any page item sizes, and preceded by a call to beginPageSizeChange()...
void endMacro()
Ends a macro command.
QgsLayoutPageCollection(QgsLayout *layout)
Constructor for QgsLayoutItemPage, with the specified parent layout.
void reflow()
Forces the page collection to reflow the arrangement of pages, e.g.
This class provides a method of storing sizes, consisting of a width and height, for use in QGIS layo...
Base class for frame items, which form a layout multiframe item.
double pageShadowWidth() const
Returns the size of the page shadow, in layout units.
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 left() const
Returns the left margin.
QgsLayout * layout() override
Returns the layout the object belongs to.
The QgsMargins class defines the four margins of a rectangle.
Item representing the paper in a layout.