QGIS API Documentation 3.99.0-Master (a26b91b364d)
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;
34
41class CORE_EXPORT QgsLayoutItemMapAtlasClippingSettings : public QObject
42{
43 Q_OBJECT
44
45 public:
46
51
57 bool enabled() const;
58
64 void setEnabled( bool enabled );
65
71 QgsMapClippingRegion::FeatureClippingType featureClippingType() const;
72
78 void setFeatureClippingType( QgsMapClippingRegion::FeatureClippingType type );
79
85 bool forceLabelsInsideFeature() const;
86
92 void setForceLabelsInsideFeature( bool forceInside );
93
100 bool restrictToLayers() const;
101
108 void setRestrictToLayers( bool enabled );
109
118 QList< QgsMapLayer * > layersToClip() const;
119
128 void setLayersToClip( const QList< QgsMapLayer * > &layers );
129
135 bool writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const;
136
142 bool readXml( const QDomElement &element, const QDomDocument &doc, const QgsReadWriteContext &context );
143
144 signals:
145
149 void changed();
150
151 private slots:
152 void layersAboutToBeRemoved( const QList<QgsMapLayer *> &layers );
153
154 private:
155
156 QgsLayoutItemMap *mMap = nullptr;
157 bool mClipToAtlasFeature = false;
158 bool mRestrictToLayers = false;
159 QList< QgsMapLayerRef > mLayersToClip;
161 bool mForceLabelsInsideFeature = false;
162};
163
164
171class CORE_EXPORT QgsLayoutItemMapItemClipPathSettings : public QObject
172{
173 Q_OBJECT
174
175 public:
176
181
188 bool isActive() const;
189
195 bool enabled() const;
196
202 void setEnabled( bool enabled );
203
209 QgsGeometry clippedMapExtent() const;
210
220 QgsGeometry clipPathInMapItemCoordinates() const;
221
225 QgsMapClippingRegion toMapClippingRegion() const;
226
234 void setSourceItem( QgsLayoutItem *item );
235
242 QgsLayoutItem *sourceItem();
243
249 QgsMapClippingRegion::FeatureClippingType featureClippingType() const;
250
256 void setFeatureClippingType( QgsMapClippingRegion::FeatureClippingType type );
257
263 bool forceLabelsInsideClipPath() const;
264
270 void setForceLabelsInsideClipPath( bool forceInside );
271
277 bool writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const;
278
285 bool readXml( const QDomElement &element, const QDomDocument &doc, const QgsReadWriteContext &context );
286
291 void finalizeRestoreFromXml();
292
293 signals:
294
298 void changed();
299
300 private:
301
302 QgsLayoutItemMap *mMap = nullptr;
303 bool mEnabled = false;
305 bool mForceLabelsInsideClipPath = false;
306
307 QPointer< QgsLayoutItem > mClipPathSource;
308 QString mClipPathUuid;
309
310};
311
312
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
404 Q_DECL_DEPRECATED int numberExportLayers() const override SIP_DEPRECATED;
405
406 void startLayeredExport() override;
407 void stopLayeredExport() override;
408 bool nextExportPart() override;
409 ExportLayerBehavior exportLayerBehavior() const override;
410 QgsLayoutItem::ExportLayerDetail exportLayerDetails() const override;
411 void setFrameStrokeWidth( QgsLayoutMeasurement width ) override;
412
418 double scale() const;
419
427 void setScale( double scale, bool forceUpdate = true );
428
436 void setExtent( const QgsRectangle &extent );
437
445 void zoomToExtent( const QgsRectangle &extent );
446
452 QgsRectangle extent() const;
453
454
462 QPolygonF visibleExtentPolygon() const;
463
473
482 QgsCoordinateReferenceSystem presetCrs() const { return mCrs; }
483
491 void setCrs( const QgsCoordinateReferenceSystem &crs );
492
501 bool keepLayerSet() const { return mKeepLayerSet; }
502
511 void setKeepLayerSet( bool enabled ) { mKeepLayerSet = enabled; }
512
519 QList<QgsMapLayer *> layers() const;
520
530 void setLayers( const QList<QgsMapLayer *> &layers );
531
536 bool keepLayerStyles() const { return mKeepLayerStyles; }
537
542 void setKeepLayerStyles( bool enabled ) { mKeepLayerStyles = enabled; }
543
548 QMap<QString, QString> layerStyleOverrides() const { return mLayerStyleOverrides; }
549
554 void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
555
559 void storeCurrentLayerStyles();
560
571 bool followVisibilityPreset() const { return mFollowVisibilityPreset; }
572
576 void setFollowVisibilityPreset( bool follow );
577
585 QString followVisibilityPresetName() const { return mFollowVisibilityPresetName; }
586
593 void setFollowVisibilityPresetName( const QString &name );
594
595 void moveContent( double dx, double dy ) override;
596 void setMoveContentPreviewOffset( double dx, double dy ) override;
597
598 void zoomContent( double factor, QPointF point ) override;
599
600
602 bool containsWmsLayer() const;
603
604 bool requiresRasterization() const override;
605 bool containsAdvancedEffects() const override;
606
613 void setMapRotation( double rotation );
614
623 double mapRotation( QgsLayoutObject::PropertyValueType valueType = QgsLayoutObject::EvaluatedValue ) const;
624
629 void setDrawAnnotations( bool draw ) { mDrawAnnotations = draw; }
630
635 bool drawAnnotations() const { return mDrawAnnotations; }
636
637
644 bool atlasDriven() const { return mAtlasDriven; }
645
652 void setAtlasDriven( bool enabled );
653
663 AtlasScalingMode atlasScalingMode() const { return mAtlasScalingMode; }
664
674 void setAtlasScalingMode( AtlasScalingMode mode ) { mAtlasScalingMode = mode; }
675
687
695 void setAtlasMargin( double margin ) { mAtlasMargin = margin; }
696
702 QgsLayoutItemMapGridStack *grids() { return mGridStack.get(); }
703
708 QgsLayoutItemMapGrid *grid();
709
716 QgsLayoutItemMapOverviewStack *overviews() { return mOverviewStack.get(); }
717
723 QgsLayoutItemMapOverview *overview();
724
735 QgsLayoutMeasurement labelMargin() const;
736
747 void setLabelMargin( const QgsLayoutMeasurement &margin );
748
749 QgsExpressionContext createExpressionContext() const override;
750
756 double mapUnitsToLayoutUnits() const;
757
765 QgsMapSettings mapSettings( const QgsRectangle &extent, QSizeF size, double dpi, bool includeLayerSettings ) const;
766
767 void finalizeRestoreFromXml() override;
768
773 QList<QgsMapLayer *> layersToRender( const QgsExpressionContext *context = nullptr ) const;
774
786 void addLabelBlockingItem( QgsLayoutItem *item );
787
799 void removeLabelBlockingItem( QgsLayoutItem *item );
800
812 bool isLabelBlockingItem( QgsLayoutItem *item ) const;
813
818 QgsMapRendererJob::Errors renderingErrors() const { return mRenderingErrors; }
819
827 QgsLabelingResults *previewLabelingResults() const;
828
829 bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
830
843 void addRenderedFeatureHandler( QgsRenderedFeatureHandlerInterface *handler );
844
851 void removeRenderedFeatureHandler( QgsRenderedFeatureHandlerInterface *handler );
852
856 QTransform layoutToMapCoordsTransform() const;
857
864
871
879 void setZRangeEnabled( bool enabled );
880
889 bool zRangeEnabled() const;
890
901 QgsDoubleRange zRange() const;
902
913 void setZRange( const QgsDoubleRange &range );
914
915 // Reimplement estimatedFrameBleed to take the grid frame into account
916 double estimatedFrameBleed() const override;
917
918 protected:
919
920 void draw( QgsLayoutItemRenderContext &context ) override;
921 bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
922 bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
923 QPainterPath framePath() const override;
924
926 bool isDrawing() const {return mDrawing;}
927
928 // In case of annotations, the bounding rectangle can be larger than the map item rectangle
929 QRectF boundingRect() const override;
930
932 QPolygonF transformedMapPolygon() const;
933
935 QPointF mapToItemCoords( QPointF mapCoords ) const;
936
940 QgsRectangle requestedExtent() const;
941
942 signals:
943
950
956 void mapRotationChanged( double newRotation );
957
960
966
975 void themeChanged( const QString &theme );
976
983
990
991 public slots:
992
993 void refresh() override;
994
995 void invalidateCache() override;
996
998 void updateBoundingRect();
999
1000 void refreshDataDefinedProperty( QgsLayoutObject::DataDefinedProperty property = QgsLayoutObject::DataDefinedProperty::AllProperties ) override;
1001
1002 private slots:
1003 void layersAboutToBeRemoved( const QList<QgsMapLayer *> &layers );
1004
1005 void painterJobFinished();
1006
1007 void shapeChanged();
1008
1009 void mapThemeChanged( const QString &theme );
1010
1012 void currentMapThemeRenamed( const QString &theme, const QString &newTheme );
1013
1015 void recreateCachedImageInBackground();
1016
1017 void updateAtlasFeature();
1018 private:
1019
1021
1023 int mMapId = 1;
1024
1025 std::unique_ptr< QgsLayoutItemMapGridStack > mGridStack;
1026 std::unique_ptr< QgsLayoutItemMapOverviewStack > mOverviewStack;
1027
1028 // Map region in map units really used for rendering
1029 // It can be the same as mUserExtent, but it can be bigger in on dimension if mCalculate==Scale,
1030 // so that full rectangle in paper is used.
1031 QgsRectangle mExtent;
1032
1035
1036 // Current temporary map region in map units. This is overwritten when atlas feature changes. It's also
1037 // used when the user changes the map extent and an atlas preview is enabled. This allows the user
1038 // to manually tweak each atlas preview page without affecting the actual original map extent.
1039 QgsRectangle mAtlasFeatureExtent;
1040
1041 // We have two images used for rendering/storing cached map images.
1042 // the first (mCacheFinalImage) is used ONLY for storing the most recent completed map render. It's always
1043 // used when drawing map item previews. The second (mCacheRenderingImage) is used temporarily while
1044 // rendering a new preview image in the background. If (and only if) the background render completes, then
1045 // mCacheRenderingImage is pushed into mCacheFinalImage, and used from then on when drawing the item preview.
1046 // This ensures that something is always shown in the map item, even while refreshing the preview image in the
1047 // background
1048 std::unique_ptr< QImage > mCacheFinalImage;
1049 std::unique_ptr< QImage > mCacheRenderingImage;
1050 bool mUpdatesEnabled = true;
1051
1053 bool mCacheInvalidated = true;
1054
1056 int mNumCachedLayers;
1057
1058 // Set to true if in state of drawing. Concurrent requests to draw method are returned if set to true
1059 bool mDrawing = false;
1060
1061 QTimer *mBackgroundUpdateTimer = nullptr;
1062 double mPreviewScaleFactor = 0;
1063 double mPreviewDevicePixelRatio = 1.0;
1064
1065 bool mDrawingPreview = false;
1066
1068 double mXOffset = 0.0;
1070 double mYOffset = 0.0;
1071
1072 double mLastRenderedImageOffsetX = 0.0;
1073 double mLastRenderedImageOffsetY = 0.0;
1074
1076 double mMapRotation = 0;
1077
1082 double mEvaluatedMapRotation = 0;
1083
1084 bool mZRangeEnabled = false;
1085 QgsDoubleRange mZRange;
1086
1088 bool mKeepLayerSet = false;
1089
1091 QList< QgsMapLayerRef > mLayers;
1092
1093 bool mKeepLayerStyles = false;
1095 QMap<QString, QString> mLayerStyleOverrides;
1096
1098 mutable QString mCachedLayerStyleOverridesPresetName;
1100 mutable QMap<QString, QString> mCachedPresetLayerStyleOverrides;
1101
1107 bool mFollowVisibilityPreset = false;
1108
1113 QString mFollowVisibilityPresetName;
1114
1116 QString mLastEvaluatedThemeName;
1117
1125 void drawMap( QPainter *painter, const QgsRectangle &extent, QSizeF size, double dpi );
1126
1128 void connectUpdateSlot();
1129
1131 void syncLayerSet();
1132
1134 const QgsLayoutItemMapGrid *constFirstMapGrid() const;
1135
1137 const QgsLayoutItemMapOverview *constFirstMapOverview() const;
1138
1143 QList< QgsLabelBlockingRegion > createLabelBlockingRegions( const QgsMapSettings &mapSettings ) const;
1144
1146 QRectF mCurrentRectangle;
1148 bool mDrawAnnotations = true;
1149
1151 bool mAtlasDriven = false;
1153 AtlasScalingMode mAtlasScalingMode = Auto;
1155 double mAtlasMargin = 0.10;
1156
1157 std::unique_ptr< QPainter > mPainter;
1158 std::unique_ptr< QgsMapRendererCustomPainterJob > mPainterJob;
1159 bool mPainterCancelWait = false;
1160
1161 QgsLayoutMeasurement mLabelMargin{ 0 };
1162 QgsLayoutMeasurement mEvaluatedLabelMargin{ 0 };
1163
1164 QStringList mBlockingLabelItemUuids;
1165 QList< QPointer< QgsLayoutItem > > mBlockingLabelItems;
1166
1168 QgsMapRendererJob::Errors mRenderingErrors;
1169
1170 QList< QgsRenderedFeatureHandlerInterface * > mRenderedFeatureHandlers;
1171
1172 std::unique_ptr< QgsMapRendererStagedRenderJob > mStagedRendererJob;
1173
1174 std::unique_ptr< QgsLabelingResults > mPreviewLabelingResults;
1175 std::unique_ptr< QgsLabelingResults > mExportLabelingResults;
1176
1177 void init();
1178
1180 void updateToolTip();
1181
1182 QString themeToRender( const QgsExpressionContext &context ) const;
1183
1185 QMap<QString, QString> layerStyleOverridesToRender( const QgsExpressionContext &context ) const;
1186
1188 QgsRectangle transformedExtent() const;
1189
1191 void mapPolygon( const QgsRectangle &extent, QPolygonF &poly ) const;
1192
1198 void transformShift( double &xShift, double &yShift ) const;
1199
1200 void drawAnnotations( QPainter *painter );
1201 void drawAnnotation( const QgsAnnotation *item, QgsRenderContext &context );
1202 QPointF layoutMapPosForItem( const QgsAnnotation *item ) const;
1203
1204 void drawMapFrame( QPainter *p );
1205 void drawMapBackground( QPainter *p );
1206
1207 enum PartType
1208 {
1209 Start,
1210 Background,
1211 Layer,
1212 Grid,
1213 OverviewMapExtent,
1214 Frame,
1215 SelectionBoxes,
1216 End,
1217 NotLayered,
1218 };
1219
1221 bool shouldDrawPart( PartType part ) const;
1222
1223 PartType mCurrentExportPart = NotLayered;
1224 QStringList mExportThemes;
1225 QStringList::iterator mExportThemeIt;
1226
1227 QgsLayoutItemMapAtlasClippingSettings *mAtlasClippingSettings = nullptr;
1228 QgsLayoutItemMapItemClipPathSettings *mItemClippingSettings = nullptr;
1229
1230 std::unique_ptr<QgsGroupedFeatureFilterProvider> mAtlasFeatureFilterProvider;
1231
1236 void refreshMapExtents( const QgsExpressionContext *context = nullptr );
1237
1238 void refreshLabelMargin( bool updateItem );
1239
1240 QgsRectangle computeAtlasRectangle();
1241
1242 void createStagedRenderJob( const QgsRectangle &extent, const QSizeF size, double dpi );
1243
1244 QPolygonF calculateVisibleExtentPolygon( bool includeClipping ) const;
1245
1249 std::map<QString, std::unique_ptr<QgsGroupLayer>> mGroupLayers;
1250
1254 friend class TestQgsLayoutMap;
1256 friend class QgsGeospatialPdfRenderedFeatureHandler;
1257 friend class QgsLayoutExporter;
1258
1259};
1260
1262
1263#endif //QGSLAYOUTITEMMAP_H
Abstract base class for annotation items which are drawn over a map.
Converts a QGIS 2.x composition to a QGIS 3.x layout.
Represents a coordinate reference system (CRS).
QgsRange which stores a range of double values.
Definition qgsrange.h:233
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
A geometry is the spatial representation of a feature.
A filter filter provider grouping several filter providers.
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.
QFlags< MapItemFlag > MapItemFlags
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.
Base class for graphical items within a QgsLayout.
ExportLayerBehavior
Behavior of item when exporting to layered outputs.
QFlags< Flag > Flags
Provides a method of storing measurements for use in QGIS layouts using a variety of different measur...
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:49
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
Contains configuration for rendering maps.
A container for the context for various read/write operations on objects.
A rectangle specified with double values.
Contains information about the context of a rendering operation.
An interface for classes which provide 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_ENUM_BASETYPE(type)
Definition qgis_sip.h:278
#define SIP_FACTORY
Definition qgis_sip.h:76
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsTextRendererUtils::CurvedTextFlags)
const QgsCoordinateReferenceSystem & crs
Contains details of a particular export layer relating to a layout item.