Skip to content

Commit cb91176

Browse files
committed
[bugfix] Fix double escping of colon in QgsMimeDataUtils
Fixes #19195 - WMS layer loaded in incorrect CRS when using drag and drop from Browser panel
1 parent 59fa2ce commit cb91176

File tree

5 files changed

+120
-3
lines changed

5 files changed

+120
-3
lines changed

src/core/qgsmimedatautils.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,13 @@ QByteArray QgsMimeDataUtils::layerTreeNodesToUriList( const QList<QgsLayerTreeNo
206206
QString QgsMimeDataUtils::encode( const QStringList &items )
207207
{
208208
QString encoded;
209+
// Do not escape colon twice
210+
QRegExp re( "([^\\\\]):" );
209211
Q_FOREACH ( const QString &item, items )
210212
{
211213
QString str = item;
212214
str.replace( '\\', QLatin1String( "\\\\" ) );
213-
str.replace( ':', QLatin1String( "\\:" ) );
215+
str.replace( re, QLatin1String( "\\1\\:" ) );
214216
encoded += str + ':';
215217
}
216218
return encoded.left( encoded.length() - 1 );

src/core/qgsmimedatautils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ class CORE_EXPORT QgsMimeDataUtils
107107
static QStringList decode( const QString &encoded );
108108
static QByteArray uriListToByteArray( const UriList &layers );
109109

110+
111+
friend class TestQgsMimeDataUtils;
112+
110113
};
111114

112115
Q_DECLARE_METATYPE( QgsMimeDataUtils::UriList )

tests/src/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ SET(TESTS
194194
testqgsmeshlayerrenderer.cpp
195195
testqgslayerdefinition.cpp
196196
testqgssqliteutils.cpp
197+
testqgsmimedatautils.cpp
197198
)
198199

199200
IF(WITH_QTWEBKIT)

tests/src/core/testqgslayerdefinition.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/***************************************************************************
2-
testqgsfilefiledownloader.cpp
2+
testqgslayerdefinition.cpp
33
--------------------------------------
44
Date : 07.06.2018
55
Copyright : (C) 2018 Alessandro Pasotti
@@ -37,7 +37,7 @@ class TestQgsLayerDefinition: public QObject
3737
void cleanup(); // will be called after every testfunction.
3838

3939
/**
40-
* test that findLayers()
40+
* test findLayers()
4141
*/
4242
void testFindLayers();
4343

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/***************************************************************************
2+
testqgsmimedatautils.cpp
3+
--------------------------------------
4+
Date : 18.06.2018
5+
Copyright : (C) 2018 Alessandro Pasotti
6+
Email : elpaso at itopen dot it
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
17+
#include "qgstest.h"
18+
#include <QObject>
19+
20+
#include <qgsmimedatautils.h>
21+
22+
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"
23+
"&url=https\\://geoegl.msp.gouv.qc.:EPSG\\\\:2036\\:EPSG\\\\:3857:image/tiff\\:image/jpeg" );
24+
25+
class TestQgsMimeDataUtils: public QObject
26+
{
27+
Q_OBJECT
28+
public:
29+
TestQgsMimeDataUtils() = default;
30+
31+
private slots:
32+
void initTestCase(); // will be called before the first testfunction is executed.
33+
void cleanupTestCase(); // will be called after the last testfunction was executed.
34+
void init(); // will be called before each testfunction is executed.
35+
void cleanup(); // will be called after every testfunction.
36+
37+
/**
38+
* test findLayers()
39+
*/
40+
void testEncodeDecode();
41+
42+
};
43+
44+
45+
void TestQgsMimeDataUtils::initTestCase()
46+
{
47+
QgsApplication::init();
48+
QgsApplication::initQgis();
49+
50+
}
51+
52+
void TestQgsMimeDataUtils::cleanupTestCase()
53+
{
54+
QgsApplication::exitQgis();
55+
}
56+
57+
void TestQgsMimeDataUtils::init()
58+
{
59+
}
60+
61+
62+
void TestQgsMimeDataUtils::cleanup()
63+
{
64+
}
65+
66+
void TestQgsMimeDataUtils::testEncodeDecode()
67+
{
68+
69+
QgsMimeDataUtils::Uri uri;
70+
uri.layerType = QStringLiteral( "raster" );
71+
uri.name = QStringLiteral( "A Fancy WMS From Ciriè City" );
72+
uri.providerKey = QStringLiteral( "wms" );
73+
uri.supportedCrs << QStringLiteral( "EPSG:2036" ) << QStringLiteral( "EPSG:3857" ) ;
74+
uri.supportedFormats << QStringLiteral( "image/tiff" ) << QStringLiteral( "image/jpeg" );
75+
uri.uri = QStringLiteral( "crs=EPSG:2036&dpiMode=7&format=image/png&layers=lidar&styles=default&url=https://geoegl.msp.gouv.qc." );
76+
77+
QgsMimeDataUtils::UriList uriList;
78+
uriList << uri;
79+
80+
QMimeData *mimeData = QgsMimeDataUtils::encodeUriList( uriList );
81+
82+
QgsMimeDataUtils::Uri uriDecoded( QgsMimeDataUtils::decodeUriList( mimeData )[0] );
83+
84+
QCOMPARE( uriDecoded.name, uri.name );
85+
QCOMPARE( uriDecoded.providerKey, uri.providerKey );
86+
QCOMPARE( uriDecoded.supportedFormats, uri.supportedFormats );
87+
QCOMPARE( uriDecoded.uri, uri.uri );
88+
QCOMPARE( uriDecoded.supportedCrs, uri.supportedCrs );
89+
90+
QgsMimeDataUtils::decodeUriList( mimeData );
91+
92+
// Encode representation:
93+
QString data( uri.data() );
94+
95+
QCOMPARE( data, TEST_ENCODED_DATA );
96+
97+
QStringList fragments( QgsMimeDataUtils::decode( data ) );
98+
99+
QCOMPARE( fragments[0], "raster" );
100+
QCOMPARE( fragments[1], "wms" );
101+
QCOMPARE( fragments[2], "A Fancy WMS From Ciriè City" );
102+
QCOMPARE( fragments[3], "crs=EPSG:2036&dpiMode=7&format=image/png&layers=lidar&styles=default&url=https://geoegl.msp.gouv.qc." );
103+
QCOMPARE( fragments[4], "EPSG\\:2036:EPSG\\:3857" );
104+
105+
}
106+
107+
108+
QGSTEST_MAIN( TestQgsMimeDataUtils )
109+
#include "testqgsmimedatautils.moc"
110+
111+

0 commit comments

Comments
 (0)