QGIS API Documentation  2.2.0-Valmiera
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
30 class QDomDocument;
31 class QDomNode;
32 class QPainter;
33 
34 class QgsMapToPixel;
35 class QgsMapLayer;
36 class QgsMapRenderer;
37 class QgsScaleCalculator;
39 class QgsDistanceArea;
40 class QgsVectorLayer;
41 
44 
45 class CORE_EXPORT QgsLabelPosition
46 {
47  public:
48  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 ):
49  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 ) {}
50  QgsLabelPosition(): featureId( -1 ), rotation( 0 ), labelRect( QgsRectangle() ), width( 0 ), height( 0 ), layerID( "" ), labelText( "" ), labelFont( QFont() ), upsideDown( false ), isDiagram( false ), isPinned( false ) {}
51  int featureId;
52  double rotation;
53  QVector< QgsPoint > cornerPoints;
55  double width;
56  double height;
57  QString layerID;
58  QString labelText;
59  QFont labelFont;
60  bool upsideDown;
61  bool isDiagram;
62  bool isPinned;
63 };
64 
68 class CORE_EXPORT QgsLabelingEngineInterface
69 {
70  public:
71 
73 
75  virtual void init( QgsMapRenderer* mp ) = 0;
77  virtual bool willUseLayer( QgsVectorLayer* layer ) = 0;
80  virtual void clearActiveLayers() = 0;
83  virtual void clearActiveLayer( QgsVectorLayer* layer ) = 0;
86  virtual int prepareLayer( QgsVectorLayer* layer, QSet<int>& attrIndices, QgsRenderContext& ctx ) = 0;
89  virtual QgsPalLayerSettings& layer( const QString& layerName ) = 0;
92  { Q_UNUSED( layer ); Q_UNUSED( s ); return 0; }
94  virtual void registerFeature( QgsVectorLayer* layer, QgsFeature& feat, const QgsRenderContext& context = QgsRenderContext() ) = 0;
96  virtual void registerDiagramFeature( QgsVectorLayer* layer, QgsFeature& feat, const QgsRenderContext& context = QgsRenderContext() )
97  { Q_UNUSED( layer ); Q_UNUSED( feat ); Q_UNUSED( context ); }
99  virtual void drawLabeling( QgsRenderContext& context ) = 0;
101  virtual void exit() = 0;
104  virtual QList<QgsLabelPosition> labelsAtPosition( const QgsPoint& p ) = 0;
107  virtual QList<QgsLabelPosition> labelsWithinRect( const QgsRectangle& r ) = 0;
108 
110  virtual QgsLabelingEngineInterface* clone() = 0;
111 };
112 
113 struct CORE_EXPORT QgsLayerCoordinateTransform
114 {
115  QString srcAuthId;
116  QString destAuthId;
117  int srcDatumTransform; //-1 if unknown or not specified
119 };
120 
125 class CORE_EXPORT QgsMapRenderer : public QObject
126 {
127  Q_OBJECT
128 
129  public:
130 
133  {
135  Pixels
136  //MAP_UNITS probably supported in future versions
137  };
138 
143  {
156  BlendSubtract
157  };
158 
160  QgsMapRenderer();
161 
163  ~QgsMapRenderer();
164 
167  void render( QPainter* painter, double* forceWidthScale = 0 );
168 
170  bool setExtent( const QgsRectangle& extent );
171 
173  QgsRectangle extent() const;
174 
175  const QgsMapToPixel* coordinateTransform() { return &( mRenderContext.mapToPixel() ); }
176 
178  double scale() const { return mScale; }
181  void setScale( double scale ) {mScale = scale;}
182  double mapUnitsPerPixel() const { return mMapUnitsPerPixel; }
183 
184  int width() const { return mSize.width(); };
185  int height() const { return mSize.height(); };
186 
188  void updateScale();
189 
190  QGis::UnitType mapUnits() const;
191  void setMapUnits( QGis::UnitType u );
192 
194  void enableOverviewMode( bool isOverview = true ) { mOverview = isOverview; }
195 
196  void setOutputSize( QSize size, int dpi );
197  void setOutputSize( QSizeF size, double dpi );
198 
200  double outputDpi();
202  QSize outputSize();
203  QSizeF outputSizeF();
204 
210  QgsRectangle layerExtentToOutputExtent( QgsMapLayer* theLayer, QgsRectangle extent );
211 
217  QgsRectangle outputExtentToLayerExtent( QgsMapLayer* theLayer, QgsRectangle extent );
218 
223  QgsPoint layerToMapCoordinates( QgsMapLayer* theLayer, QgsPoint point );
224 
230  QgsRectangle layerToMapCoordinates( QgsMapLayer* theLayer, QgsRectangle rect );
231 
236  QgsPoint mapToLayerCoordinates( QgsMapLayer* theLayer, QgsPoint point );
237 
243  QgsRectangle mapToLayerCoordinates( QgsMapLayer* theLayer, QgsRectangle rect );
244 
246  void setProjectionsEnabled( bool enabled );
247 
249  bool hasCrsTransformEnabled() const;
250 
252  void setDestinationCrs( const QgsCoordinateReferenceSystem& crs, bool refreshCoordinateTransformInfo = true );
253 
255  const QgsCoordinateReferenceSystem& destinationCrs() const;
256 
257  void setOutputUnits( OutputUnits u ) {mOutputUnits = u;}
258 
259  OutputUnits outputUnits() const {return mOutputUnits;}
260 
262  QgsRectangle fullExtent();
263 
265  QStringList& layerSet();
266 
268  void setLayerSet( const QStringList& layers );
269 
271  void updateFullExtent();
272 
274  bool readXML( QDomNode & theNode );
275 
277  bool writeXML( QDomNode & theNode, QDomDocument & theDoc );
278 
280  QgsRenderContext* rendererContext() {return &mRenderContext;}
281 
284  QgsLabelingEngineInterface* labelingEngine() { return mLabelingEngine; }
285 
289  void setLabelingEngine( QgsLabelingEngineInterface* iface );
290 
293  static QPainter::CompositionMode getCompositionMode( const QgsMapRenderer::BlendMode &blendMode );
296  static QgsMapRenderer::BlendMode getBlendModeEnum( const QPainter::CompositionMode &blendMode );
297 
298  void addLayerCoordinateTransform( const QString& layerId, const QString& srcAuthId, const QString& destAuthId, int srcDatumTransform = -1, int destDatumTransform = -1 );
299  void clearLayerCoordinateTransforms();
300 
301  const QgsCoordinateTransform* transformation( const QgsMapLayer *layer ) const;
302 
303  signals:
304 
305  void drawingProgress( int current, int total );
306 
311 #ifndef Q_MOC_RUN
312  Q_DECL_DEPRECATED
313 #endif
314  void hasCrsTransformEnabled( bool flag );
315 
319  void hasCrsTransformEnabledChanged( bool flag );
320 
321  void destinationSrsChanged();
322 
323  void updateMap();
324 
325  void mapUnitsChanged();
326 
328  void drawError( QgsMapLayer* );
329 
331  void datumTransformInfoRequested( const QgsMapLayer* ml, const QString& srcAuthId, const QString& destAuthId ) const;
332 
333  public slots:
334 
336  void onDrawingProgress( int current, int total );
337 
338  protected:
339 
341  void adjustExtentToSize();
342 
349  bool splitLayersExtent( QgsMapLayer* layer, QgsRectangle& extent, QgsRectangle& r2 );
350 
352  static bool mDrawing;
353 
356 
358  double mScale;
359 
362 
365  //
372 
374  bool mOverview;
375 
376  QSizeF mSize;
377 
380 
383 
385  QStringList mLayerSet;
386 
389 
392 
395 
398 
401 
403  QMutex mRenderMutex;
404 
405  QHash< QString, QgsLayerCoordinateTransform > mLayerCoordinateTransformInfo;
406 
407 };
408 
409 #endif
410 
virtual int addDiagramLayer(QgsVectorLayer *layer, QgsDiagramLayerSettings *s)
adds a diagram layer to the labeling engine
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:45
QgsRenderContext * rendererContext()
Accessor for render context.
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 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
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
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:114
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
QMutex mRenderMutex
Locks rendering loop for concurrent draws.
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:33
QgsCoordinateReferenceSystem * mDestCRS
destination spatial reference system of the projection
QgsDistanceArea * mDistArea
tool for measuring
double mapUnitsPerPixel() const
double scale() const
Scale denominator.
virtual void registerDiagramFeature(QgsVectorLayer *layer, QgsFeature &feat, const QgsRenderContext &context=QgsRenderContext())
called for every diagram feature
bool mOverview
indicates whether it's map image for overview
A class to represent a point geometry.
Definition: qgspoint.h:63
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.
Contains information about the context of a rendering operation.
int height() const
QgsLabelingEngineInterface * labelingEngine()
Labeling engine (NULL if there'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:229
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 (NULL by default)
double size
Definition: qgssvgcache.cpp:77
void setOutputUnits(OutputUnits u)
QgsRectangle mFullExtent
full extent of the layer set