Skip to content

Commit

Permalink
distinct files for QgsMapLayerStyle and QgsMapLayerStyleManager (#7806)
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Sep 5, 2018
1 parent 70a37bf commit 8fdcbdd
Show file tree
Hide file tree
Showing 9 changed files with 338 additions and 257 deletions.
110 changes: 110 additions & 0 deletions python/core/auto_generated/qgsmaplayerstyle.sip.in
@@ -0,0 +1,110 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgsmaplayerstyle.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/






class QgsMapLayerStyle
{
%Docstring
Stores style information (renderer, opacity, labeling, diagrams etc.) applicable to a map layer.

Stored data are considered as opaque - it is not possible to access them directly or modify them - it is
only possible to read or write layer's current style.

.. versionadded:: 2.8
%End

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

QgsMapLayerStyle();
%Docstring
construct invalid style
%End

explicit QgsMapLayerStyle( const QString &xmlData );
%Docstring
construct style from QML definition (XML)
%End

bool isValid() const;
%Docstring
Tell whether the style is valid (i.e. there is something stored in it)
%End

void clear();
%Docstring
Remove any stored style data (will get invalid)
%End

QString xmlData() const;
%Docstring
Returns XML content of the style
%End

void readFromLayer( QgsMapLayer *layer );
%Docstring
Store layer's active style information in the instance
%End
void writeToLayer( QgsMapLayer *layer ) const;
%Docstring
Apply stored layer's style information to the layer
%End

void readXml( const QDomElement &styleElement );
%Docstring
Read style configuration (for project file reading)
%End
void writeXml( QDomElement &styleElement ) const;
%Docstring
Write style configuration (for project file writing)
%End

};


class QgsMapLayerStyleOverride
{
%Docstring
Restore overridden layer style on destruction.

.. versionadded:: 3.2
%End

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

QgsMapLayerStyleOverride( QgsMapLayer *layer );
%Docstring
Construct a style override object associated with a map layer.
The overridden style will be restored upon object destruction.
%End

~QgsMapLayerStyleOverride();

void setOverrideStyle( const QString &style );
%Docstring
Temporarily apply a different style to the layer. The argument
can be either a style name or a full QML style definition.
%End

};
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgsmaplayerstyle.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
90 changes: 0 additions & 90 deletions python/core/auto_generated/qgsmaplayerstylemanager.sip.in
Expand Up @@ -11,67 +11,6 @@



class QgsMapLayerStyle
{
%Docstring
Stores style information (renderer, opacity, labeling, diagrams etc.) applicable to a map layer.

Stored data are considered as opaque - it is not possible to access them directly or modify them - it is
only possible to read or write layer's current style.

.. versionadded:: 2.8
%End

%TypeHeaderCode
#include "qgsmaplayerstylemanager.h"
%End
public:
QgsMapLayerStyle();
%Docstring
construct invalid style
%End

explicit QgsMapLayerStyle( const QString &xmlData );
%Docstring
construct style from QML definition (XML)
%End

bool isValid() const;
%Docstring
Tell whether the style is valid (i.e. there is something stored in it)
%End

void clear();
%Docstring
Remove any stored style data (will get invalid)
%End

QString xmlData() const;
%Docstring
Returns XML content of the style
%End

void readFromLayer( QgsMapLayer *layer );
%Docstring
Store layer's active style information in the instance
%End
void writeToLayer( QgsMapLayer *layer ) const;
%Docstring
Apply stored layer's style information to the layer
%End

void readXml( const QDomElement &styleElement );
%Docstring
Read style configuration (for project file reading)
%End
void writeXml( QDomElement &styleElement ) const;
%Docstring
Write style configuration (for project file writing)
%End

};


class QgsMapLayerStyleManager : QObject
{
%Docstring
Expand Down Expand Up @@ -223,35 +162,6 @@ Emitted when the current style has been changed

};


class QgsMapLayerStyleOverride
{
%Docstring
Restore overridden layer style on destruction.

.. versionadded:: 3.2
%End

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

QgsMapLayerStyleOverride( QgsMapLayer *layer );
%Docstring
Construct a style override object associated with a map layer.
The overridden style will be restored upon object destruction.
%End

~QgsMapLayerStyleOverride();

void setOverrideStyle( const QString &style );
%Docstring
Temporarily apply a different style to the layer. The argument
can be either a style name or a full QML style definition.
%End

};
/************************************************************************
* This file has been generated automatically from *
* *
Expand Down
1 change: 1 addition & 0 deletions python/core/core_auto.sip
Expand Up @@ -334,6 +334,7 @@
%Include auto_generated/qgsmaplayermodel.sip
%Include auto_generated/qgsmaplayerproxymodel.sip
%Include auto_generated/qgsmaplayerstore.sip
%Include auto_generated/qgsmaplayerstyle.sip
%Include auto_generated/qgsmaprenderercache.sip
%Include auto_generated/qgsmaprenderercustompainterjob.sip
%Include auto_generated/qgsmaprendererjob.sip
Expand Down
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -227,6 +227,7 @@ SET(QGIS_CORE_SRCS
qgsmaplayermodel.cpp
qgsmaplayerproxymodel.cpp
qgsmaplayerstore.cpp
qgsmaplayerstyle.cpp
qgsmaplayerstylemanager.cpp
qgsmaprenderercache.cpp
qgsmaprenderercustompainterjob.cpp
Expand Down Expand Up @@ -609,6 +610,7 @@ SET(QGIS_CORE_MOC_HDRS
qgsmaplayermodel.h
qgsmaplayerproxymodel.h
qgsmaplayerstore.h
qgsmaplayerstyle.h
qgsmaplayerstylemanager.h
qgsmaprenderercache.h
qgsmaprenderercustompainterjob.h
Expand Down
1 change: 1 addition & 0 deletions src/core/dxf/qgsdxfexport.cpp
Expand Up @@ -41,6 +41,7 @@
#include "qgsunittypes.h"
#include "qgstextlabelfeature.h"
#include "qgslogger.h"
#include "qgsmaplayerstyle.h"
#include "qgsmaplayerstylemanager.h"

#include "qgswkbtypes.h"
Expand Down
116 changes: 116 additions & 0 deletions src/core/qgsmaplayerstyle.cpp
@@ -0,0 +1,116 @@
/***************************************************************************
qgsmaplayersty.cpp
--------------------------------------
Date : September 2019
Copyright : (C) 2018 by Denis Rouzaud
Email : denis@opengis.ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsmaplayerstyle.h"
#include "qgsmaplayerstylemanager.h"

#include "qgslogger.h"

#include <QDomElement>
#include <QTextStream>


QgsMapLayerStyle::QgsMapLayerStyle( const QString &xmlData )
: mXmlData( xmlData )
{
}

bool QgsMapLayerStyle::isValid() const
{
return !mXmlData.isEmpty();
}

void QgsMapLayerStyle::clear()
{
mXmlData.clear();
}

QString QgsMapLayerStyle::xmlData() const
{
return mXmlData;
}

void QgsMapLayerStyle::readFromLayer( QgsMapLayer *layer )
{
QString errorMsg;
QDomDocument doc;
layer->exportNamedStyle( doc, errorMsg );
if ( !errorMsg.isEmpty() )
{
QgsDebugMsg( "Failed to export style from layer: " + errorMsg );
return;
}

mXmlData.clear();
QTextStream stream( &mXmlData );
doc.documentElement().save( stream, 0 );
}

void QgsMapLayerStyle::writeToLayer( QgsMapLayer *layer ) const
{
if ( !isValid() )
{
return;
}

QDomDocument doc( QStringLiteral( "qgis" ) );
if ( !doc.setContent( mXmlData ) )
{
QgsDebugMsg( "Failed to parse XML of previously stored XML data - this should not happen!" );
return;
}

QString errorMsg;
if ( !layer->importNamedStyle( doc, errorMsg ) )
{
QgsDebugMsg( "Failed to import style to layer: " + errorMsg );
}
}

void QgsMapLayerStyle::readXml( const QDomElement &styleElement )
{
mXmlData.clear();
QTextStream stream( &mXmlData );
styleElement.firstChildElement().save( stream, 0 );
}

void QgsMapLayerStyle::writeXml( QDomElement &styleElement ) const
{
// the currently selected style has no content stored here (layer has all the information inside)
if ( !isValid() )
return;

QDomDocument docX;
docX.setContent( mXmlData );
styleElement.appendChild( docX.documentElement() );
}

QgsMapLayerStyleOverride::~QgsMapLayerStyleOverride()
{
if ( mLayer && mStyleOverridden )
mLayer->styleManager()->restoreOverrideStyle();
}

void QgsMapLayerStyleOverride::setOverrideStyle( const QString &style )
{
if ( mLayer )
{
if ( mStyleOverridden )
mLayer->styleManager()->restoreOverrideStyle();

mLayer->styleManager()->setOverrideStyle( style );
mStyleOverridden = true;
}
}

0 comments on commit 8fdcbdd

Please sign in to comment.