Skip to content
Permalink
Browse files

Minor refactor of QgsDiagramLayerSettings

- add public getters and setters for variables, will make variables
private in QGIS 3.0
- avoid some memory leaks and potential crashes
- move calculation of referenced fields to diagram classes, out
of QgsVectorLayerDiagramProvider
  • Loading branch information
nyalldawson committed Mar 14, 2016
1 parent e1ae69a commit e72a82e04f5c360f5d416c4be76c0a8e5ae3753f
@@ -1,8 +1,17 @@
struct QgsDiagramLayerSettings
/** \ingroup core
* \class QgsDiagramLayerSettings
* \brief Stores the settings for rendering of all diagrams for a layer.
*
* QgsDiagramSettings stores the settings related to rendering the individual diagrams themselves, while
* QgsDiagramLayerSettings stores settings which control how ALL diagrams within a layer are rendered.
*/

class QgsDiagramLayerSettings
{
%TypeHeaderCode
#include <qgsdiagramrendererv2.h>
%End

public:
//avoid inclusion of QgsPalLabeling
enum Placement
@@ -15,6 +24,7 @@ struct QgsDiagramLayerSettings
Free // Polygon
};

//! Line placement flags for controlling line based placements
enum LinePlacementFlags
{
OnLine,
@@ -24,36 +34,226 @@ struct QgsDiagramLayerSettings
};

QgsDiagramLayerSettings();

//! Copy constructor
QgsDiagramLayerSettings( const QgsDiagramLayerSettings& rh );

~QgsDiagramLayerSettings();

//pal placement properties
/** Returns the diagram placement.
* @see setPlacement()
* @note added in QGIS 2.16
*/
//TODO QGIS 3.0 - rename getter to placement()
Placement getPlacement() const;

/** Sets the diagram placement.
* @param value placement value
* @see getPlacement()
* @note added in QGIS 2.16
*/
void setPlacement( Placement value );

//! Diagram placement
//TODO QGIS 3.0 - make private, rename to mPlacement
Placement placement;

/** Returns the diagram placement flags. These are only used if the diagram placement
* is set to a line type.
* @see setLinePlacementFlags()
* @note added in QGIS 2.16
*/
unsigned int linePlacementFlags() const;

/** Sets the the diagram placement flags. These are only used if the diagram placement
* is set to a line type.
* @param flags placement value
* @see getPlacement()
* @note added in QGIS 2.16
*/
void setLinePlacementFlags( unsigned int flags );

//! Diagram placement flags
// TODO QGIS 3.0 - make private, rename to mPlacementFlags, use QFlags
unsigned int placementFlags;
int priority; // 0 = low, 10 = high

/** Returns the diagram priority.
* @returns diagram priority, where 0 = low and 10 = high
* @note placement priority is shared with labeling, so diagrams with a high priority may displace labels
* and vice-versa
* @see setPriority()
* @note added in QGIS 2.16
*/
//TODO QGIS 3.0 - rename getter to priority()
int getPriority() const;

/** Sets the diagram priority.
* @param value priority, where 0 = low and 10 = high
* @see getPriority()
* @note added in QGIS 2.16
*/
void setPriority( int value );

//! Placement priority, where 0 = low and 10 = high
//! @note placement priority is shared with labeling, so diagrams with a high priority may displace labels
//! and vice-versa
// TODO QGIS 3.0 - make private, rename to mPriority
int priority;

/** Returns the diagram z-index. Diagrams (or labels) with a higher z-index are drawn over diagrams
* with a lower z-index.
* @note z-index ordering is shared with labeling, so diagrams with a high z-index may be drawn over labels
* with a low z-index and vice-versa
* @see setZIndex()
* @note added in QGIS 2.16
*/
//TODO QGIS 3.0 - rename getter to zIndex()
double getZIndex() const;

/** Sets the diagram z-index. Diagrams (or labels) with a higher z-index are drawn over diagrams
* with a lower z-index.
* @param index diagram z-index
* @see getZIndex()
* @note added in QGIS 2.16
*/
void setZIndex( double index );

//! Z-index of diagrams, where diagrams with a higher z-index are drawn on top of diagrams with a lower z-index
// TODO QGIS 3.0 - rename to mZIndex, make private
double zIndex;

bool obstacle; // whether it's an obstacle
double dist; // distance from the feature (in mm)
QgsDiagramRendererV2* renderer; // if any renderer is assigned, it is owned by this class
/** Returns whether the feature associated with a diagram acts as an obstacle for other labels or diagrams.
* @see setIsObstacle()
* @note added in QGIS 2.16
*/
bool isObstacle() const;

/** Sets whether the feature associated with a diagram acts as an obstacle for other labels or diagrams.
* @param isObstacle set to true for feature to act as obstacle
* @see isObstacle()
* @note added in QGIS 2.16
*/
void setIsObstacle( bool isObstacle );

//! Whether associated feature acts as an obstacle for other labels or diagrams
// TODO QGIS 3.0 - rename to mObstacle, make private
bool obstacle;

/** Returns the distance between the diagram and the feature (in mm).
* @see setDistance()
* @note added in QGIS 2.16
*/
double distance() const;

/** Sets the distance between the diagram and the feature.
* @param distance distance in mm
* @see distance()
* @note added in QGIS 2.16
*/
void setDistance( double distance );

//! Distance between diagram and the feature (in mm)
// TODO QGIS 3.0 - make private, rename to mDistance
double dist;

/** Returns the diagram renderer associated with the layer.
* @see setRenderer()
* @note added in QGIS 2.16
*/
// TODO QGIS 3.0 - rename to renderer()
QgsDiagramRendererV2* getRenderer();

/** Returns the diagram renderer associated with the layer.
* @see setRenderer()
* @note added in QGIS 2.16
*/
// TODO QGIS 3.0 - rename to renderer()
//const QgsDiagramRendererV2* getRenderer() const;

/** Sets the diagram renderer associated with the layer.
* @param diagramRenderer diagram renderer. Ownership is transferred to the object.
* @see getRenderer()
* @note added in QGIS 2.16
*/
void setRenderer( QgsDiagramRendererV2* diagramRenderer /Transfer/ );

//! Associated diagram renderer. Owned by this object.
// TODO QGIS 3.0 - make private, rename to mRenderer
QgsDiagramRendererV2* renderer;

/** Returns the coordinate transform associated with the layer.
* @see setCoordinateTransform()
* @note added in QGIS 2.16
*/
QgsCoordinateTransform* coordinateTransform();

/** Returns the coordinate transform associated with the layer.
* @see setCoordinateTransform()
* @note added in QGIS 2.16
*/
//const QgsCoordinateTransform* coordinateTransform() const;

/** Sets the coordinate transform associated with the layer.
* @param transform coordinate transform. Ownership is transferred to the object.
* @see coordinateTransform()
* @note added in QGIS 2.16
*/
void setCoordinateTransform( QgsCoordinateTransform* transform /Transfer/ );

//assigned when layer gets prepared
const QgsCoordinateTransform* ct;
//! Associated coordinate transform. Owned by this object.
// TODO QGIS 3.0 - make private, rename to mCt
QgsCoordinateTransform* ct;

//! @deprecated will be removed in QGIS 3.0
const QgsMapToPixel* xform;

//! @deprecated will be removed in QGIS 3.0
QgsFields fields;

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)
//! Attribute index for x coordinate (or -1 if position not data defined)
int xPosColumn;

//! Attribute index for y coordinate (or -1 if position not data defined)
int yPosColumn;

/** Returns whether the layer should show all diagrams, including overlapping diagrams
* @see setShowAllDiagrams()
* @note added in QGIS 2.16
*/
bool showAllDiagrams() const;

/** Sets whether the layer should show all diagrams, including overlapping diagrams
* @param showAllDiagrams set to true to show all diagrams
* @see showAllDiagrams()
* @note added in QGIS 2.16
*/
void setShowAllDiagrams( bool showAllDiagrams );

//! Whether to show all diagrams, including overlapping diagrams
// TODO QGIS 3.0 - make private, rename to mShowAll
bool showAll;

void readXML( const QDomElement& elem, const QgsVectorLayer* layer );
void writeXML( QDomElement& layerElem, QDomDocument& doc, const QgsVectorLayer* layer ) const;

/** Returns the set of any fields referenced by the layer's diagrams.
* @param context expression context the diagrams will be drawn using
* @param fields layer fields
* @note added in QGIS 2.16
*/
//TODO QGIS 3.0 - remove need for fields parameter
QSet< QString > referencedFields( const QgsExpressionContext& context = QgsExpressionContext(), const QgsFields& fields = QgsFields() ) const;

};

//diagram settings for rendering
/** \ingroup core
* \class QgsDiagramSettings
* \brief Stores the settings for rendering a single diagram.
*
* QgsDiagramSettings stores the settings related to rendering the individual diagrams themselves, while
* QgsDiagramLayerSettings stores settings which control how ALL diagrams within a layer are rendered.
*/

class QgsDiagramSettings
{
%TypeHeaderCode
@@ -136,7 +336,12 @@ class QgsDiagramInterpolationSettings
bool classificationAttributeIsExpression;
};

/** Returns diagram settings for a feature*/

/** \ingroup core
* \class QgsDiagramRendererV2
* \brief Evaluates and returns the diagram settings relating to a diagram for a specific feature.
*/

class QgsDiagramRendererV2
{
%TypeHeaderCode
@@ -152,13 +357,21 @@ class QgsDiagramRendererV2
virtual QgsDiagramRendererV2* clone() const = 0 /Factory/;

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

virtual QString rendererName() const = 0;

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

/** Returns the set of any fields required for diagram rendering
* @param context expression context the diagrams will be drawn using
* @param fields layer fields
* @note added in QGIS 2.16
*/
//TODO QGIS 3.0 - remove need for fields parameter
virtual QSet< QString > referencedFields( const QgsExpressionContext& context = QgsExpressionContext(), const QgsFields& fields = QgsFields() ) const;

void renderDiagram( const QgsFeature& feature, QgsRenderContext& c, QPointF pos );

void setDiagram( QgsDiagram* d /Transfer/ );
@@ -183,10 +396,10 @@ class QgsDiagramRendererV2
* @param c render context
* @param s out: diagram settings for the feature
*/
virtual bool diagramSettings( const QgsFeature &feature, const QgsRenderContext& c, QgsDiagramSettings& s ) = 0;
virtual bool diagramSettings( const QgsFeature &feature, const QgsRenderContext& c, QgsDiagramSettings& s ) const = 0;

/** Returns size of the diagram (in painter units) or an invalid size in case of error*/
virtual QSizeF diagramSize( const QgsFeature& features, const QgsRenderContext& c ) = 0;
virtual QSizeF diagramSize( const QgsFeature& features, const QgsRenderContext& c ) const = 0;

/** Converts size from mm to map units*/
void convertSizeToMapUnits( QSizeF& size, const QgsRenderContext& context ) const;
@@ -226,9 +439,9 @@ class QgsSingleCategoryDiagramRenderer : QgsDiagramRendererV2
virtual QList< QgsLayerTreeModelLegendNode* > legendItems( QgsLayerTreeLayer* nodeLayer ) const /Factory/;

protected:
bool diagramSettings( const QgsFeature &feature, const QgsRenderContext& c, QgsDiagramSettings& s );
bool diagramSettings( const QgsFeature &feature, const QgsRenderContext& c, QgsDiagramSettings& s ) const;

QSizeF diagramSize( const QgsFeature&, const QgsRenderContext& c );
QSizeF diagramSize( const QgsFeature&, const QgsRenderContext& c ) const;
};

class QgsLinearlyInterpolatedDiagramRenderer : QgsDiagramRendererV2
@@ -250,6 +463,8 @@ class QgsLinearlyInterpolatedDiagramRenderer : QgsDiagramRendererV2

QList<QString> diagramAttributes() const;

virtual QSet< QString > referencedFields( const QgsExpressionContext& context = QgsExpressionContext(), const QgsFields& fields = QgsFields() ) const;

QString rendererName() const;

void setLowerValue( double val );
@@ -279,7 +494,7 @@ class QgsLinearlyInterpolatedDiagramRenderer : QgsDiagramRendererV2
virtual QList< QgsLayerTreeModelLegendNode* > legendItems( QgsLayerTreeLayer* nodeLayer ) const /Factory/;

protected:
bool diagramSettings( const QgsFeature &feature, const QgsRenderContext& c, QgsDiagramSettings& s );
bool diagramSettings( const QgsFeature &feature, const QgsRenderContext& c, QgsDiagramSettings& s ) const;

QSizeF diagramSize( const QgsFeature&, const QgsRenderContext& c );
QSizeF diagramSize( const QgsFeature&, const QgsRenderContext& c ) const;
};

0 comments on commit e72a82e

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