|
Quantum GIS API Documentation
master-693a1fe
|
00001 /*************************************************************************** 00002 qgshistogramdiagram.cpp 00003 --------------------- 00004 begin : August 2012 00005 copyright : (C) 2012 by Matthias Kuhn 00006 email : matthias dot kuhn at gmx 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 #include "qgshistogramdiagram.h" 00016 #include "qgsdiagramrendererv2.h" 00017 #include "qgsrendercontext.h" 00018 00019 #include <QPainter> 00020 00021 QgsHistogramDiagram::QgsHistogramDiagram() 00022 { 00023 mCategoryBrush.setStyle( Qt::SolidPattern ); 00024 mPen.setStyle( Qt::SolidLine ); 00025 mScaleFactor = 0; 00026 } 00027 00028 QgsHistogramDiagram::~QgsHistogramDiagram() 00029 { 00030 } 00031 00032 QSizeF QgsHistogramDiagram::diagramSize( const QgsAttributes& attributes, const QgsRenderContext& c, const QgsDiagramSettings& s, const QgsDiagramInterpolationSettings& is ) 00033 { 00034 Q_UNUSED( c ); 00035 QSize size; 00036 if ( attributes.count() == 0 ) 00037 { 00038 return QSizeF(); //zero size if no attributes 00039 } 00040 00041 double maxValue = attributes[0].toDouble(); 00042 00043 for ( int i = 1; i < attributes.count(); ++i ) 00044 { 00045 maxValue = qMax( attributes[i].toDouble(), maxValue ); 00046 } 00047 00048 // Scale, if extension is smaller than the specified minimum 00049 if ( maxValue < s.minimumSize ) 00050 { 00051 maxValue = s.minimumSize; 00052 } 00053 00054 switch ( s.diagramOrientation ) 00055 { 00056 case QgsDiagramSettings::Up: 00057 case QgsDiagramSettings::Down: 00058 mScaleFactor = (( is.upperSize.width() - is.lowerSize.height() ) / ( is.upperValue - is.lowerValue ) ); 00059 size.scale( s.barWidth * attributes.size(), maxValue * mScaleFactor, Qt::IgnoreAspectRatio ); 00060 break; 00061 00062 case QgsDiagramSettings::Right: 00063 case QgsDiagramSettings::Left: 00064 mScaleFactor = (( is.upperSize.width() - is.lowerSize.width() ) / ( is.upperValue - is.lowerValue ) ); 00065 size.scale( maxValue * mScaleFactor, s.barWidth * attributes.size(), Qt::IgnoreAspectRatio ); 00066 break; 00067 } 00068 00069 return size; 00070 } 00071 00072 QSizeF QgsHistogramDiagram::diagramSize( const QgsAttributes& attributes, const QgsRenderContext& c, const QgsDiagramSettings& s ) 00073 { 00074 Q_UNUSED( c ); 00075 QSizeF size; 00076 00077 if ( attributes.count() == 0 ) 00078 { 00079 return QSizeF(); //zero size if no attributes 00080 } 00081 00082 double maxValue = attributes[0].toDouble(); 00083 00084 for ( int i = 0; i < attributes.count(); ++i ) 00085 { 00086 maxValue = qMax( attributes[i].toDouble(), maxValue ); 00087 } 00088 00089 switch ( s.diagramOrientation ) 00090 { 00091 case QgsDiagramSettings::Up: 00092 case QgsDiagramSettings::Down: 00093 mScaleFactor = maxValue / s.size.height(); 00094 size.scale( s.barWidth * s.categoryColors.size(), s.size.height(), Qt::IgnoreAspectRatio ); 00095 break; 00096 00097 case QgsDiagramSettings::Right: 00098 case QgsDiagramSettings::Left: 00099 default: // just in case... 00100 mScaleFactor = maxValue / s.size.width(); 00101 size.scale( s.size.width(), s.barWidth * s.categoryColors.size(), Qt::IgnoreAspectRatio ); 00102 break; 00103 } 00104 00105 return size; 00106 } 00107 00108 void QgsHistogramDiagram::renderDiagram( const QgsAttributes& att, QgsRenderContext& c, const QgsDiagramSettings& s, const QPointF& position ) 00109 { 00110 QPainter* p = c.painter(); 00111 if ( !p ) 00112 { 00113 return; 00114 } 00115 00116 QList<double> values; 00117 00118 QList<int>::const_iterator catIt = s.categoryIndices.constBegin(); 00119 for ( ; catIt != s.categoryIndices.constEnd(); ++catIt ) 00120 { 00121 double currentVal = att[*catIt].toDouble(); 00122 values.push_back( currentVal ); 00123 } 00124 00125 double currentOffset = 0; 00126 double scaledWidth = sizePainterUnits( s.barWidth, s, c ); 00127 00128 double baseX = position.x(); 00129 double baseY = position.y(); 00130 00131 mPen.setColor( s.penColor ); 00132 setPenWidth( mPen, s, c ); 00133 p->setPen( mPen ); 00134 00135 QList<double>::const_iterator valIt = values.constBegin(); 00136 QList< QColor >::const_iterator colIt = s.categoryColors.constBegin(); 00137 for ( ; valIt != values.constEnd(); ++valIt, ++colIt ) 00138 { 00139 double length = sizePainterUnits( *valIt * mScaleFactor, s, c ); 00140 00141 mCategoryBrush.setColor( *colIt ); 00142 p->setBrush( mCategoryBrush ); 00143 00144 switch ( s.diagramOrientation ) 00145 { 00146 case QgsDiagramSettings::Up: 00147 p->drawRect( baseX + currentOffset, baseY, scaledWidth, 0 - length ); 00148 break; 00149 00150 case QgsDiagramSettings::Down: 00151 p->drawRect( baseX + currentOffset, baseY, scaledWidth, length ); 00152 break; 00153 00154 case QgsDiagramSettings::Right: 00155 p->drawRect( baseX, baseY + currentOffset, length, scaledWidth ); 00156 break; 00157 00158 case QgsDiagramSettings::Left: 00159 p->drawRect( baseX, baseY + currentOffset, 0 - length, scaledWidth ); 00160 break; 00161 } 00162 00163 currentOffset += scaledWidth; 00164 } 00165 }