Skip to content

Commit

Permalink
do not show file extension in TOC, add QqsLayerItem::layerName() for …
Browse files Browse the repository at this point in the history
…getting layer name (bug #5621)
  • Loading branch information
etiennesky committed May 28, 2012
1 parent 147911b commit 04aab29
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/app/qgisapp.cpp
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/app/qgsbrowserdockwidget.cpp
Expand Up @@ -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 )
{
Expand Down Expand Up @@ -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 );
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/core/qgsdataitem.h
Expand Up @@ -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(); }
};


Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsmimedatautils.cpp
Expand Up @@ -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() )
{
Expand Down
9 changes: 9 additions & 0 deletions src/providers/gdal/qgsgdaldataitems.cpp
Expand Up @@ -103,6 +103,15 @@ QVector<QgsDataItem*> 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;
Expand Down
1 change: 1 addition & 0 deletions src/providers/gdal/qgsgdaldataitems.h
Expand Up @@ -34,6 +34,7 @@ class QgsGdalLayerItem : public QgsLayerItem

QVector<QgsDataItem*> createChildren();

QString layerName() const;
};


Expand Down
20 changes: 15 additions & 5 deletions src/providers/ogr/qgsogrdataitems.cpp
Expand Up @@ -78,15 +78,16 @@ 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
OGRSpatialReferenceH hSRS = OSRNewSpatialReference( wkt.toLocal8Bit().data() );
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 );
Expand All @@ -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 );
Expand All @@ -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;
}

Expand All @@ -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 )
Expand Down
1 change: 1 addition & 0 deletions src/providers/ogr/qgsogrdataitems.h
Expand Up @@ -28,6 +28,7 @@ class QgsOgrLayerItem : public QgsLayerItem

bool setCrs( QgsCoordinateReferenceSystem crs );
Capability capabilities();
QString layerName() const;
};

class QgsOgrDataCollectionItem : public QgsDataCollectionItem
Expand Down
28 changes: 26 additions & 2 deletions tests/src/core/testqgsdataitem.cpp
Expand Up @@ -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 );
Expand All @@ -106,13 +105,15 @@ void TestQgsDataItem::testDirItemChildren()
QgsLayerItem* layerItem = dynamic_cast<QgsLayerItem*>( 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" )
{
Expand All @@ -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;
Expand Down

0 comments on commit 04aab29

Please sign in to comment.