QGIS API Documentation  2.14.11-Essen
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 )
50  : featureId( id ), rotation( r ), cornerPoints( corners ), labelRect( rect ), width( w ), height( h ), layerID( layer ), labelText( labeltext ), labelFont( labelfont ), upsideDown( upside_down ), isDiagram( diagram ), isPinned( pinned ) {}
52  : featureId( -1 ), rotation( 0 ), labelRect( QgsRectangle() ), width( 0 ), height( 0 ), layerID( "" ), labelText( "" ), labelFont( QFont() ), upsideDown( false ), isDiagram( false ), isPinned( false ) {}
53  int featureId;
54  double rotation;
57  double width;
58  double height;
62  bool upsideDown;
63  bool isDiagram;
64  bool isPinned;
65 };
66 
68 class CORE_EXPORT QgsLabelingEngineInterface
69 {
70  public:
71 
73 
76  Q_DECL_DEPRECATED virtual void init( QgsMapRenderer *mp ) = 0;
78  virtual void init( const QgsMapSettings& mapSettings ) = 0;
80  virtual bool willUseLayer( QgsVectorLayer* layer ) = 0;
82  virtual void clearActiveLayers() = 0;
84  virtual void clearActiveLayer( const QString& layerID ) = 0;
86  virtual int prepareLayer( QgsVectorLayer* layer, QStringList& attrNames, QgsRenderContext& ctx ) = 0;
89  Q_DECL_DEPRECATED virtual QgsPalLayerSettings &layer( const QString &layerName ) = 0;
92  virtual int prepareDiagramLayer( QgsVectorLayer *layer, QStringList &attrNames, QgsRenderContext &ctx )
93  { Q_UNUSED( layer ); Q_UNUSED( attrNames ); Q_UNUSED( ctx ); return 0; }
96  Q_DECL_DEPRECATED virtual int addDiagramLayer( QgsVectorLayer *layer, const QgsDiagramLayerSettings *s )
97  { Q_UNUSED( layer ); Q_UNUSED( s ); return 0; }
99  virtual void registerFeature( const QString &layerID, QgsFeature &feat, QgsRenderContext &context ) = 0;
101  virtual void registerDiagramFeature( const QString &layerID, QgsFeature &feat, QgsRenderContext &context )
102  { Q_UNUSED( layerID ); Q_UNUSED( feat ); Q_UNUSED( context ); }
104  virtual void drawLabeling( QgsRenderContext& context ) = 0;
106  virtual void exit() = 0;
109  Q_DECL_DEPRECATED virtual QList<QgsLabelPosition> labelsAtPosition( const QgsPoint& p ) = 0;
112  Q_DECL_DEPRECATED virtual QList<QgsLabelPosition> labelsWithinRect( const QgsRectangle& r ) = 0;
113 
115  virtual QgsLabelingEngineInterface* clone() = 0;
116 };
117 
118 struct CORE_EXPORT QgsLayerCoordinateTransform
119 {
122  int srcDatumTransform; //-1 if unknown or not specified
124 };
125 
126 // ### QGIS 3: remove QgsMapRenderer in favor of QgsMapRendererJob
127 
132 class CORE_EXPORT QgsMapRenderer : public QObject
133 {
134  Q_OBJECT
135 
136  public:
137 
140  {
142  Pixels
143  //MAP_UNITS probably supported in future versions
144  };
145 
150  {
175  };
176 
178  QgsMapRenderer();
179 
181  ~QgsMapRenderer();
182 
186  void render( QPainter* painter, double* forceWidthScale = nullptr );
187 
189  bool setExtent( const QgsRectangle& extent );
190 
192  QgsRectangle extent() const;
193 
197  void setRotation( double degrees );
198 
201  double rotation() const;
202 
203  const QgsMapToPixel* coordinateTransform() { return &( mRenderContext.mapToPixel() ); }
204 
206  double scale() const { return mScale; }
209  void setScale( double scale ) {mScale = scale;}
210  double mapUnitsPerPixel() const { return mMapUnitsPerPixel; }
211 
212  int width() const { return static_cast< int >( mSize.width() ); }
213  int height() const { return static_cast< int >( mSize.height() ); }
214 
216  void updateScale();
217 
218  QGis::UnitType mapUnits() const;
219  void setMapUnits( QGis::UnitType u );
220 
222  void enableOverviewMode( bool isOverview = true ) { mOverview = isOverview; }
223 
224  void setOutputSize( QSize size, int dpi );
225  void setOutputSize( QSizeF size, double dpi );
226 
228  double outputDpi();
230  QSize outputSize();
231  QSizeF outputSizeF();
232 
238  QgsRectangle layerExtentToOutputExtent( QgsMapLayer* theLayer, QgsRectangle extent );
239 
245  QgsRectangle outputExtentToLayerExtent( QgsMapLayer* theLayer, QgsRectangle extent );
246 
251  QgsPoint layerToMapCoordinates( QgsMapLayer* theLayer, QgsPoint point );
252 
258  QgsRectangle layerToMapCoordinates( QgsMapLayer* theLayer, QgsRectangle rect );
259 
264  QgsPoint mapToLayerCoordinates( QgsMapLayer* theLayer, QgsPoint point );
265 
271  QgsRectangle mapToLayerCoordinates( QgsMapLayer* theLayer, QgsRectangle rect );
272 
274  void setProjectionsEnabled( bool enabled );
275 
277  bool hasCrsTransformEnabled() const;
278 
280  void setDestinationCrs( const QgsCoordinateReferenceSystem& crs, bool refreshCoordinateTransformInfo = true, bool transformExtent = true );
281 
283  const QgsCoordinateReferenceSystem& destinationCrs() const;
284 
285  void setOutputUnits( OutputUnits u ) {mOutputUnits = u;}
286 
287  OutputUnits outputUnits() const {return mOutputUnits;}
288 
290  QgsRectangle fullExtent();
291 
293  QStringList& layerSet();
294 
296  void setLayerSet( const QStringList& layers );
297 
299  void updateFullExtent();
300 
302  bool readXML( QDomNode & theNode );
303 
305  bool writeXML( QDomNode & theNode, QDomDocument & theDoc );
306 
308  QgsRenderContext* rendererContext() {return &mRenderContext;}
309 
311  QgsLabelingEngineInterface* labelingEngine() { return mLabelingEngine; }
312 
315  void setLabelingEngine( QgsLabelingEngineInterface* iface );
316 
318  static QPainter::CompositionMode getCompositionMode( BlendMode blendMode );
320  static QgsMapRenderer::BlendMode getBlendModeEnum( QPainter::CompositionMode blendMode );
321 
322  void addLayerCoordinateTransform( const QString& layerId, const QString& srcAuthId, const QString& destAuthId, int srcDatumTransform = -1, int destDatumTransform = -1 );
323  void clearLayerCoordinateTransforms();
324 
325  const QgsCoordinateTransform* transformation( const QgsMapLayer *layer ) const;
326 
329  const QgsMapSettings& mapSettings();
330 
337  bool splitLayersExtent( QgsMapLayer* layer, QgsRectangle& extent, QgsRectangle& r2 );
338 
344  {
345  mRenderContext.setFeatureFilterProvider( ffp );
346  }
347 
348  signals:
349 
351  void drawingProgress( int current, int total );
352 
357 #ifndef Q_MOC_RUN
358  Q_DECL_DEPRECATED
359 #endif
360  void hasCrsTransformEnabled( bool flag );
361 
365  void hasCrsTransformEnabledChanged( bool flag );
366 
367  void destinationSrsChanged();
368 
370  void updateMap();
371 
372  void mapUnitsChanged();
373 
375  void drawError( QgsMapLayer* );
376 
379  void extentsChanged();
380 
383  void rotationChanged( double );
384 
387  void datumTransformInfoRequested( const QgsMapLayer* ml, const QString& srcAuthId, const QString& destAuthId ) const;
388 
389 
390  public slots:
391 
393  Q_DECL_DEPRECATED void onDrawingProgress( int current, int total );
394 
395  protected:
396 
398  void adjustExtentToSize();
399 
401  static bool mDrawing;
402 
405 
407  double mScale;
408 
410  double mRotation;
411 
414 
417  //
423 
425  bool mOverview;
426 
428 
431 
434 
437 
440 
443 
446 
449 
452 
455 
458 
460 };
461 
462 #endif
463 
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
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)
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
double scale() const
Scale denominator.
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
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
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:65
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.
General purpose distance and area calculator.
virtual void registerDiagramFeature(const QString &layerID, QgsFeature &feat, QgsRenderContext &context)
called for every diagram feature
double mapUnitsPerPixel() const
Contains information about the context of a rendering operation.
QgsMapSettings mMapSettings
map settings - used only for export in mapSettings() for use in classes that deal with QgsMapSettings...
QgsLabelingEngineInterface * labelingEngine()
Labeling engine (nullptr if there&#39;s no custom engine)
int width() const
double mScale
Map scale denominator at its current zoom level.
QHash< QString, QgsLayerCoordinateTransform > mLayerCoordinateTransformInfo
Class for storing a coordinate reference system (CRS)
int height() const
Class for doing transforms between two map coordinate systems.
UnitType
Map units that qgis supports.
Definition: qgis.h:155
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