Skip to content
Permalink
Browse files

More robust multiple paths split and utility static method

  • Loading branch information
elpaso committed Aug 24, 2017
1 parent cc4cee8 commit b9474061558e6bf0ba0c79a66974035f6079fdba
@@ -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
@@ -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() )
@@ -86,6 +96,7 @@ void QgsFileWidget::setFilePath( QString path )

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

}

void QgsFileWidget::setReadOnly( bool readOnly )
@@ -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 );
}

@@ -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 );

@@ -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;
@@ -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" ) );
}
}

@@ -31,6 +31,7 @@ class TestQgsFileWidget: public QObject
void toUrl();
void testDroppedFiles();
void testMultipleFiles();
void testSplitFilePaths();

};

@@ -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" ) )
@@ -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.
You can’t perform that action at this time.