Skip to content

Commit

Permalink
Save edit widgets also to QML style information.
Browse files Browse the repository at this point in the history
Fix #11123
Fix #10752
  • Loading branch information
m-kuhn committed Sep 17, 2014
1 parent 307d4bd commit e6a259d
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 10 deletions.
19 changes: 11 additions & 8 deletions src/core/qgsvectorlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -2822,8 +2823,10 @@ void QgsVectorLayer::uniqueValues( int index, QList<QVariant> &uniqueValues, int
if ( mEditBuffer )
{
QSet<QString> 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 ) )
Expand Down Expand Up @@ -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<QgsAttributeEditorElement*> 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() );
Expand Down Expand Up @@ -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 ) );
}
Expand All @@ -3624,7 +3627,7 @@ QList<QgsAttributeEditorElement*> QgsAttributeEditorContainer::findElements( Qgs
{
QList<QgsAttributeEditorElement*> results;

Q_FOREACH ( QgsAttributeEditorElement* elem, mChildren )
Q_FOREACH( QgsAttributeEditorElement* elem, mChildren )
{
if ( elem->type() == type )
{
Expand Down
22 changes: 22 additions & 0 deletions src/core/qgsvectorlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
36 changes: 35 additions & 1 deletion src/gui/editorwidgets/core/qgseditorwidgetregistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "qgsmessagelog.h"
#include "qgsproject.h"
#include "qgsvectorlayer.h"
#include "qgsmaplayerregistry.h"

QgsEditorWidgetRegistry* QgsEditorWidgetRegistry::instance()
{
Expand All @@ -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()
Expand Down Expand Up @@ -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 )
{
Expand Down Expand Up @@ -223,3 +226,34 @@ void QgsEditorWidgetRegistry::writeMapLayer( QgsMapLayer* mapLayer, QDomElement&

layerElem.appendChild( editTypesNode );
}

void QgsEditorWidgetRegistry::mapLayerAdded( QgsMapLayer* mapLayer )
{
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( 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<QgsVectorLayer*>( sender() );

Q_ASSERT( vl );

readMapLayer( vl, element );
}

void QgsEditorWidgetRegistry::writeSymbology( QDomElement& element, QDomDocument& doc, QString& errorMessage )
{
Q_UNUSED( errorMessage )
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( sender() );

Q_ASSERT( vl );

writeMapLayer( vl, element, doc );
}
28 changes: 27 additions & 1 deletion src/gui/editorwidgets/core/qgseditorwidgetregistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<QString, QgsEditorWidgetFactory*> mWidgetFactories;
Expand Down

0 comments on commit e6a259d

Please sign in to comment.