Skip to content
Permalink
Browse files
Fix #11369 (New layers are not active with new legend)
  • Loading branch information
wonder-sk committed Oct 22, 2014
1 parent c9e05f9 commit ea910d887d09d588700340f64da58fb6c60c83ac
@@ -63,6 +63,13 @@ class QgsLayerTreeView : QTreeView
//! Get list of selected layers
QList<QgsMapLayer*> selectedLayers() const;

//! if enabled, current selection will be automatically changed to the newly added layer node.
//! This is purely for user's convenience so they do not need to click on the layer explicitly.
//! @note added in 2.6
void setAutoSelectAddedLayers( bool enabled );
//! @note added in 2.6
bool autoSelectAddedLayers() const;

public slots:
//! Force refresh of layer symbology. Normally not needed as the changes of layer's renderer are monitored by the model
void refreshLayerSymbology( const QString& layerId );
@@ -2315,6 +2315,7 @@ void QgisApp::initLayerTreeView()

mLayerTreeView->setModel( model );
mLayerTreeView->setMenuProvider( new QgsAppLayerTreeViewMenuProvider( mLayerTreeView, mMapCanvas ) );
mLayerTreeView->setAutoSelectAddedLayers( true );

setupLayerTreeViewFromSettings();

@@ -3797,6 +3798,11 @@ bool QgisApp::addProject( QString projectFile )
// close the previous opened project if any
closeProject();

// temporarily disable auto-select for project loading
// (having it on all the time would give inconsistent results,
// e.g. we select the first node if it is a layer, but not if it is a group)
mLayerTreeView->setAutoSelectAddedLayers( false );

if ( ! QgsProject::instance()->read( projectFile ) )
{
QApplication::restoreOverrideCursor();
@@ -3812,6 +3818,8 @@ bool QgisApp::addProject( QString projectFile )
return false;
}

mLayerTreeView->setAutoSelectAddedLayers( true );

setTitleBarText_( *this );
int myRedInt = QgsProject::instance()->readNumEntry( "Gui", "/CanvasColorRedPart", 255 );
int myGreenInt = QgsProject::instance()->readNumEntry( "Gui", "/CanvasColorGreenPart", 255 );
@@ -49,14 +49,13 @@ void QgsLayerTreeRegistryBridge::layersAdded( QList<QgsMapLayer*> layers )
if ( !mEnabled )
return;

int i = 0;
QList<QgsLayerTreeNode*> nodes;
foreach ( QgsMapLayer* layer, layers )
{
QgsLayerTreeLayer* nodeLayer = new QgsLayerTreeLayer( layer );
nodeLayer->setVisible( mNewLayersVisible ? Qt::Checked : Qt::Unchecked );

// add new layer to the top
mInsertionPointGroup->insertChildNode( mInsertionPointIndex + i++, nodeLayer );
nodes << nodeLayer;

// check whether the layer is marked as embedded
QString projectFile = QgsProject::instance()->layerIsEmbedded( nodeLayer->layerId() );
@@ -66,6 +65,9 @@ void QgsLayerTreeRegistryBridge::layersAdded( QList<QgsMapLayer*> layers )
nodeLayer->setCustomProperty( "embedded_project", projectFile );
}
}

// add new layers to the right place
mInsertionPointGroup->insertChildNodes( mInsertionPointIndex, nodes );
}

void QgsLayerTreeRegistryBridge::layersWillBeRemoved( QStringList layerIds )
@@ -28,6 +28,7 @@ QgsLayerTreeView::QgsLayerTreeView( QWidget *parent )
: QTreeView( parent )
, mDefaultActions( 0 )
, mMenuProvider( 0 )
, mAutoSelectAddedLayers( false )
{
setHeaderHidden( true );

@@ -131,11 +132,16 @@ void QgsLayerTreeView::modelRowsInserted( QModelIndex index, int start, int end
if ( QgsLayerTree::isLayer( parentNode ) )
return; // layers have only symbology nodes (no expanded/collapsed handling)

QList<QgsLayerTreeNode*> children = parentNode->children();
for ( int i = start; i <= end; ++i )
{
updateExpandedStateFromNode( parentNode->children()[i] );
updateExpandedStateFromNode( children[i] );
}

// make newly added layer active (if auto-select is enabled)
if ( mAutoSelectAddedLayers && QgsLayerTree::isLayer( children[start] ) )
setCurrentIndex( layerTreeModel()->node2index( children[start] ) );

// make sure we still have correct current layer
onCurrentChanged();
}
@@ -81,6 +81,13 @@ class GUI_EXPORT QgsLayerTreeView : public QTreeView
//! Get list of selected layers
QList<QgsMapLayer*> selectedLayers() const;

//! if enabled, current selection will be automatically changed to the newly added layer node.
//! This is purely for user's convenience so they do not need to click on the layer explicitly.
//! @note added in 2.6
void setAutoSelectAddedLayers( bool enabled ) { mAutoSelectAddedLayers = enabled; }
//! @note added in 2.6
bool autoSelectAddedLayers() const { return mAutoSelectAddedLayers; }

public slots:
//! Force refresh of layer symbology. Normally not needed as the changes of layer's renderer are monitored by the model
void refreshLayerSymbology( const QString& layerId );
@@ -114,6 +121,8 @@ class GUI_EXPORT QgsLayerTreeView : public QTreeView
QgsLayerTreeViewMenuProvider* mMenuProvider;
//! Keeps track of current layer ID (to check when to emit signal about change of current layer)
QString mCurrentLayerID;
//! Indicates whether the view should select newly added layers when they are added to the model
bool mAutoSelectAddedLayers;
};


0 comments on commit ea910d8

Please sign in to comment.