The following commit has been merged in the master branch:
commit d8c8a6768f43614ac099a9a0dfeb610ed26527e2
Author: Etienne Tourigny <etourigny....@gmail.com>
Date:   Fri Mar 9 10:30:53 2012 -0300

    QgisApp::askUserForGDALSublayers : fix order of new layers in the layers 
dock ; add an option to load all sublayers of a GDAL raster with subdatasets

diff --git a/src/app/qgisapp.cpp b/src/app/qgisapp.cpp
index dd6ee6c..5c28856 100644
--- a/src/app/qgisapp.cpp
+++ b/src/app/qgisapp.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.
diff --git a/src/app/qgisapp.h b/src/app/qgisapp.h
index c56828b..8b54096 100644
--- a/src/app/qgisapp.h
+++ b/src/app/qgisapp.h
@@ -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 );
diff --git a/src/app/qgsoptions.cpp b/src/app/qgsoptions.cpp
index 40234f3..dc49ff8 100644
--- a/src/app/qgsoptions.cpp
+++ b/src/app/qgsoptions.cpp
@@ -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

-- 
The Quantum GIS in Debian project

_______________________________________________
Pkg-grass-devel mailing list
Pkg-grass-devel@lists.alioth.debian.org
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/pkg-grass-devel

Reply via email to