do not show file extension in TOC, add QqsLayerItem::layerName() for getting layer name (bug #5621)

This commit is contained in:
Etienne Tourigny 2012-05-28 00:05:44 -03:00
parent 147911b2c4
commit 04aab2938c
9 changed files with 59 additions and 12 deletions

View File

@ -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,

View File

@ -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 );
}
}

View File

@ -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(); }
};

View File

@ -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() )
{

View File

@ -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;

View File

@ -34,6 +34,7 @@ class QgsGdalLayerItem : public QgsLayerItem
QVector<QgsDataItem*> createChildren();
QString layerName() const;
};

View File

@ -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 )

View File

@ -28,6 +28,7 @@ class QgsOgrLayerItem : public QgsLayerItem
bool setCrs( QgsCoordinateReferenceSystem crs );
Capability capabilities();
QString layerName() const;
};
class QgsOgrDataCollectionItem : public QgsDataCollectionItem

View File

@ -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<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" )
{
@ -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;