Skip to content

Commit

Permalink
More robust multiple paths split and utility static method
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Aug 24, 2017
1 parent cc4cee8 commit b947406
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 17 deletions.
12 changes: 11 additions & 1 deletion python/gui/qgsfilewidget.sip
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,20 @@ class QgsFileWidget : QWidget

QString filePath();
%Docstring
Returns the current file path
Returns the current file path(s)
when multiple files are selected, they are quoted and separated
by a single space (for example: '"/path/foo" "path/bar"')
.. seealso:: filePaths
:rtype: str
%End

static QStringList splitFilePaths( const QString &path );
%Docstring
Split the the quoted and space separated ``path`` and returns a QString list
.. seealso:: filePath
:rtype: list of str
%End

void setFilePath( QString path );
%Docstring
Sets the file path
Expand Down
20 changes: 20 additions & 0 deletions src/gui/qgsfilewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ QString QgsFileWidget::filePath()
return mFilePath;
}

QStringList QgsFileWidget::splitFilePaths( const QString &path )
{
QStringList paths;
for ( auto pathsPart : path.split( QRegExp( "\"\\s+\"" ), QString::SkipEmptyParts ) )
{
paths.append( pathsPart.remove( QRegExp( "(^\\s*\")|(\"\\s*)" ) ) );
}
return paths;
}

void QgsFileWidget::setFilePath( QString path )
{
if ( path == QgsApplication::nullRepresentation() )
Expand All @@ -86,6 +96,7 @@ void QgsFileWidget::setFilePath( QString path )

//will trigger textEdited slot
mLineEdit->setValue( path );

}

void QgsFileWidget::setReadOnly( bool readOnly )
Expand Down Expand Up @@ -130,6 +141,15 @@ void QgsFileWidget::textEdited( const QString &path )
{
mFilePath = path;
mLinkLabel->setText( toUrl( path ) );
// Show tooltip if multiple files are selected
if ( path.contains( QStringLiteral( "\" \"" ) ) )
{
mLineEdit->setToolTip( tr( "Selected files:<br><ul><li>%1</li></ul><br>" ).arg( splitFilePaths( path ).join( QStringLiteral( "</li><li>" ) ) ) );
}
else
{
mLineEdit->setToolTip( QString() );
}
emit fileChanged( mFilePath );
}

Expand Down
17 changes: 13 additions & 4 deletions src/gui/qgsfilewidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,20 @@ class GUI_EXPORT QgsFileWidget : public QWidget
*/
explicit QgsFileWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr );

//! Returns the current file path
/**
* \brief Returns the current file path(s)
* when multiple files are selected, they are quoted and separated
* by a single space (for example: '"/path/foo" "path/bar"')
* \see filePaths
*/
QString filePath();

/**
* \brief Split the the quoted and space separated \a path and returns a QString list
* \see filePath
*/
static QStringList splitFilePaths( const QString &path );

//! Sets the file path
void setFilePath( QString path );

Expand Down Expand Up @@ -212,9 +223,7 @@ class GUI_EXPORT QgsFileDropEdit: public QgsFilterLineEdit

private:

/**
Return file name if object meets drop criteria.
*/
//! Return file name if object meets drop criteria.
QString acceptableFilePath( QDropEvent *event ) const;

QStringList mAcceptableExtensions;
Expand Down
13 changes: 2 additions & 11 deletions src/providers/gdal/qgsgdalsourceselect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,9 @@ QgsGdalSourceSelect::~QgsGdalSourceSelect()

void QgsGdalSourceSelect::addButtonClicked()
{
// Check if multiple files where selected
if ( mRasterPath.startsWith( '"' ) )
Q_FOREACH ( const QString &path, QgsFileWidget::splitFilePaths( mRasterPath ) )
{
for ( QString path : mRasterPath.split( QRegExp( "\"\\s+\"" ), QString::SkipEmptyParts ) )
{
QString cleanPath( path.remove( '"' ) );
emit addRasterLayer( cleanPath, QFileInfo( cleanPath ).baseName(), QStringLiteral( "gdal" ) );
}
}
else
{
emit addRasterLayer( mRasterPath, QFileInfo( mRasterPath ).baseName(), QStringLiteral( "gdal" ) );
emit addRasterLayer( path, QFileInfo( path ).baseName(), QStringLiteral( "gdal" ) );
}
}

Expand Down
18 changes: 17 additions & 1 deletion tests/src/gui/testqgsfilewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class TestQgsFileWidget: public QObject
void toUrl();
void testDroppedFiles();
void testMultipleFiles();
void testSplitFilePaths();

};

Expand Down Expand Up @@ -143,7 +144,7 @@ void TestQgsFileWidget::testDroppedFiles()
void TestQgsFileWidget::testMultipleFiles()
{
QgsFileWidget *w = new QgsFileWidget();
w->setStorageMode( QgsFileWidget::GetFile );
w->setStorageMode( QgsFileWidget::GetMultipleFiles );

std::unique_ptr< QMimeData > mime( new QMimeData() );
mime->setUrls( QList<QUrl>() << QUrl::fromLocalFile( TEST_DATA_DIR + QStringLiteral( "/bug5598.shp" ) )
Expand All @@ -155,5 +156,20 @@ void TestQgsFileWidget::testMultipleFiles()
}



void TestQgsFileWidget::testSplitFilePaths()
{
const QString path = QString( TEST_DATA_DIR + QStringLiteral( "/bug5598.shp" ) );
QCOMPARE( QgsFileWidget::splitFilePaths( QStringLiteral( "\"%1\" \"%1\"" ).arg( path ) ), QStringList() << path << path );
QCOMPARE( QgsFileWidget::splitFilePaths( QStringLiteral( "\"%1\" \"%1\"" ).arg( path ) ), QStringList() << path << path );
QCOMPARE( QgsFileWidget::splitFilePaths( QStringLiteral( " \"%1\" \"%1\"" ).arg( path ) ), QStringList() << path << path );
QCOMPARE( QgsFileWidget::splitFilePaths( QStringLiteral( " \"%1\" \"%1\" " ).arg( path ) ), QStringList() << path << path );
QCOMPARE( QgsFileWidget::splitFilePaths( QStringLiteral( "\"%1\" \"%1\" " ).arg( path ) ), QStringList() << path << path );
QCOMPARE( QgsFileWidget::splitFilePaths( path ), QStringList() << path );
}




QGSTEST_MAIN( TestQgsFileWidget )
#include "testqgsfilewidget.moc"

0 comments on commit b947406

Please sign in to comment.