Skip to content
Permalink
Browse files

Remove QgsEditFormConfig::widgetType

  • Loading branch information
m-kuhn committed Dec 20, 2016
1 parent 930ce54 commit f294201e7b446b693508f3baff115bd9481802b9
@@ -806,7 +806,8 @@ QgsEditFormConfig {#qgis_api_break_3_0_QgsEditFormConfig}
- Does no longer inherit QObject
- widgetType() and widgetConfig() now reflect only the user configured values.
QgsEditorWidgetRegistry::instance()->findBest() must be used instead.
- widgetType(), widgetConfig(), setWidgetType(), setWidgetConfig() and removeWidgetConfig() now only take a string as first parameter. Access by index has been removed.
- widgetConfig(), setWidgetType(), setWidgetConfig() and removeWidgetConfig() now only take a string as first parameter. Access by index has been removed.
- widgetType() has been removed in favor of QgsVectorLayer::editorWidgetSetup()
- expression(), setExpression(), expressionDescription() and setExpressionDescription()
have been removed. Use QgsVectorLayer.setConstraintExpression()/constraintExpression(),
or QgsField.constraintExpression()/QgsField.constraintDescription() instead.
@@ -108,88 +108,10 @@ class QgsEditFormConfig
* EditorLayout::GeneratedLayout.
*/
void setUiForm( const QString& ui );
bool setWidgetConfig( const QString& widgetName, const QVariantMap& config );
QVariantMap widgetConfig( const QString& widgetName ) const;
bool removeWidgetConfig( const QString& widgetName );


// Widget stuff

/**
* Set the editor widget type for a field
*
* QGIS ships the following widget types, additional types may be available depending
* on plugins.
*
* <ul>
* <li>CheckBox (QgsCheckboxWidgetWrapper)</li>
* <li>Classification (QgsClassificationWidgetWrapper)</li>
* <li>Color (QgsColorWidgetWrapper)</li>
* <li>DateTime (QgsDateTimeEditWrapper)</li>
* <li>Enumeration (QgsEnumerationWidgetWrapper)</li>
* <li>FileName (QgsFileNameWidgetWrapper)</li>
* <li>Hidden (QgsHiddenWidgetWrapper)</li>
* <li>Photo (QgsPhotoWidgetWrapper)</li>
* <li>Range (QgsRangeWidgetWrapper)</li>
* <li>RelationReference (QgsRelationReferenceWidgetWrapper)</li>
* <li>TextEdit (QgsTextEditWrapper)</li>
* <li>UniqueValues (QgsUniqueValuesWidgetWrapper)</li>
* <li>UuidGenerator (QgsUuidWidgetWrapper)</li>
* <li>ValueMap (QgsValueMapWidgetWrapper)</li>
* <li>ValueRelation (QgsValueRelationWidgetWrapper)</li>
* <li>WebView (QgsWebViewWidgetWrapper)</li>
* </ul>
*
* @param fieldName The name of the field
* @param widgetType Type id of the editor widget to use
*/
void setWidgetType( const QString& widgetName, const QString& widgetType );

/**
* Get the id for the editor widget used to represent the field at the given index
*
* @param fieldName The name of the field
*
* @return The id for the editor widget or a NULL string if not applicable
*/
QString widgetType( const QString& fieldName ) const;

/**
* Set the editor widget config for a widget.
*
* Example:
* \code{.py}
* layer.setWidgetConfig( 'relation_id', { 'nm-rel': 'other_relation' } )
* \endcode
*
* @param fieldName The name of the field to configure
* @param config The config to set for this field
*
* @see setWidgetType() for a list of widgets and choose the widget to see the available options.
*
* @note not available in python bindings
*/
void setWidgetConfig( const QString& fieldName, const QVariantMap& config );

/**
* Get the configuration for the editor widget used to represent the field with the given name
*
* @param fieldName The name of the field.
*
* @return The configuration for the editor widget or an empty config if the field does not exist
*/
QVariantMap widgetConfig( const QString& fieldName ) const;

/**
* Remove the configuration for the editor widget used to represent the field with the given name
*
* @param fieldName The name of the field.
*
* @return true if successful, false if the field does not exist
*/
bool removeWidgetConfig( const QString& fieldName );

/**
* This returns true if the field is manually set to read only or if the field
* does not support editing like joins or virtual fields.
*/
bool readOnly( int idx ) const;

/**
@@ -992,9 +992,7 @@ void QgsFieldsProperties::apply()
editFormConfig.setReadOnly( i, !cfg.mEditable );
editFormConfig.setLabelOnTop( i, cfg.mLabelOnTop );
mLayer->setConstraintExpression( i, cfg.mConstraint, cfg.mConstraintDescription );

editFormConfig.setWidgetType( name, cfg.mEditorWidgetType );
editFormConfig.setWidgetConfig( name, cfg.mEditorWidgetConfig );
mLayer->setEditorWidgetSetup( idx, QgsEditorWidgetSetup( cfg.mEditorWidgetType, cfg.mEditorWidgetConfig ) );

if ( cfg.mConstraints & QgsFieldConstraints::ConstraintNotNull )
{
@@ -16,6 +16,7 @@
#include "qgseditformconfig.h"
#include "qgsproject.h"
#include "qgsrelationmanager.h"
#include "qgslogger.h"

//#include "qgseditorwidgetregistry.h"

@@ -29,14 +30,13 @@ QgsEditFormConfig::QgsEditFormConfig()
{
}

QString QgsEditFormConfig::widgetType( const QString& fieldName ) const
QVariantMap QgsEditFormConfig::widgetConfig( const QString& widgetName ) const
{
return d->mEditorWidgetTypes.value( fieldName );
}

QVariantMap QgsEditFormConfig::widgetConfig( const QString& fieldName ) const
{
return d->mWidgetConfigs.value( fieldName );
int fieldIndex = d->mFields.indexOf( widgetName );
if ( fieldIndex != -1 )
return d->mFields.at( fieldIndex ).editorWidgetSetup().config();
else
return d->mWidgetConfigs.value( widgetName );
}

void QgsEditFormConfig::setFields( const QgsFields& fields )
@@ -69,15 +69,17 @@ void QgsEditFormConfig::onRelationsLoaded()
}
}

void QgsEditFormConfig::setWidgetType( const QString& widgetName, const QString& widgetType )
bool QgsEditFormConfig::setWidgetConfig( const QString& widgetName, const QVariantMap& config )
{
d->mEditorWidgetTypes[widgetName] = widgetType;
}
if ( d->mFields.indexOf( widgetName ) != -1 )
{
QgsDebugMsg( "Trying to set a widget config for a field on QgsEditFormConfig. Use layer->setEditorWidgetSetup() instead." );
return false;
}

void QgsEditFormConfig::setWidgetConfig( const QString& widgetName, const QVariantMap& config )
{
d.detach();
d->mWidgetConfigs[widgetName] = config;
return true;
}

bool QgsEditFormConfig::removeWidgetConfig( const QString& widgetName )
@@ -141,82 +141,40 @@ class CORE_EXPORT QgsEditFormConfig
*/
void setUiForm( const QString& ui );


// Widget stuff

/**
* Set the editor widget type for a field
*
* QGIS ships the following widget types, additional types may be available depending
* on plugins.
*
* <ul>
* <li>CheckBox (QgsCheckboxWidgetWrapper)</li>
* <li>Classification (QgsClassificationWidgetWrapper)</li>
* <li>Color (QgsColorWidgetWrapper)</li>
* <li>DateTime (QgsDateTimeEditWrapper)</li>
* <li>Enumeration (QgsEnumerationWidgetWrapper)</li>
* <li>FileName (QgsFileNameWidgetWrapper)</li>
* <li>Hidden (QgsHiddenWidgetWrapper)</li>
* <li>Photo (QgsPhotoWidgetWrapper)</li>
* <li>Range (QgsRangeWidgetWrapper)</li>
* <li>RelationReference (QgsRelationReferenceWidgetWrapper)</li>
* <li>TextEdit (QgsTextEditWrapper)</li>
* <li>UniqueValues (QgsUniqueValuesWidgetWrapper)</li>
* <li>UuidGenerator (QgsUuidWidgetWrapper)</li>
* <li>ValueMap (QgsValueMapWidgetWrapper)</li>
* <li>ValueRelation (QgsValueRelationWidgetWrapper)</li>
* <li>WebView (QgsWebViewWidgetWrapper)</li>
* </ul>
*
* @param fieldName The name of the field
* @param widgetType Type id of the editor widget to use
*/
void setWidgetType( const QString& fieldName, const QString& widgetType );

/**
* Get the id for the editor widget used to represent the field at the given index
* Don't use this directly. Prefere the use of QgsEditorWidgetRegistry::instance()->findBestType.
*
* @param fieldName The name of the field
*
* @return The id for the editor widget or a NULL string if not applicable
*/
QString widgetType( const QString& fieldName ) const;

/**
* Set the editor widget config for a widget.
* Set the editor widget config for a widget which is not for a simple field.
*
* Example:
* \code{.py}
* layer.setWidgetConfig( 'relation_id', { 'nm-rel': 'other_relation' } )
* editFormConfig = layer.editFormConfig()
* editFormConfig.setWidgetConfig( 'relation_id', { 'nm-rel': 'other_relation' } )
* layer.setEditFormConfig(editFormConfig)
* \endcode
*
* @param fieldName The name of the field to configure
* @param config The config to set for this field
*
* @see setWidgetType() for a list of widgets and choose the widget to see the available options.
* @param widgetName The name of the widget to configure
* @param config The config to set for this widget
* @returns false if a field exists with the provided widgetName. In this case
* QgsVectorLayer::setEditorWidgetSetup should be used.
*
* @note not available in python bindings
* @see QgsVectorLayer::setEditorWidgetSetup() for field configurations.
*/
void setWidgetConfig( const QString& fieldName, const QVariantMap& config );
bool setWidgetConfig( const QString& widgetName, const QVariantMap& config );

/**
* Get the configuration for the editor widget used to represent the field with the given name
* Don't use this directly. Prefere the use of QgsEditorWidgetRegistry::instance()->findBestConfig.
* Get the configuration for the editor widget with the given name.
*
* @param fieldName The name of the field.
* @param widgetName The name of the widget.
*
* @return The configuration for the editor widget or an empty config if the field does not exist
*/
QVariantMap widgetConfig( const QString& fieldName ) const;

/**
* Remove the configuration for the editor widget used to represent the field with the given name
* Remove the configuration for the editor widget with the given name
*
* @param fieldName The name of the widget.
* @param widgetName The name of the widget.
*
* @return true if successful, false if the field does not exist
* @return true if a configuration has been removed
*/
bool removeWidgetConfig( const QString& fieldName );

@@ -4567,3 +4567,24 @@ void QgsVectorLayer::setConstraintExpression( int index, const QString& expressi
}
updateFields();
}

void QgsVectorLayer::setEditorWidgetSetup( int index, const QgsEditorWidgetSetup& setup )
{
if ( index < 0 || index >= mFields.count() )
return;

if ( setup.isNull() )
mFieldWidgetSetups.remove( mFields.at( index ).name() );
else
mFieldWidgetSetups.insert( mFields.at( index ).name(), setup );
updateFields();
}

QgsEditorWidgetSetup QgsVectorLayer::editorWidgetSetup( int index ) const
{

if ( index < 0 || index >= mFields.count() )
return QgsEditorWidgetSetup();

return mFields.at( index ).editorWidgetSetup();
}
@@ -1454,6 +1454,9 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
*/
void setConstraintExpression( int index, const QString& expression, const QString& description = QString() );

void setEditorWidgetSetup( int index, const QgsEditorWidgetSetup& setup );
QgsEditorWidgetSetup editorWidgetSetup( int index ) const;

/** Calculates a list of unique values contained within an attribute in the layer. Note that
* in some circumstances when unsaved changes are present for the layer then the returned list
* may contain outdated values (for instance when the attribute value in a saved feature has
@@ -125,7 +125,6 @@ bool QgsVectorLayerEditPassthrough::deleteAttribute( int attr )
if ( L->dataProvider()->deleteAttributes( QgsAttributeIds() << attr ) )
{
mModified = true;
L->editFormConfig().removeWidgetConfig( L->fields().at( attr ).name() );
emit attributeDeleted( attr );
mModified = true;
return true;
@@ -345,7 +345,6 @@ QgsVectorLayerUndoCommandDeleteAttribute::QgsVectorLayerUndoCommandDeleteAttribu
mOriginIndex = fields.fieldOriginIndex( mFieldIndex );
mProviderField = ( origin == QgsFields::OriginProvider );
mFieldName = fields.field( mFieldIndex ).name();
mOldEditorWidgetConfig = mBuffer->L->editFormConfig().widgetConfig( mFieldName );

if ( !mProviderField )
{
@@ -412,7 +411,6 @@ void QgsVectorLayerUndoCommandDeleteAttribute::undo()
}

QgsEditFormConfig formConfig = mBuffer->L->editFormConfig();
formConfig.setWidgetConfig( mFieldName, mOldEditorWidgetConfig );
mBuffer->L->setEditFormConfig( formConfig );

emit mBuffer->attributeAdded( mFieldIndex );
@@ -431,7 +429,6 @@ void QgsVectorLayerUndoCommandDeleteAttribute::redo()
mBuffer->mAddedAttributes.removeAt( mOriginIndex ); // removing temporary attribute
}

mBuffer->L->editFormConfig().removeWidgetConfig( mFieldName );
mBuffer->handleAttributeDeleted( mFieldIndex ); // update changed attributes + new features
mBuffer->updateLayerFields();
emit mBuffer->attributeDeleted( mFieldIndex );
@@ -89,10 +89,10 @@ QgsEditorWidgetRegistry::~QgsEditorWidgetRegistry()

QgsEditorWidgetSetup QgsEditorWidgetRegistry::findBest( const QgsVectorLayer* vl, const QString& fieldName ) const
{
const QString fromConfig = vl->editFormConfig().widgetType( fieldName );
if ( !fromConfig.isNull() )
QgsEditorWidgetSetup setup = vl->fields().field( fieldName ).editorWidgetSetup();
if ( !setup.isNull() )
{
return QgsEditorWidgetSetup( fromConfig, vl->editFormConfig().widgetConfig( fieldName ) );
return setup;
}
return mAutoConf.editorWidgetSetup( vl, fieldName );
}
@@ -53,7 +53,7 @@ unsigned int QgsDateTimeEditFactory::fieldScore( const QgsVectorLayer* vl, int f
{
const QgsField field = vl->fields().field( fieldIdx );
const QVariant::Type type = field.type();
const QVariantMap config = vl->editFormConfig().widgetConfig( field.name() );
const QVariantMap config = field.editorWidgetSetup().config();
if ( type == QVariant::DateTime || type == QVariant::Date || type == QVariant::Time || config.contains( QStringLiteral( "field_format" ) ) )
{
return 20;
@@ -28,6 +28,7 @@
#include "qgseditorwidgetregistry.h"
#include "qgslayertreegroup.h"
#include "qgslogger.h"
#include "qgseditorwidgetsetup.h"

#include <QDomDocument>
#include <QFileInfo>
@@ -827,7 +828,7 @@ void QgsServerProjectParser::addLayerProjectSettings( QDomElement& layerElem, QD
}

//edit type to text
attributeElem.setAttribute( QStringLiteral( "editType" ), vLayer->editFormConfig().widgetType( field.name() ) );
attributeElem.setAttribute( QStringLiteral( "editType" ), vLayer->editorWidgetSetup( idx ).type() );
attributeElem.setAttribute( QStringLiteral( "comment" ), field.comment() );
attributeElem.setAttribute( QStringLiteral( "length" ), field.length() );
attributeElem.setAttribute( QStringLiteral( "precision" ), field.precision() );
@@ -1581,10 +1582,10 @@ void QgsServerProjectParser::addValueRelationLayersForLayer( const QgsVectorLaye
for ( int idx = 0; idx < vl->pendingFields().size(); idx++ )
{
const QString name = vl->pendingFields().field( idx ).name();
if ( vl->editFormConfig().widgetType( name ) != QLatin1String( "ValueRelation" ) )
if ( vl->editorWidgetSetup( idx ).type() != QLatin1String( "ValueRelation" ) )
continue;

QVariantMap cfg( vl->editFormConfig().widgetConfig( name ) );
QVariantMap cfg( vl->editorWidgetSetup( idx ).config() );
if ( !cfg.contains( QStringLiteral( "Layer" ) ) )
continue;

@@ -75,9 +75,7 @@ void TestQgsVectorLayerSaveAsDialog::testAttributesAsDisplayedValues()
QVERIFY( tempLayer->isValid() );

// Set a widget
QgsEditFormConfig editFormConfig = tempLayer->editFormConfig();
editFormConfig.setWidgetType( 0, QStringLiteral( "ValueRelation" ) );
tempLayer->setEditFormConfig( editFormConfig );
tempLayer->setEditorWidgetSetup( 0, QgsEditorWidgetSetup( QStringLiteral( "ValueRelation" ), QVariantMap() ) );

QgsVectorLayerSaveAsDialog d( tempLayer.data() );

0 comments on commit f294201

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