distinct files for QgsMapLayerStyle and QgsMapLayerStyleManager (#7806)

This commit is contained in:
Denis Rouzaud 2018-09-05 22:28:38 +02:00 committed by GitHub
parent 70a37bf981
commit 8fdcbdd36c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 338 additions and 257 deletions

View File

@ -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 *
************************************************************************/

View File

@ -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
@ -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 *
* *

View File

@ -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

View File

@ -227,6 +227,7 @@ SET(QGIS_CORE_SRCS
qgsmaplayermodel.cpp
qgsmaplayerproxymodel.cpp
qgsmaplayerstore.cpp
qgsmaplayerstyle.cpp
qgsmaplayerstylemanager.cpp
qgsmaprenderercache.cpp
qgsmaprenderercustompainterjob.cpp
@ -609,6 +610,7 @@ SET(QGIS_CORE_MOC_HDRS
qgsmaplayermodel.h
qgsmaplayerproxymodel.h
qgsmaplayerstore.h
qgsmaplayerstyle.h
qgsmaplayerstylemanager.h
qgsmaprenderercache.h
qgsmaprenderercustompainterjob.h

View File

@ -41,6 +41,7 @@
#include "qgsunittypes.h"
#include "qgstextlabelfeature.h"
#include "qgslogger.h"
#include "qgsmaplayerstyle.h"
#include "qgsmaplayerstylemanager.h"
#include "qgswkbtypes.h"

View File

@ -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;
}
}

106
src/core/qgsmaplayerstyle.h Normal file
View File

@ -0,0 +1,106 @@
/***************************************************************************
qgsmaplayersty.h
--------------------------------------
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. *
* *
***************************************************************************/
#ifndef QGSMAPLAYERSTYLE_H
#define QGSMAPLAYERSTYLE_H
#include "qgis_core.h"
#include "qgis_sip.h"
#include "qgsmaplayer.h"
#include <QByteArray>
#include <QMap>
#include <QStringList>
#include <QObject>
class QDomElement;
/**
* \ingroup core
* 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.
*
* \since QGIS 2.8
*/
class CORE_EXPORT QgsMapLayerStyle
{
public:
//! construct invalid style
QgsMapLayerStyle() = default;
//! construct style from QML definition (XML)
explicit QgsMapLayerStyle( const QString &xmlData );
//! Tell whether the style is valid (i.e. there is something stored in it)
bool isValid() const;
//! Remove any stored style data (will get invalid)
void clear();
//! Returns XML content of the style
QString xmlData() const;
//! Store layer's active style information in the instance
void readFromLayer( QgsMapLayer *layer );
//! Apply stored layer's style information to the layer
void writeToLayer( QgsMapLayer *layer ) const;
//! Read style configuration (for project file reading)
void readXml( const QDomElement &styleElement );
//! Write style configuration (for project file writing)
void writeXml( QDomElement &styleElement ) const;
private:
QString mXmlData;
};
/**
* \ingroup core
* Restore overridden layer style on destruction.
*
* \since QGIS 3.2
*/
class CORE_EXPORT QgsMapLayerStyleOverride
{
public:
/**
* Construct a style override object associated with a map layer.
* The overridden style will be restored upon object destruction.
*/
QgsMapLayerStyleOverride( QgsMapLayer *layer )
: mLayer( layer )
{
}
~QgsMapLayerStyleOverride();
/**
* Temporarily apply a different style to the layer. The argument
* can be either a style name or a full QML style definition.
*/
void setOverrideStyle( const QString &style );
private:
QgsMapLayer *mLayer = nullptr;
bool mStyleOverridden = false;
};
#endif // QGSMAPLAYERSTYLE_H

View File

@ -14,6 +14,7 @@
***************************************************************************/
#include "qgsmaplayerstylemanager.h"
#include "qgsmaplayerstyle.h"
#include "qgslogger.h"
@ -227,81 +228,3 @@ bool QgsMapLayerStyleManager::isDefault( const QString &styleName ) const
{
return styleName == defaultStyleName();
}
// -----
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() );
}

View File

@ -19,6 +19,7 @@
#include "qgis_core.h"
#include "qgis_sip.h"
#include "qgsmaplayer.h"
#include "qgsmaplayerstyle.h"
#include <QByteArray>
#include <QMap>
@ -28,48 +29,6 @@
class QDomElement;
/**
* \ingroup core
* 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.
*
* \since QGIS 2.8
*/
class CORE_EXPORT QgsMapLayerStyle
{
public:
//! construct invalid style
QgsMapLayerStyle() = default;
//! construct style from QML definition (XML)
explicit QgsMapLayerStyle( const QString &xmlData );
//! Tell whether the style is valid (i.e. there is something stored in it)
bool isValid() const;
//! Remove any stored style data (will get invalid)
void clear();
//! Returns XML content of the style
QString xmlData() const;
//! Store layer's active style information in the instance
void readFromLayer( QgsMapLayer *layer );
//! Apply stored layer's style information to the layer
void writeToLayer( QgsMapLayer *layer ) const;
//! Read style configuration (for project file reading)
void readXml( const QDomElement &styleElement );
//! Write style configuration (for project file writing)
void writeXml( QDomElement &styleElement ) const;
private:
QString mXmlData;
};
/**
* \ingroup core
* Management of styles for use with one map layer. Stored styles are identified by their names. The manager
@ -194,51 +153,4 @@ class CORE_EXPORT QgsMapLayerStyleManager : public QObject
QString defaultStyleName() const;
};
/**
* \ingroup core
* Restore overridden layer style on destruction.
*
* \since QGIS 3.2
*/
class CORE_EXPORT QgsMapLayerStyleOverride
{
public:
/**
* Construct a style override object associated with a map layer.
* The overridden style will be restored upon object destruction.
*/
QgsMapLayerStyleOverride( QgsMapLayer *layer )
: mLayer( layer )
{
}
~QgsMapLayerStyleOverride()
{
if ( mLayer && mStyleOverridden )
mLayer->styleManager()->restoreOverrideStyle();
}
/**
* Temporarily apply a different style to the layer. The argument
* can be either a style name or a full QML style definition.
*/
void setOverrideStyle( const QString &style )
{
if ( mLayer )
{
if ( mStyleOverridden )
mLayer->styleManager()->restoreOverrideStyle();
mLayer->styleManager()->setOverrideStyle( style );
mStyleOverridden = true;
}
}
private:
QgsMapLayer *mLayer = nullptr;
bool mStyleOverridden = false;
};
#endif // QGSMAPLAYERSTYLEMANAGER_H