QGIS API Documentation 3.99.0-Master (f78f5286a64)
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
330 static const QgsSettingsEntryBool *settingForceRasterMasks SIP_SKIP;
331
336 {
338
346
352 Auto
353 };
354
360 {
361 ShowPartialLabels = 1 << 0,
362 ShowUnplacedLabels = 1 << 1,
363 };
364 Q_DECLARE_FLAGS( MapItemFlags, MapItemFlag )
365
366
369 explicit QgsLayoutItemMap( QgsLayout *layout );
370 ~QgsLayoutItemMap() override;
371
372 int type() const override;
373 QIcon icon() const override;
374 QgsLayoutItem::Flags itemFlags() const override;
375
381 QgsLayoutItemMap::MapItemFlags mapFlags() const;
382
388 void setMapFlags( QgsLayoutItemMap::MapItemFlags flags );
389
393 void assignFreeId();
394
395 //overridden to show "Map 1" type names
396 QString displayName() const override;
397
403 static QgsLayoutItemMap *create( QgsLayout *layout ) SIP_FACTORY;
404
405 // for now, map items behave a bit differently and don't implement draw. TODO - see if we can avoid this
406 void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) override;
407
411 Q_DECL_DEPRECATED int numberExportLayers() const override SIP_DEPRECATED;
412
413 void startLayeredExport() override;
414 void stopLayeredExport() override;
415 bool nextExportPart() override;
416 ExportLayerBehavior exportLayerBehavior() const override;
417 QgsLayoutItem::ExportLayerDetail exportLayerDetails() const override;
418 void setFrameStrokeWidth( QgsLayoutMeasurement width ) override;
419
425 double scale() const;
426
434 void setScale( double scale, bool forceUpdate = true );
435
443 void setExtent( const QgsRectangle &extent );
444
452 void zoomToExtent( const QgsRectangle &extent );
453
459 QgsRectangle extent() const;
460
461
469 QPolygonF visibleExtentPolygon() const;
470
480
489 QgsCoordinateReferenceSystem presetCrs() const { return mCrs; }
490
498 void setCrs( const QgsCoordinateReferenceSystem &crs );
499
508 bool keepLayerSet() const { return mKeepLayerSet; }
509
518 void setKeepLayerSet( bool enabled ) { mKeepLayerSet = enabled; }
519
526 QList<QgsMapLayer *> layers() const;
527
537 void setLayers( const QList<QgsMapLayer *> &layers );
538
543 bool keepLayerStyles() const { return mKeepLayerStyles; }
544
549 void setKeepLayerStyles( bool enabled ) { mKeepLayerStyles = enabled; }
550
555 QMap<QString, QString> layerStyleOverrides() const { return mLayerStyleOverrides; }
556
561 void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
562
566 void storeCurrentLayerStyles();
567
578 bool followVisibilityPreset() const { return mFollowVisibilityPreset; }
579
583 void setFollowVisibilityPreset( bool follow );
584
592 QString followVisibilityPresetName() const { return mFollowVisibilityPresetName; }
593
600 void setFollowVisibilityPresetName( const QString &name );
601
602 void moveContent( double dx, double dy ) override;
603 void setMoveContentPreviewOffset( double dx, double dy ) override;
604
605 void zoomContent( double factor, QPointF point ) override;
606
607
609 bool containsWmsLayer() const;
610
611 bool requiresRasterization() const override;
612 bool containsAdvancedEffects() const override;
613
620 void setMapRotation( double rotation );
621
630 double mapRotation( QgsLayoutObject::PropertyValueType valueType = QgsLayoutObject::EvaluatedValue ) const;
631
636 void setDrawAnnotations( bool draw ) { mDrawAnnotations = draw; }
637
642 bool drawAnnotations() const { return mDrawAnnotations; }
643
644
651 bool atlasDriven() const { return mAtlasDriven; }
652
659 void setAtlasDriven( bool enabled );
660
670 AtlasScalingMode atlasScalingMode() const { return mAtlasScalingMode; }
671
681 void setAtlasScalingMode( AtlasScalingMode mode ) { mAtlasScalingMode = mode; }
682
694
702 void setAtlasMargin( double margin ) { mAtlasMargin = margin; }
703
709 QgsLayoutItemMapGridStack *grids() { return mGridStack.get(); }
710
715 QgsLayoutItemMapGrid *grid();
716
723 QgsLayoutItemMapOverviewStack *overviews() { return mOverviewStack.get(); }
724
730 QgsLayoutItemMapOverview *overview();
731
742 QgsLayoutMeasurement labelMargin() const;
743
754 void setLabelMargin( const QgsLayoutMeasurement &margin );
755
756 QgsExpressionContext createExpressionContext() const override;
757
763 double mapUnitsToLayoutUnits() const;
764
772 QgsMapSettings mapSettings( const QgsRectangle &extent, QSizeF size, double dpi, bool includeLayerSettings ) const;
773
774 void finalizeRestoreFromXml() override;
775
780 QList<QgsMapLayer *> layersToRender( const QgsExpressionContext *context = nullptr ) const;
781
793 void addLabelBlockingItem( QgsLayoutItem *item );
794
806 void removeLabelBlockingItem( QgsLayoutItem *item );
807
819 bool isLabelBlockingItem( QgsLayoutItem *item ) const;
820
825 QgsMapRendererJob::Errors renderingErrors() const { return mRenderingErrors; }
826
834 QgsLabelingResults *previewLabelingResults() const;
835
836 bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
837
850 void addRenderedFeatureHandler( QgsRenderedFeatureHandlerInterface *handler );
851
858 void removeRenderedFeatureHandler( QgsRenderedFeatureHandlerInterface *handler );
859
863 QTransform layoutToMapCoordsTransform() const;
864
871
878
886 void setZRangeEnabled( bool enabled );
887
896 bool zRangeEnabled() const;
897
908 QgsDoubleRange zRange() const;
909
920 void setZRange( const QgsDoubleRange &range );
921
922 // Reimplement estimatedFrameBleed to take the grid frame into account
923 double estimatedFrameBleed() const override;
924
925 protected:
926
927 void draw( QgsLayoutItemRenderContext &context ) override;
928 bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
929 bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
930 QPainterPath framePath() const override;
931
933 bool isDrawing() const {return mDrawing;}
934
935 // In case of annotations, the bounding rectangle can be larger than the map item rectangle
936 QRectF boundingRect() const override;
937
939 QPolygonF transformedMapPolygon() const;
940
942 QPointF mapToItemCoords( QPointF mapCoords ) const;
943
947 QgsRectangle requestedExtent() const;
948
949 signals:
950
957
963 void mapRotationChanged( double newRotation );
964
967
973
982 void themeChanged( const QString &theme );
983
990
997
998 public slots:
999
1000 void refresh() override;
1001
1002 void invalidateCache() override;
1003
1005 void updateBoundingRect();
1006
1007 void refreshDataDefinedProperty( QgsLayoutObject::DataDefinedProperty property = QgsLayoutObject::DataDefinedProperty::AllProperties ) override;
1008
1009 private slots:
1010 void layersAboutToBeRemoved( const QList<QgsMapLayer *> &layers );
1011
1012 void painterJobFinished();
1013
1014 void shapeChanged();
1015
1016 void mapThemeChanged( const QString &theme );
1017
1019 void currentMapThemeRenamed( const QString &theme, const QString &newTheme );
1020
1022 void recreateCachedImageInBackground();
1023
1024 void updateAtlasFeature();
1025 private:
1026
1028
1030 int mMapId = 1;
1031
1032 std::unique_ptr< QgsLayoutItemMapGridStack > mGridStack;
1033 std::unique_ptr< QgsLayoutItemMapOverviewStack > mOverviewStack;
1034
1035 // Map region in map units really used for rendering
1036 // It can be the same as mUserExtent, but it can be bigger in on dimension if mCalculate==Scale,
1037 // so that full rectangle in paper is used.
1038 QgsRectangle mExtent;
1039
1042
1043 // Current temporary map region in map units. This is overwritten when atlas feature changes. It's also
1044 // used when the user changes the map extent and an atlas preview is enabled. This allows the user
1045 // to manually tweak each atlas preview page without affecting the actual original map extent.
1046 QgsRectangle mAtlasFeatureExtent;
1047
1048 // We have two images used for rendering/storing cached map images.
1049 // the first (mCacheFinalImage) is used ONLY for storing the most recent completed map render. It's always
1050 // used when drawing map item previews. The second (mCacheRenderingImage) is used temporarily while
1051 // rendering a new preview image in the background. If (and only if) the background render completes, then
1052 // mCacheRenderingImage is pushed into mCacheFinalImage, and used from then on when drawing the item preview.
1053 // This ensures that something is always shown in the map item, even while refreshing the preview image in the
1054 // background
1055 std::unique_ptr< QImage > mCacheFinalImage;
1056 std::unique_ptr< QImage > mCacheRenderingImage;
1057 bool mUpdatesEnabled = true;
1058
1060 bool mCacheInvalidated = true;
1061
1063 int mNumCachedLayers;
1064
1065 // Set to true if in state of drawing. Concurrent requests to draw method are returned if set to true
1066 bool mDrawing = false;
1067
1068 QTimer *mBackgroundUpdateTimer = nullptr;
1069 double mPreviewScaleFactor = 0;
1070 double mPreviewDevicePixelRatio = 1.0;
1071
1072 bool mDrawingPreview = false;
1073
1075 double mXOffset = 0.0;
1077 double mYOffset = 0.0;
1078
1079 double mLastRenderedImageOffsetX = 0.0;
1080 double mLastRenderedImageOffsetY = 0.0;
1081
1083 double mMapRotation = 0;
1084
1089 double mEvaluatedMapRotation = 0;
1090
1091 bool mZRangeEnabled = false;
1092 QgsDoubleRange mZRange;
1093
1095 bool mKeepLayerSet = false;
1096
1098 QList< QgsMapLayerRef > mLayers;
1099
1100 bool mKeepLayerStyles = false;
1102 QMap<QString, QString> mLayerStyleOverrides;
1103
1105 mutable QString mCachedLayerStyleOverridesPresetName;
1107 mutable QMap<QString, QString> mCachedPresetLayerStyleOverrides;
1108
1114 bool mFollowVisibilityPreset = false;
1115
1120 QString mFollowVisibilityPresetName;
1121
1123 QString mLastEvaluatedThemeName;
1124
1132 void drawMap( QPainter *painter, const QgsRectangle &extent, QSizeF size, double dpi );
1133
1135 void connectUpdateSlot();
1136
1138 void syncLayerSet();
1139
1141 const QgsLayoutItemMapGrid *constFirstMapGrid() const;
1142
1144 const QgsLayoutItemMapOverview *constFirstMapOverview() const;
1145
1150 QList< QgsLabelBlockingRegion > createLabelBlockingRegions( const QgsMapSettings &mapSettings ) const;
1151
1153 QRectF mCurrentRectangle;
1155 bool mDrawAnnotations = true;
1156
1158 bool mAtlasDriven = false;
1160 AtlasScalingMode mAtlasScalingMode = Auto;
1162 double mAtlasMargin = 0.10;
1163
1164 std::unique_ptr< QPainter > mPainter;
1165 std::unique_ptr< QgsMapRendererCustomPainterJob > mPainterJob;
1166 bool mPainterCancelWait = false;
1167
1168 QgsLayoutMeasurement mLabelMargin{ 0 };
1169 QgsLayoutMeasurement mEvaluatedLabelMargin{ 0 };
1170
1171 QStringList mBlockingLabelItemUuids;
1172 QList< QPointer< QgsLayoutItem > > mBlockingLabelItems;
1173
1175 QgsMapRendererJob::Errors mRenderingErrors;
1176
1177 QList< QgsRenderedFeatureHandlerInterface * > mRenderedFeatureHandlers;
1178
1179 std::unique_ptr< QgsMapRendererStagedRenderJob > mStagedRendererJob;
1180
1181 std::unique_ptr< QgsLabelingResults > mPreviewLabelingResults;
1182 std::unique_ptr< QgsLabelingResults > mExportLabelingResults;
1183
1184 void init();
1185
1187 void updateToolTip();
1188
1189 QString themeToRender( const QgsExpressionContext &context ) const;
1190
1192 QMap<QString, QString> layerStyleOverridesToRender( const QgsExpressionContext &context ) const;
1193
1195 QgsRectangle transformedExtent() const;
1196
1198 void mapPolygon( const QgsRectangle &extent, QPolygonF &poly ) const;
1199
1205 void transformShift( double &xShift, double &yShift ) const;
1206
1207 void drawAnnotations( QPainter *painter );
1208 void drawAnnotation( const QgsAnnotation *item, QgsRenderContext &context );
1209 QPointF layoutMapPosForItem( const QgsAnnotation *item ) const;
1210
1211 void drawMapFrame( QPainter *p );
1212 void drawMapBackground( QPainter *p );
1213
1214 enum PartType
1215 {
1216 Start,
1217 Background,
1218 Layer,
1219 Grid,
1220 OverviewMapExtent,
1221 Frame,
1222 SelectionBoxes,
1223 End,
1224 NotLayered,
1225 };
1226
1228 bool shouldDrawPart( PartType part ) const;
1229
1230 PartType mCurrentExportPart = NotLayered;
1231 QStringList mExportThemes;
1232 QStringList::iterator mExportThemeIt;
1233
1234 QgsLayoutItemMapAtlasClippingSettings *mAtlasClippingSettings = nullptr;
1235 QgsLayoutItemMapItemClipPathSettings *mItemClippingSettings = nullptr;
1236
1237 std::unique_ptr<QgsGroupedFeatureFilterProvider> mAtlasFeatureFilterProvider;
1238
1243 void refreshMapExtents( const QgsExpressionContext *context = nullptr );
1244
1245 void refreshLabelMargin( bool updateItem );
1246
1247 QgsRectangle computeAtlasRectangle();
1248
1249 void createStagedRenderJob( const QgsRectangle &extent, const QSizeF size, double dpi );
1250
1251 QPolygonF calculateVisibleExtentPolygon( bool includeClipping ) const;
1252
1256 std::map<QString, std::unique_ptr<QgsGroupLayer>> mGroupLayers;
1257
1261 friend class TestQgsLayoutMap;
1263 friend class QgsGeospatialPdfRenderedFeatureHandler;
1264 friend class QgsLayoutExporter;
1265
1266};
1267
1269
1270#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 ...
A boolean settings entry.
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:114
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:286
#define SIP_SKIP
Definition qgis_sip.h:134
#define SIP_FACTORY
Definition qgis_sip.h:84
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsTextRendererUtils::CurvedTextFlags)
const QgsCoordinateReferenceSystem & crs
Contains details of a particular export layer relating to a layout item.