QGIS API Documentation 4.1.0-Master (3b8ef1f72a3)
Loading...
Searching...
No Matches
qgslayout.h
Go to the documentation of this file.
1/***************************************************************************
2 qgslayout.h
3 -------------------
4 begin : June 2017
5 copyright : (C) 2017 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8/***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16#ifndef QGSLAYOUT_H
17#define QGSLAYOUT_H
18
19#include "qgis_core.h"
23#include "qgslayoutsnapper.h"
25
26#include <QGraphicsScene>
27
29class QgsLayoutModel;
37
50class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContextGenerator, public QgsLayoutUndoObjectInterface
51{
52 Q_OBJECT
53
54 public:
57 {
58 ZPage = 0,
59 ZItem = 1,
60 ZGrid = 9997,
61 ZGuide = 9998,
62 ZSmartGuide = 9999,
63 ZMouseHandles = 10000,
64 ZViewTool = 10001,
66 };
67
74
82 QgsLayout( QgsProject *project );
83
84 ~QgsLayout() override;
85
90 QgsLayout *clone() const SIP_FACTORY;
91
96 void initializeDefaults();
97
103 void clear();
104
110 QgsProject *project() const;
111
115 QgsLayoutModel *itemsModel();
116
121 template<class T> void layoutItems( QList<T *> &itemList ) const SIP_SKIP
122 {
123 itemList.clear();
124 QList<QGraphicsItem *> graphicsItemList = items();
125 QList<QGraphicsItem *>::iterator itemIt = graphicsItemList.begin();
126 for ( ; itemIt != graphicsItemList.end(); ++itemIt )
127 {
128 T *item = dynamic_cast<T *>( *itemIt );
129 if ( item )
130 {
131 itemList.push_back( item );
132 }
133 }
134 }
135
140 template<class T> void layoutObjects( QList<T *> &objectList ) const SIP_SKIP
141 {
142 objectList.clear();
143 const QList<QGraphicsItem *> itemList( items() );
144 const QList<QgsLayoutMultiFrame *> frameList( multiFrames() );
145 for ( const auto &obj : itemList )
146 {
147 T *item = dynamic_cast<T *>( obj );
148 if ( item )
149 {
150 objectList.push_back( item );
151 }
152 }
153 for ( const auto &obj : frameList )
154 {
155 T *item = dynamic_cast<T *>( obj );
156 if ( item )
157 {
158 objectList.push_back( item );
159 }
160 }
161 }
162
169 QList<QgsLayoutItem *> selectedLayoutItems( bool includeLockedItems = true );
170
174 void setSelectedItem( QgsLayoutItem *item );
175
182 void deselectAll();
183
196 bool raiseItem( QgsLayoutItem *item, bool deferUpdate = false );
197
210 bool lowerItem( QgsLayoutItem *item, bool deferUpdate = false );
211
224 bool moveItemToTop( QgsLayoutItem *item, bool deferUpdate = false );
225
237 bool moveItemToBottom( QgsLayoutItem *item, bool deferUpdate = false );
238
244 void updateZValues( bool addUndoCommands = true );
245
261 QgsLayoutItem *itemByUuid( const QString &uuid, bool includeTemplateUuids = false ) const;
262
278 QgsLayoutItem *itemByTemplateUuid( const QString &uuid ) const;
279
286 QgsLayoutItem *itemById( const QString &id ) const;
287
301 QgsLayoutMultiFrame *multiFrameByUuid( const QString &uuid, bool includeTemplateUuids = false ) const;
302
308 QgsLayoutItem *layoutItemAt( QPointF position, bool ignoreLocked = false, double searchTolerance = 0 ) const;
309
315 QgsLayoutItem *layoutItemAt( QPointF position, const QgsLayoutItem *belowItem, bool ignoreLocked = false, double searchTolerance = 0 ) const;
316
323 void setUnits( Qgis::LayoutUnit units ) { mUnits = units; }
324
330 Qgis::LayoutUnit units() const { return mUnits; }
331
338 double convertToLayoutUnits( QgsLayoutMeasurement measurement ) const;
339
346 QSizeF convertToLayoutUnits( const QgsLayoutSize &size ) const;
347
354 QPointF convertToLayoutUnits( const QgsLayoutPoint &point ) const;
355
362 QgsLayoutMeasurement convertFromLayoutUnits( double length, Qgis::LayoutUnit unit ) const;
363
370 QgsLayoutSize convertFromLayoutUnits( QSizeF size, Qgis::LayoutUnit unit ) const;
371
378 QgsLayoutPoint convertFromLayoutUnits( QPointF point, Qgis::LayoutUnit unit ) const;
379
384 QgsLayoutRenderContext &renderContext();
385
390 SIP_SKIP const QgsLayoutRenderContext &renderContext() const;
391
396 QgsLayoutReportContext &reportContext();
397
402 SIP_SKIP const QgsLayoutReportContext &reportContext() const;
403
408 QgsLayoutSnapper &snapper() { return mSnapper; }
409
414 SIP_SKIP const QgsLayoutSnapper &snapper() const { return mSnapper; }
415
420 QgsLayoutGridSettings &gridSettings() { return mGridSettings; }
421
426 SIP_SKIP const QgsLayoutGridSettings &gridSettings() const { return mGridSettings; }
427
431 void reloadSettings();
432
436 QgsLayoutGuideCollection &guides();
437
441 SIP_SKIP const QgsLayoutGuideCollection &guides() const;
442
448
457 void setCustomProperty( const QString &key, const QVariant &value );
458
468 QVariant customProperty( const QString &key, const QVariant &defaultValue = QVariant() ) const;
469
477 void removeCustomProperty( const QString &key );
478
485 QStringList customProperties() const;
486
493 QgsLayoutItemMap *referenceMap() const;
494
500 void setReferenceMap( QgsLayoutItemMap *map );
501
506 QgsLayoutPageCollection *pageCollection();
507
512 SIP_SKIP const QgsLayoutPageCollection *pageCollection() const;
513
523 QRectF layoutBounds( bool ignorePages = false, double margin = 0.0 ) const;
524
535 QRectF pageItemBounds( int page, bool visibleOnly = false ) const;
536
541 void addLayoutItem( QgsLayoutItem *item SIP_TRANSFER );
542
548 void removeLayoutItem( QgsLayoutItem *item );
549
555 void addMultiFrame( QgsLayoutMultiFrame *multiFrame SIP_TRANSFER );
556
562 void removeMultiFrame( QgsLayoutMultiFrame *multiFrame );
563
569 QList< QgsLayoutMultiFrame * > multiFrames() const;
570
576 bool saveAsTemplate( const QString &path, const QgsReadWriteContext &context ) const;
577
589 QList< QgsLayoutItem * > loadFromTemplate( const QDomDocument &document, const QgsReadWriteContext &context, bool clearExisting = true, bool *ok SIP_OUT = nullptr );
590
595 virtual QDomElement writeXml( QDomDocument &document, const QgsReadWriteContext &context ) const;
596
601 virtual bool readXml( const QDomElement &layoutElement, const QDomDocument &document, const QgsReadWriteContext &context );
602
615 QList< QgsLayoutItem * > addItemsFromXml( const QDomElement &parentElement, const QDomDocument &document, const QgsReadWriteContext &context, QPointF *position = nullptr, bool pasteInPlace = false );
616
621 QgsLayoutUndoStack *undoStack();
622
627 SIP_SKIP const QgsLayoutUndoStack *undoStack() const;
628
629 QgsAbstractLayoutUndoCommand *createCommand( const QString &text, int id = 0, QUndoCommand *parent = nullptr ) SIP_FACTORY override;
630
636 QgsLayoutItemGroup *groupItems( const QList<QgsLayoutItem *> &items );
637
647 QList<QgsLayoutItem *> ungroupItems( QgsLayoutItemGroup *group );
648
658 bool accept( QgsStyleEntityVisitorInterface *visitor ) const;
659
660#ifndef SIP_RUN
663
669
675#endif
676
677 public slots:
678
687 void refresh();
688
692 void updateBounds();
693
694 signals:
695
701 void changed();
702
707
713
718 void refreshed();
719
725 void backgroundTaskCountChanged( int total );
726
733
734 private slots:
735 void itemBackgroundTaskCountChanged( int count );
736
737 private:
738 QgsProject *mProject = nullptr;
739 std::unique_ptr< QgsLayoutModel > mItemsModel;
740
741 QgsObjectCustomProperties mCustomProperties;
742
744 QgsLayoutRenderContext *mRenderContext = nullptr;
745 QgsLayoutReportContext *mReportContext = nullptr;
746 QgsLayoutSnapper mSnapper;
747 QgsLayoutGridSettings mGridSettings;
748
749 std::unique_ptr< QgsLayoutPageCollection > mPageCollection;
750 std::unique_ptr< QgsLayoutUndoStack > mUndoStack;
751
753 QList<QgsLayoutMultiFrame *> mMultiFrames;
754
756 QString mWorldFileMapId;
757
758 QHash< QgsLayoutItem *, int > mBackgroundTaskCount;
759
761 void writeXmlLayoutSettings( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const;
763 bool readXmlLayoutSettings( const QDomElement &layoutElement, const QDomDocument &document, const QgsReadWriteContext &context );
764
768 void addLayoutItemPrivate( QgsLayoutItem *item );
769
773 void removeLayoutItemPrivate( QgsLayoutItem *item );
774
775 void deleteAndRemoveMultiFrames();
776
778 QPointF minPointFromXml( const QDomElement &elem ) const;
779
780 QgsLayout( const QgsLayout & ) = delete;
781 QgsLayout &operator=( const QgsLayout & ) = delete;
782
788 friend class QgsLayoutModel;
791};
792
793#endif //QGSLAYOUT_H
LayoutUnit
Layout measurement units.
Definition qgis.h:5459
@ Millimeters
Millimeters.
Definition qgis.h:5460
Base class for commands to undo/redo layout and layout object changes.
Abstract interface for generating an expression context.
virtual QgsExpressionContext createExpressionContext() const =0
This method needs to be reimplemented in all classes which implement this interface and return an exp...
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Contains settings relating to the appearance, spacing and offset for layout grids.
Stores and manages the snap guides used by a layout.
A container for grouping several QgsLayoutItems.
Layout graphical items for displaying a map.
Base class for graphical items within a QgsLayout.
Provides a method of storing measurements for use in QGIS layouts using a variety of different measur...
A model for items attached to a layout.
Abstract base class for layout items with the ability to distribute the content to several frames (Qg...
A manager for a collection of pages in a layout.
Provides a method of storing points, consisting of an x and y coordinate, for use in QGIS layouts.
Stores information relating to the current rendering settings for a layout.
Stores information relating to the current reporting context for a layout.
Provides a method of storing sizes, consisting of a width and height, for use in QGIS layouts.
Manages snapping grids and preset snap lines in a layout, and handles snapping points to the nearest ...
Interface for layout objects which support undo/redo commands.
virtual QgsAbstractLayoutUndoCommand * createCommand(const QString &text, int id=0, QUndoCommand *parent=nullptr)=0
Creates a new layout undo command with the specified text and parent.
An undo stack for QgsLayouts.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition qgslayout.h:51
friend class QgsLayoutItemDeleteUndoCommand
Definition qgslayout.h:784
void updateBounds()
Updates the scene bounds of the layout.
QgsLayoutSnapper & snapper()
Returns a reference to the layout's snapper, which stores handles layout snap grids and lines and sna...
Definition qgslayout.h:408
void variablesChanged()
Emitted whenever the expression variables stored in the layout have been changed.
void layoutItems(QList< T * > &itemList) const
Returns a list of layout items of a specific type.
Definition qgslayout.h:121
void setUnits(Qgis::LayoutUnit units)
Sets the native measurement units for the layout.
Definition qgslayout.h:323
friend class QgsLayoutItemUndoCommand
Definition qgslayout.h:785
const QgsLayoutGridSettings & gridSettings() const
Returns a reference to the layout's grid settings, which stores settings relating to grid appearance,...
Definition qgslayout.h:426
void changed()
Emitted when properties of the layout change.
friend class QgsLayoutMultiFrame
Definition qgslayout.h:789
static const QgsSettingsEntryString * settingsLayoutDefaultNorthArrow
Settings entry for the default north arrow SVG path.
Definition qgslayout.h:674
void layoutObjects(QList< T * > &objectList) const
Returns a list of layout objects (items and multiframes) of a specific type.
Definition qgslayout.h:140
friend class QgsLayoutItemAddItemCommand
Definition qgslayout.h:783
friend class QgsLayoutUndoCommand
Definition qgslayout.h:786
friend class QgsLayoutModel
Definition qgslayout.h:788
static const QgsSettingsEntryStringList * settingsSearchPathForTemplates
Settings entry search path for templates.
Definition qgslayout.h:662
void selectedItemChanged(QgsLayoutItem *selected)
Emitted whenever the selected item changes.
const QgsLayoutSnapper & snapper() const
Returns a reference to the layout's snapper, which stores handles layout snap grids and lines and sna...
Definition qgslayout.h:414
QgsLayoutGridSettings & gridSettings()
Returns a reference to the layout's grid settings, which stores settings relating to grid appearance,...
Definition qgslayout.h:420
void refresh()
Forces the layout, and all items contained within it, to refresh.
void backgroundTaskCountChanged(int total)
Emitted whenever the total number of background tasks running in items from the layout changes.
friend class QgsCompositionConverter
Definition qgslayout.h:790
void refreshed()
Emitted when the layout has been refreshed and items should also be refreshed and updated.
friend class QgsLayoutItemGroupUndoCommand
Definition qgslayout.h:787
QList< QgsLayoutMultiFrame * > multiFrames() const
Returns a list of multi frames contained in the layout.
ZValues
Preset item z-values, to ensure correct stacking.
Definition qgslayout.h:57
@ ZMouseHandles
Z-value for mouse handles.
Definition qgslayout.h:63
@ ZSmartGuide
Z-value for smart (item bounds based) guides.
Definition qgslayout.h:62
@ ZItem
Minimum z value for items.
Definition qgslayout.h:59
@ ZGuide
Z-value for page guides.
Definition qgslayout.h:61
@ ZPage
Z-value for page (paper) items.
Definition qgslayout.h:58
@ ZSnapIndicator
Z-value for snapping indicator.
Definition qgslayout.h:65
@ ZViewTool
Z-value for temporary view tool items.
Definition qgslayout.h:64
@ ZGrid
Z-value for page grids.
Definition qgslayout.h:60
static const QgsSettingsEntryString * settingsLayoutDefaultFont
Settings entry for the default font family used for new layout items.
Definition qgslayout.h:668
Qgis::LayoutUnit units() const
Returns the native units for the layout.
Definition qgslayout.h:330
void itemAdded(QgsLayoutItem *item)
Emitted when an item was added to the layout.
QgsLayout(QgsProject *project)
Construct a new layout linked to the specified project.
Definition qgslayout.cpp:61
UndoCommand
Layout undo commands, used for collapsing undo commands.
Definition qgslayout.h:70
@ UndoLayoutDpi
Change layout default DPI.
Definition qgslayout.h:71
@ UndoNone
No command suppression.
Definition qgslayout.h:72
Simple key-value store (keys = strings, values = variants) that supports loading/saving to/from XML i...
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:113
A container for the context for various read/write operations on objects.
A string list settings entry.
A string settings entry.
An interface for classes which can visit style entity (e.g.
#define SIP_SKIP
Definition qgis_sip.h:133
#define SIP_TRANSFER
Definition qgis_sip.h:35
#define SIP_OUT
Definition qgis_sip.h:57
#define SIP_FACTORY
Definition qgis_sip.h:83