QGIS API Documentation
4.3.0-Master (16eddb2670e)
Toggle main menu visibility
Loading...
Searching...
No Matches
src
core
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
6
Email : denis@opengis.ch
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
"
20
#include "
qgsmaplayerstylemanager.h
"
21
#include "
qgsreadwritecontext.h
"
22
23
#include <QDomElement>
24
#include <QString>
25
#include <QTextStream>
26
27
using namespace
Qt::StringLiterals;
28
29
QgsMapLayerStyle::QgsMapLayerStyle
(
const
QString &
xmlData
)
30
: mXmlData(
xmlData
)
31
{}
32
33
bool
QgsMapLayerStyle::isValid
()
const
34
{
35
return
!mXmlData.isEmpty();
36
}
37
38
void
QgsMapLayerStyle::clear
()
39
{
40
mXmlData.clear();
41
}
42
43
QString
QgsMapLayerStyle::xmlData
()
const
44
{
45
return
mXmlData;
46
}
47
48
void
QgsMapLayerStyle::readFromLayer
(
QgsMapLayer
*layer )
49
{
50
QString errorMsg;
51
QDomDocument doc;
52
const
QgsReadWriteContext
context;
53
layer->
exportNamedStyle
( doc, errorMsg, context );
54
if
( !errorMsg.isEmpty() )
55
{
56
QgsDebugError
(
"Failed to export style from layer: "
+ errorMsg );
57
return
;
58
}
59
60
mXmlData.clear();
61
QTextStream stream( &mXmlData );
62
doc.documentElement().save( stream, 0 );
63
}
64
65
void
QgsMapLayerStyle::writeToLayer
(
QgsMapLayer
*layer )
const
66
{
67
if
( !
isValid
() )
68
{
69
return
;
70
}
71
72
QDomDocument doc( u
"qgis"
_s );
73
if
( !doc.setContent( mXmlData ) )
74
{
75
QgsDebugError
( u
"Failed to parse XML of previously stored XML data - this should not happen!"
_s );
76
return
;
77
}
78
79
QString errorMsg;
80
if
( !layer->
importNamedStyle
( doc, errorMsg ) )
81
{
82
QgsDebugError
(
"Failed to import style to layer: "
+ errorMsg );
83
}
84
}
85
86
void
QgsMapLayerStyle::readXml
(
const
QDomElement &styleElement )
87
{
88
mXmlData.clear();
89
QTextStream stream( &mXmlData );
90
styleElement.firstChildElement().save( stream, 0 );
91
}
92
93
void
QgsMapLayerStyle::writeXml
( QDomElement &styleElement )
const
94
{
95
// the currently selected style has no content stored here (layer has all the information inside)
96
if
( !
isValid
() )
97
return
;
98
99
QDomDocument docX;
100
docX.setContent( mXmlData );
101
styleElement.appendChild( docX.documentElement() );
102
}
103
104
QgsMapLayerStyleOverride::~QgsMapLayerStyleOverride
()
105
{
106
if
( mLayer && mStyleOverridden )
107
mLayer->styleManager()->restoreOverrideStyle();
108
}
109
110
void
QgsMapLayerStyleOverride::setOverrideStyle
(
const
QString &style )
111
{
112
if
( mLayer )
113
{
114
if
( mStyleOverridden )
115
mLayer->styleManager()->restoreOverrideStyle();
116
117
mLayer->styleManager()->setOverrideStyle( style );
118
mStyleOverridden =
true
;
119
}
120
}
QgsMapLayerStyleOverride::setOverrideStyle
void setOverrideStyle(const QString &style)
Temporarily apply a different style to the layer.
Definition
qgsmaplayerstyle.cpp:110
QgsMapLayerStyleOverride::~QgsMapLayerStyleOverride
~QgsMapLayerStyleOverride()
Definition
qgsmaplayerstyle.cpp:104
QgsMapLayerStyle::readXml
void readXml(const QDomElement &styleElement)
Read style configuration (for project file reading).
Definition
qgsmaplayerstyle.cpp:86
QgsMapLayerStyle::readFromLayer
void readFromLayer(QgsMapLayer *layer)
Store layer's active style information in the instance.
Definition
qgsmaplayerstyle.cpp:48
QgsMapLayerStyle::clear
void clear()
Remove any stored style data (will get invalid).
Definition
qgsmaplayerstyle.cpp:38
QgsMapLayerStyle::writeToLayer
void writeToLayer(QgsMapLayer *layer) const
Apply stored layer's style information to the layer.
Definition
qgsmaplayerstyle.cpp:65
QgsMapLayerStyle::isValid
bool isValid() const
Tell whether the style is valid (i.e. there is something stored in it).
Definition
qgsmaplayerstyle.cpp:33
QgsMapLayerStyle::writeXml
void writeXml(QDomElement &styleElement) const
Write style configuration (for project file writing).
Definition
qgsmaplayerstyle.cpp:93
QgsMapLayerStyle::xmlData
QString xmlData() const
Returns XML content of the style.
Definition
qgsmaplayerstyle.cpp:43
QgsMapLayerStyle::QgsMapLayerStyle
QgsMapLayerStyle()=default
construct invalid style
QgsMapLayer
Base class for all map layer types.
Definition
qgsmaplayer.h:83
QgsMapLayer::exportNamedStyle
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.
Definition
qgsmaplayer.cpp:1798
QgsMapLayer::importNamedStyle
virtual bool importNamedStyle(QDomDocument &doc, QString &errorMsg, QgsMapLayer::StyleCategories categories=QgsMapLayer::AllStyleCategories)
Import the properties of this layer from a QDomDocument.
Definition
qgsmaplayer.cpp:1732
QgsReadWriteContext
A container for the context for various read/write operations on objects.
Definition
qgsreadwritecontext.h:38
qgslogger.h
QgsDebugError
#define QgsDebugError(str)
Definition
qgslogger.h:71
qgsmaplayer.h
qgsmaplayerstyle.h
qgsmaplayerstylemanager.h
qgsreadwritecontext.h
Generated on
for QGIS API Documentation by
1.17.0