Skip to content

Commit c3ac6df

Browse files
author
mhugent
committed
[FEATURE] Add diagram system that uses the same pal instance as labeling-ng
git-svn-id: http://svn.osgeo.org/qgis/trunk@15503 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 393e206 commit c3ac6df

35 files changed

+2371
-77
lines changed

python/core/qgsdiagramrendererv2.sip

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
struct QgsDiagramLayerSettings
2+
{
3+
%TypeHeaderCode
4+
#include <qgsdiagramrendererv2.h>
5+
%End
6+
7+
//avoid inclusion of QgsPalLabeling
8+
enum Placement
9+
{
10+
AroundPoint, // Point / Polygon
11+
OverPoint, // Point / Polygon
12+
Line, // Line / Polygon
13+
Curved, // Line
14+
Horizontal, // Polygon
15+
Free // Polygon
16+
};
17+
18+
enum LinePlacementFlags
19+
{
20+
OnLine = 1,
21+
AboveLine = 2,
22+
BelowLine = 4,
23+
MapOrientation = 8
24+
};
25+
26+
QgsDiagramLayerSettings();
27+
28+
//pal placement properties
29+
Placement placement;
30+
LinePlacementFlags placementFlags;
31+
int priority; // 0 = low, 10 = high
32+
bool obstacle; // whether it's an obstacle
33+
double dist; // distance from the feature (in mm)
34+
QgsDiagramRendererV2* renderer;
35+
int xPosColumn; //attribute index for x coordinate (or -1 if position not data defined)
36+
int yPosColumn;//attribute index for y coordinate (or -1 if position not data defined)
37+
38+
void readXML( const QDomElement& elem );
39+
void writeXML( QDomElement& layerElem, QDomDocument& doc ) const;
40+
};
41+
42+
//diagram settings for rendering
43+
struct QgsDiagramSettings
44+
{
45+
%TypeHeaderCode
46+
#include <qgsdiagramrendererv2.h>
47+
%End
48+
enum SizeType
49+
{
50+
MM,
51+
MapUnits
52+
};
53+
54+
QgsDiagramSettings();
55+
QFont font;
56+
QList< QColor > categoryColors;
57+
QList< int > categoryIndices;
58+
QSizeF size; //size
59+
SizeType sizeType; //mm or map units
60+
QColor backgroundColor;
61+
QColor penColor;
62+
double penWidth;
63+
64+
//scale range (-1 if no lower / upper bound )
65+
double minScaleDenominator;
66+
double maxScaleDenominator;
67+
68+
void readXML( const QDomElement& elem );
69+
void writeXML( QDomElement& rendererElem, QDomDocument& doc ) const;
70+
};
71+
72+
/**Returns diagram settings for a feature*/
73+
class QgsDiagramRendererV2
74+
{
75+
%TypeHeaderCode
76+
#include <qgsdiagramrendererv2.h>
77+
%End
78+
public:
79+
80+
QgsDiagramRendererV2();
81+
virtual ~QgsDiagramRendererV2();
82+
83+
/**Returns size of the diagram for feature f in map units. Returns an invalid QSizeF in case of error*/
84+
virtual QSizeF sizeMapUnits( const QgsAttributeMap& attributes, const QgsRenderContext& c );
85+
86+
virtual QString rendererName() const = 0;
87+
88+
/**Returns attribute indices needed for diagram rendering*/
89+
virtual QList<int> diagramAttributes() const = 0;
90+
91+
void renderDiagram( const QgsAttributeMap& att, QgsRenderContext& c, const QPointF& pos );
92+
93+
void setDiagram( QgsDiagram* d );
94+
const QgsDiagram* diagram() const;
95+
96+
/**Returns list with all diagram settings in the renderer*/
97+
virtual QList<QgsDiagramSettings> diagramSettings() const = 0;
98+
99+
virtual void readXML( const QDomElement& elem ) = 0;
100+
virtual void writeXML( QDomElement& layerElem, QDomDocument& doc ) const = 0;
101+
};
102+
103+
/**Renders the diagrams for all features with the same settings*/
104+
class QgsSingleCategoryDiagramRenderer: QgsDiagramRendererV2
105+
{
106+
%TypeHeaderCode
107+
#include <qgsdiagramrendererv2.h>
108+
%End
109+
public:
110+
QgsSingleCategoryDiagramRenderer();
111+
~QgsSingleCategoryDiagramRenderer();
112+
113+
QString rendererName() const;
114+
115+
QList<int> diagramAttributes() const;
116+
117+
void setDiagramSettings( const QgsDiagramSettings& s );
118+
119+
QList<QgsDiagramSettings> diagramSettings() const;
120+
121+
void readXML( const QDomElement& elem );
122+
void writeXML( QDomElement& layerElem, QDomDocument& doc ) const;
123+
};
124+
125+
class QgsLinearlyInterpolatedDiagramRenderer: QgsDiagramRendererV2
126+
{
127+
%TypeHeaderCode
128+
#include <qgsdiagramrendererv2.h>
129+
%End
130+
public:
131+
QgsLinearlyInterpolatedDiagramRenderer();
132+
~QgsLinearlyInterpolatedDiagramRenderer();
133+
134+
/**Returns list with all diagram settings in the renderer*/
135+
QList<QgsDiagramSettings> diagramSettings() const;
136+
137+
void setDiagramSettings( const QgsDiagramSettings& s );
138+
139+
QList<int> diagramAttributes() const;
140+
141+
QString rendererName() const;
142+
143+
void setLowerValue( double val );
144+
double lowerValue() const;
145+
146+
void setUpperValue( double val );
147+
double upperValue() const;
148+
149+
void setLowerSize( QSizeF s );
150+
QSizeF lowerSize() const;
151+
152+
void setUpperSize( QSizeF s );
153+
QSizeF upperSize() const;
154+
155+
int classificationAttribute() const;
156+
void setClassificationAttribute( int index );
157+
158+
void readXML( const QDomElement& elem );
159+
void writeXML( QDomElement& layerElem, QDomDocument& doc ) const;
160+
};

src/analysis/interpolation/qgsinterpolator.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "qgsinterpolator.h"
1919
#include "qgsvectordataprovider.h"
20+
#include "qgsvectorlayer.h"
2021
#include "qgsgeometry.h"
2122

2223
QgsInterpolator::QgsInterpolator( const QList<LayerData>& layerData ): mDataIsCached( false ), mLayerData( layerData )

src/app/qgisapp.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5663,14 +5663,17 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer* layer )
56635663
for ( QMap<QString, QgsMapLayer*>::iterator it = layers.begin(); it != layers.end(); it++ )
56645664
{
56655665
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( it.value() );
5666-
if ( !vlayer || !vlayer->isEditable() || vlayer->customProperty( "labeling" ).toString() != QString( "pal" ) )
5666+
if ( !vlayer || !vlayer->isEditable() ||
5667+
( !vlayer->diagramRenderer() && vlayer->customProperty( "labeling" ).toString() != QString( "pal" ) ) )
56675668
continue;
56685669

56695670
int colX, colY, colAng;
56705671
enableMove =
56715672
enableMove ||
56725673
( qobject_cast<QgsMapToolMoveLabel*>( mMapTools.mMoveLabel ) &&
5673-
qobject_cast<QgsMapToolMoveLabel*>( mMapTools.mMoveLabel )->layerIsMoveable( vlayer, colX, colY ) );
5674+
( qobject_cast<QgsMapToolMoveLabel*>( mMapTools.mMoveLabel )->labelMoveable( vlayer, colX, colY )
5675+
|| qobject_cast<QgsMapToolMoveLabel*>( mMapTools.mMoveLabel )->diagramMoveable( vlayer, colX, colY ) )
5676+
);
56745677

56755678
enableRotate =
56765679
enableRotate ||

src/app/qgslabelpropertydialog.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "qgslabelpropertydialog.h"
1919
#include "qgsmaplayerregistry.h"
2020
#include "qgsmaprenderer.h"
21+
#include "qgsvectorlayer.h"
2122
#include <QColorDialog>
2223
#include <QFontDialog>
2324

src/app/qgsmaptoollabel.cpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,14 @@ bool QgsMapToolLabel::rotationPoint( QgsPoint& pos )
280280
pos = mCurrentLabelPos.cornerPoints.at( 0 );
281281
}
282282

283+
//alignment always center/center and rotation 0 for diagrams
284+
if ( mCurrentLabelPos.isDiagram )
285+
{
286+
pos.setX( pos.x() + mCurrentLabelPos.labelRect.width() / 2.0 );
287+
pos.setY( pos.y() + mCurrentLabelPos.labelRect.height() / 2.0 );
288+
return true;
289+
}
290+
283291
//adapt pos depending on data defined alignment
284292
QString haliString, valiString;
285293
currentAlignment( haliString, valiString );
@@ -356,7 +364,14 @@ bool QgsMapToolLabel::dataDefinedPosition( QgsVectorLayer* vlayer, int featureId
356364
return false;
357365
}
358366

359-
if ( !layerIsMoveable( vlayer, xCol, yCol ) )
367+
if ( mCurrentLabelPos.isDiagram )
368+
{
369+
if ( !diagramMoveable( vlayer, xCol, yCol ) )
370+
{
371+
return false;
372+
}
373+
}
374+
else if ( !labelMoveable( vlayer, xCol, yCol ) )
360375
{
361376
return false;
362377
}
@@ -374,7 +389,23 @@ bool QgsMapToolLabel::dataDefinedPosition( QgsVectorLayer* vlayer, int featureId
374389
return true;
375390
}
376391

377-
bool QgsMapToolLabel::layerIsMoveable( const QgsMapLayer* ml, int& xCol, int& yCol ) const
392+
bool QgsMapToolLabel:: diagramMoveable( const QgsMapLayer* ml, int& xCol, int& yCol ) const
393+
{
394+
const QgsVectorLayer* vlayer = dynamic_cast<const QgsVectorLayer*>( ml );
395+
if ( vlayer && vlayer->diagramRenderer() )
396+
{
397+
QgsDiagramLayerSettings dls = vlayer->diagramLayerSettings();
398+
if ( dls.xPosColumn >= 0 && dls.yPosColumn >= 0 )
399+
{
400+
xCol = dls.xPosColumn;
401+
yCol = dls.yPosColumn;
402+
return true;
403+
}
404+
}
405+
return false;
406+
}
407+
408+
bool QgsMapToolLabel::labelMoveable( const QgsMapLayer* ml, int& xCol, int& yCol ) const
378409
{
379410
const QgsVectorLayer* vlayer = dynamic_cast<const QgsVectorLayer*>( ml );
380411
if ( !vlayer || !vlayer->isEditable() )

src/app/qgsmaptoollabel.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,16 @@ class QgsMapToolLabel: public QgsMapTool
3131
QgsMapToolLabel( QgsMapCanvas* canvas );
3232
~QgsMapToolLabel();
3333

34-
/**Returns true if layer move can be applied to a layer
34+
/**Returns true if label move can be applied to a layer
3535
@param xCol out: index of the attribute for data defined x coordinate
3636
@param yCol out: index of the attribute for data defined y coordinate
3737
@return true if labels of layer can be moved*/
38-
bool layerIsMoveable( const QgsMapLayer* ml, int& xCol, int& yCol ) const;
38+
bool labelMoveable( const QgsMapLayer* ml, int& xCol, int& yCol ) const;
39+
/**Returns true if diagram move can be applied to a layer
40+
@param xCol out: index of the attribute for data defined x coordinate
41+
@param yCol out: index of the attribute for data defined y coordinate
42+
@return true if labels of layer can be moved*/
43+
bool diagramMoveable( const QgsMapLayer* ml, int& xCol, int& yCol ) const;
3944

4045
protected:
4146
QgsRubberBand* mLabelRubberBand;

src/app/qgsmaptoolmovelabel.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ void QgsMapToolMoveLabel::canvasPressEvent( QMouseEvent * e )
4040
}
4141

4242
QgsMapLayer* layer = QgsMapLayerRegistry::instance()->mapLayer( mCurrentLabelPos.layerID );
43-
if ( !layer )
43+
if ( !layer || !layer->isEditable() )
4444
{
4545
return;
4646
}
4747

4848
int xCol, yCol;
49-
if ( layerIsMoveable( layer, xCol, yCol ) )
49+
if ( labelMoveable( layer, xCol, yCol ) || diagramMoveable( layer, xCol, yCol ) )
5050
{
5151
mStartPointMapCoords = toMapCoordinates( e->pos() );
5252
mClickOffsetX = mStartPointMapCoords.x() - mCurrentLabelPos.labelRect.xMinimum();

0 commit comments

Comments
 (0)