QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
qgslayoutitemmap.h
Go to the documentation of this file.
1/***************************************************************************
2 qgslayoutitemmap.h
3 -------------------
4 begin : July 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
17#ifndef QGSLAYOUTITEMMAP_H
18#define QGSLAYOUTITEMMAP_H
19
20#include "qgis_core.h"
21#include "qgsgrouplayer.h"
22#include "qgslayoutitem.h"
24#include "qgsmaplayerref.h"
30
31class QgsAnnotation;
33
40class CORE_EXPORT QgsLayoutItemMapAtlasClippingSettings : public QObject
41{
42 Q_OBJECT
43
44 public:
45
50
56 bool enabled() const;
57
63 void setEnabled( bool enabled );
64
70 QgsMapClippingRegion::FeatureClippingType featureClippingType() const;
71
77 void setFeatureClippingType( QgsMapClippingRegion::FeatureClippingType type );
78
84 bool forceLabelsInsideFeature() const;
85
91 void setForceLabelsInsideFeature( bool forceInside );
92
99 bool restrictToLayers() const;
100
107 void setRestrictToLayers( bool enabled );
108
117 QList< QgsMapLayer * > layersToClip() const;
118
127 void setLayersToClip( const QList< QgsMapLayer * > &layers );
128
134 bool writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const;
135
141 bool readXml( const QDomElement &element, const QDomDocument &doc, const QgsReadWriteContext &context );
142
143 signals:
144
148 void changed();
149
150 private slots:
151 void layersAboutToBeRemoved( const QList<QgsMapLayer *> &layers );
152
153 private:
154
155 QgsLayoutItemMap *mMap = nullptr;
156 bool mClipToAtlasFeature = false;
157 bool mRestrictToLayers = false;
158 QList< QgsMapLayerRef > mLayersToClip;
160 bool mForceLabelsInsideFeature = false;
161};
162
163
170class CORE_EXPORT QgsLayoutItemMapItemClipPathSettings : public QObject
171{
172 Q_OBJECT
173
174 public:
175
180
187 bool isActive() const;
188
194 bool enabled() const;
195
201 void setEnabled( bool enabled );
202
208 QgsGeometry clippedMapExtent() const;
209
219 QgsGeometry clipPathInMapItemCoordinates() const;
220
224 QgsMapClippingRegion toMapClippingRegion() const;
225
233 void setSourceItem( QgsLayoutItem *item );
234
241 QgsLayoutItem *sourceItem();
242
248 QgsMapClippingRegion::FeatureClippingType featureClippingType() const;
249
255 void setFeatureClippingType( QgsMapClippingRegion::FeatureClippingType type );
256
262 bool forceLabelsInsideClipPath() const;
263
269 void setForceLabelsInsideClipPath( bool forceInside );
270
276 bool writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const;
277
284 bool readXml( const QDomElement &element, const QDomDocument &doc, const QgsReadWriteContext &context );
285
290 void finalizeRestoreFromXml();
291
292 signals:
293
297 void changed();
298
299 private:
300
301 QgsLayoutItemMap *mMap = nullptr;
302 bool mEnabled = false;
304 bool mForceLabelsInsideClipPath = false;
305
306 QPointer< QgsLayoutItem > mClipPathSource;
307 QString mClipPathUuid;
308
309};
310
311
318class CORE_EXPORT QgsLayoutItemMap : public QgsLayoutItem, public QgsTemporalRangeObject
319{
320
321 Q_OBJECT
322
323 public:
324
329 {
331
339
345 Auto
346 };
347
353 {
354 ShowPartialLabels = 1 << 0,
355 ShowUnplacedLabels = 1 << 1,
356 };
357 Q_DECLARE_FLAGS( MapItemFlags, MapItemFlag )
358
359
362 explicit QgsLayoutItemMap( QgsLayout *layout );
363 ~QgsLayoutItemMap() override;
364
365 int type() const override;
366 QIcon icon() const override;
367 QgsLayoutItem::Flags itemFlags() const override;
368
374 QgsLayoutItemMap::MapItemFlags mapFlags() const;
375
381 void setMapFlags( QgsLayoutItemMap::MapItemFlags flags );
382
386 void assignFreeId();
387
388 //overridden to show "Map 1" type names
389 QString displayName() const override;
390
396 static QgsLayoutItemMap *create( QgsLayout *layout ) SIP_FACTORY;
397
398 // for now, map items behave a bit differently and don't implement draw. TODO - see if we can avoid this
399 void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) override;
400 Q_DECL_DEPRECATED int numberExportLayers() const override SIP_DEPRECATED;
401 void startLayeredExport() override;
402 void stopLayeredExport() override;
403 bool nextExportPart() override;
404 ExportLayerBehavior exportLayerBehavior() const override;
405 QgsLayoutItem::ExportLayerDetail exportLayerDetails() const override;
406 void setFrameStrokeWidth( QgsLayoutMeasurement width ) override;
407
413 double scale() const;
414
422 void setScale( double scale, bool forceUpdate = true );
423
431 void setExtent( const QgsRectangle &extent );
432
440 void zoomToExtent( const QgsRectangle &extent );
441
447 QgsRectangle extent() const;
448
449
457 QPolygonF visibleExtentPolygon() const;
458
468
477 QgsCoordinateReferenceSystem presetCrs() const { return mCrs; }
478
486 void setCrs( const QgsCoordinateReferenceSystem &crs );
487
496 bool keepLayerSet() const { return mKeepLayerSet; }
497
506 void setKeepLayerSet( bool enabled ) { mKeepLayerSet = enabled; }
507
514 QList<QgsMapLayer *> layers() const;
515
525 void setLayers( const QList<QgsMapLayer *> &layers );
526
531 bool keepLayerStyles() const { return mKeepLayerStyles; }
532
537 void setKeepLayerStyles( bool enabled ) { mKeepLayerStyles = enabled; }
538
543 QMap<QString, QString> layerStyleOverrides() const { return mLayerStyleOverrides; }
544
549 void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
550
554 void storeCurrentLayerStyles();
555
566 bool followVisibilityPreset() const { return mFollowVisibilityPreset; }
567
571 void setFollowVisibilityPreset( bool follow );
572
580 QString followVisibilityPresetName() const { return mFollowVisibilityPresetName; }
581
588 void setFollowVisibilityPresetName( const QString &name );
589
590 void moveContent( double dx, double dy ) override;
591 void setMoveContentPreviewOffset( double dx, double dy ) override;
592
593 void zoomContent( double factor, QPointF point ) override;
594
595
597 bool containsWmsLayer() const;
598
599 bool requiresRasterization() const override;
600 bool containsAdvancedEffects() const override;
601
608 void setMapRotation( double rotation );
609
618 double mapRotation( QgsLayoutObject::PropertyValueType valueType = QgsLayoutObject::EvaluatedValue ) const;
619
624 void setDrawAnnotations( bool draw ) { mDrawAnnotations = draw; }
625
630 bool drawAnnotations() const { return mDrawAnnotations; }
631
632
639 bool atlasDriven() const { return mAtlasDriven; }
640
647 void setAtlasDriven( bool enabled );
648
658 AtlasScalingMode atlasScalingMode() const { return mAtlasScalingMode; }
659
669 void setAtlasScalingMode( AtlasScalingMode mode ) { mAtlasScalingMode = mode; }
670
682
690 void setAtlasMargin( double margin ) { mAtlasMargin = margin; }
691
697 QgsLayoutItemMapGridStack *grids() { return mGridStack.get(); }
698
703 QgsLayoutItemMapGrid *grid();
704
711 QgsLayoutItemMapOverviewStack *overviews() { return mOverviewStack.get(); }
712
718 QgsLayoutItemMapOverview *overview();
719
730 QgsLayoutMeasurement labelMargin() const;
731
742 void setLabelMargin( const QgsLayoutMeasurement &margin );
743
744 QgsExpressionContext createExpressionContext() const override;
745
751 double mapUnitsToLayoutUnits() const;
752
760 QgsMapSettings mapSettings( const QgsRectangle &extent, QSizeF size, double dpi, bool includeLayerSettings ) const;
761
762 void finalizeRestoreFromXml() override;
763
768 QList<QgsMapLayer *> layersToRender( const QgsExpressionContext *context = nullptr ) const;
769
781 void addLabelBlockingItem( QgsLayoutItem *item );
782
794 void removeLabelBlockingItem( QgsLayoutItem *item );
795
807 bool isLabelBlockingItem( QgsLayoutItem *item ) const;
808
813 QgsMapRendererJob::Errors renderingErrors() const { return mRenderingErrors; }
814
822 QgsLabelingResults *previewLabelingResults() const;
823
824 bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
825
838 void addRenderedFeatureHandler( QgsRenderedFeatureHandlerInterface *handler );
839
846 void removeRenderedFeatureHandler( QgsRenderedFeatureHandlerInterface *handler );
847
851 QTransform layoutToMapCoordsTransform() const;
852
859
866
867 protected:
868
869 void draw( QgsLayoutItemRenderContext &context ) override;
870 bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
871 bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
872 QPainterPath framePath() const override;
873
875 bool isDrawing() const {return mDrawing;}
876
877 // In case of annotations, the bounding rectangle can be larger than the map item rectangle
878 QRectF boundingRect() const override;
879
881 QPolygonF transformedMapPolygon() const;
882
884 QPointF mapToItemCoords( QPointF mapCoords ) const;
885
889 QgsRectangle requestedExtent() const;
890
891 signals:
892
899
905 void mapRotationChanged( double newRotation );
906
909
915
924 void themeChanged( const QString &theme );
925
932
939
940 public slots:
941
942 void refresh() override;
943
944 void invalidateCache() override;
945
947 void updateBoundingRect();
948
949 void refreshDataDefinedProperty( QgsLayoutObject::DataDefinedProperty property = QgsLayoutObject::AllProperties ) override;
950
951 private slots:
952 void layersAboutToBeRemoved( const QList<QgsMapLayer *> &layers );
953
954 void painterJobFinished();
955
956 void shapeChanged();
957
958 void mapThemeChanged( const QString &theme );
959
961 void currentMapThemeRenamed( const QString &theme, const QString &newTheme );
962
964 void recreateCachedImageInBackground();
965
966 void updateAtlasFeature();
967 private:
968
969 QgsLayoutItemMap::MapItemFlags mMapFlags = QgsLayoutItemMap::MapItemFlags();
970
972 int mMapId = 1;
973
974 std::unique_ptr< QgsLayoutItemMapGridStack > mGridStack;
975 std::unique_ptr< QgsLayoutItemMapOverviewStack > mOverviewStack;
976
977 // Map region in map units really used for rendering
978 // It can be the same as mUserExtent, but it can be bigger in on dimension if mCalculate==Scale,
979 // so that full rectangle in paper is used.
980 QgsRectangle mExtent;
981
984
985 // Current temporary map region in map units. This is overwritten when atlas feature changes. It's also
986 // used when the user changes the map extent and an atlas preview is enabled. This allows the user
987 // to manually tweak each atlas preview page without affecting the actual original map extent.
988 QgsRectangle mAtlasFeatureExtent;
989
990 // We have two images used for rendering/storing cached map images.
991 // the first (mCacheFinalImage) is used ONLY for storing the most recent completed map render. It's always
992 // used when drawing map item previews. The second (mCacheRenderingImage) is used temporarily while
993 // rendering a new preview image in the background. If (and only if) the background render completes, then
994 // mCacheRenderingImage is pushed into mCacheFinalImage, and used from then on when drawing the item preview.
995 // This ensures that something is always shown in the map item, even while refreshing the preview image in the
996 // background
997 std::unique_ptr< QImage > mCacheFinalImage;
998 std::unique_ptr< QImage > mCacheRenderingImage;
999 bool mUpdatesEnabled = true;
1000
1002 bool mCacheInvalidated = true;
1003
1005 int mNumCachedLayers;
1006
1007 // Set to true if in state of drawing. Concurrent requests to draw method are returned if set to true
1008 bool mDrawing = false;
1009
1010 QTimer *mBackgroundUpdateTimer = nullptr;
1011 double mPreviewScaleFactor = 0;
1012
1013 bool mDrawingPreview = false;
1014
1016 double mXOffset = 0.0;
1018 double mYOffset = 0.0;
1019
1020 double mLastRenderedImageOffsetX = 0.0;
1021 double mLastRenderedImageOffsetY = 0.0;
1022
1024 double mMapRotation = 0;
1025
1030 double mEvaluatedMapRotation = 0;
1031
1033 bool mKeepLayerSet = false;
1034
1036 QList< QgsMapLayerRef > mLayers;
1037
1038 bool mKeepLayerStyles = false;
1040 QMap<QString, QString> mLayerStyleOverrides;
1041
1043 mutable QString mCachedLayerStyleOverridesPresetName;
1045 mutable QMap<QString, QString> mCachedPresetLayerStyleOverrides;
1046
1052 bool mFollowVisibilityPreset = false;
1053
1058 QString mFollowVisibilityPresetName;
1059
1061 QString mLastEvaluatedThemeName;
1062
1070 void drawMap( QPainter *painter, const QgsRectangle &extent, QSizeF size, double dpi );
1071
1073 void connectUpdateSlot();
1074
1076 void syncLayerSet();
1077
1079 const QgsLayoutItemMapGrid *constFirstMapGrid() const;
1080
1082 const QgsLayoutItemMapOverview *constFirstMapOverview() const;
1083
1088 QList< QgsLabelBlockingRegion > createLabelBlockingRegions( const QgsMapSettings &mapSettings ) const;
1089
1091 QRectF mCurrentRectangle;
1093 bool mDrawAnnotations = true;
1094
1096 bool mAtlasDriven = false;
1098 AtlasScalingMode mAtlasScalingMode = Auto;
1100 double mAtlasMargin = 0.10;
1101
1102 std::unique_ptr< QPainter > mPainter;
1103 std::unique_ptr< QgsMapRendererCustomPainterJob > mPainterJob;
1104 bool mPainterCancelWait = false;
1105
1106 QgsLayoutMeasurement mLabelMargin{ 0 };
1107 QgsLayoutMeasurement mEvaluatedLabelMargin{ 0 };
1108
1109 QStringList mBlockingLabelItemUuids;
1110 QList< QPointer< QgsLayoutItem > > mBlockingLabelItems;
1111
1113 QgsMapRendererJob::Errors mRenderingErrors;
1114
1115 QList< QgsRenderedFeatureHandlerInterface * > mRenderedFeatureHandlers;
1116
1117 std::unique_ptr< QgsMapRendererStagedRenderJob > mStagedRendererJob;
1118
1119 std::unique_ptr< QgsLabelingResults > mPreviewLabelingResults;
1120 std::unique_ptr< QgsLabelingResults > mExportLabelingResults;
1121
1122 void init();
1123
1125 void updateToolTip();
1126
1127 QString themeToRender( const QgsExpressionContext &context ) const;
1128
1130 QMap<QString, QString> layerStyleOverridesToRender( const QgsExpressionContext &context ) const;
1131
1133 QgsRectangle transformedExtent() const;
1134
1136 void mapPolygon( const QgsRectangle &extent, QPolygonF &poly ) const;
1137
1143 void transformShift( double &xShift, double &yShift ) const;
1144
1145 void drawAnnotations( QPainter *painter );
1146 void drawAnnotation( const QgsAnnotation *item, QgsRenderContext &context );
1147 QPointF layoutMapPosForItem( const QgsAnnotation *item ) const;
1148
1149 void drawMapFrame( QPainter *p );
1150 void drawMapBackground( QPainter *p );
1151
1152 enum PartType
1153 {
1154 Start,
1155 Background,
1156 Layer,
1157 Grid,
1158 OverviewMapExtent,
1159 Frame,
1160 SelectionBoxes,
1161 End,
1162 NotLayered,
1163 };
1164
1166 bool shouldDrawPart( PartType part ) const;
1167
1168 PartType mCurrentExportPart = NotLayered;
1169 QStringList mExportThemes;
1170 QStringList::iterator mExportThemeIt;
1171
1172 QgsLayoutItemMapAtlasClippingSettings *mAtlasClippingSettings = nullptr;
1173 QgsLayoutItemMapItemClipPathSettings *mItemClippingSettings = nullptr;
1174
1179 void refreshMapExtents( const QgsExpressionContext *context = nullptr );
1180
1181 void refreshLabelMargin( bool updateItem );
1182
1183 QgsRectangle computeAtlasRectangle();
1184
1185 void createStagedRenderJob( const QgsRectangle &extent, const QSizeF size, double dpi );
1186
1187 QPolygonF calculateVisibleExtentPolygon( bool includeClipping ) const;
1188
1192 std::map<QString, std::unique_ptr<QgsGroupLayer>> mGroupLayers;
1193
1197 friend class TestQgsLayoutMap;
1199 friend class QgsGeoPdfRenderedFeatureHandler;
1200 friend class QgsLayoutExporter;
1201
1202};
1203
1204Q_DECLARE_OPERATORS_FOR_FLAGS( QgsLayoutItemMap::MapItemFlags )
1205
1206#endif //QGSLAYOUTITEMMAP_H
Abstract base class for annotation items which are drawn over a map.
Definition: qgsannotation.h:54
QgsCompositionConverter class converts a QGIS 2.x composition to a QGIS 3.x layout.
This class represents a coordinate reference system (CRS).
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:164
Class that stores computed placement from labeling engine.
Handles rendering and exports of layouts to various formats.
A layout item subclass for map legends.
Contains settings relating to clipping a layout map by the current atlas feature.
void changed()
Emitted when the atlas clipping settings are changed.
A collection of grids which is drawn above the map content in a QgsLayoutItemMap.
An individual grid which is drawn above the map content in a QgsLayoutItemMap.
Contains settings relating to clipping a layout map by another layout item.
void changed()
Emitted when the item clipping settings are changed.
A collection of overviews which are drawn above the map content in a QgsLayoutItemMap.
An individual overview which is drawn above the map content in a QgsLayoutItemMap,...
Layout graphical items for displaying a map.
void extentChanged()
Emitted when the map's extent changes.
void preparedForAtlas()
Emitted when the map has been prepared for atlas rendering, just before actual rendering.
QgsMapRendererJob::Errors renderingErrors() const
Returns map rendering errors.
bool keepLayerSet() const
Returns whether a stored layer set should be used or the current layer set from the project associate...
void setKeepLayerSet(bool enabled)
Sets whether the stored layer set should be used or the current layer set of the associated project.
AtlasScalingMode
Scaling modes used for the serial rendering (atlas)
@ Predefined
A scale is chosen from the predefined scales.
@ Fixed
The current scale of the map is used for each feature of the atlas.
void layerStyleOverridesChanged()
Emitted when layer style overrides are changed... a means to let associated legend items know they sh...
void mapRotationChanged(double newRotation)
Emitted when the map's rotation changes.
bool isDrawing() const
True if a draw is already in progress.
QString followVisibilityPresetName() const
Preset name that decides which layers and layer styles are used for map rendering.
void previewRefreshed()
Emitted whenever the item's map preview has been refreshed.
void setDrawAnnotations(bool draw)
Sets whether annotations are drawn within the map.
bool followVisibilityPreset() const
Returns whether the map should follow a map theme.
void crsChanged()
Emitted when the map's coordinate reference system is changed.
QgsLayoutItemMapOverviewStack * overviews()
Returns the map item's overview stack, which is used to control how overviews are drawn over the map'...
bool atlasDriven() const
Returns whether the map extent is set to follow the current atlas feature.
bool keepLayerStyles() const
Returns whether current styles of layers should be overridden by previously stored styles.
QMap< QString, QString > layerStyleOverrides() const
Returns stored overrides of styles for layers.
AtlasScalingMode atlasScalingMode() const
Returns the current atlas scaling mode.
void setAtlasScalingMode(AtlasScalingMode mode)
Sets the current atlas scaling mode.
void themeChanged(const QString &theme)
Emitted when the map's associated theme is changed.
MapItemFlag
Various flags that affect drawing of map items.
bool drawAnnotations() const
Returns whether annotations are drawn within the map.
QgsLayoutItemMapGridStack * grids()
Returns the map item's grid stack, which is used to control how grids are drawn over the map's conten...
QgsLayoutItemMapAtlasClippingSettings * atlasClippingSettings()
Returns the map's atlas clipping settings.
void setAtlasMargin(double margin)
Sets the margin size (percentage) used when the map is in atlas mode.
void setKeepLayerStyles(bool enabled)
Sets whether current styles of layers should be overridden by previously stored styles.
QgsLayoutItemMapItemClipPathSettings * itemClippingSettings()
Returns the map's item based clip path settings.
Contains settings and helpers relating to a render of a QgsLayoutItem.
Definition: qgslayoutitem.h:44
Base class for graphical items within a QgsLayout.
This class provides a method of storing measurements for use in QGIS layouts using a variety of diffe...
DataDefinedProperty
Data defined properties for different item types.
@ AllProperties
All properties for item.
PropertyValueType
Specifies whether the value returned by a function should be the original, user set value,...
@ EvaluatedValue
Return the current evaluated value for the property.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:50
A map clipping region (in map coordinates and CRS).
FeatureClippingType
Feature clipping behavior, which controls how features from vector layers will be clipped.
@ ClipPainterOnly
Applying clipping on the painter only (i.e. feature boundaries will be unchanged, but may be invisibl...
QList< QgsMapRendererJob::Error > Errors
The QgsMapSettings class contains configuration for rendering of the map.
The class is used as a container of context for various read/write operations on other objects.
A rectangle specified with double values.
Definition: qgsrectangle.h:42
Contains information about the context of a rendering operation.
An interface for classes which provider custom handlers for features rendered as part of a map render...
An interface for classes which can visit style entity (e.g.
Base class for objects with an associated (optional) temporal range.
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
#define SIP_FACTORY
Definition: qgis_sip.h:76
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsField::ConfigurationFlags) CORE_EXPORT QDataStream &operator<<(QDataStream &out
Writes the field to stream out. QGIS version compatibility is not guaranteed.
const QgsCoordinateReferenceSystem & crs