Skip to content
Permalink
Browse files
QgisApp::askUserForGDALSublayers : fix order of new layers in the lay…
…ers dock ; add an option to load all sublayers of a GDAL raster with subdatasets
  • Loading branch information
etiennesky authored and jef-n committed Mar 9, 2012
1 parent 2272639 commit d8c8a6768f43614ac099a9a0dfeb610ed26527e2
Showing with 44 additions and 16 deletions.
  1. +38 −16 src/app/qgisapp.cpp
  2. +1 −0 src/app/qgisapp.h
  3. +5 −0 src/app/qgsoptions.cpp
@@ -2262,15 +2262,23 @@ bool QgisApp::addVectorLayers( QStringList const & theLayerQStringList, const QS
return true;
} // QgisApp::addVectorLayer()

// present a dialog to choose GDAL raster sublayers
void QgisApp::askUserForGDALSublayers( QgsRasterLayer *layer )
{
if ( !layer )
if ( !layer || layer->subLayers().size() < 1 )
return;

QStringList sublayers = layer->subLayers();

QgsDebugMsg( "sublayers:\n " + sublayers.join( " \n" ) + "\n" );

// if promptLayers=Load all, load all sublayers without prompting
QSettings settings;
if ( settings.value( "/qgis/promptForRasterSublayers", 1 ).toInt() == 3 )
{
loadGDALSublayers( layer->source(), sublayers );
return;
}

// We initialize a selection dialog and display it.
QgsOGRSublayersDialog chooseSublayersDialog( this );
chooseSublayersDialog.setWindowTitle( tr( "Select raster layers to add..." ) );
@@ -2285,19 +2293,11 @@ void QgisApp::askUserForGDALSublayers( QgsRasterLayer *layer )

if ( chooseSublayersDialog.exec() )
{
foreach( QString path, chooseSublayersDialog.getSelection() )
{
QString name = path;
name.replace( layer->source(), QFileInfo( layer->source() ).completeBaseName() );
QgsRasterLayer *rlayer = new QgsRasterLayer( path, name );
if ( rlayer && rlayer->isValid() )
{
addRasterLayer( rlayer );
}
}
loadGDALSublayers( layer->source(), chooseSublayersDialog.getSelection() );
}
}

// should the GDAL sublayers dialog should be presented to the user?
bool QgisApp::shouldAskUserForGDALSublayers( QgsRasterLayer *layer )
{
// return false if layer is empty or raster has no sublayers
@@ -2306,13 +2306,35 @@ bool QgisApp::shouldAskUserForGDALSublayers( QgsRasterLayer *layer )

QSettings settings;
int promptLayers = settings.value( "/qgis/promptForRasterSublayers", 1 ).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 );
// return true if promptLayers=Always or if promptLayers!=Never and there are no bands
return promptLayers == 0 || ( promptLayers != 2 && layer->bandCount() == 0 );
}

// This method will load with GDAL the layers in parameter.
// It is normally triggered by the sublayer selection dialog.
void QgisApp::loadGDALSublayers( QString uri, QStringList list )
{
QString path, name;
QgsRasterLayer *subLayer = NULL;

//add layers in reverse order so they appear in the right order in the layer dock
for ( int i = list.size() - 1; i >= 0 ; i-- )
{
path = list[i];
// shorten name by replacing complete path with filename
name = path;
name.replace( uri, QFileInfo( uri ).completeBaseName() );
subLayer = new QgsRasterLayer( path, name );
if ( subLayer )
{
if ( subLayer->isValid() )
addRasterLayer( subLayer );
else
delete subLayer;
}
}
}

// 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.
@@ -422,6 +422,7 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
void editPaste( QgsMapLayer * destinationLayer = 0 );

void loadOGRSublayers( QString layertype, QString uri, QStringList list );
void loadGDALSublayers( QString uri, QStringList list );

/**Deletes the selected attributes for the currently selected vector layer*/
void deleteSelected( QgsMapLayer *layer = 0, QWidget* parent = 0 );
@@ -185,10 +185,15 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WFlags fl ) :
spinBoxAttrTableRowCache->setValue( settings.value( "/qgis/attributeTableRowCache", 10000 ).toInt() );

// set the prompt for raster sublayers
// 0 = Always -> always ask (if there are existing sublayers)
// 1 = If needed -> ask if layer has no bands, but has sublayers
// 2 = Never -> never prompt, will not load anything
// 4 = Load all -> never prompt, but load all 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->addItem( tr( "Load all" ) );
cmbPromptRasterSublayers->setCurrentIndex( settings.value( "/qgis/promptForRasterSublayers", 0 ).toInt() );

// set the display update threshold

0 comments on commit d8c8a67

Please sign in to comment.