QGIS API Documentation  master-3f58142
src/core/qgsdiagramrendererv2.h
Go to the documentation of this file.
00001 /***************************************************************************
00002     qgsdiagramrendererv2.h
00003     ---------------------
00004     begin                : March 2011
00005     copyright            : (C) 2011 by Marco Hugentobler
00006     email                : marco dot hugentobler at sourcepole dot ch
00007  ***************************************************************************
00008  *                                                                         *
00009  *   This program is free software; you can redistribute it and/or modify  *
00010  *   it under the terms of the GNU General Public License as published by  *
00011  *   the Free Software Foundation; either version 2 of the License, or     *
00012  *   (at your option) any later version.                                   *
00013  *                                                                         *
00014  ***************************************************************************/
00015 #ifndef QGSDIAGRAMRENDERERV2_H
00016 #define QGSDIAGRAMRENDERERV2_H
00017 
00018 #include <QColor>
00019 #include <QFont>
00020 #include <QList>
00021 #include <QPointF>
00022 #include <QSizeF>
00023 #include <QDomDocument>
00024 
00025 #include "qgsfeature.h"
00026 
00027 class QgsDiagram;
00028 class QgsDiagramRendererV2;
00029 class QgsFeature;
00030 class QgsRenderContext;
00031 class QDomElement;
00032 class QgsPalGeometry;
00033 class QgsCoordinateTransform;
00034 class QgsMapToPixel;
00035 namespace pal { class Layer; }
00036 
00037 class CORE_EXPORT QgsDiagramLayerSettings
00038 {
00039   public:
00040     //avoid inclusion of QgsPalLabeling
00041     enum Placement
00042     {
00043       AroundPoint = 0, // Point / Polygon
00044       OverPoint, // Point / Polygon
00045       Line, // Line / Polygon
00046       Curved, // Line
00047       Horizontal, // Polygon
00048       Free // Polygon
00049     };
00050 
00051     enum LinePlacementFlags
00052     {
00053       OnLine    = 1,
00054       AboveLine = 2,
00055       BelowLine = 4,
00056       MapOrientation = 8
00057     };
00058 
00059     QgsDiagramLayerSettings()
00060         : placement( AroundPoint )
00061         , placementFlags( OnLine )
00062         , priority( 5 )
00063         , obstacle( false )
00064         , dist( 0.0 )
00065         , renderer( 0 )
00066         , palLayer( 0 )
00067         , ct( 0 )
00068         , xform( 0 )
00069         , xPosColumn( -1 )
00070         , yPosColumn( -1 )
00071     {
00072     }
00073 
00074     //pal placement properties
00075     Placement placement;
00076     LinePlacementFlags placementFlags;
00077     int priority; // 0 = low, 10 = high
00078     bool obstacle; // whether it's an obstacle
00079     double dist; // distance from the feature (in mm)
00080     QgsDiagramRendererV2* renderer;
00081 
00082     //assigned when layer gets prepared
00083     pal::Layer* palLayer;
00084     const QgsCoordinateTransform* ct;
00085     const QgsMapToPixel* xform;
00086     QList<QgsPalGeometry*> geometries;
00087 
00088     int xPosColumn; //attribute index for x coordinate (or -1 if position not data defined)
00089     int yPosColumn;//attribute index for y coordinate (or -1 if position not data defined)
00090 
00091     void readXML( const QDomElement& elem );
00092     void writeXML( QDomElement& layerElem, QDomDocument& doc ) const;
00093 };
00094 
00095 //diagram settings for rendering
00096 class CORE_EXPORT QgsDiagramSettings
00097 {
00098   public:
00099     enum SizeType
00100     {
00101       MM,
00102       MapUnits
00103     };
00104 
00105     enum LabelPlacementMethod
00106     {
00107       Height,
00108       XHeight
00109     };
00110 
00112     enum DiagramOrientation
00113     {
00114       Up,
00115       Down,
00116       Left,
00117       Right
00118     };
00119 
00120     QgsDiagramSettings(): sizeType( MM ), minScaleDenominator( -1 ), maxScaleDenominator( -1 )
00121     {}
00122     QFont font;
00123     QList< QColor > categoryColors;
00124     QList< int > categoryIndices;
00125     QSizeF size; //size
00126     SizeType sizeType; //mm or map units
00127     QColor backgroundColor;
00128     QColor penColor;
00129     double penWidth;
00130     LabelPlacementMethod labelPlacementMethod;
00131     DiagramOrientation diagramOrientation;
00132     double barWidth;
00133     int transparency; // 0 - 100
00134     bool scaleByArea;
00135     int angleOffset;
00136 
00137     //scale range (-1 if no lower / upper bound )
00138     double minScaleDenominator;
00139     double maxScaleDenominator;
00140 
00142     double minimumSize;
00143 
00144     void readXML( const QDomElement& elem );
00145     void writeXML( QDomElement& rendererElem, QDomDocument& doc ) const;
00146 };
00147 
00148 //additional diagram settings for interpolated size rendering
00149 class CORE_EXPORT QgsDiagramInterpolationSettings
00150 {
00151   public:
00152     QSizeF lowerSize;
00153     QSizeF upperSize;
00154     double lowerValue;
00155     double upperValue;
00157     int classificationAttribute;
00158 };
00159 
00161 class CORE_EXPORT QgsDiagramRendererV2
00162 {
00163   public:
00164 
00165     QgsDiagramRendererV2();
00166     virtual ~QgsDiagramRendererV2();
00167 
00169     virtual QSizeF sizeMapUnits( const QgsAttributes& attributes, const QgsRenderContext& c );
00170 
00171     virtual QString rendererName() const = 0;
00172 
00174     virtual QList<int> diagramAttributes() const = 0;
00175 
00176     void renderDiagram( const QgsAttributes& att, QgsRenderContext& c, const QPointF& pos );
00177 
00178     void setDiagram( QgsDiagram* d );
00179     const QgsDiagram* diagram() const { return mDiagram; }
00180 
00182     virtual QList<QgsDiagramSettings> diagramSettings() const = 0;
00183 
00184     virtual void readXML( const QDomElement& elem ) = 0;
00185     virtual void writeXML( QDomElement& layerElem, QDomDocument& doc ) const = 0;
00186 
00187   protected:
00188 
00194     virtual bool diagramSettings( const QgsAttributes& att, const QgsRenderContext& c, QgsDiagramSettings& s ) = 0;
00195 
00197     virtual QSizeF diagramSize( const QgsAttributes& attributes, const QgsRenderContext& c ) = 0;
00198 
00200     void convertSizeToMapUnits( QSizeF& size, const QgsRenderContext& context ) const;
00201 
00203     static int dpiPaintDevice( const QPainter* );
00204 
00205     //read / write diagram
00206     void _readXML( const QDomElement& elem );
00207     void _writeXML( QDomElement& rendererElem, QDomDocument& doc ) const;
00208 
00210     QgsDiagram* mDiagram;
00211 };
00212 
00214 class CORE_EXPORT QgsSingleCategoryDiagramRenderer : public QgsDiagramRendererV2
00215 {
00216   public:
00217     QgsSingleCategoryDiagramRenderer();
00218     ~QgsSingleCategoryDiagramRenderer();
00219 
00220     QString rendererName() const { return "SingleCategory"; }
00221 
00222     QList<int> diagramAttributes() const { return mSettings.categoryIndices; }
00223 
00224     void setDiagramSettings( const QgsDiagramSettings& s ) { mSettings = s; }
00225 
00226     QList<QgsDiagramSettings> diagramSettings() const;
00227 
00228     void readXML( const QDomElement& elem );
00229     void writeXML( QDomElement& layerElem, QDomDocument& doc ) const;
00230 
00231   protected:
00232     bool diagramSettings( const QgsAttributes&, const QgsRenderContext& c, QgsDiagramSettings& s );
00233 
00234     QSizeF diagramSize( const QgsAttributes& attributes, const QgsRenderContext& c );
00235 
00236   private:
00237     QgsDiagramSettings mSettings;
00238 };
00239 
00240 class CORE_EXPORT QgsLinearlyInterpolatedDiagramRenderer : public QgsDiagramRendererV2
00241 {
00242   public:
00243     QgsLinearlyInterpolatedDiagramRenderer();
00244     ~QgsLinearlyInterpolatedDiagramRenderer();
00245 
00247     QList<QgsDiagramSettings> diagramSettings() const;
00248 
00249     void setDiagramSettings( const QgsDiagramSettings& s ) { mSettings = s; }
00250 
00251     QList<int> diagramAttributes() const;
00252 
00253     QString rendererName() const { return "LinearlyInterpolated"; }
00254 
00255     void setLowerValue( double val ) { mInterpolationSettings.lowerValue = val; }
00256     double lowerValue() const { return mInterpolationSettings.lowerValue; }
00257 
00258     void setUpperValue( double val ) { mInterpolationSettings.upperValue = val; }
00259     double upperValue() const { return mInterpolationSettings.upperValue; }
00260 
00261     void setLowerSize( QSizeF s ) { mInterpolationSettings.lowerSize = s; }
00262     QSizeF lowerSize() const { return mInterpolationSettings.lowerSize; }
00263 
00264     void setUpperSize( QSizeF s ) { mInterpolationSettings.upperSize = s; }
00265     QSizeF upperSize() const { return mInterpolationSettings.upperSize; }
00266 
00267     int classificationAttribute() const { return mInterpolationSettings.classificationAttribute; }
00268     void setClassificationAttribute( int index ) { mInterpolationSettings.classificationAttribute = index; }
00269 
00270     void readXML( const QDomElement& elem );
00271     void writeXML( QDomElement& layerElem, QDomDocument& doc ) const;
00272 
00273   protected:
00274     bool diagramSettings( const QgsAttributes&, const QgsRenderContext& c, QgsDiagramSettings& s );
00275 
00276     QSizeF diagramSize( const QgsAttributes& attributes, const QgsRenderContext& c );
00277 
00278   private:
00279     QgsDiagramSettings mSettings;
00280     QgsDiagramInterpolationSettings mInterpolationSettings;
00281 };
00282 
00283 #endif // QGSDIAGRAMRENDERERV2_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines