Skip to content

Commit

Permalink
[FEATURE] Support legend patch shapes in style manager databases
Browse files Browse the repository at this point in the history
And clean up a lot of duplicate code which makes adding new types
to QgsStyle painful
  • Loading branch information
nyalldawson committed Apr 22, 2020
1 parent 99d50bb commit 8a0e007
Show file tree
Hide file tree
Showing 20 changed files with 2,083 additions and 631 deletions.
2 changes: 2 additions & 0 deletions python/core/auto_generated/layout/qgslayoutitemlegend.sip.in
Expand Up @@ -539,6 +539,8 @@ Returns the legend's renderer settings object.

virtual ExportLayerBehavior exportLayerBehavior() const;

virtual bool accept( QgsStyleEntityVisitorInterface *visitor ) const;


public slots:

Expand Down
138 changes: 126 additions & 12 deletions python/core/auto_generated/symbology/qgsstyle.sip.in
Expand Up @@ -96,6 +96,7 @@ Constructor for QgsStyle.
SmartgroupEntity,
TextFormatEntity,
LabelSettingsEntity,
LegendPatchShapeEntity,
};

bool addEntity( const QString &name, const QgsStyleEntityInterface *entity, bool update = false );
Expand Down Expand Up @@ -123,7 +124,7 @@ Adds a symbol to style and takes symbol's ownership

:param name: is the name of the symbol being added or updated
:param symbol: is the Vector symbol
:param update: set to ``True`` when the style DB has to be updated, by default it is ``False``
:param update: set to ``True`` when the style database has to be updated, by default it is ``False``

:return: success status of the operation
%End
Expand All @@ -138,7 +139,7 @@ Adds a color ramp to the style. Calling this method takes the ramp's ownership.

:param name: is the name of the color ramp being added or updated
:param colorRamp: is the color ramp. Ownership is transferred.
:param update: set to ``True`` when the style DB has to be updated, by default it is ``False``
:param update: set to ``True`` when the style database has to be updated, by default it is ``False``

:return: success status of the operation
%End
Expand All @@ -147,7 +148,7 @@ Adds a color ramp to the style. Calling this method takes the ramp's ownership.
%Docstring
Adds a text ``format`` with the specified ``name`` to the style.

If ``update`` is set to ``True``, the style DB will be automatically updated with the new text format.
If ``update`` is set to ``True``, the style database will be automatically updated with the new text format.

Returns ``True`` if the operation was successful.

Expand All @@ -162,7 +163,7 @@ Returns ``True`` if the operation was successful.
%Docstring
Adds label ``settings`` with the specified ``name`` to the style.

If ``update`` is set to ``True``, the style DB will be automatically updated with the new text format.
If ``update`` is set to ``True``, the style database will be automatically updated with the new text format.

Returns ``True`` if the operation was successful.

Expand All @@ -171,6 +172,21 @@ Returns ``True`` if the operation was successful.
Adding label settings with the name of existing ones replaces them.

.. versionadded:: 3.10
%End

bool addLegendPatchShape( const QString &name, const QgsLegendPatchShape &shape, bool update = false );
%Docstring
Adds a legend patch ``shape`` with the specified ``name`` to the style.

If ``update`` is set to ``True``, the style database will be automatically updated with the new legend patch shape.

Returns ``True`` if the operation was successful.

.. note::

Adding legend patch shapes with the name of existing ones replaces them.

.. versionadded:: 3.14
%End

int addTag( const QString &tagName );
Expand All @@ -179,7 +195,7 @@ Adds a new tag and returns the tag's id

:param tagName: the name of the new tag to be created

:return: returns an int, which is the DB id of the new tag created, 0 if the tag couldn't be created
:return: returns an int, which is the database id of the new tag created, 0 if the tag couldn't be created
%End


Expand Down Expand Up @@ -273,6 +289,29 @@ Returns 0 if the text format was not found.
Returns the label settings with the specified ``name``.

.. versionadded:: 3.10
%End

QgsLegendPatchShape legendPatchShape( const QString &name ) const;
%Docstring
Returns the legend patch shape with the specified ``name``.

.. versionadded:: 3.14
%End

int legendPatchShapesCount() const;
%Docstring
Returns count of legend patch shapes in the style.

.. versionadded:: 3.14
%End

QgsSymbol::SymbolType legendPatchShapeSymbolType( const QString &name ) const;
%Docstring
Returns the symbol type corresponding to the legend patch shape
with the specified ``name``, or QgsSymbol.Hybrid
if a matching legend patch shape is not present.

.. versionadded:: 3.14
%End

QgsWkbTypes::GeometryType labelSettingsLayerType( const QString &name ) const;
Expand Down Expand Up @@ -396,13 +435,20 @@ Returns a list of names of symbols
Returns the id in the style database for the given symbol name
returns 0 if not found
%End

int entityId( StyleEntity type, const QString &name );
%Docstring
Returns the id in the style database for the given ``name`` of the specified entity ``type``.
Returns 0 if not found.
%End

int tagId( const QString &tag );
%Docstring
Returns the DB id for the given tag name
Returns the database id for the given tag name
%End
int smartgroupId( const QString &smartgroup );
%Docstring
Returns the DB id for the given smartgroup name
Returns the database id for the given smartgroup name
%End

QStringList allNames( StyleEntity type ) const;
Expand Down Expand Up @@ -456,21 +502,32 @@ Removes the specified symbol from favorites
Renames the given entity with the specified id

:param type: is any of the style entities. Refer enum StyleEntity.
:param id: is the DB id of the entity which is to be renamed
:param id: is the database id of the entity which is to be renamed
:param newName: is the new name of the entity
%End

bool remove( StyleEntity type, int id );
%Docstring
Removes the specified entity from the db
Removes the specified entity from the database.

:param type: is any of the style entities. Refer enum StyleEntity.
:param id: is the DB id of the entity to be removed
:param id: is the database id of the entity to be removed

.. seealso:: :py:func:`removeEntityByName`
%End

bool removeEntityByName( StyleEntity type, const QString &name );
%Docstring
Removes the entry of the specified ``type`` with matching ``name`` from the database.

.. seealso:: :py:func:`remove`

.. versionadded:: 3.14
%End

bool saveSymbol( const QString &name, QgsSymbol *symbol, bool favorite, const QStringList &tags );
%Docstring
Adds the symbol to the DB with the tags
Adds the symbol to the database with tags.

:param name: is the name of the symbol as QString
:param symbol: is the pointer to the new QgsSymbol being saved
Expand All @@ -482,7 +539,7 @@ Adds the symbol to the DB with the tags

bool saveColorRamp( const QString &name, QgsColorRamp *ramp, bool favorite, const QStringList &tags );
%Docstring
Adds the colorramp to the DB
Adds the colorramp to the database.

:param name: is the name of the colorramp as QString
:param ramp: is the pointer to the new QgsColorRamp being saved
Expand Down Expand Up @@ -552,6 +609,34 @@ Removes label settings from the style.
Changes a label setting's name.

.. versionadded:: 3.10
%End

bool saveLegendPatchShape( const QString &name, const QgsLegendPatchShape &shape, bool favorite, const QStringList &tags );
%Docstring
Adds a legend patch ``shape`` to the database.

:param name: is the name of the legend patch shape
:param shape: legend patch shape to save
:param favorite: is a boolean value to specify whether the legend patch shape should be added to favorites
:param tags: is a list of tags that are associated with the legend patch shape

:return: returns the success state of the save operation

.. versionadded:: 3.14
%End

bool renameLegendPatchShape( const QString &oldName, const QString &newName );
%Docstring
Changes a legend patch shape's name.

.. versionadded:: 3.14
%End

QStringList legendPatchShapeNames() const;
%Docstring
Returns a list of names of legend patch shapes in the style.

.. versionadded:: 3.14
%End

QgsLegendPatchShape defaultPatch( QgsSymbol::SymbolType type, QSizeF size ) const;
Expand Down Expand Up @@ -1120,6 +1205,35 @@ Returns the entity's label settings.

};

class QgsStyleLegendPatchShapeEntity : QgsStyleEntityInterface
{
%Docstring
A legend patch shape entity for QgsStyle databases.

.. versionadded:: 3.14
%End

%TypeHeaderCode
#include "qgsstyle.h"
%End
public:

QgsStyleLegendPatchShapeEntity( const QgsLegendPatchShape &shape );
%Docstring
Constructor for QgsStyleLegendPatchShapeEntity, with the specified legend patch ``shape``.
%End

virtual QgsStyle::StyleEntity type() const;



const QgsLegendPatchShape &shape() const;
%Docstring
Returns the entity's legend patch shape.
%End

};

/************************************************************************
* This file has been generated automatically from *
* *
Expand Down
31 changes: 31 additions & 0 deletions python/gui/auto_generated/qgslegendpatchshapewidget.sip.in
Expand Up @@ -8,6 +8,7 @@




class QgsLegendPatchShapeWidget : QgsPanelWidget
{
%Docstring
Expand Down Expand Up @@ -49,6 +50,36 @@ Emitted whenever the patch shape defined by the widget is changed.

};

class QgsLegendPatchShapeDialog : QDialog
{
%Docstring
A dialog for configuring a custom legend patch shape.

.. versionadded:: 3.14
%End

%TypeHeaderCode
#include "qgslegendpatchshapewidget.h"
%End
public:

QgsLegendPatchShapeDialog( const QgsLegendPatchShape &shape, QWidget *parent /TransferThis/ = 0 );
%Docstring
Constructor for QgsLegendPatchShapeDialog, initially showing the specified ``shape``.
%End

QgsLegendPatchShape shape() const;
%Docstring
Returns the legend patch shape defined by the dialog.
%End

QDialogButtonBox *buttonBox() const;
%Docstring
Returns a reference to the dialog's button box.
%End

};

/************************************************************************
* This file has been generated automatically from *
* *
Expand Down
45 changes: 45 additions & 0 deletions src/core/layout/qgslayoutitemlegend.cpp
Expand Up @@ -32,6 +32,7 @@
#include "qgslayertreeutils.h"
#include "qgslayoututils.h"
#include "qgsmapthemecollection.h"
#include "qgsstyleentityvisitor.h"
#include <QDomDocument>
#include <QDomElement>
#include <QPainter>
Expand Down Expand Up @@ -932,6 +933,50 @@ QgsLayoutItem::ExportLayerBehavior QgsLayoutItemLegend::exportLayerBehavior() co
return MustPlaceInOwnLayer;
}

bool QgsLayoutItemLegend::accept( QgsStyleEntityVisitorInterface *visitor ) const
{
std::function<bool( QgsLayerTreeGroup *group ) >visit;

visit = [ =, &visit]( QgsLayerTreeGroup * group ) -> bool
{
const QList<QgsLayerTreeNode *> childNodes = group->children();
for ( QgsLayerTreeNode *node : childNodes )
{
if ( QgsLayerTree::isGroup( node ) )
{
QgsLayerTreeGroup *nodeGroup = QgsLayerTree::toGroup( node );
if ( !visit( nodeGroup ) )
return false;
}
else if ( QgsLayerTree::isLayer( node ) )
{
QgsLayerTreeLayer *nodeLayer = QgsLayerTree::toLayer( node );
if ( !nodeLayer->patchShape().isNull() )
{
QgsStyleLegendPatchShapeEntity entity( nodeLayer->patchShape() );
if ( !visitor->visit( QgsStyleEntityVisitorInterface::StyleLeaf( &entity, uuid(), displayName() ) ) )
return false;
}
const QList<QgsLayerTreeModelLegendNode *> legendNodes = mLegendModel->layerLegendNodes( nodeLayer );
for ( QgsLayerTreeModelLegendNode *legendNode : legendNodes )
{
if ( QgsSymbolLegendNode *symbolNode = dynamic_cast< QgsSymbolLegendNode * >( legendNode ) )
{
if ( !symbolNode->patchShape().isNull() )
{
QgsStyleLegendPatchShapeEntity entity( symbolNode->patchShape() );
if ( !visitor->visit( QgsStyleEntityVisitorInterface::StyleLeaf( &entity, uuid(), displayName() ) ) )
return false;
}
}
}
}
}
return true;
};
return visit( mLegendModel->rootGroup( ) );
}


// -------------------------------------------------------------------------
#include "qgslayertreemodellegendnode.h"
Expand Down
1 change: 1 addition & 0 deletions src/core/layout/qgslayoutitemlegend.h
Expand Up @@ -507,6 +507,7 @@ class CORE_EXPORT QgsLayoutItemLegend : public QgsLayoutItem

QgsExpressionContext createExpressionContext() const override;
ExportLayerBehavior exportLayerBehavior() const override;
bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;

public slots:

Expand Down

0 comments on commit 8a0e007

Please sign in to comment.