Skip to content

Commit

Permalink
[FEATURE] Legend for markers with data-defined size
Browse files Browse the repository at this point in the history
(Merge pull request #4752 from wonder-sk/legend-data-defined-size)
  • Loading branch information
wonder-sk committed Jun 22, 2017
2 parents df07772 + 863b7d2 commit 7cd517c
Show file tree
Hide file tree
Showing 52 changed files with 2,176 additions and 272 deletions.
3 changes: 2 additions & 1 deletion doc/api_break.dox
Expand Up @@ -987,7 +987,7 @@ QgsDiagramRenderer {#qgis_api_break_3_0_QgsDiagramRenderer}
- renderDiagram() now takes an optional data defined overrides collection argument.
- readXml(), _readXml(), writeXml(), _writeXml() do not take QgsVectorLayer as an argument anymore.
- readXml(), _readXml(), writeXml(), _writeXml() require a new argument: a reference to QgsReadWriteContext

- sizeLegend(), setSizeLegend(), sizeLegendSymbol() and setSizeLegendSymbol() have been replaced by dataDefinedSizeLegend() and setDataDefinedSizeLegend() methods in QgsLinearlyInterpolatedDiagramRenderer.


QgsDiagramLayerSettings {#qgis_api_break_3_0_QgsDiagramLayerSettings}
Expand Down Expand Up @@ -2110,6 +2110,7 @@ QgsSingleSymbolRendererWidget {#qgis_api_break_3_0_QgsSingleSymbolRendere

- sizeScaleFieldChanged() and scaleMethodChanged() were removed. These settings are no longer exposed in the widget's GUI.
- The Mode enum was removed.
- changeSingleSymbol() and showSymbolLevels() slots were made private.

QgsSlider {#qgis_api_break_3_0_QgsSlider}
---------
Expand Down
1 change: 1 addition & 0 deletions python/core/core.sip
Expand Up @@ -36,6 +36,7 @@
%Include qgscoordinatetransform.sip
%Include qgscredentials.sip
%Include qgscrscache.sip
%Include qgsdatadefinedsizelegend.sip
%Include qgsdataitem.sip
%Include qgsdataitemprovider.sip
%Include qgsdataitemproviderregistry.sip
Expand Down
30 changes: 30 additions & 0 deletions python/core/layertree/qgslayertreemodellegendnode.sip
Expand Up @@ -151,6 +151,12 @@ Emitted on internal data change so the layer tree model can forward the signal t
Construct the node with pointer to its parent layer node
%End

QgsRenderContext *createTemporaryRenderContext() const /Factory/;
%Docstring
Returns a temporary context or null if legendMapViewData are not valid
:rtype: QgsRenderContext
%End

protected:
};

Expand Down Expand Up @@ -371,6 +377,30 @@ class QgsWmsLegendNode : QgsLayerTreeModelLegendNode

};


class QgsDataDefinedSizeLegendNode : QgsLayerTreeModelLegendNode
{
%Docstring
Produces legend node with a marker symbol
.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgslayertreemodellegendnode.h"
%End
public:
QgsDataDefinedSizeLegendNode( QgsLayerTreeLayer *nodeLayer, const QgsDataDefinedSizeLegend &settings, QObject *parent /TransferThis/ = 0 );
%Docstring
Construct the node using QgsDataDefinedSizeLegend as definition of the node's appearance
%End

virtual QVariant data( int role ) const;

virtual ItemMetrics draw( const QgsLegendSettings &settings, ItemContext *ctx );


};

/************************************************************************
* This file has been generated automatically from *
* *
Expand Down
177 changes: 177 additions & 0 deletions python/core/qgsdatadefinedsizelegend.sip
@@ -0,0 +1,177 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgsdatadefinedsizelegend.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/






class QgsDataDefinedSizeLegend
{
%Docstring
Object that keeps configuration of appearance of marker symbol's data-defined size in legend.
For example: the list of classes (size values), whether the classes should appear in separate
legend nodes or whether to collapse them into one legend node.

.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgsdatadefinedsizelegend.h"
%End
public:
QgsDataDefinedSizeLegend();

QgsDataDefinedSizeLegend( const QgsDataDefinedSizeLegend &other );
%Docstring
Copy constructor
%End

enum LegendType
{
LegendSeparated,
LegendCollapsed,
};

enum VerticalAlignment
{
AlignCenter,
AlignBottom,
};

struct SizeClass
{
SizeClass( double size, const QString &label );

double size; //!< Marker size in units used by the symbol (usually millimeters)
QString label; //!< Label to be shown with the particular symbol size
};

void setLegendType( LegendType type );
%Docstring
Sets how the legend should be rendered
%End
LegendType legendType() const;
%Docstring
Returns how the legend should be rendered
:rtype: LegendType
%End

void setSymbol( QgsMarkerSymbol *symbol /Transfer/ );
%Docstring
Sets marker symbol that will be used to draw markers in legend
%End
QgsMarkerSymbol *symbol() const;
%Docstring
Returns marker symbol that will be used to draw markers in legend
:rtype: QgsMarkerSymbol
%End

void setClasses( const QList<QgsDataDefinedSizeLegend::SizeClass> &classes );
%Docstring
Sets list of classes: each class is a pair of symbol size (in units used by the symbol) and label
%End
QList<QgsDataDefinedSizeLegend::SizeClass> classes() const;
%Docstring
Returns list of classes: each class is a pair of symbol size (in units used by the symbol) and label
:rtype: list of QgsDataDefinedSizeLegend.SizeClass
%End

void setTitle( const QString &title );
%Docstring
Sets title label for data-defined size legend
%End
QString title() const;
%Docstring
Returns title label for data-defined size legend
:rtype: str
%End

void setVerticalAlignment( VerticalAlignment vAlign );
%Docstring
Sets vertical alignment of symbols - only valid for collapsed legend
%End
VerticalAlignment verticalAlignment() const;
%Docstring
Returns vertical alignment of symbols - only valid for collapsed legend
:rtype: VerticalAlignment
%End

void setFont( const QFont &font );
%Docstring
Sets font used for rendering of labels - only valid for collapsed legend
%End
QFont font() const;
%Docstring
Returns font used for rendering of labels - only valid for collapsed legend
:rtype: QFont
%End

void setTextColor( const QColor &color );
%Docstring
Sets text color for rendering of labels - only valid for collapsed legend
%End
QColor textColor() const;
%Docstring
Returns text color for rendering of labels - only valid for collapsed legend
:rtype: QColor
%End

void setTextAlignment( Qt::AlignmentFlag flag );
%Docstring
Sets horizontal text alignment for rendering of labels - only valid for collapsed legend
%End
Qt::AlignmentFlag textAlignment() const;
%Docstring
Returns horizontal text alignment for rendering of labels - only valid for collapsed legend
:rtype: Qt.AlignmentFlag
%End


void updateFromSymbolAndProperty( const QgsMarkerSymbol *symbol, const QgsProperty &ddSize );
%Docstring
Updates the list of classes, source symbol and title label from given symbol and property
%End

QgsLegendSymbolList legendSymbolList() const;
%Docstring
Generates legend symbol items according to the configuration
:rtype: QgsLegendSymbolList
%End

void drawCollapsedLegend( QgsRenderContext &context, QSize *outputSize /Out/ = 0, int *labelXOffset /Out/ = 0 ) const;
%Docstring
Does nothing if legend is not configured as collapsed.
%End

QImage collapsedLegendImage( QgsRenderContext &context, const QColor &backgroundColor = Qt::transparent, double paddingMM = 1 ) const;
%Docstring
Returns output image that would be shown in the legend. Returns invalid image if legend is not configured as collapsed.
:rtype: QImage
%End

static QgsDataDefinedSizeLegend *readXml( const QDomElement &elem, const QgsReadWriteContext &context ) /Factory/;
%Docstring
Creates instance from given element and returns it (caller takes ownership). Returns null on error.
:rtype: QgsDataDefinedSizeLegend
%End

void writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const;
%Docstring
Writes configuration to the given XML element.
%End

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgsdatadefinedsizelegend.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
67 changes: 27 additions & 40 deletions python/core/qgsdiagramrenderer.sip
Expand Up @@ -450,6 +450,15 @@ class QgsDiagramRenderer

%TypeHeaderCode
#include "qgsdiagramrenderer.h"
%End

%ConvertToSubClassCode
if ( sipCpp->rendererName() == QStringLiteral( "SingleCategory" ) )
sipType = sipType_QgsSingleCategoryDiagramRenderer;
else if ( sipCpp->rendererName() == QStringLiteral( "LinearlyInterpolated" ) )
sipType = sipType_QgsLinearlyInterpolatedDiagramRenderer;
else
sipType = NULL;
%End
public:

Expand Down Expand Up @@ -545,44 +554,6 @@ Returns list with all diagram settings in the renderer
.. versionadded:: 2.16
.. seealso:: attributeLegend()
.. seealso:: setSizeLegend()
%End

bool sizeLegend() const;
%Docstring
Returns true if renderer will show legend items for diagram sizes.
.. versionadded:: 2.16
.. seealso:: setSizeLegend()
.. seealso:: attributeLegend()
.. seealso:: sizeLegendSymbol()
:rtype: bool
%End

void setSizeLegend( bool enabled );
%Docstring
Sets whether the renderer will show legend items for diagram sizes.
\param enabled set to true to show diagram size legend
.. versionadded:: 2.16
.. seealso:: sizeLegend()
.. seealso:: setAttributeLegend()
.. seealso:: setSizeLegendSymbol()
%End

QgsMarkerSymbol *sizeLegendSymbol() const;
%Docstring
Returns the marker symbol used for rendering the diagram size legend.
.. versionadded:: 2.16
.. seealso:: setSizeLegendSymbol()
.. seealso:: sizeLegend()
:rtype: QgsMarkerSymbol
%End

void setSizeLegendSymbol( QgsMarkerSymbol *symbol /Transfer/ );
%Docstring
Sets the marker symbol used for rendering the diagram size legend.
\param symbol marker symbol, ownership is transferred to the renderer.
.. versionadded:: 2.16
.. seealso:: sizeLegendSymbol()
.. seealso:: setSizeLegend()
%End

protected:
Expand Down Expand Up @@ -628,8 +599,6 @@ Returns the paint device dpi (or -1 in case of error
%End




};

class QgsSingleCategoryDiagramRenderer : QgsDiagramRenderer
Expand Down Expand Up @@ -760,13 +729,31 @@ Returns list with all diagram settings in the renderer
virtual QList< QgsLayerTreeModelLegendNode * > legendItems( QgsLayerTreeLayer *nodeLayer ) const /Factory/;


void setDataDefinedSizeLegend( QgsDataDefinedSizeLegend *settings /Transfer/ );
%Docstring
Configures appearance of legend. Takes ownership of the passed settings objects.
.. versionadded:: 3.0
%End

QgsDataDefinedSizeLegend *dataDefinedSizeLegend() const;
%Docstring
Returns configuration of appearance of legend. Will return null if no configuration has been set.
.. versionadded:: 3.0
:rtype: QgsDataDefinedSizeLegend
%End

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


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


QgsLinearlyInterpolatedDiagramRenderer( const QgsLinearlyInterpolatedDiagramRenderer &other );
%Docstring
Copy constructor
%End

};

/************************************************************************
Expand Down
22 changes: 22 additions & 0 deletions python/core/symbology-ng/qgscategorizedsymbolrenderer.sip
Expand Up @@ -78,6 +78,7 @@ class QgsCategorizedSymbolRenderer : QgsFeatureRenderer
public:

QgsCategorizedSymbolRenderer( const QString &attrName = QString(), const QgsCategoryList &categories = QgsCategoryList() );
~QgsCategorizedSymbolRenderer();

virtual QgsSymbol *symbolForFeature( QgsFeature &feature, QgsRenderContext &context );
virtual QgsSymbol *originalSymbolForFeature( QgsFeature &feature, QgsRenderContext &context );
Expand Down Expand Up @@ -220,10 +221,31 @@ create renderer from XML element
:rtype: QgsCategorizedSymbolRenderer
%End

void setDataDefinedSizeLegend( QgsDataDefinedSizeLegend *settings /Transfer/ );
%Docstring
Configures appearance of legend when renderer is configured to use data-defined size for marker symbols.
This allows to configure for what values (symbol sizes) should be shown in the legend, whether to display
different symbol sizes collapsed in one legend node or separated across multiple legend nodes etc.

When renderer does not use data-defined size or does not use marker symbols, these settings will be ignored.
Takes ownership of the passed settings objects. Null pointer is a valid input that disables data-defined
size legend.
.. versionadded:: 3.0
%End

QgsDataDefinedSizeLegend *dataDefinedSizeLegend() const;
%Docstring
Returns configuration of appearance of legend when using data-defined size for marker symbols.
Will return null if the functionality is disabled.
.. versionadded:: 3.0
:rtype: QgsDataDefinedSizeLegend
%End

protected:




void rebuildHash();
%Docstring
hashtable for faster access to symbols
Expand Down

0 comments on commit 7cd517c

Please sign in to comment.