Skip to content

Commit 93b124e

Browse files
committed
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
1 parent f32b85e commit 93b124e

15 files changed

+314
-79
lines changed

python/core/qgsfield.sip

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,20 @@ class QgsField
4242
bool operator==( const QgsField& other ) const;
4343
bool operator!=( const QgsField& other ) const;
4444

45-
//! Gets the name of the field
45+
/** Returns the name of the field.
46+
* @see setName()
47+
* @see displayName()
48+
*/
4649
QString name() const;
4750

51+
/** Returns the name to use when displaying this field. This will be the
52+
* field alias if set, otherwise the field name.
53+
* @see name()
54+
* @see alias()
55+
* @note added in QGIS 3.0
56+
*/
57+
QString displayName() const;
58+
4859
//! Gets variant type of the field as it will be retrieved from data source
4960
QVariant::Type type() const;
5061

@@ -115,6 +126,36 @@ class QgsField
115126
*/
116127
void setComment( const QString& comment );
117128

129+
/** Returns the expression used when calculating the default value for the field.
130+
* @returns expression evaluated when calculating default values for field, or an
131+
* empty string if no default is set
132+
* @note added in QGIS 3.0
133+
* @see setDefaultValueExpression()
134+
*/
135+
QString defaultValueExpression() const;
136+
137+
/** Sets an expression to use when calculating the default value for the field.
138+
* @param expression expression to evaluate when calculating default values for field. Pass
139+
* an empty expression to clear the default.
140+
* @note added in QGIS 3.0
141+
* @see defaultValueExpression()
142+
*/
143+
void setDefaultValueExpression( const QString& expression );
144+
145+
/** Returns the alias for the field (the friendly displayed name of the field ),
146+
* or an empty string if there is no alias.
147+
* @see setAlias()
148+
* @note added in QGIS 3.0
149+
*/
150+
QString alias() const;
151+
152+
/** Sets the alias for the field (the friendly displayed name of the field ).
153+
* @param alias field alias, or empty string to remove an existing alias
154+
* @see alias()
155+
* @note added in QGIS 3.0
156+
*/
157+
void setAlias( const QString& alias );
158+
118159
/** Formats string for display*/
119160
QString displayString( const QVariant& v ) const;
120161

src/app/qgsfieldsproperties.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ void QgsFieldsProperties::setRow( int row, int idx, const QgsField& field )
343343
setConfigForRow( row, cfg );
344344

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

348348
//published WMS/WFS attributes
349349
QTableWidgetItem* wmsAttrItem = new QTableWidgetItem();

src/core/qgsfield.cpp

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ QString QgsField::name() const
8686
return d->name;
8787
}
8888

89+
QString QgsField::displayName() const
90+
{
91+
if ( !d->alias.isEmpty() )
92+
return d->alias;
93+
else
94+
return d->name;
95+
}
96+
8997
QVariant::Type QgsField::type() const
9098
{
9199
return d->type;
@@ -151,6 +159,26 @@ void QgsField::setComment( const QString& comment )
151159
d->comment = comment;
152160
}
153161

162+
QString QgsField::defaultValueExpression() const
163+
{
164+
return d->defaultValueExpression;
165+
}
166+
167+
void QgsField::setDefaultValueExpression( const QString& expression )
168+
{
169+
d->defaultValueExpression = expression;
170+
}
171+
172+
QString QgsField::alias() const
173+
{
174+
return d->alias;
175+
}
176+
177+
void QgsField::setAlias( const QString& alias )
178+
{
179+
d->alias = alias;
180+
}
181+
154182
/***************************************************************************
155183
* This class is considered CRITICAL and any change MUST be accompanied with
156184
* full unit tests in testqgsfield.cpp.
@@ -252,20 +280,24 @@ QDataStream& operator<<( QDataStream& out, const QgsField& field )
252280
out << field.length();
253281
out << field.precision();
254282
out << field.comment();
283+
out << field.alias();
284+
out << field.defaultValueExpression();
255285
return out;
256286
}
257287

258288
QDataStream& operator>>( QDataStream& in, QgsField& field )
259289
{
260290
quint32 type, length, precision;
261-
QString name, typeName, comment;
262-
in >> name >> type >> typeName >> length >> precision >> comment;
291+
QString name, typeName, comment, alias, defaultValueExpression;
292+
in >> name >> type >> typeName >> length >> precision >> comment >> alias >> defaultValueExpression;
263293
field.setName( name );
264294
field.setType( static_cast< QVariant::Type >( type ) );
265295
field.setTypeName( typeName );
266296
field.setLength( static_cast< int >( length ) );
267297
field.setPrecision( static_cast< int >( precision ) );
268298
field.setComment( comment );
299+
field.setAlias( alias );
300+
field.setDefaultValueExpression( defaultValueExpression );
269301
return in;
270302
}
271303

src/core/qgsfield.h

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ class CORE_EXPORT QgsField
4646
Q_GADGET
4747

4848
Q_PROPERTY( bool isNumeric READ isNumeric )
49-
Q_PROPERTY( int length READ length )
50-
Q_PROPERTY( int precision READ precision )
51-
Q_PROPERTY( QString comment READ comment )
52-
Q_PROPERTY( QString name READ name )
49+
Q_PROPERTY( int length READ length WRITE setLength )
50+
Q_PROPERTY( int precision READ precision WRITE setPrecision )
51+
Q_PROPERTY( QString comment READ comment WRITE setComment )
52+
Q_PROPERTY( QString name READ name WRITE setName )
53+
Q_PROPERTY( QString alias READ alias WRITE setAlias )
54+
Q_PROPERTY( QString defaultValueExpression READ defaultValueExpression WRITE setDefaultValueExpression )
5355

5456
public:
5557
/** Constructor. Constructs a new QgsField object.
@@ -84,9 +86,20 @@ class CORE_EXPORT QgsField
8486
bool operator==( const QgsField& other ) const;
8587
bool operator!=( const QgsField& other ) const;
8688

87-
//! Gets the name of the field
89+
/** Returns the name of the field.
90+
* @see setName()
91+
* @see displayName()
92+
*/
8893
QString name() const;
8994

95+
/** Returns the name to use when displaying this field. This will be the
96+
* field alias if set, otherwise the field name.
97+
* @see name()
98+
* @see alias()
99+
* @note added in QGIS 3.0
100+
*/
101+
QString displayName() const;
102+
90103
//! Gets variant type of the field as it will be retrieved from data source
91104
QVariant::Type type() const;
92105

@@ -157,6 +170,36 @@ class CORE_EXPORT QgsField
157170
*/
158171
void setComment( const QString& comment );
159172

173+
/** Returns the expression used when calculating the default value for the field.
174+
* @returns expression evaluated when calculating default values for field, or an
175+
* empty string if no default is set
176+
* @note added in QGIS 3.0
177+
* @see setDefaultValueExpression()
178+
*/
179+
QString defaultValueExpression() const;
180+
181+
/** Sets an expression to use when calculating the default value for the field.
182+
* @param expression expression to evaluate when calculating default values for field. Pass
183+
* an empty expression to clear the default.
184+
* @note added in QGIS 3.0
185+
* @see defaultValueExpression()
186+
*/
187+
void setDefaultValueExpression( const QString& expression );
188+
189+
/** Returns the alias for the field (the friendly displayed name of the field ),
190+
* or an empty string if there is no alias.
191+
* @see setAlias()
192+
* @note added in QGIS 3.0
193+
*/
194+
QString alias() const;
195+
196+
/** Sets the alias for the field (the friendly displayed name of the field ).
197+
* @param alias field alias, or empty string to remove an existing alias
198+
* @see alias()
199+
* @note added in QGIS 3.0
200+
*/
201+
void setAlias( const QString& alias );
202+
160203
/** Formats string for display*/
161204
QString displayString( const QVariant& v ) const;
162205

src/core/qgsfield_p.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ class QgsFieldPrivate : public QSharedData
6565
, length( other.length )
6666
, precision( other.precision )
6767
, comment( other.comment )
68+
, alias( other.alias )
69+
, defaultValueExpression( other.defaultValueExpression )
6870
{
6971
}
7072

@@ -73,7 +75,8 @@ class QgsFieldPrivate : public QSharedData
7375
bool operator==( const QgsFieldPrivate& other ) const
7476
{
7577
return (( name == other.name ) && ( type == other.type )
76-
&& ( length == other.length ) && ( precision == other.precision ) );
78+
&& ( length == other.length ) && ( precision == other.precision )
79+
&& ( alias == other.alias ) && ( defaultValueExpression == other.defaultValueExpression ) );
7780
}
7881

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

9497
//! Comment
9598
QString comment;
99+
100+
//! Alias for field name (friendly name shown to users)
101+
QString alias;
102+
103+
//! Default value expression
104+
QString defaultValueExpression;
96105
};
97106

98107

0 commit comments

Comments
 (0)