Skip to content

Commit

Permalink
[core] Introduce QgsStackedDiagramRenderer for rendering stacked diag…
Browse files Browse the repository at this point in the history
…rams. This allows subdiagrams to define their own sizes and legends; add tests for a stacked diagram composed of mixed renderers (single category and linearly interpolated categories), as well as nested stacked diagrams.
  • Loading branch information
gacarrillor committed Sep 3, 2024
1 parent 83d7786 commit 55b88df
Show file tree
Hide file tree
Showing 19 changed files with 1,061 additions and 768 deletions.
4 changes: 0 additions & 4 deletions python/PyQt6/core/auto_additions/qgsstackeddiagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,3 @@
QgsStackedDiagram.__group__ = ['diagram']
except NameError:
pass
try:
QgsStackedDiagram.DiagramData.__group__ = ['diagram']
except NameError:
pass
44 changes: 0 additions & 44 deletions python/PyQt6/core/auto_generated/diagram/qgsstackeddiagram.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -25,55 +25,11 @@ A diagram composed of several subdiagrams, located side by side.
%End
public:

struct DiagramData
{
QgsDiagram *diagram;
QgsDiagramSettings *settings;
};

QgsStackedDiagram();

virtual QgsStackedDiagram *clone() const /Factory/;


void addSubDiagram( QgsDiagram *diagram, QgsDiagramSettings *s );
%Docstring
Adds a subdiagram to the stacked diagram object along with its corresponding settings.

:param diagram: subdiagram to be added to the stacked diagram
:param s: subdiagram settings
Subdiagrams added first will appear more to the left (if stacked diagram is horizontal),
or more to the top (if stacked diagram is vertical).
%End

int subDiagramCount() const;
%Docstring
Returns the number of subdiagrams that this stacked diagram is composed of.
%End

QString subDiagramType( int index ) const;
%Docstring
Returns the type of the subdiagram located at a given ``index``.
%End

QList< QgsDiagram * > subDiagrams( const QgsDiagramSettings &s ) const;
%Docstring
Returns an ordered list with the subdiagrams of the stacked diagram object.
If the stacked diagram orientation is vertical, the list is returned backwards.

:param s: stacked diagram settings
%End

QgsDiagramSettings *subDiagramSettings( const QgsDiagram *diagram ) const;
%Docstring
Returns the settings associated to the ``diagram``.
%End

QgsDiagramSettings *subDiagramSettings( int index ) const;
%Docstring
Returns the diagram settings for the diagram located at a given ``index``.
%End

void subDiagramPosition( QPointF &newPos, const QgsRenderContext &c, const QgsDiagramSettings &s, const QgsDiagramSettings &subSettings );
%Docstring
Calculates the position for the next subdiagram, updating the ``newPos`` object.
Expand Down
132 changes: 110 additions & 22 deletions python/PyQt6/core/auto_generated/qgsdiagramrenderer.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,8 @@ Evaluates and returns the diagram settings relating to a diagram for a specific
sipType = sipType_QgsSingleCategoryDiagramRenderer;
else if ( sipCpp->rendererName() == QLatin1String( "LinearlyInterpolated" ) )
sipType = sipType_QgsLinearlyInterpolatedDiagramRenderer;
else if ( sipCpp->rendererName() == QLatin1String( "Stacked" ) )
sipType = sipType_QgsStackedDiagramRenderer;
else
sipType = NULL;
%End
Expand Down Expand Up @@ -712,7 +714,7 @@ Returns the set of any fields required for diagram rendering
:param context: expression context the diagrams will be drawn using
%End

void renderDiagram( const QgsFeature &feature, QgsRenderContext &c, QPointF pos, const QgsPropertyCollection &properties = QgsPropertyCollection() ) const;
virtual void renderDiagram( const QgsFeature &feature, QgsRenderContext &c, QPointF pos, const QgsPropertyCollection &properties = QgsPropertyCollection() ) const;
%Docstring
Renders the diagram for a specified feature at a specific position in the passed render context.
%End
Expand Down Expand Up @@ -769,23 +771,21 @@ Sets whether the renderer will show legend items for diagram attributes.
protected:
QgsDiagramRenderer( const QgsDiagramRenderer &other );

virtual bool diagramSettings( const QgsFeature &feature, const QgsRenderContext &c, QgsDiagramSettings &s, QgsDiagram *subDiagram = 0 ) const = 0;
virtual bool diagramSettings( const QgsFeature &feature, const QgsRenderContext &c, QgsDiagramSettings &s ) const = 0;
%Docstring
Returns diagram settings for a feature (or ``False`` if the diagram for the feature is not to be rendered). Used internally within :py:func:`~QgsDiagramRenderer.renderDiagram`

:param feature: the feature
:param c: render context
:param s: out: diagram settings for the feature
:param subDiagram: subDiagram object for stacked diagram case
%End

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

:param feature: the feature
:param c: render context
:param subDiagram: subDiagram object for stacked diagram case
%End

void convertSizeToMapUnits( QSizeF &size, const QgsRenderContext &context ) const;
Expand All @@ -804,13 +804,6 @@ Returns the paint device dpi (or -1 in case of error
Reads internal QgsDiagramRenderer state from a DOM element.

.. seealso:: _writeXml
%End

void _readXmlSubdiagrams( const QDomElement &elem, const QgsReadWriteContext &context );
%Docstring
Reads Stacked Diagram's subdiagram state from a DOM element.

.. seealso:: _writeXmlSubDiagrams
%End

void _writeXml( QDomElement &rendererElem, QDomDocument &doc, const QgsReadWriteContext &context ) const;
Expand All @@ -820,12 +813,6 @@ Writes internal QgsDiagramRenderer diagram state to a DOM element.
.. seealso:: _readXml
%End

void _writeXmlSubDiagrams( QDomElement &rendererElem, QDomDocument &doc, const QgsReadWriteContext &context ) const;
%Docstring
Writes Stacked Diagram's subdiagram state to a DOM element.

.. seealso:: _readXmlSubdiagrams
%End


};
Expand Down Expand Up @@ -864,10 +851,10 @@ Renders the diagrams for all features with the same settings


protected:
virtual bool diagramSettings( const QgsFeature &feature, const QgsRenderContext &c, QgsDiagramSettings &s, QgsDiagram *subDiagram = 0 ) const;
virtual bool diagramSettings( const QgsFeature &feature, const QgsRenderContext &c, QgsDiagramSettings &s ) const;


virtual QSizeF diagramSize( const QgsFeature &, const QgsRenderContext &c, QgsDiagram *subDiagram = 0 ) const;
virtual QSizeF diagramSize( const QgsFeature &, const QgsRenderContext &c ) const;


};
Expand Down Expand Up @@ -957,14 +944,115 @@ Returns configuration of appearance of legend. Will return ``None`` if no config
%End

protected:
virtual bool diagramSettings( const QgsFeature &feature, const QgsRenderContext &c, QgsDiagramSettings &s, QgsDiagram *subDiagram = 0 ) const;
virtual bool diagramSettings( const QgsFeature &feature, const QgsRenderContext &c, QgsDiagramSettings &s ) const;


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


};

class QgsStackedDiagramRenderer : QgsDiagramRenderer
{
%Docstring(signature="appended")
Renders diagrams using mixed diagram render types. The size of
the rendered diagram is given by a combination of subrenderers.

.. versionadded:: 3.40
%End

%TypeHeaderCode
#include "qgsdiagramrenderer.h"
%End
public:
QgsStackedDiagramRenderer();

virtual QgsStackedDiagramRenderer *clone() const /Factory/;


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

virtual void renderDiagram( const QgsFeature &feature, QgsRenderContext &c, QPointF pos, const QgsPropertyCollection &properties = QgsPropertyCollection() ) const;
%Docstring
Renders the diagram for a specified feature at a specific position in the
passed render context, taking all renderers and their own diagrams into account.
Diagram rendering is delegated to renderer's diagram.
%End

virtual QList<QgsDiagramSettings> diagramSettings() const;

%Docstring
Returns list with all diagram settings in the renderer
%End

void setDiagramSettings( const QgsDiagramSettings &s );

virtual QList<QString> diagramAttributes() const;



virtual QString rendererName() const;

virtual void readXml( const QDomElement &elem, const QgsReadWriteContext &context );

virtual void writeXml( QDomElement &layerElem, QDomDocument &doc, const QgsReadWriteContext &context ) const;


void _readXmlSubRenderers( const QDomElement &elem, const QgsReadWriteContext &context );
%Docstring
Reads stacked renderers state from a DOM element.

.. seealso:: _writeXmlSubRenderers
%End

virtual QSizeF diagramSize( const QgsFeature &, const QgsRenderContext &c, QgsDiagram *subDiagram = 0 ) const;
void _writeXmlSubRenderers( QDomElement &rendererElem, QDomDocument &doc, const QgsReadWriteContext &context ) const;
%Docstring
Writes stacked renderers state to a DOM element.

.. seealso:: _readXmlSubRenderers
%End

virtual QList< QgsLayerTreeModelLegendNode * > legendItems( QgsLayerTreeLayer *nodeLayer ) const /Factory/;


QList< QgsDiagramRenderer * > renderers() const;
%Docstring
Returns an ordered list with the renderers of the stacked renderer object.
If the stacked diagram orientation is vertical, the list is returned backwards.
%End

void addRenderer( QgsDiagramRenderer *renderer );
%Docstring
Adds a renderer to the stacked renderer object.

:param renderer: diagram renderer to be added to the stacked renderer
Renderers added first will render their diagrams first, i.e., more to
the left (horizontal mode) or more to the top (vertical mode).
%End

const QgsDiagramRenderer *renderer( const int index ) const;
%Docstring
Returns the renderer at the given ``index``.
@param index index of the disired renderer in the stacked renderer
%End

int rendererCount() const;
%Docstring
Returns the number of renderers that this stacked renderer is composed of.
%End

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

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


};


/************************************************************************
* This file has been generated automatically from *
* *
Expand Down
4 changes: 0 additions & 4 deletions python/core/auto_additions/qgsstackeddiagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,3 @@
QgsStackedDiagram.__group__ = ['diagram']
except NameError:
pass
try:
QgsStackedDiagram.DiagramData.__group__ = ['diagram']
except NameError:
pass
44 changes: 0 additions & 44 deletions python/core/auto_generated/diagram/qgsstackeddiagram.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -25,55 +25,11 @@ A diagram composed of several subdiagrams, located side by side.
%End
public:

struct DiagramData
{
QgsDiagram *diagram;
QgsDiagramSettings *settings;
};

QgsStackedDiagram();

virtual QgsStackedDiagram *clone() const /Factory/;


void addSubDiagram( QgsDiagram *diagram, QgsDiagramSettings *s );
%Docstring
Adds a subdiagram to the stacked diagram object along with its corresponding settings.

:param diagram: subdiagram to be added to the stacked diagram
:param s: subdiagram settings
Subdiagrams added first will appear more to the left (if stacked diagram is horizontal),
or more to the top (if stacked diagram is vertical).
%End

int subDiagramCount() const;
%Docstring
Returns the number of subdiagrams that this stacked diagram is composed of.
%End

QString subDiagramType( int index ) const;
%Docstring
Returns the type of the subdiagram located at a given ``index``.
%End

QList< QgsDiagram * > subDiagrams( const QgsDiagramSettings &s ) const;
%Docstring
Returns an ordered list with the subdiagrams of the stacked diagram object.
If the stacked diagram orientation is vertical, the list is returned backwards.

:param s: stacked diagram settings
%End

QgsDiagramSettings *subDiagramSettings( const QgsDiagram *diagram ) const;
%Docstring
Returns the settings associated to the ``diagram``.
%End

QgsDiagramSettings *subDiagramSettings( int index ) const;
%Docstring
Returns the diagram settings for the diagram located at a given ``index``.
%End

void subDiagramPosition( QPointF &newPos, const QgsRenderContext &c, const QgsDiagramSettings &s, const QgsDiagramSettings &subSettings );
%Docstring
Calculates the position for the next subdiagram, updating the ``newPos`` object.
Expand Down
Loading

0 comments on commit 55b88df

Please sign in to comment.