54 changes: 38 additions & 16 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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..." ) );
Expand All @@ -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
Expand All @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/app/qgisapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
162 changes: 109 additions & 53 deletions src/app/qgsbrowserdockwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,35 @@ QgsBrowserDockWidget::QgsBrowserDockWidget( QWidget * parent ) :

mBrowserView = new QgsBrowserTreeView( this );

mRefreshButton = new QToolButton( this );
mRefreshButton->setIcon( QgisApp::instance()->getThemeIcon( "mActionDraw.png" ) );
mRefreshButton->setText( tr( "Refresh" ) );
mRefreshButton->setAutoRaise( true );
connect( mRefreshButton, SIGNAL( clicked() ), this, SLOT( refresh() ) );
QToolButton* refreshButton = new QToolButton( this );
refreshButton->setIcon( QgisApp::instance()->getThemeIcon( "mActionDraw.png" ) );
// remove this to save space
refreshButton->setToolButtonStyle( Qt::ToolButtonTextBesideIcon );
refreshButton->setText( tr( "Refresh" ) );
refreshButton->setToolTip( tr( "Refresh" ) );
refreshButton->setAutoRaise( true );
connect( refreshButton, SIGNAL( clicked() ), this, SLOT( refresh() ) );

QToolButton* addLayersButton = new QToolButton( this );
addLayersButton->setIcon( QgisApp::instance()->getThemeIcon( "mActionAddLayer.png" ) );
// remove this to save space
addLayersButton->setToolButtonStyle( Qt::ToolButtonTextBesideIcon );
addLayersButton->setText( tr( "Add Selection" ) );
addLayersButton->setToolTip( tr( "Add Selected Layers" ) );
addLayersButton->setAutoRaise( true );
connect( addLayersButton, SIGNAL( clicked() ), this, SLOT( addSelectedLayers() ) );

QVBoxLayout* layout = new QVBoxLayout();
QHBoxLayout* hlayout = new QHBoxLayout();
layout->setContentsMargins( 0, 0, 0, 0 );
layout->setSpacing( 0 );
layout->addWidget( mRefreshButton );
hlayout->setContentsMargins( 0, 0, 0, 0 );
hlayout->setSpacing( 5 );
hlayout->setAlignment( Qt::AlignLeft );

hlayout->addWidget( refreshButton );
hlayout->addWidget( addLayersButton );
layout->addLayout( hlayout );
layout->addWidget( mBrowserView );

QWidget* innerWidget = new QWidget( this );
Expand Down Expand Up @@ -110,55 +129,13 @@ void QgsBrowserDockWidget::showEvent( QShowEvent * e )

void QgsBrowserDockWidget::itemClicked( const QModelIndex& index )
{
QgsDataItem *item = mModel->dataItem( index );
if ( !item )
return;

QgsLayerItem *layerItem = qobject_cast<QgsLayerItem*>( mModel->dataItem( index ) );
if ( layerItem == NULL )
return;

QString uri = layerItem->uri();
if ( uri.isEmpty() )
return;
QgsDataItem *dataItem = mModel->dataItem( index );

QgsMapLayer::LayerType type = layerItem->mapLayerType();
QString providerKey = layerItem->providerKey();

QgsDebugMsg( providerKey + " : " + uri );
if ( type == QgsMapLayer::VectorLayer )
if ( dataItem != NULL && dataItem->type() == QgsDataItem::Layer )
{
QgisApp::instance()->addVectorLayer( uri, layerItem->name(), providerKey );
}
if ( type == QgsMapLayer::RasterLayer )
{
// This should go to WMS provider
QStringList URIParts = uri.split( "|" );
QString rasterLayerPath = URIParts.at( 0 );
QStringList layers;
QStringList styles;
QString format;
QString crs;
for ( int i = 1 ; i < URIParts.size(); i++ )
{
QString part = URIParts.at( i );
int pos = part.indexOf( "=" );
QString field = part.left( pos );
QString value = part.mid( pos + 1 );

if ( field == "layers" )
layers = value.split( "," );
if ( field == "styles" )
styles = value.split( "," );
if ( field == "format" )
format = value;
if ( field == "crs" )
crs = value;
}
QgsDebugMsg( "rasterLayerPath = " + rasterLayerPath );
QgsDebugMsg( "layers = " + layers.join( " " ) );

QgisApp::instance()->addRasterLayer( rasterLayerPath, layerItem->name(), providerKey, layers, styles, format, crs );
QgsLayerItem *layerItem = qobject_cast<QgsLayerItem*>( dataItem );
if ( layerItem != NULL )
addLayer( layerItem );
}
}

Expand Down Expand Up @@ -189,6 +166,12 @@ void QgsBrowserDockWidget::showContextMenu( const QPoint & pt )
}
}

else if ( item->type() == QgsDataItem::Layer )
{
menu->addAction( tr( "Add Layer" ), this, SLOT( itemClicked( idx ) ) );
menu->addAction( tr( "Add Selected Layers" ), this, SLOT( addSelectedLayers() ) );
}

QList<QAction*> actions = item->actions();
if ( !actions.isEmpty() )
{
Expand Down Expand Up @@ -279,3 +262,76 @@ void QgsBrowserDockWidget::refreshModel( const QModelIndex& index )
}
}
}

void QgsBrowserDockWidget::addLayer( QgsLayerItem *layerItem )
{
if ( layerItem == NULL )
return;

QString uri = layerItem->uri();
if ( uri.isEmpty() )
return;

QgsMapLayer::LayerType type = layerItem->mapLayerType();
QString providerKey = layerItem->providerKey();

QgsDebugMsg( providerKey + " : " + uri );
if ( type == QgsMapLayer::VectorLayer )
{
QgisApp::instance()->addVectorLayer( uri, layerItem->name(), providerKey );
}
if ( type == QgsMapLayer::RasterLayer )
{
// This should go to WMS provider
QStringList URIParts = uri.split( "|" );
QString rasterLayerPath = URIParts.at( 0 );
QStringList layers;
QStringList styles;
QString format;
QString crs;
for ( int i = 1 ; i < URIParts.size(); i++ )
{
QString part = URIParts.at( i );
int pos = part.indexOf( "=" );
QString field = part.left( pos );
QString value = part.mid( pos + 1 );

if ( field == "layers" )
layers = value.split( "," );
if ( field == "styles" )
styles = value.split( "," );
if ( field == "format" )
format = value;
if ( field == "crs" )
crs = value;
}
QgsDebugMsg( "rasterLayerPath = " + rasterLayerPath );
QgsDebugMsg( "layers = " + layers.join( " " ) );

QgisApp::instance()->addRasterLayer( rasterLayerPath, layerItem->name(), providerKey, layers, styles, format, crs );
}
}

void QgsBrowserDockWidget::addSelectedLayers()
{
QApplication::setOverrideCursor( Qt::WaitCursor );

// get a sorted list of selected indexes
QModelIndexList list = mBrowserView->selectionModel()->selectedIndexes();
qSort( list );

// add items in reverse order so they are in correct order in the layers dock
for ( int i = list.size() - 1; i >= 0; i-- )
{
QModelIndex index = list[i];
QgsDataItem *dataItem = mModel->dataItem( index );
if ( dataItem && dataItem->type() == QgsDataItem::Layer )
{
QgsLayerItem *layerItem = qobject_cast<QgsLayerItem*>( dataItem );
if ( layerItem )
addLayer( layerItem );
}
}

QApplication::restoreOverrideCursor();
}
7 changes: 5 additions & 2 deletions src/app/qgsbrowserdockwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class QgsBrowserModel;
class QModelIndex;
class QTreeView;
class QToolButton;
class QgsLayerItem;

class QgsBrowserDockWidget : public QDockWidget
{
Expand All @@ -25,14 +25,17 @@ class QgsBrowserDockWidget : public QDockWidget

void refresh();

void addSelectedLayers();

protected:

void refreshModel( const QModelIndex& index );

void showEvent( QShowEvent * event );

void addLayer( QgsLayerItem *layerItem );

QTreeView* mBrowserView;
QToolButton* mRefreshButton;
QgsBrowserModel* mModel;
};

Expand Down
5 changes: 5 additions & 0 deletions src/app/qgsoptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/core/qgsdataitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ bool QgsDataItem::hasChildren()

void QgsDataItem::addChildItem( QgsDataItem * child, bool refresh )
{
QgsDebugMsg( "mName = " + child->mName );
QgsDebugMsg( QString( "add child #%1 - %2" ).arg( mChildren.size() ).arg( child->mName ) );

int i;
for ( i = 0; i < mChildren.size(); i++ )
{
Expand Down
14 changes: 10 additions & 4 deletions src/providers/gdal/qgsgdaldataitems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
QgsDebugMsg( "extensions: " + extensions.join( " " ) );
QgsDebugMsg( "wildcards: " + wildcards.join( " " ) );
}
// skip *.aux.xml files (GDAL auxilary metadata files)
// unless that extension is in the list (*.xml might be though)
if ( thePath.right( 8 ) == ".aux.xml" &&
extensions.indexOf( "aux.xml" ) < 0 )
return 0;

if ( extensions.indexOf( info.suffix().toLower() ) < 0 )
{
bool matches = false;
Expand Down Expand Up @@ -107,7 +113,8 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )

QgsDebugMsg( "GdalDataset opened " + thePath );

QString name = info.completeBaseName();
//extract basename with extension
QString name = info.completeBaseName() + "." + QFileInfo( thePath ).suffix();
QString uri = thePath;

QgsLayerItem * item = new QgsGdalLayerItem( parentItem, name, thePath, uri );
Expand All @@ -124,11 +131,10 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
if ( hChildDS )
{
GDALClose( hChildDS );
QgsDebugMsg( QString( "add child #%1 - %2" ).arg( i ).arg( sublayers[i] ) );

QString name = sublayers[i];
name.replace( thePath, QFileInfo( thePath ).completeBaseName() );

//replace full path with basename+extension
name.replace( thePath, QFileInfo( thePath ).completeBaseName() + "." + QFileInfo( thePath ).suffix() );
childItem = new QgsGdalLayerItem( item, name, thePath + "/" + name, sublayers[i] );
if ( childItem )
item->addChildItem( childItem );
Expand Down
Loading