From fc0c49669e7d88b45fc52e35e94925b9dd49ff75 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Thu, 9 May 2024 09:01:05 +1000 Subject: [PATCH] Set initial selected item to matching path when changing data source too --- .../PyQt6/gui/auto_generated/qgsbrowsertreeview.sip.in | 4 +++- .../auto_generated/qgsdatasourceselectdialog.sip.in | 8 ++++++-- python/gui/auto_generated/qgsbrowsertreeview.sip.in | 4 +++- .../auto_generated/qgsdatasourceselectdialog.sip.in | 8 ++++++-- src/app/qgisapp.cpp | 2 +- src/gui/qgsbrowsertreeview.cpp | 7 ++++++- src/gui/qgsbrowsertreeview.h | 4 +++- src/gui/qgsdatasourceselectdialog.cpp | 10 ++++------ src/gui/qgsdatasourceselectdialog.h | 8 ++++++-- 9 files changed, 38 insertions(+), 17 deletions(-) diff --git a/python/PyQt6/gui/auto_generated/qgsbrowsertreeview.sip.in b/python/PyQt6/gui/auto_generated/qgsbrowsertreeview.sip.in index b66a85c92856..b1066e07390d 100644 --- a/python/PyQt6/gui/auto_generated/qgsbrowsertreeview.sip.in +++ b/python/PyQt6/gui/auto_generated/qgsbrowsertreeview.sip.in @@ -66,12 +66,14 @@ Returns ``True`` if the item was found and could be selected. .. versionadded:: 3.28 %End - void expandPath( const QString &path ); + void expandPath( const QString &path, bool selectPath = false ); %Docstring Expands out a file ``path`` in the view. The ``path`` must correspond to a valid directory existing on the file system. +Since QGIS 3.38 the ``selectPath`` argument can be used to automatically select the path too. + .. versionadded:: 3.28 %End diff --git a/python/PyQt6/gui/auto_generated/qgsdatasourceselectdialog.sip.in b/python/PyQt6/gui/auto_generated/qgsdatasourceselectdialog.sip.in index 9784db0fb3be..bb10bdfa3b08 100644 --- a/python/PyQt6/gui/auto_generated/qgsdatasourceselectdialog.sip.in +++ b/python/PyQt6/gui/auto_generated/qgsdatasourceselectdialog.sip.in @@ -65,12 +65,14 @@ Sets a description label .. versionadded:: 3.8 %End - void expandPath( const QString &path ); + void expandPath( const QString &path, bool selectPath = false ); %Docstring Expands out a file ``path`` in the view. The ``path`` must correspond to a valid directory existing on the file system. +Since QGIS 3.38 the ``selectPath`` argument can be used to automatically select the path too. + .. versionadded:: 3.28 %End @@ -176,12 +178,14 @@ Sets a description label .. versionadded:: 3.8 %End - void expandPath( const QString &path ); + void expandPath( const QString &path, bool selectPath = false ); %Docstring Expands out a file ``path`` in the view. The ``path`` must correspond to a valid directory existing on the file system. +Since QGIS 3.38 the ``selectPath`` argument can be used to automatically select the path too. + .. versionadded:: 3.28 %End diff --git a/python/gui/auto_generated/qgsbrowsertreeview.sip.in b/python/gui/auto_generated/qgsbrowsertreeview.sip.in index b66a85c92856..b1066e07390d 100644 --- a/python/gui/auto_generated/qgsbrowsertreeview.sip.in +++ b/python/gui/auto_generated/qgsbrowsertreeview.sip.in @@ -66,12 +66,14 @@ Returns ``True`` if the item was found and could be selected. .. versionadded:: 3.28 %End - void expandPath( const QString &path ); + void expandPath( const QString &path, bool selectPath = false ); %Docstring Expands out a file ``path`` in the view. The ``path`` must correspond to a valid directory existing on the file system. +Since QGIS 3.38 the ``selectPath`` argument can be used to automatically select the path too. + .. versionadded:: 3.28 %End diff --git a/python/gui/auto_generated/qgsdatasourceselectdialog.sip.in b/python/gui/auto_generated/qgsdatasourceselectdialog.sip.in index 9784db0fb3be..bb10bdfa3b08 100644 --- a/python/gui/auto_generated/qgsdatasourceselectdialog.sip.in +++ b/python/gui/auto_generated/qgsdatasourceselectdialog.sip.in @@ -65,12 +65,14 @@ Sets a description label .. versionadded:: 3.8 %End - void expandPath( const QString &path ); + void expandPath( const QString &path, bool selectPath = false ); %Docstring Expands out a file ``path`` in the view. The ``path`` must correspond to a valid directory existing on the file system. +Since QGIS 3.38 the ``selectPath`` argument can be used to automatically select the path too. + .. versionadded:: 3.28 %End @@ -176,12 +178,14 @@ Sets a description label .. versionadded:: 3.8 %End - void expandPath( const QString &path ); + void expandPath( const QString &path, bool selectPath = false ); %Docstring Expands out a file ``path`` in the view. The ``path`` must correspond to a valid directory existing on the file system. +Since QGIS 3.38 the ``selectPath`` argument can be used to automatically select the path too. + .. versionadded:: 3.28 %End diff --git a/src/app/qgisapp.cpp b/src/app/qgisapp.cpp index 18295736e3e3..c6856c998c8d 100644 --- a/src/app/qgisapp.cpp +++ b/src/app/qgisapp.cpp @@ -7755,7 +7755,7 @@ void QgisApp::changeDataSource( QgsMapLayer *layer ) const QString closestPath = QFile::exists( path ) ? path : QgsFileUtils::findClosestExistingPath( path ); const QFileInfo pathInfo( closestPath ); - dlg.expandPath( pathInfo.isDir() ? closestPath : pathInfo.dir().path() ); + dlg.expandPath( pathInfo.isDir() ? closestPath : pathInfo.dir().path(), true ); if ( source.contains( path ) ) { source.replace( path, QStringLiteral( "%2" ).arg( QUrl::fromLocalFile( closestPath ).toString(), diff --git a/src/gui/qgsbrowsertreeview.cpp b/src/gui/qgsbrowsertreeview.cpp index 6ce4a05b989d..4377084e8e0f 100644 --- a/src/gui/qgsbrowsertreeview.cpp +++ b/src/gui/qgsbrowsertreeview.cpp @@ -177,7 +177,7 @@ bool QgsBrowserTreeView::hasExpandedDescendant( const QModelIndex &index ) const return false; } -void QgsBrowserTreeView::expandPath( const QString &str ) +void QgsBrowserTreeView::expandPath( const QString &str, bool selectPath ) { const QStringList pathParts = QgsFileUtils::splitPathToComponents( str ); if ( pathParts.isEmpty() ) @@ -289,6 +289,7 @@ void QgsBrowserTreeView::expandPath( const QString &str ) currentDir = QDir( thisPath ); } + QgsDirectoryItem *lastItem = nullptr; for ( QgsDirectoryItem *i : std::as_const( pathItems ) ) { QModelIndex index = mBrowserModel->findItem( i ); @@ -297,7 +298,11 @@ void QgsBrowserTreeView::expandPath( const QString &str ) index = proxyModel->mapFromSource( index ); } expand( index ); + lastItem = i; } + + if ( selectPath && lastItem ) + setSelectedItem( lastItem ); } bool QgsBrowserTreeView::setSelectedItem( QgsDataItem *item ) diff --git a/src/gui/qgsbrowsertreeview.h b/src/gui/qgsbrowsertreeview.h index 121b8f6003ab..d91a8c08e3b9 100644 --- a/src/gui/qgsbrowsertreeview.h +++ b/src/gui/qgsbrowsertreeview.h @@ -82,9 +82,11 @@ class GUI_EXPORT QgsBrowserTreeView : public QTreeView * * The \a path must correspond to a valid directory existing on the file system. * + * Since QGIS 3.38 the \a selectPath argument can be used to automatically select the path too. + * * \since QGIS 3.28 */ - void expandPath( const QString &path ); + void expandPath( const QString &path, bool selectPath = false ); protected: diff --git a/src/gui/qgsdatasourceselectdialog.cpp b/src/gui/qgsdatasourceselectdialog.cpp index fc76099de596..af21480a89f8 100644 --- a/src/gui/qgsdatasourceselectdialog.cpp +++ b/src/gui/qgsdatasourceselectdialog.cpp @@ -194,9 +194,9 @@ void QgsDataSourceSelectWidget::setDescription( const QString &description ) } } -void QgsDataSourceSelectWidget::expandPath( const QString &path ) +void QgsDataSourceSelectWidget::expandPath( const QString &path, bool selectPath ) { - mBrowserTreeView->expandPath( path ); + mBrowserTreeView->expandPath( path, selectPath ); } void QgsDataSourceSelectWidget::setFilter() @@ -205,7 +205,6 @@ void QgsDataSourceSelectWidget::setFilter() mBrowserProxyModel.setFilterString( filter ); } - void QgsDataSourceSelectWidget::refreshModel( const QModelIndex &index ) { @@ -255,7 +254,6 @@ void QgsDataSourceSelectWidget::setValid( bool valid ) } - void QgsDataSourceSelectWidget::setFilterSyntax( QAction *action ) { if ( !action ) @@ -354,9 +352,9 @@ void QgsDataSourceSelectDialog::setDescription( const QString &description ) mWidget->setDescription( description ); } -void QgsDataSourceSelectDialog::expandPath( const QString &path ) +void QgsDataSourceSelectDialog::expandPath( const QString &path, bool selectPath ) { - mWidget->expandPath( path ); + mWidget->expandPath( path, selectPath ); } QgsMimeDataUtils::Uri QgsDataSourceSelectDialog::uri() const diff --git a/src/gui/qgsdatasourceselectdialog.h b/src/gui/qgsdatasourceselectdialog.h index d9668835d1c3..95861cab06fc 100644 --- a/src/gui/qgsdatasourceselectdialog.h +++ b/src/gui/qgsdatasourceselectdialog.h @@ -82,9 +82,11 @@ class GUI_EXPORT QgsDataSourceSelectWidget: public QgsPanelWidget, private Ui::Q * * The \a path must correspond to a valid directory existing on the file system. * + * Since QGIS 3.38 the \a selectPath argument can be used to automatically select the path too. + * * \since QGIS 3.28 */ - void expandPath( const QString &path ); + void expandPath( const QString &path, bool selectPath = false ); /** * Returns the (possibly invalid) uri of the selected data source @@ -195,9 +197,11 @@ class GUI_EXPORT QgsDataSourceSelectDialog: public QDialog * * The \a path must correspond to a valid directory existing on the file system. * + * Since QGIS 3.38 the \a selectPath argument can be used to automatically select the path too. + * * \since QGIS 3.28 */ - void expandPath( const QString &path ); + void expandPath( const QString &path, bool selectPath = false ); /** * Returns the (possibly invalid) uri of the selected data source