QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
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
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 Q_DECL_DEPRECATED int numberExportLayers() const override SIP_DEPRECATED;
401 void startLayeredExport() override;
402 void stopLayeredExport() override;
403 bool nextExportPart() override;
404 ExportLayerBehavior exportLayerBehavior() const override;
405 QgsLayoutItem::ExportLayerDetail exportLayerDetails() const override;
406 void setFrameStrokeWidth( QgsLayoutMeasurement width ) override;
407
413 double scale() const;
414
422 void setScale( double scale, bool forceUpdate = true );
423
431 void setExtent( const QgsRectangle &extent );
432
440 void zoomToExtent( const QgsRectangle &extent );
441
447 QgsRectangle extent() const;
448
449
457 QPolygonF visibleExtentPolygon() const;
458
468
477 QgsCoordinateReferenceSystem presetCrs() const { return mCrs; }
478
486 void setCrs( const QgsCoordinateReferenceSystem &crs );
487
496 bool keepLayerSet() const { return mKeepLayerSet; }
497
506 void setKeepLayerSet( bool enabled ) { mKeepLayerSet = enabled; }
507
514 QList<QgsMapLayer *> layers() const;
515
525 void setLayers( const QList<QgsMapLayer *> &layers );
526
531 bool keepLayerStyles() const { return mKeepLayerStyles; }
532
537 void setKeepLayerStyles( bool enabled ) { mKeepLayerStyles = enabled; }
538
543 QMap<QString, QString> layerStyleOverrides() const { return mLayerStyleOverrides; }
544
549 void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
550
554 void storeCurrentLayerStyles();
555
566 bool followVisibilityPreset() const { return mFollowVisibilityPreset; }
567
571 void setFollowVisibilityPreset( bool follow );
572
580 QString followVisibilityPresetName() const { return mFollowVisibilityPresetName; }
581
588 void setFollowVisibilityPresetName( const QString &name );
589
590 void moveContent( double dx, double dy ) override;
591 void setMoveContentPreviewOffset( double dx, double dy ) override;
592
593 void zoomContent( double factor, QPointF point ) override;
594
595
597 bool containsWmsLayer() const;
598
599 bool requiresRasterization() const override;
600 bool containsAdvancedEffects() const override;
601
608 void setMapRotation( double rotation );
609
618 double mapRotation( QgsLayoutObject::PropertyValueType valueType = QgsLayoutObject::EvaluatedValue ) const;
619
624 void setDrawAnnotations( bool draw ) { mDrawAnnotations = draw; }
625
630 bool drawAnnotations() const { return mDrawAnnotations; }
631
632
639 bool atlasDriven() const { return mAtlasDriven; }
640
647 void setAtlasDriven( bool enabled );
648
658 AtlasScalingMode atlasScalingMode() const { return mAtlasScalingMode; }
659
669 void setAtlasScalingMode( AtlasScalingMode mode ) { mAtlasScalingMode = mode; }
670
682
690 void setAtlasMargin( double margin ) { mAtlasMargin = margin; }
691
697 QgsLayoutItemMapGridStack *grids() { return mGridStack.get(); }
698
703 QgsLayoutItemMapGrid *grid();
704
711 QgsLayoutItemMapOverviewStack *overviews() { return mOverviewStack.get(); }
712
718 QgsLayoutItemMapOverview *overview();
719
730 QgsLayoutMeasurement labelMargin() const;
731
742 void setLabelMargin( const QgsLayoutMeasurement &margin );
743
744 QgsExpressionContext createExpressionContext() const override;
745
751 double mapUnitsToLayoutUnits() const;
752
760 QgsMapSettings mapSettings( const QgsRectangle &extent, QSizeF size, double dpi, bool includeLayerSettings ) const;
761
762 void finalizeRestoreFromXml() override;
763
768 QList<QgsMapLayer *> layersToRender( const QgsExpressionContext *context = nullptr ) const;
769
781 void addLabelBlockingItem( QgsLayoutItem *item );
782
794 void removeLabelBlockingItem( QgsLayoutItem *item );
795
807 bool isLabelBlockingItem( QgsLayoutItem *item ) const;
808
813 QgsMapRendererJob::Errors renderingErrors() const { return mRenderingErrors; }
814
822 QgsLabelingResults *previewLabelingResults() const;
823
824 bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
825
838 void addRenderedFeatureHandler( QgsRenderedFeatureHandlerInterface *handler );
839
846 void removeRenderedFeatureHandler( QgsRenderedFeatureHandlerInterface *handler );
847
851 QTransform layoutToMapCoordsTransform() const;
852
859
866
867 // Reimplement estimatedFrameBleed to take the grid frame into account
868 double estimatedFrameBleed() const override;
869
870 protected:
871
872 void draw( QgsLayoutItemRenderContext &context ) override;
873 bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
874 bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
875 QPainterPath framePath() const override;
876
878 bool isDrawing() const {return mDrawing;}
879
880 // In case of annotations, the bounding rectangle can be larger than the map item rectangle
881 QRectF boundingRect() const override;
882
884 QPolygonF transformedMapPolygon() const;
885
887 QPointF mapToItemCoords( QPointF mapCoords ) const;
888
892 QgsRectangle requestedExtent() const;
893
894 signals:
895
902
908 void mapRotationChanged( double newRotation );
909
912
918
927 void themeChanged( const QString &theme );
928
935
942
943 public slots:
944
945 void refresh() override;
946
947 void invalidateCache() override;
948
950 void updateBoundingRect();
951
952 void refreshDataDefinedProperty( QgsLayoutObject::DataDefinedProperty property = QgsLayoutObject::AllProperties ) override;
953
954 private slots:
955 void layersAboutToBeRemoved( const QList<QgsMapLayer *> &layers );
956
957 void painterJobFinished();
958
959 void shapeChanged();
960
961 void mapThemeChanged( const QString &theme );
962
964 void currentMapThemeRenamed( const QString &theme, const QString &newTheme );
965
967 void recreateCachedImageInBackground();
968
969 void updateAtlasFeature();
970 private:
971
972 QgsLayoutItemMap::MapItemFlags mMapFlags = QgsLayoutItemMap::MapItemFlags();
973
975 int mMapId = 1;
976
977 std::unique_ptr< QgsLayoutItemMapGridStack > mGridStack;
978 std::unique_ptr< QgsLayoutItemMapOverviewStack > mOverviewStack;
979
980 // Map region in map units really used for rendering
981 // It can be the same as mUserExtent, but it can be bigger in on dimension if mCalculate==Scale,
982 // so that full rectangle in paper is used.
983 QgsRectangle mExtent;
984
987
988 // Current temporary map region in map units. This is overwritten when atlas feature changes. It's also
989 // used when the user changes the map extent and an atlas preview is enabled. This allows the user
990 // to manually tweak each atlas preview page without affecting the actual original map extent.
991 QgsRectangle mAtlasFeatureExtent;
992
993 // We have two images used for rendering/storing cached map images.
994 // the first (mCacheFinalImage) is used ONLY for storing the most recent completed map render. It's always
995 // used when drawing map item previews. The second (mCacheRenderingImage) is used temporarily while
996 // rendering a new preview image in the background. If (and only if) the background render completes, then
997 // mCacheRenderingImage is pushed into mCacheFinalImage, and used from then on when drawing the item preview.
998 // This ensures that something is always shown in the map item, even while refreshing the preview image in the
999 // background
1000 std::unique_ptr< QImage > mCacheFinalImage;
1001 std::unique_ptr< QImage > mCacheRenderingImage;
1002 bool mUpdatesEnabled = true;
1003
1005 bool mCacheInvalidated = true;
1006
1008 int mNumCachedLayers;
1009
1010 // Set to true if in state of drawing. Concurrent requests to draw method are returned if set to true
1011 bool mDrawing = false;
1012
1013 QTimer *mBackgroundUpdateTimer = nullptr;
1014 double mPreviewScaleFactor = 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:50
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_FACTORY
Definition qgis_sip.h:76
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsTextRendererUtils::CurvedTextFlags)
const QgsCoordinateReferenceSystem & crs