[QGIS Commit] r14064 - in trunk/qgis/src: app app/ogr core/raster ui

svn_qgis at osgeo.org svn_qgis at osgeo.org
Wed Aug 11 15:00:16 EDT 2010


Author: jef
Date: 2010-08-11 19:00:16 +0000 (Wed, 11 Aug 2010)
New Revision: 14064

Modified:
   trunk/qgis/src/app/ogr/qgsogrsublayersdialog.cpp
   trunk/qgis/src/app/ogr/qgsogrsublayersdialog.h
   trunk/qgis/src/app/qgisapp.cpp
   trunk/qgis/src/app/qgisapp.h
   trunk/qgis/src/core/raster/qgsrasterlayer.cpp
   trunk/qgis/src/core/raster/qgsrasterlayer.h
   trunk/qgis/src/ui/qgsogrsublayersdialogbase.ui
Log:
apply #1040

Modified: trunk/qgis/src/app/ogr/qgsogrsublayersdialog.cpp
===================================================================
--- trunk/qgis/src/app/ogr/qgsogrsublayersdialog.cpp	2010-08-11 18:59:33 UTC (rev 14063)
+++ trunk/qgis/src/app/ogr/qgsogrsublayersdialog.cpp	2010-08-11 19:00:16 UTC (rev 14064)
@@ -42,14 +42,14 @@
   return list;
 }
 
-void QgsOGRSublayersDialog::populateLayerTable( QStringList theList )
+void QgsOGRSublayersDialog::populateLayerTable( QStringList theList, QString delim )
 {
   for ( int i = 0; i < theList.size(); i++ )
   {
-    QString ligne = theList.at( i );
-    QStringList elements = ligne.split( ":" );
+    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 ) );
   }
 }

Modified: trunk/qgis/src/app/ogr/qgsogrsublayersdialog.h
===================================================================
--- trunk/qgis/src/app/ogr/qgsogrsublayersdialog.h	2010-08-11 18:59:33 UTC (rev 14063)
+++ trunk/qgis/src/app/ogr/qgsogrsublayersdialog.h	2010-08-11 19:00:16 UTC (rev 14064)
@@ -19,18 +19,19 @@
 
 #include <QDialog>
 #include <ui_qgsogrsublayersdialogbase.h>
+#include "qgscontexthelp.h"
 
-
-
 class QgsOGRSublayersDialog : public QDialog, private Ui::QgsOGRSublayersDialogBase
 {
     Q_OBJECT
   public:
     QgsOGRSublayersDialog( QWidget* parent = 0, Qt::WFlags fl = 0 );
     ~QgsOGRSublayersDialog();
-    void populateLayerTable( QStringList theList );
+    void populateLayerTable( QStringList theList, QString delim = ":" );
     QStringList getSelection();
 
+  public slots:
+    void on_buttonBox_helpRequested() { QgsContextHelp::run( metaObject()->className() ); }
 };
 
 #endif

Modified: trunk/qgis/src/app/qgisapp.cpp
===================================================================
--- trunk/qgis/src/app/qgisapp.cpp	2010-08-11 18:59:33 UTC (rev 14063)
+++ trunk/qgis/src/app/qgisapp.cpp	2010-08-11 19:00:16 UTC (rev 14064)
@@ -2621,7 +2621,7 @@
       // sublayers selection dialog so the user can select the sublayers to actually load.
       if ( sublayers.count() > 1 )
       {
-        askUserForSublayers( layer );
+        askUserForOGRSublayers( 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.
@@ -2674,18 +2674,49 @@
   return true;
 } // QgisApp::addVectorLayer()
 
+void QgisApp::askUserForGDALSublayers( QgsRasterLayer *layer )
+{
+  if ( !layer )
+    return;
+
+  QStringList sublayers = layer->subLayers();
+
+  QgsDebugMsg( "sublayers:\n  " + sublayers.join( "  \n" ) + "\n" );
+
+  // We initialize a selection dialog and display it.
+  QgsOGRSublayersDialog chooseSublayersDialog( this );
+  chooseSublayersDialog.setWindowTitle( tr( "Select raster layers to add..." ) );
+
+  QStringList layers;
+  for ( int i = 0; i < sublayers.size(); i++ )
+  {
+    layers << QString( "%1|%2|1|%3" ).arg( i ).arg( sublayers[i] ).arg( tr( "Raster" ) );
+  }
+
+  chooseSublayersDialog.populateLayerTable( layers, "|" );
+
+  if ( chooseSublayersDialog.exec() )
+  {
+    foreach( QString layer, chooseSublayersDialog.getSelection() )
+    {
+      QgsRasterLayer *rlayer = new QgsRasterLayer( layer, layer );
+      if ( rlayer && rlayer->isValid() )
+      {
+        addRasterLayer( rlayer );
+      }
+    }
+  }
+}
+
 // 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::askUserForSublayers( QgsVectorLayer *layer )
+void QgisApp::askUserForOGRSublayers( QgsVectorLayer *layer )
 {
   if ( layer == NULL )
   {
-    if ( activeLayer() == NULL || activeLayer()->type() != QgsMapLayer::VectorLayer )
+    layer = qobject_cast<QgsVectorLayer *>( activeLayer() );
+    if ( !layer || layer->dataProvider()->name() != "ogr" )
       return;
-
-    layer = ( QgsVectorLayer* ) activeLayer();
-    if ( layer->dataProvider()->name() != "ogr" )
-      return;
   }
 
   QStringList sublayers = layer->dataProvider()->subLayers();
@@ -2693,6 +2724,7 @@
 
   // We initialize a selection dialog and display it.
   QgsOGRSublayersDialog chooseSublayersDialog( this );
+  chooseSublayersDialog.setWindowTitle( tr( "Select vector layers to add..." ) );
   chooseSublayersDialog.populateLayerTable( sublayers );
 
   if ( chooseSublayersDialog.exec() )
@@ -6350,16 +6382,28 @@
 
       // create the layer
       QgsRasterLayer *layer = new QgsRasterLayer( *myIterator, myBaseNameQString );
+      QStringList sublayers = layer->subLayers();
 
-      addRasterLayer( layer );
+      if ( sublayers.size() > 0 )
+      {
+        askUserForGDALSublayers( layer );
 
-      //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 coverate,
+        // 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
+      {
+        addRasterLayer( layer );
 
-      if ( myBaseNameQString.toLower().endsWith( ".adf" ) )
-      {
-        break;
+        //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 coverate,
+
+        if ( myBaseNameQString.toLower().endsWith( ".adf" ) )
+        {
+          break;
+        }
       }
     }
     else

Modified: trunk/qgis/src/app/qgisapp.h
===================================================================
--- trunk/qgis/src/app/qgisapp.h	2010-08-11 18:59:33 UTC (rev 14063)
+++ trunk/qgis/src/app/qgisapp.h	2010-08-11 19:00:16 UTC (rev 14064)
@@ -765,10 +765,10 @@
     void initializationCompleted();
 
   private:
-    /** This method will open a dialog so the user can select the sublayers
-    * to load
+    /** This method will open a dialog so the user can select the sublayers to load
     */
-    void askUserForSublayers( QgsVectorLayer *layer );
+    void askUserForOGRSublayers( QgsVectorLayer *layer );
+    void askUserForGDALSublayers( QgsRasterLayer *layer );
     /** Add a raster layer to the map (passed in as a ptr).
      * It won't force a refresh.
      */

Modified: trunk/qgis/src/core/raster/qgsrasterlayer.cpp
===================================================================
--- trunk/qgis/src/core/raster/qgsrasterlayer.cpp	2010-08-11 18:59:33 UTC (rev 14063)
+++ trunk/qgis/src/core/raster/qgsrasterlayer.cpp	2010-08-11 19:00:16 UTC (rev 14064)
@@ -444,10 +444,8 @@
 /**
  * This helper checks to see whether the file name appears to be a valid raster file name
  */
-bool QgsRasterLayer::isValidRasterFileName( QString const & theFileNameQString,
-    QString & retErrMsg )
+bool QgsRasterLayer::isValidRasterFileName( QString const & theFileNameQString, QString & retErrMsg )
 {
-
   GDALDatasetH myDataset;
   registerGdalDrivers();
 
@@ -463,10 +461,15 @@
   }
   else if ( GDALGetRasterCount( myDataset ) == 0 )
   {
-    GDALClose( myDataset );
-    myDataset = NULL;
-    retErrMsg = tr( "This raster file has no bands and is invalid as a raster layer." );
-    return false;
+    QStringList layers = subLayers( myDataset );
+    if ( layers.size() == 0 )
+    {
+      GDALClose( myDataset );
+      myDataset = NULL;
+      retErrMsg = tr( "This raster file has no bands and is invalid as a raster layer." );
+      return false;
+    }
+    return true;
   }
   else
   {
@@ -476,10 +479,8 @@
 }
 
 bool QgsRasterLayer::isValidRasterFileName( QString const & theFileNameQString )
-
 {
   QString retErrMsg;
-
   return isValidRasterFileName( theFileNameQString, retErrMsg );
 }
 
@@ -2626,7 +2627,7 @@
       }
       else
       {
-        QgsDebugMsg( "band " + QString::number( i ) + "has no metadata" );
+        QgsDebugMsg( "band " + QString::number( i ) + " has no metadata" );
       }
 
       char ** GDALcategories = GDALGetRasterCategoryNames( gdalBand );
@@ -3604,18 +3605,40 @@
   emit statusChanged( theMessage );
 }
 
+QStringList QgsRasterLayer::subLayers( GDALDatasetH dataset )
+{
+  QStringList subLayers;
+
+  char **metadata = GDALGetMetadata( dataset, "SUBDATASETS" );
+  if ( metadata )
+  {
+    for ( int i = 0; metadata[i] != NULL; i++ )
+    {
+      QString layer = QString::fromUtf8( metadata[i] );
+
+      int pos = layer.indexOf( "_NAME=" );
+      if ( pos >= 0 )
+      {
+        subLayers << layer.mid( pos + 6 );
+      }
+    }
+  }
+
+  QgsDebugMsg( "sublayers:\n  " + subLayers.join( "\n  " ) );
+
+  return subLayers;
+}
+
 QStringList QgsRasterLayer::subLayers() const
 {
-
   if ( mDataProvider )
   {
     return mDataProvider->subLayers();
   }
   else
   {
-    return QStringList();   // Empty
+    return subLayers( mGdalDataset );
   }
-
 }
 
 void QgsRasterLayer::thumbnailAsPixmap( QPixmap * theQPixmap )
@@ -5258,6 +5281,13 @@
     GDALReferenceDataset( mGdalDataset );
   }
 
+  if ( subLayers().size() > 0 )
+  {
+    // just to get the sublayers
+    mValid = false;
+    return true;
+  }
+
   //check f this file has pyramids
   GDALRasterBandH myGDALBand = GDALGetRasterBand( mGdalDataset, 1 ); //just use the first band
   if ( myGDALBand == NULL )
@@ -5270,15 +5300,9 @@
     mValid = false;
     return false;
   }
-  if ( GDALGetOverviewCount( myGDALBand ) > 0 )
-  {
-    mHasPyramids = true;
-  }
-  else
-  {
-    mHasPyramids = false;
-  }
 
+  mHasPyramids = GDALGetOverviewCount( myGDALBand ) > 0;
+
   //populate the list of what pyramids exist
   buildPyramidList();
 

Modified: trunk/qgis/src/core/raster/qgsrasterlayer.h
===================================================================
--- trunk/qgis/src/core/raster/qgsrasterlayer.h	2010-08-11 18:59:33 UTC (rev 14063)
+++ trunk/qgis/src/core/raster/qgsrasterlayer.h	2010-08-11 19:00:16 UTC (rev 14064)
@@ -240,7 +240,7 @@
       PalettedMultiBandColor,         // currently not supported
       MultiBandSingleGandGray,        // a layer containing 2 or more bands, but a single band drawn as a range of gray colors
       //added in 1.6 to fix naming glitch
-      MultiBandSingleBandGray=MultiBandSingleGandGray, // a layer containing 2 or more bands, but a single band drawn as a range of gray colors
+      MultiBandSingleBandGray = MultiBandSingleGandGray, // a layer containing 2 or more bands, but a single band drawn as a range of gray colors
       MultiBandSingleBandPseudoColor, //a layer containing 2 or more bands, but a single band drawn using a pseudocolor algorithm
       MultiBandColor                  //a layer containing 2 or more bands, mapped to RGB color space.
       //In the case of a multiband with only two bands, one band will be mapped to more than one color.
@@ -286,9 +286,10 @@
      *  returned in retError.
      */
     static bool isValidRasterFileName( const QString & theFileNameQString, QString &retError );
-
     static bool isValidRasterFileName( const QString & theFileNameQString );
+    static QStringList subLayers( GDALDatasetH dataset );
 
+
     /** Return time stamp for given file name */
     static QDateTime lastModified( const QString &  name );
 

Modified: trunk/qgis/src/ui/qgsogrsublayersdialogbase.ui
===================================================================
--- trunk/qgis/src/ui/qgsogrsublayersdialogbase.ui	2010-08-11 18:59:33 UTC (rev 14063)
+++ trunk/qgis/src/ui/qgsogrsublayersdialogbase.ui	2010-08-11 19:00:16 UTC (rev 14064)
@@ -1,103 +1,45 @@
-<ui version="4.0" >
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
  <class>QgsOGRSublayersDialogBase</class>
- <widget class="QDialog" name="QgsOGRSublayersDialogBase" >
-  <property name="geometry" >
+ <widget class="QDialog" name="QgsOGRSublayersDialogBase">
+  <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>584</width>
-    <height>535</height>
+    <height>236</height>
    </rect>
   </property>
-  <property name="windowTitle" >
-   <string>Select OGR layers to load</string>
+  <property name="windowTitle">
+   <string>Select layers to load</string>
   </property>
-  <property name="windowIcon" >
-   <iconset>../../../qgis_1.0.0/src/plugins/ogrsublayers</iconset>
-  </property>
-  <layout class="QGridLayout" >
-   <property name="leftMargin" >
+  <layout class="QGridLayout">
+   <property name="margin">
     <number>9</number>
    </property>
-   <property name="topMargin" >
-    <number>9</number>
-   </property>
-   <property name="rightMargin" >
-    <number>9</number>
-   </property>
-   <property name="bottomMargin" >
-    <number>9</number>
-   </property>
-   <property name="horizontalSpacing" >
+   <property name="spacing">
     <number>6</number>
    </property>
-   <property name="verticalSpacing" >
-    <number>6</number>
-   </property>
-   <item row="0" column="0" >
-    <widget class="QLabel" name="txtHeading" >
-     <property name="sizePolicy" >
-      <sizepolicy vsizetype="Minimum" hsizetype="Preferred" >
-       <horstretch>0</horstretch>
-       <verstretch>0</verstretch>
-      </sizepolicy>
-     </property>
-     <property name="font" >
-      <font>
-       <family>Sans Serif</family>
-       <pointsize>24</pointsize>
-       <weight>75</weight>
-       <italic>false</italic>
-       <bold>true</bold>
-       <underline>false</underline>
-       <strikeout>false</strikeout>
-      </font>
-     </property>
-     <property name="text" >
-      <string>Sub layers list</string>
-     </property>
-     <property name="alignment" >
-      <set>Qt::AlignCenter</set>
-     </property>
-    </widget>
-   </item>
-   <item row="8" column="0" >
-    <widget class="QDialogButtonBox" name="buttonBox" >
-     <property name="orientation" >
+   <item row="6" column="0">
+    <widget class="QDialogButtonBox" name="buttonBox">
+     <property name="orientation">
       <enum>Qt::Horizontal</enum>
      </property>
-     <property name="standardButtons" >
-      <set>QDialogButtonBox::Cancel|QDialogButtonBox::NoButton|QDialogButtonBox::Ok</set>
+     <property name="standardButtons">
+      <set>QDialogButtonBox::Cancel|QDialogButtonBox::Help|QDialogButtonBox::Ok</set>
      </property>
     </widget>
    </item>
-   <item row="1" column="0" >
-    <widget class="QTextEdit" name="textEdit" >
-     <property name="html" >
-      <string>&lt;html>&lt;head>&lt;meta name="qrichtext" content="1" />&lt;style type="text/css">
-p, li { white-space: pre-wrap; }
-&lt;/style>&lt;/head>&lt;body style=" font-family:'DejaVu Sans'; font-size:9pt; font-weight:400; font-style:normal;">
-&lt;p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Sans Serif';">This is the list of all layers available in the datasource of the active layer. You can select the layers to load. The layers will be loaded when you press "OK".&lt;/p>
-&lt;p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Sans Serif';">&lt;/p>
-&lt;p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Sans Serif';">The layer name is format dependent. Consult the OGR documentation or the documentation of your data format to determine the nature of the included information.&lt;/p>
-&lt;p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Sans Serif';">&lt;/p>
-&lt;p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Sans Serif';">&lt;span style=" font-weight:600;">Be advised: &lt;/span>selecting an already opened layer will not generate an error message and the layer will end up loaded twice!&lt;/p>&lt;/body>&lt;/html></string>
-     </property>
-    </widget>
-   </item>
-   <item row="2" column="0" >
-    <widget class="QTreeWidget" name="layersTable" >
-     <property name="windowModality" >
-      <enum>Qt::NonModal</enum>
-     </property>
-     <property name="selectionMode" >
+   <item row="0" column="0">
+    <widget class="QTreeWidget" name="layersTable">
+     <property name="selectionMode">
       <enum>QAbstractItemView::ExtendedSelection</enum>
      </property>
-     <property name="selectionBehavior" >
+     <property name="selectionBehavior">
       <enum>QAbstractItemView::SelectRows</enum>
      </property>
      <column>
-      <property name="text" >
+      <property name="text">
        <string>1</string>
       </property>
      </column>
@@ -105,7 +47,7 @@
    </item>
   </layout>
  </widget>
- <layoutdefault spacing="6" margin="11" />
+ <layoutdefault spacing="6" margin="11"/>
  <resources/>
  <connections>
   <connection>
@@ -114,11 +56,11 @@
    <receiver>QgsOGRSublayersDialogBase</receiver>
    <slot>accept()</slot>
    <hints>
-    <hint type="sourcelabel" >
+    <hint type="sourcelabel">
      <x>446</x>
      <y>508</y>
     </hint>
-    <hint type="destinationlabel" >
+    <hint type="destinationlabel">
      <x>351</x>
      <y>473</y>
     </hint>
@@ -130,11 +72,11 @@
    <receiver>QgsOGRSublayersDialogBase</receiver>
    <slot>reject()</slot>
    <hints>
-    <hint type="sourcelabel" >
+    <hint type="sourcelabel">
      <x>541</x>
      <y>507</y>
     </hint>
-    <hint type="destinationlabel" >
+    <hint type="destinationlabel">
      <x>503</x>
      <y>434</y>
     </hint>



More information about the QGIS-commit mailing list