QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgsmaplayerstyle.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmaplayersty.cpp
3 --------------------------------------
4 Date : September 2019
5 Copyright : (C) 2018 by Denis Rouzaud
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgsmaplayerstyle.h"
17
18#include "qgslogger.h"
19#include "qgsmaplayer.h"
21#include "qgsreadwritecontext.h"
22
23#include <QDomElement>
24#include <QTextStream>
25
27 : mXmlData( xmlData )
28{
29}
30
32{
33 return !mXmlData.isEmpty();
34}
35
37{
38 mXmlData.clear();
39}
40
42{
43 return mXmlData;
44}
45
47{
48 QString errorMsg;
49 QDomDocument doc;
50 const QgsReadWriteContext context;
51 layer->exportNamedStyle( doc, errorMsg, context );
52 if ( !errorMsg.isEmpty() )
53 {
54 QgsDebugError( "Failed to export style from layer: " + errorMsg );
55 return;
56 }
57
58 mXmlData.clear();
59 QTextStream stream( &mXmlData );
60 doc.documentElement().save( stream, 0 );
61}
62
64{
65 if ( !isValid() )
66 {
67 return;
68 }
69
70 QDomDocument doc( QStringLiteral( "qgis" ) );
71 if ( !doc.setContent( mXmlData ) )
72 {
73 QgsDebugError( QStringLiteral( "Failed to parse XML of previously stored XML data - this should not happen!" ) );
74 return;
75 }
76
77 QString errorMsg;
78 if ( !layer->importNamedStyle( doc, errorMsg ) )
79 {
80 QgsDebugError( "Failed to import style to layer: " + errorMsg );
81 }
82}
83
84void QgsMapLayerStyle::readXml( const QDomElement &styleElement )
85{
86 mXmlData.clear();
87 QTextStream stream( &mXmlData );
88 styleElement.firstChildElement().save( stream, 0 );
89}
90
91void QgsMapLayerStyle::writeXml( QDomElement &styleElement ) const
92{
93 // the currently selected style has no content stored here (layer has all the information inside)
94 if ( !isValid() )
95 return;
96
97 QDomDocument docX;
98 docX.setContent( mXmlData );
99 styleElement.appendChild( docX.documentElement() );
100}
101
103{
104 if ( mLayer && mStyleOverridden )
105 mLayer->styleManager()->restoreOverrideStyle();
106}
107
109{
110 if ( mLayer )
111 {
112 if ( mStyleOverridden )
113 mLayer->styleManager()->restoreOverrideStyle();
114
115 mLayer->styleManager()->setOverrideStyle( style );
116 mStyleOverridden = true;
117 }
118}
void setOverrideStyle(const QString &style)
Temporarily apply a different style to the layer.
void readXml(const QDomElement &styleElement)
Read style configuration (for project file reading).
void readFromLayer(QgsMapLayer *layer)
Store layer's active style information in the instance.
void clear()
Remove any stored style data (will get invalid).
void writeToLayer(QgsMapLayer *layer) const
Apply stored layer's style information to the layer.
bool isValid() const
Tell whether the style is valid (i.e. there is something stored in it).
void writeXml(QDomElement &styleElement) const
Write style configuration (for project file writing).
QString xmlData() const
Returns XML content of the style.
QgsMapLayerStyle()=default
construct invalid style
Base class for all map layer types.
Definition qgsmaplayer.h:80
virtual void exportNamedStyle(QDomDocument &doc, QString &errorMsg, const QgsReadWriteContext &context=QgsReadWriteContext(), QgsMapLayer::StyleCategories categories=QgsMapLayer::AllStyleCategories) const
Export the properties of this layer as named style in a QDomDocument.
virtual bool importNamedStyle(QDomDocument &doc, QString &errorMsg, QgsMapLayer::StyleCategories categories=QgsMapLayer::AllStyleCategories)
Import the properties of this layer from a QDomDocument.
A container for the context for various read/write operations on objects.
#define QgsDebugError(str)
Definition qgslogger.h:57