QGIS API Documentation 3.41.0-Master (af5edcb665c)
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
403 Q_DECL_DEPRECATED int numberExportLayers() const override SIP_DEPRECATED;
404
405 void startLayeredExport() override;
406 void stopLayeredExport() override;
407 bool nextExportPart() override;
408 ExportLayerBehavior exportLayerBehavior() const override;
409 QgsLayoutItem::ExportLayerDetail exportLayerDetails() const override;
410 void setFrameStrokeWidth( QgsLayoutMeasurement width ) override;
411
417 double scale() const;
418
426 void setScale( double scale, bool forceUpdate = true );
427
435 void setExtent( const QgsRectangle &extent );
436
444 void zoomToExtent( const QgsRectangle &extent );
445
451 QgsRectangle extent() const;
452
453
461 QPolygonF visibleExtentPolygon() const;
462
472
481 QgsCoordinateReferenceSystem presetCrs() const { return mCrs; }
482
490 void setCrs( const QgsCoordinateReferenceSystem &crs );
491
500 bool keepLayerSet() const { return mKeepLayerSet; }
501
510 void setKeepLayerSet( bool enabled ) { mKeepLayerSet = enabled; }
511
518 QList<QgsMapLayer *> layers() const;
519
529 void setLayers( const QList<QgsMapLayer *> &layers );
530
535 bool keepLayerStyles() const { return mKeepLayerStyles; }
536
541 void setKeepLayerStyles( bool enabled ) { mKeepLayerStyles = enabled; }
542
547 QMap<QString, QString> layerStyleOverrides() const { return mLayerStyleOverrides; }
548
553 void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
554
558 void storeCurrentLayerStyles();
559
570 bool followVisibilityPreset() const { return mFollowVisibilityPreset; }
571
575 void setFollowVisibilityPreset( bool follow );
576
584 QString followVisibilityPresetName() const { return mFollowVisibilityPresetName; }
585
592 void setFollowVisibilityPresetName( const QString &name );
593
594 void moveContent( double dx, double dy ) override;
595 void setMoveContentPreviewOffset( double dx, double dy ) override;
596
597 void zoomContent( double factor, QPointF point ) override;
598
599
601 bool containsWmsLayer() const;
602
603 bool requiresRasterization() const override;
604 bool containsAdvancedEffects() const override;
605
612 void setMapRotation( double rotation );
613
622 double mapRotation( QgsLayoutObject::PropertyValueType valueType = QgsLayoutObject::EvaluatedValue ) const;
623
628 void setDrawAnnotations( bool draw ) { mDrawAnnotations = draw; }
629
634 bool drawAnnotations() const { return mDrawAnnotations; }
635
636
643 bool atlasDriven() const { return mAtlasDriven; }
644
651 void setAtlasDriven( bool enabled );
652
662 AtlasScalingMode atlasScalingMode() const { return mAtlasScalingMode; }
663
673 void setAtlasScalingMode( AtlasScalingMode mode ) { mAtlasScalingMode = mode; }
674
686
694 void setAtlasMargin( double margin ) { mAtlasMargin = margin; }
695
701 QgsLayoutItemMapGridStack *grids() { return mGridStack.get(); }
702
707 QgsLayoutItemMapGrid *grid();
708
715 QgsLayoutItemMapOverviewStack *overviews() { return mOverviewStack.get(); }
716
722 QgsLayoutItemMapOverview *overview();
723
734 QgsLayoutMeasurement labelMargin() const;
735
746 void setLabelMargin( const QgsLayoutMeasurement &margin );
747
748 QgsExpressionContext createExpressionContext() const override;
749
755 double mapUnitsToLayoutUnits() const;
756
764 QgsMapSettings mapSettings( const QgsRectangle &extent, QSizeF size, double dpi, bool includeLayerSettings ) const;
765
766 void finalizeRestoreFromXml() override;
767
772 QList<QgsMapLayer *> layersToRender( const QgsExpressionContext *context = nullptr ) const;
773
785 void addLabelBlockingItem( QgsLayoutItem *item );
786
798 void removeLabelBlockingItem( QgsLayoutItem *item );
799
811 bool isLabelBlockingItem( QgsLayoutItem *item ) const;
812
817 QgsMapRendererJob::Errors renderingErrors() const { return mRenderingErrors; }
818
826 QgsLabelingResults *previewLabelingResults() const;
827
828 bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
829
842 void addRenderedFeatureHandler( QgsRenderedFeatureHandlerInterface *handler );
843
850 void removeRenderedFeatureHandler( QgsRenderedFeatureHandlerInterface *handler );
851
855 QTransform layoutToMapCoordsTransform() const;
856
863
870
878 void setZRangeEnabled( bool enabled );
879
888 bool zRangeEnabled() const;
889
900 QgsDoubleRange zRange() const;
901
912 void setZRange( const QgsDoubleRange &range );
913
914 // Reimplement estimatedFrameBleed to take the grid frame into account
915 double estimatedFrameBleed() const override;
916
917 protected:
918
919 void draw( QgsLayoutItemRenderContext &context ) override;
920 bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
921 bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
922 QPainterPath framePath() const override;
923
925 bool isDrawing() const {return mDrawing;}
926
927 // In case of annotations, the bounding rectangle can be larger than the map item rectangle
928 QRectF boundingRect() const override;
929
931 QPolygonF transformedMapPolygon() const;
932
934 QPointF mapToItemCoords( QPointF mapCoords ) const;
935
939 QgsRectangle requestedExtent() const;
940
941 signals:
942
949
955 void mapRotationChanged( double newRotation );
956
959
965
974 void themeChanged( const QString &theme );
975
982
989
990 public slots:
991
992 void refresh() override;
993
994 void invalidateCache() override;
995
997 void updateBoundingRect();
998
999 void refreshDataDefinedProperty( QgsLayoutObject::DataDefinedProperty property = QgsLayoutObject::DataDefinedProperty::AllProperties ) override;
1000
1001 private slots:
1002 void layersAboutToBeRemoved( const QList<QgsMapLayer *> &layers );
1003
1004 void painterJobFinished();
1005
1006 void shapeChanged();
1007
1008 void mapThemeChanged( const QString &theme );
1009
1011 void currentMapThemeRenamed( const QString &theme, const QString &newTheme );
1012
1014 void recreateCachedImageInBackground();
1015
1016 void updateAtlasFeature();
1017 private:
1018
1020
1022 int mMapId = 1;
1023
1024 std::unique_ptr< QgsLayoutItemMapGridStack > mGridStack;
1025 std::unique_ptr< QgsLayoutItemMapOverviewStack > mOverviewStack;
1026
1027 // Map region in map units really used for rendering
1028 // It can be the same as mUserExtent, but it can be bigger in on dimension if mCalculate==Scale,
1029 // so that full rectangle in paper is used.
1030 QgsRectangle mExtent;
1031
1034
1035 // Current temporary map region in map units. This is overwritten when atlas feature changes. It's also
1036 // used when the user changes the map extent and an atlas preview is enabled. This allows the user
1037 // to manually tweak each atlas preview page without affecting the actual original map extent.
1038 QgsRectangle mAtlasFeatureExtent;
1039
1040 // We have two images used for rendering/storing cached map images.
1041 // the first (mCacheFinalImage) is used ONLY for storing the most recent completed map render. It's always
1042 // used when drawing map item previews. The second (mCacheRenderingImage) is used temporarily while
1043 // rendering a new preview image in the background. If (and only if) the background render completes, then
1044 // mCacheRenderingImage is pushed into mCacheFinalImage, and used from then on when drawing the item preview.
1045 // This ensures that something is always shown in the map item, even while refreshing the preview image in the
1046 // background
1047 std::unique_ptr< QImage > mCacheFinalImage;
1048 std::unique_ptr< QImage > mCacheRenderingImage;
1049 bool mUpdatesEnabled = true;
1050
1052 bool mCacheInvalidated = true;
1053
1055 int mNumCachedLayers;
1056
1057 // Set to true if in state of drawing. Concurrent requests to draw method are returned if set to true
1058 bool mDrawing = false;
1059
1060 QTimer *mBackgroundUpdateTimer = nullptr;
1061 double mPreviewScaleFactor = 0;
1062 double mPreviewDevicePixelRatio = 1.0;
1063
1064 bool mDrawingPreview = false;
1065
1067 double mXOffset = 0.0;
1069 double mYOffset = 0.0;
1070
1071 double mLastRenderedImageOffsetX = 0.0;
1072 double mLastRenderedImageOffsetY = 0.0;
1073
1075 double mMapRotation = 0;
1076
1081 double mEvaluatedMapRotation = 0;
1082
1083 bool mZRangeEnabled = false;
1084 QgsDoubleRange mZRange;
1085
1087 bool mKeepLayerSet = false;
1088
1090 QList< QgsMapLayerRef > mLayers;
1091
1092 bool mKeepLayerStyles = false;
1094 QMap<QString, QString> mLayerStyleOverrides;
1095
1097 mutable QString mCachedLayerStyleOverridesPresetName;
1099 mutable QMap<QString, QString> mCachedPresetLayerStyleOverrides;
1100
1106 bool mFollowVisibilityPreset = false;
1107
1112 QString mFollowVisibilityPresetName;
1113
1115 QString mLastEvaluatedThemeName;
1116
1124 void drawMap( QPainter *painter, const QgsRectangle &extent, QSizeF size, double dpi );
1125
1127 void connectUpdateSlot();
1128
1130 void syncLayerSet();
1131
1133 const QgsLayoutItemMapGrid *constFirstMapGrid() const;
1134
1136 const QgsLayoutItemMapOverview *constFirstMapOverview() const;
1137
1142 QList< QgsLabelBlockingRegion > createLabelBlockingRegions( const QgsMapSettings &mapSettings ) const;
1143
1145 QRectF mCurrentRectangle;
1147 bool mDrawAnnotations = true;
1148
1150 bool mAtlasDriven = false;
1152 AtlasScalingMode mAtlasScalingMode = Auto;
1154 double mAtlasMargin = 0.10;
1155
1156 std::unique_ptr< QPainter > mPainter;
1157 std::unique_ptr< QgsMapRendererCustomPainterJob > mPainterJob;
1158 bool mPainterCancelWait = false;
1159
1160 QgsLayoutMeasurement mLabelMargin{ 0 };
1161 QgsLayoutMeasurement mEvaluatedLabelMargin{ 0 };
1162
1163 QStringList mBlockingLabelItemUuids;
1164 QList< QPointer< QgsLayoutItem > > mBlockingLabelItems;
1165
1167 QgsMapRendererJob::Errors mRenderingErrors;
1168
1169 QList< QgsRenderedFeatureHandlerInterface * > mRenderedFeatureHandlers;
1170
1171 std::unique_ptr< QgsMapRendererStagedRenderJob > mStagedRendererJob;
1172
1173 std::unique_ptr< QgsLabelingResults > mPreviewLabelingResults;
1174 std::unique_ptr< QgsLabelingResults > mExportLabelingResults;
1175
1176 void init();
1177
1179 void updateToolTip();
1180
1181 QString themeToRender( const QgsExpressionContext &context ) const;
1182
1184 QMap<QString, QString> layerStyleOverridesToRender( const QgsExpressionContext &context ) const;
1185
1187 QgsRectangle transformedExtent() const;
1188
1190 void mapPolygon( const QgsRectangle &extent, QPolygonF &poly ) const;
1191
1197 void transformShift( double &xShift, double &yShift ) const;
1198
1199 void drawAnnotations( QPainter *painter );
1200 void drawAnnotation( const QgsAnnotation *item, QgsRenderContext &context );
1201 QPointF layoutMapPosForItem( const QgsAnnotation *item ) const;
1202
1203 void drawMapFrame( QPainter *p );
1204 void drawMapBackground( QPainter *p );
1205
1206 enum PartType
1207 {
1208 Start,
1209 Background,
1210 Layer,
1211 Grid,
1212 OverviewMapExtent,
1213 Frame,
1214 SelectionBoxes,
1215 End,
1216 NotLayered,
1217 };
1218
1220 bool shouldDrawPart( PartType part ) const;
1221
1222 PartType mCurrentExportPart = NotLayered;
1223 QStringList mExportThemes;
1224 QStringList::iterator mExportThemeIt;
1225
1226 QgsLayoutItemMapAtlasClippingSettings *mAtlasClippingSettings = nullptr;
1227 QgsLayoutItemMapItemClipPathSettings *mItemClippingSettings = nullptr;
1228
1233 void refreshMapExtents( const QgsExpressionContext *context = nullptr );
1234
1235 void refreshLabelMargin( bool updateItem );
1236
1237 QgsRectangle computeAtlasRectangle();
1238
1239 void createStagedRenderJob( const QgsRectangle &extent, const QSizeF size, double dpi );
1240
1241 QPolygonF calculateVisibleExtentPolygon( bool includeClipping ) const;
1242
1246 std::map<QString, std::unique_ptr<QgsGroupLayer>> mGroupLayers;
1247
1251 friend class TestQgsLayoutMap;
1253 friend class QgsGeospatialPdfRenderedFeatureHandler;
1254 friend class QgsLayoutExporter;
1255
1256};
1257
1259
1260#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.