Skip to content
Permalink
Browse files

Try to use the project CRS and last selected image format when adding…

… a layer from the browser
  • Loading branch information
manisandro committed Dec 23, 2014
1 parent a4aaff5 commit a0ef27a9d805382122976c67c24321f4c5980630
@@ -151,6 +151,7 @@
#include "qgsmaplayerregistry.h"
#include "qgsmapoverviewcanvas.h"
#include "qgsmaprenderer.h"
#include "qgsmapsettings.h"
#include "qgsmaptip.h"
#include "qgsmergeattributesdialog.h"
#include "qgsmessageviewer.h"
@@ -955,13 +956,15 @@ void QgisApp::dropEvent( QDropEvent *event )
QgsMimeDataUtils::UriList lst = QgsMimeDataUtils::decodeUriList( event->mimeData() );
foreach ( const QgsMimeDataUtils::Uri& u, lst )
{
QString uri = crsAndFormatAdjustedLayerUri( u.uri, u.supportedCrs, u.supportedFormats );

if ( u.layerType == "vector" )
{
addVectorLayer( u.uri, u.name, u.providerKey );
addVectorLayer( uri, u.name, u.providerKey );
}
else if ( u.layerType == "raster" )
{
addRasterLayer( u.uri, u.name, u.providerKey );
addRasterLayer( uri, u.name, u.providerKey );
}
}
}
@@ -2731,6 +2734,36 @@ void QgisApp::addLayerDefinition()
openLayerDefinition( path );
}

QString QgisApp::crsAndFormatAdjustedLayerUri( const QString &uri , const QStringList &supportedCrs, const QStringList &supportedFormats ) const
{
QString newuri = uri;

// Adjust layer CRS to project CRS
QgsCoordinateReferenceSystem testCrs;
foreach ( QString c, supportedCrs )
{
testCrs.createFromOgcWmsCrs( c );
if ( testCrs == mMapCanvas->mapRenderer()->destinationCrs() )
{
newuri.replace( QRegExp( "crs=[^&]+" ), "crs=" + c );
QgsDebugMsg( QString( "Changing layer crs to %1, new uri: %2" ).arg( c, uri ) );
break;
}
}

// Use the last used image format
QString lastImageEncoding = QSettings().value( "/qgis/lastWmsImageEncoding", "image/png" ).toString();
foreach ( QString fmt, supportedFormats )
{
if ( fmt == lastImageEncoding )
{
newuri.replace( QRegExp( "format=[^&]+" ), "format=" + fmt );
QgsDebugMsg( QString( "Changing layer format to %1, new uri: %2" ).arg( fmt, uri ) );
break;
}
}
return newuri;
}

/**
This method prompts the user for a list of vector file names with a dialog.
@@ -150,6 +150,11 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
*/
QgsRasterLayer *addRasterLayer( const QString &rasterFile, const QString &baseName, bool guiWarning = true );

/** Returns and adjusted uri for the layer based on current and available CRS as well as the last selected image format
* @note added in 2.4
*/
QString crsAndFormatAdjustedLayerUri(const QString& uri, const QStringList& supportedCrs, const QStringList& supportedFormats) const;

/** Add a 'pre-made' map layer to the project */
void addMapLayer( QgsMapLayer *theMapLayer );

@@ -480,7 +480,7 @@ void QgsBrowserDockWidget::addLayer( QgsLayerItem *layerItem )
if ( layerItem == NULL )
return;

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

@@ -277,11 +277,23 @@ class CORE_EXPORT QgsLayerItem : public QgsDataItem
// Returns provider key
QString providerKey() { return mProviderKey; }

/** Returns the supported CRS
* @note Added in 2.8
*/
QStringList supportedCRS() { return mSupportedCRS; }

/** Returns the supported formats
* @note Added in 2.8
*/
QStringList supportedFormats() { return mSupportFormats; }

protected:

QString mProviderKey;
QString mUri;
LayerType mLayerType;
QStringList mSupportedCRS;
QStringList mSupportFormats;

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

#endif // QGSDATAITEM_H


@@ -22,7 +22,11 @@
static const char* QGIS_URILIST_MIMETYPE = "application/x-vnd.qgis.qgis.uri";

QgsMimeDataUtils::Uri::Uri( QgsLayerItem* layerItem )
: providerKey( layerItem->providerKey() ), name( layerItem->layerName() ), uri( layerItem->uri() )
: providerKey( layerItem->providerKey() )
, name( layerItem->layerName() )
, uri( layerItem->uri() )
, supportedCrs( layerItem->supportedCRS() )
, supportedFormats( layerItem->supportedFormats() )
{
switch ( layerItem->mapLayerType() )
{
@@ -36,66 +40,26 @@ QgsMimeDataUtils::Uri::Uri( QgsLayerItem* layerItem )
layerType = "plugin";
break;
}

}

QgsMimeDataUtils::Uri::Uri( QString& encData )
{
QStringList parts;
QChar split = ':';
QChar escape = '\\';
QString part;
bool inEscape = false;
if ( encData.isEmpty() )
return;
for ( int i = 0; i < encData.length(); ++i )
{
if ( encData.at( i ) == escape && !inEscape )
{
inEscape = true;
}
else if ( encData.at( i ) == split && !inEscape )
{
parts << part;
part = "";
}
else
{
part += encData.at( i );
inEscape = false;
}
}
if ( !part.isEmpty() )
QStringList decoded = decode( encData );
if ( decoded.size() == 6 )
{
parts << part;
}

if ( parts.size() <= 5 ) // PostGISTRaster layers yields five parts
{
layerType = parts.value( 0 );
providerKey = parts.value( 1 );
name = parts.value( 2 );
// fetchs PostGISRaster layers
if ( parts.value( 3 ) == "PG" )
{
uri = parts.value( 3 ) + ":" + parts.value( 4 );
}
else
{
uri = parts.value( 3 );
}
QgsDebugMsg( "type: " + layerType + " key: " + providerKey + " name: " + name + " uri: " + uri );
layerType = decoded[0];
providerKey = decoded[1];
name = decoded[2];
uri = decoded[3];
supportedCrs = decode( decoded[4] );
supportedFormats = decode( decoded[5] );
QgsDebugMsg( "type: " + layerType + " key: " + providerKey + " name: " + name + " uri: " + uri + " supportedCRS: " + decoded[4] + " supportedFormats: " + decoded[5] );
}
}

QString QgsMimeDataUtils::Uri::data() const
{
QString escapedName = name;
QString escapeUri = uri;
escapedName.replace( ":", "\\:" );
escapeUri.replace( "\\", "\\\\" );
escapeUri.replace( ":", "\\:" );
return layerType + ":" + providerKey + ":" + escapedName + ":" + escapeUri;
return encode( QStringList() << layerType << providerKey << name << uri << encode( supportedCrs ) << encode( supportedFormats ) );
}

// -----
@@ -135,3 +99,46 @@ QgsMimeDataUtils::UriList QgsMimeDataUtils::decodeUriList( const QMimeData* data
}
return list;
}

QString QgsMimeDataUtils::encode( const QStringList& items )
{
QString encoded;
foreach ( const QString& item, items )
{
QString str = item;
str.replace( ":", "\\:" );
encoded += str + ":";
}
return encoded.left( encoded.length() - 1 );
}

QStringList QgsMimeDataUtils::decode( const QString& encoded )
{
QStringList items;
QString item;
bool inEscape = false;
foreach ( const QChar& c, encoded )
{
if ( c == '\\' && inEscape )
{
item += c;
}
else if ( c == '\\' )
{
inEscape = true;
}
else if ( c == ':' && !inEscape )
{
items.append( item );
item = "";
}
else
{
item += c;
inEscape = false;
}
}
items.append( item );
return items;
}

@@ -16,6 +16,7 @@
#define QGSMIMEDATAUTILS_H

#include <QMimeData>
#include <QStringList>

class QgsLayerItem;

@@ -34,6 +35,8 @@ class CORE_EXPORT QgsMimeDataUtils
QString providerKey;
QString name;
QString uri;
QStringList supportedCrs;
QStringList supportedFormats;
};
typedef QList<Uri> UriList;

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

static UriList decodeUriList( const QMimeData* data );

private:
static QString encode( const QStringList& items );
static QStringList decode( const QString& encoded );

};

#endif // QGSMIMEDATAUTILS_H

@@ -125,6 +125,7 @@ QgsWCSLayerItem::QgsWCSLayerItem( QgsDataItem* parent, QString name, QString pat
mDataSourceUri( dataSourceUri ),
mCoverageSummary( coverageSummary )
{
mSupportedCRS = mCoverageSummary.supportedCrs;
QgsDebugMsg( "uri = " + mDataSourceUri.encodedUri() );
mUri = createUri();
// Populate everything, it costs nothing, all info about layers is collected
@@ -312,3 +313,4 @@ QGISEXTERN QgsWCSSourceSelect * selectWidget( QWidget * parent, Qt::WindowFlags
{
return new QgsWCSSourceSelect( parent, fl );
}

@@ -244,6 +244,8 @@ QgsWMSLayerItem::QgsWMSLayerItem( QgsDataItem* parent, QString name, QString pat
, mDataSourceUri( dataSourceUri )
, mLayerProperty( layerProperty )
{
mSupportedCRS = mLayerProperty.crs;
mSupportFormats = mCapabilitiesProperty.capability.request.getMap.format;
QgsDebugMsg( "uri = " + mDataSourceUri.encodedUri() );

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

return 0;
}

0 comments on commit a0ef27a

Please sign in to comment.
You can’t perform that action at this time.