Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wonder-sk committed Oct 23, 2014
1 parent 88e5cde commit cc30609
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 30 deletions.
5 changes: 5 additions & 0 deletions python/core/layertree/qgslayertreeregistrybridge.sip
Expand Up @@ -29,4 +29,9 @@ class QgsLayerTreeRegistryBridge : QObject
//! By default it is root group with zero index.
void setLayerInsertionPoint( QgsLayerTreeGroup* parentGroup, int index );

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

};
7 changes: 0 additions & 7 deletions python/gui/layertree/qgslayertreeview.sip
Expand Up @@ -63,13 +63,6 @@ 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 );
Expand Down
20 changes: 11 additions & 9 deletions src/app/qgisapp.cpp
Expand Up @@ -2315,13 +2315,14 @@ void QgisApp::initLayerTreeView()

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

setupLayerTreeViewFromSettings();

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

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

void QgisApp::autoSelectAddedLayer( QList<QgsMapLayer*> layers )
{
if ( layers.count() )
{
QgsLayerTreeLayer* nodeLayer = QgsProject::instance()->layerTreeRoot()->findLayer( layers[0]->id() );
QModelIndex index = mLayerTreeView->layerTreeModel()->node2index( nodeLayer );
mLayerTreeView->setCurrentIndex( index );
}
}

void QgisApp::createMapTips()
{
Expand Down Expand Up @@ -3782,7 +3792,6 @@ void QgisApp::enableProjectMacros()
QgsPythonRunner::run( "qgis.utils.reloadProjectMacros()" );
}


/**
adds a saved project to qgis, usually called on startup by specifying a
project file on the command line
Expand All @@ -3798,11 +3807,6 @@ 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();
Expand All @@ -3818,8 +3822,6 @@ 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 );
Expand Down
2 changes: 2 additions & 0 deletions src/app/qgisapp.h
Expand Up @@ -477,6 +477,8 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
void layerTreeViewDoubleClicked( const QModelIndex& index );
//! Make sure the insertion point for new layers is up-to-date with the current item in layer tree view
void updateNewLayerInsertionPoint();
//! connected to layer tree registry bridge, selects first of the newly added map layers
void autoSelectAddedLayer( QList<QgsMapLayer*> layers );
void activeLayerChanged( QgsMapLayer* layer );
//! Zoom to full extent
void zoomFull();
Expand Down
3 changes: 3 additions & 0 deletions src/core/layertree/qgslayertreeregistrybridge.cpp
Expand Up @@ -68,6 +68,9 @@ void QgsLayerTreeRegistryBridge::layersAdded( QList<QgsMapLayer*> layers )

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

// tell other components that layers have been added - this signal is used in QGIS to auto-select the first layer
emit addedLayersToLayerTree( layers );
}

void QgsLayerTreeRegistryBridge::layersWillBeRemoved( QStringList layerIds )
Expand Down
3 changes: 3 additions & 0 deletions src/core/layertree/qgslayertreeregistrybridge.h
Expand Up @@ -53,6 +53,9 @@ class CORE_EXPORT QgsLayerTreeRegistryBridge : public QObject
void setLayerInsertionPoint( QgsLayerTreeGroup* parentGroup, int index );

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

protected slots:
void layersAdded( QList<QgsMapLayer*> layers );
Expand Down
5 changes: 0 additions & 5 deletions src/gui/layertree/qgslayertreeview.cpp
Expand Up @@ -28,7 +28,6 @@ QgsLayerTreeView::QgsLayerTreeView( QWidget *parent )
: QTreeView( parent )
, mDefaultActions( 0 )
, mMenuProvider( 0 )
, mAutoSelectAddedLayers( false )
{
setHeaderHidden( true );

Expand Down Expand Up @@ -138,10 +137,6 @@ void QgsLayerTreeView::modelRowsInserted( QModelIndex index, int start, int end
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();
}
Expand Down
9 changes: 0 additions & 9 deletions src/gui/layertree/qgslayertreeview.h
Expand Up @@ -81,13 +81,6 @@ 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 );
Expand Down Expand Up @@ -121,8 +114,6 @@ 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;
};


Expand Down

0 comments on commit cc30609

Please sign in to comment.