Skip to content
Permalink
Browse files
add checkbox to open sublayers in a group
  • Loading branch information
Gustry authored and m-kuhn committed Mar 17, 2017
1 parent ed1b0a2 commit 24e3214eda44d1c39df3709bb354c5dec18dfb1c
Showing with 78 additions and 4 deletions.
  1. +12 −0 python/gui/qgssublayersdialog.sip
  2. +42 −4 src/app/qgisapp.cpp
  3. +9 −0 src/gui/qgssublayersdialog.cpp
  4. +15 −0 src/gui/qgssublayersdialog.h
@@ -39,6 +39,18 @@ class QgsSublayersDialog : QDialog
//! @note added in 2.16
QgsSublayersDialog::LayerDefinitionList selection();

//! Set if we should display the add to group checkbox
//! @note added in 3.0
void setShowAddToGroupCheckbox( bool showAddToGroupCheckbox );

//! If we should display the add to group checkbox
//! @note added in 3.0
bool showAddToGroupCheckbox() const;

//! If we should add layers in a group
//! @note added in 3.0
bool addToGroupCheckbox() const;

public slots:
void on_buttonBox_helpRequested();
int exec();
@@ -4061,6 +4061,7 @@ void QgisApp::askUserForGDALSublayers( QgsRasterLayer *layer )

// We initialize a selection dialog and display it.
QgsSublayersDialog chooseSublayersDialog( QgsSublayersDialog::Gdal, QStringLiteral( "gdal" ), this );
chooseSublayersDialog.setShowAddToGroupCheckbox( true );

QgsSublayersDialog::LayerDefinitionList layers;
QStringList names;
@@ -4112,6 +4113,13 @@ void QgisApp::askUserForGDALSublayers( QgsRasterLayer *layer )
QRegExp rx( "\"(.*)\"" );
QString uri, name;

QgsLayerTreeGroup *group = nullptr;
bool addToGroup = settings.value( QStringLiteral( "/qgis/openSublayersInGroup" ), true ).toBool();
if ( addToGroup )
{
group = QgsProject::instance()->layerTreeRoot()->insertGroup( 0, layer->name() );
}

Q_FOREACH ( const QgsSublayersDialog::LayerDefinition &def, chooseSublayersDialog.selection() )
{
int i = def.layerId;
@@ -4129,7 +4137,15 @@ void QgisApp::askUserForGDALSublayers( QgsRasterLayer *layer )
QgsRasterLayer *rlayer = new QgsRasterLayer( sublayers[i], name );
if ( rlayer && rlayer->isValid() )
{
addRasterLayer( rlayer );
if ( addToGroup )
{
QgsProject::instance()->addMapLayer( rlayer, false );
group->addLayer( rlayer );
}
else
{
addRasterLayer( rlayer );
}
}
}
}
@@ -4158,6 +4174,11 @@ void QgisApp::loadGDALSublayers( const QString &uri, const QStringList &list )
{
QString path, name;
QgsRasterLayer *subLayer = nullptr;
QgsSettings settings;
QgsLayerTreeGroup *group = nullptr;
bool addToGroup = settings.value( QStringLiteral( "/qgis/openSublayersInGroup" ), true ).toBool();
if ( addToGroup )
group = QgsProject::instance()->layerTreeRoot()->insertGroup( 0, QFileInfo( uri ).completeBaseName() );

//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-- )
@@ -4170,7 +4191,15 @@ void QgisApp::loadGDALSublayers( const QString &uri, const QStringList &list )
if ( subLayer )
{
if ( subLayer->isValid() )
addRasterLayer( subLayer );
if ( addToGroup )
{
QgsProject::instance()->addMapLayer( subLayer, false );
group->addLayer( subLayer );
}
else
{
addRasterLayer( subLayer );
}
else
delete subLayer;
}
@@ -4224,12 +4253,14 @@ void QgisApp::askUserForOGRSublayers( QgsVectorLayer *layer )

// We initialize a selection dialog and display it.
QgsSublayersDialog chooseSublayersDialog( QgsSublayersDialog::Ogr, QStringLiteral( "ogr" ), this );
chooseSublayersDialog.setShowAddToGroupCheckbox( true );
chooseSublayersDialog.populateLayerTable( list );

if ( !chooseSublayersDialog.exec() )
return;

QString uri = layer->source();
QString name = layer->name();
//the separator char & was changed to | to be compatible
//with url for protocol drivers
if ( uri.contains( '|', Qt::CaseSensitive ) )
@@ -4276,12 +4307,19 @@ void QgisApp::askUserForOGRSublayers( QgsVectorLayer *layer )

if ( ! myList.isEmpty() )
{
// Register layer(s) with the layers registry
QgsProject::instance()->addMapLayers( myList );
QgsSettings settings;
bool addToGroup = settings.value( QStringLiteral( "/qgis/openSublayersInGroup" ), true ).toBool();
QgsLayerTreeGroup *group = nullptr;
if ( addToGroup )
group = QgsProject::instance()->layerTreeRoot()->insertGroup( 0, name );

QgsProject::instance()->addMapLayers( myList, ! addToGroup );
Q_FOREACH ( QgsMapLayer *l, myList )
{
bool ok;
l->loadDefaultStyle( ok );
if ( addToGroup )
group->addLayer( l );
}
}
}
@@ -59,6 +59,13 @@ QgsSublayersDialog::QgsSublayersDialog( ProviderType providerType, const QString

QgsSettings settings;
restoreGeometry( settings.value( "/Windows/" + mName + "SubLayers/geometry" ).toByteArray() );

// Checkbox about adding sublayers to a group
checkboxAddToGroup = new QCheckBox( tr( "Add layers to a group" ) );
bool addToGroup = settings.value( QStringLiteral( "/qgis/openSublayersInGroup" ), false ).toBool();
checkboxAddToGroup->setChecked( addToGroup );
if ( mShowAddToGroupCheckbox )

This comment has been minimized.

Copy link
@nyalldawson

nyalldawson Mar 21, 2017

Collaborator

@Gustry
This code is broken - at this point mShowAddToGroupCheckbox is uninitialized. I've fixed that in 545da60, but when this line is called in the constructor mShowAddToGroupCheckbox will always be false, so the checkbox will never be shown.

This comment has been minimized.

Copy link
@Gustry

Gustry Mar 21, 2017

Author Contributor

Sorry for that, thanks for fix @nyalldawson.

when this line is called in the constructor mShowAddToGroupCheckbox will always be false, so the checkbox will never be shown.

This is fine, I wanted the checkbox to be hidden by default.

I'm just wondering what I did. I compiled and tried my code many times, and my behaviour was working fine.

buttonBox->addButton( checkboxAddToGroup, QDialogButtonBox::ActionRole );
}

QgsSublayersDialog::~QgsSublayersDialog()
@@ -177,5 +184,7 @@ int QgsSublayersDialog::exec()
int ret = QDialog::exec();
if ( overrideCursor )
QApplication::setOverrideCursor( cursor );

settings.setValue( QStringLiteral( "/qgis/openSublayersInGroup" ), checkboxAddToGroup->isChecked() );
return ret;
}
@@ -17,6 +17,7 @@
#define QGSSUBLAYERSDIALOG_H

#include <QDialog>
#include <QCheckBox>
#include <ui_qgssublayersdialogbase.h>
#include "qgscontexthelp.h"
#include "qgis_gui.h"
@@ -63,6 +64,18 @@ class GUI_EXPORT QgsSublayersDialog : public QDialog, private Ui::QgsSublayersDi
//! @note added in 2.16
LayerDefinitionList selection();

//! Set if we should display the add to group checkbox
//! @note added in 3.0
void setShowAddToGroupCheckbox( bool showAddToGroupCheckbox ) { mShowAddToGroupCheckbox = showAddToGroupCheckbox; }

//! If we should display the add to group checkbox
//! @note added in 3.0
bool showAddToGroupCheckbox() const { return mShowAddToGroupCheckbox; }

//! If we should add layers in a group
//! @note added in 3.0
bool addToGroupCheckbox() const { return checkboxAddToGroup->isChecked(); }

public slots:
void on_buttonBox_helpRequested() { QgsContextHelp::run( metaObject()->className() ); }
int exec();
@@ -72,6 +85,8 @@ class GUI_EXPORT QgsSublayersDialog : public QDialog, private Ui::QgsSublayersDi
QStringList mSelectedSubLayers;
bool mShowCount; //!< Whether to show number of features in the table
bool mShowType; //!< Whether to show type in the table
bool mShowAddToGroupCheckbox; //!< Whether to show the add to group checkbox
QCheckBox *checkboxAddToGroup = nullptr;
};

#endif

0 comments on commit 24e3214

Please sign in to comment.