QGIS API Documentation  2.99.0-Master (8ec3eaf)
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 "qgscomposeritem.h"
22 #include "qgsrectangle.h"
23 #include <QFont>
24 #include <QGraphicsRectItem>
25 
26 class QgsComposition;
30 class QgsComposerMapGrid;
31 class QgsMapToPixel;
32 class QDomNode;
33 class QDomDocument;
34 class QGraphicsView;
35 class QPainter;
36 class QgsFillSymbol;
37 class QgsLineSymbol;
38 class QgsVectorLayer;
39 class QgsAnnotation;
40 
46 class CORE_EXPORT QgsComposerMap : public QgsComposerItem
47 {
48  Q_OBJECT
49 
50  public:
52  QgsComposerMap( QgsComposition *composition, int x, int y, int width, int height );
54  QgsComposerMap( QgsComposition *composition );
55  virtual ~QgsComposerMap();
56 
58  virtual int type() const override { return ComposerMap; }
59 
62  {
63  Cache = 0, // Use raster cache
64  Render, // Render the map
65  Rectangle // Display only rectangle
66  };
67 
71  {
73  Predefined,
78  Auto
81  };
82 
90  void draw( QPainter *painter, const QgsRectangle& extent, QSizeF size, double dpi, double* forceWidthScale = nullptr );
91 
93  void paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget ) override;
94 
96  void cache();
97 
100  QgsMapSettings mapSettings( const QgsRectangle& extent, QSizeF size, int dpi ) const;
101 
103  int id() const {return mId;}
104 
106  bool isDrawing() const {return mDrawing;}
107 
109  void resize( double dx, double dy );
110 
115  void moveContent( double dx, double dy ) override;
116 
123  virtual void zoomContent( const double factor, const QPointF point, const ZoomMode mode = QgsComposerItem::Zoom ) override;
124 
126  void setSceneRect( const QRectF& rectangle ) override;
127 
129  double scale() const;
130 
132  void setNewScale( double scaleDenominator, bool forceUpdate = true );
133 
140  void setNewExtent( const QgsRectangle& extent );
141 
150  void zoomToExtent( const QgsRectangle& extent );
151 
155  void setNewAtlasFeatureExtent( const QgsRectangle& extent );
156 
163  const QgsRectangle* currentMapExtent() const;
164 
166  QgsRectangle* currentMapExtent();
167 
168  PreviewMode previewMode() const {return mPreviewMode;}
169  void setPreviewMode( PreviewMode m );
170 
172  bool keepLayerSet() const {return mKeepLayerSet;}
174  void setKeepLayerSet( bool enabled ) {mKeepLayerSet = enabled;}
175 
177  QStringList layerSet() const {return mLayerSet;}
179  void setLayerSet( const QStringList& layerSet ) {mLayerSet = layerSet;}
181  void storeCurrentLayerSet();
182 
184  bool keepLayerStyles() const { return mKeepLayerStyles; }
186  void setKeepLayerStyles( bool enabled ) { mKeepLayerStyles = enabled; }
187 
189  QMap<QString, QString> layerStyleOverrides() const { return mLayerStyleOverrides; }
191  void setLayerStyleOverrides( const QMap<QString, QString>& overrides );
193  void storeCurrentLayerStyles();
194 
204  bool followVisibilityPreset() const { return mFollowVisibilityPreset; }
205 
208  void setFollowVisibilityPreset( bool follow ) { mFollowVisibilityPreset = follow; }
209 
213  QString followVisibilityPresetName() const { return mFollowVisibilityPresetName; }
214 
217  void setFollowVisibilityPresetName( const QString& name ) { mFollowVisibilityPresetName = name; }
218 
219  // Set cache outdated
220  void setCacheUpdated( bool u = false );
221 
222  QgsRectangle extent() const {return mExtent;}
223 
225  void setOffset( double xOffset, double yOffset );
226 
228  bool containsWmsLayer() const;
229 
231  bool containsAdvancedEffects() const;
232 
237  bool writeXml( QDomElement& elem, QDomDocument & doc ) const override;
238 
243  bool readXml( const QDomElement& itemElem, const QDomDocument& doc ) override;
244 
251  QgsComposerMapGridStack* grids() { return mGridStack; }
252 
258  QgsComposerMapGrid* grid();
259 
266  QgsComposerMapOverviewStack* overviews() { return mOverviewStack; }
267 
273  QgsComposerMapOverview* overview();
274 
276  QRectF boundingRect() const override;
277 
278  /* reimplement setFrameOutlineWidth, so that updateBoundingRect() is called after setting the frame width */
279  virtual void setFrameOutlineWidth( const double outlineWidth ) override;
280 
285  void setMapRotation( double r );
286 
293  double mapRotation( QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue ) const;
294 
295  void updateItem() override;
296 
298  void setMapCanvas( QGraphicsView* canvas ) { mMapCanvas = canvas; }
299 
300  void setDrawCanvasItems( bool b ) { mDrawCanvasItems = b; }
301  bool drawCanvasItems() const { return mDrawCanvasItems; }
302 
304  double mapUnitsToMM() const;
305 
308  void assignFreeId();
309 
315  bool atlasDriven() const { return mAtlasDriven; }
316 
322  void setAtlasDriven( bool enabled );
323 
332  AtlasScalingMode atlasScalingMode() const { return mAtlasScalingMode; }
333 
342  void setAtlasScalingMode( AtlasScalingMode mode ) { mAtlasScalingMode = mode; }
343 
353  double atlasMargin( const QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue );
354 
361  void setAtlasMargin( double margin ) { mAtlasMargin = margin; }
362 
364  void setUpdatesEnabled( bool enabled ) { mUpdatesEnabled = enabled; }
365 
367  bool updatesEnabled() const { return mUpdatesEnabled; }
368 
374  int numberExportLayers() const override;
375 
382  QPolygonF visibleExtentPolygon() const;
383 
384  //overridden to show "Map 1" type names
385  virtual QString displayName() const override;
386 
388  QPolygonF transformedMapPolygon() const;
389 
391  QPointF mapToItemCoords( QPointF mapCoords ) const;
392 
395  void requestedExtent( QgsRectangle& extent ) const;
396 
397  virtual QgsExpressionContext createExpressionContext() const override;
398 
399  signals:
400  void extentChanged();
401 
403  void mapRotationChanged( double newRotation );
404 
406  void preparedForAtlas();
407 
412  void layerStyleOverridesChanged();
413 
414  public slots:
415 
417  void updateCachedImage();
418 
422  void renderModeUpdateCachedImage();
423 
425  void updateBoundingRect();
426 
427  virtual void refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties, const QgsExpressionContext* context = nullptr ) override;
428 
429  protected slots:
430 
435  void layersChanged();
436 
437  private:
438 
440  int mId;
441 
442  QgsComposerMapGridStack* mGridStack;
443 
444  QgsComposerMapOverviewStack* mOverviewStack;
445 
446  // Map region in map units realy used for rendering
447  // It can be the same as mUserExtent, but it can be bigger in on dimension if mCalculate==Scale,
448  // so that full rectangle in paper is used.
449  QgsRectangle mExtent;
450 
451  // Current temporary map region in map units. This is overwritten when atlas feature changes. It's also
452  // used when the user changes the map extent and an atlas preview is enabled. This allows the user
453  // to manually tweak each atlas preview page without affecting the actual original map extent.
454  QgsRectangle mAtlasFeatureExtent;
455 
456  // Cache used in composer preview
457  QImage mCacheImage;
458 
459  // Is cache up to date
460  bool mCacheUpdated;
461 
463  PreviewMode mPreviewMode;
464 
466  int mNumCachedLayers;
467 
469  bool mDrawing;
470 
472  double mXOffset;
474  double mYOffset;
475 
477  double mMapRotation;
478 
481  double mEvaluatedMapRotation;
482 
484  bool mKeepLayerSet;
485 
487  QStringList mLayerSet;
488 
489  bool mKeepLayerStyles;
491  QMap<QString, QString> mLayerStyleOverrides;
492 
496  bool mFollowVisibilityPreset;
497 
500  QString mFollowVisibilityPresetName;
501 
503  bool mUpdatesEnabled;
504 
506  void connectUpdateSlot();
507 
509  void syncLayerSet();
510 
512  const QgsComposerMapGrid* constFirstMapGrid() const;
513 
515  const QgsComposerMapOverview* constFirstMapOverview() const;
516 
518  QRectF mCurrentRectangle;
519  QGraphicsView* mMapCanvas;
521  bool mDrawCanvasItems;
522 
525  void adjustExtentToItemShape( double itemWidth, double itemHeight, QgsRectangle& extent ) const;
526 
528  bool mAtlasDriven;
530  AtlasScalingMode mAtlasScalingMode;
532  double mAtlasMargin;
533 
534  void init();
535 
537  void updateToolTip();
538 
540  QStringList layersToRender( const QgsExpressionContext* context = nullptr ) const;
541 
543  QMap<QString, QString> layerStyleOverridesToRender( const QgsExpressionContext& context ) const;
544 
546  QgsRectangle transformedExtent() const;
547 
549  void mapPolygon( const QgsRectangle& extent, QPolygonF& poly ) const;
550 
554  void transformShift( double& xShift, double& yShift ) const;
555 
556  void drawCanvasItems( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
557  void drawCanvasItem( const QgsAnnotation* item, QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
558  QPointF composerMapPosForItem( const QgsAnnotation* item ) const;
559 
560  enum PartType
561  {
562  Background,
563  Layer,
564  Grid,
565  OverviewMapExtent,
566  Frame,
567  SelectionBoxes
568  };
569 
571  bool shouldDrawPart( PartType part ) const;
572 
577  void refreshMapExtents( const QgsExpressionContext* context = nullptr );
578 
579  friend class QgsComposerMapOverview; //to access mXOffset, mYOffset
580  friend class TestQgsComposerMap;
581 };
582 
583 #endif
584 
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:35
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;...
QStringList layerSet() const
Getter for stored layer set that is used if mKeepLayerSet is true.
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.
DataDefinedProperty
Data defined properties for different item types.
An interface for annotation items which are drawn over a map.
Definition: qgsannotation.h:40
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:33
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
void setLayerSet(const QStringList &layerSet)
Setter for stored layer set that is used if mKeepLayerSet is true.
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)
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.