Skip to content

Commit

Permalink
add API to select section of layer style when reading/writing
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids authored and nyalldawson committed Sep 14, 2018
1 parent 3875adf commit 51e9be0
Show file tree
Hide file tree
Showing 18 changed files with 290 additions and 166 deletions.
4 changes: 4 additions & 0 deletions python/core/auto_additions/qgsmaplayerstyle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# The following has been generated automatically from src/core/qgsmaplayerstyle.h
QgsMapLayerStyle.StyleCategory.baseClass = QgsMapLayerStyle
QgsMapLayerStyle.StyleCategories.baseClass = QgsMapLayerStyle
StyleCategories = QgsMapLayerStyle # dirty hack since SIP seems to introduce the flags in module
8 changes: 4 additions & 4 deletions python/core/auto_generated/mesh/qgsmeshlayer.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ QgsMeshLayer cannot be copied.

virtual QgsMapLayerRenderer *createMapRenderer( QgsRenderContext &rendererContext ) /Factory/;

virtual bool readSymbology( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context );

virtual bool writeSymbology( QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context ) const;

virtual bool readSymbology( const QDomNode &node, QString &errorMessage,
QgsReadWriteContext &context, QgsMapLayerStyle::StyleCategories categories = QgsMapLayerStyle::All );
virtual bool writeSymbology( QDomNode &node, QDomDocument &doc, QString &errorMessage,
const QgsReadWriteContext &context, QgsMapLayerStyle::StyleCategories categories = QgsMapLayerStyle::All ) const;
virtual QString encodedSource( const QString &source, const QgsReadWriteContext &context ) const;

virtual QString decodedSource( const QString &source, const QString &provider, const QgsReadWriteContext &context ) const;
Expand Down
42 changes: 29 additions & 13 deletions python/core/auto_generated/qgsmaplayer.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -503,8 +503,8 @@ Sets state from Dom document
The Dom node corresponds to a Dom document project file XML element read
by :py:class:`QgsProject`.

This, in turn, calls readXml(), which is over-rideable by sub-classes so
that they can read their own specific state from the given Dom node.
This, in turn, calls readXml() (and then readSymbology()), which is over-rideable
by sub-classes so that they can read their own specific state from the given Dom node.

Invoked by :py:func:`QgsProject.read()`

Expand All @@ -523,8 +523,8 @@ Stores state in Dom node
The Dom node corresponds to a Dom document project file XML element to be
written by :py:class:`QgsProject`.

This, in turn, calls writeXml(), which is over-rideable by sub-classes so
that they can write their own specific state to the given Dom node.
This, in turn, calls writeXml() (and then writeSymbology), which is over-rideable
by sub-classes so that they can write their own specific state to the given Dom node.

Invoked by :py:func:`QgsProject.write()`

Expand Down Expand Up @@ -783,7 +783,8 @@ Import the properties of this layer from a QDomDocument
.. versionadded:: 2.8
%End

virtual void exportNamedStyle( QDomDocument &doc, QString &errorMsg /Out/ ) const;
virtual void exportNamedStyle( QDomDocument &doc, QString &errorMsg /Out/, QgsReadWriteContext &context,
QgsMapLayerStyle::StyleCategories categories = QgsMapLayerStyle::All ) const;
%Docstring
Export the properties of this layer as named style in a QDomDocument

Expand Down Expand Up @@ -865,7 +866,8 @@ Attempts to style the layer using the formatting from an SLD type file.



virtual bool readSymbology( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context ) = 0;
virtual bool readSymbology( const QDomNode &node, QString &errorMessage,
QgsReadWriteContext &context, QgsMapLayerStyle::StyleCategories categories = QgsMapLayerStyle::All ) = 0;
%Docstring
Read the symbology for the current layer from the Dom node supplied.

Expand All @@ -876,7 +878,8 @@ Read the symbology for the current layer from the Dom node supplied.
:return: true in case of success.
%End

virtual bool readStyle( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context );
virtual bool readStyle( const QDomNode &node, QString &errorMessage,
QgsReadWriteContext &context, QgsMapLayerStyle::StyleCategories categories = QgsMapLayerStyle::All );
%Docstring
Read the style for the current layer from the Dom node supplied.

Expand All @@ -893,21 +896,27 @@ Read the style for the current layer from the Dom node supplied.
.. versionadded:: 2.16
%End

virtual bool writeSymbology( QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context ) const = 0;
virtual bool writeSymbology( QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context,
QgsMapLayerStyle::StyleCategories categories = QgsMapLayerStyle::All ) const = 0;
%Docstring
Write the symbology for the layer into the docment provided.
Write the style for the layer into the docment provided.

:param node: the node that will have the style element added to it.
:param doc: the document that will have the QDomNode added.
:param errorMessage: reference to string that will be updated with any error messages
:param context: writing context (used for transform from absolute to relative paths)

.. note::

There is a confusion of terms with the GUI. This method actually writes what is called a style in the application.

:return: true in case of success.
%End

virtual bool writeStyle( QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context ) const;
virtual bool writeStyle( QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context,
QgsMapLayerStyle::StyleCategories categories = QgsMapLayerStyle::All ) const;
%Docstring
Write just the style information for the layer into the document
Write just the symbology information for the layer into the document

:param node: the node that will have the style element added to it.
:param doc: the document that will have the QDomNode added.
Expand All @@ -920,6 +929,10 @@ Write just the style information for the layer into the document

To be implemented in subclasses. Default implementation does nothing and returns false.

.. note::

There is a confusion of terms with the GUI. This method actually writes what is known as the symbology in the application.

.. versionadded:: 2.16
%End

Expand Down Expand Up @@ -1475,14 +1488,17 @@ Read style manager's configuration (if any). To be called by subclasses.
Write style manager's configuration (if exists). To be called by subclasses.
%End

void writeCommonStyle( QDomElement &layerElement, QDomDocument &document, const QgsReadWriteContext &context ) const;
void writeCommonStyle( QDomElement &layerElement, QDomDocument &document,
const QgsReadWriteContext &context,
QgsMapLayerStyle::StyleCategories categories = QgsMapLayerStyle::All ) const;
%Docstring
Write style data common to all layer types

.. versionadded:: 3.0
%End

void readCommonStyle( const QDomElement &layerElement, const QgsReadWriteContext &context );
void readCommonStyle( const QDomElement &layerElement, const QgsReadWriteContext &context,
QgsMapLayerStyle::StyleCategories categories = QgsMapLayerStyle::All );
%Docstring
Read style data common to all layer types

Expand Down
24 changes: 24 additions & 0 deletions python/core/auto_generated/qgsmaplayerstyle.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,27 @@ only possible to read or write layer's current style.
#include "qgsmaplayerstyle.h"
%End
public:
static const QMetaObject staticMetaObject;

public:

enum StyleCategory
{
LayerConfiguration,
Symbology,
Labels,
Fields,
Forms,
Actions,
Tooltips,
Diagrams,
AttributeTable,
Rendering,
CustomProperties,
All
};
typedef QFlags<QgsMapLayerStyle::StyleCategory> StyleCategories;


QgsMapLayerStyle();
%Docstring
Expand Down Expand Up @@ -72,6 +93,9 @@ Write style configuration (for project file writing)

};

QFlags<QgsMapLayerStyle::StyleCategory> operator|(QgsMapLayerStyle::StyleCategory f1, QFlags<QgsMapLayerStyle::StyleCategory> f2);



class QgsMapLayerStyleOverride
{
Expand Down
16 changes: 8 additions & 8 deletions python/core/auto_generated/qgsvectorlayer.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -870,8 +870,8 @@ Returns the current auxiliary layer.
%End


virtual bool readSymbology( const QDomNode &layerNode, QString &errorMessage, QgsReadWriteContext &context );

virtual bool readSymbology( const QDomNode &layerNode, QString &errorMessage,
QgsReadWriteContext &context, QgsMapLayerStyle::StyleCategories categories = QgsMapLayerStyle::All );
%Docstring
Read the symbology for the current layer from the Dom node supplied.

Expand All @@ -882,8 +882,8 @@ Read the symbology for the current layer from the Dom node supplied.
:return: true in case of success.
%End

virtual bool readStyle( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context );

virtual bool readStyle( const QDomNode &node, QString &errorMessage,
QgsReadWriteContext &context, QgsMapLayerStyle::StyleCategories categories = QgsMapLayerStyle::All );
%Docstring
Read the style for the current layer from the Dom node supplied.

Expand All @@ -894,8 +894,8 @@ Read the style for the current layer from the Dom node supplied.
:return: true in case of success.
%End

virtual bool writeSymbology( QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context ) const;

virtual bool writeSymbology( QDomNode &node, QDomDocument &doc, QString &errorMessage,
const QgsReadWriteContext &context, QgsMapLayerStyle::StyleCategories categories = QgsMapLayerStyle::All ) const;
%Docstring
Write the symbology for the layer into the docment provided.

Expand All @@ -907,8 +907,8 @@ Write the symbology for the layer into the docment provided.
:return: true in case of success.
%End

virtual bool writeStyle( QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context ) const;

virtual bool writeStyle( QDomNode &node, QDomDocument &doc, QString &errorMessage,
const QgsReadWriteContext &context, QgsMapLayerStyle::StyleCategories categories = QgsMapLayerStyle::All ) const;
%Docstring
Write just the style information for the layer into the document

Expand Down
12 changes: 6 additions & 6 deletions python/core/auto_generated/raster/qgsrasterlayer.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -348,16 +348,16 @@ Draws a preview of the rasterlayer into a QImage
void showStatusMessage( const QString &message );

protected:
virtual bool readSymbology( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context );
virtual bool readSymbology( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context, QgsMapLayerStyle::StyleCategories categories = QgsMapLayerStyle::All );

virtual bool readStyle( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context );
virtual bool readStyle( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context, QgsMapLayerStyle::StyleCategories categories = QgsMapLayerStyle::All );

virtual bool readXml( const QDomNode &layer_node, QgsReadWriteContext &context );

virtual bool writeSymbology( QDomNode &, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context ) const;

virtual bool writeStyle( QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context ) const;

virtual bool writeSymbology( QDomNode &, QDomDocument &doc, QString &errorMessage,
const QgsReadWriteContext &context, QgsMapLayerStyle::StyleCategories categories = QgsMapLayerStyle::All ) const;
virtual bool writeStyle( QDomNode &node, QDomDocument &doc, QString &errorMessage,
const QgsReadWriteContext &context, QgsMapLayerStyle::StyleCategories categories = QgsMapLayerStyle::All ) const;
virtual bool writeXml( QDomNode &layer_node, QDomDocument &doc, const QgsReadWriteContext &context ) const;

virtual QString encodedSource( const QString &source, const QgsReadWriteContext &context ) const;
Expand Down
6 changes: 4 additions & 2 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8855,7 +8855,8 @@ void QgisApp::copyStyle( QgsMapLayer *sourceLayer )
{
QString errorMsg;
QDomDocument doc( QStringLiteral( "qgis" ) );
selectionLayer->exportNamedStyle( doc, errorMsg );
QgsReadWriteContext context;
selectionLayer->exportNamedStyle( doc, errorMsg, context );


if ( !errorMsg.isEmpty() )
Expand Down Expand Up @@ -9730,7 +9731,8 @@ void QgisApp::duplicateLayers( const QList<QgsMapLayer *> &lyrList )
// duplicate the layer style
QString errMsg;
QDomDocument style;
selectedLyr->exportNamedStyle( style, errMsg );
QgsReadWriteContext context;
selectedLyr->exportNamedStyle( style, errMsg, context );
if ( errMsg.isEmpty() )
dupLayer->importNamedStyle( style, errMsg );
if ( !errMsg.isEmpty() )
Expand Down
8 changes: 6 additions & 2 deletions src/core/mesh/qgsmeshlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,12 @@ QgsMapLayerRenderer *QgsMeshLayer::createMapRenderer( QgsRenderContext &renderer
return new QgsMeshLayerRenderer( this, rendererContext );
}

bool QgsMeshLayer::readSymbology( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context )
bool QgsMeshLayer::readSymbology( const QDomNode &node, QString &errorMessage,
QgsReadWriteContext &context, QgsMapLayerStyle::StyleCategories categories )
{
Q_UNUSED( errorMessage );
Q_UNUSED( context );
Q_UNUSED( categories );

QDomElement elem = node.toElement();
QDomElement elemRendererSettings = elem.firstChildElement( "mesh-renderer-settings" );
Expand All @@ -255,10 +257,12 @@ bool QgsMeshLayer::readSymbology( const QDomNode &node, QString &errorMessage, Q
return true;
}

bool QgsMeshLayer::writeSymbology( QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context ) const
bool QgsMeshLayer::writeSymbology( QDomNode &node, QDomDocument &doc, QString &errorMessage,
const QgsReadWriteContext &context, QgsMapLayerStyle::StyleCategories categories ) const
{
Q_UNUSED( errorMessage );
Q_UNUSED( context );
Q_UNUSED( categories );

QDomElement elem = node.toElement();
QDomElement elemRendererSettings = mRendererSettings.writeXml( doc );
Expand Down
6 changes: 4 additions & 2 deletions src/core/mesh/qgsmeshlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,10 @@ class CORE_EXPORT QgsMeshLayer : public QgsMapLayer
QgsMeshLayer *clone() const override SIP_FACTORY;
QgsRectangle extent() const override;
QgsMapLayerRenderer *createMapRenderer( QgsRenderContext &rendererContext ) override SIP_FACTORY;
bool readSymbology( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context ) override;
bool writeSymbology( QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context ) const override;
bool readSymbology( const QDomNode &node, QString &errorMessage,
QgsReadWriteContext &context, QgsMapLayerStyle::StyleCategories categories = QgsMapLayerStyle::All ) override;
bool writeSymbology( QDomNode &node, QDomDocument &doc, QString &errorMessage,
const QgsReadWriteContext &context, QgsMapLayerStyle::StyleCategories categories = QgsMapLayerStyle::All ) const override;
QString encodedSource( const QString &source, const QgsReadWriteContext &context ) const override;
QString decodedSource( const QString &source, const QString &provider, const QgsReadWriteContext &context ) const override;
bool readXml( const QDomNode &layer_node, QgsReadWriteContext &context ) override;
Expand Down
Loading

0 comments on commit 51e9be0

Please sign in to comment.