QGIS API Documentation  2.99.0-Master (7fe5405)
qgscomposermap.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgscomposermap.h
3  -------------------
4  begin : January 2005
5  copyright : (C) 2005 by Radim Blazek
6  email : [email protected]
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 #ifndef QGSCOMPOSERMAP_H
18 #define QGSCOMPOSERMAP_H
19 
20 //#include "ui_qgscomposermapbase.h"
21 #include "qgis_core.h"
22 #include "qgscomposeritem.h"
23 #include "qgsrectangle.h"
25 #include <QFont>
26 #include <QGraphicsRectItem>
27 
28 class QgsComposition;
32 class QgsComposerMapGrid;
33 class QgsMapToPixel;
34 class QDomNode;
35 class QDomDocument;
36 class QGraphicsView;
37 class QPainter;
38 class QgsFillSymbol;
39 class QgsLineSymbol;
40 class QgsVectorLayer;
41 class QgsAnnotation;
42 
48 class CORE_EXPORT QgsComposerMap : public QgsComposerItem
49 {
50  Q_OBJECT
51 
52  public:
54  QgsComposerMap( QgsComposition *composition, int x, int y, int width, int height );
56  QgsComposerMap( QgsComposition *composition );
57  virtual ~QgsComposerMap();
58 
60  virtual int type() const override { return ComposerMap; }
61 
64  {
65  Cache = 0, // Use raster cache
66  Render, // Render the map
67  Rectangle // Display only rectangle
68  };
69 
73  {
75  Predefined,
80  Auto
83  };
84 
92  void draw( QPainter *painter, const QgsRectangle& extent, QSizeF size, double dpi, double* forceWidthScale = nullptr );
93 
95  void paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget ) override;
96 
98  void cache();
99 
102  QgsMapSettings mapSettings( const QgsRectangle& extent, QSizeF size, int dpi ) const;
103 
105  int id() const {return mId;}
106 
108  bool isDrawing() const {return mDrawing;}
109 
111  void resize( double dx, double dy );
112 
117  void moveContent( double dx, double dy ) override;
118 
125  virtual void zoomContent( const double factor, const QPointF point, const ZoomMode mode = QgsComposerItem::Zoom ) override;
126 
128  void setSceneRect( const QRectF& rectangle ) override;
129 
131  double scale() const;
132 
134  void setNewScale( double scaleDenominator, bool forceUpdate = true );
135 
142  void setNewExtent( const QgsRectangle& extent );
143 
152  void zoomToExtent( const QgsRectangle& extent );
153 
157  void setNewAtlasFeatureExtent( const QgsRectangle& extent );
158 
165  const QgsRectangle* currentMapExtent() const;
166 
168  QgsRectangle* currentMapExtent();
169 
179  QgsCoordinateReferenceSystem crs() const;
180 
190  QgsCoordinateReferenceSystem presetCrs() const { return mCrs; }
191 
200  void setCrs( const QgsCoordinateReferenceSystem& crs );
201 
202  PreviewMode previewMode() const {return mPreviewMode;}
203  void setPreviewMode( PreviewMode m );
204 
206  bool keepLayerSet() const {return mKeepLayerSet;}
208  void setKeepLayerSet( bool enabled ) {mKeepLayerSet = enabled;}
209 
211  QList<QgsMapLayer*> layers() const;
213  void setLayers( const QList<QgsMapLayer*> layers );
214 
216  bool keepLayerStyles() const { return mKeepLayerStyles; }
218  void setKeepLayerStyles( bool enabled ) { mKeepLayerStyles = enabled; }
219 
221  QMap<QString, QString> layerStyleOverrides() const { return mLayerStyleOverrides; }
223  void setLayerStyleOverrides( const QMap<QString, QString>& overrides );
225  void storeCurrentLayerStyles();
226 
236  bool followVisibilityPreset() const { return mFollowVisibilityPreset; }
237 
240  void setFollowVisibilityPreset( bool follow ) { mFollowVisibilityPreset = follow; }
241 
245  QString followVisibilityPresetName() const { return mFollowVisibilityPresetName; }
246 
249  void setFollowVisibilityPresetName( const QString& name ) { mFollowVisibilityPresetName = name; }
250 
251  // Set cache outdated
252  void setCacheUpdated( bool u = false );
253 
254  QgsRectangle extent() const {return mExtent;}
255 
257  void setOffset( double xOffset, double yOffset );
258 
260  bool containsWmsLayer() const;
261 
263  bool containsAdvancedEffects() const;
264 
269  bool writeXml( QDomElement& elem, QDomDocument & doc ) const override;
270 
275  bool readXml( const QDomElement& itemElem, const QDomDocument& doc ) override;
276 
283  QgsComposerMapGridStack* grids() { return mGridStack; }
284 
290  QgsComposerMapGrid* grid();
291 
298  QgsComposerMapOverviewStack* overviews() { return mOverviewStack; }
299 
305  QgsComposerMapOverview* overview();
306 
308  QRectF boundingRect() const override;
309 
310  /* reimplement setFrameOutlineWidth, so that updateBoundingRect() is called after setting the frame width */
311  virtual void setFrameOutlineWidth( const double outlineWidth ) override;
312 
317  void setMapRotation( double r );
318 
325  double mapRotation( QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue ) const;
326 
327  void updateItem() override;
328 
330  void setMapCanvas( QGraphicsView* canvas ) { mMapCanvas = canvas; }
331 
332  void setDrawCanvasItems( bool b ) { mDrawCanvasItems = b; }
333  bool drawCanvasItems() const { return mDrawCanvasItems; }
334 
336  double mapUnitsToMM() const;
337 
340  void assignFreeId();
341 
347  bool atlasDriven() const { return mAtlasDriven; }
348 
354  void setAtlasDriven( bool enabled );
355 
364  AtlasScalingMode atlasScalingMode() const { return mAtlasScalingMode; }
365 
374  void setAtlasScalingMode( AtlasScalingMode mode ) { mAtlasScalingMode = mode; }
375 
385  double atlasMargin( const QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue );
386 
393  void setAtlasMargin( double margin ) { mAtlasMargin = margin; }
394 
396  void setUpdatesEnabled( bool enabled ) { mUpdatesEnabled = enabled; }
397 
399  bool updatesEnabled() const { return mUpdatesEnabled; }
400 
406  int numberExportLayers() const override;
407 
414  QPolygonF visibleExtentPolygon() const;
415 
416  //overridden to show "Map 1" type names
417  virtual QString displayName() const override;
418 
420  QPolygonF transformedMapPolygon() const;
421 
423  QPointF mapToItemCoords( QPointF mapCoords ) const;
424 
427  void requestedExtent( QgsRectangle& extent ) const;
428 
429  virtual QgsExpressionContext createExpressionContext() const override;
430 
431  signals:
432  void extentChanged();
433 
435  void mapRotationChanged( double newRotation );
436 
438  void preparedForAtlas();
439 
444  void layerStyleOverridesChanged();
445 
446  public slots:
447 
449  void updateCachedImage();
450 
454  void renderModeUpdateCachedImage();
455 
457  void updateBoundingRect();
458 
459  virtual void refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties, const QgsExpressionContext* context = nullptr ) override;
460 
461  private slots:
462  void layersAboutToBeRemoved( QList<QgsMapLayer*> layers );
463 
464  private:
465 
467  int mId;
468 
469  QgsComposerMapGridStack* mGridStack;
470 
471  QgsComposerMapOverviewStack* mOverviewStack;
472 
473  // Map region in map units really used for rendering
474  // It can be the same as mUserExtent, but it can be bigger in on dimension if mCalculate==Scale,
475  // so that full rectangle in paper is used.
476  QgsRectangle mExtent;
477 
480 
481  // Current temporary map region in map units. This is overwritten when atlas feature changes. It's also
482  // used when the user changes the map extent and an atlas preview is enabled. This allows the user
483  // to manually tweak each atlas preview page without affecting the actual original map extent.
484  QgsRectangle mAtlasFeatureExtent;
485 
486  // Cache used in composer preview
487  QImage mCacheImage;
488 
489  // Is cache up to date
490  bool mCacheUpdated;
491 
493  PreviewMode mPreviewMode;
494 
496  int mNumCachedLayers;
497 
499  bool mDrawing;
500 
502  double mXOffset;
504  double mYOffset;
505 
507  double mMapRotation;
508 
511  double mEvaluatedMapRotation;
512 
514  bool mKeepLayerSet;
515 
517  QList< QPointer<QgsMapLayer> > mLayers;
518 
519  bool mKeepLayerStyles;
521  QMap<QString, QString> mLayerStyleOverrides;
522 
526  bool mFollowVisibilityPreset;
527 
530  QString mFollowVisibilityPresetName;
531 
533  bool mUpdatesEnabled;
534 
536  void connectUpdateSlot();
537 
539  void syncLayerSet();
540 
542  const QgsComposerMapGrid* constFirstMapGrid() const;
543 
545  const QgsComposerMapOverview* constFirstMapOverview() const;
546 
548  QRectF mCurrentRectangle;
549  QGraphicsView* mMapCanvas;
551  bool mDrawCanvasItems;
552 
555  void adjustExtentToItemShape( double itemWidth, double itemHeight, QgsRectangle& extent ) const;
556 
558  bool mAtlasDriven;
560  AtlasScalingMode mAtlasScalingMode;
562  double mAtlasMargin;
563 
564  void init();
565 
567  void updateToolTip();
568 
570  QList<QgsMapLayer*> layersToRender( const QgsExpressionContext* context = nullptr ) const;
571 
573  QMap<QString, QString> layerStyleOverridesToRender( const QgsExpressionContext& context ) const;
574 
576  QgsRectangle transformedExtent() const;
577 
579  void mapPolygon( const QgsRectangle& extent, QPolygonF& poly ) const;
580 
584  void transformShift( double& xShift, double& yShift ) const;
585 
586  void drawCanvasItems( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
587  void drawCanvasItem( const QgsAnnotation* item, QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
588  QPointF composerMapPosForItem( const QgsAnnotation* item ) const;
589 
590  enum PartType
591  {
592  Background,
593  Layer,
594  Grid,
595  OverviewMapExtent,
596  Frame,
597  SelectionBoxes
598  };
599 
601  bool shouldDrawPart( PartType part ) const;
602 
607  void refreshMapExtents( const QgsExpressionContext* context = nullptr );
608 
609  friend class QgsComposerMapOverview; //to access mXOffset, mYOffset
610  friend class TestQgsComposerMap;
611 };
612 
613 #endif
614 
AtlasScalingMode
Scaling modes used for the serial rendering (atlas)
bool atlasDriven() const
Returns whether the map extent is set to follow the current atlas feature.
A rectangle specified with double values.
Definition: qgsrectangle.h:36
virtual QString displayName() const
Get item display name.
bool drawCanvasItems() const
QgsComposerMapOverviewStack * overviews()
Returns the map item&#39;s overview stack, which is used to control how overviews are drawn over the map&#39;...
bool followVisibilityPreset() const
Whether the map should follow a map theme.
QgsRectangle extent() const
ZoomMode
Modes for zooming item content.
A collection of grids which is drawn above the map content in a QgsComposerMap.
A item that forms part of a map composition.
Zoom to center of content.
bool updatesEnabled() const
Returns whether updates to the composer map are enabled.
virtual int numberExportLayers() const
Get the number of layers that this item requires for exporting as layers.
int id() const
Get identification number.
void setMapCanvas(QGraphicsView *canvas)
Sets canvas pointer (necessary to query and draw map canvas items)
The current scale of the map is used for each feature of the atlas.
virtual void zoomContent(const double factor, const QPointF point, const ZoomMode mode=QgsComposerItem::Zoom)
Zoom content of item.
QgsCoordinateReferenceSystem presetCrs() const
Returns the map&#39;s preset coordinate reference system.
DataDefinedProperty
Data defined properties for different item types.
An interface for annotation items which are drawn over a map.
Definition: qgsannotation.h:41
void setKeepLayerStyles(bool enabled)
Setter for flag that determines if current styles of layers should be overridden by previously stored...
The QgsMapSettings class contains configuration for rendering of the map.
void setAtlasMargin(double margin)
Sets the margin size (percentage) used when the map is in atlas mode.
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:34
virtual void moveContent(double dx, double dy)
Move Content of item.
void setAtlasScalingMode(AtlasScalingMode mode)
Sets the current atlas scaling mode.
An individual overview which is drawn above the map content in a QgsComposerMap, and shows the extent...
virtual void updateItem()
Updates item, with the possibility to do custom update for subclasses.
void setFollowVisibilityPreset(bool follow)
Sets whether the map should follow a map theme.
virtual bool readXml(const QDomElement &itemElem, const QDomDocument &doc)
Sets item state from DOM element.
virtual int type() const override
Return correct graphics item type.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
virtual void refreshDataDefinedProperty(const QgsComposerObject::DataDefinedProperty property=QgsComposerObject::AllProperties, const QgsExpressionContext *context=nullptr) override
Refreshes a data defined property for the item by reevaluating the property&#39;s value and redrawing the...
QMap< QString, QString > layerStyleOverrides() const
Getter for stored overrides of styles for layers.
An individual grid which is drawn above the map content in a QgsComposerMap.
PropertyValueType
Specifies whether the value returned by a function should be the original, user set value...
bool keepLayerStyles() const
Getter for flag that determines if current styles of layers should be overridden by previously stored...
QString followVisibilityPresetName() const
Preset name that decides which layers and layer styles are used for map rendering.
bool isDrawing() const
True if a draw is already in progress.
PreviewMode
Preview style.
Graphics scene for map printing.
Object representing map window.
Return the current evaluated value for the property.
void setUpdatesEnabled(bool enabled)
Sets whether updates to the composer map are enabled.
virtual QgsExpressionContext createExpressionContext() const override
Creates an expression context relating to the item&#39;s current state.
virtual void setFrameOutlineWidth(const double outlineWidth)
Sets frame outline width.
AtlasScalingMode atlasScalingMode() const
Returns the current atlas scaling mode.
PreviewMode previewMode() const
QgsComposerMapGridStack * grids()
Returns the map item&#39;s grid stack, which is used to control how grids are drawn over the map&#39;s conten...
void setDrawCanvasItems(bool b)
This class represents a coordinate reference system (CRS).
virtual void setSceneRect(const QRectF &rectangle)
Sets this items bound in scene coordinates such that 1 item size units corresponds to 1 scene size un...
void setKeepLayerSet(bool enabled)
Setter for flag that determines if the stored layer set should be used or the current layer set of th...
virtual bool writeXml(QDomElement &elem, QDomDocument &doc) const
Stores item state in DOM element.
Represents a vector layer which manages a vector based data sets.
bool keepLayerSet() const
Getter for flag that determines if the stored layer set should be used or the current layer set of th...
All properties for item.
void setFollowVisibilityPresetName(const QString &name)
Sets preset name for map rendering.
A collection of overviews which are drawn above the map content in a QgsComposerMap.