Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow dropping paths onto data source select dialog to expand browser paths #57384

Merged
merged 3 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 11 additions & 2 deletions python/PyQt6/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 @@ -101,6 +103,11 @@ Apply filter to the model
Scroll to last selected index and expand it's children
%End

virtual void dragEnterEvent( QDragEnterEvent *event );

virtual void dropEvent( QDropEvent *event );


signals:

void validationChanged( bool isValid );
Expand Down Expand Up @@ -176,12 +183,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
13 changes: 11 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 @@ -101,6 +103,11 @@ Apply filter to the model
Scroll to last selected index and expand it's children
%End

virtual void dragEnterEvent( QDragEnterEvent *event );

virtual void dropEvent( QDropEvent *event );


signals:

void validationChanged( bool isValid );
Expand Down Expand Up @@ -176,12 +183,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 src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7753,7 +7753,9 @@ void QgisApp::changeDataSource( QgsMapLayer *layer )
{
const QString path = sourceParts.value( QStringLiteral( "path" ) ).toString();
const QString closestPath = QFile::exists( path ) ? path : QgsFileUtils::findClosestExistingPath( path );
dlg.expandPath( closestPath );

const QFileInfo pathInfo( closestPath );
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
65 changes: 59 additions & 6 deletions src/gui/qgsdatasourceselectdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <QFileInfo>
#include <QUrl>
#include <QActionGroup>
#include <QDir>

QgsDataSourceSelectWidget::QgsDataSourceSelectWidget(
QgsBrowserGuiModel *browserModel,
Expand Down Expand Up @@ -116,6 +117,8 @@ QgsDataSourceSelectWidget::QgsDataSourceSelectWidget(
{
mActionShowFilter->trigger();
}

setAcceptDrops( true );
}

QgsDataSourceSelectWidget::~QgsDataSourceSelectWidget() = default;
Expand Down Expand Up @@ -145,6 +148,58 @@ void QgsDataSourceSelectWidget::showEvent( QShowEvent *e )
}
}

QString QgsDataSourceSelectWidget::acceptableFilePath( QDropEvent *event ) const
{
if ( event->mimeData()->hasUrls() )
{
const QList< QUrl > urls = event->mimeData()->urls();
for ( const QUrl &url : urls )
{
const QString local = url.toLocalFile();
if ( local.isEmpty() )
continue;

if ( QFile::exists( local ) )
{
return local;
}
}
}
return QString();
}

void QgsDataSourceSelectWidget::dragEnterEvent( QDragEnterEvent *event )
{
const QString filePath = acceptableFilePath( event );
if ( !filePath.isEmpty() )
{
event->acceptProposedAction();
}
else
{
event->ignore();
}
}

void QgsDataSourceSelectWidget::dropEvent( QDropEvent *event )
{
const QString filePath = acceptableFilePath( event );
if ( !filePath.isEmpty() )
{
event->acceptProposedAction();

const QFileInfo fi( filePath );
if ( fi.isDir() )
{
expandPath( filePath, true );
}
else
{
expandPath( fi.dir().path(), true );
}
}
}

void QgsDataSourceSelectWidget::showFilterWidget( bool visible )
{
QgsSettings().setValue( QStringLiteral( "datasourceSelectFilterVisible" ), visible, QgsSettings::Section::Gui );
Expand Down Expand Up @@ -194,9 +249,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 +260,6 @@ void QgsDataSourceSelectWidget::setFilter()
mBrowserProxyModel.setFilterString( filter );
}


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

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

}


void QgsDataSourceSelectWidget::setFilterSyntax( QAction *action )
{
if ( !action )
Expand Down Expand Up @@ -354,9 +407,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
14 changes: 12 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 All @@ -102,6 +104,9 @@ class GUI_EXPORT QgsDataSourceSelectWidget: public QgsPanelWidget, private Ui::Q
//! Scroll to last selected index and expand it's children
void showEvent( QShowEvent *e ) override;

void dragEnterEvent( QDragEnterEvent *event ) override;
void dropEvent( QDropEvent *event ) override;

signals:

/**
Expand Down Expand Up @@ -135,6 +140,9 @@ class GUI_EXPORT QgsDataSourceSelectWidget: public QgsPanelWidget, private Ui::Q

void setValid( bool valid );

//! Returns file name if object meets drop criteria.
QString acceptableFilePath( QDropEvent *event ) const;

QgsBrowserProxyModel mBrowserProxyModel;
QgsBrowserGuiModel *mBrowserModel = nullptr;
QgsMimeDataUtils::Uri mUri;
Expand Down Expand Up @@ -195,9 +203,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
Loading