Skip to content

Commit cc30609

Browse files
committedOct 23, 2014
Fix #11474 (cannot move layers in layer tree anymore)
I have managed to break that with implementation of #11369. Obviously it is a bad idea to change selection in a slot connected to model's rowsInserted signals because the drag'n'drop does not work properly anymore. Now registry bridge will emit a signal after new layers have been added, so the selection change at that point should be safe.
1 parent 88e5cde commit cc30609

File tree

8 files changed

+24
-30
lines changed

8 files changed

+24
-30
lines changed
 

‎python/core/layertree/qgslayertreeregistrybridge.sip

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,9 @@ class QgsLayerTreeRegistryBridge : QObject
2929
//! By default it is root group with zero index.
3030
void setLayerInsertionPoint( QgsLayerTreeGroup* parentGroup, int index );
3131

32+
signals:
33+
//! Tell others we have just added layers to the tree (used in QGIS to auto-select first newly added layer)
34+
//! @note added in 2.6
35+
void addedLayersToLayerTree( QList<QgsMapLayer*> layers );
36+
3237
};

‎python/gui/layertree/qgslayertreeview.sip

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,6 @@ class QgsLayerTreeView : QTreeView
6363
//! Get list of selected layers
6464
QList<QgsMapLayer*> selectedLayers() const;
6565

66-
//! if enabled, current selection will be automatically changed to the newly added layer node.
67-
//! This is purely for user's convenience so they do not need to click on the layer explicitly.
68-
//! @note added in 2.6
69-
void setAutoSelectAddedLayers( bool enabled );
70-
//! @note added in 2.6
71-
bool autoSelectAddedLayers() const;
72-
7366
public slots:
7467
//! Force refresh of layer symbology. Normally not needed as the changes of layer's renderer are monitored by the model
7568
void refreshLayerSymbology( const QString& layerId );

‎src/app/qgisapp.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2315,13 +2315,14 @@ void QgisApp::initLayerTreeView()
23152315

23162316
mLayerTreeView->setModel( model );
23172317
mLayerTreeView->setMenuProvider( new QgsAppLayerTreeViewMenuProvider( mLayerTreeView, mMapCanvas ) );
2318-
mLayerTreeView->setAutoSelectAddedLayers( true );
23192318

23202319
setupLayerTreeViewFromSettings();
23212320

23222321
connect( mLayerTreeView, SIGNAL( doubleClicked( QModelIndex ) ), this, SLOT( layerTreeViewDoubleClicked( QModelIndex ) ) );
23232322
connect( mLayerTreeView, SIGNAL( currentLayerChanged( QgsMapLayer* ) ), this, SLOT( activeLayerChanged( QgsMapLayer* ) ) );
23242323
connect( mLayerTreeView->selectionModel(), SIGNAL( currentChanged( QModelIndex, QModelIndex ) ), this, SLOT( updateNewLayerInsertionPoint() ) );
2324+
connect( QgsProject::instance()->layerTreeRegistryBridge(), SIGNAL( addedLayersToLayerTree( QList<QgsMapLayer*> ) ),
2325+
this, SLOT( autoSelectAddedLayer( QList<QgsMapLayer*> ) ) );
23252326

23262327
// add group tool button
23272328
QToolButton* btnAddGroup = new QToolButton;
@@ -2447,6 +2448,15 @@ void QgisApp::updateNewLayerInsertionPoint()
24472448
QgsProject::instance()->layerTreeRegistryBridge()->setLayerInsertionPoint( parentGroup, index );
24482449
}
24492450

2451+
void QgisApp::autoSelectAddedLayer( QList<QgsMapLayer*> layers )
2452+
{
2453+
if ( layers.count() )
2454+
{
2455+
QgsLayerTreeLayer* nodeLayer = QgsProject::instance()->layerTreeRoot()->findLayer( layers[0]->id() );
2456+
QModelIndex index = mLayerTreeView->layerTreeModel()->node2index( nodeLayer );
2457+
mLayerTreeView->setCurrentIndex( index );
2458+
}
2459+
}
24502460

24512461
void QgisApp::createMapTips()
24522462
{
@@ -3782,7 +3792,6 @@ void QgisApp::enableProjectMacros()
37823792
QgsPythonRunner::run( "qgis.utils.reloadProjectMacros()" );
37833793
}
37843794

3785-
37863795
/**
37873796
adds a saved project to qgis, usually called on startup by specifying a
37883797
project file on the command line
@@ -3798,11 +3807,6 @@ bool QgisApp::addProject( QString projectFile )
37983807
// close the previous opened project if any
37993808
closeProject();
38003809

3801-
// temporarily disable auto-select for project loading
3802-
// (having it on all the time would give inconsistent results,
3803-
// e.g. we select the first node if it is a layer, but not if it is a group)
3804-
mLayerTreeView->setAutoSelectAddedLayers( false );
3805-
38063810
if ( ! QgsProject::instance()->read( projectFile ) )
38073811
{
38083812
QApplication::restoreOverrideCursor();
@@ -3818,8 +3822,6 @@ bool QgisApp::addProject( QString projectFile )
38183822
return false;
38193823
}
38203824

3821-
mLayerTreeView->setAutoSelectAddedLayers( true );
3822-
38233825
setTitleBarText_( *this );
38243826
int myRedInt = QgsProject::instance()->readNumEntry( "Gui", "/CanvasColorRedPart", 255 );
38253827
int myGreenInt = QgsProject::instance()->readNumEntry( "Gui", "/CanvasColorGreenPart", 255 );

‎src/app/qgisapp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,8 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
477477
void layerTreeViewDoubleClicked( const QModelIndex& index );
478478
//! Make sure the insertion point for new layers is up-to-date with the current item in layer tree view
479479
void updateNewLayerInsertionPoint();
480+
//! connected to layer tree registry bridge, selects first of the newly added map layers
481+
void autoSelectAddedLayer( QList<QgsMapLayer*> layers );
480482
void activeLayerChanged( QgsMapLayer* layer );
481483
//! Zoom to full extent
482484
void zoomFull();

‎src/core/layertree/qgslayertreeregistrybridge.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ void QgsLayerTreeRegistryBridge::layersAdded( QList<QgsMapLayer*> layers )
6868

6969
// add new layers to the right place
7070
mInsertionPointGroup->insertChildNodes( mInsertionPointIndex, nodes );
71+
72+
// tell other components that layers have been added - this signal is used in QGIS to auto-select the first layer
73+
emit addedLayersToLayerTree( layers );
7174
}
7275

7376
void QgsLayerTreeRegistryBridge::layersWillBeRemoved( QStringList layerIds )

‎src/core/layertree/qgslayertreeregistrybridge.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ class CORE_EXPORT QgsLayerTreeRegistryBridge : public QObject
5353
void setLayerInsertionPoint( QgsLayerTreeGroup* parentGroup, int index );
5454

5555
signals:
56+
//! Tell others we have just added layers to the tree (used in QGIS to auto-select first newly added layer)
57+
//! @note added in 2.6
58+
void addedLayersToLayerTree( QList<QgsMapLayer*> layers );
5659

5760
protected slots:
5861
void layersAdded( QList<QgsMapLayer*> layers );

‎src/gui/layertree/qgslayertreeview.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ QgsLayerTreeView::QgsLayerTreeView( QWidget *parent )
2828
: QTreeView( parent )
2929
, mDefaultActions( 0 )
3030
, mMenuProvider( 0 )
31-
, mAutoSelectAddedLayers( false )
3231
{
3332
setHeaderHidden( true );
3433

@@ -138,10 +137,6 @@ void QgsLayerTreeView::modelRowsInserted( QModelIndex index, int start, int end
138137
updateExpandedStateFromNode( children[i] );
139138
}
140139

141-
// make newly added layer active (if auto-select is enabled)
142-
if ( mAutoSelectAddedLayers && QgsLayerTree::isLayer( children[start] ) )
143-
setCurrentIndex( layerTreeModel()->node2index( children[start] ) );
144-
145140
// make sure we still have correct current layer
146141
onCurrentChanged();
147142
}

‎src/gui/layertree/qgslayertreeview.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,6 @@ class GUI_EXPORT QgsLayerTreeView : public QTreeView
8181
//! Get list of selected layers
8282
QList<QgsMapLayer*> selectedLayers() const;
8383

84-
//! if enabled, current selection will be automatically changed to the newly added layer node.
85-
//! This is purely for user's convenience so they do not need to click on the layer explicitly.
86-
//! @note added in 2.6
87-
void setAutoSelectAddedLayers( bool enabled ) { mAutoSelectAddedLayers = enabled; }
88-
//! @note added in 2.6
89-
bool autoSelectAddedLayers() const { return mAutoSelectAddedLayers; }
90-
9184
public slots:
9285
//! Force refresh of layer symbology. Normally not needed as the changes of layer's renderer are monitored by the model
9386
void refreshLayerSymbology( const QString& layerId );
@@ -121,8 +114,6 @@ class GUI_EXPORT QgsLayerTreeView : public QTreeView
121114
QgsLayerTreeViewMenuProvider* mMenuProvider;
122115
//! Keeps track of current layer ID (to check when to emit signal about change of current layer)
123116
QString mCurrentLayerID;
124-
//! Indicates whether the view should select newly added layers when they are added to the model
125-
bool mAutoSelectAddedLayers;
126117
};
127118

128119

0 commit comments

Comments
 (0)
Please sign in to comment.