Skip to content
Permalink
Browse files

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 f32b85e commit 93b124e60cd00c561b8e60193ad416b643a26b0c
@@ -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;

@@ -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;

@@ -343,7 +343,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();
@@ -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;
@@ -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.
@@ -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;
}

@@ -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.
@@ -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;

@@ -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;

@@ -65,6 +65,8 @@ class QgsFieldPrivate : public QSharedData
, length( other.length )
, precision( other.precision )
, comment( other.comment )
, alias( other.alias )
, defaultValueExpression( other.defaultValueExpression )
{
}

@@ -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
@@ -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;
};


0 comments on commit 93b124e

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