Skip to content
Permalink
Browse files

File widget should also accept drops from browser

  • Loading branch information
nyalldawson committed Feb 8, 2019
1 parent d741c73 commit 2857f585ae94d8d88d25e8a57f3e883d03d63c98
Showing with 52 additions and 21 deletions.
  1. +37 −21 src/gui/qgsfilewidget.cpp
  2. +15 −0 tests/src/gui/testqgsfilewidget.cpp
@@ -30,6 +30,7 @@
#include "qgsproject.h"
#include "qgsapplication.h"
#include "qgsfileutils.h"
#include "qgsmimedatautils.h"

QgsFileWidget::QgsFileWidget( QWidget *parent )
: QWidget( parent )
@@ -429,39 +430,54 @@ void QgsFileDropEdit::setFilters( const QString &filters )

QString QgsFileDropEdit::acceptableFilePath( QDropEvent *event ) const
{
QStringList rawPaths;
QStringList paths;
if ( event->mimeData()->hasUrls() )
{
Q_FOREACH ( const QUrl &url, event->mimeData()->urls() )
const QList< QUrl > urls = event->mimeData()->urls();
rawPaths.reserve( urls.count() );
for ( const QUrl &url : urls )
{
QFileInfo file( url.toLocalFile() );
switch ( mStorageMode )
rawPaths.append( url.toLocalFile() );
}
}

QgsMimeDataUtils::UriList lst = QgsMimeDataUtils::decodeUriList( event->mimeData() );
for ( const QgsMimeDataUtils::Uri &u : lst )
{
rawPaths.append( u.uri );
}

for ( const QString &path : qgis::as_const( rawPaths ) )
{
QFileInfo file( path );
switch ( mStorageMode )
{
case QgsFileWidget::GetFile:
case QgsFileWidget::GetMultipleFiles:
case QgsFileWidget::SaveFile:
{
case QgsFileWidget::GetFile:
case QgsFileWidget::GetMultipleFiles:
case QgsFileWidget::SaveFile:
{
if ( file.isFile() && ( mAcceptableExtensions.isEmpty() || mAcceptableExtensions.contains( file.suffix(), Qt::CaseInsensitive ) ) )
paths.append( file.filePath() );
if ( file.isFile() && ( mAcceptableExtensions.isEmpty() || mAcceptableExtensions.contains( file.suffix(), Qt::CaseInsensitive ) ) )
paths.append( file.filePath() );

break;
}
break;
}

case QgsFileWidget::GetDirectory:
case QgsFileWidget::GetDirectory:
{
if ( file.isDir() )
paths.append( file.filePath() );
else if ( file.isFile() )
{
if ( file.isDir() )
paths.append( file.filePath() );
else if ( file.isFile() )
{
// folder mode, but a file dropped. So get folder name from file
paths.append( file.absolutePath() );
}

break;
// folder mode, but a file dropped. So get folder name from file
paths.append( file.absolutePath() );
}

break;
}
}
}

if ( paths.size() > 1 )
{
return QStringLiteral( "\"%1\"" ).arg( paths.join( QStringLiteral( "\" \"" ) ) );
@@ -17,6 +17,7 @@
#include "qgstest.h"

#include "qgsfilewidget.h"
#include "qgsmimedatautils.h"
#include <memory>

class TestQgsFileWidget: public QObject
@@ -103,6 +104,20 @@ void TestQgsFileWidget::testDroppedFiles()
qobject_cast< QgsFileDropEdit * >( w->lineEdit() )->dropEvent( event.get() );
QCOMPARE( w->lineEdit()->text(), TEST_DATA_DIR + QStringLiteral( "/bug5598.shp" ) );

// also should support files dragged from browser
mime->setUrls( QList<QUrl>() );
QgsMimeDataUtils::Uri uri;
uri.uri = TEST_DATA_DIR + QStringLiteral( "/mesh/quad_and_triangle.2dm" );
QgsMimeDataUtils::UriList uriList;
uriList << uri;
mime.reset( QgsMimeDataUtils::encodeUriList( uriList ) );
event.reset( new QDropEvent( QPointF( 1, 1 ), Qt::CopyAction, mime.get(), Qt::LeftButton, Qt::NoModifier ) );
qobject_cast< QgsFileDropEdit * >( w->lineEdit() )->dropEvent( event.get() );
QCOMPARE( w->lineEdit()->text(), TEST_DATA_DIR + QStringLiteral( "/mesh/quad_and_triangle.2dm" ) );

mime.reset( new QMimeData() );
mime->setUrls( QList<QUrl>() << QUrl::fromLocalFile( TEST_DATA_DIR + QStringLiteral( "/bug5598.shp" ) ) );
event.reset( new QDropEvent( QPointF( 1, 1 ), Qt::CopyAction, mime.get(), Qt::LeftButton, Qt::NoModifier ) );
// with file filter
w->setFilter( QStringLiteral( "Data (*.shp)" ) );
w->setFilePath( QString() );

0 comments on commit 2857f58

Please sign in to comment.
You can’t perform that action at this time.