diff --git a/src/app/qgisapp.cpp b/src/app/qgisapp.cpp index 830d0d72513b..c93e64ff9f41 100644 --- a/src/app/qgisapp.cpp +++ b/src/app/qgisapp.cpp @@ -6920,8 +6920,8 @@ bool QgisApp::addRasterLayers( QStringList const &theFileNameQStringList, bool g QFileInfo myFileInfo( *myIterator ); // get the directory the .adf file was in QString myDirNameQString = myFileInfo.path(); - //extract basename with extension - QString myBaseNameQString = myFileInfo.completeBaseName() + "." + myFileInfo.suffix(); + //extract basename + QString myBaseNameQString = myFileInfo.completeBaseName(); //only allow one copy of a ai grid file to be loaded at a //time to prevent the user selecting all adfs in 1 dir which //actually represent 1 coverage, diff --git a/src/app/qgsbrowserdockwidget.cpp b/src/app/qgsbrowserdockwidget.cpp index 73f06afc513a..e86884cc24e4 100644 --- a/src/app/qgsbrowserdockwidget.cpp +++ b/src/app/qgsbrowserdockwidget.cpp @@ -303,7 +303,7 @@ void QgsBrowserDockWidget::addLayer( QgsLayerItem *layerItem ) QgsDebugMsg( providerKey + " : " + uri ); if ( type == QgsMapLayer::VectorLayer ) { - QgisApp::instance()->addVectorLayer( uri, layerItem->name(), providerKey ); + QgisApp::instance()->addVectorLayer( uri, layerItem->layerName(), providerKey ); } if ( type == QgsMapLayer::RasterLayer ) { @@ -333,7 +333,7 @@ void QgsBrowserDockWidget::addLayer( QgsLayerItem *layerItem ) QgsDebugMsg( "rasterLayerPath = " + rasterLayerPath ); QgsDebugMsg( "layers = " + layers.join( " " ) ); - QgisApp::instance()->addRasterLayer( rasterLayerPath, layerItem->name(), providerKey, layers, styles, format, crs ); + QgisApp::instance()->addRasterLayer( rasterLayerPath, layerItem->layerName(), providerKey, layers, styles, format, crs ); } } diff --git a/src/core/qgsdataitem.h b/src/core/qgsdataitem.h index d441e1a0e9d7..df3408144673 100644 --- a/src/core/qgsdataitem.h +++ b/src/core/qgsdataitem.h @@ -194,6 +194,8 @@ class CORE_EXPORT QgsLayerItem : public QgsDataItem static const QIcon &iconTable(); static const QIcon &iconRaster(); static const QIcon &iconDefault(); + + virtual QString layerName() const { return name(); } }; diff --git a/src/core/qgsmimedatautils.cpp b/src/core/qgsmimedatautils.cpp index cc989fbd2796..68c3259322ba 100644 --- a/src/core/qgsmimedatautils.cpp +++ b/src/core/qgsmimedatautils.cpp @@ -20,7 +20,7 @@ static const char* QGIS_URILIST_MIMETYPE = "application/x-vnd.qgis.qgis.uri"; QgsMimeDataUtils::Uri::Uri( QgsLayerItem* layerItem ) - : providerKey( layerItem->providerKey() ), name( layerItem->name() ), uri( layerItem->uri() ) + : providerKey( layerItem->providerKey() ), name( layerItem->layerName() ), uri( layerItem->uri() ) { switch ( layerItem->mapLayerType() ) { diff --git a/src/providers/gdal/qgsgdaldataitems.cpp b/src/providers/gdal/qgsgdaldataitems.cpp index 3598c75b2722..bea5c6a34ebc 100644 --- a/src/providers/gdal/qgsgdaldataitems.cpp +++ b/src/providers/gdal/qgsgdaldataitems.cpp @@ -103,6 +103,15 @@ QVector QgsGdalLayerItem::createChildren( ) return children; } +QString QgsGdalLayerItem::layerName() const +{ + QFileInfo info( name() ); + if ( info.suffix() == "gz" ) + return info.baseName(); + else + return info.completeBaseName(); +} + // --------------------------------------------------------------------------- static QString filterString; diff --git a/src/providers/gdal/qgsgdaldataitems.h b/src/providers/gdal/qgsgdaldataitems.h index 9356d9ae8188..9827b4d1e51c 100644 --- a/src/providers/gdal/qgsgdaldataitems.h +++ b/src/providers/gdal/qgsgdaldataitems.h @@ -34,6 +34,7 @@ class QgsGdalLayerItem : public QgsLayerItem QVector createChildren(); + QString layerName() const; }; diff --git a/src/providers/ogr/qgsogrdataitems.cpp b/src/providers/ogr/qgsogrdataitems.cpp index 8583540ade1b..d6882e2402cf 100644 --- a/src/providers/ogr/qgsogrdataitems.cpp +++ b/src/providers/ogr/qgsogrdataitems.cpp @@ -78,7 +78,8 @@ bool QgsOgrLayerItem::setCrs( QgsCoordinateReferenceSystem crs ) // we are able to assign CRS only to shapefiles :-( if ( driverName == "ESRI Shapefile" ) { - QString layerName = mPath.left( mPath.indexOf( ".shp", Qt::CaseInsensitive ) ); + // QString layerName = mPath.left( mPath.indexOf( ".shp", Qt::CaseInsensitive ) ); + QString lyrName = layerName(); QString wkt = crs.toWkt(); // save ordinary .prj file @@ -86,7 +87,7 @@ bool QgsOgrLayerItem::setCrs( QgsCoordinateReferenceSystem crs ) OSRMorphToESRI( hSRS ); // this is the important stuff for shapefile .prj char* pszOutWkt = NULL; OSRExportToWkt( hSRS, &pszOutWkt ); - QFile prjFile( layerName + ".prj" ); + QFile prjFile( lyrName + ".prj" ); if ( prjFile.open( QIODevice::WriteOnly ) ) { QTextStream prjStream( &prjFile ); @@ -95,14 +96,14 @@ bool QgsOgrLayerItem::setCrs( QgsCoordinateReferenceSystem crs ) } else { - QgsMessageLog::logMessage( tr( "Couldn't open file %1.prj" ).arg( layerName ), tr( "OGR" ) ); + QgsMessageLog::logMessage( tr( "Couldn't open file %1.prj" ).arg( lyrName ), tr( "OGR" ) ); return false; } OSRDestroySpatialReference( hSRS ); CPLFree( pszOutWkt ); // save qgis-specific .qpj file (maybe because of better wkt compatibility?) - QFile qpjFile( layerName + ".qpj" ); + QFile qpjFile( lyrName + ".qpj" ); if ( qpjFile.open( QIODevice::WriteOnly ) ) { QTextStream qpjStream( &qpjFile ); @@ -111,7 +112,7 @@ bool QgsOgrLayerItem::setCrs( QgsCoordinateReferenceSystem crs ) } else { - QgsMessageLog::logMessage( tr( "Couldn't open file %1.qpj" ).arg( layerName ), tr( "OGR" ) ); + QgsMessageLog::logMessage( tr( "Couldn't open file %1.qpj" ).arg( lyrName ), tr( "OGR" ) ); return false; } @@ -123,6 +124,15 @@ bool QgsOgrLayerItem::setCrs( QgsCoordinateReferenceSystem crs ) return false; } +QString QgsOgrLayerItem::layerName() const +{ + QFileInfo info( name() ); + if ( info.suffix() == "gz" ) + return info.baseName(); + else + return info.completeBaseName(); +} + // ------- static QgsOgrLayerItem* dataItemForLayer( QgsDataItem* parentItem, QString name, QString path, OGRDataSourceH hDataSource, int layerId ) diff --git a/src/providers/ogr/qgsogrdataitems.h b/src/providers/ogr/qgsogrdataitems.h index 53d860771431..7957ffd5c433 100644 --- a/src/providers/ogr/qgsogrdataitems.h +++ b/src/providers/ogr/qgsogrdataitems.h @@ -28,6 +28,7 @@ class QgsOgrLayerItem : public QgsLayerItem bool setCrs( QgsCoordinateReferenceSystem crs ); Capability capabilities(); + QString layerName() const; }; class QgsOgrDataCollectionItem : public QgsDataCollectionItem diff --git a/tests/src/core/testqgsdataitem.cpp b/tests/src/core/testqgsdataitem.cpp index 60cfd08459dc..19bce0fd2068 100644 --- a/tests/src/core/testqgsdataitem.cpp +++ b/tests/src/core/testqgsdataitem.cpp @@ -92,7 +92,6 @@ void TestQgsDataItem::testValid() void TestQgsDataItem::testDirItemChildren() { QSettings settings; - // test scanItems setting=1 to test .vrt and .gz files are not loaded by gdal and ogr for ( int iSetting = 0 ; iSetting <= 1 ; iSetting++ ) { settings.setValue( "/qgis/scanItemsInBrowser", iSetting ); @@ -106,13 +105,15 @@ void TestQgsDataItem::testDirItemChildren() QgsLayerItem* layerItem = dynamic_cast( dataItem ); if ( ! layerItem ) continue; + + // test .vrt and .gz files are not loaded by gdal and ogr QFileInfo info( layerItem->path() ); QString lFile = info.fileName(); QString lProvider = layerItem->providerKey(); QString errStr = QString( "layer #%1 - %2 provider = %3 iSetting = %4" ).arg( i ).arg( lFile ).arg( lProvider ).arg( iSetting ); const char* err = errStr.toLocal8Bit().constData(); - QgsDebugMsg( QString( "child name=%1 provider=%2 path=%3" ).arg( layerItem->name() ).arg( lProvider ).arg( lFile ) ); + QgsDebugMsg( QString( "testing child name=%1 provider=%2 path=%3" ).arg( layerItem->name() ).arg( lProvider ).arg( lFile ) ); if ( lFile == "landsat.tif" ) { @@ -134,6 +135,29 @@ void TestQgsDataItem::testDirItemChildren() { QVERIFY2( lProvider == "ogr", err ); } + + // test layerName() does not include extension for gdal and ogr items (bug #5621) + QString lName = layerItem->layerName(); + errStr = QString( "layer #%1 - %2 lName = %3 iSetting = %4" ).arg( i ).arg( lFile ).arg( lName ).arg( iSetting ); + err = errStr.toLocal8Bit().constData(); + + if ( lFile == "landsat.tif" ) + { + QVERIFY2( lName == "landsat", err ); + } + else if ( lFile == "points.shp" ) + { + QVERIFY2( lName == "points", err ); + } + else if ( lFile == "landsat_b1.tif.gz" ) + { + QVERIFY2( lName == "landsat_b1", err ); + } + else if ( lFile == "points3.geojson.gz" ) + { + QVERIFY2( lName == "points3", err ); + } + } if ( dirItem ) delete dirItem;