Skip to content
Permalink
Browse files

split diagram code into seperate files

  • Loading branch information
m-kuhn committed Aug 9, 2012
1 parent 48bc5c5 commit f148e8f23fb018925480fcf91964c658769cb70e
@@ -41,7 +41,12 @@ SET(QGIS_CORE_SRCS
symbology-ng/qgsellipsesymbollayerv2.cpp
symbology-ng/qgspointdisplacementrenderer.cpp
symbology-ng/qgsvectorfieldsymbollayer.cpp


diagram/qgsdiagram.cpp
diagram/qgspiediagram.cpp
diagram/qgstextdiagram.cpp
diagram/qgshistogramdiagram.cpp

qgis.cpp
qgsapplication.cpp
qgsattributeaction.cpp
@@ -54,7 +59,6 @@ SET(QGIS_CORE_SRCS
qgsdatasourceuri.cpp
qgsdataitem.cpp
qgsdbfilterproxymodel.cpp
qgsdiagram.cpp
qgsdiagramrendererv2.cpp
qgsdistancearea.cpp
qgsexpression.cpp
@@ -383,6 +387,12 @@ SET(QGIS_CORE_HDRS
qgsvectoroverlay.h
qgstolerance.h

diagram/qgsdiagram.h
diagram/qgspiediagram.h
diagram/qgstextdiagram.h
diagram/qgshistogramdiagram.h


composer/qgslegendmodel.h
composer/qgscomposerlegenditem.h

@@ -0,0 +1,71 @@
/***************************************************************************
qgsdiagram.cpp
---------------------
begin : March 2011
copyright : (C) 2011 by Marco Hugentobler
email : marco dot hugentobler at sourcepole dot ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsdiagram.h"
#include "qgsdiagramrendererv2.h"
#include "qgsrendercontext.h"

#include <QPainter>

void QgsDiagram::setPenWidth( QPen& pen, const QgsDiagramSettings& s, const QgsRenderContext& c )
{
if ( s.sizeType == QgsDiagramSettings::MM )
{
pen.setWidthF( s.penWidth * c.scaleFactor() );
}
else
{
pen.setWidthF( s.penWidth / c.mapToPixel().mapUnitsPerPixel() );
}
}

QSizeF QgsDiagram::sizePainterUnits( const QSizeF& size, const QgsDiagramSettings& s, const QgsRenderContext& c )
{
Q_UNUSED( size );
if ( s.sizeType == QgsDiagramSettings::MM )
{
return QSizeF( s.size.width() * c.scaleFactor(), s.size.height() * c.scaleFactor() );
}
else
{
return QSizeF( s.size.width() / c.mapToPixel().mapUnitsPerPixel(), s.size.height() / c.mapToPixel().mapUnitsPerPixel() );
}
}

float QgsDiagram::sizePainterUnits( float l, const QgsDiagramSettings& s, const QgsRenderContext& c )
{
if ( s.sizeType == QgsDiagramSettings::MM )
{
return l * c.scaleFactor();
}
else
{
return l / c.mapToPixel().mapUnitsPerPixel();
}
}

QFont QgsDiagram::scaledFont( const QgsDiagramSettings& s, const QgsRenderContext& c )
{
QFont f = s.font;
if ( s.sizeType == QgsDiagramSettings::MM )
{
f.setPixelSize( s.font.pointSizeF() * 0.376 * c.scaleFactor() );
}
else
{
f.setPixelSize( s.font.pointSizeF() / c.mapToPixel().mapUnitsPerPixel() );
}

return f;
}
@@ -0,0 +1,51 @@
/***************************************************************************
qgsdiagram.h
---------------------
begin : March 2011
copyright : (C) 2011 by Marco Hugentobler
email : marco dot hugentobler at sourcepole dot ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSDIAGRAM_H
#define QGSDIAGRAM_H

#include "qgsfeature.h"
#include <QPen>
#include <QBrush>

class QPainter;
class QPointF;
struct QgsDiagramSettings;
struct QgsDiagramInterpolationSettings;

class QgsRenderContext;



/**Base class for all diagram types*/
class CORE_EXPORT QgsDiagram
{
public:
virtual ~QgsDiagram() {}
/**Draws the diagram at the given position (in pixel coordinates)*/
virtual void renderDiagram( const QgsAttributeMap& att, QgsRenderContext& c, const QgsDiagramSettings& s, const QPointF& position ) = 0;
virtual QString diagramName() const = 0;
/**Returns the size in map units the diagram will use to render.*/
virtual QSizeF diagramSize( const QgsAttributeMap& attributes, const QgsRenderContext& c, const QgsDiagramSettings& s ) = 0;
/**Returns the size in map units the diagram will use to render. Interpolat size*/
virtual QSizeF diagramSize( const QgsAttributeMap& attributes, const QgsRenderContext& c, const QgsDiagramSettings& s, const QgsDiagramInterpolationSettings& is ) = 0;

protected:
void setPenWidth( QPen& pen, const QgsDiagramSettings& s, const QgsRenderContext& c );
QSizeF sizePainterUnits( const QSizeF& size, const QgsDiagramSettings& s, const QgsRenderContext& c );
float sizePainterUnits( float l, const QgsDiagramSettings& s, const QgsRenderContext& c );
QFont scaledFont( const QgsDiagramSettings& s, const QgsRenderContext& c );
};

#endif // QGSDIAGRAM_H
@@ -0,0 +1,157 @@
/***************************************************************************
qgsdiagram.cpp
---------------------
begin : August 2012
copyright : (C) 2012 by Matthias Kuhn
email : matthias dot kuhn at gmx dot ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgshistogramdiagram.h"
#include "qgsdiagramrendererv2.h"
#include "qgsrendercontext.h"

#include <QPainter>

QgsHistogramDiagram::QgsHistogramDiagram()
{
mCategoryBrush.setStyle( Qt::SolidPattern );
mPen.setStyle( Qt::SolidLine );
mScaleFactor = 0;
}

QgsHistogramDiagram::~QgsHistogramDiagram()
{
}

QSizeF QgsHistogramDiagram::diagramSize( const QgsAttributeMap& attributes, const QgsRenderContext& c, const QgsDiagramSettings& s, const QgsDiagramInterpolationSettings& is )
{
QgsAttributeMap::const_iterator attIt = attributes.constBegin();
if ( attIt == attributes.constEnd() )
{
return QSizeF(); //zero size if no attributes
}

double maxValue = attIt.value().toDouble();

for ( ; attIt != attributes.constEnd(); ++attIt )
{
maxValue = qMax( attIt.value().toDouble(), maxValue );
}

// Scale, if extension is smaller than the specified minimum
if ( maxValue < s.minimumSize )
{
maxValue = s.minimumSize;
}

mScaleFactor = ( maxValue - is.lowerValue ) / ( is.upperValue - is.lowerValue );

switch ( s.diagramOrientation )
{
case QgsDiagramSettings::Up:
case QgsDiagramSettings::Down:
return QSizeF( s.barWidth * attributes.size(), maxValue );

case QgsDiagramSettings::Right:
case QgsDiagramSettings::Left:
return QSizeF( maxValue, s.barWidth * attributes.size() );
}

return QSizeF();
}

QSizeF QgsHistogramDiagram::diagramSize( const QgsAttributeMap& attributes, const QgsRenderContext& c, const QgsDiagramSettings& s )
{
QgsAttributeMap::const_iterator attIt = attributes.constBegin();
if ( attIt == attributes.constEnd() )
{
return QSizeF(); //zero size if no attributes
}

double maxValue = attIt.value().toDouble();

for ( ; attIt != attributes.constEnd(); ++attIt )
{
maxValue = qMax( attIt.value().toDouble(), maxValue );
}

switch ( s.diagramOrientation )
{
case QgsDiagramSettings::Up:
case QgsDiagramSettings::Down:
mScaleFactor = maxValue / s.size.height();
return QSizeF( s.barWidth * attributes.size(), s.size.height() );

case QgsDiagramSettings::Right:
case QgsDiagramSettings::Left:
mScaleFactor = maxValue / s.size.width();
return QSizeF( s.size.width(), s.barWidth * attributes.size() );
}

return QSizeF();
}

void QgsHistogramDiagram::renderDiagram( const QgsAttributeMap& att, QgsRenderContext& c, const QgsDiagramSettings& s, const QPointF& position )
{
QPainter* p = c.painter();
if ( !p )
{
return;
}

QList<double> values;

QList<int>::const_iterator catIt = s.categoryIndices.constBegin();
for ( ; catIt != s.categoryIndices.constEnd(); ++catIt )
{
double currentVal = att[*catIt].toDouble();
values.push_back( currentVal );
}

double currentOffset = 0 - ( values.size() * s.barWidth ) / 2;
double scaledWidth = sizePainterUnits( s.barWidth, s, c );

double baseX = position.x();
double baseY = position.y();

mPen.setColor( s.penColor );
setPenWidth( mPen, s, c );
p->setPen( mPen );

QList<double>::const_iterator valIt = values.constBegin();
QList< QColor >::const_iterator colIt = s.categoryColors.constBegin();
for ( ; valIt != values.constEnd(); ++valIt, ++colIt )
{
double length = sizePainterUnits( *valIt * mScaleFactor, s, c );

mCategoryBrush.setColor( *colIt );
p->setBrush( mCategoryBrush );

switch ( s.diagramOrientation )
{
case QgsDiagramSettings::Up:
p->drawRect( baseX + currentOffset, baseY, scaledWidth, 0 - length );
break;

case QgsDiagramSettings::Down:
p->drawRect( baseX + currentOffset, baseY, scaledWidth, length );
break;

case QgsDiagramSettings::Right:
p->drawRect( baseX, baseY + currentOffset, 0 - length, scaledWidth );
break;

case QgsDiagramSettings::Left:
p->drawRect( baseX, baseY + currentOffset, length, scaledWidth );
break;
}

currentOffset += scaledWidth;
}
}
@@ -0,0 +1,50 @@
/***************************************************************************
qgsdiagram.h
---------------------
begin : August 2012
copyright : (C) 2012 by Matthias Kuhn
email : matthias dot kuhn at gmx dot ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSHISTOGRAMDIAGRAM_H
#define QGSHISTOGRAMDIAGRAM_H

#include "qgsdiagram.h"
#include "qgsfeature.h"
#include <QPen>
#include <QBrush>

class QPainter;
class QPointF;
struct QgsDiagramSettings;
struct QgsDiagramInterpolationSettings;

class QgsRenderContext;


class CORE_EXPORT QgsHistogramDiagram: public QgsDiagram
{
public:
QgsHistogramDiagram();
~QgsHistogramDiagram();

void renderDiagram( const QgsAttributeMap& att, QgsRenderContext& c, const QgsDiagramSettings& s, const QPointF& position );
QSizeF diagramSize( const QgsAttributeMap& attributes, const QgsRenderContext& c, const QgsDiagramSettings& s );
QSizeF diagramSize( const QgsAttributeMap& attributes, const QgsRenderContext& c, const QgsDiagramSettings& s, const QgsDiagramInterpolationSettings& is );
QString diagramName() const { return "Histogram"; }

private:
QBrush mCategoryBrush;
QPen mPen;
double mScaleFactor;
};


#endif // QGSHISTOGRAMDIAGRAM_H

0 comments on commit f148e8f

Please sign in to comment.
You can’t perform that action at this time.