Skip to content

Commit d637c67

Browse files
committedApr 17, 2012
QgsDataSourceURI used for WMS provider, all params are now encoded in URI, removed WMS specific functions from QgsRasterLayer and QgisAppInterface
1 parent d7e8ae1 commit d637c67

24 files changed

+267
-435
lines changed
 

‎python/core/qgsrasterlayer.sip

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,10 @@ public:
3535
bool loadDefaultStyleFlag = true );
3636

3737
/** \brief [ data provider interface ] Constructor in provider mode */
38-
QgsRasterLayer( int dummy,
39-
const QString & baseName = QString(),
40-
const QString & path = QString(),
41-
const QString & providerLib = QString(),
42-
const QStringList & layers = QStringList(),
43-
const QStringList & styles = QStringList(),
44-
const QString & format = QString(),
45-
const QString & crs = QString());
46-
38+
QgsRasterLayer( const QString & uri,
39+
const QString & baseName,
40+
const QString & providerKey,
41+
bool loadDefaultStyleFlag = true );
4742

4843
/** \brief The destructor */
4944
~QgsRasterLayer();
@@ -195,11 +190,7 @@ public:
195190
QString redBandName();
196191

197192
/** [ data provider interface ] Set the data provider */
198-
void setDataProvider( const QString & provider,
199-
const QStringList & layers,
200-
const QStringList & styles,
201-
const QString & format,
202-
const QString & crs );
193+
void setDataProvider( const QString & provider );
203194

204195
/** \brief Mutator for drawing style */
205196
void setDrawingStyle( const DrawingStyle & theDrawingStyle );

‎python/gui/qgisinterface.sip

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class QgisInterface : QObject
4646
//! Add a raster layer given a raster layer file name
4747
virtual QgsRasterLayer* addRasterLayer(QString rasterLayerPath, QString baseName = QString())=0;
4848
//! Add a WMS layer
49-
virtual QgsRasterLayer* addRasterLayer(const QString& url, const QString& layerName, const QString& providerKey, const QStringList& layers, const QStringList& styles, const QString& format, const QString& crs) = 0;
49+
virtual QgsRasterLayer* addRasterLayer(const QString& uri, const QString& baseName, const QString& providerKey) = 0;
5050

5151
//! Add a project
5252
virtual bool addProject(QString theProject)=0;

‎src/app/qgisapp.cpp

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ void QgisApp::dropEvent( QDropEvent *event )
746746
}
747747
else if ( u.layerType == "raster" )
748748
{
749-
addRasterLayer( u.uri, u.name, u.providerKey, QStringList(), QStringList(), QString(), QString() );
749+
addRasterLayer( u.uri, u.name, u.providerKey );
750750
}
751751
}
752752
}
@@ -2626,10 +2626,8 @@ void QgisApp::addWmsLayer()
26262626
QMessageBox::warning( this, tr( "WMS" ), tr( "Cannot get WMS select dialog from provider." ) );
26272627
return;
26282628
}
2629-
connect( wmss , SIGNAL( addRasterLayer( QString const &, QString const &, QString const &, QStringList const &, QStringList const &, QString const &,
2630-
QString const & ) ),
2631-
this , SLOT( addRasterLayer( QString const &, QString const &, QString const &, QStringList const &, QStringList const &, QString const &,
2632-
QString const & ) ) );
2629+
connect( wmss , SIGNAL( addRasterLayer( QString const &, QString const &, QString const & ) ),
2630+
this , SLOT( addRasterLayer( QString const &, QString const &, QString const & ) ) );
26332631
wmss->exec();
26342632
delete wmss;
26352633
}
@@ -6730,22 +6728,17 @@ QgsRasterLayer* QgisApp::addRasterLayer( QString const & rasterFile, QString con
67306728

67316729
/** Add a raster layer directly without prompting user for location
67326730
The caller must provide information compatible with the provider plugin
6733-
using the rasterLayerPath and baseName. The provider can use these
6731+
using the uri and baseName. The provider can use these
67346732
parameters in any way necessary to initialize the layer. The baseName
67356733
parameter is used in the Map Legend so it should be formed in a meaningful
67366734
way.
67376735
67386736
\note Copied from the equivalent addVectorLayer function in this file
6739-
TODO Make it work for rasters specifically.
67406737
*/
67416738
QgsRasterLayer* QgisApp::addRasterLayer(
6742-
QString const &rasterLayerPath,
6739+
QString const &uri,
67436740
QString const &baseName,
6744-
QString const &providerKey,
6745-
QStringList const & layers,
6746-
QStringList const & styles,
6747-
QString const &format,
6748-
QString const &crs )
6741+
QString const &providerKey )
67496742
{
67506743
QgsDebugMsg( "about to get library for " + providerKey );
67516744

@@ -6756,42 +6749,21 @@ QgsRasterLayer* QgisApp::addRasterLayer(
67566749

67576750
mMapCanvas->freeze();
67586751

6759-
// Let render() do its own cursor management
6760-
// QApplication::setOverrideCursor(Qt::WaitCursor);
6761-
67626752
// create the layer
67636753
QgsRasterLayer *layer;
6764-
/* Eliminate the need to instantiate the layer based on provider type.
6765-
The caller is responsible for cobbling together the needed information to
6766-
open the layer
6767-
*/
6768-
QgsDebugMsg( "Creating new raster layer using " + rasterLayerPath
6769-
+ " with baseName of " + baseName
6770-
+ " and layer list of " + layers.join( ", " )
6771-
+ " and style list of " + styles.join( ", " )
6772-
+ " and format of " + format
6773-
+ " and providerKey of " + providerKey
6774-
+ " and CRS of " + crs );
6754+
QgsDebugMsg( "Creating new raster layer using " + uri
6755+
+ " with baseName of " + baseName );
67756756

67766757
// TODO: Remove the 0 when the raster layer becomes a full provider gateway.
6777-
layer = new QgsRasterLayer( 0, rasterLayerPath, baseName, providerKey, layers, styles, format, crs );
6758+
layer = new QgsRasterLayer( uri, baseName, providerKey );
67786759

67796760
QgsDebugMsg( "Constructed new layer." );
67806761

6781-
if ( layer && shouldAskUserForGDALSublayers( layer ) )
6782-
{
6783-
askUserForGDALSublayers( layer );
6784-
6785-
// The first layer loaded is not useful in that case. The user can select it in
6786-
// the list if he wants to load it.
6787-
delete layer;
6788-
}
6789-
else if ( layer && layer->isValid() )
6762+
if ( layer && layer->isValid() )
67906763
{
67916764
addRasterLayer( layer );
67926765

67936766
statusBar()->showMessage( mMapCanvas->extent().toString( 2 ) );
6794-
67956767
}
67966768
else
67976769
{
@@ -6805,10 +6777,6 @@ QgsRasterLayer* QgisApp::addRasterLayer(
68056777
mMapCanvas->freeze( false );
68066778
mMapCanvas->refresh();
68076779
return layer;
6808-
6809-
// Let render() do its own cursor management
6810-
// QApplication::restoreOverrideCursor();
6811-
68126780
} // QgisApp::addRasterLayer
68136781

68146782

‎src/app/qgisapp.h

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -458,16 +458,9 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
458458
virtual bool event( QEvent * event );
459459

460460
/** Open a raster layer using the Raster Data Provider.
461-
* Note this is included to support WMS layers only at this stage,
462-
* GDAL layer support via a Provider is not yet implemented.
461+
* \note added in 1.9
463462
*/
464-
QgsRasterLayer* addRasterLayer( QString const & rasterLayerPath,
465-
QString const & baseName,
466-
QString const & providerKey,
467-
QStringList const & layers,
468-
QStringList const & styles,
469-
QString const & format,
470-
QString const & crs );
463+
QgsRasterLayer* addRasterLayer( QString const & uri, QString const & baseName, QString const & providerKey );
471464

472465
void addWfsLayer( QString uri, QString typeName );
473466

@@ -1190,8 +1183,8 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
11901183
QString mOldScale;
11911184

11921185
#ifdef HAVE_TOUCH
1193-
bool gestureEvent(QGestureEvent *event);
1194-
void tapAndHoldTriggered(QTapAndHoldGesture *gesture);
1186+
bool gestureEvent( QGestureEvent *event );
1187+
void tapAndHoldTriggered( QTapAndHoldGesture *gesture );
11951188
#endif
11961189
};
11971190

‎src/app/qgisappinterface.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,9 @@ QgsRasterLayer* QgisAppInterface::addRasterLayer( QString rasterLayerPath, QStri
107107
return qgis->addRasterLayer( rasterLayerPath, baseName );
108108
}
109109

110-
QgsRasterLayer* QgisAppInterface::addRasterLayer( const QString& url, const QString& baseName, const QString& providerKey,
111-
const QStringList& layers, const QStringList& styles, const QString& format, const QString& crs )
110+
QgsRasterLayer* QgisAppInterface::addRasterLayer( const QString& url, const QString& baseName, const QString& providerKey )
112111
{
113-
return qgis->addRasterLayer( url, baseName, providerKey, layers, styles, format, crs );
112+
return qgis->addRasterLayer( url, baseName, providerKey );
114113
}
115114

116115
bool QgisAppInterface::addProject( QString theProjectName )

‎src/app/qgisappinterface.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ class QgisAppInterface : public QgisInterface
6464
//! Add a raster layer given its file name
6565
QgsRasterLayer* addRasterLayer( QString rasterLayerPath, QString baseName );
6666
//! Add a WMS layer
67-
QgsRasterLayer* addRasterLayer( const QString& url, const QString& baseName, const QString& providerKey,
68-
const QStringList& layers, const QStringList& styles, const QString& format, const QString& crs );
67+
QgsRasterLayer* addRasterLayer( const QString& url, const QString& baseName, const QString& providerKey );
6968

7069
//! Add a project
7170
bool addProject( QString theProjectName );

‎src/app/qgsbrowserdockwidget.cpp

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -293,33 +293,7 @@ void QgsBrowserDockWidget::addLayer( QgsLayerItem *layerItem )
293293
}
294294
if ( type == QgsMapLayer::RasterLayer )
295295
{
296-
// This should go to WMS provider
297-
QStringList URIParts = uri.split( "|" );
298-
QString rasterLayerPath = URIParts.at( 0 );
299-
QStringList layers;
300-
QStringList styles;
301-
QString format;
302-
QString crs;
303-
for ( int i = 1 ; i < URIParts.size(); i++ )
304-
{
305-
QString part = URIParts.at( i );
306-
int pos = part.indexOf( "=" );
307-
QString field = part.left( pos );
308-
QString value = part.mid( pos + 1 );
309-
310-
if ( field == "layers" )
311-
layers = value.split( "," );
312-
if ( field == "styles" )
313-
styles = value.split( "," );
314-
if ( field == "format" )
315-
format = value;
316-
if ( field == "crs" )
317-
crs = value;
318-
}
319-
QgsDebugMsg( "rasterLayerPath = " + rasterLayerPath );
320-
QgsDebugMsg( "layers = " + layers.join( " " ) );
321-
322-
QgisApp::instance()->addRasterLayer( rasterLayerPath, layerItem->name(), providerKey, layers, styles, format, crs );
296+
QgisApp::instance()->addRasterLayer( uri, layerItem->name(), providerKey );
323297
}
324298
}
325299

@@ -399,7 +373,7 @@ void QgsBrowserDockWidget::showProperties( )
399373
{
400374
QgsDebugMsg( "creating raster layer" );
401375
// should copy code from addLayer() to split uri ?
402-
QgsRasterLayer* layer = new QgsRasterLayer( 0, layerItem->uri(), layerItem->uri(), layerItem->providerKey() );
376+
QgsRasterLayer* layer = new QgsRasterLayer( layerItem->uri(), layerItem->uri(), layerItem->providerKey() );
403377
if ( layer != NULL )
404378
{
405379
layerCrs = layer->crs();

‎src/browser/qgsbrowser.cpp

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -207,33 +207,7 @@ bool QgsBrowser::layerClicked( QgsLayerItem *item )
207207
}
208208
if ( type == QgsMapLayer::RasterLayer )
209209
{
210-
// This should go to WMS provider
211-
QStringList URIParts = uri.split( "|" );
212-
QString rasterLayerPath = URIParts.at( 0 );
213-
QStringList layers;
214-
QStringList styles;
215-
QString format;
216-
QString crs;
217-
for ( int i = 1 ; i < URIParts.size(); i++ )
218-
{
219-
QString part = URIParts.at( i );
220-
int pos = part.indexOf( "=" );
221-
QString field = part.left( pos );
222-
QString value = part.mid( pos + 1 );
223-
224-
if ( field == "layers" )
225-
layers = value.split( "," );
226-
if ( field == "styles" )
227-
styles = value.split( "," );
228-
if ( field == "format" )
229-
format = value;
230-
if ( field == "crs" )
231-
crs = value;
232-
}
233-
QgsDebugMsg( "rasterLayerPath = " + rasterLayerPath );
234-
QgsDebugMsg( "layers = " + layers.join( " " ) );
235-
236-
mLayer = new QgsRasterLayer( 0, rasterLayerPath, "", providerKey, layers, styles, format, crs );
210+
mLayer = new QgsRasterLayer( uri, "", providerKey );
237211
}
238212
}
239213

‎src/core/qgsbrowsermodel.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,7 @@ Qt::ItemFlags QgsBrowserModel::flags( const QModelIndex & index ) const
114114
if ( ptr->type() == QgsDataItem::Layer )
115115
{
116116
QgsLayerItem *layer = ( QgsLayerItem* ) ptr;
117-
if ( layer->providerKey() != "wms" )
118-
{
119-
flags |= Qt::ItemIsDragEnabled;
120-
}
117+
flags |= Qt::ItemIsDragEnabled;
121118
}
122119
if ( ptr->acceptDrop() )
123120
flags |= Qt::ItemIsDropEnabled;
@@ -354,7 +351,6 @@ QMimeData * QgsBrowserModel::mimeData( const QModelIndexList &indexes ) const
354351
QgsDataItem* ptr = ( QgsDataItem* ) index.internalPointer();
355352
if ( ptr->type() != QgsDataItem::Layer ) continue;
356353
QgsLayerItem *layer = ( QgsLayerItem* ) ptr;
357-
if ( layer->providerKey() == "wms" ) continue;
358354
lst.append( QgsMimeDataUtils::Uri( layer ) );
359355
}
360356
}

‎src/core/qgsmaplayer.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ bool QgsMapLayer::readXML( const QDomNode& layer_node )
165165
mne = mnl.toElement();
166166
mDataSource = mne.text();
167167

168+
// TODO: this should go to providers
168169
if ( provider == "spatialite" )
169170
{
170171
QgsDataSourceURI uri( mDataSource );
@@ -192,6 +193,71 @@ bool QgsMapLayer::readXML( const QDomNode& layer_node )
192193
urlDest.setQueryItems( urlSource.queryItems() );
193194
mDataSource = QString::fromAscii( urlDest.toEncoded() );
194195
}
196+
else if ( provider == "wms" )
197+
{
198+
// >>> BACKWARD COMPATIBILITY < 1.9
199+
// For project file backward compatibility we must support old format:
200+
// 1. mode: <url>
201+
// example: http://example.org/wms?
202+
// 2. mode: tiled=<width>;<height>;<resolution>;<resolution>...,ignoreUrl=GetMap;GetFeatureInfo,featureCount=<count>,username=<name>,password=<password>,url=<url>
203+
// example: tiled=256;256;0.703;0.351,url=http://example.org/tilecache?
204+
// example: featureCount=10,http://example.org/wms?
205+
// example: ignoreUrl=GetMap;GetFeatureInfo,username=cimrman,password=jara,url=http://example.org/wms?
206+
// This is modified version of old QgsWmsProvider::parseUri
207+
// The new format has always params crs,format,layers,styles and that params
208+
// should not appear in old format url -> use them to identify version
209+
if ( !mDataSource.contains( "crs=" ) && !mDataSource.contains( "format=" ) )
210+
{
211+
QgsDebugMsg( "Old WMS URI format detected -> converting to new format" );
212+
QgsDataSourceURI uri;
213+
if ( !mDataSource.startsWith( "http:" ) )
214+
{
215+
QStringList parts = mDataSource.split( "," );
216+
QStringListIterator iter( parts );
217+
while ( iter.hasNext() )
218+
{
219+
QString item = iter.next();
220+
if ( item.startsWith( "username=" ) )
221+
{
222+
uri.setParam( "username", item.mid( 9 ) );
223+
}
224+
else if ( item.startsWith( "password=" ) )
225+
{
226+
uri.setParam( "password", item.mid( 9 ) );
227+
}
228+
else if ( item.startsWith( "tiled=" ) )
229+
{
230+
QStringList params = item.mid( 6 ).split( ";" );
231+
232+
uri.setParam( "tileWidth", params.takeFirst() );
233+
uri.setParam( "tileHeight", params.takeFirst() );
234+
235+
uri.setParam( "tileResolutions", params );
236+
}
237+
else if ( item.startsWith( "featureCount=" ) )
238+
{
239+
uri.setParam( "featureCount", item.mid( 13 ) );
240+
}
241+
else if ( item.startsWith( "url=" ) )
242+
{
243+
uri.setParam( "url", item.mid( 4 ) );
244+
}
245+
else if ( item.startsWith( "ignoreUrl=" ) )
246+
{
247+
uri.setParam( "ignoreUrl", item.mid( 10 ).split( ";" ) );
248+
}
249+
}
250+
}
251+
else
252+
{
253+
uri.setParam( "url", mDataSource );
254+
}
255+
mDataSource = uri.encodedUri();
256+
// At this point, the URI is obviously incomplete, we add additional params
257+
// in QgsRasterLayer::readXml
258+
}
259+
// <<< BACKWARD COMPATIBILITY < 1.9
260+
}
195261
else
196262
{
197263
mDataSource = QgsProject::instance()->readPath( mDataSource );
@@ -318,6 +384,7 @@ bool QgsMapLayer::writeXML( QDomNode & layer_node, QDomDocument & document )
318384
QString src = source();
319385

320386
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( this );
387+
// TODO: what about postgres, mysql and others, they should not go through writePath()
321388
if ( vlayer && vlayer->providerType() == "spatialite" )
322389
{
323390
QgsDataSourceURI uri( src );

0 commit comments

Comments
 (0)
Please sign in to comment.