Skip to content

Commit

Permalink
[bugfix] Fix double escping of colon in QgsMimeDataUtils
Browse files Browse the repository at this point in the history
Fixes #19195 - WMS layer loaded in incorrect CRS when using drag and drop from Browser panel
  • Loading branch information
elpaso committed Jun 18, 2018
1 parent 59fa2ce commit cb91176
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/core/qgsmimedatautils.cpp
Expand Up @@ -206,11 +206,13 @@ QByteArray QgsMimeDataUtils::layerTreeNodesToUriList( const QList<QgsLayerTreeNo
QString QgsMimeDataUtils::encode( const QStringList &items )
{
QString encoded;
// Do not escape colon twice
QRegExp re( "([^\\\\]):" );
Q_FOREACH ( const QString &item, items )
{
QString str = item;
str.replace( '\\', QLatin1String( "\\\\" ) );
str.replace( ':', QLatin1String( "\\:" ) );
str.replace( re, QLatin1String( "\\1\\:" ) );
encoded += str + ':';
}
return encoded.left( encoded.length() - 1 );
Expand Down
3 changes: 3 additions & 0 deletions src/core/qgsmimedatautils.h
Expand Up @@ -107,6 +107,9 @@ class CORE_EXPORT QgsMimeDataUtils
static QStringList decode( const QString &encoded );
static QByteArray uriListToByteArray( const UriList &layers );


friend class TestQgsMimeDataUtils;

};

Q_DECLARE_METATYPE( QgsMimeDataUtils::UriList )
Expand Down
1 change: 1 addition & 0 deletions tests/src/core/CMakeLists.txt
Expand Up @@ -194,6 +194,7 @@ SET(TESTS
testqgsmeshlayerrenderer.cpp
testqgslayerdefinition.cpp
testqgssqliteutils.cpp
testqgsmimedatautils.cpp
)

IF(WITH_QTWEBKIT)
Expand Down
4 changes: 2 additions & 2 deletions tests/src/core/testqgslayerdefinition.cpp
@@ -1,5 +1,5 @@
/***************************************************************************
testqgsfilefiledownloader.cpp
testqgslayerdefinition.cpp
--------------------------------------
Date : 07.06.2018
Copyright : (C) 2018 Alessandro Pasotti
Expand Down Expand Up @@ -37,7 +37,7 @@ class TestQgsLayerDefinition: public QObject
void cleanup(); // will be called after every testfunction.

/**
* test that findLayers()
* test findLayers()
*/
void testFindLayers();

Expand Down
111 changes: 111 additions & 0 deletions tests/src/core/testqgsmimedatautils.cpp
@@ -0,0 +1,111 @@
/***************************************************************************
testqgsmimedatautils.cpp
--------------------------------------
Date : 18.06.2018
Copyright : (C) 2018 Alessandro Pasotti
Email : elpaso at itopen dot it
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/


#include "qgstest.h"
#include <QObject>

#include <qgsmimedatautils.h>

const QString TEST_ENCODED_DATA( "raster:wms:A Fancy WMS From Ciriè City:crs=EPSG\\:2036&dpiMode=7&format=image/png&layers=lidar&styles=default"
"&url=https\\://geoegl.msp.gouv.qc.:EPSG\\\\:2036\\:EPSG\\\\:3857:image/tiff\\:image/jpeg" );

class TestQgsMimeDataUtils: public QObject
{
Q_OBJECT
public:
TestQgsMimeDataUtils() = default;

private slots:
void initTestCase(); // will be called before the first testfunction is executed.
void cleanupTestCase(); // will be called after the last testfunction was executed.
void init(); // will be called before each testfunction is executed.
void cleanup(); // will be called after every testfunction.

/**
* test findLayers()
*/
void testEncodeDecode();

};


void TestQgsMimeDataUtils::initTestCase()
{
QgsApplication::init();
QgsApplication::initQgis();

}

void TestQgsMimeDataUtils::cleanupTestCase()
{
QgsApplication::exitQgis();
}

void TestQgsMimeDataUtils::init()
{
}


void TestQgsMimeDataUtils::cleanup()
{
}

void TestQgsMimeDataUtils::testEncodeDecode()
{

QgsMimeDataUtils::Uri uri;
uri.layerType = QStringLiteral( "raster" );
uri.name = QStringLiteral( "A Fancy WMS From Ciriè City" );
uri.providerKey = QStringLiteral( "wms" );
uri.supportedCrs << QStringLiteral( "EPSG:2036" ) << QStringLiteral( "EPSG:3857" ) ;
uri.supportedFormats << QStringLiteral( "image/tiff" ) << QStringLiteral( "image/jpeg" );
uri.uri = QStringLiteral( "crs=EPSG:2036&dpiMode=7&format=image/png&layers=lidar&styles=default&url=https://geoegl.msp.gouv.qc." );

QgsMimeDataUtils::UriList uriList;
uriList << uri;

QMimeData *mimeData = QgsMimeDataUtils::encodeUriList( uriList );

QgsMimeDataUtils::Uri uriDecoded( QgsMimeDataUtils::decodeUriList( mimeData )[0] );

QCOMPARE( uriDecoded.name, uri.name );
QCOMPARE( uriDecoded.providerKey, uri.providerKey );
QCOMPARE( uriDecoded.supportedFormats, uri.supportedFormats );
QCOMPARE( uriDecoded.uri, uri.uri );
QCOMPARE( uriDecoded.supportedCrs, uri.supportedCrs );

QgsMimeDataUtils::decodeUriList( mimeData );

// Encode representation:
QString data( uri.data() );

QCOMPARE( data, TEST_ENCODED_DATA );

QStringList fragments( QgsMimeDataUtils::decode( data ) );

QCOMPARE( fragments[0], "raster" );
QCOMPARE( fragments[1], "wms" );
QCOMPARE( fragments[2], "A Fancy WMS From Ciriè City" );
QCOMPARE( fragments[3], "crs=EPSG:2036&dpiMode=7&format=image/png&layers=lidar&styles=default&url=https://geoegl.msp.gouv.qc." );
QCOMPARE( fragments[4], "EPSG\\:2036:EPSG\\:3857" );

}


QGSTEST_MAIN( TestQgsMimeDataUtils )
#include "testqgsmimedatautils.moc"


0 comments on commit cb91176

Please sign in to comment.