Skip to content

Commit

Permalink
Save and load new-style widget configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Dec 20, 2016
1 parent 98f75a4 commit f15284e
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 77 deletions.
5 changes: 0 additions & 5 deletions python/core/qgseditformconfig.sip
Expand Up @@ -287,9 +287,4 @@ class QgsEditFormConfig
* Deserialize drag and drop designer elements. * Deserialize drag and drop designer elements.
*/ */
QgsAttributeEditorElement* attributeEditorElementFromDomElement( QDomElement &elem, QgsAttributeEditorElement* parent ); QgsAttributeEditorElement* attributeEditorElementFromDomElement( QDomElement &elem, QgsAttributeEditorElement* parent );

/**
* Parse the XML for the config of one editor widget.
*/
static QgsEditorWidgetConfig parseEditorWidgetConfig( const QDomElement& cfgElem );
}; };
36 changes: 0 additions & 36 deletions src/core/qgseditformconfig.cpp
Expand Up @@ -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 void QgsEditFormConfig::writeXml( QDomNode& node ) const
Expand Down
5 changes: 0 additions & 5 deletions src/core/qgseditformconfig.h
Expand Up @@ -328,11 +328,6 @@ class CORE_EXPORT QgsEditFormConfig
*/ */
explicit QgsEditFormConfig(); explicit QgsEditFormConfig();


/**
* Parse the XML for the config of one editor widget.
*/
static QgsEditorWidgetConfig parseEditorWidgetConfig( const QDomElement& cfgElem );

private: private:


/** /**
Expand Down
9 changes: 7 additions & 2 deletions src/core/qgseditorwidgetsetup.h
Expand Up @@ -30,7 +30,11 @@ class CORE_EXPORT QgsEditorWidgetSetup
/** /**
* Constructor * Constructor
*/ */
QgsEditorWidgetSetup( const QString& type, const QgsEditorWidgetConfig& config ) : mType( type ), mConfig( config ) {} QgsEditorWidgetSetup( const QString& type, const QgsEditorWidgetConfig& config )
: mType( type )
, mConfig( config )
{}

QgsEditorWidgetSetup() {} QgsEditorWidgetSetup() {}


/** /**
Expand All @@ -47,9 +51,10 @@ class CORE_EXPORT QgsEditorWidgetSetup
* @return true if there is no widget configured. * @return true if there is no widget configured.
*/ */
bool isNull() const { return mType.isEmpty(); } bool isNull() const { return mType.isEmpty(); }

private: private:
QString mType; QString mType;
QgsEditorWidgetConfig mConfig; QgsEditorWidgetConfig mConfig;
}; };


#endif // QGSEDITORWIDGETSETUP_H #endif // QGSEDITORWIDGETSETUP_H
2 changes: 1 addition & 1 deletion src/core/qgsfield.cpp
Expand Up @@ -292,7 +292,7 @@ void QgsField::setEditorWidgetSetup( const QgsEditorWidgetSetup& v )
d->editorWidgetSetup = v; d->editorWidgetSetup = v;
} }


const QgsEditorWidgetSetup& QgsField::editorWidgetSetup() const QgsEditorWidgetSetup QgsField::editorWidgetSetup() const
{ {
return d->editorWidgetSetup; return d->editorWidgetSetup;
} }
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsfield.h
Expand Up @@ -269,7 +269,7 @@ class CORE_EXPORT QgsField
* *
* @return the value * @return the value
*/ */
const QgsEditorWidgetSetup& editorWidgetSetup() const; QgsEditorWidgetSetup editorWidgetSetup() const;


private: private:


Expand Down
74 changes: 49 additions & 25 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -82,6 +82,7 @@
#include "qgssimplifymethod.h" #include "qgssimplifymethod.h"
#include "qgsexpressioncontext.h" #include "qgsexpressioncontext.h"
#include "qgsfeedback.h" #include "qgsfeedback.h"
#include "qgsconfigurationmap.h"


#include "diagram/qgsdiagram.h" #include "diagram/qgsdiagram.h"


Expand Down Expand Up @@ -1742,20 +1743,20 @@ bool QgsVectorLayer::writeXml( QDomNode & layer_node,
} // bool QgsVectorLayer::writeXml } // bool QgsVectorLayer::writeXml




bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage ) bool QgsVectorLayer::readSymbology( const QDomNode& layerNode, QString& errorMessage )
{ {
if ( !mExpressionFieldBuffer ) if ( !mExpressionFieldBuffer )
mExpressionFieldBuffer = new QgsExpressionFieldBuffer(); mExpressionFieldBuffer = new QgsExpressionFieldBuffer();
mExpressionFieldBuffer->readXml( node ); mExpressionFieldBuffer->readXml( layerNode );


updateFields(); updateFields();


readStyle( node, errorMessage ); readStyle( layerNode, errorMessage );


mDisplayExpression = node.namedItem( QStringLiteral( "previewExpression" ) ).toElement().text(); mDisplayExpression = layerNode.namedItem( QStringLiteral( "previewExpression" ) ).toElement().text();
mMapTipTemplate = node.namedItem( QStringLiteral( "mapTip" ) ).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 // Try to migrate pre QGIS 3.0 display field property
if ( mFields.lookupField( displayField ) < 0 ) if ( mFields.lookupField( displayField ) < 0 )
Expand All @@ -1771,17 +1772,17 @@ bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage
} }


// process the attribute actions // 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() ) if ( !annotationFormNode.isNull() )
{ {
QDomElement e = annotationFormNode.toElement(); QDomElement e = annotationFormNode.toElement();
mAnnotationForm = QgsProject::instance()->readPath( e.text() ); mAnnotationForm = QgsProject::instance()->readPath( e.text() );
} }


mAttributeAliasMap.clear(); mAttributeAliasMap.clear();
QDomNode aliasesNode = node.namedItem( QStringLiteral( "aliases" ) ); QDomNode aliasesNode = layerNode.namedItem( QStringLiteral( "aliases" ) );
if ( !aliasesNode.isNull() ) if ( !aliasesNode.isNull() )
{ {
QDomElement aliasElem; QDomElement aliasElem;
Expand Down Expand Up @@ -1811,7 +1812,7 @@ bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage


//Attributes excluded from WMS and WFS //Attributes excluded from WMS and WFS
mExcludeAttributesWMS.clear(); mExcludeAttributesWMS.clear();
QDomNode excludeWMSNode = node.namedItem( QStringLiteral( "excludeAttributesWMS" ) ); QDomNode excludeWMSNode = layerNode.namedItem( QStringLiteral( "excludeAttributesWMS" ) );
if ( !excludeWMSNode.isNull() ) if ( !excludeWMSNode.isNull() )
{ {
QDomNodeList attributeNodeList = excludeWMSNode.toElement().elementsByTagName( QStringLiteral( "attribute" ) ); QDomNodeList attributeNodeList = excludeWMSNode.toElement().elementsByTagName( QStringLiteral( "attribute" ) );
Expand All @@ -1822,7 +1823,7 @@ bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage
} }


mExcludeAttributesWFS.clear(); mExcludeAttributesWFS.clear();
QDomNode excludeWFSNode = node.namedItem( QStringLiteral( "excludeAttributesWFS" ) ); QDomNode excludeWFSNode = layerNode.namedItem( QStringLiteral( "excludeAttributesWFS" ) );
if ( !excludeWFSNode.isNull() ) if ( !excludeWFSNode.isNull() )
{ {
QDomNodeList attributeNodeList = excludeWFSNode.toElement().elementsByTagName( QStringLiteral( "attribute" ) ); QDomNodeList attributeNodeList = excludeWFSNode.toElement().elementsByTagName( QStringLiteral( "attribute" ) );
Expand All @@ -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();

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


readCustomProperties( node, QStringLiteral( "variable" ) ); mEditFormConfig.readXml( layerNode );

mAttributeTableConfig.readXml( layerNode );

mConditionalStyles->readXml( layerNode );

readCustomProperties( layerNode, QStringLiteral( "variable" ) );


return true; return true;
} }
Expand Down Expand Up @@ -1956,19 +1977,14 @@ bool QgsVectorLayer::writeSymbology( QDomNode& node, QDomDocument& doc, QString&
{ {
( void )writeStyle( node, doc, errorMessage ); ( void )writeStyle( node, doc, errorMessage );


<<<<<<< HEAD QgsFields dataProviderFields = mDataProvider->fields();
// 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
=======
QDomElement fieldConfigurationElement = doc.createElement( QStringLiteral( "fieldConfiguration" ) ); QDomElement fieldConfigurationElement = doc.createElement( QStringLiteral( "fieldConfiguration" ) );


int index = 0; int index = 0;
Q_FOREACH ( const QgsField& field, mFields ) Q_FOREACH ( const QgsField& field, mFields )
{ {

QDomElement fieldElement = doc.createElement( QStringLiteral( "field" ) ); QDomElement fieldElement = doc.createElement( QStringLiteral( "field" ) );
fieldElement.setAttribute( QStringLiteral( "name" ), field.name() ); fieldElement.setAttribute( QStringLiteral( "name" ), field.name() );


Expand All @@ -1980,7 +1996,6 @@ bool QgsVectorLayer::writeSymbology( QDomNode& node, QDomDocument& doc, QString&
QDomElement editWidgetElement = doc.createElement( QStringLiteral( "editWidget" ) ); QDomElement editWidgetElement = doc.createElement( QStringLiteral( "editWidget" ) );
fieldElement.appendChild( editWidgetElement ); fieldElement.appendChild( editWidgetElement );
editWidgetElement.setAttribute( "type", field.editorWidgetSetup().type() ); editWidgetElement.setAttribute( "type", field.editorWidgetSetup().type() );

QDomElement editWidgetConfigElement = doc.createElement( QStringLiteral( "config" ) ); QDomElement editWidgetConfigElement = doc.createElement( QStringLiteral( "config" ) );


QgsConfigurationMap( widgetSetup.config() ).toXml( editWidgetConfigElement ); QgsConfigurationMap( widgetSetup.config() ).toXml( editWidgetConfigElement );
Expand All @@ -1989,7 +2004,6 @@ bool QgsVectorLayer::writeSymbology( QDomNode& node, QDomDocument& doc, QString&


++index; ++index;
} }
>>>>>>> dc0c72f... Fixup


QDomElement afField = doc.createElement( QStringLiteral( "annotationform" ) ); QDomElement afField = doc.createElement( QStringLiteral( "annotationform" ) );
QDomText afText = doc.createTextNode( QgsProject::instance()->writePath( mAnnotationForm ) ); QDomText afText = doc.createTextNode( QgsProject::instance()->writePath( mAnnotationForm ) );
Expand Down Expand Up @@ -3056,6 +3070,16 @@ void QgsVectorLayer::updateFields()
mFields[ index ].setConstraints( constraints ); 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 ) if ( oldFields != mFields )
{ {
emit updatedFields(); emit updatedFields();
Expand Down
4 changes: 3 additions & 1 deletion src/core/qgsvectorlayer.h
Expand Up @@ -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 * @param errorMessage reference to string that will be updated with any error messages
* @return true in case of success. * @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. /** Read the style for the current layer from the Dom node supplied.
* @param node node that will contain the style definition for this layer. * @param node node that will contain the style definition for this layer.
Expand Down Expand Up @@ -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. //! Map which stores expression constraints for fields. Value is a pair of expression/description.
QMap< QString, QPair< QString, QString > > mFieldConstraintExpressions; QMap< QString, QPair< QString, QString > > mFieldConstraintExpressions;


QMap< QString, QgsEditorWidgetSetup > mFieldWidgetSetups;

//! Holds the configuration for the edit form //! Holds the configuration for the edit form
QgsEditFormConfig mEditFormConfig; QgsEditFormConfig mEditFormConfig;


Expand Down
2 changes: 1 addition & 1 deletion src/gui/editorwidgets/core/qgseditorwidgetregistry.cpp
Expand Up @@ -80,7 +80,7 @@ void QgsEditorWidgetRegistry::initEditors( QgsMapCanvas *mapCanvas, QgsMessageBa


QgsEditorWidgetRegistry::QgsEditorWidgetRegistry() 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( writeMapLayer( QgsMapLayer*, QDomElement&, QDomDocument& ) ), this, SLOT( writeMapLayer( QgsMapLayer*, QDomElement&, QDomDocument& ) ) );


connect( QgsProject::instance(), SIGNAL( layerWillBeRemoved( QgsMapLayer* ) ), this, SLOT( mapLayerWillBeRemoved( QgsMapLayer* ) ) ); connect( QgsProject::instance(), SIGNAL( layerWillBeRemoved( QgsMapLayer* ) ), this, SLOT( mapLayerWillBeRemoved( QgsMapLayer* ) ) );
Expand Down

0 comments on commit f15284e

Please sign in to comment.