Skip to content

Commit d8c8a67

Browse files
etienneskyjef-n
authored andcommitted
QgisApp::askUserForGDALSublayers : fix order of new layers in the layers dock ; add an option to load all sublayers of a GDAL raster with subdatasets
1 parent 2272639 commit d8c8a67

File tree

3 files changed

+44
-16
lines changed

3 files changed

+44
-16
lines changed

src/app/qgisapp.cpp

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2262,15 +2262,23 @@ bool QgisApp::addVectorLayers( QStringList const & theLayerQStringList, const QS
22622262
return true;
22632263
} // QgisApp::addVectorLayer()
22642264

2265+
// present a dialog to choose GDAL raster sublayers
22652266
void QgisApp::askUserForGDALSublayers( QgsRasterLayer *layer )
22662267
{
2267-
if ( !layer )
2268+
if ( !layer || layer->subLayers().size() < 1 )
22682269
return;
22692270

22702271
QStringList sublayers = layer->subLayers();
2271-
22722272
QgsDebugMsg( "sublayers:\n " + sublayers.join( " \n" ) + "\n" );
22732273

2274+
// if promptLayers=Load all, load all sublayers without prompting
2275+
QSettings settings;
2276+
if ( settings.value( "/qgis/promptForRasterSublayers", 1 ).toInt() == 3 )
2277+
{
2278+
loadGDALSublayers( layer->source(), sublayers );
2279+
return;
2280+
}
2281+
22742282
// We initialize a selection dialog and display it.
22752283
QgsOGRSublayersDialog chooseSublayersDialog( this );
22762284
chooseSublayersDialog.setWindowTitle( tr( "Select raster layers to add..." ) );
@@ -2285,19 +2293,11 @@ void QgisApp::askUserForGDALSublayers( QgsRasterLayer *layer )
22852293

22862294
if ( chooseSublayersDialog.exec() )
22872295
{
2288-
foreach( QString path, chooseSublayersDialog.getSelection() )
2289-
{
2290-
QString name = path;
2291-
name.replace( layer->source(), QFileInfo( layer->source() ).completeBaseName() );
2292-
QgsRasterLayer *rlayer = new QgsRasterLayer( path, name );
2293-
if ( rlayer && rlayer->isValid() )
2294-
{
2295-
addRasterLayer( rlayer );
2296-
}
2297-
}
2296+
loadGDALSublayers( layer->source(), chooseSublayersDialog.getSelection() );
22982297
}
22992298
}
23002299

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

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

2313-
return promptLayers == 0 || ( promptLayers == 1 && layer->bandCount() == 0 );
2310+
// return true if promptLayers=Always or if promptLayers!=Never and there are no bands
2311+
return promptLayers == 0 || ( promptLayers != 2 && layer->bandCount() == 0 );
23142312
}
23152313

2314+
// This method will load with GDAL the layers in parameter.
2315+
// It is normally triggered by the sublayer selection dialog.
2316+
void QgisApp::loadGDALSublayers( QString uri, QStringList list )
2317+
{
2318+
QString path, name;
2319+
QgsRasterLayer *subLayer = NULL;
2320+
2321+
//add layers in reverse order so they appear in the right order in the layer dock
2322+
for ( int i = list.size() - 1; i >= 0 ; i-- )
2323+
{
2324+
path = list[i];
2325+
// shorten name by replacing complete path with filename
2326+
name = path;
2327+
name.replace( uri, QFileInfo( uri ).completeBaseName() );
2328+
subLayer = new QgsRasterLayer( path, name );
2329+
if ( subLayer )
2330+
{
2331+
if ( subLayer->isValid() )
2332+
addRasterLayer( subLayer );
2333+
else
2334+
delete subLayer;
2335+
}
2336+
}
2337+
}
23162338

23172339
// This method is the method that does the real job. If the layer given in
23182340
// parameter is NULL, then the method tries to act on the activeLayer.

src/app/qgisapp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
422422
void editPaste( QgsMapLayer * destinationLayer = 0 );
423423

424424
void loadOGRSublayers( QString layertype, QString uri, QStringList list );
425+
void loadGDALSublayers( QString uri, QStringList list );
425426

426427
/**Deletes the selected attributes for the currently selected vector layer*/
427428
void deleteSelected( QgsMapLayer *layer = 0, QWidget* parent = 0 );

src/app/qgsoptions.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,15 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WFlags fl ) :
185185
spinBoxAttrTableRowCache->setValue( settings.value( "/qgis/attributeTableRowCache", 10000 ).toInt() );
186186

187187
// set the prompt for raster sublayers
188+
// 0 = Always -> always ask (if there are existing sublayers)
189+
// 1 = If needed -> ask if layer has no bands, but has sublayers
190+
// 2 = Never -> never prompt, will not load anything
191+
// 4 = Load all -> never prompt, but load all sublayers
188192
cmbPromptRasterSublayers->clear();
189193
cmbPromptRasterSublayers->addItem( tr( "Always" ) );
190194
cmbPromptRasterSublayers->addItem( tr( "If needed" ) ); //this means, prompt if there are sublayers but no band in the main dataset
191195
cmbPromptRasterSublayers->addItem( tr( "Never" ) );
196+
cmbPromptRasterSublayers->addItem( tr( "Load all" ) );
192197
cmbPromptRasterSublayers->setCurrentIndex( settings.value( "/qgis/promptForRasterSublayers", 0 ).toInt() );
193198

194199
// set the display update threshold

0 commit comments

Comments
 (0)