QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 : [email protected]
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"
18 #include "qgsreadwritecontext.h"
19 #include "qgsmaplayer.h"
20 
21 
22 #include "qgslogger.h"
23 
24 #include <QDomElement>
25 #include <QTextStream>
26 
27 
28 QgsMapLayerStyle::QgsMapLayerStyle( const QString &xmlData )
29  : mXmlData( xmlData )
30 {
31 }
32 
34 {
35  return !mXmlData.isEmpty();
36 }
37 
39 {
40  mXmlData.clear();
41 }
42 
44 {
45  return mXmlData;
46 }
47 
49 {
50  QString errorMsg;
51  QDomDocument doc;
52  QgsReadWriteContext context;
53  layer->exportNamedStyle( doc, errorMsg, context );
54  if ( !errorMsg.isEmpty() )
55  {
56  QgsDebugMsg( "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 
66 {
67  if ( !isValid() )
68  {
69  return;
70  }
71 
72  QDomDocument doc( QStringLiteral( "qgis" ) );
73  if ( !doc.setContent( mXmlData ) )
74  {
75  QgsDebugMsg( QStringLiteral( "Failed to parse XML of previously stored XML data - this should not happen!" ) );
76  return;
77  }
78 
79  QString errorMsg;
80  if ( !layer->importNamedStyle( doc, errorMsg ) )
81  {
82  QgsDebugMsg( "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 
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 }
QgsMapLayerStyleManager::setOverrideStyle
bool setOverrideStyle(const QString &styleDef)
Temporarily apply a different style to the layer.
Definition: qgsmaplayerstylemanager.cpp:187
qgsmaplayerstyle.h
QgsMapLayerStyle::writeToLayer
void writeToLayer(QgsMapLayer *layer) const
Apply stored layer's style information to the layer.
Definition: qgsmaplayerstyle.cpp:65
qgsmaplayerstylemanager.h
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:35
qgsreadwritecontext.h
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:1037
QgsMapLayerStyle::readFromLayer
void readFromLayer(QgsMapLayer *layer)
Store layer's active style information in the instance.
Definition: qgsmaplayerstyle.cpp:48
QgsMapLayer::styleManager
QgsMapLayerStyleManager * styleManager() const
Gets access to the layer's style manager.
Definition: qgsmaplayer.cpp:1806
QgsMapLayerStyleOverride::~QgsMapLayerStyleOverride
~QgsMapLayerStyleOverride()
Definition: qgsmaplayerstyle.cpp:104
QgsDebugMsg
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QgsMapLayerStyle::QgsMapLayerStyle
QgsMapLayerStyle()=default
construct invalid style
qgsmaplayer.h
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:1098
QgsMapLayerStyle::isValid
bool isValid() const
Tell whether the style is valid (i.e. there is something stored in it)
Definition: qgsmaplayerstyle.cpp:33
QgsMapLayer
Base class for all map layer types.
Definition: qgsmaplayer.h:83
QgsMapLayerStyle::clear
void clear()
Remove any stored style data (will get invalid)
Definition: qgsmaplayerstyle.cpp:38
QgsMapLayerStyle::xmlData
QString xmlData() const
Returns XML content of the style.
Definition: qgsmaplayerstyle.cpp:43
QgsMapLayerStyleOverride::setOverrideStyle
void setOverrideStyle(const QString &style)
Temporarily apply a different style to the layer.
Definition: qgsmaplayerstyle.cpp:110
qgslogger.h
QgsMapLayerStyle::writeXml
void writeXml(QDomElement &styleElement) const
Write style configuration (for project file writing)
Definition: qgsmaplayerstyle.cpp:93
QgsMapLayerStyleManager::restoreOverrideStyle
bool restoreOverrideStyle()
Restore the original store after a call to setOverrideStyle()
Definition: qgsmaplayerstylemanager.cpp:215
QgsMapLayerStyle::readXml
void readXml(const QDomElement &styleElement)
Read style configuration (for project file reading)
Definition: qgsmaplayerstyle.cpp:86