QGIS API Documentation  2.8.2-Wien
 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 
67 class CORE_EXPORT QgsLabelingEngineInterface
68 {
69  public:
70 
72 
75  Q_DECL_DEPRECATED virtual void init( QgsMapRenderer* mp ) = 0;
77  virtual void init( const QgsMapSettings& mapSettings ) = 0;
79  virtual bool willUseLayer( QgsVectorLayer* layer ) = 0;
81  virtual void clearActiveLayers() = 0;
83  virtual void clearActiveLayer( const QString& layerID ) = 0;
85  virtual int prepareLayer( QgsVectorLayer* layer, QStringList& attrNames, QgsRenderContext& ctx ) = 0;
87  virtual QgsPalLayerSettings& layer( const QString& layerName ) = 0;
89  virtual int addDiagramLayer( QgsVectorLayer* layer, const QgsDiagramLayerSettings* s )
90  { Q_UNUSED( layer ); Q_UNUSED( s ); return 0; }
92  virtual void registerFeature( const QString& layerID, QgsFeature& feat, const QgsRenderContext& context = QgsRenderContext(), QString dxfLayer = QString::null ) = 0;
94  virtual void registerDiagramFeature( const QString& layerID, QgsFeature& feat, const QgsRenderContext& context = QgsRenderContext() )
95  { Q_UNUSED( layerID ); Q_UNUSED( feat ); Q_UNUSED( context ); }
97  virtual void drawLabeling( QgsRenderContext& context ) = 0;
99  virtual void exit() = 0;
102  Q_DECL_DEPRECATED virtual QList<QgsLabelPosition> labelsAtPosition( const QgsPoint& p ) = 0;
105  Q_DECL_DEPRECATED virtual QList<QgsLabelPosition> labelsWithinRect( const QgsRectangle& r ) = 0;
106 
108  virtual QgsLabelingEngineInterface* clone() = 0;
109 };
110 
111 struct CORE_EXPORT QgsLayerCoordinateTransform
112 {
113  QString srcAuthId;
114  QString destAuthId;
115  int srcDatumTransform; //-1 if unknown or not specified
117 };
118 
119 // ### QGIS 3: remove QgsMapRenderer in favor of QgsMapRendererJob
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  {
168  };
169 
171  QgsMapRenderer();
172 
174  ~QgsMapRenderer();
175 
179  void render( QPainter* painter, double* forceWidthScale = 0 );
180 
182  bool setExtent( const QgsRectangle& extent );
183 
185  QgsRectangle extent() const;
186 
190  void setRotation( double degrees );
191 
194  double rotation() const;
195 
196  const QgsMapToPixel* coordinateTransform() { return &( mRenderContext.mapToPixel() ); }
197 
199  double scale() const { return mScale; }
202  void setScale( double scale ) {mScale = scale;}
203  double mapUnitsPerPixel() const { return mMapUnitsPerPixel; }
204 
205  int width() const { return ( int ) mSize.width(); }
206  int height() const { return ( int ) mSize.height(); }
207 
209  void updateScale();
210 
211  QGis::UnitType mapUnits() const;
212  void setMapUnits( QGis::UnitType u );
213 
215  void enableOverviewMode( bool isOverview = true ) { mOverview = isOverview; }
216 
217  void setOutputSize( QSize size, int dpi );
218  void setOutputSize( QSizeF size, double dpi );
219 
221  double outputDpi();
223  QSize outputSize();
224  QSizeF outputSizeF();
225 
231  QgsRectangle layerExtentToOutputExtent( QgsMapLayer* theLayer, QgsRectangle extent );
232 
238  QgsRectangle outputExtentToLayerExtent( QgsMapLayer* theLayer, QgsRectangle extent );
239 
244  QgsPoint layerToMapCoordinates( QgsMapLayer* theLayer, QgsPoint point );
245 
251  QgsRectangle layerToMapCoordinates( QgsMapLayer* theLayer, QgsRectangle rect );
252 
257  QgsPoint mapToLayerCoordinates( QgsMapLayer* theLayer, QgsPoint point );
258 
264  QgsRectangle mapToLayerCoordinates( QgsMapLayer* theLayer, QgsRectangle rect );
265 
267  void setProjectionsEnabled( bool enabled );
268 
270  bool hasCrsTransformEnabled() const;
271 
273  void setDestinationCrs( const QgsCoordinateReferenceSystem& crs, bool refreshCoordinateTransformInfo = true, bool transformExtent = true );
274 
276  const QgsCoordinateReferenceSystem& destinationCrs() const;
277 
278  void setOutputUnits( OutputUnits u ) {mOutputUnits = u;}
279 
280  OutputUnits outputUnits() const {return mOutputUnits;}
281 
283  QgsRectangle fullExtent();
284 
286  QStringList& layerSet();
287 
289  void setLayerSet( const QStringList& layers );
290 
292  void updateFullExtent();
293 
295  bool readXML( QDomNode & theNode );
296 
298  bool writeXML( QDomNode & theNode, QDomDocument & theDoc );
299 
301  QgsRenderContext* rendererContext() {return &mRenderContext;}
302 
304  QgsLabelingEngineInterface* labelingEngine() { return mLabelingEngine; }
305 
308  void setLabelingEngine( QgsLabelingEngineInterface* iface );
309 
311  static QPainter::CompositionMode getCompositionMode( const QgsMapRenderer::BlendMode &blendMode );
313  static QgsMapRenderer::BlendMode getBlendModeEnum( const QPainter::CompositionMode &blendMode );
314 
315  void addLayerCoordinateTransform( const QString& layerId, const QString& srcAuthId, const QString& destAuthId, int srcDatumTransform = -1, int destDatumTransform = -1 );
316  void clearLayerCoordinateTransforms();
317 
318  const QgsCoordinateTransform* transformation( const QgsMapLayer *layer ) const;
319 
322  const QgsMapSettings& mapSettings();
323 
330  bool splitLayersExtent( QgsMapLayer* layer, QgsRectangle& extent, QgsRectangle& r2 );
331 
332  signals:
333 
335  void drawingProgress( int current, int total );
336 
341 #ifndef Q_MOC_RUN
342  Q_DECL_DEPRECATED
343 #endif
344  void hasCrsTransformEnabled( bool flag );
345 
349  void hasCrsTransformEnabledChanged( bool flag );
350 
351  void destinationSrsChanged();
352 
354  void updateMap();
355 
356  void mapUnitsChanged();
357 
359  void drawError( QgsMapLayer* );
360 
363  void extentsChanged();
364 
367  void rotationChanged( double );
368 
370  void datumTransformInfoRequested( const QgsMapLayer* ml, const QString& srcAuthId, const QString& destAuthId ) const;
371 
372 
373  public slots:
374 
376  Q_DECL_DEPRECATED void onDrawingProgress( int current, int total );
377 
378  protected:
379 
381  void adjustExtentToSize();
382 
384  static bool mDrawing;
385 
388 
390  double mScale;
391 
393  double mRotation;
394 
397 
400  //
406 
408  bool mOverview;
409 
410  QSizeF mSize;
411 
414 
417 
419  QStringList mLayerSet;
420 
423 
426 
429 
432 
435 
437  QMutex mRenderMutex;
438 
441 
442  QHash< QString, QgsLayerCoordinateTransform > mLayerCoordinateTransformInfo;
443 };
444 
445 #endif
446