Save edit widgets also to QML style information.

Fix #11123
Fix #10752
This commit is contained in:
Matthias Kuhn 2014-09-17 12:48:06 +02:00
parent 307d4bd5a9
commit e6a259d180
4 changed files with 95 additions and 10 deletions

View File

@ -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 );
@ -2823,7 +2824,9 @@ void QgsVectorLayer::uniqueValues( int index, QList<QVariant> &uniqueValues, int
{
QSet<QString> vals;
Q_FOREACH( const QVariant& v, uniqueValues )
{
vals << v.toString();
}
QMapIterator< QgsFeatureId, QgsAttributeMap > it( mEditBuffer->changedAttributeValues() );
while ( it.hasNext() && ( limit < 0 || uniqueValues.count() < limit ) )

View File

@ -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();

View File

@ -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<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 );
}

View File

@ -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;