Skip to content
Permalink
Browse files

Tweaks to automatic layer name logic

  • Loading branch information
nyalldawson committed Nov 12, 2017
1 parent 15e1535 commit 020972c2916ae5a24570a33133e89d56da684d87
Showing with 17 additions and 7 deletions.
  1. +7 −2 src/app/qgisapp.cpp
  2. +1 −5 src/core/qgsmaplayer.cpp
  3. +9 −0 tests/src/core/testqgsmaplayer.cpp
@@ -4157,6 +4157,7 @@ bool QgisApp::addVectorLayers( const QStringList &layerQStringList, const QStrin
QFileInfo fi( src );
base = fi.completeBaseName();
}
base = QgsMapLayer::formatLayerName( base );

QgsDebugMsg( "completeBaseName: " + base );

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

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

if ( elements.size() >= 4 && layer->name() != elements.at( 1 ) )
if ( elements.size() >= 4 && layer->name().compare( elements.at( 1 ), Qt::CaseInsensitive ) != 0
&& layer->name().compare( subLayerNameFormatted, Qt::CaseInsensitive ) != 0 )
{
layer->setName( QStringLiteral( "%1 %2" ).arg( layer->name(), elements.at( 1 ) ) );
}
@@ -1022,12 +1022,8 @@ void QgsMapLayer::setCrs( const QgsCoordinateReferenceSystem &srs, bool emitSign
QString QgsMapLayer::formatLayerName( const QString &name )
{
QString layerName( name );

if ( !layerName.isEmpty() )
layerName = QgsStringUtils::capitalize( name, QgsStringUtils::ForceFirstLetterToCapital );

layerName.replace( '_', ' ' );

layerName = QgsStringUtils::capitalize( layerName, QgsStringUtils::ForceFirstLetterToCapital );
return layerName;
}

@@ -62,6 +62,7 @@ class TestQgsMapLayer : public QObject
void cleanup(); // will be called after every testfunction.

void isValid();
void formatName();

void setBlendMode();

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

void TestQgsMapLayer::formatName()
{
QCOMPARE( QgsMapLayer::formatLayerName( QString() ), QString() );
QCOMPARE( QgsMapLayer::formatLayerName( QStringLiteral( "layer" ) ), QStringLiteral( "Layer" ) );
QCOMPARE( QgsMapLayer::formatLayerName( QStringLiteral( "layer name" ) ), QStringLiteral( "Layer Name" ) );
QCOMPARE( QgsMapLayer::formatLayerName( QStringLiteral( "layer_name" ) ), QStringLiteral( "Layer Name" ) );
}

void TestQgsMapLayer::setBlendMode()
{
TestSignalReceiver receiver;

0 comments on commit 020972c

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