Skip to content

Commit

Permalink
Better reading/writting of editor widget config.
Browse files Browse the repository at this point in the history
Avoid saving config parameters that are not set and support not
having them in the XML.
  • Loading branch information
pvalsecc authored and m-kuhn committed Nov 7, 2016
1 parent 6d8990b commit 91e4b24
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 138 deletions.
12 changes: 12 additions & 0 deletions python/gui/editorwidgets/core/qgseditorwidgetfactory.sip
Expand Up @@ -189,4 +189,16 @@ class QgsEditorWidgetFactory
* @see supportsField( QgsVectorLayer* vl, fieldIdx )
*/
virtual unsigned int fieldScore( const QgsVectorLayer* vl, int fieldIdx ) const;

protected:

/**
* Copy the given config element to a XML attribute.
*/
static void config2xml( const QgsEditorWidgetConfig& config, QDomElement& configElement, const QString& fieldName );

/**
* Copy the given XML attribute to the configuration element.
*/
static void xml2config( const QDomElement& configElement, QgsEditorWidgetConfig& config, const QString& fieldName );
};
24 changes: 24 additions & 0 deletions src/gui/editorwidgets/core/qgseditorwidgetfactory.cpp
Expand Up @@ -123,3 +123,27 @@ unsigned int QgsEditorWidgetFactory::fieldScore( const QgsVectorLayer* vl, int f
return 5;
}

void QgsEditorWidgetFactory::config2xml( const QgsEditorWidgetConfig& config, QDomElement& configElement, const QString& fieldName )
{
const QVariant value = config.value( fieldName );
if ( value.isValid() )
{
if ( value.type() == QVariant::Bool )
{
configElement.setAttribute( fieldName, value.toBool() ? "1" : "0" );
}
else
{
configElement.setAttribute( fieldName, value.toString() );
}
}
}

void QgsEditorWidgetFactory::xml2config( const QDomElement& configElement, QgsEditorWidgetConfig& config, const QString& fieldName )
{
const QString value = configElement.attribute( fieldName );
if ( !value.isNull() )
{
config.insert( fieldName, value );
}
}
12 changes: 12 additions & 0 deletions src/gui/editorwidgets/core/qgseditorwidgetfactory.h
Expand Up @@ -207,6 +207,18 @@ class GUI_EXPORT QgsEditorWidgetFactory
*/
virtual unsigned int fieldScore( const QgsVectorLayer* vl, int fieldIdx ) const;

protected:

/**
* Copy the given config element to a XML attribute.
*/
static void config2xml( const QgsEditorWidgetConfig& config, QDomElement& configElement, const QString& fieldName );

/**
* Copy the given XML attribute to the configuration element.
*/
static void xml2config( const QDomElement& configElement, QgsEditorWidgetConfig& config, const QString& fieldName );

private:
QString mName;
};
Expand Down
8 changes: 4 additions & 4 deletions src/gui/editorwidgets/qgscheckboxwidgetfactory.cpp
Expand Up @@ -45,8 +45,8 @@ QgsEditorWidgetConfig QgsCheckboxWidgetFactory::readConfig( const QDomElement& c

QgsEditorWidgetConfig cfg;

cfg.insert( QStringLiteral( "CheckedState" ), configElement.attribute( QStringLiteral( "CheckedState" ) ) );
cfg.insert( QStringLiteral( "UncheckedState" ), configElement.attribute( QStringLiteral( "UncheckedState" ) ) );
xml2config( configElement, cfg, QStringLiteral( "CheckedState" ) );
xml2config( configElement, cfg, QStringLiteral( "UncheckedState" ) );

return cfg;
}
Expand All @@ -57,8 +57,8 @@ void QgsCheckboxWidgetFactory::writeConfig( const QgsEditorWidgetConfig& config,
Q_UNUSED( layer )
Q_UNUSED( fieldIdx )

configElement.setAttribute( QStringLiteral( "CheckedState" ), config.value( QStringLiteral( "CheckedState" ), "1" ).toString() );
configElement.setAttribute( QStringLiteral( "UncheckedState" ), config.value( QStringLiteral( "UncheckedState" ), "0" ).toString() );
config2xml( config, configElement, QStringLiteral( "CheckedState" ) );
config2xml( config, configElement, QStringLiteral( "UncheckedState" ) );
}

QHash<const char*, int> QgsCheckboxWidgetFactory::supportedWidgetTypes()
Expand Down
16 changes: 8 additions & 8 deletions src/gui/editorwidgets/qgsdatetimeeditfactory.cpp
Expand Up @@ -47,10 +47,10 @@ QgsEditorWidgetConfig QgsDateTimeEditFactory::readConfig( const QDomElement& con
Q_UNUSED( fieldIdx );
QgsEditorWidgetConfig cfg;

cfg.insert( QStringLiteral( "field_format" ), configElement.attribute( QStringLiteral( "field_format" ) ) );
cfg.insert( QStringLiteral( "display_format" ), configElement.attribute( QStringLiteral( "display_format" ) ) );
cfg.insert( QStringLiteral( "calendar_popup" ), configElement.attribute( QStringLiteral( "calendar_popup" ) ) == QLatin1String( "1" ) );
cfg.insert( QStringLiteral( "allow_null" ), configElement.attribute( QStringLiteral( "allow_null" ) ) == QLatin1String( "1" ) );
xml2config( configElement, cfg, QStringLiteral( "field_format" ) );
xml2config( configElement, cfg, QStringLiteral( "display_format" ) );
xml2config( configElement, cfg, QStringLiteral( "calendar_popup" ) );
xml2config( configElement, cfg, QStringLiteral( "allow_null" ) );

return cfg;
}
Expand All @@ -61,10 +61,10 @@ void QgsDateTimeEditFactory::writeConfig( const QgsEditorWidgetConfig& config, Q
Q_UNUSED( layer );
Q_UNUSED( fieldIdx );

configElement.setAttribute( QStringLiteral( "field_format" ), config[QStringLiteral( "field_format" )].toString() );
configElement.setAttribute( QStringLiteral( "display_format" ), config[QStringLiteral( "display_format" )].toString() );
configElement.setAttribute( QStringLiteral( "calendar_popup" ), config[QStringLiteral( "calendar_popup" )].toBool() );
configElement.setAttribute( QStringLiteral( "allow_null" ), config[QStringLiteral( "allow_null" )].toBool() );
config2xml( config, configElement, QStringLiteral( "field_format" ) );
config2xml( config, configElement, QStringLiteral( "display_format" ) );
config2xml( config, configElement, QStringLiteral( "calendar_popup" ) );
config2xml( config, configElement, QStringLiteral( "allow_null" ) );
}

QString QgsDateTimeEditFactory::representValue( QgsVectorLayer* vl, int fieldIdx, const QgsEditorWidgetConfig& config, const QVariant& cache, const QVariant& value ) const
Expand Down
83 changes: 22 additions & 61 deletions src/gui/editorwidgets/qgsexternalresourcewidgetfactory.cpp
Expand Up @@ -40,36 +40,17 @@ void QgsExternalResourceWidgetFactory::writeConfig( const QgsEditorWidgetConfig&
Q_UNUSED( layer )
Q_UNUSED( fieldIdx )

configElement.setAttribute( QStringLiteral( "FileWidget" ), config.value( QStringLiteral( "FileWidget" ), true ).toBool() );
configElement.setAttribute( QStringLiteral( "FileWidgetButton" ), config.value( QStringLiteral( "FileWidgetButton" ), true ).toBool() );


// Non mandatory options are not saved into project file (to save some space).
if ( config.contains( QStringLiteral( "UseLink" ) ) )
configElement.setAttribute( QStringLiteral( "UseLink" ), config.value( QStringLiteral( "UseLink" ) ).toBool() );

if ( config.contains( QStringLiteral( "FullUrl" ) ) )
configElement.setAttribute( QStringLiteral( "FullUrl" ), config.value( QStringLiteral( "FullUrl" ) ).toBool() );

if ( config.contains( QStringLiteral( "DefaultRoot" ) ) )
configElement.setAttribute( QStringLiteral( "DefaultRoot" ), config.value( QStringLiteral( "DefaultRoot" ) ).toString() );

if ( config.contains( QStringLiteral( "RelativeStorage" ) ) )
configElement.setAttribute( QStringLiteral( "RelativeStorage" ) , config.value( QStringLiteral( "RelativeStorage" ) ).toString() );

if ( config.contains( QStringLiteral( "DocumentViewer" ) ) )
configElement.setAttribute( QStringLiteral( "DocumentViewer" ), config.value( QStringLiteral( "DocumentViewer" ) ).toInt() );

if ( config.contains( QStringLiteral( "DocumentViewerWidth" ) ) )
configElement.setAttribute( QStringLiteral( "DocumentViewerWidth" ), config.value( QStringLiteral( "DocumentViewerWidth" ) ).toInt() );

if ( config.contains( QStringLiteral( "DocumentViewerHeight" ) ) )
configElement.setAttribute( QStringLiteral( "DocumentViewerHeight" ), config.value( QStringLiteral( "DocumentViewerHeight" ) ).toInt() );

if ( config.contains( QStringLiteral( "FileWidgetFilter" ) ) )
configElement.setAttribute( QStringLiteral( "FileWidgetFilter" ), config.value( QStringLiteral( "FileWidgetFilter" ) ).toString() );

configElement.setAttribute( QStringLiteral( "StorageMode" ), config.value( QStringLiteral( "StorageMode" ) ).toString() );
config2xml( config, configElement, QStringLiteral( "FileWidget" ) );
config2xml( config, configElement, QStringLiteral( "FileWidgetButton" ) );
config2xml( config, configElement, QStringLiteral( "UseLink" ) );
config2xml( config, configElement, QStringLiteral( "FullUrl" ) );
config2xml( config, configElement, QStringLiteral( "DefaultRoot" ) );
config2xml( config, configElement, QStringLiteral( "RelativeStorage" ) );
config2xml( config, configElement, QStringLiteral( "DocumentViewer" ) );
config2xml( config, configElement, QStringLiteral( "DocumentViewerWidth" ) );
config2xml( config, configElement, QStringLiteral( "DocumentViewerHeight" ) );
config2xml( config, configElement, QStringLiteral( "FileWidgetFilter" ) );
config2xml( config, configElement, QStringLiteral( "StorageMode" ) );
}

QgsEditorWidgetConfig QgsExternalResourceWidgetFactory::readConfig( const QDomElement& configElement, QgsVectorLayer* layer, int fieldIdx )
Expand All @@ -79,42 +60,22 @@ QgsEditorWidgetConfig QgsExternalResourceWidgetFactory::readConfig( const QDomEl

QgsEditorWidgetConfig cfg;

if ( configElement.hasAttribute( QStringLiteral( "FileWidgetButton" ) ) )
cfg.insert( QStringLiteral( "FileWidgetButton" ), configElement.attribute( QStringLiteral( "FileWidgetButton" ) ) == QLatin1String( "1" ) );

if ( configElement.hasAttribute( QStringLiteral( "FileWidget" ) ) )
cfg.insert( QStringLiteral( "FileWidget" ), configElement.attribute( QStringLiteral( "FileWidget" ) ) == QLatin1String( "1" ) );

if ( configElement.hasAttribute( QStringLiteral( "UseLink" ) ) )
cfg.insert( QStringLiteral( "UseLink" ), configElement.attribute( QStringLiteral( "UseLink" ) ) == QLatin1String( "1" ) );

if ( configElement.hasAttribute( QStringLiteral( "FullUrl" ) ) )
cfg.insert( QStringLiteral( "FullUrl" ), configElement.attribute( QStringLiteral( "FullUrl" ) ) == QLatin1String( "1" ) );

if ( configElement.hasAttribute( QStringLiteral( "DefaultRoot" ) ) )
cfg.insert( QStringLiteral( "DefaultRoot" ), configElement.attribute( QStringLiteral( "DefaultRoot" ) ) );

xml2config( configElement, cfg, QStringLiteral( "FileWidget" ) );
xml2config( configElement, cfg, QStringLiteral( "FileWidgetButton" ) );
xml2config( configElement, cfg, QStringLiteral( "UseLink" ) );
xml2config( configElement, cfg, QStringLiteral( "FullUrl" ) );
xml2config( configElement, cfg, QStringLiteral( "DefaultRoot" ) );
if ( configElement.hasAttribute( QStringLiteral( "RelativeStorage" ) ) )
{
if (( configElement.attribute( QStringLiteral( "RelativeStorage" ) ).toInt() == QgsFileWidget::RelativeDefaultPath && configElement.hasAttribute( QStringLiteral( "DefaultRoot" ) ) ) ||
configElement.attribute( QStringLiteral( "RelativeStorage" ) ).toInt() == QgsFileWidget::RelativeProject )
cfg.insert( QStringLiteral( "RelativeStorage" ) , configElement.attribute( QStringLiteral( "RelativeStorage" ) ).toInt() );
xml2config( configElement, cfg, QStringLiteral( "RelativeStorage" ) );
}

if ( configElement.hasAttribute( QStringLiteral( "DocumentViewer" ) ) )
cfg.insert( QStringLiteral( "DocumentViewer" ), configElement.attribute( QStringLiteral( "DocumentViewer" ) ) );

if ( configElement.hasAttribute( QStringLiteral( "DocumentViewerWidth" ) ) )
cfg.insert( QStringLiteral( "DocumentViewerWidth" ), configElement.attribute( QStringLiteral( "DocumentViewerWidth" ) ) );

if ( configElement.hasAttribute( QStringLiteral( "DocumentViewerHeight" ) ) )
cfg.insert( QStringLiteral( "DocumentViewerHeight" ), configElement.attribute( QStringLiteral( "DocumentViewerHeight" ) ) );

if ( configElement.hasAttribute( QStringLiteral( "FileWidgetFilter" ) ) )
cfg.insert( QStringLiteral( "FileWidgetFilter" ), configElement.attribute( QStringLiteral( "FileWidgetFilter" ) ) );


cfg.insert( QStringLiteral( "StorageMode" ), configElement.attribute( QStringLiteral( "StorageMode" ), QStringLiteral( "Files" ) ) );
xml2config( configElement, cfg, QStringLiteral( "DocumentViewer" ) );
xml2config( configElement, cfg, QStringLiteral( "DocumentViewerWidth" ) );
xml2config( configElement, cfg, QStringLiteral( "DocumentViewerHeight" ) );
xml2config( configElement, cfg, QStringLiteral( "FileWidgetFilter" ) );
xml2config( configElement, cfg, QStringLiteral( "StorageMode" ) );

return cfg;
}
Expand Down
8 changes: 4 additions & 4 deletions src/gui/editorwidgets/qgsphotowidgetfactory.cpp
Expand Up @@ -41,8 +41,8 @@ QgsEditorWidgetConfig QgsPhotoWidgetFactory::readConfig( const QDomElement& conf

QgsEditorWidgetConfig cfg;

cfg.insert( QStringLiteral( "Height" ), configElement.attribute( QStringLiteral( "Height" ), 0 ).toInt() );
cfg.insert( QStringLiteral( "Width" ), configElement.attribute( QStringLiteral( "Width" ), 0 ).toInt() );
xml2config( configElement, cfg, QStringLiteral( "Height" ) );
xml2config( configElement, cfg, QStringLiteral( "Width" ) );

return cfg;
}
Expand All @@ -53,6 +53,6 @@ void QgsPhotoWidgetFactory::writeConfig( const QgsEditorWidgetConfig& config, QD
Q_UNUSED( layer )
Q_UNUSED( fieldIdx )

configElement.setAttribute( QStringLiteral( "Height" ), config.value( QStringLiteral( "Height" ), 0 ).toString() );
configElement.setAttribute( QStringLiteral( "Width" ), config.value( QStringLiteral( "Width" ), 0 ).toString() );
config2xml( config, configElement, QStringLiteral( "Height" ) );
config2xml( config, configElement, QStringLiteral( "Width" ) );
}
31 changes: 12 additions & 19 deletions src/gui/editorwidgets/qgsrangewidgetfactory.cpp
Expand Up @@ -40,16 +40,12 @@ QgsEditorWidgetConfig QgsRangeWidgetFactory::readConfig( const QDomElement& conf
Q_UNUSED( fieldIdx );
QgsEditorWidgetConfig cfg;

cfg.insert( QStringLiteral( "Style" ), configElement.attribute( QStringLiteral( "Style" ) ) );
cfg.insert( QStringLiteral( "Min" ), configElement.attribute( QStringLiteral( "Min" ) ) );
cfg.insert( QStringLiteral( "Max" ), configElement.attribute( QStringLiteral( "Max" ) ) );
cfg.insert( QStringLiteral( "Step" ), configElement.attribute( QStringLiteral( "Step" ) ) );
cfg.insert( QStringLiteral( "AllowNull" ), configElement.attribute( QStringLiteral( "AllowNull" ) ) == QLatin1String( "1" ) );

if ( configElement.hasAttribute( QStringLiteral( "Suffix" ) ) )
{
cfg.insert( QStringLiteral( "Suffix" ), configElement.attribute( QStringLiteral( "Suffix" ) ) );
}
xml2config( configElement, cfg, QStringLiteral( "Style" ) );
xml2config( configElement, cfg, QStringLiteral( "Min" ) );
xml2config( configElement, cfg, QStringLiteral( "Max" ) );
xml2config( configElement, cfg, QStringLiteral( "Step" ) );
xml2config( configElement, cfg, QStringLiteral( "AllowNull" ) );
xml2config( configElement, cfg, QStringLiteral( "Suffix" ) );

return cfg;
}
Expand All @@ -60,15 +56,12 @@ void QgsRangeWidgetFactory::writeConfig( const QgsEditorWidgetConfig& config, QD
Q_UNUSED( layer );
Q_UNUSED( fieldIdx );

configElement.setAttribute( QStringLiteral( "Style" ), config[QStringLiteral( "Style" )].toString() );
configElement.setAttribute( QStringLiteral( "Min" ), config[QStringLiteral( "Min" )].toString() );
configElement.setAttribute( QStringLiteral( "Max" ), config[QStringLiteral( "Max" )].toString() );
configElement.setAttribute( QStringLiteral( "Step" ), config[QStringLiteral( "Step" )].toString() );
configElement.setAttribute( QStringLiteral( "AllowNull" ), config[QStringLiteral( "AllowNull" )].toBool() );
if ( config.contains( QStringLiteral( "Suffix" ) ) )
{
configElement.setAttribute( QStringLiteral( "Suffix" ), config[QStringLiteral( "Suffix" )].toString() );
}
config2xml( config, configElement, QStringLiteral( "Style" ) );
config2xml( config, configElement, QStringLiteral( "Min" ) );
config2xml( config, configElement, QStringLiteral( "Max" ) );
config2xml( config, configElement, QStringLiteral( "Step" ) );
config2xml( config, configElement, QStringLiteral( "AllowNull" ) );
config2xml( config, configElement, QStringLiteral( "Suffix" ) );
}

unsigned int QgsRangeWidgetFactory::fieldScore( const QgsVectorLayer* vl, int fieldIdx ) const
Expand Down
32 changes: 16 additions & 16 deletions src/gui/editorwidgets/qgsrelationreferencefactory.cpp
Expand Up @@ -53,13 +53,13 @@ QgsEditorWidgetConfig QgsRelationReferenceFactory::readConfig( const QDomElement
Q_UNUSED( fieldIdx );
QgsEditorWidgetConfig cfg;

cfg.insert( QStringLiteral( "AllowNULL" ), configElement.attribute( QStringLiteral( "AllowNULL" ) ) == QLatin1String( "1" ) );
cfg.insert( QStringLiteral( "OrderByValue" ), configElement.attribute( QStringLiteral( "OrderByValue" ) ) == QLatin1String( "1" ) );
cfg.insert( QStringLiteral( "ShowForm" ), configElement.attribute( QStringLiteral( "ShowForm" ) ) == QLatin1String( "1" ) );
cfg.insert( QStringLiteral( "Relation" ), configElement.attribute( QStringLiteral( "Relation" ) ) );
cfg.insert( QStringLiteral( "MapIdentification" ), configElement.attribute( QStringLiteral( "MapIdentification" ) ) == QLatin1String( "1" ) );
cfg.insert( QStringLiteral( "ReadOnly" ), configElement.attribute( QStringLiteral( "ReadOnly" ) ) == QLatin1String( "1" ) );
cfg.insert( QStringLiteral( "AllowAddFeatures" ), configElement.attribute( QStringLiteral( "AllowAddFeatures" ) ) == QLatin1String( "1" ) );
xml2config( configElement, cfg, QStringLiteral( "AllowNULL" ) );
xml2config( configElement, cfg, QStringLiteral( "OrderByValue" ) );
xml2config( configElement, cfg, QStringLiteral( "ShowForm" ) );
xml2config( configElement, cfg, QStringLiteral( "Relation" ) );
xml2config( configElement, cfg, QStringLiteral( "MapIdentification" ) );
xml2config( configElement, cfg, QStringLiteral( "ReadOnly" ) );
xml2config( configElement, cfg, QStringLiteral( "AllowAddFeatures" ) );

QDomNode filterNode = configElement.elementsByTagName( QStringLiteral( "FilterFields" ) ).at( 0 );
if ( !filterNode.isNull() )
Expand All @@ -74,7 +74,7 @@ QgsEditorWidgetConfig QgsRelationReferenceFactory::readConfig( const QDomElement
}
cfg.insert( QStringLiteral( "FilterFields" ), filterFields );

cfg.insert( QStringLiteral( "ChainFilters" ), filterNode.toElement().attribute( QStringLiteral( "ChainFilters" ) ) == QLatin1String( "1" ) );
xml2config( configElement, cfg , QStringLiteral( "ChainFilters" ) );
}
return cfg;
}
Expand All @@ -85,13 +85,13 @@ void QgsRelationReferenceFactory::writeConfig( const QgsEditorWidgetConfig& conf
Q_UNUSED( layer );
Q_UNUSED( fieldIdx );

configElement.setAttribute( QStringLiteral( "AllowNULL" ), config[QStringLiteral( "AllowNULL" )].toBool() );
configElement.setAttribute( QStringLiteral( "OrderByValue" ), config[QStringLiteral( "OrderByValue" )].toBool() );
configElement.setAttribute( QStringLiteral( "ShowForm" ), config[QStringLiteral( "ShowForm" )].toBool() );
configElement.setAttribute( QStringLiteral( "Relation" ), config[QStringLiteral( "Relation" )].toString() );
configElement.setAttribute( QStringLiteral( "MapIdentification" ), config[QStringLiteral( "MapIdentification" )].toBool() );
configElement.setAttribute( QStringLiteral( "ReadOnly" ), config[QStringLiteral( "ReadOnly" )].toBool() );
configElement.setAttribute( QStringLiteral( "AllowAddFeatures" ), config[QStringLiteral( "AllowAddFeatures" )].toBool() );
config2xml( config, configElement, QStringLiteral( "AllowNULL" ) );
config2xml( config, configElement, QStringLiteral( "OrderByValue" ) );
config2xml( config, configElement, QStringLiteral( "ShowForm" ) );
config2xml( config, configElement, QStringLiteral( "Relation" ) );
config2xml( config, configElement, QStringLiteral( "MapIdentification" ) );
config2xml( config, configElement, QStringLiteral( "ReadOnly" ) );
config2xml( config, configElement, QStringLiteral( "AllowAddFeatures" ) );

if ( config.contains( QStringLiteral( "FilterFields" ) ) )
{
Expand All @@ -105,7 +105,7 @@ void QgsRelationReferenceFactory::writeConfig( const QgsEditorWidgetConfig& conf
}
configElement.appendChild( filterFields );

filterFields.setAttribute( QStringLiteral( "ChainFilters" ), config[QStringLiteral( "ChainFilters" )].toBool() );
config2xml( config, configElement, QStringLiteral( "ChainFilters" ) );
}
}

Expand Down

0 comments on commit 91e4b24

Please sign in to comment.