Skip to content

Commit d72cfb4

Browse files
author
jef
committedAug 11, 2010
apply #1040
git-svn-id: http://svn.osgeo.org/qgis/trunk@14064 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 69b047d commit d72cfb4

File tree

7 files changed

+142
-130
lines changed

7 files changed

+142
-130
lines changed
 

‎src/app/ogr/qgsogrsublayersdialog.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ QStringList QgsOGRSublayersDialog::getSelection()
4242
return list;
4343
}
4444

45-
void QgsOGRSublayersDialog::populateLayerTable( QStringList theList )
45+
void QgsOGRSublayersDialog::populateLayerTable( QStringList theList, QString delim )
4646
{
4747
for ( int i = 0; i < theList.size(); i++ )
4848
{
49-
QString ligne = theList.at( i );
50-
QStringList elements = ligne.split( ":" );
49+
QString line = theList.at( i );
50+
QStringList elements = line.split( delim );
5151
QStringList item = QStringList();
5252
item << elements.at( 0 ) << elements.at( 1 ) << elements.at( 2 ) << elements.at( 3 );
53-
layersTable -> addTopLevelItem( new QTreeWidgetItem( item ) );
53+
layersTable->addTopLevelItem( new QTreeWidgetItem( item ) );
5454
}
5555
}

‎src/app/ogr/qgsogrsublayersdialog.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,19 @@
1919

2020
#include <QDialog>
2121
#include <ui_qgsogrsublayersdialogbase.h>
22-
23-
22+
#include "qgscontexthelp.h"
2423

2524
class QgsOGRSublayersDialog : public QDialog, private Ui::QgsOGRSublayersDialogBase
2625
{
2726
Q_OBJECT
2827
public:
2928
QgsOGRSublayersDialog( QWidget* parent = 0, Qt::WFlags fl = 0 );
3029
~QgsOGRSublayersDialog();
31-
void populateLayerTable( QStringList theList );
30+
void populateLayerTable( QStringList theList, QString delim = ":" );
3231
QStringList getSelection();
3332

33+
public slots:
34+
void on_buttonBox_helpRequested() { QgsContextHelp::run( metaObject()->className() ); }
3435
};
3536

3637
#endif

‎src/app/qgisapp.cpp

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2621,7 +2621,7 @@ bool QgisApp::addVectorLayers( QStringList const & theLayerQStringList, const QS
26212621
// sublayers selection dialog so the user can select the sublayers to actually load.
26222622
if ( sublayers.count() > 1 )
26232623
{
2624-
askUserForSublayers( layer );
2624+
askUserForOGRSublayers( layer );
26252625

26262626
// The first layer loaded is not useful in that case. The user can select it in
26272627
// the list if he wants to load it.
@@ -2674,17 +2674,48 @@ bool QgisApp::addVectorLayers( QStringList const & theLayerQStringList, const QS
26742674
return true;
26752675
} // QgisApp::addVectorLayer()
26762676

2677+
void QgisApp::askUserForGDALSublayers( QgsRasterLayer *layer )
2678+
{
2679+
if ( !layer )
2680+
return;
2681+
2682+
QStringList sublayers = layer->subLayers();
2683+
2684+
QgsDebugMsg( "sublayers:\n " + sublayers.join( " \n" ) + "\n" );
2685+
2686+
// We initialize a selection dialog and display it.
2687+
QgsOGRSublayersDialog chooseSublayersDialog( this );
2688+
chooseSublayersDialog.setWindowTitle( tr( "Select raster layers to add..." ) );
2689+
2690+
QStringList layers;
2691+
for ( int i = 0; i < sublayers.size(); i++ )
2692+
{
2693+
layers << QString( "%1|%2|1|%3" ).arg( i ).arg( sublayers[i] ).arg( tr( "Raster" ) );
2694+
}
2695+
2696+
chooseSublayersDialog.populateLayerTable( layers, "|" );
2697+
2698+
if ( chooseSublayersDialog.exec() )
2699+
{
2700+
foreach( QString layer, chooseSublayersDialog.getSelection() )
2701+
{
2702+
QgsRasterLayer *rlayer = new QgsRasterLayer( layer, layer );
2703+
if ( rlayer && rlayer->isValid() )
2704+
{
2705+
addRasterLayer( rlayer );
2706+
}
2707+
}
2708+
}
2709+
}
2710+
26772711
// This method is the method that does the real job. If the layer given in
26782712
// parameter is NULL, then the method tries to act on the activeLayer.
2679-
void QgisApp::askUserForSublayers( QgsVectorLayer *layer )
2713+
void QgisApp::askUserForOGRSublayers( QgsVectorLayer *layer )
26802714
{
26812715
if ( layer == NULL )
26822716
{
2683-
if ( activeLayer() == NULL || activeLayer()->type() != QgsMapLayer::VectorLayer )
2684-
return;
2685-
2686-
layer = ( QgsVectorLayer* ) activeLayer();
2687-
if ( layer->dataProvider()->name() != "ogr" )
2717+
layer = qobject_cast<QgsVectorLayer *>( activeLayer() );
2718+
if ( !layer || layer->dataProvider()->name() != "ogr" )
26882719
return;
26892720
}
26902721

@@ -2693,6 +2724,7 @@ void QgisApp::askUserForSublayers( QgsVectorLayer *layer )
26932724

26942725
// We initialize a selection dialog and display it.
26952726
QgsOGRSublayersDialog chooseSublayersDialog( this );
2727+
chooseSublayersDialog.setWindowTitle( tr( "Select vector layers to add..." ) );
26962728
chooseSublayersDialog.populateLayerTable( sublayers );
26972729

26982730
if ( chooseSublayersDialog.exec() )
@@ -6350,16 +6382,28 @@ bool QgisApp::addRasterLayers( QStringList const &theFileNameQStringList, bool g
63506382

63516383
// create the layer
63526384
QgsRasterLayer *layer = new QgsRasterLayer( *myIterator, myBaseNameQString );
6385+
QStringList sublayers = layer->subLayers();
63536386

6354-
addRasterLayer( layer );
6355-
6356-
//only allow one copy of a ai grid file to be loaded at a
6357-
//time to prevent the user selecting all adfs in 1 dir which
6358-
//actually represent 1 coverate,
6387+
if ( sublayers.size() > 0 )
6388+
{
6389+
askUserForGDALSublayers( layer );
63596390

6360-
if ( myBaseNameQString.toLower().endsWith( ".adf" ) )
6391+
// The first layer loaded is not useful in that case. The user can select it in
6392+
// the list if he wants to load it.
6393+
delete layer;
6394+
}
6395+
else
63616396
{
6362-
break;
6397+
addRasterLayer( layer );
6398+
6399+
//only allow one copy of a ai grid file to be loaded at a
6400+
//time to prevent the user selecting all adfs in 1 dir which
6401+
//actually represent 1 coverate,
6402+
6403+
if ( myBaseNameQString.toLower().endsWith( ".adf" ) )
6404+
{
6405+
break;
6406+
}
63636407
}
63646408
}
63656409
else

‎src/app/qgisapp.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -765,10 +765,10 @@ class QgisApp : public QMainWindow
765765
void initializationCompleted();
766766

767767
private:
768-
/** This method will open a dialog so the user can select the sublayers
769-
* to load
768+
/** This method will open a dialog so the user can select the sublayers to load
770769
*/
771-
void askUserForSublayers( QgsVectorLayer *layer );
770+
void askUserForOGRSublayers( QgsVectorLayer *layer );
771+
void askUserForGDALSublayers( QgsRasterLayer *layer );
772772
/** Add a raster layer to the map (passed in as a ptr).
773773
* It won't force a refresh.
774774
*/

‎src/core/raster/qgsrasterlayer.cpp

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -444,10 +444,8 @@ void QgsRasterLayer::buildSupportedRasterFileFilter( QString & theFileFiltersStr
444444
/**
445445
* This helper checks to see whether the file name appears to be a valid raster file name
446446
*/
447-
bool QgsRasterLayer::isValidRasterFileName( QString const & theFileNameQString,
448-
QString & retErrMsg )
447+
bool QgsRasterLayer::isValidRasterFileName( QString const & theFileNameQString, QString & retErrMsg )
449448
{
450-
451449
GDALDatasetH myDataset;
452450
registerGdalDrivers();
453451

@@ -463,10 +461,15 @@ bool QgsRasterLayer::isValidRasterFileName( QString const & theFileNameQString,
463461
}
464462
else if ( GDALGetRasterCount( myDataset ) == 0 )
465463
{
466-
GDALClose( myDataset );
467-
myDataset = NULL;
468-
retErrMsg = tr( "This raster file has no bands and is invalid as a raster layer." );
469-
return false;
464+
QStringList layers = subLayers( myDataset );
465+
if ( layers.size() == 0 )
466+
{
467+
GDALClose( myDataset );
468+
myDataset = NULL;
469+
retErrMsg = tr( "This raster file has no bands and is invalid as a raster layer." );
470+
return false;
471+
}
472+
return true;
470473
}
471474
else
472475
{
@@ -476,10 +479,8 @@ bool QgsRasterLayer::isValidRasterFileName( QString const & theFileNameQString,
476479
}
477480

478481
bool QgsRasterLayer::isValidRasterFileName( QString const & theFileNameQString )
479-
480482
{
481483
QString retErrMsg;
482-
483484
return isValidRasterFileName( theFileNameQString, retErrMsg );
484485
}
485486

@@ -2626,7 +2627,7 @@ QString QgsRasterLayer::metadata()
26262627
}
26272628
else
26282629
{
2629-
QgsDebugMsg( "band " + QString::number( i ) + "has no metadata" );
2630+
QgsDebugMsg( "band " + QString::number( i ) + " has no metadata" );
26302631
}
26312632

26322633
char ** GDALcategories = GDALGetRasterCategoryNames( gdalBand );
@@ -3604,18 +3605,40 @@ void QgsRasterLayer::showStatusMessage( QString const & theMessage )
36043605
emit statusChanged( theMessage );
36053606
}
36063607

3607-
QStringList QgsRasterLayer::subLayers() const
3608+
QStringList QgsRasterLayer::subLayers( GDALDatasetH dataset )
36083609
{
3610+
QStringList subLayers;
3611+
3612+
char **metadata = GDALGetMetadata( dataset, "SUBDATASETS" );
3613+
if ( metadata )
3614+
{
3615+
for ( int i = 0; metadata[i] != NULL; i++ )
3616+
{
3617+
QString layer = QString::fromUtf8( metadata[i] );
3618+
3619+
int pos = layer.indexOf( "_NAME=" );
3620+
if ( pos >= 0 )
3621+
{
3622+
subLayers << layer.mid( pos + 6 );
3623+
}
3624+
}
3625+
}
36093626

3627+
QgsDebugMsg( "sublayers:\n " + subLayers.join( "\n " ) );
3628+
3629+
return subLayers;
3630+
}
3631+
3632+
QStringList QgsRasterLayer::subLayers() const
3633+
{
36103634
if ( mDataProvider )
36113635
{
36123636
return mDataProvider->subLayers();
36133637
}
36143638
else
36153639
{
3616-
return QStringList(); // Empty
3640+
return subLayers( mGdalDataset );
36173641
}
3618-
36193642
}
36203643

36213644
void QgsRasterLayer::thumbnailAsPixmap( QPixmap * theQPixmap )
@@ -5258,6 +5281,13 @@ bool QgsRasterLayer::readFile( QString const &theFilename )
52585281
GDALReferenceDataset( mGdalDataset );
52595282
}
52605283

5284+
if ( subLayers().size() > 0 )
5285+
{
5286+
// just to get the sublayers
5287+
mValid = false;
5288+
return true;
5289+
}
5290+
52615291
//check f this file has pyramids
52625292
GDALRasterBandH myGDALBand = GDALGetRasterBand( mGdalDataset, 1 ); //just use the first band
52635293
if ( myGDALBand == NULL )
@@ -5270,14 +5300,8 @@ bool QgsRasterLayer::readFile( QString const &theFilename )
52705300
mValid = false;
52715301
return false;
52725302
}
5273-
if ( GDALGetOverviewCount( myGDALBand ) > 0 )
5274-
{
5275-
mHasPyramids = true;
5276-
}
5277-
else
5278-
{
5279-
mHasPyramids = false;
5280-
}
5303+
5304+
mHasPyramids = GDALGetOverviewCount( myGDALBand ) > 0;
52815305

52825306
//populate the list of what pyramids exist
52835307
buildPyramidList();

‎src/core/raster/qgsrasterlayer.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
240240
PalettedMultiBandColor, // currently not supported
241241
MultiBandSingleGandGray, // a layer containing 2 or more bands, but a single band drawn as a range of gray colors
242242
//added in 1.6 to fix naming glitch
243-
MultiBandSingleBandGray=MultiBandSingleGandGray, // a layer containing 2 or more bands, but a single band drawn as a range of gray colors
243+
MultiBandSingleBandGray = MultiBandSingleGandGray, // a layer containing 2 or more bands, but a single band drawn as a range of gray colors
244244
MultiBandSingleBandPseudoColor, //a layer containing 2 or more bands, but a single band drawn using a pseudocolor algorithm
245245
MultiBandColor //a layer containing 2 or more bands, mapped to RGB color space.
246246
//In the case of a multiband with only two bands, one band will be mapped to more than one color.
@@ -286,8 +286,9 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
286286
* returned in retError.
287287
*/
288288
static bool isValidRasterFileName( const QString & theFileNameQString, QString &retError );
289-
290289
static bool isValidRasterFileName( const QString & theFileNameQString );
290+
static QStringList subLayers( GDALDatasetH dataset );
291+
291292

292293
/** Return time stamp for given file name */
293294
static QDateTime lastModified( const QString & name );

‎src/ui/qgsogrsublayersdialogbase.ui

Lines changed: 25 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,53 @@
1-
<ui version="4.0" >
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
23
<class>QgsOGRSublayersDialogBase</class>
3-
<widget class="QDialog" name="QgsOGRSublayersDialogBase" >
4-
<property name="geometry" >
4+
<widget class="QDialog" name="QgsOGRSublayersDialogBase">
5+
<property name="geometry">
56
<rect>
67
<x>0</x>
78
<y>0</y>
89
<width>584</width>
9-
<height>535</height>
10+
<height>236</height>
1011
</rect>
1112
</property>
12-
<property name="windowTitle" >
13-
<string>Select OGR layers to load</string>
13+
<property name="windowTitle">
14+
<string>Select layers to load</string>
1415
</property>
15-
<property name="windowIcon" >
16-
<iconset>../../../qgis_1.0.0/src/plugins/ogrsublayers</iconset>
17-
</property>
18-
<layout class="QGridLayout" >
19-
<property name="leftMargin" >
20-
<number>9</number>
21-
</property>
22-
<property name="topMargin" >
23-
<number>9</number>
24-
</property>
25-
<property name="rightMargin" >
16+
<layout class="QGridLayout">
17+
<property name="margin">
2618
<number>9</number>
2719
</property>
28-
<property name="bottomMargin" >
29-
<number>9</number>
30-
</property>
31-
<property name="horizontalSpacing" >
20+
<property name="spacing">
3221
<number>6</number>
3322
</property>
34-
<property name="verticalSpacing" >
35-
<number>6</number>
36-
</property>
37-
<item row="0" column="0" >
38-
<widget class="QLabel" name="txtHeading" >
39-
<property name="sizePolicy" >
40-
<sizepolicy vsizetype="Minimum" hsizetype="Preferred" >
41-
<horstretch>0</horstretch>
42-
<verstretch>0</verstretch>
43-
</sizepolicy>
44-
</property>
45-
<property name="font" >
46-
<font>
47-
<family>Sans Serif</family>
48-
<pointsize>24</pointsize>
49-
<weight>75</weight>
50-
<italic>false</italic>
51-
<bold>true</bold>
52-
<underline>false</underline>
53-
<strikeout>false</strikeout>
54-
</font>
55-
</property>
56-
<property name="text" >
57-
<string>Sub layers list</string>
58-
</property>
59-
<property name="alignment" >
60-
<set>Qt::AlignCenter</set>
61-
</property>
62-
</widget>
63-
</item>
64-
<item row="8" column="0" >
65-
<widget class="QDialogButtonBox" name="buttonBox" >
66-
<property name="orientation" >
23+
<item row="6" column="0">
24+
<widget class="QDialogButtonBox" name="buttonBox">
25+
<property name="orientation">
6726
<enum>Qt::Horizontal</enum>
6827
</property>
69-
<property name="standardButtons" >
70-
<set>QDialogButtonBox::Cancel|QDialogButtonBox::NoButton|QDialogButtonBox::Ok</set>
28+
<property name="standardButtons">
29+
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Help|QDialogButtonBox::Ok</set>
7130
</property>
7231
</widget>
7332
</item>
74-
<item row="1" column="0" >
75-
<widget class="QTextEdit" name="textEdit" >
76-
<property name="html" >
77-
<string>&lt;html>&lt;head>&lt;meta name="qrichtext" content="1" />&lt;style type="text/css">
78-
p, li { white-space: pre-wrap; }
79-
&lt;/style>&lt;/head>&lt;body style=" font-family:'DejaVu Sans'; font-size:9pt; font-weight:400; font-style:normal;">
80-
&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>
81-
&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>
82-
&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>
83-
&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>
84-
&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>
85-
</property>
86-
</widget>
87-
</item>
88-
<item row="2" column="0" >
89-
<widget class="QTreeWidget" name="layersTable" >
90-
<property name="windowModality" >
91-
<enum>Qt::NonModal</enum>
92-
</property>
93-
<property name="selectionMode" >
33+
<item row="0" column="0">
34+
<widget class="QTreeWidget" name="layersTable">
35+
<property name="selectionMode">
9436
<enum>QAbstractItemView::ExtendedSelection</enum>
9537
</property>
96-
<property name="selectionBehavior" >
38+
<property name="selectionBehavior">
9739
<enum>QAbstractItemView::SelectRows</enum>
9840
</property>
9941
<column>
100-
<property name="text" >
42+
<property name="text">
10143
<string>1</string>
10244
</property>
10345
</column>
10446
</widget>
10547
</item>
10648
</layout>
10749
</widget>
108-
<layoutdefault spacing="6" margin="11" />
50+
<layoutdefault spacing="6" margin="11"/>
10951
<resources/>
11052
<connections>
11153
<connection>
@@ -114,11 +56,11 @@ p, li { white-space: pre-wrap; }
11456
<receiver>QgsOGRSublayersDialogBase</receiver>
11557
<slot>accept()</slot>
11658
<hints>
117-
<hint type="sourcelabel" >
59+
<hint type="sourcelabel">
11860
<x>446</x>
11961
<y>508</y>
12062
</hint>
121-
<hint type="destinationlabel" >
63+
<hint type="destinationlabel">
12264
<x>351</x>
12365
<y>473</y>
12466
</hint>
@@ -130,11 +72,11 @@ p, li { white-space: pre-wrap; }
13072
<receiver>QgsOGRSublayersDialogBase</receiver>
13173
<slot>reject()</slot>
13274
<hints>
133-
<hint type="sourcelabel" >
75+
<hint type="sourcelabel">
13476
<x>541</x>
13577
<y>507</y>
13678
</hint>
137-
<hint type="destinationlabel" >
79+
<hint type="destinationlabel">
13880
<x>503</x>
13981
<y>434</y>
14082
</hint>

0 commit comments

Comments
 (0)
Please sign in to comment.