Skip to content

Commit 020972c

Browse files
committedNov 14, 2017
Tweaks to automatic layer name logic
1 parent 15e1535 commit 020972c

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed
 

‎src/app/qgisapp.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4157,6 +4157,7 @@ bool QgisApp::addVectorLayers( const QStringList &layerQStringList, const QStrin
41574157
QFileInfo fi( src );
41584158
base = fi.completeBaseName();
41594159
}
4160+
base = QgsMapLayer::formatLayerName( base );
41604161

41614162
QgsDebugMsg( "completeBaseName: " + base );
41624163

@@ -4200,8 +4201,10 @@ bool QgisApp::addVectorLayers( const QStringList &layerQStringList, const QStrin
42004201
{
42014202
//set friendly name for datasources with only one layer
42024203
QStringList elements = sublayers.at( 0 ).split( ':' );
4204+
QString subLayerNameFormatted = elements.size() >= 2 ? QgsMapLayer::formatLayerName( elements.at( 1 ) ) : QString();
42034205

4204-
if ( elements.size() >= 4 && layer->name() != elements.at( 1 ) )
4206+
if ( elements.size() >= 4 && layer->name().compare( elements.at( 1 ), Qt::CaseInsensitive ) != 0
4207+
&& layer->name().compare( subLayerNameFormatted, Qt::CaseInsensitive ) != 0 )
42054208
{
42064209
layer->setName( QStringLiteral( "%1 %2" ).arg( layer->name(), elements.at( 1 ) ) );
42074210
}
@@ -9994,8 +9997,10 @@ QgsVectorLayer *QgisApp::addVectorLayer( const QString &vectorLayerPath, const Q
99949997
if ( !sublayers.isEmpty() )
99959998
{
99969999
QStringList elements = sublayers.at( 0 ).split( ':' );
10000+
QString subLayerNameFormatted = elements.size() >= 2 ? QgsMapLayer::formatLayerName( elements.at( 1 ) ) : QString();
999710001

9998-
if ( elements.size() >= 4 && layer->name() != elements.at( 1 ) )
10002+
if ( elements.size() >= 4 && layer->name().compare( elements.at( 1 ), Qt::CaseInsensitive ) != 0
10003+
&& layer->name().compare( subLayerNameFormatted, Qt::CaseInsensitive ) != 0 )
999910004
{
1000010005
layer->setName( QStringLiteral( "%1 %2" ).arg( layer->name(), elements.at( 1 ) ) );
1000110006
}

‎src/core/qgsmaplayer.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,12 +1022,8 @@ void QgsMapLayer::setCrs( const QgsCoordinateReferenceSystem &srs, bool emitSign
10221022
QString QgsMapLayer::formatLayerName( const QString &name )
10231023
{
10241024
QString layerName( name );
1025-
1026-
if ( !layerName.isEmpty() )
1027-
layerName = QgsStringUtils::capitalize( name, QgsStringUtils::ForceFirstLetterToCapital );
1028-
10291025
layerName.replace( '_', ' ' );
1030-
1026+
layerName = QgsStringUtils::capitalize( layerName, QgsStringUtils::ForceFirstLetterToCapital );
10311027
return layerName;
10321028
}
10331029

‎tests/src/core/testqgsmaplayer.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class TestQgsMapLayer : public QObject
6262
void cleanup(); // will be called after every testfunction.
6363

6464
void isValid();
65+
void formatName();
6566

6667
void setBlendMode();
6768

@@ -116,6 +117,14 @@ void TestQgsMapLayer::isValid()
116117
QVERIFY( mpLayer->isValid() );
117118
}
118119

120+
void TestQgsMapLayer::formatName()
121+
{
122+
QCOMPARE( QgsMapLayer::formatLayerName( QString() ), QString() );
123+
QCOMPARE( QgsMapLayer::formatLayerName( QStringLiteral( "layer" ) ), QStringLiteral( "Layer" ) );
124+
QCOMPARE( QgsMapLayer::formatLayerName( QStringLiteral( "layer name" ) ), QStringLiteral( "Layer Name" ) );
125+
QCOMPARE( QgsMapLayer::formatLayerName( QStringLiteral( "layer_name" ) ), QStringLiteral( "Layer Name" ) );
126+
}
127+
119128
void TestQgsMapLayer::setBlendMode()
120129
{
121130
TestSignalReceiver receiver;

0 commit comments

Comments
 (0)
Please sign in to comment.