Skip to content

Commit

Permalink
Merge remote-tracking branch 'etiennesky/browser_fixes'
Browse files Browse the repository at this point in the history
  • Loading branch information
brushtyler committed May 28, 2012
2 parents 5e45e87 + 04aab29 commit 149c96a
Show file tree
Hide file tree
Showing 13 changed files with 291 additions and 46 deletions.
4 changes: 2 additions & 2 deletions src/app/qgisapp.cpp
Expand Up @@ -6918,8 +6918,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
48 changes: 32 additions & 16 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 Expand Up @@ -163,7 +172,7 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
return 0;

// Filter files by extension
if ( !extensions.contains( info.suffix().toLower() ) )
if ( !extensions.contains( suffix ) )
{
bool matches = false;
foreach( QString wildcard, wildcards )
Expand Down Expand Up @@ -197,20 +206,27 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
}
}

// if setting = 2 (Basic scan), return a /vsizip/ item without testing
if ( is_vsizip && scanZipSetting == 2 )
{
QStringList sublayers;
QgsDebugMsg( QString( "adding item name=%1 thePath=%2" ).arg( name ).arg( thePath ) );
QgsLayerItem * item = new QgsGdalLayerItem( parentItem, name, thePath, thePath, &sublayers );
if ( item )
return item;
}

// if scan items == "Check extension", add item here without trying to open
// unless item is /vsizip
if ( scanItemsSetting == 1 && !is_vsizip )
// return a /vsizip/ item without testing if:
// zipfile and scan zip == "Basic scan"
// not zipfile and scan items == "Check extension"
if (( is_vsizip && scanZipSetting == 2 ) ||
( !is_vsizip && scanItemsSetting == 1 ) )
{
// if this is a VRT file make sure it is raster VRT to avoid duplicates
if ( suffix == "vrt" )
{
// do not print errors, but write to debug
CPLPushErrorHandler( CPLQuietErrorHandler );
CPLErrorReset();
if ( ! GDALIdentifyDriver( thePath.toLocal8Bit().constData(), 0 ) )
{
QgsDebugMsg( "Skipping VRT file because root is not a GDAL VRT" );
CPLPopErrorHandler();
return 0;
}
CPLPopErrorHandler();
}
// add the item
QStringList sublayers;
QgsDebugMsg( QString( "adding item name=%1 thePath=%2" ).arg( name ).arg( thePath ) );
QgsLayerItem * item = new QgsGdalLayerItem( parentItem, name, thePath, thePath, &sublayers );
Expand All @@ -221,10 +237,10 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
// test that file is valid with GDAL
GDALAllRegister();
// do not print errors, but write to debug
CPLErrorHandler oErrorHandler = CPLSetErrorHandler( CPLQuietErrorHandler );
CPLPushErrorHandler( CPLQuietErrorHandler );
CPLErrorReset();
GDALDatasetH hDS = GDALOpen( TO8F( thePath ), GA_ReadOnly );
CPLSetErrorHandler( oErrorHandler );
CPLPopErrorHandler();

if ( ! hDS )
{
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
67 changes: 45 additions & 22 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 Expand Up @@ -268,7 +278,7 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )

// We have to filter by extensions, otherwise e.g. all Shapefile files are displayed
// because OGR drive can open also .dbf, .shx.
if ( myExtensions.indexOf( info.suffix().toLower() ) < 0 )
if ( myExtensions.indexOf( suffix ) < 0 )
{
bool matches = false;
foreach( QString wildcard, wildcards() )
Expand All @@ -285,7 +295,7 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
}

// .dbf should probably appear if .shp is not present
if ( info.suffix().toLower() == "dbf" )
if ( suffix == "dbf" )
{
QString pathShp = thePath.left( thePath.count() - 4 ) + ".shp";
if ( QFileInfo( pathShp ).exists() )
Expand All @@ -310,20 +320,33 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
}
}

// if setting = 2 (Basic scan), return a /vsizip/ item without testing
if ( is_vsizip && scanZipSetting == 2 )
{
QStringList sublayers;
QgsDebugMsg( QString( "adding item name=%1 thePath=%2" ).arg( name ).arg( thePath ) );
QgsLayerItem * item = new QgsOgrLayerItem( parentItem, name, thePath, thePath, QgsLayerItem::Vector );
if ( item )
return item;
}

// if scan items == "Check extension", add item here without trying to open
// unless item is /vsizip
if ( scanItemsSetting == 1 && !is_vsizip && !is_vsigzip )
// return a /vsizip/ item without testing if:
// zipfile and scan zip == "Basic scan"
// not zipfile and scan items == "Check extension"
if (( is_vsizip && scanZipSetting == 2 ) ||
( !is_vsizip && scanItemsSetting == 1 ) )
{
// if this is a VRT file make sure it is vector VRT to avoid duplicates
if ( suffix == "vrt" )
{
OGRSFDriverH hDriver = OGRGetDriverByName( "VRT" );
if ( hDriver )
{
// do not print errors, but write to debug
CPLPushErrorHandler( CPLQuietErrorHandler );
CPLErrorReset();
OGRDataSourceH hDataSource = OGR_Dr_Open( hDriver, thePath.toLocal8Bit().constData(), 0 );
CPLPopErrorHandler();
if ( ! hDataSource )
{
QgsDebugMsg( "Skipping VRT file because root is not a OGR VRT" );
return 0;
}
OGR_DS_Destroy( hDataSource );
}
}
// add the item
// TODO: how to handle collections?
QgsLayerItem * item = new QgsOgrLayerItem( parentItem, name, thePath, thePath, QgsLayerItem::Vector );
if ( item )
return item;
Expand All @@ -333,10 +356,10 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
OGRRegisterAll();
OGRSFDriverH hDriver;
// do not print errors, but write to debug
CPLErrorHandler oErrorHandler = CPLSetErrorHandler( CPLQuietErrorHandler );
CPLPushErrorHandler( CPLQuietErrorHandler );
CPLErrorReset();
OGRDataSourceH hDataSource = OGROpen( TO8F( thePath ), false, &hDriver );
CPLSetErrorHandler( oErrorHandler );
CPLPopErrorHandler();

if ( ! hDataSource )
{
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
1 change: 1 addition & 0 deletions tests/src/core/CMakeLists.txt
Expand Up @@ -95,3 +95,4 @@ ADD_QGIS_TEST(searchstringtest testqgssearchstring.cpp)
ADD_QGIS_TEST(vectorlayertest testqgsvectorlayer.cpp)
ADD_QGIS_TEST(rulebasedrenderertest testqgsrulebasedrenderer.cpp)
ADD_QGIS_TEST(ziplayertest testziplayer.cpp)
ADD_QGIS_TEST(dataitemtest testqgsdataitem.cpp)

0 comments on commit 149c96a

Please sign in to comment.