QGIS API Documentation  2.15.0-Master (13f053b)
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 
250  void setOutputSize( QSize size, int dpi );
251  void setOutputSize( QSizeF size, double dpi );
252 
254  double outputDpi();
256  QSize outputSize();
257  QSizeF outputSizeF();
258 
264  QgsRectangle layerExtentToOutputExtent( QgsMapLayer* theLayer, QgsRectangle extent );
265 
271  QgsRectangle outputExtentToLayerExtent( QgsMapLayer* theLayer, QgsRectangle extent );
272 
277  QgsPoint layerToMapCoordinates( QgsMapLayer* theLayer, QgsPoint point );
278 
284  QgsRectangle layerToMapCoordinates( QgsMapLayer* theLayer, QgsRectangle rect );
285 
290  QgsPoint mapToLayerCoordinates( QgsMapLayer* theLayer, QgsPoint point );
291 
297  QgsRectangle mapToLayerCoordinates( QgsMapLayer* theLayer, QgsRectangle rect );
298 
300  void setProjectionsEnabled( bool enabled );
301 
303  bool hasCrsTransformEnabled() const;
304 
306  void setDestinationCrs( const QgsCoordinateReferenceSystem& crs, bool refreshCoordinateTransformInfo = true, bool transformExtent = true );
307 
309  const QgsCoordinateReferenceSystem& destinationCrs() const;
310 
311  void setOutputUnits( OutputUnits u ) {mOutputUnits = u;}
312 
313  OutputUnits outputUnits() const {return mOutputUnits;}
314 
316  QgsRectangle fullExtent();
317 
319  QStringList& layerSet();
320 
322  void setLayerSet( const QStringList& layers );
323 
325  void updateFullExtent();
326 
328  bool readXML( QDomNode & theNode );
329 
331  bool writeXML( QDomNode & theNode, QDomDocument & theDoc );
332 
334  QgsRenderContext* rendererContext() {return &mRenderContext;}
335 
337  QgsLabelingEngineInterface* labelingEngine() { return mLabelingEngine; }
338 
341  void setLabelingEngine( QgsLabelingEngineInterface* iface );
342 
344  static QPainter::CompositionMode getCompositionMode( BlendMode blendMode );
346  static QgsMapRenderer::BlendMode getBlendModeEnum( QPainter::CompositionMode blendMode );
347 
348  void addLayerCoordinateTransform( const QString& layerId, const QString& srcAuthId, const QString& destAuthId, int srcDatumTransform = -1, int destDatumTransform = -1 );
349  void clearLayerCoordinateTransforms();
350 
351  const QgsCoordinateTransform* transformation( const QgsMapLayer *layer ) const;
352 
355  const QgsMapSettings& mapSettings();
356 
363  bool splitLayersExtent( QgsMapLayer* layer, QgsRectangle& extent, QgsRectangle& r2 );
364 
370  {
371  mRenderContext.setFeatureFilterProvider( ffp );
372  }
373 
374  signals:
375 
377  void drawingProgress( int current, int total );
378 
383 #ifndef Q_MOC_RUN
384  Q_DECL_DEPRECATED
385 #endif
386  void hasCrsTransformEnabled( bool flag );
387 
391  void hasCrsTransformEnabledChanged( bool flag );
392 
393  void destinationSrsChanged();
394 
396  void updateMap();
397 
398  void mapUnitsChanged();
399 
401  void drawError( QgsMapLayer* );
402 
405  void extentsChanged();
406 
409  void rotationChanged( double );
410 
413  void datumTransformInfoRequested( const QgsMapLayer* ml, const QString& srcAuthId, const QString& destAuthId ) const;
414 
415 
416  public slots:
417 
419  Q_DECL_DEPRECATED void onDrawingProgress( int current, int total );
420 
421  protected:
422 
424  void adjustExtentToSize();
425 
427  static bool mDrawing;
428 
431 
433  double mScale;
434 
436  double mRotation;
437 
440 
443  //
449 
451  bool mOverview;
452 
454 
457 
460 
463 
466 
469 
472 
475 
478 
481 
484 
486 
488 
489  private:
490  void readDefaultDatumTransformations();
491 };
492 
493 #endif
494 
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