QGIS API Documentation
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 ), rotation( r ), cornerPoints( corners ), labelRect( rect ), width( w ), height( h ), layerID( layer ), labelText( labeltext ), labelFont( labelfont ), upsideDown( upside_down ), isDiagram( diagram ), isPinned( pinned ), providerID( providerId ) {}
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;
67 };
68 
70 class CORE_EXPORT QgsLabelingEngineInterface
71 {
72  public:
73 
75 
78  Q_DECL_DEPRECATED virtual void init( QgsMapRenderer *mp ) = 0;
80  virtual void init( const QgsMapSettings& mapSettings ) = 0;
82  virtual bool willUseLayer( QgsVectorLayer* layer ) = 0;
84  virtual void clearActiveLayers() = 0;
86  virtual void clearActiveLayer( const QString& layerID ) = 0;
88  virtual int prepareLayer( QgsVectorLayer* layer, QStringList& attrNames, QgsRenderContext& ctx ) = 0;
91  Q_DECL_DEPRECATED virtual QgsPalLayerSettings &layer( const QString &layerName ) = 0;
94  virtual int prepareDiagramLayer( QgsVectorLayer *layer, QStringList &attrNames, QgsRenderContext &ctx )
95  { Q_UNUSED( layer ); Q_UNUSED( attrNames ); Q_UNUSED( ctx ); return 0; }
98  Q_DECL_DEPRECATED virtual int addDiagramLayer( QgsVectorLayer *layer, const QgsDiagramLayerSettings *s )
99  { Q_UNUSED( layer ); Q_UNUSED( s ); return 0; }
101  virtual void registerFeature( const QString &layerID, QgsFeature &feat, QgsRenderContext &context ) = 0;
103  virtual void registerDiagramFeature( const QString &layerID, QgsFeature &feat, QgsRenderContext &context )
104  { Q_UNUSED( layerID ); Q_UNUSED( feat ); Q_UNUSED( context ); }
106  virtual void drawLabeling( QgsRenderContext& context ) = 0;
108  virtual void exit() = 0;
111  Q_DECL_DEPRECATED virtual QList<QgsLabelPosition> labelsAtPosition( const QgsPoint& p ) = 0;
114  Q_DECL_DEPRECATED virtual QList<QgsLabelPosition> labelsWithinRect( const QgsRectangle& r ) = 0;
115 
117  virtual QgsLabelingEngineInterface* clone() = 0;
118 };
119 
120 struct CORE_EXPORT QgsLayerCoordinateTransform
121 {
124  int srcDatumTransform; //-1 if unknown or not specified
126 };
127 
128 // ### QGIS 3: remove QgsMapRenderer in favor of QgsMapRendererJob
129 
134 class CORE_EXPORT QgsMapRenderer : public QObject
135 {
136  Q_OBJECT
137 
138  public:
139 
142  {
144  Pixels
145  //MAP_UNITS probably supported in future versions
146  };
147 
152  {
177  };
178 
180  QgsMapRenderer();
181 
183  ~QgsMapRenderer();
184 
188  void render( QPainter* painter, double* forceWidthScale = nullptr );
189 
191  bool setExtent( const QgsRectangle& extent );
192 
194  QgsRectangle extent() const;
195 
199  void setRotation( double degrees );
200 
203  double rotation() const;
204 
205  const QgsMapToPixel* coordinateTransform() { return &( mRenderContext.mapToPixel() ); }
206 
208  double scale() const { return mScale; }
211  void setScale( double scale ) {mScale = scale;}
212  double mapUnitsPerPixel() const { return mMapUnitsPerPixel; }
213 
214  int width() const { return static_cast< int >( mSize.width() ); }
215  int height() const { return static_cast< int >( mSize.height() ); }
216 
218  void updateScale();
219 
220  QGis::UnitType mapUnits() const;
221  void setMapUnits( QGis::UnitType u );
222 
224  void enableOverviewMode( bool isOverview = true ) { mOverview = isOverview; }
225 
226  void setOutputSize( QSize size, int dpi );
227  void setOutputSize( QSizeF size, double dpi );
228 
230  double outputDpi();
232  QSize outputSize();
233  QSizeF outputSizeF();
234 
240  QgsRectangle layerExtentToOutputExtent( QgsMapLayer* theLayer, QgsRectangle extent );
241 
247  QgsRectangle outputExtentToLayerExtent( QgsMapLayer* theLayer, QgsRectangle extent );
248 
253  QgsPoint layerToMapCoordinates( QgsMapLayer* theLayer, QgsPoint point );
254 
260  QgsRectangle layerToMapCoordinates( QgsMapLayer* theLayer, QgsRectangle rect );
261 
266  QgsPoint mapToLayerCoordinates( QgsMapLayer* theLayer, QgsPoint point );
267 
273  QgsRectangle mapToLayerCoordinates( QgsMapLayer* theLayer, QgsRectangle rect );
274 
276  void setProjectionsEnabled( bool enabled );
277 
279  bool hasCrsTransformEnabled() const;
280 
282  void setDestinationCrs( const QgsCoordinateReferenceSystem& crs, bool refreshCoordinateTransformInfo = true, bool transformExtent = true );
283 
285  const QgsCoordinateReferenceSystem& destinationCrs() const;
286 
287  void setOutputUnits( OutputUnits u ) {mOutputUnits = u;}
288 
289  OutputUnits outputUnits() const {return mOutputUnits;}
290 
292  QgsRectangle fullExtent();
293 
295  QStringList& layerSet();
296 
298  void setLayerSet( const QStringList& layers );
299 
301  void updateFullExtent();
302 
304  bool readXML( QDomNode & theNode );
305 
307  bool writeXML( QDomNode & theNode, QDomDocument & theDoc );
308 
310  QgsRenderContext* rendererContext() {return &mRenderContext;}
311 
313  QgsLabelingEngineInterface* labelingEngine() { return mLabelingEngine; }
314 
317  void setLabelingEngine( QgsLabelingEngineInterface* iface );
318 
320  static QPainter::CompositionMode getCompositionMode( BlendMode blendMode );
322  static QgsMapRenderer::BlendMode getBlendModeEnum( QPainter::CompositionMode blendMode );
323 
324  void addLayerCoordinateTransform( const QString& layerId, const QString& srcAuthId, const QString& destAuthId, int srcDatumTransform = -1, int destDatumTransform = -1 );
325  void clearLayerCoordinateTransforms();
326 
327  const QgsCoordinateTransform* transformation( const QgsMapLayer *layer ) const;
328 
331  const QgsMapSettings& mapSettings();
332 
339  bool splitLayersExtent( QgsMapLayer* layer, QgsRectangle& extent, QgsRectangle& r2 );
340 
346  {
347  mRenderContext.setFeatureFilterProvider( ffp );
348  }
349 
350  signals:
351 
353  void drawingProgress( int current, int total );
354 
359 #ifndef Q_MOC_RUN
360  Q_DECL_DEPRECATED
361 #endif
362  void hasCrsTransformEnabled( bool flag );
363 
367  void hasCrsTransformEnabledChanged( bool flag );
368 
369  void destinationSrsChanged();
370 
372  void updateMap();
373 
374  void mapUnitsChanged();
375 
377  void drawError( QgsMapLayer* );
378 
381  void extentsChanged();
382 
385  void rotationChanged( double );
386 
389  void datumTransformInfoRequested( const QgsMapLayer* ml, const QString& srcAuthId, const QString& destAuthId ) const;
390 
391 
392  public slots:
393 
395  Q_DECL_DEPRECATED void onDrawingProgress( int current, int total );
396 
397  protected:
398 
400  void adjustExtentToSize();
401 
403  static bool mDrawing;
404 
407 
409  double mScale;
410 
412  double mRotation;
413 
416 
419  //
425 
427  bool mOverview;
428 
430 
433 
436 
439 
442 
445 
448 
451 
454 
457 
460 
462 
464 
465  private:
466  void readDefaultDatumTransformations();
467 };
468 
469 #endif
470 
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: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