QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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  const 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 }
bool restoreOverrideStyle()
Restore the original store after a call to setOverrideStyle()
bool setOverrideStyle(const QString &styleDef)
Temporarily apply a different style to the layer.
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:73
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.
QgsMapLayerStyleManager * styleManager() const
Gets access to the layer's style manager.
The class is used as a container of context for various read/write operations on other objects.
#define QgsDebugMsg(str)
Definition: qgslogger.h:38