Skip to content
Permalink
Browse files

Dropping a file onto a file widget in directory mode should set the w…

…idget

to the folder containing that file, not reject the event entirely
  • Loading branch information
nyalldawson committed Feb 8, 2019
1 parent 73fa58a commit d741c73d9b3036e3bf91f54579776a23c940d93d
Showing with 29 additions and 6 deletions.
  1. +25 −4 src/gui/qgsfilewidget.cpp
  2. +4 −2 tests/src/gui/testqgsfilewidget.cpp
@@ -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 )
@@ -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 ) );

0 comments on commit d741c73

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