QGIS API Documentation 3.36.0-Maidenhead (09951dc0acf)
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
866 // Reimplement estimatedFrameBleed to take the grid frame into account
867 double estimatedFrameBleed() const override;
868
869 protected:
870
871 void draw( QgsLayoutItemRenderContext &context ) override;
872 bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
873 bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
874 QPainterPath framePath() const override;
875
877 bool isDrawing() const {return mDrawing;}
878
879 // In case of annotations, the bounding rectangle can be larger than the map item rectangle
880 QRectF boundingRect() const override;
881
883 QPolygonF transformedMapPolygon() const;
884
886 QPointF mapToItemCoords( QPointF mapCoords ) const;
887
891 QgsRectangle requestedExtent() const;
892
893 signals:
894
901
907 void mapRotationChanged( double newRotation );
908
911
917
926 void themeChanged( const QString &theme );
927
934
941
942 public slots:
943
944 void refresh() override;
945
946 void invalidateCache() override;
947
949 void updateBoundingRect();
950
951 void refreshDataDefinedProperty( QgsLayoutObject::DataDefinedProperty property = QgsLayoutObject::DataDefinedProperty::AllProperties ) override;
952
953 private slots:
954 void layersAboutToBeRemoved( const QList<QgsMapLayer *> &layers );
955
956 void painterJobFinished();
957
958 void shapeChanged();
959
960 void mapThemeChanged( const QString &theme );
961
963 void currentMapThemeRenamed( const QString &theme, const QString &newTheme );
964
966 void recreateCachedImageInBackground();
967
968 void updateAtlasFeature();
969 private:
970
971 QgsLayoutItemMap::MapItemFlags mMapFlags = QgsLayoutItemMap::MapItemFlags();
972
974 int mMapId = 1;
975
976 std::unique_ptr< QgsLayoutItemMapGridStack > mGridStack;
977 std::unique_ptr< QgsLayoutItemMapOverviewStack > mOverviewStack;
978
979 // Map region in map units really used for rendering
980 // It can be the same as mUserExtent, but it can be bigger in on dimension if mCalculate==Scale,
981 // so that full rectangle in paper is used.
982 QgsRectangle mExtent;
983
986
987 // Current temporary map region in map units. This is overwritten when atlas feature changes. It's also
988 // used when the user changes the map extent and an atlas preview is enabled. This allows the user
989 // to manually tweak each atlas preview page without affecting the actual original map extent.
990 QgsRectangle mAtlasFeatureExtent;
991
992 // We have two images used for rendering/storing cached map images.
993 // the first (mCacheFinalImage) is used ONLY for storing the most recent completed map render. It's always
994 // used when drawing map item previews. The second (mCacheRenderingImage) is used temporarily while
995 // rendering a new preview image in the background. If (and only if) the background render completes, then
996 // mCacheRenderingImage is pushed into mCacheFinalImage, and used from then on when drawing the item preview.
997 // This ensures that something is always shown in the map item, even while refreshing the preview image in the
998 // background
999 std::unique_ptr< QImage > mCacheFinalImage;
1000 std::unique_ptr< QImage > mCacheRenderingImage;
1001 bool mUpdatesEnabled = true;
1002
1004 bool mCacheInvalidated = true;
1005
1007 int mNumCachedLayers;
1008
1009 // Set to true if in state of drawing. Concurrent requests to draw method are returned if set to true
1010 bool mDrawing = false;
1011
1012 QTimer *mBackgroundUpdateTimer = nullptr;
1013 double mPreviewScaleFactor = 0;
1014 double mPreviewDevicePixelRatio = 1.0;
1015
1016 bool mDrawingPreview = false;
1017
1019 double mXOffset = 0.0;
1021 double mYOffset = 0.0;
1022
1023 double mLastRenderedImageOffsetX = 0.0;
1024 double mLastRenderedImageOffsetY = 0.0;
1025
1027 double mMapRotation = 0;
1028
1033 double mEvaluatedMapRotation = 0;
1034
1036 bool mKeepLayerSet = false;
1037
1039 QList< QgsMapLayerRef > mLayers;
1040
1041 bool mKeepLayerStyles = false;
1043 QMap<QString, QString> mLayerStyleOverrides;
1044
1046 mutable QString mCachedLayerStyleOverridesPresetName;
1048 mutable QMap<QString, QString> mCachedPresetLayerStyleOverrides;
1049
1055 bool mFollowVisibilityPreset = false;
1056
1061 QString mFollowVisibilityPresetName;
1062
1064 QString mLastEvaluatedThemeName;
1065
1073 void drawMap( QPainter *painter, const QgsRectangle &extent, QSizeF size, double dpi );
1074
1076 void connectUpdateSlot();
1077
1079 void syncLayerSet();
1080
1082 const QgsLayoutItemMapGrid *constFirstMapGrid() const;
1083
1085 const QgsLayoutItemMapOverview *constFirstMapOverview() const;
1086
1091 QList< QgsLabelBlockingRegion > createLabelBlockingRegions( const QgsMapSettings &mapSettings ) const;
1092
1094 QRectF mCurrentRectangle;
1096 bool mDrawAnnotations = true;
1097
1099 bool mAtlasDriven = false;
1101 AtlasScalingMode mAtlasScalingMode = Auto;
1103 double mAtlasMargin = 0.10;
1104
1105 std::unique_ptr< QPainter > mPainter;
1106 std::unique_ptr< QgsMapRendererCustomPainterJob > mPainterJob;
1107 bool mPainterCancelWait = false;
1108
1109 QgsLayoutMeasurement mLabelMargin{ 0 };
1110 QgsLayoutMeasurement mEvaluatedLabelMargin{ 0 };
1111
1112 QStringList mBlockingLabelItemUuids;
1113 QList< QPointer< QgsLayoutItem > > mBlockingLabelItems;
1114
1116 QgsMapRendererJob::Errors mRenderingErrors;
1117
1118 QList< QgsRenderedFeatureHandlerInterface * > mRenderedFeatureHandlers;
1119
1120 std::unique_ptr< QgsMapRendererStagedRenderJob > mStagedRendererJob;
1121
1122 std::unique_ptr< QgsLabelingResults > mPreviewLabelingResults;
1123 std::unique_ptr< QgsLabelingResults > mExportLabelingResults;
1124
1125 void init();
1126
1128 void updateToolTip();
1129
1130 QString themeToRender( const QgsExpressionContext &context ) const;
1131
1133 QMap<QString, QString> layerStyleOverridesToRender( const QgsExpressionContext &context ) const;
1134
1136 QgsRectangle transformedExtent() const;
1137
1139 void mapPolygon( const QgsRectangle &extent, QPolygonF &poly ) const;
1140
1146 void transformShift( double &xShift, double &yShift ) const;
1147
1148 void drawAnnotations( QPainter *painter );
1149 void drawAnnotation( const QgsAnnotation *item, QgsRenderContext &context );
1150 QPointF layoutMapPosForItem( const QgsAnnotation *item ) const;
1151
1152 void drawMapFrame( QPainter *p );
1153 void drawMapBackground( QPainter *p );
1154
1155 enum PartType
1156 {
1157 Start,
1158 Background,
1159 Layer,
1160 Grid,
1161 OverviewMapExtent,
1162 Frame,
1163 SelectionBoxes,
1164 End,
1165 NotLayered,
1166 };
1167
1169 bool shouldDrawPart( PartType part ) const;
1170
1171 PartType mCurrentExportPart = NotLayered;
1172 QStringList mExportThemes;
1173 QStringList::iterator mExportThemeIt;
1174
1175 QgsLayoutItemMapAtlasClippingSettings *mAtlasClippingSettings = nullptr;
1176 QgsLayoutItemMapItemClipPathSettings *mItemClippingSettings = nullptr;
1177
1182 void refreshMapExtents( const QgsExpressionContext *context = nullptr );
1183
1184 void refreshLabelMargin( bool updateItem );
1185
1186 QgsRectangle computeAtlasRectangle();
1187
1188 void createStagedRenderJob( const QgsRectangle &extent, const QSizeF size, double dpi );
1189
1190 QPolygonF calculateVisibleExtentPolygon( bool includeClipping ) const;
1191
1195 std::map<QString, std::unique_ptr<QgsGroupLayer>> mGroupLayers;
1196
1200 friend class TestQgsLayoutMap;
1202 friend class QgsGeoPdfRenderedFeatureHandler;
1203 friend class QgsLayoutExporter;
1204
1205};
1206
1207Q_DECLARE_OPERATORS_FOR_FLAGS( QgsLayoutItemMap::MapItemFlags )
1208
1209#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).
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.
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.
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