diff --git a/src/core/qgsvectorlayer.cpp b/src/core/qgsvectorlayer.cpp index 61ba712696b..44a8fcafb78 100644 --- a/src/core/qgsvectorlayer.cpp +++ b/src/core/qgsvectorlayer.cpp @@ -1485,7 +1485,8 @@ bool QgsVectorLayer::writeXml( QDomNode & layer_node, bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage ) { - Q_UNUSED( errorMessage ); + emit readCustomSymbology( node.toElement(), errorMessage ); + if ( hasGeometryType() ) { // try renderer v2 first @@ -1644,7 +1645,6 @@ bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage if ( !aliasesNode.isNull() ) { QDomElement aliasElem; - QString name; QDomNodeList aliasNodeList = aliasesNode.toElement().elementsByTagName( "alias" ); for ( int i = 0; i < aliasNodeList.size(); ++i ) @@ -1767,9 +1767,10 @@ QgsAttributeEditorElement* QgsVectorLayer::attributeEditorElementFromDomElement( bool QgsVectorLayer::writeSymbology( QDomNode& node, QDomDocument& doc, QString& errorMessage ) const { - Q_UNUSED( errorMessage ); QDomElement mapLayerNode = node.toElement(); + emit writeCustomSymbology( mapLayerNode, doc, errorMessage ); + if ( hasGeometryType() ) { QDomElement rendererElement = mRendererV2->save( doc ); @@ -2822,8 +2823,10 @@ void QgsVectorLayer::uniqueValues( int index, QList &uniqueValues, int if ( mEditBuffer ) { QSet vals; - Q_FOREACH ( const QVariant& v, uniqueValues ) + Q_FOREACH( const QVariant& v, uniqueValues ) + { vals << v.toString(); + } QMapIterator< QgsFeatureId, QgsAttributeMap > it( mEditBuffer->changedAttributeValues() ); while ( it.hasNext() && ( limit < 0 || uniqueValues.count() < limit ) ) @@ -3546,13 +3549,13 @@ void QgsVectorLayer::invalidateSymbolCountedFlag() void QgsVectorLayer::onRelationsLoaded() { - Q_FOREACH ( QgsAttributeEditorElement* elem, mAttributeEditorElements ) + Q_FOREACH( QgsAttributeEditorElement* elem, mAttributeEditorElements ) { if ( elem->type() == QgsAttributeEditorElement::AeTypeContainer ) { QgsAttributeEditorContainer* cont = dynamic_cast< QgsAttributeEditorContainer* >( elem ); QList relations = cont->findElements( QgsAttributeEditorElement::AeTypeRelation ); - Q_FOREACH ( QgsAttributeEditorElement* relElem, relations ) + Q_FOREACH( QgsAttributeEditorElement* relElem, relations ) { QgsAttributeEditorRelation* rel = dynamic_cast< QgsAttributeEditorRelation* >( relElem ); rel->init( QgsProject::instance()->relationManager() ); @@ -3608,7 +3611,7 @@ QDomElement QgsAttributeEditorContainer::toDomElement( QDomDocument& doc ) const QDomElement elem = doc.createElement( "attributeEditorContainer" ); elem.setAttribute( "name", mName ); - Q_FOREACH ( QgsAttributeEditorElement* child, mChildren ) + Q_FOREACH( QgsAttributeEditorElement* child, mChildren ) { elem.appendChild( child->toDomElement( doc ) ); } @@ -3624,7 +3627,7 @@ QList QgsAttributeEditorContainer::findElements( Qgs { QList results; - Q_FOREACH ( QgsAttributeEditorElement* elem, mChildren ) + Q_FOREACH( QgsAttributeEditorElement* elem, mChildren ) { if ( elem->type() == type ) { diff --git a/src/core/qgsvectorlayer.h b/src/core/qgsvectorlayer.h index 76d3fcbf43d..a08aabcb89e 100644 --- a/src/core/qgsvectorlayer.h +++ b/src/core/qgsvectorlayer.h @@ -1685,6 +1685,28 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer */ void editCommandDestroyed(); + /** + * Signal emitted whenever the symbology (QML-file) for this layer is being read. + * If there is custom style information saved in the file, you can connect to this signal + * and update the layer style accordingly. + * + * @param element The XML layer style element. + * + * @param errorMessage Write error messages into this string. + */ + void readCustomSymbology( const QDomElement& element, QString& errorMessage ); + + /** + * Signal emitted whenever the symbology (QML-file) for this layer is being written. + * If there is custom style information you want to save to the file, you can connect + * to this signal and update the element accordingly. + * + * @param element The XML element where you can add additional style information to. + * @param doc The XML document that you can use to create new XML nodes. + * @param errorMessage Write error messages into this string. + */ + void writeCustomSymbology( QDomElement& element, QDomDocument& doc, QString& errorMessage ) const; + private slots: void onRelationsLoaded(); void onJoinedFieldsChanged(); diff --git a/src/gui/editorwidgets/core/qgseditorwidgetregistry.cpp b/src/gui/editorwidgets/core/qgseditorwidgetregistry.cpp index 84e1b497ff2..825974e7f24 100644 --- a/src/gui/editorwidgets/core/qgseditorwidgetregistry.cpp +++ b/src/gui/editorwidgets/core/qgseditorwidgetregistry.cpp @@ -21,6 +21,7 @@ #include "qgsmessagelog.h" #include "qgsproject.h" #include "qgsvectorlayer.h" +#include "qgsmaplayerregistry.h" QgsEditorWidgetRegistry* QgsEditorWidgetRegistry::instance() { @@ -32,6 +33,8 @@ QgsEditorWidgetRegistry::QgsEditorWidgetRegistry() { connect( QgsProject::instance(), SIGNAL( readMapLayer( QgsMapLayer*, const QDomElement& ) ), this, SLOT( readMapLayer( QgsMapLayer*, const QDomElement& ) ) ); connect( QgsProject::instance(), SIGNAL( writeMapLayer( QgsMapLayer*, QDomElement&, QDomDocument& ) ), this, SLOT( writeMapLayer( QgsMapLayer*, QDomElement&, QDomDocument& ) ) ); + + connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWasAdded( QgsMapLayer* ) ), this, SLOT( mapLayerAdded( QgsMapLayer* ) ) ); } QgsEditorWidgetRegistry::~QgsEditorWidgetRegistry() @@ -177,7 +180,7 @@ const QString QgsEditorWidgetRegistry::readLegacyConfig( QgsVectorLayer* vl, con Q_NOWARN_DEPRECATED_POP } -void QgsEditorWidgetRegistry::writeMapLayer( QgsMapLayer* mapLayer, QDomElement& layerElem, QDomDocument& doc ) +void QgsEditorWidgetRegistry::writeMapLayer( QgsMapLayer* mapLayer, QDomElement& layerElem, QDomDocument& doc ) const { if ( mapLayer->type() != QgsMapLayer::VectorLayer ) { @@ -223,3 +226,34 @@ void QgsEditorWidgetRegistry::writeMapLayer( QgsMapLayer* mapLayer, QDomElement& layerElem.appendChild( editTypesNode ); } + +void QgsEditorWidgetRegistry::mapLayerAdded( QgsMapLayer* mapLayer ) +{ + QgsVectorLayer* vl = qobject_cast( mapLayer ); + + if ( vl ) + { + connect( vl, SIGNAL( readCustomSymbology( const QDomElement&, QString& ) ), this, SLOT( readSymbology( const QDomElement&, QString& ) ) ); + connect( vl, SIGNAL( writeCustomSymbology( QDomElement&, QDomDocument&, QString& ) ), this, SLOT( writeSymbology( QDomElement&, QDomDocument&, QString& ) ) ); + } +} + +void QgsEditorWidgetRegistry::readSymbology( const QDomElement& element, QString& errorMessage ) +{ + Q_UNUSED( errorMessage ) + QgsVectorLayer* vl = qobject_cast( sender() ); + + Q_ASSERT( vl ); + + readMapLayer( vl, element ); +} + +void QgsEditorWidgetRegistry::writeSymbology( QDomElement& element, QDomDocument& doc, QString& errorMessage ) +{ + Q_UNUSED( errorMessage ) + QgsVectorLayer* vl = qobject_cast( sender() ); + + Q_ASSERT( vl ); + + writeMapLayer( vl, element, doc ); +} diff --git a/src/gui/editorwidgets/core/qgseditorwidgetregistry.h b/src/gui/editorwidgets/core/qgseditorwidgetregistry.h index f376cb98889..5d38889fb16 100644 --- a/src/gui/editorwidgets/core/qgseditorwidgetregistry.h +++ b/src/gui/editorwidgets/core/qgseditorwidgetregistry.h @@ -136,7 +136,33 @@ class GUI_EXPORT QgsEditorWidgetRegistry : public QObject * @param layerElem The XML element to which the config will be written * @param doc The document from which to create elements */ - void writeMapLayer( QgsMapLayer* mapLayer , QDomElement& layerElem, QDomDocument& doc ); + void writeMapLayer( QgsMapLayer* mapLayer , QDomElement& layerElem, QDomDocument& doc ) const; + + /** + * Will connect to appropriate signals from map layers to load and save style + * + * @param mapLayer The layer to connect + */ + void mapLayerAdded( QgsMapLayer* mapLayer ); + + /** + * Loads layer symbology for the layer that emitted the signal + * + * @param element The XML element containing the style information + * + * @param errorMessage Errors will be written into this string (unused) + */ + void readSymbology( const QDomElement& element, QString& errorMessage ); + + /** + * Saves layer symbology for the layer that emitted the signal + * + * @param element The XML element where the style information be written to + * @param doc The XML document where the style information be written to + * + * @param errorMessage Errors will be written into this string (unused) + */ + void writeSymbology( QDomElement& element, QDomDocument& doc, QString& errorMessage ); private: QMap mWidgetFactories;