Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Move storage of field alias and default value to QgsField
This is a partial implementation - QMaps are still used internally
within QgsVectorLayer to track the alias/default values
between attribute edit operations.

Sponsored by DB Fahrwegdienste GmbH
  • Loading branch information
nyalldawson committed Aug 30, 2016
1 parent 9940586 commit 97d7b73
Show file tree
Hide file tree
Showing 18 changed files with 320 additions and 89 deletions.
43 changes: 42 additions & 1 deletion python/core/qgsfield.sip
Expand Up @@ -42,9 +42,20 @@ class QgsField
bool operator==( const QgsField& other ) const;
bool operator!=( const QgsField& other ) const;

//! Gets the name of the field
/** Returns the name of the field.
* @see setName()
* @see displayName()
*/
QString name() const;

/** Returns the name to use when displaying this field. This will be the
* field alias if set, otherwise the field name.
* @see name()
* @see alias()
* @note added in QGIS 3.0
*/
QString displayName() const;

//! Gets variant type of the field as it will be retrieved from data source
QVariant::Type type() const;

Expand Down Expand Up @@ -115,6 +126,36 @@ class QgsField
*/
void setComment( const QString& comment );

/** Returns the expression used when calculating the default value for the field.
* @returns expression evaluated when calculating default values for field, or an
* empty string if no default is set
* @note added in QGIS 3.0
* @see setDefaultValueExpression()
*/
QString defaultValueExpression() const;

/** Sets an expression to use when calculating the default value for the field.
* @param expression expression to evaluate when calculating default values for field. Pass
* an empty expression to clear the default.
* @note added in QGIS 3.0
* @see defaultValueExpression()
*/
void setDefaultValueExpression( const QString& expression );

/** Returns the alias for the field (the friendly displayed name of the field ),
* or an empty string if there is no alias.
* @see setAlias()
* @note added in QGIS 3.0
*/
QString alias() const;

/** Sets the alias for the field (the friendly displayed name of the field ).
* @param alias field alias, or empty string to remove an existing alias
* @see alias()
* @note added in QGIS 3.0
*/
void setAlias( const QString& alias );

/** Formats string for display*/
QString displayString( const QVariant& v ) const;

Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsfieldsproperties.cpp
Expand Up @@ -342,7 +342,7 @@ void QgsFieldsProperties::setRow( int row, int idx, const QgsField& field )
setConfigForRow( row, cfg );

//set the alias for the attribute
mFieldsList->setItem( row, attrAliasCol, new QTableWidgetItem( mLayer->attributeAlias( idx ) ) );
mFieldsList->setItem( row, attrAliasCol, new QTableWidgetItem( field.alias() ) );

//published WMS/WFS attributes
QTableWidgetItem* wmsAttrItem = new QTableWidgetItem();
Expand Down
6 changes: 3 additions & 3 deletions src/core/qgseditformconfig.cpp
Expand Up @@ -53,18 +53,18 @@ QgsEditorWidgetConfig QgsEditFormConfig::widgetConfig( const QString& widgetName
return d->mWidgetConfigs.value( widgetName );
}

void QgsEditFormConfig::setFields( const QgsFields& fields, const QMap<QString, QString>& attributeAliasMap )
void QgsEditFormConfig::setFields( const QgsFields& fields )
{
d.detach();
d->mFields = fields;
d->mAttributeAliasMap = attributeAliasMap;

if ( !d->mConfiguredRootContainer )
{
d->mInvisibleRootContainer->clear();
for ( int i = 0; i < d->mFields.size(); ++i )
{
QgsAttributeEditorField* field = new QgsAttributeEditorField( d->mAttributeAliasMap.value( d->mFields.at( i ).name(), d->mFields.at( i ).name() ), i, d->mInvisibleRootContainer );
QgsAttributeEditorField* field = new QgsAttributeEditorField( !d->mFields.at( i ).alias().isEmpty() ? d->mFields.at( i ).alias()
: d->mFields.at( i ).name(), i, d->mInvisibleRootContainer );
d->mInvisibleRootContainer->addChildElement( field );
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgseditformconfig.h
Expand Up @@ -417,7 +417,7 @@ class CORE_EXPORT QgsEditFormConfig
* Used internally to set the fields when they change.
* This should only be called from QgsVectorLayer for synchronization reasons
*/
void setFields( const QgsFields& fields, const QMap<QString, QString>& attributeAliasMap );
void setFields( const QgsFields& fields );

/**
* Will be called by friend class QgsVectorLayer
Expand Down
2 changes: 0 additions & 2 deletions src/core/qgseditformconfig_p.h
Expand Up @@ -90,8 +90,6 @@ class QgsEditFormConfigPrivate : public QSharedData
QgsEditFormConfig::FeatureFormSuppress mSuppressForm;

QgsFields mFields;
QMap<QString, QString> mAttributeAliasMap;

};

/// @endcond
Expand Down
36 changes: 34 additions & 2 deletions src/core/qgsfield.cpp
Expand Up @@ -86,6 +86,14 @@ QString QgsField::name() const
return d->name;
}

QString QgsField::displayName() const
{
if ( !d->alias.isEmpty() )
return d->alias;
else
return d->name;
}

QVariant::Type QgsField::type() const
{
return d->type;
Expand Down Expand Up @@ -151,6 +159,26 @@ void QgsField::setComment( const QString& comment )
d->comment = comment;
}

QString QgsField::defaultValueExpression() const
{
return d->defaultValueExpression;
}

void QgsField::setDefaultValueExpression( const QString& expression )
{
d->defaultValueExpression = expression;
}

QString QgsField::alias() const
{
return d->alias;
}

void QgsField::setAlias( const QString& alias )
{
d->alias = alias;
}

/***************************************************************************
* This class is considered CRITICAL and any change MUST be accompanied with
* full unit tests in testqgsfield.cpp.
Expand Down Expand Up @@ -252,20 +280,24 @@ QDataStream& operator<<( QDataStream& out, const QgsField& field )
out << field.length();
out << field.precision();
out << field.comment();
out << field.alias();
out << field.defaultValueExpression();
return out;
}

QDataStream& operator>>( QDataStream& in, QgsField& field )
{
quint32 type, length, precision;
QString name, typeName, comment;
in >> name >> type >> typeName >> length >> precision >> comment;
QString name, typeName, comment, alias, defaultValueExpression;
in >> name >> type >> typeName >> length >> precision >> comment >> alias >> defaultValueExpression;
field.setName( name );
field.setType( static_cast< QVariant::Type >( type ) );
field.setTypeName( typeName );
field.setLength( static_cast< int >( length ) );
field.setPrecision( static_cast< int >( precision ) );
field.setComment( comment );
field.setAlias( alias );
field.setDefaultValueExpression( defaultValueExpression );
return in;
}

Expand Down
53 changes: 48 additions & 5 deletions src/core/qgsfield.h
Expand Up @@ -46,10 +46,12 @@ class CORE_EXPORT QgsField
Q_GADGET

Q_PROPERTY( bool isNumeric READ isNumeric )
Q_PROPERTY( int length READ length )
Q_PROPERTY( int precision READ precision )
Q_PROPERTY( QString comment READ comment )
Q_PROPERTY( QString name READ name )
Q_PROPERTY( int length READ length WRITE setLength )
Q_PROPERTY( int precision READ precision WRITE setPrecision )
Q_PROPERTY( QString comment READ comment WRITE setComment )
Q_PROPERTY( QString name READ name WRITE setName )
Q_PROPERTY( QString alias READ alias WRITE setAlias )
Q_PROPERTY( QString defaultValueExpression READ defaultValueExpression WRITE setDefaultValueExpression )

public:
/** Constructor. Constructs a new QgsField object.
Expand Down Expand Up @@ -84,9 +86,20 @@ class CORE_EXPORT QgsField
bool operator==( const QgsField& other ) const;
bool operator!=( const QgsField& other ) const;

//! Gets the name of the field
/** Returns the name of the field.
* @see setName()
* @see displayName()
*/
QString name() const;

/** Returns the name to use when displaying this field. This will be the
* field alias if set, otherwise the field name.
* @see name()
* @see alias()
* @note added in QGIS 3.0
*/
QString displayName() const;

//! Gets variant type of the field as it will be retrieved from data source
QVariant::Type type() const;

Expand Down Expand Up @@ -157,6 +170,36 @@ class CORE_EXPORT QgsField
*/
void setComment( const QString& comment );

/** Returns the expression used when calculating the default value for the field.
* @returns expression evaluated when calculating default values for field, or an
* empty string if no default is set
* @note added in QGIS 3.0
* @see setDefaultValueExpression()
*/
QString defaultValueExpression() const;

/** Sets an expression to use when calculating the default value for the field.
* @param expression expression to evaluate when calculating default values for field. Pass
* an empty expression to clear the default.
* @note added in QGIS 3.0
* @see defaultValueExpression()
*/
void setDefaultValueExpression( const QString& expression );

/** Returns the alias for the field (the friendly displayed name of the field ),
* or an empty string if there is no alias.
* @see setAlias()
* @note added in QGIS 3.0
*/
QString alias() const;

/** Sets the alias for the field (the friendly displayed name of the field ).
* @param alias field alias, or empty string to remove an existing alias
* @see alias()
* @note added in QGIS 3.0
*/
void setAlias( const QString& alias );

/** Formats string for display*/
QString displayString( const QVariant& v ) const;

Expand Down
11 changes: 10 additions & 1 deletion src/core/qgsfield_p.h
Expand Up @@ -65,6 +65,8 @@ class QgsFieldPrivate : public QSharedData
, length( other.length )
, precision( other.precision )
, comment( other.comment )
, alias( other.alias )
, defaultValueExpression( other.defaultValueExpression )
{
}

Expand All @@ -73,7 +75,8 @@ class QgsFieldPrivate : public QSharedData
bool operator==( const QgsFieldPrivate& other ) const
{
return (( name == other.name ) && ( type == other.type )
&& ( length == other.length ) && ( precision == other.precision ) );
&& ( length == other.length ) && ( precision == other.precision )
&& ( alias == other.alias ) && ( defaultValueExpression == other.defaultValueExpression ) );
}

//! Name
Expand All @@ -93,6 +96,12 @@ class QgsFieldPrivate : public QSharedData

//! Comment
QString comment;

//! Alias for field name (friendly name shown to users)
QString alias;

//! Default value expression
QString defaultValueExpression;
};


Expand Down

0 comments on commit 97d7b73

Please sign in to comment.