Skip to content

Commit

Permalink
Allow dropping paths onto data source select dialog to expand browser…
Browse files Browse the repository at this point in the history
… paths

Makes it a bit easier to fix data sources for files in deep paths
  • Loading branch information
nyalldawson committed May 8, 2024
1 parent fc0c496 commit d95897d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,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
5 changes: 5 additions & 0 deletions python/gui/auto_generated/qgsdatasourceselectdialog.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -103,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
55 changes: 55 additions & 0 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
6 changes: 6 additions & 0 deletions src/gui/qgsdatasourceselectdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,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 @@ -137,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

0 comments on commit d95897d

Please sign in to comment.