Skip to content
Permalink
Browse files
apply #5041 (slighly modified)
  • Loading branch information
jef-n committed Feb 18, 2012
1 parent 4c0dd44 commit 73afb0f295211ae40c7edcb634dc7d87e0da6d36
@@ -24,11 +24,10 @@ QgsOGRSublayersDialog::QgsOGRSublayersDialog( QWidget* parent, Qt::WFlags fl )
{
setupUi( this );

layersTable->setHeaderLabels( QStringList() << tr( "Layer ID" ) << tr( "Layer name" ) << tr( "Nb of features" ) << tr( "Geometry type" ) );

QSettings settings;
restoreGeometry( settings.value( "/Windows/OGRSubLayers/geometry" ).toByteArray() );

QStringList labels = QStringList() << tr( "Layer ID" ) << tr( "Layer name" ) << tr( "Nb of features" ) << tr( "Geometry type" );
layersTable->setHeaderLabels( labels );
}

QgsOGRSublayersDialog::~QgsOGRSublayersDialog()
@@ -39,23 +38,24 @@ QgsOGRSublayersDialog::~QgsOGRSublayersDialog()

QStringList QgsOGRSublayersDialog::getSelection()
{
QStringList list = QStringList();
QStringList list;
for ( int i = 0; i < layersTable->selectedItems().size(); i++ )
{
QString theItem = layersTable->selectedItems().at( i )->text( 1 );
list.append( theItem );
list << layersTable->selectedItems().at( i )->text( 1 );
}
return list;
}

void QgsOGRSublayersDialog::populateLayerTable( QStringList theList, QString delim )
{
for ( int i = 0; i < theList.size(); i++ )
foreach( QString item, theList )
{
QString line = theList.at( i );
QStringList elements = line.split( delim );
QStringList item = QStringList();
item << elements.at( 0 ) << elements.at( 1 ) << elements.at( 2 ) << elements.at( 3 );
layersTable->addTopLevelItem( new QTreeWidgetItem( item ) );
layersTable->addTopLevelItem( new QTreeWidgetItem( item.split( delim ) ) );
}

// resize columns
for ( int i = 0; i < layersTable->columnCount(); i++ )
layersTable->resizeColumnToContents( i );

layersTable->setColumnWidth( 1, layersTable->columnWidth( 1 ) + 10 );
}
@@ -177,7 +177,7 @@
#include "ogr/qgsopenvectorlayerdialog.h"
#include "ogr/qgsvectorlayersaveasdialog.h"
//
// Gdal/Ogr includes
// GDAL/OGR includes
//
#include <ogr_api.h>

@@ -2135,7 +2135,6 @@ void QgisApp::addVectorLayer()

bool QgisApp::addVectorLayers( QStringList const & theLayerQStringList, const QString& enc, const QString dataSourceType )
{

foreach( QString src, theLayerQStringList )
{
src = src.trimmed();
@@ -2270,11 +2269,27 @@ void QgisApp::askUserForGDALSublayers( QgsRasterLayer *layer )
}
}

bool QgisApp::shouldAskUserForGDALSublayers( QgsRasterLayer *layer )
{
// return false if layer is empty or raster has no sublayers
if ( !layer || layer->subLayers().size() < 1 )
return false;

QSettings settings;
int promptLayers = settings.value( "/qgis/promptForRasterSublayers", "if_need" ).toInt();
// 0 = always -> always ask (if there are existing sublayers)
// 1 = if needed -> ask if layer has no bands, but has sublayers
// 2 = never

return promptLayers == 0 || ( promptLayers == 1 && layer->bandCount() == 0 );
}


// This method is the method that does the real job. If the layer given in
// parameter is NULL, then the method tries to act on the activeLayer.
void QgisApp::askUserForOGRSublayers( QgsVectorLayer *layer )
{
if ( layer == NULL )
if ( !layer )
{
layer = qobject_cast<QgsVectorLayer *>( activeLayer() );
if ( !layer || layer->dataProvider()->name() != "ogr" )
@@ -6472,7 +6487,24 @@ QgsRasterLayer* QgisApp::addRasterLayer( QString const & rasterFile, QString con
QgsRasterLayer *layer =
new QgsRasterLayer( rasterFile, baseName ); // fi.completeBaseName());

if ( !addRasterLayer( layer ) )
bool ok = false;

if ( shouldAskUserForGDALSublayers( layer ) )
{
askUserForGDALSublayers( layer );
ok = true;

// The first layer loaded is not useful in that case. The user can select it in
// the list if he wants to load it.
delete layer;
layer = 0;
}
else
{
ok = addRasterLayer( layer );
}

if ( !ok )
{
mMapCanvas->freeze( false );
QApplication::restoreOverrideCursor();
@@ -6556,7 +6588,15 @@ QgsRasterLayer* QgisApp::addRasterLayer(

QgsDebugMsg( "Constructed new layer." );

if ( layer && layer->isValid() )
if ( layer && shouldAskUserForGDALSublayers( layer ) )
{
askUserForGDALSublayers( layer );

// The first layer loaded is not useful in that case. The user can select it in
// the list if he wants to load it.
delete layer;
}
else if ( layer && layer->isValid() )
{
addRasterLayer( layer );

@@ -6582,7 +6622,6 @@ QgsRasterLayer* QgisApp::addRasterLayer(
} // QgisApp::addRasterLayer



//create a raster layer object and delegate to addRasterLayer(QgsRasterLayer *)
bool QgisApp::addRasterLayers( QStringList const &theFileNameQStringList, bool guiWarning )
{
@@ -6627,9 +6666,8 @@ bool QgisApp::addRasterLayers( QStringList const &theFileNameQStringList, bool g

// create the layer
QgsRasterLayer *layer = new QgsRasterLayer( *myIterator, myBaseNameQString );
QStringList sublayers = layer->subLayers();

if ( sublayers.size() > 0 )
if ( shouldAskUserForGDALSublayers( layer ) )
{
askUserForGDALSublayers( layer );

@@ -877,6 +877,7 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
private:
/** This method will open a dialog so the user can select the sublayers to load
*/
bool shouldAskUserForGDALSublayers( QgsRasterLayer *layer );
void askUserForOGRSublayers( QgsVectorLayer *layer );
void askUserForGDALSublayers( QgsRasterLayer *layer );
/** Add a raster layer to the map (passed in as a ptr).
@@ -129,7 +129,7 @@ void QgsBrowserDockWidget::itemClicked( const QModelIndex& index )
QgsMapLayer* layer = NULL;
if ( type == QgsMapLayer::VectorLayer )
{
layer = new QgsVectorLayer( uri, layerItem->name(), providerKey );
layer = QgisApp::instance()->addVectorLayer( uri, layerItem->name(), providerKey );
}
if ( type == QgsMapLayer::RasterLayer )
{
@@ -159,18 +159,8 @@ void QgsBrowserDockWidget::itemClicked( const QModelIndex& index )
QgsDebugMsg( "rasterLayerPath = " + rasterLayerPath );
QgsDebugMsg( "layers = " + layers.join( " " ) );

layer = new QgsRasterLayer( 0, rasterLayerPath, layerItem->name(), providerKey, layers, styles, format, crs );
layer = QgisApp::instance()->addRasterLayer( rasterLayerPath, layerItem->name(), providerKey, layers, styles, format, crs );
}

if ( !layer || !layer->isValid() )
{
qDebug( "No layer" );
delete layer;
return;
}

// add layer to the application
QgsMapLayerRegistry::instance()->addMapLayer( layer );
}

void QgsBrowserDockWidget::showContextMenu( const QPoint & pt )
@@ -184,6 +184,13 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WFlags fl ) :

spinBoxAttrTableRowCache->setValue( settings.value( "/qgis/attributeTableRowCache", 10000 ).toInt() );

// set the prompt for raster sublayers
cmbPromptRasterSublayers->clear();
cmbPromptRasterSublayers->addItem( tr( "Always" ) );
cmbPromptRasterSublayers->addItem( tr( "If needed" ) ); //this means, prompt if there are sublayers but no band in the main dataset
cmbPromptRasterSublayers->addItem( tr( "Never" ) );
cmbPromptRasterSublayers->setCurrentIndex( settings.value( "/qgis/promptForRasterSublayers", 0 ).toInt() );

// set the display update threshold
spinBoxUpdateThreshold->setValue( settings.value( "/Map/updateThreshold" ).toInt() );
//set the default projection behaviour radio buttongs
@@ -600,6 +607,7 @@ void QgsOptions::saveOptions()
settings.setValue( "/qgis/dockAttributeTable", cbxAttributeTableDocked->isChecked() );
settings.setValue( "/qgis/attributeTableBehaviour", cmbAttrTableBehaviour->currentIndex() );
settings.setValue( "/qgis/attributeTableRowCache", spinBoxAttrTableRowCache->value() );
settings.setValue( "/qgis/promptForRasterSublayers", cmbPromptRasterSublayers->currentIndex() );
settings.setValue( "/qgis/dockIdentifyResults", cbxIdentifyResultsDocked->isChecked() );
settings.setValue( "/qgis/dockSnapping", cbxSnappingOptionsDocked->isChecked() );
settings.setValue( "/qgis/addPostgisDC", cbxAddPostgisDC->isChecked() );
@@ -101,6 +101,8 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
if ( !hDS )
return 0;

QStringList sublayers = QgsGdalProvider::subLayers( hDS );

GDALClose( hDS );

QgsDebugMsg( "GdalDataset opened " + thePath );
@@ -109,8 +111,28 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
QString uri = thePath;

QgsLayerItem * item = new QgsGdalLayerItem( parentItem, name, thePath, uri );

QgsDataItem * childItem = NULL;
GDALDatasetH hChildDS = NULL;

if ( item && sublayers.count() > 1 )
{
QgsDebugMsg( QString( "dataItem() got %1 sublayers" ).arg( sublayers.count() ) );
for ( int i = 0; i < sublayers.count(); i++ )
{
hChildDS = GDALOpen( TO8F( sublayers[i] ), GA_ReadOnly );
if ( hChildDS )
{
GDALClose( hChildDS );
QgsDebugMsg( QString( "add child #%1 - %2" ).arg( i ).arg( sublayers[i] ) );
childItem = new QgsGdalLayerItem( item, sublayers[i], thePath + "/" + sublayers[i], sublayers[i] );
if ( childItem )
item->addChildItem( childItem );
}
}
}

return item;
}
return 0;
}

0 comments on commit 73afb0f

Please sign in to comment.