QGIS API Documentation  2.17.0-Master (3a3b9ab7)
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 
49 class CORE_EXPORT QgsLabelPosition
50 {
51  public:
52  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() )
53  : featureId( id )
54  , rotation( r )
55  , cornerPoints( corners )
56  , labelRect( rect )
57  , width( w )
58  , height( h )
59  , layerID( layer )
60  , labelText( labeltext )
61  , labelFont( labelfont )
62  , upsideDown( upside_down )
63  , isDiagram( diagram )
64  , isPinned( pinned )
65  , providerID( providerId )
66  {}
68  : featureId( -1 )
69  , rotation( 0 )
70  , labelRect( QgsRectangle() )
71  , width( 0 )
72  , height( 0 )
73  , layerID( "" )
74  , labelText( "" )
75  , labelFont( QFont() )
76  , upsideDown( false )
77  , isDiagram( false )
78  , isPinned( false )
79  {}
80  int featureId;
81  double rotation;
84  double width;
85  double height;
89  bool upsideDown;
90  bool isDiagram;
91  bool isPinned;
94 };
95 
99 class CORE_EXPORT QgsLabelingEngineInterface
100 {
101  public:
102 
104 
107  Q_DECL_DEPRECATED virtual void init( QgsMapRenderer *mp ) = 0;
109  virtual void init( const QgsMapSettings& mapSettings ) = 0;
111  virtual bool willUseLayer( QgsVectorLayer* layer ) = 0;
113  virtual void clearActiveLayers() = 0;
115  virtual void clearActiveLayer( const QString& layerID ) = 0;
117  virtual int prepareLayer( QgsVectorLayer* layer, QStringList& attrNames, QgsRenderContext& ctx ) = 0;
120  Q_DECL_DEPRECATED virtual QgsPalLayerSettings &layer( const QString &layerName ) = 0;
123  virtual int prepareDiagramLayer( QgsVectorLayer *layer, QStringList &attrNames, QgsRenderContext &ctx )
124  { Q_UNUSED( layer ); Q_UNUSED( attrNames ); Q_UNUSED( ctx ); return 0; }
127  Q_DECL_DEPRECATED virtual int addDiagramLayer( QgsVectorLayer *layer, const QgsDiagramLayerSettings *s )
128  { Q_UNUSED( layer ); Q_UNUSED( s ); return 0; }
130  virtual void registerFeature( const QString &layerID, QgsFeature &feat, QgsRenderContext &context ) = 0;
132  virtual void registerDiagramFeature( const QString &layerID, QgsFeature &feat, QgsRenderContext &context )
133  { Q_UNUSED( layerID ); Q_UNUSED( feat ); Q_UNUSED( context ); }
135  virtual void drawLabeling( QgsRenderContext& context ) = 0;
137  virtual void exit() = 0;
140  Q_DECL_DEPRECATED virtual QList<QgsLabelPosition> labelsAtPosition( const QgsPoint& p ) = 0;
143  Q_DECL_DEPRECATED virtual QList<QgsLabelPosition> labelsWithinRect( const QgsRectangle& r ) = 0;
144 
146  virtual QgsLabelingEngineInterface* clone() = 0;
147 };
148 
149 struct CORE_EXPORT QgsLayerCoordinateTransform
150 {
153  int srcDatumTransform; //-1 if unknown or not specified
155 };
156 
157 // ### QGIS 3: remove QgsMapRenderer in favor of QgsMapRendererJob
158 
163 class CORE_EXPORT QgsMapRenderer : public QObject
164 {
165  Q_OBJECT
166 
167  public:
168 
171  {
173  Pixels
174  //MAP_UNITS probably supported in future versions
175  };
176 
181  {
206  };
207 
209  QgsMapRenderer();
210 
212  ~QgsMapRenderer();
213 
217  void render( QPainter* painter, double* forceWidthScale = nullptr );
218 
220  bool setExtent( const QgsRectangle& extent );
221 
223  QgsRectangle extent() const;
224 
228  void setRotation( double degrees );
229 
232  double rotation() const;
233 
234  const QgsMapToPixel* coordinateTransform() { return &( mRenderContext.mapToPixel() ); }
235 
237  double scale() const { return mScale; }
240  void setScale( double scale ) {mScale = scale;}
241  double mapUnitsPerPixel() const { return mMapUnitsPerPixel; }
242 
243  int width() const { return static_cast< int >( mSize.width() ); }
244  int height() const { return static_cast< int >( mSize.height() ); }
245 
247  void updateScale();
248 
249  QGis::UnitType mapUnits() const;
250  void setMapUnits( QGis::UnitType u );
251 
253  void enableOverviewMode( bool isOverview = true ) { mOverview = isOverview; }
254 
260  void setOutputSize( QSize size, double dpi );
261 
267  void setOutputSize( QSizeF size, double dpi );
268 
270  double outputDpi();
271 
276  QSize outputSize() const;
277 
282  QSizeF outputSizeF() const;
283 
289  QgsRectangle layerExtentToOutputExtent( QgsMapLayer* theLayer, QgsRectangle extent );
290 
296  QgsRectangle outputExtentToLayerExtent( QgsMapLayer* theLayer, QgsRectangle extent );
297 
302  QgsPoint layerToMapCoordinates( QgsMapLayer* theLayer, QgsPoint point );
303 
309  QgsRectangle layerToMapCoordinates( QgsMapLayer* theLayer, QgsRectangle rect );
310 
315  QgsPoint mapToLayerCoordinates( QgsMapLayer* theLayer, QgsPoint point );
316 
322  QgsRectangle mapToLayerCoordinates( QgsMapLayer* theLayer, QgsRectangle rect );
323 
325  void setProjectionsEnabled( bool enabled );
326 
328  bool hasCrsTransformEnabled() const;
329 
331  void setDestinationCrs( const QgsCoordinateReferenceSystem& crs, bool refreshCoordinateTransformInfo = true, bool transformExtent = true );
332 
334  const QgsCoordinateReferenceSystem& destinationCrs() const;
335 
336  void setOutputUnits( OutputUnits u ) {mOutputUnits = u;}
337 
338  OutputUnits outputUnits() const {return mOutputUnits;}
339 
341  QgsRectangle fullExtent();
342 
344  QStringList& layerSet();
345 
347  void setLayerSet( const QStringList& layers );
348 
350  void updateFullExtent();
351 
353  bool readXML( QDomNode & theNode );
354 
356  bool writeXML( QDomNode & theNode, QDomDocument & theDoc );
357 
359  QgsRenderContext* rendererContext() {return &mRenderContext;}
360 
362  QgsLabelingEngineInterface* labelingEngine() { return mLabelingEngine; }
363 
366  void setLabelingEngine( QgsLabelingEngineInterface* iface );
367 
369  static QPainter::CompositionMode getCompositionMode( BlendMode blendMode );
371  static QgsMapRenderer::BlendMode getBlendModeEnum( QPainter::CompositionMode blendMode );
372 
373  void addLayerCoordinateTransform( const QString& layerId, const QString& srcAuthId, const QString& destAuthId, int srcDatumTransform = -1, int destDatumTransform = -1 );
374  void clearLayerCoordinateTransforms();
375 
376  const QgsCoordinateTransform* transformation( const QgsMapLayer *layer ) const;
377 
380  const QgsMapSettings& mapSettings();
381 
388  bool splitLayersExtent( QgsMapLayer* layer, QgsRectangle& extent, QgsRectangle& r2 );
389 
395  {
396  mRenderContext.setFeatureFilterProvider( ffp );
397  }
398 
399  signals:
400 
402  void drawingProgress( int current, int total );
403 
408 #ifndef Q_MOC_RUN
409  Q_DECL_DEPRECATED
410 #endif
411  void hasCrsTransformEnabled( bool flag );
412 
416  void hasCrsTransformEnabledChanged( bool flag );
417 
418  void destinationSrsChanged();
419 
421  void updateMap();
422 
423  void mapUnitsChanged();
424 
426  void drawError( QgsMapLayer* );
427 
430  void extentsChanged();
431 
434  void rotationChanged( double );
435 
438  void datumTransformInfoRequested( const QgsMapLayer* ml, const QString& srcAuthId, const QString& destAuthId ) const;
439 
440 
441  public slots:
442 
444  Q_DECL_DEPRECATED void onDrawingProgress( int current, int total );
445 
446  protected:
447 
449  void adjustExtentToSize();
450 
452  static bool mDrawing;
453 
456 
458  double mScale;
459 
461  double mRotation;
462 
465 
468  //
474 
476  bool mOverview;
477 
479 
482 
484  // TODO QGIS 3.0 - make object, not pointer
486 
489 
492 
495 
498 
501 
504 
507 
510 
512 
514 
515  private:
516  void readDefaultDatumTransformations();
517 };
518 
519 #endif
520 
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