Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FEATURE] Add diagram system that uses the same pal instance as label…
…ing-ng

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@15503 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Mar 15, 2011
1 parent f2d40a7 commit 15d3812
Show file tree
Hide file tree
Showing 35 changed files with 2,371 additions and 77 deletions.
160 changes: 160 additions & 0 deletions python/core/qgsdiagramrendererv2.sip
@@ -0,0 +1,160 @@
struct QgsDiagramLayerSettings
{
%TypeHeaderCode
#include <qgsdiagramrendererv2.h>
%End

//avoid inclusion of QgsPalLabeling
enum Placement
{
AroundPoint, // Point / Polygon
OverPoint, // Point / Polygon
Line, // Line / Polygon
Curved, // Line
Horizontal, // Polygon
Free // Polygon
};

enum LinePlacementFlags
{
OnLine = 1,
AboveLine = 2,
BelowLine = 4,
MapOrientation = 8
};

QgsDiagramLayerSettings();

//pal placement properties
Placement placement;
LinePlacementFlags placementFlags;
int priority; // 0 = low, 10 = high
bool obstacle; // whether it's an obstacle
double dist; // distance from the feature (in mm)
QgsDiagramRendererV2* renderer;
int xPosColumn; //attribute index for x coordinate (or -1 if position not data defined)
int yPosColumn;//attribute index for y coordinate (or -1 if position not data defined)

void readXML( const QDomElement& elem );
void writeXML( QDomElement& layerElem, QDomDocument& doc ) const;
};

//diagram settings for rendering
struct QgsDiagramSettings
{
%TypeHeaderCode
#include <qgsdiagramrendererv2.h>
%End
enum SizeType
{
MM,
MapUnits
};

QgsDiagramSettings();
QFont font;
QList< QColor > categoryColors;
QList< int > categoryIndices;
QSizeF size; //size
SizeType sizeType; //mm or map units
QColor backgroundColor;
QColor penColor;
double penWidth;

//scale range (-1 if no lower / upper bound )
double minScaleDenominator;
double maxScaleDenominator;

void readXML( const QDomElement& elem );
void writeXML( QDomElement& rendererElem, QDomDocument& doc ) const;
};

/**Returns diagram settings for a feature*/
class QgsDiagramRendererV2
{
%TypeHeaderCode
#include <qgsdiagramrendererv2.h>
%End
public:

QgsDiagramRendererV2();
virtual ~QgsDiagramRendererV2();

/**Returns size of the diagram for feature f in map units. Returns an invalid QSizeF in case of error*/
virtual QSizeF sizeMapUnits( const QgsAttributeMap& attributes, const QgsRenderContext& c );

virtual QString rendererName() const = 0;

/**Returns attribute indices needed for diagram rendering*/
virtual QList<int> diagramAttributes() const = 0;

void renderDiagram( const QgsAttributeMap& att, QgsRenderContext& c, const QPointF& pos );

void setDiagram( QgsDiagram* d );
const QgsDiagram* diagram() const;

/**Returns list with all diagram settings in the renderer*/
virtual QList<QgsDiagramSettings> diagramSettings() const = 0;

virtual void readXML( const QDomElement& elem ) = 0;
virtual void writeXML( QDomElement& layerElem, QDomDocument& doc ) const = 0;
};

/**Renders the diagrams for all features with the same settings*/
class QgsSingleCategoryDiagramRenderer: QgsDiagramRendererV2
{
%TypeHeaderCode
#include <qgsdiagramrendererv2.h>
%End
public:
QgsSingleCategoryDiagramRenderer();
~QgsSingleCategoryDiagramRenderer();

QString rendererName() const;

QList<int> diagramAttributes() const;

void setDiagramSettings( const QgsDiagramSettings& s );

QList<QgsDiagramSettings> diagramSettings() const;

void readXML( const QDomElement& elem );
void writeXML( QDomElement& layerElem, QDomDocument& doc ) const;
};

class QgsLinearlyInterpolatedDiagramRenderer: QgsDiagramRendererV2
{
%TypeHeaderCode
#include <qgsdiagramrendererv2.h>
%End
public:
QgsLinearlyInterpolatedDiagramRenderer();
~QgsLinearlyInterpolatedDiagramRenderer();

/**Returns list with all diagram settings in the renderer*/
QList<QgsDiagramSettings> diagramSettings() const;

void setDiagramSettings( const QgsDiagramSettings& s );

QList<int> diagramAttributes() const;

QString rendererName() const;

void setLowerValue( double val );
double lowerValue() const;

void setUpperValue( double val );
double upperValue() const;

void setLowerSize( QSizeF s );
QSizeF lowerSize() const;

void setUpperSize( QSizeF s );
QSizeF upperSize() const;

int classificationAttribute() const;
void setClassificationAttribute( int index );

void readXML( const QDomElement& elem );
void writeXML( QDomElement& layerElem, QDomDocument& doc ) const;
};
1 change: 1 addition & 0 deletions src/analysis/interpolation/qgsinterpolator.cpp
Expand Up @@ -17,6 +17,7 @@

#include "qgsinterpolator.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorlayer.h"
#include "qgsgeometry.h"

QgsInterpolator::QgsInterpolator( const QList<LayerData>& layerData ): mDataIsCached( false ), mLayerData( layerData )
Expand Down
7 changes: 5 additions & 2 deletions src/app/qgisapp.cpp
Expand Up @@ -5663,14 +5663,17 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer* layer )
for ( QMap<QString, QgsMapLayer*>::iterator it = layers.begin(); it != layers.end(); it++ )
{
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( it.value() );
if ( !vlayer || !vlayer->isEditable() || vlayer->customProperty( "labeling" ).toString() != QString( "pal" ) )
if ( !vlayer || !vlayer->isEditable() ||
( !vlayer->diagramRenderer() && vlayer->customProperty( "labeling" ).toString() != QString( "pal" ) ) )
continue;

int colX, colY, colAng;
enableMove =
enableMove ||
( qobject_cast<QgsMapToolMoveLabel*>( mMapTools.mMoveLabel ) &&
qobject_cast<QgsMapToolMoveLabel*>( mMapTools.mMoveLabel )->layerIsMoveable( vlayer, colX, colY ) );
( qobject_cast<QgsMapToolMoveLabel*>( mMapTools.mMoveLabel )->labelMoveable( vlayer, colX, colY )
|| qobject_cast<QgsMapToolMoveLabel*>( mMapTools.mMoveLabel )->diagramMoveable( vlayer, colX, colY ) )
);

enableRotate =
enableRotate ||
Expand Down
1 change: 1 addition & 0 deletions src/app/qgslabelpropertydialog.cpp
Expand Up @@ -18,6 +18,7 @@
#include "qgslabelpropertydialog.h"
#include "qgsmaplayerregistry.h"
#include "qgsmaprenderer.h"
#include "qgsvectorlayer.h"
#include <QColorDialog>
#include <QFontDialog>

Expand Down
35 changes: 33 additions & 2 deletions src/app/qgsmaptoollabel.cpp
Expand Up @@ -280,6 +280,14 @@ bool QgsMapToolLabel::rotationPoint( QgsPoint& pos )
pos = mCurrentLabelPos.cornerPoints.at( 0 );
}

//alignment always center/center and rotation 0 for diagrams
if ( mCurrentLabelPos.isDiagram )
{
pos.setX( pos.x() + mCurrentLabelPos.labelRect.width() / 2.0 );
pos.setY( pos.y() + mCurrentLabelPos.labelRect.height() / 2.0 );
return true;
}

//adapt pos depending on data defined alignment
QString haliString, valiString;
currentAlignment( haliString, valiString );
Expand Down Expand Up @@ -356,7 +364,14 @@ bool QgsMapToolLabel::dataDefinedPosition( QgsVectorLayer* vlayer, int featureId
return false;
}

if ( !layerIsMoveable( vlayer, xCol, yCol ) )
if ( mCurrentLabelPos.isDiagram )
{
if ( !diagramMoveable( vlayer, xCol, yCol ) )
{
return false;
}
}
else if ( !labelMoveable( vlayer, xCol, yCol ) )
{
return false;
}
Expand All @@ -374,7 +389,23 @@ bool QgsMapToolLabel::dataDefinedPosition( QgsVectorLayer* vlayer, int featureId
return true;
}

bool QgsMapToolLabel::layerIsMoveable( const QgsMapLayer* ml, int& xCol, int& yCol ) const
bool QgsMapToolLabel:: diagramMoveable( const QgsMapLayer* ml, int& xCol, int& yCol ) const
{
const QgsVectorLayer* vlayer = dynamic_cast<const QgsVectorLayer*>( ml );
if ( vlayer && vlayer->diagramRenderer() )
{
QgsDiagramLayerSettings dls = vlayer->diagramLayerSettings();
if ( dls.xPosColumn >= 0 && dls.yPosColumn >= 0 )
{
xCol = dls.xPosColumn;
yCol = dls.yPosColumn;
return true;
}
}
return false;
}

bool QgsMapToolLabel::labelMoveable( const QgsMapLayer* ml, int& xCol, int& yCol ) const
{
const QgsVectorLayer* vlayer = dynamic_cast<const QgsVectorLayer*>( ml );
if ( !vlayer || !vlayer->isEditable() )
Expand Down
9 changes: 7 additions & 2 deletions src/app/qgsmaptoollabel.h
Expand Up @@ -31,11 +31,16 @@ class QgsMapToolLabel: public QgsMapTool
QgsMapToolLabel( QgsMapCanvas* canvas );
~QgsMapToolLabel();

/**Returns true if layer move can be applied to a layer
/**Returns true if label move can be applied to a layer
@param xCol out: index of the attribute for data defined x coordinate
@param yCol out: index of the attribute for data defined y coordinate
@return true if labels of layer can be moved*/
bool layerIsMoveable( const QgsMapLayer* ml, int& xCol, int& yCol ) const;
bool labelMoveable( const QgsMapLayer* ml, int& xCol, int& yCol ) const;
/**Returns true if diagram move can be applied to a layer
@param xCol out: index of the attribute for data defined x coordinate
@param yCol out: index of the attribute for data defined y coordinate
@return true if labels of layer can be moved*/
bool diagramMoveable( const QgsMapLayer* ml, int& xCol, int& yCol ) const;

protected:
QgsRubberBand* mLabelRubberBand;
Expand Down
4 changes: 2 additions & 2 deletions src/app/qgsmaptoolmovelabel.cpp
Expand Up @@ -40,13 +40,13 @@ void QgsMapToolMoveLabel::canvasPressEvent( QMouseEvent * e )
}

QgsMapLayer* layer = QgsMapLayerRegistry::instance()->mapLayer( mCurrentLabelPos.layerID );
if ( !layer )
if ( !layer || !layer->isEditable() )
{
return;
}

int xCol, yCol;
if ( layerIsMoveable( layer, xCol, yCol ) )
if ( labelMoveable( layer, xCol, yCol ) || diagramMoveable( layer, xCol, yCol ) )
{
mStartPointMapCoords = toMapCoordinates( e->pos() );
mClickOffsetX = mStartPointMapCoords.x() - mCurrentLabelPos.labelRect.xMinimum();
Expand Down

0 comments on commit 15d3812

Please sign in to comment.