Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default to display field when first setting labels enabled on a layer #30245

Merged
merged 7 commits into from
Jun 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/app/qgslabelingwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ void QgsLabelingWidget::labelModeChanged( int index )
if ( !mSimpleSettings )
mSimpleSettings.reset( new QgsPalLayerSettings() );

if ( mSimpleSettings->fieldName.isEmpty() )
mSimpleSettings->fieldName = mLayer->displayField();

QgsLabelingGui *simpleWidget = new QgsLabelingGui( mLayer, mCanvas, *mSimpleSettings, this );
simpleWidget->setDockMode( dockMode() );
connect( simpleWidget, &QgsTextFormatWidget::widgetChanged, this, &QgsLabelingWidget::widgetChanged );
Expand Down
46 changes: 25 additions & 21 deletions src/core/qgsvectorlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3106,7 +3106,7 @@ void QgsVectorLayer::setCoordinateSystem()

QString QgsVectorLayer::displayField() const
{
QgsExpression exp( mDisplayExpression );
QgsExpression exp( displayExpression() );
if ( exp.isField() )
{
return static_cast<const QgsExpressionNodeColumnRef *>( exp.rootNode() )->name();
Expand Down Expand Up @@ -3134,30 +3134,34 @@ QString QgsVectorLayer::displayExpression() const
{
QString idxName;

const auto constMFields = mFields;
for ( const QgsField &field : constMFields )
// Check the fields and keep the first one that matches.
// We assume that the user has organized the data with the
// more "interesting" field names first. As such, name should
// be selected before oldname, othername, etc.
// This candidates list is a prioritized list of candidates ranked by "interestingness"!
// See discussion at https://github.com/qgis/QGIS/pull/30245 - this list must NOT be translated,
// but adding hardcoded localized variants of the strings is encouraged.
static QStringList sCandidates{ QStringLiteral( "name" ),
QStringLiteral( "heibt" ),
QStringLiteral( "desc" ),
QStringLiteral( "nom" ),
QStringLiteral( "street" ),
QStringLiteral( "road" ),
QStringLiteral( "id" )};
for ( const QString &candidate : sCandidates )
m-kuhn marked this conversation as resolved.
Show resolved Hide resolved
{
QString fldName = field.name();

// Check the fields and keep the first one that matches.
// We assume that the user has organized the data with the
// more "interesting" field names first. As such, name should
// be selected before oldname, othername, etc.
if ( fldName.indexOf( QLatin1String( "name" ), 0, Qt::CaseInsensitive ) > -1 )
{
idxName = fldName;
break;
}
if ( fldName.indexOf( QLatin1String( "descrip" ), 0, Qt::CaseInsensitive ) > -1 )
for ( const QgsField &field : mFields )
{
idxName = fldName;
break;
QString fldName = field.name();
if ( fldName.indexOf( candidate, 0, Qt::CaseInsensitive ) > -1 )
{
idxName = fldName;
break;
}
}
if ( fldName.indexOf( QLatin1String( "id" ), 0, Qt::CaseInsensitive ) > -1 )
{
idxName = fldName;

if ( !idxName.isEmpty() )
break;
}
}

if ( !idxName.isNull() )
Expand Down
20 changes: 20 additions & 0 deletions tests/src/python/test_qgsvectorlayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2949,6 +2949,26 @@ def testPrecisionAndDuplicateNodes(self):
layer.addFeature(feature)
self.assertGeometriesEqual(QgsGeometry.fromWkt('Polygon ((2596410 1224650, 2596400 1224650, 2596410 1224640, 2596410 1224650))'), feature.geometry(), 'geometry with unsnapped nodes', 'fixed geometry')

def testDefaultDisplayExpression(self):
"""
Test that default display expression gravitates to most interesting column names
"""
layer = QgsVectorLayer("Polygon?crs=epsg:2056&field=pk:int", "vl", "memory")
self.assertEqual(layer.displayExpression(), '"pk"')
self.assertEqual(layer.displayField(), 'pk')
layer = QgsVectorLayer("Polygon?crs=epsg:2056&field=pk:int&field=fid:int", "vl", "memory")
self.assertEqual(layer.displayExpression(), '"fid"')
self.assertEqual(layer.displayField(), 'fid')
layer = QgsVectorLayer("Polygon?crs=epsg:2056&field=pk:int&field=DESCRIPTION:string&field=fid:int", "vl", "memory")
self.assertEqual(layer.displayExpression(), '"DESCRIPTION"')
self.assertEqual(layer.displayField(), 'DESCRIPTION')
layer = QgsVectorLayer("Polygon?crs=epsg:2056&field=pk:int&field=DESCRIPTION:string&field=fid:int&field=NAME:string", "vl", "memory")
self.assertEqual(layer.displayExpression(), '"NAME"')
self.assertEqual(layer.displayField(), 'NAME')
layer = QgsVectorLayer("Polygon?crs=epsg:2056&field=pk:int&field=DESCRIPTION:string&field=fid:int&field=BETTER_NAME:string&field=NAME:string", "vl", "memory")
self.assertEqual(layer.displayExpression(), '"BETTER_NAME"')
self.assertEqual(layer.displayField(), 'BETTER_NAME')


class TestQgsVectorLayerSourceAddedFeaturesInBuffer(unittest.TestCase, FeatureSourceTestCase):

Expand Down
2 changes: 1 addition & 1 deletion tests/testdata/qgis_server/getprojectsettings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ Content-Type: text/xml; charset=utf-8
<BoundingBox maxy="5.60603e+06" maxx="913215" miny="5.60601e+06" CRS="EPSG:3857" minx="913205"/>
<BoundingBox maxy="8.20355" maxx="44.9015" miny="8.20346" CRS="EPSG:4326" minx="44.9014"/>
<TreeName>groupwithoutshortname</TreeName>
<Layer geometryType="Point" queryable="0" displayField="id" visible="1">
<Layer geometryType="Point" queryable="0" displayField="name" visible="1">
<Name>testlayer3</Name>
<Title>testlayer3</Title>
<CRS>CRS:84</CRS>
Expand Down