QGIS API Documentation 3.99.0-Master (d270888f95f)
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 <QString>
25#include <QTextStream>
26
27using namespace Qt::StringLiterals;
28
30 : mXmlData( xmlData )
31{
32}
33
35{
36 return !mXmlData.isEmpty();
37}
38
40{
41 mXmlData.clear();
42}
43
45{
46 return mXmlData;
47}
48
50{
51 QString errorMsg;
52 QDomDocument doc;
53 const QgsReadWriteContext context;
54 layer->exportNamedStyle( doc, errorMsg, context );
55 if ( !errorMsg.isEmpty() )
56 {
57 QgsDebugError( "Failed to export style from layer: " + errorMsg );
58 return;
59 }
60
61 mXmlData.clear();
62 QTextStream stream( &mXmlData );
63 doc.documentElement().save( stream, 0 );
64}
65
67{
68 if ( !isValid() )
69 {
70 return;
71 }
72
73 QDomDocument doc( u"qgis"_s );
74 if ( !doc.setContent( mXmlData ) )
75 {
76 QgsDebugError( u"Failed to parse XML of previously stored XML data - this should not happen!"_s );
77 return;
78 }
79
80 QString errorMsg;
81 if ( !layer->importNamedStyle( doc, errorMsg ) )
82 {
83 QgsDebugError( "Failed to import style to layer: " + errorMsg );
84 }
85}
86
87void QgsMapLayerStyle::readXml( const QDomElement &styleElement )
88{
89 mXmlData.clear();
90 QTextStream stream( &mXmlData );
91 styleElement.firstChildElement().save( stream, 0 );
92}
93
94void QgsMapLayerStyle::writeXml( QDomElement &styleElement ) const
95{
96 // the currently selected style has no content stored here (layer has all the information inside)
97 if ( !isValid() )
98 return;
99
100 QDomDocument docX;
101 docX.setContent( mXmlData );
102 styleElement.appendChild( docX.documentElement() );
103}
104
106{
107 if ( mLayer && mStyleOverridden )
108 mLayer->styleManager()->restoreOverrideStyle();
109}
110
112{
113 if ( mLayer )
114 {
115 if ( mStyleOverridden )
116 mLayer->styleManager()->restoreOverrideStyle();
117
118 mLayer->styleManager()->setOverrideStyle( style );
119 mStyleOverridden = true;
120 }
121}
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:83
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:59