Skip to content

Commit e6a259d

Browse files
committed
Save edit widgets also to QML style information.
Fix #11123 Fix #10752
1 parent 307d4bd commit e6a259d

File tree

4 files changed

+95
-10
lines changed

4 files changed

+95
-10
lines changed

src/core/qgsvectorlayer.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,7 +1485,8 @@ bool QgsVectorLayer::writeXml( QDomNode & layer_node,
14851485

14861486
bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage )
14871487
{
1488-
Q_UNUSED( errorMessage );
1488+
emit readCustomSymbology( node.toElement(), errorMessage );
1489+
14891490
if ( hasGeometryType() )
14901491
{
14911492
// try renderer v2 first
@@ -1644,7 +1645,6 @@ bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage
16441645
if ( !aliasesNode.isNull() )
16451646
{
16461647
QDomElement aliasElem;
1647-
QString name;
16481648

16491649
QDomNodeList aliasNodeList = aliasesNode.toElement().elementsByTagName( "alias" );
16501650
for ( int i = 0; i < aliasNodeList.size(); ++i )
@@ -1767,9 +1767,10 @@ QgsAttributeEditorElement* QgsVectorLayer::attributeEditorElementFromDomElement(
17671767

17681768
bool QgsVectorLayer::writeSymbology( QDomNode& node, QDomDocument& doc, QString& errorMessage ) const
17691769
{
1770-
Q_UNUSED( errorMessage );
17711770
QDomElement mapLayerNode = node.toElement();
17721771

1772+
emit writeCustomSymbology( mapLayerNode, doc, errorMessage );
1773+
17731774
if ( hasGeometryType() )
17741775
{
17751776
QDomElement rendererElement = mRendererV2->save( doc );
@@ -2822,8 +2823,10 @@ void QgsVectorLayer::uniqueValues( int index, QList<QVariant> &uniqueValues, int
28222823
if ( mEditBuffer )
28232824
{
28242825
QSet<QString> vals;
2825-
Q_FOREACH ( const QVariant& v, uniqueValues )
2826+
Q_FOREACH( const QVariant& v, uniqueValues )
2827+
{
28262828
vals << v.toString();
2829+
}
28272830

28282831
QMapIterator< QgsFeatureId, QgsAttributeMap > it( mEditBuffer->changedAttributeValues() );
28292832
while ( it.hasNext() && ( limit < 0 || uniqueValues.count() < limit ) )
@@ -3546,13 +3549,13 @@ void QgsVectorLayer::invalidateSymbolCountedFlag()
35463549

35473550
void QgsVectorLayer::onRelationsLoaded()
35483551
{
3549-
Q_FOREACH ( QgsAttributeEditorElement* elem, mAttributeEditorElements )
3552+
Q_FOREACH( QgsAttributeEditorElement* elem, mAttributeEditorElements )
35503553
{
35513554
if ( elem->type() == QgsAttributeEditorElement::AeTypeContainer )
35523555
{
35533556
QgsAttributeEditorContainer* cont = dynamic_cast< QgsAttributeEditorContainer* >( elem );
35543557
QList<QgsAttributeEditorElement*> relations = cont->findElements( QgsAttributeEditorElement::AeTypeRelation );
3555-
Q_FOREACH ( QgsAttributeEditorElement* relElem, relations )
3558+
Q_FOREACH( QgsAttributeEditorElement* relElem, relations )
35563559
{
35573560
QgsAttributeEditorRelation* rel = dynamic_cast< QgsAttributeEditorRelation* >( relElem );
35583561
rel->init( QgsProject::instance()->relationManager() );
@@ -3608,7 +3611,7 @@ QDomElement QgsAttributeEditorContainer::toDomElement( QDomDocument& doc ) const
36083611
QDomElement elem = doc.createElement( "attributeEditorContainer" );
36093612
elem.setAttribute( "name", mName );
36103613

3611-
Q_FOREACH ( QgsAttributeEditorElement* child, mChildren )
3614+
Q_FOREACH( QgsAttributeEditorElement* child, mChildren )
36123615
{
36133616
elem.appendChild( child->toDomElement( doc ) );
36143617
}
@@ -3624,7 +3627,7 @@ QList<QgsAttributeEditorElement*> QgsAttributeEditorContainer::findElements( Qgs
36243627
{
36253628
QList<QgsAttributeEditorElement*> results;
36263629

3627-
Q_FOREACH ( QgsAttributeEditorElement* elem, mChildren )
3630+
Q_FOREACH( QgsAttributeEditorElement* elem, mChildren )
36283631
{
36293632
if ( elem->type() == type )
36303633
{

src/core/qgsvectorlayer.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,6 +1685,28 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
16851685
*/
16861686
void editCommandDestroyed();
16871687

1688+
/**
1689+
* Signal emitted whenever the symbology (QML-file) for this layer is being read.
1690+
* If there is custom style information saved in the file, you can connect to this signal
1691+
* and update the layer style accordingly.
1692+
*
1693+
* @param element The XML layer style element.
1694+
*
1695+
* @param errorMessage Write error messages into this string.
1696+
*/
1697+
void readCustomSymbology( const QDomElement& element, QString& errorMessage );
1698+
1699+
/**
1700+
* Signal emitted whenever the symbology (QML-file) for this layer is being written.
1701+
* If there is custom style information you want to save to the file, you can connect
1702+
* to this signal and update the element accordingly.
1703+
*
1704+
* @param element The XML element where you can add additional style information to.
1705+
* @param doc The XML document that you can use to create new XML nodes.
1706+
* @param errorMessage Write error messages into this string.
1707+
*/
1708+
void writeCustomSymbology( QDomElement& element, QDomDocument& doc, QString& errorMessage ) const;
1709+
16881710
private slots:
16891711
void onRelationsLoaded();
16901712
void onJoinedFieldsChanged();

src/gui/editorwidgets/core/qgseditorwidgetregistry.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "qgsmessagelog.h"
2222
#include "qgsproject.h"
2323
#include "qgsvectorlayer.h"
24+
#include "qgsmaplayerregistry.h"
2425

2526
QgsEditorWidgetRegistry* QgsEditorWidgetRegistry::instance()
2627
{
@@ -32,6 +33,8 @@ QgsEditorWidgetRegistry::QgsEditorWidgetRegistry()
3233
{
3334
connect( QgsProject::instance(), SIGNAL( readMapLayer( QgsMapLayer*, const QDomElement& ) ), this, SLOT( readMapLayer( QgsMapLayer*, const QDomElement& ) ) );
3435
connect( QgsProject::instance(), SIGNAL( writeMapLayer( QgsMapLayer*, QDomElement&, QDomDocument& ) ), this, SLOT( writeMapLayer( QgsMapLayer*, QDomElement&, QDomDocument& ) ) );
36+
37+
connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWasAdded( QgsMapLayer* ) ), this, SLOT( mapLayerAdded( QgsMapLayer* ) ) );
3538
}
3639

3740
QgsEditorWidgetRegistry::~QgsEditorWidgetRegistry()
@@ -177,7 +180,7 @@ const QString QgsEditorWidgetRegistry::readLegacyConfig( QgsVectorLayer* vl, con
177180
Q_NOWARN_DEPRECATED_POP
178181
}
179182

180-
void QgsEditorWidgetRegistry::writeMapLayer( QgsMapLayer* mapLayer, QDomElement& layerElem, QDomDocument& doc )
183+
void QgsEditorWidgetRegistry::writeMapLayer( QgsMapLayer* mapLayer, QDomElement& layerElem, QDomDocument& doc ) const
181184
{
182185
if ( mapLayer->type() != QgsMapLayer::VectorLayer )
183186
{
@@ -223,3 +226,34 @@ void QgsEditorWidgetRegistry::writeMapLayer( QgsMapLayer* mapLayer, QDomElement&
223226

224227
layerElem.appendChild( editTypesNode );
225228
}
229+
230+
void QgsEditorWidgetRegistry::mapLayerAdded( QgsMapLayer* mapLayer )
231+
{
232+
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( mapLayer );
233+
234+
if ( vl )
235+
{
236+
connect( vl, SIGNAL( readCustomSymbology( const QDomElement&, QString& ) ), this, SLOT( readSymbology( const QDomElement&, QString& ) ) );
237+
connect( vl, SIGNAL( writeCustomSymbology( QDomElement&, QDomDocument&, QString& ) ), this, SLOT( writeSymbology( QDomElement&, QDomDocument&, QString& ) ) );
238+
}
239+
}
240+
241+
void QgsEditorWidgetRegistry::readSymbology( const QDomElement& element, QString& errorMessage )
242+
{
243+
Q_UNUSED( errorMessage )
244+
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( sender() );
245+
246+
Q_ASSERT( vl );
247+
248+
readMapLayer( vl, element );
249+
}
250+
251+
void QgsEditorWidgetRegistry::writeSymbology( QDomElement& element, QDomDocument& doc, QString& errorMessage )
252+
{
253+
Q_UNUSED( errorMessage )
254+
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( sender() );
255+
256+
Q_ASSERT( vl );
257+
258+
writeMapLayer( vl, element, doc );
259+
}

src/gui/editorwidgets/core/qgseditorwidgetregistry.h

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,33 @@ class GUI_EXPORT QgsEditorWidgetRegistry : public QObject
136136
* @param layerElem The XML element to which the config will be written
137137
* @param doc The document from which to create elements
138138
*/
139-
void writeMapLayer( QgsMapLayer* mapLayer , QDomElement& layerElem, QDomDocument& doc );
139+
void writeMapLayer( QgsMapLayer* mapLayer , QDomElement& layerElem, QDomDocument& doc ) const;
140+
141+
/**
142+
* Will connect to appropriate signals from map layers to load and save style
143+
*
144+
* @param mapLayer The layer to connect
145+
*/
146+
void mapLayerAdded( QgsMapLayer* mapLayer );
147+
148+
/**
149+
* Loads layer symbology for the layer that emitted the signal
150+
*
151+
* @param element The XML element containing the style information
152+
*
153+
* @param errorMessage Errors will be written into this string (unused)
154+
*/
155+
void readSymbology( const QDomElement& element, QString& errorMessage );
156+
157+
/**
158+
* Saves layer symbology for the layer that emitted the signal
159+
*
160+
* @param element The XML element where the style information be written to
161+
* @param doc The XML document where the style information be written to
162+
*
163+
* @param errorMessage Errors will be written into this string (unused)
164+
*/
165+
void writeSymbology( QDomElement& element, QDomDocument& doc, QString& errorMessage );
140166

141167
private:
142168
QMap<QString, QgsEditorWidgetFactory*> mWidgetFactories;

0 commit comments

Comments
 (0)