QGIS API Documentation  2.15.0-Master (972fc9f)
qgsmaprenderer.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmaprender.h - class for rendering map layer set
3  ----------------------
4  begin : January 2006
5  copyright : (C) 2006 by Martin Dobias
6  email : wonder.sk at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #ifndef QGSMAPRENDER_H
17 #define QGSMAPRENDER_H
18 
19 #include <QMutex>
20 #include <QSize>
21 #include <QStringList>
22 #include <QVector>
23 #include <QPainter>
24 
25 #include "qgis.h"
26 #include "qgsrectangle.h"
27 #include "qgsrendercontext.h"
28 #include "qgsfeature.h"
29 #include "qgsmapsettings.h"
30 
31 class QDomDocument;
32 class QDomNode;
33 class QPainter;
34 
35 class QgsMapToPixel;
36 class QgsMapLayer;
37 class QgsMapRenderer;
38 class QgsScaleCalculator;
40 class QgsDistanceArea;
41 class QgsVectorLayer;
42 
45 
46 class CORE_EXPORT QgsLabelPosition
47 {
48  public:
49  QgsLabelPosition( int id, double r, const QVector< QgsPoint >& corners, const QgsRectangle& rect, double w, double h, const QString& layer, const QString& labeltext, const QFont& labelfont, bool upside_down, bool diagram = false, bool pinned = false, const QString& providerId = QString() )
50  : featureId( id )
51  , rotation( r )
52  , cornerPoints( corners )
53  , labelRect( rect )
54  , width( w )
55  , height( h )
56  , layerID( layer )
57  , labelText( labeltext )
58  , labelFont( labelfont )
59  , upsideDown( upside_down )
60  , isDiagram( diagram )
61  , isPinned( pinned )
62  , providerID( providerId )
63  {}
65  : featureId( -1 )
66  , rotation( 0 )
67  , labelRect( QgsRectangle() )
68  , width( 0 )
69  , height( 0 )
70  , layerID( "" )
71  , labelText( "" )
72  , labelFont( QFont() )
73  , upsideDown( false )
74  , isDiagram( false )
75  , isPinned( false )
76  {}
77  int featureId;
78  double rotation;
81  double width;
82  double height;
86  bool upsideDown;
87  bool isDiagram;
88  bool isPinned;
91 };
92 
94 class CORE_EXPORT QgsLabelingEngineInterface
95 {
96  public:
97 
99 
102  Q_DECL_DEPRECATED virtual void init( QgsMapRenderer *mp ) = 0;
104  virtual void init( const QgsMapSettings& mapSettings ) = 0;
106  virtual bool willUseLayer( QgsVectorLayer* layer ) = 0;
108  virtual void clearActiveLayers() = 0;
110  virtual void clearActiveLayer( const QString& layerID ) = 0;
112  virtual int prepareLayer( QgsVectorLayer* layer, QStringList& attrNames, QgsRenderContext& ctx ) = 0;
115  Q_DECL_DEPRECATED virtual QgsPalLayerSettings &layer( const QString &layerName ) = 0;
118  virtual int prepareDiagramLayer( QgsVectorLayer *layer, QStringList &attrNames, QgsRenderContext &ctx )
119  { Q_UNUSED( layer ); Q_UNUSED( attrNames ); Q_UNUSED( ctx ); return 0; }
122  Q_DECL_DEPRECATED virtual int addDiagramLayer( QgsVectorLayer *layer, const QgsDiagramLayerSettings *s )
123  { Q_UNUSED( layer ); Q_UNUSED( s ); return 0; }
125  virtual void registerFeature( const QString &layerID, QgsFeature &feat, QgsRenderContext &context ) = 0;
127  virtual void registerDiagramFeature( const QString &layerID, QgsFeature &feat, QgsRenderContext &context )
128  { Q_UNUSED( layerID ); Q_UNUSED( feat ); Q_UNUSED( context ); }
130  virtual void drawLabeling( QgsRenderContext& context ) = 0;
132  virtual void exit() = 0;
135  Q_DECL_DEPRECATED virtual QList<QgsLabelPosition> labelsAtPosition( const QgsPoint& p ) = 0;
138  Q_DECL_DEPRECATED virtual QList<QgsLabelPosition> labelsWithinRect( const QgsRectangle& r ) = 0;
139 
141  virtual QgsLabelingEngineInterface* clone() = 0;
142 };
143 
144 struct CORE_EXPORT QgsLayerCoordinateTransform
145 {
148  int srcDatumTransform; //-1 if unknown or not specified
150 };
151 
152 // ### QGIS 3: remove QgsMapRenderer in favor of QgsMapRendererJob
153 
158 class CORE_EXPORT QgsMapRenderer : public QObject
159 {
160  Q_OBJECT
161 
162  public:
163 
166  {
168  Pixels
169  //MAP_UNITS probably supported in future versions
170  };
171 
176  {
201  };
202 
204  QgsMapRenderer();
205 
207  ~QgsMapRenderer();
208 
212  void render( QPainter* painter, double* forceWidthScale = nullptr );
213 
215  bool setExtent( const QgsRectangle& extent );
216 
218  QgsRectangle extent() const;
219 
223  void setRotation( double degrees );
224 
227  double rotation() const;
228 
229  const QgsMapToPixel* coordinateTransform() { return &( mRenderContext.mapToPixel() ); }
230 
232  double scale() const { return mScale; }
235  void setScale( double scale ) {mScale = scale;}
236  double mapUnitsPerPixel() const { return mMapUnitsPerPixel; }
237 
238  int width() const { return static_cast< int >( mSize.width() ); }
239  int height() const { return static_cast< int >( mSize.height() ); }
240 
242  void updateScale();
243 
244  QGis::UnitType mapUnits() const;
245  void setMapUnits( QGis::UnitType u );
246 
248  void enableOverviewMode( bool isOverview = true ) { mOverview = isOverview; }
249 
255  void setOutputSize( QSize size, double dpi );
256 
262  void setOutputSize( QSizeF size, double dpi );
263 
265  double outputDpi();
266 
271  QSize outputSize() const;
272 
277  QSizeF outputSizeF() const;
278 
284  QgsRectangle layerExtentToOutputExtent( QgsMapLayer* theLayer, QgsRectangle extent );
285 
291  QgsRectangle outputExtentToLayerExtent( QgsMapLayer* theLayer, QgsRectangle extent );
292 
297  QgsPoint layerToMapCoordinates( QgsMapLayer* theLayer, QgsPoint point );
298 
304  QgsRectangle layerToMapCoordinates( QgsMapLayer* theLayer, QgsRectangle rect );
305 
310  QgsPoint mapToLayerCoordinates( QgsMapLayer* theLayer, QgsPoint point );
311 
317  QgsRectangle mapToLayerCoordinates( QgsMapLayer* theLayer, QgsRectangle rect );
318 
320  void setProjectionsEnabled( bool enabled );
321 
323  bool hasCrsTransformEnabled() const;
324 
326  void setDestinationCrs( const QgsCoordinateReferenceSystem& crs, bool refreshCoordinateTransformInfo = true, bool transformExtent = true );
327 
329  const QgsCoordinateReferenceSystem& destinationCrs() const;
330 
331  void setOutputUnits( OutputUnits u ) {mOutputUnits = u;}
332 
333  OutputUnits outputUnits() const {return mOutputUnits;}
334 
336  QgsRectangle fullExtent();
337 
339  QStringList& layerSet();
340 
342  void setLayerSet( const QStringList& layers );
343 
345  void updateFullExtent();
346 
348  bool readXML( QDomNode & theNode );
349 
351  bool writeXML( QDomNode & theNode, QDomDocument & theDoc );
352 
354  QgsRenderContext* rendererContext() {return &mRenderContext;}
355 
357  QgsLabelingEngineInterface* labelingEngine() { return mLabelingEngine; }
358 
361  void setLabelingEngine( QgsLabelingEngineInterface* iface );
362 
364  static QPainter::CompositionMode getCompositionMode( BlendMode blendMode );
366  static QgsMapRenderer::BlendMode getBlendModeEnum( QPainter::CompositionMode blendMode );
367 
368  void addLayerCoordinateTransform( const QString& layerId, const QString& srcAuthId, const QString& destAuthId, int srcDatumTransform = -1, int destDatumTransform = -1 );
369  void clearLayerCoordinateTransforms();
370 
371  const QgsCoordinateTransform* transformation( const QgsMapLayer *layer ) const;
372 
375  const QgsMapSettings& mapSettings();
376 
383  bool splitLayersExtent( QgsMapLayer* layer, QgsRectangle& extent, QgsRectangle& r2 );
384 
390  {
391  mRenderContext.setFeatureFilterProvider( ffp );
392  }
393 
394  signals:
395 
397  void drawingProgress( int current, int total );
398 
403 #ifndef Q_MOC_RUN
404  Q_DECL_DEPRECATED
405 #endif
406  void hasCrsTransformEnabled( bool flag );
407 
411  void hasCrsTransformEnabledChanged( bool flag );
412 
413  void destinationSrsChanged();
414 
416  void updateMap();
417 
418  void mapUnitsChanged();
419 
421  void drawError( QgsMapLayer* );
422 
425  void extentsChanged();
426 
429  void rotationChanged( double );
430 
433  void datumTransformInfoRequested( const QgsMapLayer* ml, const QString& srcAuthId, const QString& destAuthId ) const;
434 
435 
436  public slots:
437 
439  Q_DECL_DEPRECATED void onDrawingProgress( int current, int total );
440 
441  protected:
442 
444  void adjustExtentToSize();
445 
447  static bool mDrawing;
448 
451 
453  double mScale;
454 
456  double mRotation;
457 
460 
463  //
469 
471  bool mOverview;
472 
474 
477 
479  // TODO QGIS 3.0 - make object, not pointer
481 
484 
487 
490 
493 
496 
499 
502 
505 
507 
509 
510  private:
511  void readDefaultDatumTransformations();
512 };
513 
514 #endif
515 
QgsRectangle labelRect
const QgsMapToPixel * coordinateTransform()
A rectangle specified with double values.
Definition: qgsrectangle.h:35
Base class for all map layer types.
Definition: qgsmaplayer.h:49
QgsRenderContext * rendererContext()
Accessor for render context.
OutputUnits outputUnits() const
OutputUnits mOutputUnits
Output units.
bool mProjectionsEnabled
detemines whether on the fly projection support is enabled
QgsRectangle mLastExtent
Last extent to we drew so we know if we can used layer render caching or not.
QgsScaleCalculator * mScaleCalculator
scale calculator
virtual int prepareDiagramLayer(QgsVectorLayer *layer, QStringList &attrNames, QgsRenderContext &ctx)
adds a diagram layer to the labeling engine
void setScale(double scale)
Sets scale for scale based visibility.
static bool mDrawing
indicates drawing in progress
void enableOverviewMode(bool isOverview=true)
sets whether map image will be for overview
QHash< QPair< QString, QString >, QPair< int, int > > mDefaultDatumTransformations
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:187
A non GUI class for rendering a map layer set onto a QPainter.
QVector< QgsPoint > cornerPoints
BlendMode
Blending modes enum defining the available composition modes that can be used when rendering a layer...
QgsRectangle mExtent
current extent to be drawn
int width() const
The QgsMapSettings class contains configuration for rendering of the map.
double mRotation
Map rotation.
QMutex mRenderMutex
Locks rendering loop for concurrent draws.
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:34
void setFeatureFilterProvider(const QgsFeatureFilterProvider *ffp)
Set a feature filter provider to filter the features shown in the map.
QgsCoordinateReferenceSystem * mDestCRS
destination spatial reference system of the projection
QgsDistanceArea * mDistArea
tool for measuring
double mapUnitsPerPixel() const
double scale() const
Scale denominator.
virtual Q_DECL_DEPRECATED int addDiagramLayer(QgsVectorLayer *layer, const QgsDiagramLayerSettings *s)
adds a diagram layer to the labeling engine
bool mOverview
indicates whether it&#39;s map image for overview
Abstract interface for use by classes that filter the features of a layer.
A class to represent a point.
Definition: qgspoint.h:117
OutputUnits
Output units for pen width and point marker width/height.
Calculates scale for a given combination of canvas size, map extent, and monitor dpi.
Stores the settings for rendering of all diagrams for a layer.
General purpose distance and area calculator.
virtual void registerDiagramFeature(const QString &layerID, QgsFeature &feat, QgsRenderContext &context)
called for every diagram feature
Contains information about the context of a rendering operation.
int height() const
QgsMapSettings mMapSettings
map settings - used only for export in mapSettings() for use in classes that deal with QgsMapSettings...
QgsLabelPosition(int id, double r, const QVector< QgsPoint > &corners, const QgsRectangle &rect, double w, double h, const QString &layer, const QString &labeltext, const QFont &labelfont, bool upside_down, bool diagram=false, bool pinned=false, const QString &providerId=QString())
QgsLabelingEngineInterface * labelingEngine()
Labeling engine (nullptr if there&#39;s no custom engine)
double mScale
Map scale denominator at its current zoom level.
QHash< QString, QgsLayerCoordinateTransform > mLayerCoordinateTransformInfo
Class for storing a coordinate reference system (CRS)
Class for doing transforms between two map coordinate systems.
UnitType
Map units that qgis supports.
Definition: qgis.h:159
QStringList mLayerSet
stores array of layers to be rendered (identified by string)
double mMapUnitsPerPixel
map units per pixel
Labeling engine interface.
QgsRenderContext mRenderContext
Encapsulates context of rendering.
Represents a vector layer which manages a vector based data sets.
QgsLabelingEngineInterface * mLabelingEngine
Labeling engine (nullptr by default)
void setOutputUnits(OutputUnits u)
QgsRectangle mFullExtent
full extent of the layer set