QGIS API Documentation  2.0.1-Dufour
 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;
91  virtual int addDiagramLayer( QgsVectorLayer* layer, QgsDiagramLayerSettings* s )
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 
114 
119 class CORE_EXPORT QgsMapRenderer : public QObject
120 {
121  Q_OBJECT
122 
123  public:
124 
127  {
129  Pixels
130  //MAP_UNITS probably supported in future versions
131  };
132 
137  {
150  BlendSubtract
151  };
152 
154  QgsMapRenderer();
155 
157  ~QgsMapRenderer();
158 
161  void render( QPainter* painter, double* forceWidthScale = 0 );
162 
164  bool setExtent( const QgsRectangle& extent );
165 
167  QgsRectangle extent() const;
168 
169  const QgsMapToPixel* coordinateTransform() { return &( mRenderContext.mapToPixel() ); }
170 
172  double scale() const { return mScale; }
175  void setScale( double scale ) {mScale = scale;}
176  double mapUnitsPerPixel() const { return mMapUnitsPerPixel; }
177 
178  int width() const { return mSize.width(); };
179  int height() const { return mSize.height(); };
180 
182  void updateScale();
183 
184  QGis::UnitType mapUnits() const;
185  void setMapUnits( QGis::UnitType u );
186 
188  void enableOverviewMode( bool isOverview = true ) { mOverview = isOverview; }
189 
190  void setOutputSize( QSize size, int dpi );
191  void setOutputSize( QSizeF size, double dpi );
192 
194  double outputDpi();
196  QSize outputSize();
197  QSizeF outputSizeF();
198 
204  QgsRectangle layerExtentToOutputExtent( QgsMapLayer* theLayer, QgsRectangle extent );
205 
211  QgsRectangle outputExtentToLayerExtent( QgsMapLayer* theLayer, QgsRectangle extent );
212 
217  QgsPoint layerToMapCoordinates( QgsMapLayer* theLayer, QgsPoint point );
218 
224  QgsRectangle layerToMapCoordinates( QgsMapLayer* theLayer, QgsRectangle rect );
225 
230  QgsPoint mapToLayerCoordinates( QgsMapLayer* theLayer, QgsPoint point );
231 
237  QgsRectangle mapToLayerCoordinates( QgsMapLayer* theLayer, QgsRectangle rect );
238 
240  void setProjectionsEnabled( bool enabled );
241 
243  bool hasCrsTransformEnabled() const;
244 
246  void setDestinationCrs( const QgsCoordinateReferenceSystem& crs );
247 
249  const QgsCoordinateReferenceSystem& destinationCrs() const;
250 
251  void setOutputUnits( OutputUnits u ) {mOutputUnits = u;}
252 
253  OutputUnits outputUnits() const {return mOutputUnits;}
254 
256  QgsRectangle fullExtent();
257 
259  QStringList& layerSet();
260 
262  void setLayerSet( const QStringList& layers );
263 
265  void updateFullExtent();
266 
268  bool readXML( QDomNode & theNode );
269 
271  bool writeXML( QDomNode & theNode, QDomDocument & theDoc );
272 
274  QgsRenderContext* rendererContext() {return &mRenderContext;}
275 
278  QgsLabelingEngineInterface* labelingEngine() { return mLabelingEngine; }
279 
283  void setLabelingEngine( QgsLabelingEngineInterface* iface );
284 
287  static QPainter::CompositionMode getCompositionMode( const QgsMapRenderer::BlendMode blendMode );
290  static QgsMapRenderer::BlendMode getBlendModeEnum( const QPainter::CompositionMode blendMode );
291 
292  signals:
293 
294  void drawingProgress( int current, int total );
295 
296  void hasCrsTransformEnabled( bool flag );
297 
298  void destinationSrsChanged();
299 
300  void updateMap();
301 
302  void mapUnitsChanged();
303 
305  void drawError( QgsMapLayer* );
306 
307  public slots:
308 
310  void onDrawingProgress( int current, int total );
311 
312  protected:
313 
315  void adjustExtentToSize();
316 
323  bool splitLayersExtent( QgsMapLayer* layer, QgsRectangle& extent, QgsRectangle& r2 );
324 
326  static bool mDrawing;
327 
330 
332  double mScale;
333 
336 
339  //
346 
348  bool mOverview;
349 
350  QSizeF mSize;
351 
354 
357 
359  QStringList mLayerSet;
360 
363 
366 
369 
372 
375 
377  QMutex mRenderMutex;
378 
379  private:
380  const QgsCoordinateTransform* tr( QgsMapLayer *layer );
381 };
382 
383 #endif
384