From 850eae1606a1c61897ec9d440df672bae5afc72a Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Tue, 30 Oct 2018 13:29:20 +1000 Subject: [PATCH] [browser] Avoid expanding/collapsing nodes if the double click was specifically handled by an item itself --- .../qgsbrowserdockwidget.sip.in | 9 +++++++-- src/gui/qgsbrowserdockwidget.cpp | 19 ++++++++++++++++--- src/gui/qgsbrowserdockwidget.h | 11 +++++++++-- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/python/gui/auto_generated/qgsbrowserdockwidget.sip.in b/python/gui/auto_generated/qgsbrowserdockwidget.sip.in index a1b4d3ac53d..dcaa143386b 100644 --- a/python/gui/auto_generated/qgsbrowserdockwidget.sip.in +++ b/python/gui/auto_generated/qgsbrowserdockwidget.sip.in @@ -37,10 +37,15 @@ Add directory to favorites %End public slots: - void addLayerAtIndex( const QModelIndex &index ); + + bool addLayerAtIndex( const QModelIndex &index ); %Docstring -Add layer at index +Adds the layer corresponding to the specified model ``index``. + +Returns true if the index was successfully intrepreted as a map layer and loaded, or +false if the index is not a map layer or could not be loaded. %End + void showContextMenu( QPoint ); %Docstring Show context menu diff --git a/src/gui/qgsbrowserdockwidget.cpp b/src/gui/qgsbrowserdockwidget.cpp index b25a69351b6..3a0670b15ce 100644 --- a/src/gui/qgsbrowserdockwidget.cpp +++ b/src/gui/qgsbrowserdockwidget.cpp @@ -91,6 +91,8 @@ QgsBrowserDockWidget::QgsBrowserDockWidget( const QString &name, QgsBrowserModel action->setCheckable( true ); menu->addAction( action ); + mBrowserView->setExpandsOnDoubleClick( false ); + connect( mActionRefresh, &QAction::triggered, this, &QgsBrowserDockWidget::refresh ); connect( mActionAddLayers, &QAction::triggered, this, &QgsBrowserDockWidget::addSelectedLayers ); connect( mActionCollapse, &QAction::triggered, mBrowserView, &QgsDockBrowserTreeView::collapseAll ); @@ -169,8 +171,16 @@ void QgsBrowserDockWidget::itemDoubleClicked( const QModelIndex &index ) if ( item->handleDoubleClick() ) return; + else if ( addLayerAtIndex( index ) ) // default double-click handler + return; else - addLayerAtIndex( index ); // default double-click handler + { + // double click not handled by browser model, so use as default view expand behavior + if ( mBrowserView->isExpanded( index ) ) + mBrowserView->collapse( index ); + else + mBrowserView->expand( index ); + } } void QgsBrowserDockWidget::renameFavorite() @@ -416,7 +426,7 @@ void QgsBrowserDockWidget::addLayer( QgsLayerItem *layerItem ) emit handleDropUriList( list ); } -void QgsBrowserDockWidget::addLayerAtIndex( const QModelIndex &index ) +bool QgsBrowserDockWidget::addLayerAtIndex( const QModelIndex &index ) { QgsDebugMsg( QStringLiteral( "rowCount() = %1" ).arg( mModel->rowCount( mProxyModel->mapToSource( index ) ) ) ); QgsDataItem *item = mModel->dataItem( mProxyModel->mapToSource( index ) ); @@ -430,8 +440,9 @@ void QgsBrowserDockWidget::addLayerAtIndex( const QModelIndex &index ) emit openFile( projectItem->path(), QStringLiteral( "project" ) ); QApplication::restoreOverrideCursor(); } + return true; } - if ( item && item->type() == QgsDataItem::Layer ) + else if ( item && item->type() == QgsDataItem::Layer ) { QgsLayerItem *layerItem = qobject_cast( item ); if ( layerItem ) @@ -440,7 +451,9 @@ void QgsBrowserDockWidget::addLayerAtIndex( const QModelIndex &index ) addLayer( layerItem ); QApplication::restoreOverrideCursor(); } + return true; } + return false; } void QgsBrowserDockWidget::addSelectedLayers() diff --git a/src/gui/qgsbrowserdockwidget.h b/src/gui/qgsbrowserdockwidget.h index 74ef74f9a4e..ace8667e8c0 100644 --- a/src/gui/qgsbrowserdockwidget.h +++ b/src/gui/qgsbrowserdockwidget.h @@ -56,8 +56,15 @@ class GUI_EXPORT QgsBrowserDockWidget : public QgsDockWidget, private Ui::QgsBro void addFavoriteDirectory( const QString &favDir, const QString &name = QString() ); public slots: - //! Add layer at index - void addLayerAtIndex( const QModelIndex &index ); + + /** + * Adds the layer corresponding to the specified model \a index. + * + * Returns true if the index was successfully intrepreted as a map layer and loaded, or + * false if the index is not a map layer or could not be loaded. + */ + bool addLayerAtIndex( const QModelIndex &index ); + //! Show context menu void showContextMenu( QPoint );