QGIS API Documentation  2.5.0-Master
 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 #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 ) {}
51  QgsLabelPosition(): featureId( -1 ), rotation( 0 ), labelRect( QgsRectangle() ), width( 0 ), height( 0 ), layerID( "" ), labelText( "" ), labelFont( QFont() ), upsideDown( false ), isDiagram( false ), isPinned( false ) {}
52  int featureId;
53  double rotation;
54  QVector< QgsPoint > cornerPoints;
56  double width;
57  double height;
58  QString layerID;
59  QString labelText;
60  QFont labelFont;
61  bool upsideDown;
62  bool isDiagram;
63  bool isPinned;
64 };
65 
69 class CORE_EXPORT QgsLabelingEngineInterface
70 {
71  public:
72 
74 
77  Q_DECL_DEPRECATED virtual void init( QgsMapRenderer* mp ) = 0;
79  virtual void init( const QgsMapSettings& mapSettings ) = 0;
81  virtual bool willUseLayer( QgsVectorLayer* layer ) = 0;
84  virtual void clearActiveLayers() = 0;
87  virtual void clearActiveLayer( const QString& layerID ) = 0;
90  virtual int prepareLayer( QgsVectorLayer* layer, QStringList& attrNames, QgsRenderContext& ctx ) = 0;
93  virtual QgsPalLayerSettings& layer( const QString& layerName ) = 0;
95  virtual int addDiagramLayer( QgsVectorLayer* layer, const QgsDiagramLayerSettings* s )
96  { Q_UNUSED( layer ); Q_UNUSED( s ); return 0; }
98  virtual void registerFeature( const QString& layerID, QgsFeature& feat, const QgsRenderContext& context = QgsRenderContext() ) = 0;
100  virtual void registerDiagramFeature( const QString& layerID, QgsFeature& feat, const QgsRenderContext& context = QgsRenderContext() )
101  { Q_UNUSED( layerID ); Q_UNUSED( feat ); Q_UNUSED( context ); }
103  virtual void drawLabeling( QgsRenderContext& context ) = 0;
105  virtual void exit() = 0;
109  Q_DECL_DEPRECATED virtual QList<QgsLabelPosition> labelsAtPosition( const QgsPoint& p ) = 0;
113  Q_DECL_DEPRECATED virtual QList<QgsLabelPosition> labelsWithinRect( const QgsRectangle& r ) = 0;
114 
116  virtual QgsLabelingEngineInterface* clone() = 0;
117 };
118 
119 struct CORE_EXPORT QgsLayerCoordinateTransform
120 {
121  QString srcAuthId;
122  QString destAuthId;
123  int srcDatumTransform; //-1 if unknown or not specified
125 };
126 
127 // ### QGIS 3: remove QgsMapRenderer in favor of QgsMapRendererJob
128 
133 class CORE_EXPORT QgsMapRenderer : public QObject
134 {
135  Q_OBJECT
136 
137  public:
138 
141  {
143  Pixels
144  //MAP_UNITS probably supported in future versions
145  };
146 
151  {
164  BlendSubtract
165  };
166 
168  QgsMapRenderer();
169 
171  ~QgsMapRenderer();
172 
176  void render( QPainter* painter, double* forceWidthScale = 0 );
177 
179  bool setExtent( const QgsRectangle& extent );
180 
182  QgsRectangle extent() const;
183 
184  const QgsMapToPixel* coordinateTransform() { return &( mRenderContext.mapToPixel() ); }
185 
187  double scale() const { return mScale; }
190  void setScale( double scale ) {mScale = scale;}
191  double mapUnitsPerPixel() const { return mMapUnitsPerPixel; }
192 
193  int width() const { return ( int ) mSize.width(); }
194  int height() const { return ( int ) mSize.height(); }
195 
197  void updateScale();
198 
199  QGis::UnitType mapUnits() const;
200  void setMapUnits( QGis::UnitType u );
201 
203  void enableOverviewMode( bool isOverview = true ) { mOverview = isOverview; }
204 
205  void setOutputSize( QSize size, int dpi );
206  void setOutputSize( QSizeF size, double dpi );
207 
209  double outputDpi();
211  QSize outputSize();
212  QSizeF outputSizeF();
213 
219  QgsRectangle layerExtentToOutputExtent( QgsMapLayer* theLayer, QgsRectangle extent );
220 
226  QgsRectangle outputExtentToLayerExtent( QgsMapLayer* theLayer, QgsRectangle extent );
227 
232  QgsPoint layerToMapCoordinates( QgsMapLayer* theLayer, QgsPoint point );
233 
239  QgsRectangle layerToMapCoordinates( QgsMapLayer* theLayer, QgsRectangle rect );
240 
245  QgsPoint mapToLayerCoordinates( QgsMapLayer* theLayer, QgsPoint point );
246 
252  QgsRectangle mapToLayerCoordinates( QgsMapLayer* theLayer, QgsRectangle rect );
253 
255  void setProjectionsEnabled( bool enabled );
256 
258  bool hasCrsTransformEnabled() const;
259 
261  void setDestinationCrs( const QgsCoordinateReferenceSystem& crs, bool refreshCoordinateTransformInfo = true, bool transformExtent = true );
262 
264  const QgsCoordinateReferenceSystem& destinationCrs() const;
265 
266  void setOutputUnits( OutputUnits u ) {mOutputUnits = u;}
267 
268  OutputUnits outputUnits() const {return mOutputUnits;}
269 
271  QgsRectangle fullExtent();
272 
274  QStringList& layerSet();
275 
277  void setLayerSet( const QStringList& layers );
278 
280  void updateFullExtent();
281 
283  bool readXML( QDomNode & theNode );
284 
286  bool writeXML( QDomNode & theNode, QDomDocument & theDoc );
287 
289  QgsRenderContext* rendererContext() {return &mRenderContext;}
290 
293  QgsLabelingEngineInterface* labelingEngine() { return mLabelingEngine; }
294 
298  void setLabelingEngine( QgsLabelingEngineInterface* iface );
299 
302  static QPainter::CompositionMode getCompositionMode( const QgsMapRenderer::BlendMode &blendMode );
305  static QgsMapRenderer::BlendMode getBlendModeEnum( const QPainter::CompositionMode &blendMode );
306 
307  void addLayerCoordinateTransform( const QString& layerId, const QString& srcAuthId, const QString& destAuthId, int srcDatumTransform = -1, int destDatumTransform = -1 );
308  void clearLayerCoordinateTransforms();
309 
310  const QgsCoordinateTransform* transformation( const QgsMapLayer *layer ) const;
311 
314  const QgsMapSettings& mapSettings();
315 
316  signals:
317 
319  void drawingProgress( int current, int total );
320 
325 #ifndef Q_MOC_RUN
326  Q_DECL_DEPRECATED
327 #endif
328  void hasCrsTransformEnabled( bool flag );
329 
333  void hasCrsTransformEnabledChanged( bool flag );
334 
335  void destinationSrsChanged();
336 
338  void updateMap();
339 
340  void mapUnitsChanged();
341 
343  void drawError( QgsMapLayer* );
344 
347  void extentsChanged();
348 
350  void datumTransformInfoRequested( const QgsMapLayer* ml, const QString& srcAuthId, const QString& destAuthId ) const;
351 
352 
353  public slots:
354 
356  Q_DECL_DEPRECATED void onDrawingProgress( int current, int total );
357 
358  protected:
359 
361  void adjustExtentToSize();
362 
369  bool splitLayersExtent( QgsMapLayer* layer, QgsRectangle& extent, QgsRectangle& r2 );
370 
372  static bool mDrawing;
373 
376 
378  double mScale;
379 
382 
385  //
392 
394  bool mOverview;
395 
396  QSizeF mSize;
397 
400 
403 
405  QStringList mLayerSet;
406 
409 
412 
415 
418 
421 
423  QMutex mRenderMutex;
424 
427 
428  QHash< QString, QgsLayerCoordinateTransform > mLayerCoordinateTransformInfo;
429 
430 };
431 
432 #endif
433