-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
distinct files for QgsMapLayerStyle and QgsMapLayerStyleManager (#7806)
- Loading branch information
Showing
9 changed files
with
338 additions
and
257 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 * | ||
************************************************************************/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
Oops, something went wrong.