Skip to content

Commit 1bce9cc

Browse files
committedDec 23, 2014
Merge pull request #1310 from manisandro/adjust_crs_drop_from_browser
Try to use the project CRS when adding a layer from the browser
2 parents 32079ed + 2efda5d commit 1bce9cc

File tree

9 files changed

+134
-54
lines changed

9 files changed

+134
-54
lines changed
 

‎python/core/qgsdataitem.sip

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,15 @@ class QgsLayerItem : QgsDataItem
141141
// Returns provider key
142142
QString providerKey();
143143

144+
/** Returns the supported CRS
145+
* @note Added in 2.8
146+
*/
147+
QStringList supportedCRS();
148+
149+
/** Returns the supported formats
150+
* @note Added in 2.8
151+
*/
152+
QStringList supportedFormats();
144153
public:
145154
static const QIcon &iconPoint();
146155
static const QIcon &iconLine();

‎src/app/qgisapp.cpp

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@
151151
#include "qgsmaplayerregistry.h"
152152
#include "qgsmapoverviewcanvas.h"
153153
#include "qgsmaprenderer.h"
154+
#include "qgsmapsettings.h"
154155
#include "qgsmaptip.h"
155156
#include "qgsmergeattributesdialog.h"
156157
#include "qgsmessageviewer.h"
@@ -955,13 +956,15 @@ void QgisApp::dropEvent( QDropEvent *event )
955956
QgsMimeDataUtils::UriList lst = QgsMimeDataUtils::decodeUriList( event->mimeData() );
956957
foreach ( const QgsMimeDataUtils::Uri& u, lst )
957958
{
959+
QString uri = crsAndFormatAdjustedLayerUri( u.uri, u.supportedCrs, u.supportedFormats );
960+
958961
if ( u.layerType == "vector" )
959962
{
960-
addVectorLayer( u.uri, u.name, u.providerKey );
963+
addVectorLayer( uri, u.name, u.providerKey );
961964
}
962965
else if ( u.layerType == "raster" )
963966
{
964-
addRasterLayer( u.uri, u.name, u.providerKey );
967+
addRasterLayer( uri, u.name, u.providerKey );
965968
}
966969
}
967970
}
@@ -2731,6 +2734,36 @@ void QgisApp::addLayerDefinition()
27312734
openLayerDefinition( path );
27322735
}
27332736

2737+
QString QgisApp::crsAndFormatAdjustedLayerUri( const QString &uri , const QStringList &supportedCrs, const QStringList &supportedFormats ) const
2738+
{
2739+
QString newuri = uri;
2740+
2741+
// Adjust layer CRS to project CRS
2742+
QgsCoordinateReferenceSystem testCrs;
2743+
foreach ( QString c, supportedCrs )
2744+
{
2745+
testCrs.createFromOgcWmsCrs( c );
2746+
if ( testCrs == mMapCanvas->mapRenderer()->destinationCrs() )
2747+
{
2748+
newuri.replace( QRegExp( "crs=[^&]+" ), "crs=" + c );
2749+
QgsDebugMsg( QString( "Changing layer crs to %1, new uri: %2" ).arg( c, uri ) );
2750+
break;
2751+
}
2752+
}
2753+
2754+
// Use the last used image format
2755+
QString lastImageEncoding = QSettings().value( "/qgis/lastWmsImageEncoding", "image/png" ).toString();
2756+
foreach ( QString fmt, supportedFormats )
2757+
{
2758+
if ( fmt == lastImageEncoding )
2759+
{
2760+
newuri.replace( QRegExp( "format=[^&]+" ), "format=" + fmt );
2761+
QgsDebugMsg( QString( "Changing layer format to %1, new uri: %2" ).arg( fmt, uri ) );
2762+
break;
2763+
}
2764+
}
2765+
return newuri;
2766+
}
27342767

27352768
/**
27362769
This method prompts the user for a list of vector file names with a dialog.

‎src/app/qgisapp.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
150150
*/
151151
QgsRasterLayer *addRasterLayer( const QString &rasterFile, const QString &baseName, bool guiWarning = true );
152152

153+
/** Returns and adjusted uri for the layer based on current and available CRS as well as the last selected image format
154+
* @note added in 2.4
155+
*/
156+
QString crsAndFormatAdjustedLayerUri(const QString& uri, const QStringList& supportedCrs, const QStringList& supportedFormats) const;
157+
153158
/** Add a 'pre-made' map layer to the project */
154159
void addMapLayer( QgsMapLayer *theMapLayer );
155160

‎src/app/qgsbrowserdockwidget.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ void QgsBrowserDockWidget::addLayer( QgsLayerItem *layerItem )
480480
if ( layerItem == NULL )
481481
return;
482482

483-
QString uri = layerItem->uri();
483+
QString uri = QgisApp::instance()->crsAndFormatAdjustedLayerUri( layerItem->uri(), layerItem->supportedCRS(), layerItem->supportedFormats() );
484484
if ( uri.isEmpty() )
485485
return;
486486

‎src/core/qgsdataitem.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,23 @@ class CORE_EXPORT QgsLayerItem : public QgsDataItem
277277
// Returns provider key
278278
QString providerKey() { return mProviderKey; }
279279

280+
/** Returns the supported CRS
281+
* @note Added in 2.8
282+
*/
283+
QStringList supportedCRS() { return mSupportedCRS; }
284+
285+
/** Returns the supported formats
286+
* @note Added in 2.8
287+
*/
288+
QStringList supportedFormats() { return mSupportFormats; }
289+
280290
protected:
281291

282292
QString mProviderKey;
283293
QString mUri;
284294
LayerType mLayerType;
295+
QStringList mSupportedCRS;
296+
QStringList mSupportFormats;
285297

286298
public:
287299
static const QIcon &iconPoint();
@@ -442,3 +454,4 @@ class CORE_EXPORT QgsZipItem : public QgsDataCollectionItem
442454

443455
#endif // QGSDATAITEM_H
444456

457+

‎src/core/qgsmimedatautils.cpp

Lines changed: 58 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222
static const char* QGIS_URILIST_MIMETYPE = "application/x-vnd.qgis.qgis.uri";
2323

2424
QgsMimeDataUtils::Uri::Uri( QgsLayerItem* layerItem )
25-
: providerKey( layerItem->providerKey() ), name( layerItem->layerName() ), uri( layerItem->uri() )
25+
: providerKey( layerItem->providerKey() )
26+
, name( layerItem->layerName() )
27+
, uri( layerItem->uri() )
28+
, supportedCrs( layerItem->supportedCRS() )
29+
, supportedFormats( layerItem->supportedFormats() )
2630
{
2731
switch ( layerItem->mapLayerType() )
2832
{
@@ -36,66 +40,26 @@ QgsMimeDataUtils::Uri::Uri( QgsLayerItem* layerItem )
3640
layerType = "plugin";
3741
break;
3842
}
39-
4043
}
4144

4245
QgsMimeDataUtils::Uri::Uri( QString& encData )
4346
{
44-
QStringList parts;
45-
QChar split = ':';
46-
QChar escape = '\\';
47-
QString part;
48-
bool inEscape = false;
49-
if ( encData.isEmpty() )
50-
return;
51-
for ( int i = 0; i < encData.length(); ++i )
52-
{
53-
if ( encData.at( i ) == escape && !inEscape )
54-
{
55-
inEscape = true;
56-
}
57-
else if ( encData.at( i ) == split && !inEscape )
58-
{
59-
parts << part;
60-
part = "";
61-
}
62-
else
63-
{
64-
part += encData.at( i );
65-
inEscape = false;
66-
}
67-
}
68-
if ( !part.isEmpty() )
47+
QStringList decoded = decode( encData );
48+
if ( decoded.size() == 6 )
6949
{
70-
parts << part;
71-
}
72-
73-
if ( parts.size() <= 5 ) // PostGISTRaster layers yields five parts
74-
{
75-
layerType = parts.value( 0 );
76-
providerKey = parts.value( 1 );
77-
name = parts.value( 2 );
78-
// fetchs PostGISRaster layers
79-
if ( parts.value( 3 ) == "PG" )
80-
{
81-
uri = parts.value( 3 ) + ":" + parts.value( 4 );
82-
}
83-
else
84-
{
85-
uri = parts.value( 3 );
86-
}
87-
QgsDebugMsg( "type: " + layerType + " key: " + providerKey + " name: " + name + " uri: " + uri );
50+
layerType = decoded[0];
51+
providerKey = decoded[1];
52+
name = decoded[2];
53+
uri = decoded[3];
54+
supportedCrs = decode( decoded[4] );
55+
supportedFormats = decode( decoded[5] );
56+
QgsDebugMsg( "type: " + layerType + " key: " + providerKey + " name: " + name + " uri: " + uri + " supportedCRS: " + decoded[4] + " supportedFormats: " + decoded[5] );
8857
}
8958
}
9059

9160
QString QgsMimeDataUtils::Uri::data() const
9261
{
93-
QString escapedName = name;
94-
QString escapeUri = uri;
95-
escapedName.replace( ":", "\\:" );
96-
escapeUri.replace( "\\", "\\\\" );
97-
escapeUri.replace( ":", "\\:" );
98-
return layerType + ":" + providerKey + ":" + escapedName + ":" + escapeUri;
62+
return encode( QStringList() << layerType << providerKey << name << uri << encode( supportedCrs ) << encode( supportedFormats ) );
9963
}
10064

10165
// -----
@@ -135,3 +99,46 @@ QgsMimeDataUtils::UriList QgsMimeDataUtils::decodeUriList( const QMimeData* data
13599
}
136100
return list;
137101
}
102+
103+
QString QgsMimeDataUtils::encode( const QStringList& items )
104+
{
105+
QString encoded;
106+
foreach ( const QString& item, items )
107+
{
108+
QString str = item;
109+
str.replace( ":", "\\:" );
110+
encoded += str + ":";
111+
}
112+
return encoded.left( encoded.length() - 1 );
113+
}
114+
115+
QStringList QgsMimeDataUtils::decode( const QString& encoded )
116+
{
117+
QStringList items;
118+
QString item;
119+
bool inEscape = false;
120+
foreach ( const QChar& c, encoded )
121+
{
122+
if ( c == '\\' && inEscape )
123+
{
124+
item += c;
125+
}
126+
else if ( c == '\\' )
127+
{
128+
inEscape = true;
129+
}
130+
else if ( c == ':' && !inEscape )
131+
{
132+
items.append( item );
133+
item = "";
134+
}
135+
else
136+
{
137+
item += c;
138+
inEscape = false;
139+
}
140+
}
141+
items.append( item );
142+
return items;
143+
}
144+

‎src/core/qgsmimedatautils.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define QGSMIMEDATAUTILS_H
1717

1818
#include <QMimeData>
19+
#include <QStringList>
1920

2021
class QgsLayerItem;
2122

@@ -34,6 +35,8 @@ class CORE_EXPORT QgsMimeDataUtils
3435
QString providerKey;
3536
QString name;
3637
QString uri;
38+
QStringList supportedCrs;
39+
QStringList supportedFormats;
3740
};
3841
typedef QList<Uri> UriList;
3942

@@ -43,6 +46,11 @@ class CORE_EXPORT QgsMimeDataUtils
4346

4447
static UriList decodeUriList( const QMimeData* data );
4548

49+
private:
50+
static QString encode( const QStringList& items );
51+
static QStringList decode( const QString& encoded );
52+
4653
};
4754

4855
#endif // QGSMIMEDATAUTILS_H
56+

‎src/providers/wcs/qgswcsdataitems.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ QgsWCSLayerItem::QgsWCSLayerItem( QgsDataItem* parent, QString name, QString pat
125125
mDataSourceUri( dataSourceUri ),
126126
mCoverageSummary( coverageSummary )
127127
{
128+
mSupportedCRS = mCoverageSummary.supportedCrs;
128129
QgsDebugMsg( "uri = " + mDataSourceUri.encodedUri() );
129130
mUri = createUri();
130131
// Populate everything, it costs nothing, all info about layers is collected
@@ -312,3 +313,4 @@ QGISEXTERN QgsWCSSourceSelect * selectWidget( QWidget * parent, Qt::WindowFlags
312313
{
313314
return new QgsWCSSourceSelect( parent, fl );
314315
}
316+

‎src/providers/wms/qgswmsdataitems.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ QgsWMSLayerItem::QgsWMSLayerItem( QgsDataItem* parent, QString name, QString pat
244244
, mDataSourceUri( dataSourceUri )
245245
, mLayerProperty( layerProperty )
246246
{
247+
mSupportedCRS = mLayerProperty.crs;
248+
mSupportFormats = mCapabilitiesProperty.capability.request.getMap.format;
247249
QgsDebugMsg( "uri = " + mDataSourceUri.encodedUri() );
248250

249251
mUri = createUri();
@@ -454,3 +456,4 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
454456

455457
return 0;
456458
}
459+

0 commit comments

Comments
 (0)