Skip to content

Commit

Permalink
Dropping a file onto a file widget in directory mode should set the w…
Browse files Browse the repository at this point in the history
…idget

to the folder containing that file, not reject the event entirely
  • Loading branch information
nyalldawson committed Feb 8, 2019
1 parent 73fa58a commit d741c73
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
29 changes: 25 additions & 4 deletions src/gui/qgsfilewidget.cpp
Expand Up @@ -435,10 +435,31 @@ QString QgsFileDropEdit::acceptableFilePath( QDropEvent *event ) const
Q_FOREACH ( const QUrl &url, event->mimeData()->urls() )
{
QFileInfo file( url.toLocalFile() );
if ( ( mStorageMode != QgsFileWidget::GetDirectory && file.isFile() &&
( mAcceptableExtensions.isEmpty() || mAcceptableExtensions.contains( file.suffix(), Qt::CaseInsensitive ) ) )
|| ( mStorageMode == QgsFileWidget::GetDirectory && file.isDir() ) )
paths.append( file.filePath() );
switch ( mStorageMode )
{
case QgsFileWidget::GetFile:
case QgsFileWidget::GetMultipleFiles:
case QgsFileWidget::SaveFile:
{
if ( file.isFile() && ( mAcceptableExtensions.isEmpty() || mAcceptableExtensions.contains( file.suffix(), Qt::CaseInsensitive ) ) )
paths.append( file.filePath() );

break;
}

case QgsFileWidget::GetDirectory:
{
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;
}
}
}
}
if ( paths.size() > 1 )
Expand Down
6 changes: 4 additions & 2 deletions tests/src/gui/testqgsfilewidget.cpp
Expand Up @@ -129,9 +129,11 @@ void TestQgsFileWidget::testDroppedFiles()
// try with folders
w->setStorageMode( QgsFileWidget::GetDirectory );
w->setFilePath( QString() );
// should be rejected
// dropping a file should accept only the folder containing that file
mime->setUrls( QList<QUrl>() << QUrl::fromLocalFile( TEST_DATA_DIR + QStringLiteral( "/mesh/quad_and_triangle.2dm" ) ) );
event.reset( new QDropEvent( QPointF( 1, 1 ), Qt::CopyAction, mime.get(), Qt::LeftButton, Qt::NoModifier ) );
qobject_cast< QgsFileDropEdit * >( w->lineEdit() )->dropEvent( event.get() );
QVERIFY( w->lineEdit()->text().isEmpty() );
QCOMPARE( w->lineEdit()->text(), QString( TEST_DATA_DIR ) + QStringLiteral( "/mesh" ) );

// but dropping a folder should work
mime->setUrls( QList<QUrl>() << QUrl::fromLocalFile( TEST_DATA_DIR ) );
Expand Down

0 comments on commit d741c73

Please sign in to comment.