Skip to content

Commit 04aab29

Browse files
committed
do not show file extension in TOC, add QqsLayerItem::layerName() for getting layer name (bug #5621)
1 parent 147911b commit 04aab29

9 files changed

+59
-12
lines changed

src/app/qgisapp.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -6920,8 +6920,8 @@ bool QgisApp::addRasterLayers( QStringList const &theFileNameQStringList, bool g
69206920
QFileInfo myFileInfo( *myIterator );
69216921
// get the directory the .adf file was in
69226922
QString myDirNameQString = myFileInfo.path();
6923-
//extract basename with extension
6924-
QString myBaseNameQString = myFileInfo.completeBaseName() + "." + myFileInfo.suffix();
6923+
//extract basename
6924+
QString myBaseNameQString = myFileInfo.completeBaseName();
69256925
//only allow one copy of a ai grid file to be loaded at a
69266926
//time to prevent the user selecting all adfs in 1 dir which
69276927
//actually represent 1 coverage,

src/app/qgsbrowserdockwidget.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ void QgsBrowserDockWidget::addLayer( QgsLayerItem *layerItem )
303303
QgsDebugMsg( providerKey + " : " + uri );
304304
if ( type == QgsMapLayer::VectorLayer )
305305
{
306-
QgisApp::instance()->addVectorLayer( uri, layerItem->name(), providerKey );
306+
QgisApp::instance()->addVectorLayer( uri, layerItem->layerName(), providerKey );
307307
}
308308
if ( type == QgsMapLayer::RasterLayer )
309309
{
@@ -333,7 +333,7 @@ void QgsBrowserDockWidget::addLayer( QgsLayerItem *layerItem )
333333
QgsDebugMsg( "rasterLayerPath = " + rasterLayerPath );
334334
QgsDebugMsg( "layers = " + layers.join( " " ) );
335335

336-
QgisApp::instance()->addRasterLayer( rasterLayerPath, layerItem->name(), providerKey, layers, styles, format, crs );
336+
QgisApp::instance()->addRasterLayer( rasterLayerPath, layerItem->layerName(), providerKey, layers, styles, format, crs );
337337
}
338338
}
339339

src/core/qgsdataitem.h

+2
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ class CORE_EXPORT QgsLayerItem : public QgsDataItem
194194
static const QIcon &iconTable();
195195
static const QIcon &iconRaster();
196196
static const QIcon &iconDefault();
197+
198+
virtual QString layerName() const { return name(); }
197199
};
198200

199201

src/core/qgsmimedatautils.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
static const char* QGIS_URILIST_MIMETYPE = "application/x-vnd.qgis.qgis.uri";
2121

2222
QgsMimeDataUtils::Uri::Uri( QgsLayerItem* layerItem )
23-
: providerKey( layerItem->providerKey() ), name( layerItem->name() ), uri( layerItem->uri() )
23+
: providerKey( layerItem->providerKey() ), name( layerItem->layerName() ), uri( layerItem->uri() )
2424
{
2525
switch ( layerItem->mapLayerType() )
2626
{

src/providers/gdal/qgsgdaldataitems.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@ QVector<QgsDataItem*> QgsGdalLayerItem::createChildren( )
103103
return children;
104104
}
105105

106+
QString QgsGdalLayerItem::layerName() const
107+
{
108+
QFileInfo info( name() );
109+
if ( info.suffix() == "gz" )
110+
return info.baseName();
111+
else
112+
return info.completeBaseName();
113+
}
114+
106115
// ---------------------------------------------------------------------------
107116

108117
static QString filterString;

src/providers/gdal/qgsgdaldataitems.h

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class QgsGdalLayerItem : public QgsLayerItem
3434

3535
QVector<QgsDataItem*> createChildren();
3636

37+
QString layerName() const;
3738
};
3839

3940

src/providers/ogr/qgsogrdataitems.cpp

+15-5
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,16 @@ bool QgsOgrLayerItem::setCrs( QgsCoordinateReferenceSystem crs )
7878
// we are able to assign CRS only to shapefiles :-(
7979
if ( driverName == "ESRI Shapefile" )
8080
{
81-
QString layerName = mPath.left( mPath.indexOf( ".shp", Qt::CaseInsensitive ) );
81+
// QString layerName = mPath.left( mPath.indexOf( ".shp", Qt::CaseInsensitive ) );
82+
QString lyrName = layerName();
8283
QString wkt = crs.toWkt();
8384

8485
// save ordinary .prj file
8586
OGRSpatialReferenceH hSRS = OSRNewSpatialReference( wkt.toLocal8Bit().data() );
8687
OSRMorphToESRI( hSRS ); // this is the important stuff for shapefile .prj
8788
char* pszOutWkt = NULL;
8889
OSRExportToWkt( hSRS, &pszOutWkt );
89-
QFile prjFile( layerName + ".prj" );
90+
QFile prjFile( lyrName + ".prj" );
9091
if ( prjFile.open( QIODevice::WriteOnly ) )
9192
{
9293
QTextStream prjStream( &prjFile );
@@ -95,14 +96,14 @@ bool QgsOgrLayerItem::setCrs( QgsCoordinateReferenceSystem crs )
9596
}
9697
else
9798
{
98-
QgsMessageLog::logMessage( tr( "Couldn't open file %1.prj" ).arg( layerName ), tr( "OGR" ) );
99+
QgsMessageLog::logMessage( tr( "Couldn't open file %1.prj" ).arg( lyrName ), tr( "OGR" ) );
99100
return false;
100101
}
101102
OSRDestroySpatialReference( hSRS );
102103
CPLFree( pszOutWkt );
103104

104105
// save qgis-specific .qpj file (maybe because of better wkt compatibility?)
105-
QFile qpjFile( layerName + ".qpj" );
106+
QFile qpjFile( lyrName + ".qpj" );
106107
if ( qpjFile.open( QIODevice::WriteOnly ) )
107108
{
108109
QTextStream qpjStream( &qpjFile );
@@ -111,7 +112,7 @@ bool QgsOgrLayerItem::setCrs( QgsCoordinateReferenceSystem crs )
111112
}
112113
else
113114
{
114-
QgsMessageLog::logMessage( tr( "Couldn't open file %1.qpj" ).arg( layerName ), tr( "OGR" ) );
115+
QgsMessageLog::logMessage( tr( "Couldn't open file %1.qpj" ).arg( lyrName ), tr( "OGR" ) );
115116
return false;
116117
}
117118

@@ -123,6 +124,15 @@ bool QgsOgrLayerItem::setCrs( QgsCoordinateReferenceSystem crs )
123124
return false;
124125
}
125126

127+
QString QgsOgrLayerItem::layerName() const
128+
{
129+
QFileInfo info( name() );
130+
if ( info.suffix() == "gz" )
131+
return info.baseName();
132+
else
133+
return info.completeBaseName();
134+
}
135+
126136
// -------
127137

128138
static QgsOgrLayerItem* dataItemForLayer( QgsDataItem* parentItem, QString name, QString path, OGRDataSourceH hDataSource, int layerId )

src/providers/ogr/qgsogrdataitems.h

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class QgsOgrLayerItem : public QgsLayerItem
2828

2929
bool setCrs( QgsCoordinateReferenceSystem crs );
3030
Capability capabilities();
31+
QString layerName() const;
3132
};
3233

3334
class QgsOgrDataCollectionItem : public QgsDataCollectionItem

tests/src/core/testqgsdataitem.cpp

+26-2
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ void TestQgsDataItem::testValid()
9292
void TestQgsDataItem::testDirItemChildren()
9393
{
9494
QSettings settings;
95-
// test scanItems setting=1 to test .vrt and .gz files are not loaded by gdal and ogr
9695
for ( int iSetting = 0 ; iSetting <= 1 ; iSetting++ )
9796
{
9897
settings.setValue( "/qgis/scanItemsInBrowser", iSetting );
@@ -106,13 +105,15 @@ void TestQgsDataItem::testDirItemChildren()
106105
QgsLayerItem* layerItem = dynamic_cast<QgsLayerItem*>( dataItem );
107106
if ( ! layerItem )
108107
continue;
108+
109+
// test .vrt and .gz files are not loaded by gdal and ogr
109110
QFileInfo info( layerItem->path() );
110111
QString lFile = info.fileName();
111112
QString lProvider = layerItem->providerKey();
112113
QString errStr = QString( "layer #%1 - %2 provider = %3 iSetting = %4" ).arg( i ).arg( lFile ).arg( lProvider ).arg( iSetting );
113114
const char* err = errStr.toLocal8Bit().constData();
114115

115-
QgsDebugMsg( QString( "child name=%1 provider=%2 path=%3" ).arg( layerItem->name() ).arg( lProvider ).arg( lFile ) );
116+
QgsDebugMsg( QString( "testing child name=%1 provider=%2 path=%3" ).arg( layerItem->name() ).arg( lProvider ).arg( lFile ) );
116117

117118
if ( lFile == "landsat.tif" )
118119
{
@@ -134,6 +135,29 @@ void TestQgsDataItem::testDirItemChildren()
134135
{
135136
QVERIFY2( lProvider == "ogr", err );
136137
}
138+
139+
// test layerName() does not include extension for gdal and ogr items (bug #5621)
140+
QString lName = layerItem->layerName();
141+
errStr = QString( "layer #%1 - %2 lName = %3 iSetting = %4" ).arg( i ).arg( lFile ).arg( lName ).arg( iSetting );
142+
err = errStr.toLocal8Bit().constData();
143+
144+
if ( lFile == "landsat.tif" )
145+
{
146+
QVERIFY2( lName == "landsat", err );
147+
}
148+
else if ( lFile == "points.shp" )
149+
{
150+
QVERIFY2( lName == "points", err );
151+
}
152+
else if ( lFile == "landsat_b1.tif.gz" )
153+
{
154+
QVERIFY2( lName == "landsat_b1", err );
155+
}
156+
else if ( lFile == "points3.geojson.gz" )
157+
{
158+
QVERIFY2( lName == "points3", err );
159+
}
160+
137161
}
138162
if ( dirItem )
139163
delete dirItem;

0 commit comments

Comments
 (0)