Skip to content

Commit 8fdcbdd

Browse files
authored
distinct files for QgsMapLayerStyle and QgsMapLayerStyleManager (#7806)
1 parent 70a37bf commit 8fdcbdd

9 files changed

+338
-257
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/core/qgsmaplayerstyle.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
8+
9+
10+
11+
12+
13+
14+
class QgsMapLayerStyle
15+
{
16+
%Docstring
17+
Stores style information (renderer, opacity, labeling, diagrams etc.) applicable to a map layer.
18+
19+
Stored data are considered as opaque - it is not possible to access them directly or modify them - it is
20+
only possible to read or write layer's current style.
21+
22+
.. versionadded:: 2.8
23+
%End
24+
25+
%TypeHeaderCode
26+
#include "qgsmaplayerstyle.h"
27+
%End
28+
public:
29+
30+
QgsMapLayerStyle();
31+
%Docstring
32+
construct invalid style
33+
%End
34+
35+
explicit QgsMapLayerStyle( const QString &xmlData );
36+
%Docstring
37+
construct style from QML definition (XML)
38+
%End
39+
40+
bool isValid() const;
41+
%Docstring
42+
Tell whether the style is valid (i.e. there is something stored in it)
43+
%End
44+
45+
void clear();
46+
%Docstring
47+
Remove any stored style data (will get invalid)
48+
%End
49+
50+
QString xmlData() const;
51+
%Docstring
52+
Returns XML content of the style
53+
%End
54+
55+
void readFromLayer( QgsMapLayer *layer );
56+
%Docstring
57+
Store layer's active style information in the instance
58+
%End
59+
void writeToLayer( QgsMapLayer *layer ) const;
60+
%Docstring
61+
Apply stored layer's style information to the layer
62+
%End
63+
64+
void readXml( const QDomElement &styleElement );
65+
%Docstring
66+
Read style configuration (for project file reading)
67+
%End
68+
void writeXml( QDomElement &styleElement ) const;
69+
%Docstring
70+
Write style configuration (for project file writing)
71+
%End
72+
73+
};
74+
75+
76+
class QgsMapLayerStyleOverride
77+
{
78+
%Docstring
79+
Restore overridden layer style on destruction.
80+
81+
.. versionadded:: 3.2
82+
%End
83+
84+
%TypeHeaderCode
85+
#include "qgsmaplayerstyle.h"
86+
%End
87+
public:
88+
89+
QgsMapLayerStyleOverride( QgsMapLayer *layer );
90+
%Docstring
91+
Construct a style override object associated with a map layer.
92+
The overridden style will be restored upon object destruction.
93+
%End
94+
95+
~QgsMapLayerStyleOverride();
96+
97+
void setOverrideStyle( const QString &style );
98+
%Docstring
99+
Temporarily apply a different style to the layer. The argument
100+
can be either a style name or a full QML style definition.
101+
%End
102+
103+
};
104+
/************************************************************************
105+
* This file has been generated automatically from *
106+
* *
107+
* src/core/qgsmaplayerstyle.h *
108+
* *
109+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
110+
************************************************************************/

python/core/auto_generated/qgsmaplayerstylemanager.sip.in

-90
Original file line numberDiff line numberDiff line change
@@ -11,67 +11,6 @@
1111

1212

1313

14-
class QgsMapLayerStyle
15-
{
16-
%Docstring
17-
Stores style information (renderer, opacity, labeling, diagrams etc.) applicable to a map layer.
18-
19-
Stored data are considered as opaque - it is not possible to access them directly or modify them - it is
20-
only possible to read or write layer's current style.
21-
22-
.. versionadded:: 2.8
23-
%End
24-
25-
%TypeHeaderCode
26-
#include "qgsmaplayerstylemanager.h"
27-
%End
28-
public:
29-
QgsMapLayerStyle();
30-
%Docstring
31-
construct invalid style
32-
%End
33-
34-
explicit QgsMapLayerStyle( const QString &xmlData );
35-
%Docstring
36-
construct style from QML definition (XML)
37-
%End
38-
39-
bool isValid() const;
40-
%Docstring
41-
Tell whether the style is valid (i.e. there is something stored in it)
42-
%End
43-
44-
void clear();
45-
%Docstring
46-
Remove any stored style data (will get invalid)
47-
%End
48-
49-
QString xmlData() const;
50-
%Docstring
51-
Returns XML content of the style
52-
%End
53-
54-
void readFromLayer( QgsMapLayer *layer );
55-
%Docstring
56-
Store layer's active style information in the instance
57-
%End
58-
void writeToLayer( QgsMapLayer *layer ) const;
59-
%Docstring
60-
Apply stored layer's style information to the layer
61-
%End
62-
63-
void readXml( const QDomElement &styleElement );
64-
%Docstring
65-
Read style configuration (for project file reading)
66-
%End
67-
void writeXml( QDomElement &styleElement ) const;
68-
%Docstring
69-
Write style configuration (for project file writing)
70-
%End
71-
72-
};
73-
74-
7514
class QgsMapLayerStyleManager : QObject
7615
{
7716
%Docstring
@@ -223,35 +162,6 @@ Emitted when the current style has been changed
223162

224163
};
225164

226-
227-
class QgsMapLayerStyleOverride
228-
{
229-
%Docstring
230-
Restore overridden layer style on destruction.
231-
232-
.. versionadded:: 3.2
233-
%End
234-
235-
%TypeHeaderCode
236-
#include "qgsmaplayerstylemanager.h"
237-
%End
238-
public:
239-
240-
QgsMapLayerStyleOverride( QgsMapLayer *layer );
241-
%Docstring
242-
Construct a style override object associated with a map layer.
243-
The overridden style will be restored upon object destruction.
244-
%End
245-
246-
~QgsMapLayerStyleOverride();
247-
248-
void setOverrideStyle( const QString &style );
249-
%Docstring
250-
Temporarily apply a different style to the layer. The argument
251-
can be either a style name or a full QML style definition.
252-
%End
253-
254-
};
255165
/************************************************************************
256166
* This file has been generated automatically from *
257167
* *

python/core/core_auto.sip

+1
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@
334334
%Include auto_generated/qgsmaplayermodel.sip
335335
%Include auto_generated/qgsmaplayerproxymodel.sip
336336
%Include auto_generated/qgsmaplayerstore.sip
337+
%Include auto_generated/qgsmaplayerstyle.sip
337338
%Include auto_generated/qgsmaprenderercache.sip
338339
%Include auto_generated/qgsmaprenderercustompainterjob.sip
339340
%Include auto_generated/qgsmaprendererjob.sip

src/core/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ SET(QGIS_CORE_SRCS
227227
qgsmaplayermodel.cpp
228228
qgsmaplayerproxymodel.cpp
229229
qgsmaplayerstore.cpp
230+
qgsmaplayerstyle.cpp
230231
qgsmaplayerstylemanager.cpp
231232
qgsmaprenderercache.cpp
232233
qgsmaprenderercustompainterjob.cpp
@@ -609,6 +610,7 @@ SET(QGIS_CORE_MOC_HDRS
609610
qgsmaplayermodel.h
610611
qgsmaplayerproxymodel.h
611612
qgsmaplayerstore.h
613+
qgsmaplayerstyle.h
612614
qgsmaplayerstylemanager.h
613615
qgsmaprenderercache.h
614616
qgsmaprenderercustompainterjob.h

src/core/dxf/qgsdxfexport.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "qgsunittypes.h"
4242
#include "qgstextlabelfeature.h"
4343
#include "qgslogger.h"
44+
#include "qgsmaplayerstyle.h"
4445
#include "qgsmaplayerstylemanager.h"
4546

4647
#include "qgswkbtypes.h"

src/core/qgsmaplayerstyle.cpp

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
#include "qgsmaplayerstylemanager.h"
18+
19+
#include "qgslogger.h"
20+
21+
#include <QDomElement>
22+
#include <QTextStream>
23+
24+
25+
QgsMapLayerStyle::QgsMapLayerStyle( const QString &xmlData )
26+
: mXmlData( xmlData )
27+
{
28+
}
29+
30+
bool QgsMapLayerStyle::isValid() const
31+
{
32+
return !mXmlData.isEmpty();
33+
}
34+
35+
void QgsMapLayerStyle::clear()
36+
{
37+
mXmlData.clear();
38+
}
39+
40+
QString QgsMapLayerStyle::xmlData() const
41+
{
42+
return mXmlData;
43+
}
44+
45+
void QgsMapLayerStyle::readFromLayer( QgsMapLayer *layer )
46+
{
47+
QString errorMsg;
48+
QDomDocument doc;
49+
layer->exportNamedStyle( doc, errorMsg );
50+
if ( !errorMsg.isEmpty() )
51+
{
52+
QgsDebugMsg( "Failed to export style from layer: " + errorMsg );
53+
return;
54+
}
55+
56+
mXmlData.clear();
57+
QTextStream stream( &mXmlData );
58+
doc.documentElement().save( stream, 0 );
59+
}
60+
61+
void QgsMapLayerStyle::writeToLayer( QgsMapLayer *layer ) const
62+
{
63+
if ( !isValid() )
64+
{
65+
return;
66+
}
67+
68+
QDomDocument doc( QStringLiteral( "qgis" ) );
69+
if ( !doc.setContent( mXmlData ) )
70+
{
71+
QgsDebugMsg( "Failed to parse XML of previously stored XML data - this should not happen!" );
72+
return;
73+
}
74+
75+
QString errorMsg;
76+
if ( !layer->importNamedStyle( doc, errorMsg ) )
77+
{
78+
QgsDebugMsg( "Failed to import style to layer: " + errorMsg );
79+
}
80+
}
81+
82+
void QgsMapLayerStyle::readXml( const QDomElement &styleElement )
83+
{
84+
mXmlData.clear();
85+
QTextStream stream( &mXmlData );
86+
styleElement.firstChildElement().save( stream, 0 );
87+
}
88+
89+
void QgsMapLayerStyle::writeXml( QDomElement &styleElement ) const
90+
{
91+
// the currently selected style has no content stored here (layer has all the information inside)
92+
if ( !isValid() )
93+
return;
94+
95+
QDomDocument docX;
96+
docX.setContent( mXmlData );
97+
styleElement.appendChild( docX.documentElement() );
98+
}
99+
100+
QgsMapLayerStyleOverride::~QgsMapLayerStyleOverride()
101+
{
102+
if ( mLayer && mStyleOverridden )
103+
mLayer->styleManager()->restoreOverrideStyle();
104+
}
105+
106+
void QgsMapLayerStyleOverride::setOverrideStyle( const QString &style )
107+
{
108+
if ( mLayer )
109+
{
110+
if ( mStyleOverridden )
111+
mLayer->styleManager()->restoreOverrideStyle();
112+
113+
mLayer->styleManager()->setOverrideStyle( style );
114+
mStyleOverridden = true;
115+
}
116+
}

0 commit comments

Comments
 (0)