Save and load new-style widget configuration

This commit is contained in:
Matthias Kuhn 2016-12-02 08:11:22 +01:00
parent 98f75a4084
commit f15284ec3d
9 changed files with 62 additions and 77 deletions

View File

@ -287,9 +287,4 @@ class QgsEditFormConfig
* Deserialize drag and drop designer elements.
*/
QgsAttributeEditorElement* attributeEditorElementFromDomElement( QDomElement &elem, QgsAttributeEditorElement* parent );
/**
* Parse the XML for the config of one editor widget.
*/
static QgsEditorWidgetConfig parseEditorWidgetConfig( const QDomElement& cfgElem );
};

View File

@ -355,42 +355,6 @@ void QgsEditFormConfig::readXml( const QDomNode& node )
}
}
}
QDomElement widgetsElem = node.namedItem( QStringLiteral( "widgets" ) ).toElement();
QDomNodeList widgetConfigsElems = widgetsElem.childNodes();
for ( int i = 0; i < widgetConfigsElems.size(); ++i )
{
const QDomElement wdgElem = widgetConfigsElems.at( i ).toElement();
const QDomElement cfgElem = wdgElem.namedItem( QStringLiteral( "config" ) ).toElement();
const QgsEditorWidgetConfig widgetConfig = parseEditorWidgetConfig( cfgElem );
setWidgetConfig( wdgElem.attribute( QStringLiteral( "name" ) ), widgetConfig );
}
}
QgsEditorWidgetConfig QgsEditFormConfig::parseEditorWidgetConfig( const QDomElement& cfgElem )
{
QgsEditorWidgetConfig cfg;
//// TODO: MAKE THIS MORE GENERIC, SO INDIVIDUALL WIDGETS CAN NOT ONLY SAVE STRINGS
/// SEE QgsEditorWidgetFactory::writeConfig
for ( int j = 0; j < cfgElem.attributes().size(); ++j )
{
const QDomAttr attr = cfgElem.attributes().item( j ).toAttr();
cfg.insert( attr.name(), attr.value() );
}
const QDomNodeList optionElements = cfgElem.elementsByTagName( QStringLiteral( "option" ) );
for ( int j = 0; j < optionElements.size(); ++j )
{
const QDomElement option = optionElements.at( j ).toElement();
const QString key = option.attribute( QStringLiteral( "key" ) );
const QString value = option.attribute( QStringLiteral( "value" ) );
cfg.insert( key, value );
}
//// END TODO
return cfg;
}
void QgsEditFormConfig::writeXml( QDomNode& node ) const

View File

@ -328,11 +328,6 @@ class CORE_EXPORT QgsEditFormConfig
*/
explicit QgsEditFormConfig();
/**
* Parse the XML for the config of one editor widget.
*/
static QgsEditorWidgetConfig parseEditorWidgetConfig( const QDomElement& cfgElem );
private:
/**

View File

@ -30,7 +30,11 @@ class CORE_EXPORT QgsEditorWidgetSetup
/**
* Constructor
*/
QgsEditorWidgetSetup( const QString& type, const QgsEditorWidgetConfig& config ) : mType( type ), mConfig( config ) {}
QgsEditorWidgetSetup( const QString& type, const QgsEditorWidgetConfig& config )
: mType( type )
, mConfig( config )
{}
QgsEditorWidgetSetup() {}
/**
@ -47,6 +51,7 @@ class CORE_EXPORT QgsEditorWidgetSetup
* @return true if there is no widget configured.
*/
bool isNull() const { return mType.isEmpty(); }
private:
QString mType;
QgsEditorWidgetConfig mConfig;

View File

@ -292,7 +292,7 @@ void QgsField::setEditorWidgetSetup( const QgsEditorWidgetSetup& v )
d->editorWidgetSetup = v;
}
const QgsEditorWidgetSetup& QgsField::editorWidgetSetup() const
QgsEditorWidgetSetup QgsField::editorWidgetSetup() const
{
return d->editorWidgetSetup;
}

View File

@ -269,7 +269,7 @@ class CORE_EXPORT QgsField
*
* @return the value
*/
const QgsEditorWidgetSetup& editorWidgetSetup() const;
QgsEditorWidgetSetup editorWidgetSetup() const;
private:

View File

@ -82,6 +82,7 @@
#include "qgssimplifymethod.h"
#include "qgsexpressioncontext.h"
#include "qgsfeedback.h"
#include "qgsconfigurationmap.h"
#include "diagram/qgsdiagram.h"
@ -1742,20 +1743,20 @@ bool QgsVectorLayer::writeXml( QDomNode & layer_node,
} // bool QgsVectorLayer::writeXml
bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage )
bool QgsVectorLayer::readSymbology( const QDomNode& layerNode, QString& errorMessage )
{
if ( !mExpressionFieldBuffer )
mExpressionFieldBuffer = new QgsExpressionFieldBuffer();
mExpressionFieldBuffer->readXml( node );
mExpressionFieldBuffer->readXml( layerNode );
updateFields();
readStyle( node, errorMessage );
readStyle( layerNode, errorMessage );
mDisplayExpression = node.namedItem( QStringLiteral( "previewExpression" ) ).toElement().text();
mMapTipTemplate = node.namedItem( QStringLiteral( "mapTip" ) ).toElement().text();
mDisplayExpression = layerNode.namedItem( QStringLiteral( "previewExpression" ) ).toElement().text();
mMapTipTemplate = layerNode.namedItem( QStringLiteral( "mapTip" ) ).toElement().text();
QString displayField = node.namedItem( QStringLiteral( "displayfield" ) ).toElement().text();
QString displayField = layerNode.namedItem( QStringLiteral( "displayfield" ) ).toElement().text();
// Try to migrate pre QGIS 3.0 display field property
if ( mFields.lookupField( displayField ) < 0 )
@ -1771,9 +1772,9 @@ bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage
}
// process the attribute actions
mActions->readXml( node );
mActions->readXml( layerNode );
QDomNode annotationFormNode = node.namedItem( QStringLiteral( "annotationform" ) );
QDomNode annotationFormNode = layerNode.namedItem( QStringLiteral( "annotationform" ) );
if ( !annotationFormNode.isNull() )
{
QDomElement e = annotationFormNode.toElement();
@ -1781,7 +1782,7 @@ bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage
}
mAttributeAliasMap.clear();
QDomNode aliasesNode = node.namedItem( QStringLiteral( "aliases" ) );
QDomNode aliasesNode = layerNode.namedItem( QStringLiteral( "aliases" ) );
if ( !aliasesNode.isNull() )
{
QDomElement aliasElem;
@ -1811,7 +1812,7 @@ bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage
//Attributes excluded from WMS and WFS
mExcludeAttributesWMS.clear();
QDomNode excludeWMSNode = node.namedItem( QStringLiteral( "excludeAttributesWMS" ) );
QDomNode excludeWMSNode = layerNode.namedItem( QStringLiteral( "excludeAttributesWMS" ) );
if ( !excludeWMSNode.isNull() )
{
QDomNodeList attributeNodeList = excludeWMSNode.toElement().elementsByTagName( QStringLiteral( "attribute" ) );
@ -1822,7 +1823,7 @@ bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage
}
mExcludeAttributesWFS.clear();
QDomNode excludeWFSNode = node.namedItem( QStringLiteral( "excludeAttributesWFS" ) );
QDomNode excludeWFSNode = layerNode.namedItem( QStringLiteral( "excludeAttributesWFS" ) );
if ( !excludeWFSNode.isNull() )
{
QDomNodeList attributeNodeList = excludeWFSNode.toElement().elementsByTagName( QStringLiteral( "attribute" ) );
@ -1832,13 +1833,33 @@ bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage
}
}
mEditFormConfig.readXml( node );
// Load editor widget configuration
QDomElement widgetsElem = layerNode.namedItem( QStringLiteral( "fieldConfiguration" ) ).toElement();
mAttributeTableConfig.readXml( node );
QDomNodeList fieldConfigurationElementList = widgetsElem.elementsByTagName( QStringLiteral( "field" ) );
mConditionalStyles->readXml( node );
for ( int i = 0; i < fieldConfigurationElementList.size(); ++i )
{
const QDomElement fieldConfigElement = fieldConfigurationElementList.at( i ).toElement();
const QDomElement fieldWidgetElement = fieldConfigElement.elementsByTagName( QStringLiteral( "editWidget" ) ).at( 0 ).toElement();
readCustomProperties( node, QStringLiteral( "variable" ) );
QString fieldName = fieldConfigElement.attribute( QStringLiteral( "name" ) );
const QString widgetType = fieldWidgetElement.attribute( QStringLiteral( "type" ) );
const QDomElement cfgElem = fieldConfigElement.elementsByTagName( QStringLiteral( "config" ) ).at( 0 ).toElement();
QgsConfigurationMap editWidgetConfiguration;
editWidgetConfiguration.fromXml( cfgElem );
QgsEditorWidgetSetup setup = QgsEditorWidgetSetup( widgetType, editWidgetConfiguration.get() );
mFieldWidgetSetups[fieldName] = setup;
}
mEditFormConfig.readXml( layerNode );
mAttributeTableConfig.readXml( layerNode );
mConditionalStyles->readXml( layerNode );
readCustomProperties( layerNode, QStringLiteral( "variable" ) );
return true;
}
@ -1956,19 +1977,14 @@ bool QgsVectorLayer::writeSymbology( QDomNode& node, QDomDocument& doc, QString&
{
( void )writeStyle( node, doc, errorMessage );
<<<<<<< HEAD
// FIXME
// edittypes are written to the layerNode
// by slot QgsEditorWidgetRegistry::writeMapLayer()
// triggered by signal QgsProject::writeMapLayer()
// still other editing settings are written here,
// although they are not part of symbology either
=======
QgsFields dataProviderFields = mDataProvider->fields();
QDomElement fieldConfigurationElement = doc.createElement( QStringLiteral( "fieldConfiguration" ) );
int index = 0;
Q_FOREACH ( const QgsField& field, mFields )
{
QDomElement fieldElement = doc.createElement( QStringLiteral( "field" ) );
fieldElement.setAttribute( QStringLiteral( "name" ), field.name() );
@ -1980,7 +1996,6 @@ bool QgsVectorLayer::writeSymbology( QDomNode& node, QDomDocument& doc, QString&
QDomElement editWidgetElement = doc.createElement( QStringLiteral( "editWidget" ) );
fieldElement.appendChild( editWidgetElement );
editWidgetElement.setAttribute( "type", field.editorWidgetSetup().type() );
QDomElement editWidgetConfigElement = doc.createElement( QStringLiteral( "config" ) );
QgsConfigurationMap( widgetSetup.config() ).toXml( editWidgetConfigElement );
@ -1989,7 +2004,6 @@ bool QgsVectorLayer::writeSymbology( QDomNode& node, QDomDocument& doc, QString&
++index;
}
>>>>>>> dc0c72f... Fixup
QDomElement afField = doc.createElement( QStringLiteral( "annotationform" ) );
QDomText afText = doc.createTextNode( QgsProject::instance()->writePath( mAnnotationForm ) );
@ -3056,6 +3070,16 @@ void QgsVectorLayer::updateFields()
mFields[ index ].setConstraints( constraints );
}
auto fieldWidgetIterator = mFieldWidgetSetups.constBegin();
for ( ; fieldWidgetIterator != mFieldWidgetSetups.constEnd(); ++ fieldWidgetIterator )
{
int index = mFields.indexOf( fieldWidgetIterator.key() );
if ( index < 0 )
continue;
mFields[index].setEditorWidgetSetup( fieldWidgetIterator.value() );
}
if ( oldFields != mFields )
{
emit updatedFields();

View File

@ -799,7 +799,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
* @param errorMessage reference to string that will be updated with any error messages
* @return true in case of success.
*/
bool readSymbology( const QDomNode& node, QString& errorMessage ) override;
bool readSymbology( const QDomNode& layerNode, QString& errorMessage ) override;
/** Read the style for the current layer from the Dom node supplied.
* @param node node that will contain the style definition for this layer.
@ -2016,6 +2016,8 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
//! Map which stores expression constraints for fields. Value is a pair of expression/description.
QMap< QString, QPair< QString, QString > > mFieldConstraintExpressions;
QMap< QString, QgsEditorWidgetSetup > mFieldWidgetSetups;
//! Holds the configuration for the edit form
QgsEditFormConfig mEditFormConfig;

View File

@ -80,7 +80,7 @@ void QgsEditorWidgetRegistry::initEditors( QgsMapCanvas *mapCanvas, QgsMessageBa
QgsEditorWidgetRegistry::QgsEditorWidgetRegistry()
{
connect( QgsProject::instance(), SIGNAL( readMapLayer( QgsMapLayer*, const QDomElement& ) ), this, SLOT( readMapLayer( QgsMapLayer*, const QDomElement& ) ) );
// 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( QgsProject::instance(), SIGNAL( layerWillBeRemoved( QgsMapLayer* ) ), this, SLOT( mapLayerWillBeRemoved( QgsMapLayer* ) ) );