Skip to content

Commit f15284e

Browse files
committed
Save and load new-style widget configuration
1 parent 98f75a4 commit f15284e

9 files changed

+62
-77
lines changed

python/core/qgseditformconfig.sip

-5
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,4 @@ class QgsEditFormConfig
287287
* Deserialize drag and drop designer elements.
288288
*/
289289
QgsAttributeEditorElement* attributeEditorElementFromDomElement( QDomElement &elem, QgsAttributeEditorElement* parent );
290-
291-
/**
292-
* Parse the XML for the config of one editor widget.
293-
*/
294-
static QgsEditorWidgetConfig parseEditorWidgetConfig( const QDomElement& cfgElem );
295290
};

src/core/qgseditformconfig.cpp

-36
Original file line numberDiff line numberDiff line change
@@ -355,42 +355,6 @@ void QgsEditFormConfig::readXml( const QDomNode& node )
355355
}
356356
}
357357
}
358-
359-
360-
QDomElement widgetsElem = node.namedItem( QStringLiteral( "widgets" ) ).toElement();
361-
362-
QDomNodeList widgetConfigsElems = widgetsElem.childNodes();
363-
364-
for ( int i = 0; i < widgetConfigsElems.size(); ++i )
365-
{
366-
const QDomElement wdgElem = widgetConfigsElems.at( i ).toElement();
367-
const QDomElement cfgElem = wdgElem.namedItem( QStringLiteral( "config" ) ).toElement();
368-
const QgsEditorWidgetConfig widgetConfig = parseEditorWidgetConfig( cfgElem );
369-
setWidgetConfig( wdgElem.attribute( QStringLiteral( "name" ) ), widgetConfig );
370-
}
371-
}
372-
373-
QgsEditorWidgetConfig QgsEditFormConfig::parseEditorWidgetConfig( const QDomElement& cfgElem )
374-
{
375-
QgsEditorWidgetConfig cfg;
376-
//// TODO: MAKE THIS MORE GENERIC, SO INDIVIDUALL WIDGETS CAN NOT ONLY SAVE STRINGS
377-
/// SEE QgsEditorWidgetFactory::writeConfig
378-
for ( int j = 0; j < cfgElem.attributes().size(); ++j )
379-
{
380-
const QDomAttr attr = cfgElem.attributes().item( j ).toAttr();
381-
cfg.insert( attr.name(), attr.value() );
382-
}
383-
384-
const QDomNodeList optionElements = cfgElem.elementsByTagName( QStringLiteral( "option" ) );
385-
for ( int j = 0; j < optionElements.size(); ++j )
386-
{
387-
const QDomElement option = optionElements.at( j ).toElement();
388-
const QString key = option.attribute( QStringLiteral( "key" ) );
389-
const QString value = option.attribute( QStringLiteral( "value" ) );
390-
cfg.insert( key, value );
391-
}
392-
//// END TODO
393-
return cfg;
394358
}
395359

396360
void QgsEditFormConfig::writeXml( QDomNode& node ) const

src/core/qgseditformconfig.h

-5
Original file line numberDiff line numberDiff line change
@@ -328,11 +328,6 @@ class CORE_EXPORT QgsEditFormConfig
328328
*/
329329
explicit QgsEditFormConfig();
330330

331-
/**
332-
* Parse the XML for the config of one editor widget.
333-
*/
334-
static QgsEditorWidgetConfig parseEditorWidgetConfig( const QDomElement& cfgElem );
335-
336331
private:
337332

338333
/**

src/core/qgseditorwidgetsetup.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ class CORE_EXPORT QgsEditorWidgetSetup
3030
/**
3131
* Constructor
3232
*/
33-
QgsEditorWidgetSetup( const QString& type, const QgsEditorWidgetConfig& config ) : mType( type ), mConfig( config ) {}
33+
QgsEditorWidgetSetup( const QString& type, const QgsEditorWidgetConfig& config )
34+
: mType( type )
35+
, mConfig( config )
36+
{}
37+
3438
QgsEditorWidgetSetup() {}
3539

3640
/**
@@ -47,9 +51,10 @@ class CORE_EXPORT QgsEditorWidgetSetup
4751
* @return true if there is no widget configured.
4852
*/
4953
bool isNull() const { return mType.isEmpty(); }
54+
5055
private:
5156
QString mType;
5257
QgsEditorWidgetConfig mConfig;
5358
};
5459

55-
#endif // QGSEDITORWIDGETSETUP_H
60+
#endif // QGSEDITORWIDGETSETUP_H

src/core/qgsfield.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ void QgsField::setEditorWidgetSetup( const QgsEditorWidgetSetup& v )
292292
d->editorWidgetSetup = v;
293293
}
294294

295-
const QgsEditorWidgetSetup& QgsField::editorWidgetSetup() const
295+
QgsEditorWidgetSetup QgsField::editorWidgetSetup() const
296296
{
297297
return d->editorWidgetSetup;
298298
}

src/core/qgsfield.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ class CORE_EXPORT QgsField
269269
*
270270
* @return the value
271271
*/
272-
const QgsEditorWidgetSetup& editorWidgetSetup() const;
272+
QgsEditorWidgetSetup editorWidgetSetup() const;
273273

274274
private:
275275

src/core/qgsvectorlayer.cpp

+49-25
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
#include "qgssimplifymethod.h"
8383
#include "qgsexpressioncontext.h"
8484
#include "qgsfeedback.h"
85+
#include "qgsconfigurationmap.h"
8586

8687
#include "diagram/qgsdiagram.h"
8788

@@ -1742,20 +1743,20 @@ bool QgsVectorLayer::writeXml( QDomNode & layer_node,
17421743
} // bool QgsVectorLayer::writeXml
17431744

17441745

1745-
bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage )
1746+
bool QgsVectorLayer::readSymbology( const QDomNode& layerNode, QString& errorMessage )
17461747
{
17471748
if ( !mExpressionFieldBuffer )
17481749
mExpressionFieldBuffer = new QgsExpressionFieldBuffer();
1749-
mExpressionFieldBuffer->readXml( node );
1750+
mExpressionFieldBuffer->readXml( layerNode );
17501751

17511752
updateFields();
17521753

1753-
readStyle( node, errorMessage );
1754+
readStyle( layerNode, errorMessage );
17541755

1755-
mDisplayExpression = node.namedItem( QStringLiteral( "previewExpression" ) ).toElement().text();
1756-
mMapTipTemplate = node.namedItem( QStringLiteral( "mapTip" ) ).toElement().text();
1756+
mDisplayExpression = layerNode.namedItem( QStringLiteral( "previewExpression" ) ).toElement().text();
1757+
mMapTipTemplate = layerNode.namedItem( QStringLiteral( "mapTip" ) ).toElement().text();
17571758

1758-
QString displayField = node.namedItem( QStringLiteral( "displayfield" ) ).toElement().text();
1759+
QString displayField = layerNode.namedItem( QStringLiteral( "displayfield" ) ).toElement().text();
17591760

17601761
// Try to migrate pre QGIS 3.0 display field property
17611762
if ( mFields.lookupField( displayField ) < 0 )
@@ -1771,17 +1772,17 @@ bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage
17711772
}
17721773

17731774
// process the attribute actions
1774-
mActions->readXml( node );
1775+
mActions->readXml( layerNode );
17751776

1776-
QDomNode annotationFormNode = node.namedItem( QStringLiteral( "annotationform" ) );
1777+
QDomNode annotationFormNode = layerNode.namedItem( QStringLiteral( "annotationform" ) );
17771778
if ( !annotationFormNode.isNull() )
17781779
{
17791780
QDomElement e = annotationFormNode.toElement();
17801781
mAnnotationForm = QgsProject::instance()->readPath( e.text() );
17811782
}
17821783

17831784
mAttributeAliasMap.clear();
1784-
QDomNode aliasesNode = node.namedItem( QStringLiteral( "aliases" ) );
1785+
QDomNode aliasesNode = layerNode.namedItem( QStringLiteral( "aliases" ) );
17851786
if ( !aliasesNode.isNull() )
17861787
{
17871788
QDomElement aliasElem;
@@ -1811,7 +1812,7 @@ bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage
18111812

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

18241825
mExcludeAttributesWFS.clear();
1825-
QDomNode excludeWFSNode = node.namedItem( QStringLiteral( "excludeAttributesWFS" ) );
1826+
QDomNode excludeWFSNode = layerNode.namedItem( QStringLiteral( "excludeAttributesWFS" ) );
18261827
if ( !excludeWFSNode.isNull() )
18271828
{
18281829
QDomNodeList attributeNodeList = excludeWFSNode.toElement().elementsByTagName( QStringLiteral( "attribute" ) );
@@ -1832,13 +1833,33 @@ bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage
18321833
}
18331834
}
18341835

1835-
mEditFormConfig.readXml( node );
1836+
// Load editor widget configuration
1837+
QDomElement widgetsElem = layerNode.namedItem( QStringLiteral( "fieldConfiguration" ) ).toElement();
18361838

1837-
mAttributeTableConfig.readXml( node );
1839+
QDomNodeList fieldConfigurationElementList = widgetsElem.elementsByTagName( QStringLiteral( "field" ) );
18381840

1839-
mConditionalStyles->readXml( node );
1841+
for ( int i = 0; i < fieldConfigurationElementList.size(); ++i )
1842+
{
1843+
const QDomElement fieldConfigElement = fieldConfigurationElementList.at( i ).toElement();
1844+
const QDomElement fieldWidgetElement = fieldConfigElement.elementsByTagName( QStringLiteral( "editWidget" ) ).at( 0 ).toElement();
1845+
1846+
QString fieldName = fieldConfigElement.attribute( QStringLiteral( "name" ) );
1847+
1848+
const QString widgetType = fieldWidgetElement.attribute( QStringLiteral( "type" ) );
1849+
const QDomElement cfgElem = fieldConfigElement.elementsByTagName( QStringLiteral( "config" ) ).at( 0 ).toElement();
1850+
QgsConfigurationMap editWidgetConfiguration;
1851+
editWidgetConfiguration.fromXml( cfgElem );
1852+
QgsEditorWidgetSetup setup = QgsEditorWidgetSetup( widgetType, editWidgetConfiguration.get() );
1853+
mFieldWidgetSetups[fieldName] = setup;
1854+
}
18401855

1841-
readCustomProperties( node, QStringLiteral( "variable" ) );
1856+
mEditFormConfig.readXml( layerNode );
1857+
1858+
mAttributeTableConfig.readXml( layerNode );
1859+
1860+
mConditionalStyles->readXml( layerNode );
1861+
1862+
readCustomProperties( layerNode, QStringLiteral( "variable" ) );
18421863

18431864
return true;
18441865
}
@@ -1956,19 +1977,14 @@ bool QgsVectorLayer::writeSymbology( QDomNode& node, QDomDocument& doc, QString&
19561977
{
19571978
( void )writeStyle( node, doc, errorMessage );
19581979

1959-
<<<<<<< HEAD
1960-
// FIXME
1961-
// edittypes are written to the layerNode
1962-
// by slot QgsEditorWidgetRegistry::writeMapLayer()
1963-
// triggered by signal QgsProject::writeMapLayer()
1964-
// still other editing settings are written here,
1965-
// although they are not part of symbology either
1966-
=======
1980+
QgsFields dataProviderFields = mDataProvider->fields();
1981+
19671982
QDomElement fieldConfigurationElement = doc.createElement( QStringLiteral( "fieldConfiguration" ) );
19681983

19691984
int index = 0;
19701985
Q_FOREACH ( const QgsField& field, mFields )
19711986
{
1987+
19721988
QDomElement fieldElement = doc.createElement( QStringLiteral( "field" ) );
19731989
fieldElement.setAttribute( QStringLiteral( "name" ), field.name() );
19741990

@@ -1980,7 +1996,6 @@ bool QgsVectorLayer::writeSymbology( QDomNode& node, QDomDocument& doc, QString&
19801996
QDomElement editWidgetElement = doc.createElement( QStringLiteral( "editWidget" ) );
19811997
fieldElement.appendChild( editWidgetElement );
19821998
editWidgetElement.setAttribute( "type", field.editorWidgetSetup().type() );
1983-
19841999
QDomElement editWidgetConfigElement = doc.createElement( QStringLiteral( "config" ) );
19852000

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

19902005
++index;
19912006
}
1992-
>>>>>>> dc0c72f... Fixup
19932007

19942008
QDomElement afField = doc.createElement( QStringLiteral( "annotationform" ) );
19952009
QDomText afText = doc.createTextNode( QgsProject::instance()->writePath( mAnnotationForm ) );
@@ -3056,6 +3070,16 @@ void QgsVectorLayer::updateFields()
30563070
mFields[ index ].setConstraints( constraints );
30573071
}
30583072

3073+
auto fieldWidgetIterator = mFieldWidgetSetups.constBegin();
3074+
for ( ; fieldWidgetIterator != mFieldWidgetSetups.constEnd(); ++ fieldWidgetIterator )
3075+
{
3076+
int index = mFields.indexOf( fieldWidgetIterator.key() );
3077+
if ( index < 0 )
3078+
continue;
3079+
3080+
mFields[index].setEditorWidgetSetup( fieldWidgetIterator.value() );
3081+
}
3082+
30593083
if ( oldFields != mFields )
30603084
{
30613085
emit updatedFields();

src/core/qgsvectorlayer.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
799799
* @param errorMessage reference to string that will be updated with any error messages
800800
* @return true in case of success.
801801
*/
802-
bool readSymbology( const QDomNode& node, QString& errorMessage ) override;
802+
bool readSymbology( const QDomNode& layerNode, QString& errorMessage ) override;
803803

804804
/** Read the style for the current layer from the Dom node supplied.
805805
* @param node node that will contain the style definition for this layer.
@@ -2016,6 +2016,8 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
20162016
//! Map which stores expression constraints for fields. Value is a pair of expression/description.
20172017
QMap< QString, QPair< QString, QString > > mFieldConstraintExpressions;
20182018

2019+
QMap< QString, QgsEditorWidgetSetup > mFieldWidgetSetups;
2020+
20192021
//! Holds the configuration for the edit form
20202022
QgsEditFormConfig mEditFormConfig;
20212023

src/gui/editorwidgets/core/qgseditorwidgetregistry.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void QgsEditorWidgetRegistry::initEditors( QgsMapCanvas *mapCanvas, QgsMessageBa
8080

8181
QgsEditorWidgetRegistry::QgsEditorWidgetRegistry()
8282
{
83-
connect( QgsProject::instance(), SIGNAL( readMapLayer( QgsMapLayer*, const QDomElement& ) ), this, SLOT( readMapLayer( QgsMapLayer*, const QDomElement& ) ) );
83+
// connect( QgsProject::instance(), SIGNAL( readMapLayer( QgsMapLayer*, const QDomElement& ) ), this, SLOT( readMapLayer( QgsMapLayer*, const QDomElement& ) ) );
8484
// connect( QgsProject::instance(), SIGNAL( writeMapLayer( QgsMapLayer*, QDomElement&, QDomDocument& ) ), this, SLOT( writeMapLayer( QgsMapLayer*, QDomElement&, QDomDocument& ) ) );
8585

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

0 commit comments

Comments
 (0)