Skip to content
Permalink
Browse files

QgsField::ConfigurationFlags: add read/save in core

  • Loading branch information
3nids committed Sep 11, 2020
1 parent 63d8ee7 commit 59ff51697663c41a79d34cfa2989f3aa00c57ac6
@@ -2292,6 +2292,8 @@ can also be set. Setting an empty expression will clear any existing expression
.. versionadded:: 3.0
%End



void setEditorWidgetSetup( int index, const QgsEditorWidgetSetup &setup );
%Docstring
\copydoc editorWidgetSetup
@@ -75,11 +75,13 @@ class CORE_EXPORT QgsField
*/
enum class ConfigurationFlag : int
{
None = 0, //!< No flag is defined
Searchable = 0x1, //!< Defines if the field is searchable (used in the locator search for instance)
DefaultFlags = Searchable, //!< Default set of flags for a field
};
Q_ENUM( ConfigurationFlag )
Q_DECLARE_FLAGS( ConfigurationFlags, ConfigurationFlag )
Q_FLAG( ConfigurationFlags )
#endif

/**
@@ -431,6 +433,8 @@ class CORE_EXPORT QgsField

Q_DECLARE_METATYPE( QgsField )

Q_DECLARE_OPERATORS_FOR_FLAGS( QgsField::ConfigurationFlags ) SIP_SKIP

//! Writes the field to stream out. QGIS version compatibility is not guaranteed.
CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsField &field );
//! Reads a field from stream in into field. QGIS version compatibility is not guaranteed.
@@ -313,6 +313,7 @@ QgsVectorLayer *QgsVectorLayer::clone() const
for ( int i = 0; i < fields().count(); i++ )
{
layer->setFieldAlias( i, attributeAlias( i ) );
layer->setFieldConfigurationFlags( i, fieldConfigurationFlags( i ) );
layer->setEditorWidgetSetup( i, editorWidgetSetup( i ) );
layer->setConstraintExpression( i, constraintExpression( i ), constraintDescription( i ) );
layer->setDefaultValueDefinition( i, defaultValueDefinition( i ) );
@@ -2337,6 +2338,7 @@ bool QgsVectorLayer::readSymbology( const QDomNode &layerNode, QString &errorMes
const QDomElement fieldWidgetElement = fieldConfigElement.elementsByTagName( QStringLiteral( "editWidget" ) ).at( 0 ).toElement();

QString fieldName = fieldConfigElement.attribute( QStringLiteral( "name" ) );
mFieldConfigurationFlags[fieldName] = qgsFlagKeysToValue( fieldConfigElement.attribute( QStringLiteral( "configurationFlags" ) ), QgsField::ConfigurationFlag::DefaultFlags );

const QString widgetType = fieldWidgetElement.attribute( QStringLiteral( "type" ) );
const QDomElement cfgElem = fieldConfigElement.elementsByTagName( QStringLiteral( "config" ) ).at( 0 ).toElement();
@@ -2627,6 +2629,7 @@ bool QgsVectorLayer::writeSymbology( QDomNode &node, QDomDocument &doc, QString
{
QDomElement fieldElement = doc.createElement( QStringLiteral( "field" ) );
fieldElement.setAttribute( QStringLiteral( "name" ), field.name() );
fieldElement.setAttribute( QStringLiteral( "configurationFlags" ), qgsFlagValueToKeys( field.configurationFlags() ) );

fieldConfigurationElement.appendChild( fieldElement );

@@ -3774,6 +3777,17 @@ void QgsVectorLayer::updateFields()
mFields[ index ].setAlias( aliasIt.value() );
}

// Update configuration flags
QMap< QString, QgsField::ConfigurationFlags >::const_iterator flagsIt = mFieldConfigurationFlags.constBegin();
for ( ; flagsIt != mFieldConfigurationFlags.constEnd(); ++flagsIt )
{
int index = mFields.lookupField( flagsIt.key() );
if ( index < 0 )
continue;

mFields[index].setConfigurationFlags( flagsIt.value() );
}

// Update default values
mDefaultValueOnUpdateFields.clear();
QMap< QString, QgsDefaultValue >::const_iterator defaultIt = mDefaultExpressionMap.constBegin();
@@ -5468,6 +5482,26 @@ void QgsVectorLayer::setConstraintExpression( int index, const QString &expressi
updateFields();
}

void QgsVectorLayer::setFieldConfigurationFlags( int index, QgsField::ConfigurationFlags flags )
{
if ( index < 0 || index >= mFields.count() )
return;

mFields.at( index ).setConfigurationFlags( flags );

mFieldConfigurationFlags.insert( mFields.at( index ).name(), flags );
updateFields();
}

QgsField::ConfigurationFlags QgsVectorLayer::fieldConfigurationFlags( int index ) const
{

if ( index < 0 || index >= mFields.count() )
return QgsField::ConfigurationFlag::DefaultFlags;

return mFields.at( index ).configurationFlags();
}

void QgsVectorLayer::setEditorWidgetSetup( int index, const QgsEditorWidgetSetup &setup )
{
if ( index < 0 || index >= mFields.count() )
@@ -2115,6 +2115,20 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
*/
void setConstraintExpression( int index, const QString &expression, const QString &description = QString() );

/**
* Sets the configuration flags of the field at given index
* \see QgsField::ConfigurationFlags
* \since QGIS 3.16
*/
void setFieldConfigurationFlags( int index, QgsField::ConfigurationFlags flags ) SIP_SKIP;

/**
* Returns the configuration flags of the field at given index
* \see QgsField::ConfigurationFlags
* \since QGIS 3.16
*/
QgsField::ConfigurationFlags fieldConfigurationFlags( int index ) const SIP_SKIP;

/**
* \copydoc editorWidgetSetup
*/
@@ -2834,6 +2848,7 @@ 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, QgsField::ConfigurationFlags > mFieldConfigurationFlags;
QMap< QString, QgsEditorWidgetSetup > mFieldWidgetSetups;

//! Holds the configuration for the edit form

0 comments on commit 59ff516

Please sign in to comment.
You can’t perform that action at this time.