QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
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
317class CORE_EXPORT QgsLayoutItemMap : public QgsLayoutItem, public QgsTemporalRangeObject
318{
319
320 Q_OBJECT
321
322 public:
323
328 {
330
338
344 Auto
345 };
346
352 {
353 ShowPartialLabels = 1 << 0,
354 ShowUnplacedLabels = 1 << 1,
355 };
356 Q_DECLARE_FLAGS( MapItemFlags, MapItemFlag )
357
358
361 explicit QgsLayoutItemMap( QgsLayout *layout );
362 ~QgsLayoutItemMap() override;
363
364 int type() const override;
365 QIcon icon() const override;
366 QgsLayoutItem::Flags itemFlags() const override;
367
373 QgsLayoutItemMap::MapItemFlags mapFlags() const;
374
380 void setMapFlags( QgsLayoutItemMap::MapItemFlags flags );
381
385 void assignFreeId();
386
387 //overridden to show "Map 1" type names
388 QString displayName() const override;
389
395 static QgsLayoutItemMap *create( QgsLayout *layout ) SIP_FACTORY;
396
397 // for now, map items behave a bit differently and don't implement draw. TODO - see if we can avoid this
398 void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) override;
399 Q_DECL_DEPRECATED int numberExportLayers() const override SIP_DEPRECATED;
400 void startLayeredExport() override;
401 void stopLayeredExport() override;
402 bool nextExportPart() override;
403 ExportLayerBehavior exportLayerBehavior() const override;
404 QgsLayoutItem::ExportLayerDetail exportLayerDetails() const override;
405 void setFrameStrokeWidth( QgsLayoutMeasurement width ) override;
406
412 double scale() const;
413
421 void setScale( double scale, bool forceUpdate = true );
422
430 void setExtent( const QgsRectangle &extent );
431
439 void zoomToExtent( const QgsRectangle &extent );
440
446 QgsRectangle extent() const;
447
448
456 QPolygonF visibleExtentPolygon() const;
457
467
476 QgsCoordinateReferenceSystem presetCrs() const { return mCrs; }
477
485 void setCrs( const QgsCoordinateReferenceSystem &crs );
486
495 bool keepLayerSet() const { return mKeepLayerSet; }
496
505 void setKeepLayerSet( bool enabled ) { mKeepLayerSet = enabled; }
506
513 QList<QgsMapLayer *> layers() const;
514
524 void setLayers( const QList<QgsMapLayer *> &layers );
525
530 bool keepLayerStyles() const { return mKeepLayerStyles; }
531
536 void setKeepLayerStyles( bool enabled ) { mKeepLayerStyles = enabled; }
537
542 QMap<QString, QString> layerStyleOverrides() const { return mLayerStyleOverrides; }
543
548 void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
549
553 void storeCurrentLayerStyles();
554
565 bool followVisibilityPreset() const { return mFollowVisibilityPreset; }
566
570 void setFollowVisibilityPreset( bool follow );
571
579 QString followVisibilityPresetName() const { return mFollowVisibilityPresetName; }
580
587 void setFollowVisibilityPresetName( const QString &name );
588
589 void moveContent( double dx, double dy ) override;
590 void setMoveContentPreviewOffset( double dx, double dy ) override;
591
592 void zoomContent( double factor, QPointF point ) override;
593
594
596 bool containsWmsLayer() const;
597
598 bool requiresRasterization() const override;
599 bool containsAdvancedEffects() const override;
600
607 void setMapRotation( double rotation );
608
617 double mapRotation( QgsLayoutObject::PropertyValueType valueType = QgsLayoutObject::EvaluatedValue ) const;
618
623 void setDrawAnnotations( bool draw ) { mDrawAnnotations = draw; }
624
629 bool drawAnnotations() const { return mDrawAnnotations; }
630
631
638 bool atlasDriven() const { return mAtlasDriven; }
639
646 void setAtlasDriven( bool enabled );
647
657 AtlasScalingMode atlasScalingMode() const { return mAtlasScalingMode; }
658
668 void setAtlasScalingMode( AtlasScalingMode mode ) { mAtlasScalingMode = mode; }
669
681
689 void setAtlasMargin( double margin ) { mAtlasMargin = margin; }
690
696 QgsLayoutItemMapGridStack *grids() { return mGridStack.get(); }
697
702 QgsLayoutItemMapGrid *grid();
703
710 QgsLayoutItemMapOverviewStack *overviews() { return mOverviewStack.get(); }
711
717 QgsLayoutItemMapOverview *overview();
718
729 QgsLayoutMeasurement labelMargin() const;
730
741 void setLabelMargin( const QgsLayoutMeasurement &margin );
742
743 QgsExpressionContext createExpressionContext() const override;
744
750 double mapUnitsToLayoutUnits() const;
751
759 QgsMapSettings mapSettings( const QgsRectangle &extent, QSizeF size, double dpi, bool includeLayerSettings ) const;
760
761 void finalizeRestoreFromXml() override;
762
767 QList<QgsMapLayer *> layersToRender( const QgsExpressionContext *context = nullptr ) const;
768
780 void addLabelBlockingItem( QgsLayoutItem *item );
781
793 void removeLabelBlockingItem( QgsLayoutItem *item );
794
806 bool isLabelBlockingItem( QgsLayoutItem *item ) const;
807
812 QgsMapRendererJob::Errors renderingErrors() const { return mRenderingErrors; }
813
821 QgsLabelingResults *previewLabelingResults() const;
822
823 bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
824
837 void addRenderedFeatureHandler( QgsRenderedFeatureHandlerInterface *handler );
838
845 void removeRenderedFeatureHandler( QgsRenderedFeatureHandlerInterface *handler );
846
850 QTransform layoutToMapCoordsTransform() const;
851
858
865
873 void setZRangeEnabled( bool enabled );
874
883 bool zRangeEnabled() const;
884
895 QgsDoubleRange zRange() const;
896
907 void setZRange( const QgsDoubleRange &range );
908
909 // Reimplement estimatedFrameBleed to take the grid frame into account
910 double estimatedFrameBleed() const override;
911
912 protected:
913
914 void draw( QgsLayoutItemRenderContext &context ) override;
915 bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
916 bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
917 QPainterPath framePath() const override;
918
920 bool isDrawing() const {return mDrawing;}
921
922 // In case of annotations, the bounding rectangle can be larger than the map item rectangle
923 QRectF boundingRect() const override;
924
926 QPolygonF transformedMapPolygon() const;
927
929 QPointF mapToItemCoords( QPointF mapCoords ) const;
930
934 QgsRectangle requestedExtent() const;
935
936 signals:
937
944
950 void mapRotationChanged( double newRotation );
951
954
960
969 void themeChanged( const QString &theme );
970
977
984
985 public slots:
986
987 void refresh() override;
988
989 void invalidateCache() override;
990
992 void updateBoundingRect();
993
994 void refreshDataDefinedProperty( QgsLayoutObject::DataDefinedProperty property = QgsLayoutObject::DataDefinedProperty::AllProperties ) override;
995
996 private slots:
997 void layersAboutToBeRemoved( const QList<QgsMapLayer *> &layers );
998
999 void painterJobFinished();
1000
1001 void shapeChanged();
1002
1003 void mapThemeChanged( const QString &theme );
1004
1006 void currentMapThemeRenamed( const QString &theme, const QString &newTheme );
1007
1009 void recreateCachedImageInBackground();
1010
1011 void updateAtlasFeature();
1012 private:
1013
1015
1017 int mMapId = 1;
1018
1019 std::unique_ptr< QgsLayoutItemMapGridStack > mGridStack;
1020 std::unique_ptr< QgsLayoutItemMapOverviewStack > mOverviewStack;
1021
1022 // Map region in map units really used for rendering
1023 // It can be the same as mUserExtent, but it can be bigger in on dimension if mCalculate==Scale,
1024 // so that full rectangle in paper is used.
1025 QgsRectangle mExtent;
1026
1029
1030 // Current temporary map region in map units. This is overwritten when atlas feature changes. It's also
1031 // used when the user changes the map extent and an atlas preview is enabled. This allows the user
1032 // to manually tweak each atlas preview page without affecting the actual original map extent.
1033 QgsRectangle mAtlasFeatureExtent;
1034
1035 // We have two images used for rendering/storing cached map images.
1036 // the first (mCacheFinalImage) is used ONLY for storing the most recent completed map render. It's always
1037 // used when drawing map item previews. The second (mCacheRenderingImage) is used temporarily while
1038 // rendering a new preview image in the background. If (and only if) the background render completes, then
1039 // mCacheRenderingImage is pushed into mCacheFinalImage, and used from then on when drawing the item preview.
1040 // This ensures that something is always shown in the map item, even while refreshing the preview image in the
1041 // background
1042 std::unique_ptr< QImage > mCacheFinalImage;
1043 std::unique_ptr< QImage > mCacheRenderingImage;
1044 bool mUpdatesEnabled = true;
1045
1047 bool mCacheInvalidated = true;
1048
1050 int mNumCachedLayers;
1051
1052 // Set to true if in state of drawing. Concurrent requests to draw method are returned if set to true
1053 bool mDrawing = false;
1054
1055 QTimer *mBackgroundUpdateTimer = nullptr;
1056 double mPreviewScaleFactor = 0;
1057 double mPreviewDevicePixelRatio = 1.0;
1058
1059 bool mDrawingPreview = false;
1060
1062 double mXOffset = 0.0;
1064 double mYOffset = 0.0;
1065
1066 double mLastRenderedImageOffsetX = 0.0;
1067 double mLastRenderedImageOffsetY = 0.0;
1068
1070 double mMapRotation = 0;
1071
1076 double mEvaluatedMapRotation = 0;
1077
1078 bool mZRangeEnabled = false;
1079 QgsDoubleRange mZRange;
1080
1082 bool mKeepLayerSet = false;
1083
1085 QList< QgsMapLayerRef > mLayers;
1086
1087 bool mKeepLayerStyles = false;
1089 QMap<QString, QString> mLayerStyleOverrides;
1090
1092 mutable QString mCachedLayerStyleOverridesPresetName;
1094 mutable QMap<QString, QString> mCachedPresetLayerStyleOverrides;
1095
1101 bool mFollowVisibilityPreset = false;
1102
1107 QString mFollowVisibilityPresetName;
1108
1110 QString mLastEvaluatedThemeName;
1111
1119 void drawMap( QPainter *painter, const QgsRectangle &extent, QSizeF size, double dpi );
1120
1122 void connectUpdateSlot();
1123
1125 void syncLayerSet();
1126
1128 const QgsLayoutItemMapGrid *constFirstMapGrid() const;
1129
1131 const QgsLayoutItemMapOverview *constFirstMapOverview() const;
1132
1137 QList< QgsLabelBlockingRegion > createLabelBlockingRegions( const QgsMapSettings &mapSettings ) const;
1138
1140 QRectF mCurrentRectangle;
1142 bool mDrawAnnotations = true;
1143
1145 bool mAtlasDriven = false;
1147 AtlasScalingMode mAtlasScalingMode = Auto;
1149 double mAtlasMargin = 0.10;
1150
1151 std::unique_ptr< QPainter > mPainter;
1152 std::unique_ptr< QgsMapRendererCustomPainterJob > mPainterJob;
1153 bool mPainterCancelWait = false;
1154
1155 QgsLayoutMeasurement mLabelMargin{ 0 };
1156 QgsLayoutMeasurement mEvaluatedLabelMargin{ 0 };
1157
1158 QStringList mBlockingLabelItemUuids;
1159 QList< QPointer< QgsLayoutItem > > mBlockingLabelItems;
1160
1162 QgsMapRendererJob::Errors mRenderingErrors;
1163
1164 QList< QgsRenderedFeatureHandlerInterface * > mRenderedFeatureHandlers;
1165
1166 std::unique_ptr< QgsMapRendererStagedRenderJob > mStagedRendererJob;
1167
1168 std::unique_ptr< QgsLabelingResults > mPreviewLabelingResults;
1169 std::unique_ptr< QgsLabelingResults > mExportLabelingResults;
1170
1171 void init();
1172
1174 void updateToolTip();
1175
1176 QString themeToRender( const QgsExpressionContext &context ) const;
1177
1179 QMap<QString, QString> layerStyleOverridesToRender( const QgsExpressionContext &context ) const;
1180
1182 QgsRectangle transformedExtent() const;
1183
1185 void mapPolygon( const QgsRectangle &extent, QPolygonF &poly ) const;
1186
1192 void transformShift( double &xShift, double &yShift ) const;
1193
1194 void drawAnnotations( QPainter *painter );
1195 void drawAnnotation( const QgsAnnotation *item, QgsRenderContext &context );
1196 QPointF layoutMapPosForItem( const QgsAnnotation *item ) const;
1197
1198 void drawMapFrame( QPainter *p );
1199 void drawMapBackground( QPainter *p );
1200
1201 enum PartType
1202 {
1203 Start,
1204 Background,
1205 Layer,
1206 Grid,
1207 OverviewMapExtent,
1208 Frame,
1209 SelectionBoxes,
1210 End,
1211 NotLayered,
1212 };
1213
1215 bool shouldDrawPart( PartType part ) const;
1216
1217 PartType mCurrentExportPart = NotLayered;
1218 QStringList mExportThemes;
1219 QStringList::iterator mExportThemeIt;
1220
1221 QgsLayoutItemMapAtlasClippingSettings *mAtlasClippingSettings = nullptr;
1222 QgsLayoutItemMapItemClipPathSettings *mItemClippingSettings = nullptr;
1223
1228 void refreshMapExtents( const QgsExpressionContext *context = nullptr );
1229
1230 void refreshLabelMargin( bool updateItem );
1231
1232 QgsRectangle computeAtlasRectangle();
1233
1234 void createStagedRenderJob( const QgsRectangle &extent, const QSizeF size, double dpi );
1235
1236 QPolygonF calculateVisibleExtentPolygon( bool includeClipping ) const;
1237
1241 std::map<QString, std::unique_ptr<QgsGroupLayer>> mGroupLayers;
1242
1246 friend class TestQgsLayoutMap;
1248 friend class QgsGeospatialPdfRenderedFeatureHandler;
1249 friend class QgsLayoutExporter;
1250
1251};
1252
1254
1255#endif //QGSLAYOUTITEMMAP_H
Abstract base class for annotation items which are drawn over a map.
QgsCompositionConverter class converts a QGIS 2.x composition to a QGIS 3.x layout.
This class represents a coordinate reference system (CRS).
QgsRange which stores a range of double values.
Definition qgsrange.h:231
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
A geometry is the spatial representation of a feature.
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.
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
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: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
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.
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_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.