Skip to content
Permalink
Browse files

QgsFileWidget should also accept dropped plain text paths

  • Loading branch information
nyalldawson committed Feb 8, 2019
1 parent 8ad2d72 commit 324fb40500f9c448c628b5567ae34a107641947d
Showing with 20 additions and 4 deletions.
  1. +10 −2 src/gui/qgsfilewidget.cpp
  2. +10 −2 tests/src/gui/testqgsfilewidget.cpp
@@ -54,6 +54,7 @@ QgsFileWidget::QgsFileWidget( QWidget *parent )

// otherwise, use the traditional QLineEdit subclass
mLineEdit = new QgsFileDropEdit( this );
mLineEdit->setDragEnabled( true );
connect( mLineEdit, &QLineEdit::textChanged, this, &QgsFileWidget::textEdited );
mLayout->addWidget( mLineEdit );

@@ -438,16 +439,23 @@ QString QgsFileDropEdit::acceptableFilePath( QDropEvent *event ) const
rawPaths.reserve( urls.count() );
for ( const QUrl &url : urls )
{
rawPaths.append( url.toLocalFile() );
const QString local = url.toLocalFile();
if ( !rawPaths.contains( local ) )
rawPaths.append( local );
}
}

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

if ( !event->mimeData()->text().isEmpty() && !rawPaths.contains( event->mimeData()->text() ) )
rawPaths.append( event->mimeData()->text() );

paths.reserve( rawPaths.count() );
for ( const QString &path : qgis::as_const( rawPaths ) )
{
QFileInfo file( path );
@@ -126,6 +126,13 @@ void TestQgsFileWidget::testDroppedFiles()
qobject_cast< QgsFileDropEdit * >( w->lineEdit() )->dropEvent( event.get() );
QCOMPARE( w->lineEdit()->text(), QString( TEST_DATA_DIR ) + QStringLiteral( "/mesh/quad_and_triangle.txt" ) );

// plain text should also be permitted
mime = qgis::make_unique< QMimeData >();
mime->setText( 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() );
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 ) );
@@ -183,11 +190,12 @@ void TestQgsFileWidget::testMultipleFiles()

std::unique_ptr< QMimeData > mime( new QMimeData() );
mime->setUrls( QList<QUrl>() << QUrl::fromLocalFile( TEST_DATA_DIR + QStringLiteral( "/bug5598.shp" ) )
<< QUrl::fromLocalFile( TEST_DATA_DIR + QStringLiteral( "/bug5598.shp" ) ) );
<< QUrl::fromLocalFile( TEST_DATA_DIR + QStringLiteral( "/elev.gpx" ) ) );
std::unique_ptr< QDropEvent > event( 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(), QStringLiteral( "\"%1\" \"%1\"" ).arg( TEST_DATA_DIR + QStringLiteral( "/bug5598.shp" ) ) );
QCOMPARE( w->lineEdit()->text(), QStringLiteral( "\"%1\" \"%2\"" ).arg( TEST_DATA_DIR + QStringLiteral( "/bug5598.shp" ) )
.arg( TEST_DATA_DIR + QStringLiteral( "/elev.gpx" ) ) );
}


0 comments on commit 324fb40

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