Skip to content

Commit

Permalink
Set initial selected item to matching path when changing data source too
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 8, 2024
1 parent cd7cc86 commit fc0c496
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 17 deletions.
4 changes: 3 additions & 1 deletion python/PyQt6/gui/auto_generated/qgsbrowsertreeview.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion python/gui/auto_generated/qgsbrowsertreeview.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 6 additions & 2 deletions python/gui/auto_generated/qgsdatasourceselectdialog.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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( "<a href=\"%1\">%2</a>" ).arg( QUrl::fromLocalFile( closestPath ).toString(),
Expand Down
7 changes: 6 additions & 1 deletion src/gui/qgsbrowsertreeview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() )
Expand Down Expand Up @@ -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 );
Expand All @@ -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 )
Expand Down
4 changes: 3 additions & 1 deletion src/gui/qgsbrowsertreeview.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
10 changes: 4 additions & 6 deletions src/gui/qgsdatasourceselectdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -205,7 +205,6 @@ void QgsDataSourceSelectWidget::setFilter()
mBrowserProxyModel.setFilterString( filter );
}


void QgsDataSourceSelectWidget::refreshModel( const QModelIndex &index )
{

Expand Down Expand Up @@ -255,7 +254,6 @@ void QgsDataSourceSelectWidget::setValid( bool valid )

}


void QgsDataSourceSelectWidget::setFilterSyntax( QAction *action )
{
if ( !action )
Expand Down Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions src/gui/qgsdatasourceselectdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fc0c496

Please sign in to comment.