QGIS API Documentation  2.14.3-Essen
qgsmaplayer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmaplayer.cpp - description
3  -------------------
4  begin : Fri Jun 28 2002
5  copyright : (C) 2002 by Gary E.Sherman
6  email : sherman at mrcc.com
7 ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 
19 #include <QDateTime>
20 #include <QDir>
21 #include <QDomDocument>
22 #include <QDomElement>
23 #include <QDomImplementation>
24 #include <QDomNode>
25 #include <QFile>
26 #include <QFileInfo>
27 #include <QSettings> // TODO: get rid of it [MD]
28 #include <QTextStream>
29 #include <QUrl>
30 
31 #include <sqlite3.h>
32 
33 #include "qgsapplication.h"
35 #include "qgsdatasourceuri.h"
36 #include "qgslogger.h"
37 #include "qgsauthmanager.h"
38 #include "qgsmaplayer.h"
39 #include "qgsmaplayerlegend.h"
41 #include "qgspluginlayer.h"
42 #include "qgspluginlayerregistry.h"
44 #include "qgsproject.h"
45 #include "qgsproviderregistry.h"
46 #include "qgsrasterlayer.h"
47 #include "qgsrectangle.h"
48 #include "qgsvectorlayer.h"
49 #include "qgsvectordataprovider.h"
50 #include "qgsmaplayerregistry.h"
51 
52 
54  const QString& lyrname,
55  const QString& source )
56  : mValid( false ) // assume the layer is invalid
57  , mDataSource( source )
58  , mLayerOrigName( lyrname ) // store the original name
59  , mID( "" )
60  , mLayerType( type )
61  , mBlendMode( QPainter::CompositionMode_SourceOver ) // Default to normal blending
62  , mLegend( nullptr )
63  , mStyleManager( new QgsMapLayerStyleManager( this ) )
64 {
65  mCRS = new QgsCoordinateReferenceSystem();
66 
67  // Set the display name = internal name
68  QgsDebugMsg( "original name: '" + mLayerOrigName + '\'' );
70  QgsDebugMsg( "display name: '" + mLayerName + '\'' );
71 
72  mShortName = "";
73  //mShortName.replace( QRegExp( "[\\W]" ), "_" );
74 
75  // Generate the unique ID of this layer
77  mID = lyrname + dt.toString( "yyyyMMddhhmmsszzz" );
78  // Tidy the ID up to avoid characters that may cause problems
79  // elsewhere (e.g in some parts of XML). Replaces every non-word
80  // character (word characters are the alphabet, numbers and
81  // underscore) with an underscore.
82  // Note that the first backslashe in the regular expression is
83  // there for the compiler, so the pattern is actually \W
84  mID.replace( QRegExp( "[\\W]" ), "_" );
85 
86  //set some generous defaults for scale based visibility
87  mMinScale = 0;
88  mMaxScale = 100000000;
89  mScaleBasedVisibility = false;
90 }
91 
93 {
94  delete mCRS;
95  delete mLegend;
96  delete mStyleManager;
97 }
98 
100 {
101  return mLayerType;
102 }
103 
106 {
107  return mID;
108 }
109 
112 {
113  QgsDebugMsg( "new original name: '" + name + '\'' );
114  QString newName = capitaliseLayerName( name );
115  QgsDebugMsg( "new display name: '" + name + '\'' );
116  if ( name == mLayerOrigName && newName == mLayerName ) return;
117  mLayerOrigName = name; // store the new original name
118  mLayerName = newName;
119  emit layerNameChanged();
120 }
121 
124 {
125  QgsDebugMsgLevel( "returning name '" + mLayerName + '\'', 4 );
126  return mLayerName;
127 }
128 
130 {
131  // Redo this every time we're asked for it, as we don't know if
132  // dataSource has changed.
134  return safeName;
135 }
136 
138 {
139  return mDataSource;
140 }
141 
143 {
144  return mExtent;
145 }
146 
148 void QgsMapLayer::setBlendMode( QPainter::CompositionMode blendMode )
149 {
150  mBlendMode = blendMode;
151  emit blendModeChanged( blendMode );
152 }
153 
155 QPainter::CompositionMode QgsMapLayer::blendMode() const
156 {
157  return mBlendMode;
158 }
159 
160 bool QgsMapLayer::draw( QgsRenderContext& rendererContext )
161 {
162  Q_UNUSED( rendererContext );
163  return false;
164 }
165 
167 {
168  Q_UNUSED( rendererContext );
169 }
170 
171 bool QgsMapLayer::readLayerXML( const QDomElement& layerElement )
172 {
174  CUSTOM_CRS_VALIDATION savedValidation;
175  bool layerError;
176 
177  QDomNode mnl;
178  QDomElement mne;
179 
180  // read provider
181  QString provider;
182  mnl = layerElement.namedItem( "provider" );
183  mne = mnl.toElement();
184  provider = mne.text();
185 
186  // set data source
187  mnl = layerElement.namedItem( "datasource" );
188  mne = mnl.toElement();
189  mDataSource = mne.text();
190 
191  // if the layer needs authentication, ensure the master password is set
192  QRegExp rx( "authcfg=([a-z]|[A-Z]|[0-9]){7}" );
193  if (( rx.indexIn( mDataSource ) != -1 )
195  {
196  return false;
197  }
198 
199  // TODO: this should go to providers
200  // see also QgsProject::createEmbeddedLayer
201  if ( provider == "spatialite" )
202  {
204  uri.setDatabase( QgsProject::instance()->readPath( uri.database() ) );
205  mDataSource = uri.uri();
206  }
207  else if ( provider == "ogr" )
208  {
209  QStringList theURIParts = mDataSource.split( '|' );
210  theURIParts[0] = QgsProject::instance()->readPath( theURIParts[0] );
211  mDataSource = theURIParts.join( "|" );
212  }
213  else if ( provider == "gpx" )
214  {
215  QStringList theURIParts = mDataSource.split( '?' );
216  theURIParts[0] = QgsProject::instance()->readPath( theURIParts[0] );
217  mDataSource = theURIParts.join( "?" );
218  }
219  else if ( provider == "delimitedtext" )
220  {
221  QUrl urlSource = QUrl::fromEncoded( mDataSource.toAscii() );
222 
223  if ( !mDataSource.startsWith( "file:" ) )
224  {
226  urlSource.setScheme( "file" );
227  urlSource.setPath( file.path() );
228  }
229 
230  QUrl urlDest = QUrl::fromLocalFile( QgsProject::instance()->readPath( urlSource.toLocalFile() ) );
231  urlDest.setQueryItems( urlSource.queryItems() );
233  }
234  else if ( provider == "wms" )
235  {
236  // >>> BACKWARD COMPATIBILITY < 1.9
237  // For project file backward compatibility we must support old format:
238  // 1. mode: <url>
239  // example: http://example.org/wms?
240  // 2. mode: tiled=<width>;<height>;<resolution>;<resolution>...,ignoreUrl=GetMap;GetFeatureInfo,featureCount=<count>,username=<name>,password=<password>,url=<url>
241  // example: tiled=256;256;0.703;0.351,url=http://example.org/tilecache?
242  // example: featureCount=10,http://example.org/wms?
243  // example: ignoreUrl=GetMap;GetFeatureInfo,username=cimrman,password=jara,url=http://example.org/wms?
244  // This is modified version of old QgsWmsProvider::parseUri
245  // The new format has always params crs,format,layers,styles and that params
246  // should not appear in old format url -> use them to identify version
247  if ( !mDataSource.contains( "crs=" ) && !mDataSource.contains( "format=" ) )
248  {
249  QgsDebugMsg( "Old WMS URI format detected -> converting to new format" );
250  QgsDataSourceURI uri;
251  if ( !mDataSource.startsWith( "http:" ) )
252  {
253  QStringList parts = mDataSource.split( ',' );
254  QStringListIterator iter( parts );
255  while ( iter.hasNext() )
256  {
257  QString item = iter.next();
258  if ( item.startsWith( "username=" ) )
259  {
260  uri.setParam( "username", item.mid( 9 ) );
261  }
262  else if ( item.startsWith( "password=" ) )
263  {
264  uri.setParam( "password", item.mid( 9 ) );
265  }
266  else if ( item.startsWith( "tiled=" ) )
267  {
268  // in < 1.9 tiled= may apper in to variants:
269  // tiled=width;height - non tiled mode, specifies max width and max height
270  // tiled=width;height;resolutions-1;resolution2;... - tile mode
271 
272  QStringList params = item.mid( 6 ).split( ';' );
273 
274  if ( params.size() == 2 ) // non tiled mode
275  {
276  uri.setParam( "maxWidth", params.takeFirst() );
277  uri.setParam( "maxHeight", params.takeFirst() );
278  }
279  else if ( params.size() > 2 ) // tiled mode
280  {
281  // resolutions are no more needed and size limit is not used for tiles
282  // we have to tell to the provider however that it is tiled
283  uri.setParam( "tileMatrixSet", "" );
284  }
285  }
286  else if ( item.startsWith( "featureCount=" ) )
287  {
288  uri.setParam( "featureCount", item.mid( 13 ) );
289  }
290  else if ( item.startsWith( "url=" ) )
291  {
292  uri.setParam( "url", item.mid( 4 ) );
293  }
294  else if ( item.startsWith( "ignoreUrl=" ) )
295  {
296  uri.setParam( "ignoreUrl", item.mid( 10 ).split( ';' ) );
297  }
298  }
299  }
300  else
301  {
302  uri.setParam( "url", mDataSource );
303  }
304  mDataSource = uri.encodedUri();
305  // At this point, the URI is obviously incomplete, we add additional params
306  // in QgsRasterLayer::readXml
307  }
308  // <<< BACKWARD COMPATIBILITY < 1.9
309  }
310  else
311  {
312  bool handled = false;
313 
314  if ( provider == "gdal" )
315  {
316  if ( mDataSource.startsWith( "NETCDF:" ) )
317  {
318  // NETCDF:filename:variable
319  // filename can be quoted with " as it can contain colons
320  QRegExp r( "NETCDF:(.+):([^:]+)" );
321  if ( r.exactMatch( mDataSource ) )
322  {
323  QString filename = r.cap( 1 );
324  if ( filename.startsWith( '"' ) && filename.endsWith( '"' ) )
325  filename = filename.mid( 1, filename.length() - 2 );
326  mDataSource = "NETCDF:\"" + QgsProject::instance()->readPath( filename ) + "\":" + r.cap( 2 );
327  handled = true;
328  }
329  }
330  else if ( mDataSource.startsWith( "HDF4_SDS:" ) )
331  {
332  // HDF4_SDS:subdataset_type:file_name:subdataset_index
333  // filename can be quoted with " as it can contain colons
334  QRegExp r( "HDF4_SDS:([^:]+):(.+):([^:]+)" );
335  if ( r.exactMatch( mDataSource ) )
336  {
337  QString filename = r.cap( 2 );
338  if ( filename.startsWith( '"' ) && filename.endsWith( '"' ) )
339  filename = filename.mid( 1, filename.length() - 2 );
340  mDataSource = "HDF4_SDS:" + r.cap( 1 ) + ":\"" + QgsProject::instance()->readPath( filename ) + "\":" + r.cap( 3 );
341  handled = true;
342  }
343  }
344  else if ( mDataSource.startsWith( "HDF5:" ) )
345  {
346  // HDF5:file_name:subdataset
347  // filename can be quoted with " as it can contain colons
348  QRegExp r( "HDF5:(.+):([^:]+)" );
349  if ( r.exactMatch( mDataSource ) )
350  {
351  QString filename = r.cap( 1 );
352  if ( filename.startsWith( '"' ) && filename.endsWith( '"' ) )
353  filename = filename.mid( 1, filename.length() - 2 );
354  mDataSource = "HDF5:\"" + QgsProject::instance()->readPath( filename ) + "\":" + r.cap( 2 );
355  handled = true;
356  }
357  }
358  else if ( mDataSource.contains( QRegExp( "^(NITF_IM|RADARSAT_2_CALIB):" ) ) )
359  {
360  // NITF_IM:0:filename
361  // RADARSAT_2_CALIB:?:filename
362  QRegExp r( "([^:]+):([^:]+):(.+)" );
363  if ( r.exactMatch( mDataSource ) )
364  {
365  mDataSource = r.cap( 1 ) + ':' + r.cap( 2 ) + ':' + QgsProject::instance()->readPath( r.cap( 3 ) );
366  handled = true;
367  }
368  }
369  }
370 
371  if ( !handled )
373  }
374 
375  // Set the CRS from project file, asking the user if necessary.
376  // Make it the saved CRS to have WMS layer projected correctly.
377  // We will still overwrite whatever GDAL etc picks up anyway
378  // further down this function.
379  mnl = layerElement.namedItem( "layername" );
380  mne = mnl.toElement();
381 
382  QDomNode srsNode = layerElement.namedItem( "srs" );
383  mCRS->readXML( srsNode );
384  mCRS->setValidationHint( tr( "Specify CRS for layer %1" ).arg( mne.text() ) );
385  mCRS->validate();
386  savedCRS = *mCRS;
387 
388  // Do not validate any projections in children, they will be overwritten anyway.
389  // No need to ask the user for a projections when it is overwritten, is there?
392 
393  // now let the children grab what they need from the Dom node.
394  layerError = !readXml( layerElement );
395 
396  // overwrite CRS with what we read from project file before the raster/vector
397  // file readnig functions changed it. They will if projections is specfied in the file.
398  // FIXME: is this necessary?
400  *mCRS = savedCRS;
401 
402  // Abort if any error in layer, such as not found.
403  if ( layerError )
404  {
405  return false;
406  }
407 
408  // the internal name is just the data source basename
409  //QFileInfo dataSourceFileInfo( mDataSource );
410  //internalName = dataSourceFileInfo.baseName();
411 
412  // set ID
413  mnl = layerElement.namedItem( "id" );
414  if ( ! mnl.isNull() )
415  {
416  mne = mnl.toElement();
417  if ( ! mne.isNull() && mne.text().length() > 10 ) // should be at least 17 (yyyyMMddhhmmsszzz)
418  {
419  mID = mne.text();
420  }
421  }
422 
423  // use scale dependent visibility flag
424  setScaleBasedVisibility( layerElement.attribute( "hasScaleBasedVisibilityFlag" ).toInt() == 1 );
425  setMinimumScale( layerElement.attribute( "minimumScale" ).toFloat() );
426  setMaximumScale( layerElement.attribute( "maximumScale" ).toFloat() );
427 
428  // set name
429  mnl = layerElement.namedItem( "layername" );
430  mne = mnl.toElement();
431  setLayerName( mne.text() );
432 
433  //short name
434  QDomElement shortNameElem = layerElement.firstChildElement( "shortname" );
435  if ( !shortNameElem.isNull() )
436  {
437  mShortName = shortNameElem.text();
438  }
439 
440  //title
441  QDomElement titleElem = layerElement.firstChildElement( "title" );
442  if ( !titleElem.isNull() )
443  {
444  mTitle = titleElem.text();
445  }
446 
447  //abstract
448  QDomElement abstractElem = layerElement.firstChildElement( "abstract" );
449  if ( !abstractElem.isNull() )
450  {
451  mAbstract = abstractElem.text();
452  }
453 
454  //keywordList
455  QDomElement keywordListElem = layerElement.firstChildElement( "keywordList" );
456  if ( !keywordListElem.isNull() )
457  {
458  QStringList kwdList;
459  for ( QDomNode n = keywordListElem.firstChild(); !n.isNull(); n = n.nextSibling() )
460  {
461  kwdList << n.toElement().text();
462  }
463  mKeywordList = kwdList.join( ", " );
464  }
465 
466  //metadataUrl
467  QDomElement dataUrlElem = layerElement.firstChildElement( "dataUrl" );
468  if ( !dataUrlElem.isNull() )
469  {
470  mDataUrl = dataUrlElem.text();
471  mDataUrlFormat = dataUrlElem.attribute( "format", "" );
472  }
473 
474  //legendUrl
475  QDomElement legendUrlElem = layerElement.firstChildElement( "legendUrl" );
476  if ( !legendUrlElem.isNull() )
477  {
478  mLegendUrl = legendUrlElem.text();
479  mLegendUrlFormat = legendUrlElem.attribute( "format", "" );
480  }
481 
482  //attribution
483  QDomElement attribElem = layerElement.firstChildElement( "attribution" );
484  if ( !attribElem.isNull() )
485  {
486  mAttribution = attribElem.text();
487  mAttributionUrl = attribElem.attribute( "href", "" );
488  }
489 
490  //metadataUrl
491  QDomElement metaUrlElem = layerElement.firstChildElement( "metadataUrl" );
492  if ( !metaUrlElem.isNull() )
493  {
494  mMetadataUrl = metaUrlElem.text();
495  mMetadataUrlType = metaUrlElem.attribute( "type", "" );
496  mMetadataUrlFormat = metaUrlElem.attribute( "format", "" );
497  }
498 
499 #if 0
500  //read transparency level
501  QDomNode transparencyNode = layer_node.namedItem( "transparencyLevelInt" );
502  if ( ! transparencyNode.isNull() )
503  {
504  // set transparency level only if it's in project
505  // (otherwise it sets the layer transparent)
506  QDomElement myElement = transparencyNode.toElement();
507  setTransparency( myElement.text().toInt() );
508  }
509 #endif
510 
511  readCustomProperties( layerElement );
512 
513  return true;
514 } // bool QgsMapLayer::readLayerXML
515 
516 
517 bool QgsMapLayer::readXml( const QDomNode& layer_node )
518 {
519  Q_UNUSED( layer_node );
520  // NOP by default; children will over-ride with behavior specific to them
521 
522  return true;
523 } // void QgsMapLayer::readXml
524 
525 
526 
527 bool QgsMapLayer::writeLayerXML( QDomElement& layerElement, QDomDocument& document, const QString& relativeBasePath )
528 {
529  // use scale dependent visibility flag
530  layerElement.setAttribute( "hasScaleBasedVisibilityFlag", hasScaleBasedVisibility() ? 1 : 0 );
531  layerElement.setAttribute( "minimumScale", QString::number( minimumScale() ) );
532  layerElement.setAttribute( "maximumScale", QString::number( maximumScale() ) );
533 
534  // ID
535  QDomElement layerId = document.createElement( "id" );
536  QDomText layerIdText = document.createTextNode( id() );
537  layerId.appendChild( layerIdText );
538 
539  layerElement.appendChild( layerId );
540 
541  // data source
542  QDomElement dataSource = document.createElement( "datasource" );
543 
544  QString src = source();
545 
546  QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( this );
547  // TODO: what about postgres, mysql and others, they should not go through writePath()
548  if ( vlayer && vlayer->providerType() == "spatialite" )
549  {
550  QgsDataSourceURI uri( src );
551  QString database = QgsProject::instance()->writePath( uri.database(), relativeBasePath );
552  uri.setConnection( uri.host(), uri.port(), database, uri.username(), uri.password() );
553  src = uri.uri();
554  }
555  else if ( vlayer && vlayer->providerType() == "ogr" )
556  {
557  QStringList theURIParts = src.split( '|' );
558  theURIParts[0] = QgsProject::instance()->writePath( theURIParts[0], relativeBasePath );
559  src = theURIParts.join( "|" );
560  }
561  else if ( vlayer && vlayer->providerType() == "gpx" )
562  {
563  QStringList theURIParts = src.split( '?' );
564  theURIParts[0] = QgsProject::instance()->writePath( theURIParts[0], relativeBasePath );
565  src = theURIParts.join( "?" );
566  }
567  else if ( vlayer && vlayer->providerType() == "delimitedtext" )
568  {
569  QUrl urlSource = QUrl::fromEncoded( src.toAscii() );
570  QUrl urlDest = QUrl::fromLocalFile( QgsProject::instance()->writePath( urlSource.toLocalFile(), relativeBasePath ) );
571  urlDest.setQueryItems( urlSource.queryItems() );
572  src = QString::fromAscii( urlDest.toEncoded() );
573  }
574  else if ( vlayer && vlayer->providerType() == "memory" )
575  {
576  // Refetch the source from the provider, because adding fields actually changes the source for this provider.
577  src = vlayer->dataProvider()->dataSourceUri();
578  }
579  else
580  {
581  bool handled = false;
582 
583  if ( !vlayer )
584  {
585  QgsRasterLayer *rlayer = qobject_cast<QgsRasterLayer *>( this );
586  // Update path for subdataset
587  if ( rlayer && rlayer->providerType() == "gdal" )
588  {
589  if ( src.startsWith( "NETCDF:" ) )
590  {
591  // NETCDF:filename:variable
592  // filename can be quoted with " as it can contain colons
593  QRegExp r( "NETCDF:(.+):([^:]+)" );
594  if ( r.exactMatch( src ) )
595  {
596  QString filename = r.cap( 1 );
597  if ( filename.startsWith( '"' ) && filename.endsWith( '"' ) )
598  filename = filename.mid( 1, filename.length() - 2 );
599  src = "NETCDF:\"" + QgsProject::instance()->writePath( filename, relativeBasePath ) + "\":" + r.cap( 2 );
600  handled = true;
601  }
602  }
603  else if ( src.startsWith( "HDF4_SDS:" ) )
604  {
605  // HDF4_SDS:subdataset_type:file_name:subdataset_index
606  // filename can be quoted with " as it can contain colons
607  QRegExp r( "HDF4_SDS:([^:]+):(.+):([^:]+)" );
608  if ( r.exactMatch( src ) )
609  {
610  QString filename = r.cap( 2 );
611  if ( filename.startsWith( '"' ) && filename.endsWith( '"' ) )
612  filename = filename.mid( 1, filename.length() - 2 );
613  src = "HDF4_SDS:" + r.cap( 1 ) + ":\"" + QgsProject::instance()->writePath( filename, relativeBasePath ) + "\":" + r.cap( 3 );
614  handled = true;
615  }
616  }
617  else if ( src.startsWith( "HDF5:" ) )
618  {
619  // HDF5:file_name:subdataset
620  // filename can be quoted with " as it can contain colons
621  QRegExp r( "HDF5:(.+):([^:]+)" );
622  if ( r.exactMatch( src ) )
623  {
624  QString filename = r.cap( 1 );
625  if ( filename.startsWith( '"' ) && filename.endsWith( '"' ) )
626  filename = filename.mid( 1, filename.length() - 2 );
627  src = "HDF5:\"" + QgsProject::instance()->writePath( filename, relativeBasePath ) + "\":" + r.cap( 2 );
628  handled = true;
629  }
630  }
631  else if ( src.contains( QRegExp( "^(NITF_IM|RADARSAT_2_CALIB):" ) ) )
632  {
633  // NITF_IM:0:filename
634  // RADARSAT_2_CALIB:?:filename
635  QRegExp r( "([^:]+):([^:]+):(.+)" );
636  if ( r.exactMatch( src ) )
637  {
638  src = r.cap( 1 ) + ':' + r.cap( 2 ) + ':' + QgsProject::instance()->writePath( r.cap( 3 ), relativeBasePath );
639  handled = true;
640  }
641  }
642  }
643  }
644 
645  if ( !handled )
646  src = QgsProject::instance()->writePath( src, relativeBasePath );
647  }
648 
649  QDomText dataSourceText = document.createTextNode( src );
650  dataSource.appendChild( dataSourceText );
651 
652  layerElement.appendChild( dataSource );
653 
654 
655  // layer name
656  QDomElement layerName = document.createElement( "layername" );
657  QDomText layerNameText = document.createTextNode( originalName() );
658  layerName.appendChild( layerNameText );
659  layerElement.appendChild( layerName );
660 
661  // layer short name
662  if ( !mShortName.isEmpty() )
663  {
664  QDomElement layerShortName = document.createElement( "shortname" );
665  QDomText layerShortNameText = document.createTextNode( mShortName );
666  layerShortName.appendChild( layerShortNameText );
667  layerElement.appendChild( layerShortName );
668  }
669 
670  // layer title
671  if ( !mTitle.isEmpty() )
672  {
673  QDomElement layerTitle = document.createElement( "title" );
674  QDomText layerTitleText = document.createTextNode( mTitle );
675  layerTitle.appendChild( layerTitleText );
676  layerElement.appendChild( layerTitle );
677  }
678 
679  // layer abstract
680  if ( !mAbstract.isEmpty() )
681  {
682  QDomElement layerAbstract = document.createElement( "abstract" );
683  QDomText layerAbstractText = document.createTextNode( mAbstract );
684  layerAbstract.appendChild( layerAbstractText );
685  layerElement.appendChild( layerAbstract );
686  }
687 
688  // layer keyword list
689  QStringList keywordStringList = keywordList().split( ',' );
690  if ( !keywordStringList.isEmpty() )
691  {
692  QDomElement layerKeywordList = document.createElement( "keywordList" );
693  for ( int i = 0; i < keywordStringList.size(); ++i )
694  {
695  QDomElement layerKeywordValue = document.createElement( "value" );
696  QDomText layerKeywordText = document.createTextNode( keywordStringList.at( i ).trimmed() );
697  layerKeywordValue.appendChild( layerKeywordText );
698  layerKeywordList.appendChild( layerKeywordValue );
699  }
700  layerElement.appendChild( layerKeywordList );
701  }
702 
703  // layer metadataUrl
704  QString aDataUrl = dataUrl();
705  if ( !aDataUrl.isEmpty() )
706  {
707  QDomElement layerDataUrl = document.createElement( "dataUrl" );
708  QDomText layerDataUrlText = document.createTextNode( aDataUrl );
709  layerDataUrl.appendChild( layerDataUrlText );
710  layerDataUrl.setAttribute( "format", dataUrlFormat() );
711  layerElement.appendChild( layerDataUrl );
712  }
713 
714  // layer legendUrl
715  QString aLegendUrl = legendUrl();
716  if ( !aLegendUrl.isEmpty() )
717  {
718  QDomElement layerLegendUrl = document.createElement( "legendUrl" );
719  QDomText layerLegendUrlText = document.createTextNode( aLegendUrl );
720  layerLegendUrl.appendChild( layerLegendUrlText );
721  layerLegendUrl.setAttribute( "format", legendUrlFormat() );
722  layerElement.appendChild( layerLegendUrl );
723  }
724 
725  // layer attribution
726  QString aAttribution = attribution();
727  if ( !aAttribution.isEmpty() )
728  {
729  QDomElement layerAttribution = document.createElement( "attribution" );
730  QDomText layerAttributionText = document.createTextNode( aAttribution );
731  layerAttribution.appendChild( layerAttributionText );
732  layerAttribution.setAttribute( "href", attributionUrl() );
733  layerElement.appendChild( layerAttribution );
734  }
735 
736  // layer metadataUrl
737  QString aMetadataUrl = metadataUrl();
738  if ( !aMetadataUrl.isEmpty() )
739  {
740  QDomElement layerMetadataUrl = document.createElement( "metadataUrl" );
741  QDomText layerMetadataUrlText = document.createTextNode( aMetadataUrl );
742  layerMetadataUrl.appendChild( layerMetadataUrlText );
743  layerMetadataUrl.setAttribute( "type", metadataUrlType() );
744  layerMetadataUrl.setAttribute( "format", metadataUrlFormat() );
745  layerElement.appendChild( layerMetadataUrl );
746  }
747 
748  // timestamp if supported
749  if ( timestamp() > QDateTime() )
750  {
751  QDomElement stamp = document.createElement( "timestamp" );
752  QDomText stampText = document.createTextNode( timestamp().toString( Qt::ISODate ) );
753  stamp.appendChild( stampText );
754  layerElement.appendChild( stamp );
755  }
756 
757  layerElement.appendChild( layerName );
758 
759  // zorder
760  // This is no longer stored in the project file. It is superfluous since the layers
761  // are written and read in the proper order.
762 
763  // spatial reference system id
764  QDomElement mySrsElement = document.createElement( "srs" );
765  mCRS->writeXML( mySrsElement, document );
766  layerElement.appendChild( mySrsElement );
767 
768 #if 0
769  // <transparencyLevelInt>
770  QDomElement transparencyLevelIntElement = document.createElement( "transparencyLevelInt" );
771  QDomText transparencyLevelIntText = document.createTextNode( QString::number( getTransparency() ) );
772  transparencyLevelIntElement.appendChild( transparencyLevelIntText );
773  maplayer.appendChild( transparencyLevelIntElement );
774 #endif
775 
776  // now append layer node to map layer node
777 
778  writeCustomProperties( layerElement, document );
779 
780  return writeXml( layerElement, document );
781 
782 } // bool QgsMapLayer::writeXML
783 
785 {
786  QDomDocument doc( "qgis-layer-definition" );
787  QDomElement qgiselm = doc.createElement( "qlr" );
788  doc.appendChild( qgiselm );
789  QDomElement layerselm = doc.createElement( "maplayers" );
790  Q_FOREACH ( QgsMapLayer* layer, layers )
791  {
792  QDomElement layerelm = doc.createElement( "maplayer" );
793  layer->writeLayerXML( layerelm, doc, relativeBasePath );
794  layerselm.appendChild( layerelm );
795  }
796  qgiselm.appendChild( layerselm );
797  return doc;
798 }
799 
800 QList<QgsMapLayer*> QgsMapLayer::fromLayerDefinition( QDomDocument& document, bool addToRegistry, bool addToLegend )
801 {
802  QList<QgsMapLayer*> layers;
803  QDomNodeList layernodes = document.elementsByTagName( "maplayer" );
804  for ( int i = 0; i < layernodes.size(); ++i )
805  {
806  QDomNode layernode = layernodes.at( i );
807  QDomElement layerElem = layernode.toElement();
808 
809  QString type = layerElem.attribute( "type" );
810  QgsDebugMsg( type );
811  QgsMapLayer *layer = nullptr;
812 
813  if ( type == "vector" )
814  {
815  layer = new QgsVectorLayer;
816  }
817  else if ( type == "raster" )
818  {
819  layer = new QgsRasterLayer;
820  }
821  else if ( type == "plugin" )
822  {
823  QString typeName = layerElem.attribute( "name" );
824  layer = QgsPluginLayerRegistry::instance()->createLayer( typeName );
825  }
826 
827  if ( !layer )
828  continue;
829 
830  bool ok = layer->readLayerXML( layerElem );
831  if ( ok )
832  {
833  layers << layer;
834  if ( addToRegistry )
835  QgsMapLayerRegistry::instance()->addMapLayer( layer, addToLegend );
836  }
837  }
838  return layers;
839 }
840 
842 {
843  QFile file( qlrfile );
844  if ( !file.open( QIODevice::ReadOnly ) )
845  {
846  QgsDebugMsg( "Can't open file" );
847  return QList<QgsMapLayer*>();
848  }
849 
850  QDomDocument doc;
851  if ( !doc.setContent( &file ) )
852  {
853  QgsDebugMsg( "Can't set content" );
854  return QList<QgsMapLayer*>();
855  }
856 
857  QFileInfo fileinfo( file );
858  QDir::setCurrent( fileinfo.absoluteDir().path() );
859  return QgsMapLayer::fromLayerDefinition( doc );
860 }
861 
862 
863 bool QgsMapLayer::writeXml( QDomNode & layer_node, QDomDocument & document )
864 {
865  Q_UNUSED( layer_node );
866  Q_UNUSED( document );
867  // NOP by default; children will over-ride with behavior specific to them
868 
869  return true;
870 } // void QgsMapLayer::writeXml
871 
872 
873 void QgsMapLayer::readCustomProperties( const QDomNode &layerNode, const QString &keyStartsWith )
874 {
875  mCustomProperties.readXml( layerNode, keyStartsWith );
876 }
877 
879 {
880  mCustomProperties.writeXml( layerNode, doc );
881 }
882 
883 void QgsMapLayer::readStyleManager( const QDomNode& layerNode )
884 {
885  QDomElement styleMgrElem = layerNode.firstChildElement( "map-layer-style-manager" );
886  if ( !styleMgrElem.isNull() )
887  mStyleManager->readXml( styleMgrElem );
888  else
889  mStyleManager->reset();
890 }
891 
893 {
894  if ( mStyleManager )
895  {
896  QDomElement styleMgrElem = doc.createElement( "map-layer-style-manager" );
897  mStyleManager->writeXml( styleMgrElem );
898  layerNode.appendChild( styleMgrElem );
899  }
900 }
901 
902 
903 
904 
906 {
907  return mValid;
908 }
909 
910 
912 {
913  QgsDebugMsg( "called" );
914  // TODO: emit a signal - it will be used to update legend
915 }
916 
917 
919 {
920  return QString();
921 }
922 
924 {
925  return QString();
926 }
927 
928 #if 0
929 void QgsMapLayer::connectNotify( const char * signal )
930 {
931  Q_UNUSED( signal );
932  QgsDebugMsgLevel( "QgsMapLayer connected to " + QString( signal ), 3 );
933 } // QgsMapLayer::connectNotify
934 #endif
935 
936 
937 void QgsMapLayer::toggleScaleBasedVisibility( bool theVisibilityFlag )
938 {
939  setScaleBasedVisibility( theVisibilityFlag );
940 }
941 
943 {
944  return mScaleBasedVisibility;
945 }
946 
947 void QgsMapLayer::setMinimumScale( const float theMinScale )
948 {
949  mMinScale = theMinScale;
950 }
951 
953 {
954  return mMinScale;
955 }
956 
957 
958 void QgsMapLayer::setMaximumScale( const float theMaxScale )
959 {
960  mMaxScale = theMaxScale;
961 }
962 
963 void QgsMapLayer::setScaleBasedVisibility( const bool enabled )
964 {
965  mScaleBasedVisibility = enabled;
966 }
967 
969 {
970  return mMaxScale;
971 }
972 
974 {
975  return QStringList(); // Empty
976 }
977 
979 {
980  Q_UNUSED( layers );
981  // NOOP
982 }
983 
985 {
986  Q_UNUSED( name );
987  Q_UNUSED( vis );
988  // NOOP
989 }
990 
992 {
993  return *mCRS;
994 }
995 
996 void QgsMapLayer::setCrs( const QgsCoordinateReferenceSystem& srs, bool emitSignal )
997 {
998  *mCRS = srs;
999 
1000  if ( !mCRS->isValid() )
1001  {
1002  mCRS->setValidationHint( tr( "Specify CRS for layer %1" ).arg( name() ) );
1003  mCRS->validate();
1004  }
1005 
1006  if ( emitSignal )
1007  emit layerCrsChanged();
1008 }
1009 
1011 {
1012  // Capitalise the first letter of the layer name if requested
1013  QSettings settings;
1014  bool capitaliseLayerName =
1015  settings.value( "/qgis/capitaliseLayerName", QVariant( false ) ).toBool();
1016 
1017  QString layerName( name );
1018 
1019  if ( capitaliseLayerName )
1020  layerName = layerName.left( 1 ).toUpper() + layerName.mid( 1 );
1021 
1022  return layerName;
1023 }
1024 
1026 {
1027  QString myURI = publicSource();
1028 
1029  // if file is using the VSIFILE mechanism, remove the prefix
1030  if ( myURI.startsWith( "/vsigzip/", Qt::CaseInsensitive ) )
1031  {
1032  myURI.remove( 0, 9 );
1033  }
1034  else if ( myURI.startsWith( "/vsizip/", Qt::CaseInsensitive ) &&
1035  myURI.endsWith( ".zip", Qt::CaseInsensitive ) )
1036  {
1037  // ideally we should look for .qml file inside zip file
1038  myURI.remove( 0, 8 );
1039  }
1040  else if ( myURI.startsWith( "/vsitar/", Qt::CaseInsensitive ) &&
1041  ( myURI.endsWith( ".tar", Qt::CaseInsensitive ) ||
1042  myURI.endsWith( ".tar.gz", Qt::CaseInsensitive ) ||
1043  myURI.endsWith( ".tgz", Qt::CaseInsensitive ) ) )
1044  {
1045  // ideally we should look for .qml file inside tar file
1046  myURI.remove( 0, 8 );
1047  }
1048 
1049  QFileInfo myFileInfo( myURI );
1050  QString key;
1051 
1052  if ( myFileInfo.exists() )
1053  {
1054  // if file is using the /vsizip/ or /vsigzip/ mechanism, cleanup the name
1055  if ( myURI.endsWith( ".gz", Qt::CaseInsensitive ) )
1056  myURI.chop( 3 );
1057  else if ( myURI.endsWith( ".zip", Qt::CaseInsensitive ) )
1058  myURI.chop( 4 );
1059  else if ( myURI.endsWith( ".tar", Qt::CaseInsensitive ) )
1060  myURI.chop( 4 );
1061  else if ( myURI.endsWith( ".tar.gz", Qt::CaseInsensitive ) )
1062  myURI.chop( 7 );
1063  else if ( myURI.endsWith( ".tgz", Qt::CaseInsensitive ) )
1064  myURI.chop( 4 );
1065  myFileInfo.setFile( myURI );
1066  // get the file name for our .qml style file
1067  key = myFileInfo.path() + QDir::separator() + myFileInfo.completeBaseName() + ".qml";
1068  }
1069  else
1070  {
1071  key = publicSource();
1072  }
1073 
1074  return key;
1075 }
1076 
1078 {
1079  return loadNamedStyle( styleURI(), theResultFlag );
1080 }
1081 
1082 bool QgsMapLayer::loadNamedStyleFromDb( const QString &db, const QString &theURI, QString &qml )
1083 {
1084  QgsDebugMsg( QString( "db = %1 uri = %2" ).arg( db, theURI ) );
1085 
1086  bool theResultFlag = false;
1087 
1088  // read from database
1089  sqlite3 *myDatabase;
1090  sqlite3_stmt *myPreparedStatement;
1091  const char *myTail;
1092  int myResult;
1093 
1094  QgsDebugMsg( QString( "Trying to load style for \"%1\" from \"%2\"" ).arg( theURI, db ) );
1095 
1096  if ( db.isEmpty() || !QFile( db ).exists() )
1097  return false;
1098 
1099  myResult = sqlite3_open_v2( db.toUtf8().data(), &myDatabase, SQLITE_OPEN_READONLY, nullptr );
1100  if ( myResult != SQLITE_OK )
1101  {
1102  return false;
1103  }
1104 
1105  QString mySql = "select qml from tbl_styles where style=?";
1106  myResult = sqlite3_prepare( myDatabase, mySql.toUtf8().data(), mySql.toUtf8().length(), &myPreparedStatement, &myTail );
1107  if ( myResult == SQLITE_OK )
1108  {
1109  QByteArray param = theURI.toUtf8();
1110 
1111  if ( sqlite3_bind_text( myPreparedStatement, 1, param.data(), param.length(), SQLITE_STATIC ) == SQLITE_OK &&
1112  sqlite3_step( myPreparedStatement ) == SQLITE_ROW )
1113  {
1114  qml = QString::fromUtf8( reinterpret_cast< const char * >( sqlite3_column_text( myPreparedStatement, 0 ) ) );
1115  theResultFlag = true;
1116  }
1117 
1118  sqlite3_finalize( myPreparedStatement );
1119  }
1120 
1121  sqlite3_close( myDatabase );
1122 
1123  return theResultFlag;
1124 }
1125 
1126 
1127 QString QgsMapLayer::loadNamedStyle( const QString &theURI, bool &theResultFlag )
1128 {
1129  QgsDebugMsg( QString( "uri = %1 myURI = %2" ).arg( theURI, publicSource() ) );
1130 
1131  theResultFlag = false;
1132 
1133  QDomDocument myDocument( "qgis" );
1134 
1135  // location of problem associated with errorMsg
1136  int line, column;
1137  QString myErrorMessage;
1138 
1139  QFile myFile( theURI );
1140  if ( myFile.open( QFile::ReadOnly ) )
1141  {
1142  // read file
1143  theResultFlag = myDocument.setContent( &myFile, &myErrorMessage, &line, &column );
1144  if ( !theResultFlag )
1145  myErrorMessage = tr( "%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column );
1146  myFile.close();
1147  }
1148  else
1149  {
1150  QFileInfo project( QgsProject::instance()->fileName() );
1151  QgsDebugMsg( QString( "project fileName: %1" ).arg( project.absoluteFilePath() ) );
1152 
1153  QString qml;
1154  if ( loadNamedStyleFromDb( QDir( QgsApplication::qgisSettingsDirPath() ).absoluteFilePath( "qgis.qmldb" ), theURI, qml ) ||
1155  ( project.exists() && loadNamedStyleFromDb( project.absoluteDir().absoluteFilePath( project.baseName() + ".qmldb" ), theURI, qml ) ) ||
1156  loadNamedStyleFromDb( QDir( QgsApplication::pkgDataPath() ).absoluteFilePath( "resources/qgis.qmldb" ), theURI, qml ) )
1157  {
1158  theResultFlag = myDocument.setContent( qml, &myErrorMessage, &line, &column );
1159  if ( !theResultFlag )
1160  {
1161  myErrorMessage = tr( "%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column );
1162  }
1163  }
1164  else
1165  {
1166  myErrorMessage = tr( "Style not found in database" );
1167  }
1168  }
1169 
1170  if ( !theResultFlag )
1171  {
1172  return myErrorMessage;
1173  }
1174 
1175  theResultFlag = importNamedStyle( myDocument, myErrorMessage );
1176  if ( !theResultFlag )
1177  myErrorMessage = tr( "Loading style file %1 failed because:\n%2" ).arg( theURI, myErrorMessage );
1178 
1179  return myErrorMessage;
1180 }
1181 
1182 
1183 bool QgsMapLayer::importNamedStyle( QDomDocument& myDocument, QString& myErrorMessage )
1184 {
1185  QDomElement myRoot = myDocument.firstChildElement( "qgis" );
1186  if ( myRoot.isNull() )
1187  {
1188  myErrorMessage = tr( "Root <qgis> element could not be found" );
1189  return false;
1190  }
1191 
1192  // get style file version string, if any
1193  QgsProjectVersion fileVersion( myRoot.attribute( "version" ) );
1194  QgsProjectVersion thisVersion( QGis::QGIS_VERSION );
1195 
1196  if ( thisVersion > fileVersion )
1197  {
1198  QgsProjectFileTransform styleFile( myDocument, fileVersion );
1199  styleFile.updateRevision( thisVersion );
1200  }
1201 
1202  //Test for matching geometry type on vector layers when applying, if geometry type is given in the style
1203  if ( type() == QgsMapLayer::VectorLayer && !myRoot.firstChildElement( "layerGeometryType" ).isNull() )
1204  {
1205  QgsVectorLayer *vl = static_cast<QgsVectorLayer*>( this );
1206  int importLayerGeometryType = myRoot.firstChildElement( "layerGeometryType" ).text().toInt();
1207  if ( vl->geometryType() != importLayerGeometryType )
1208  {
1209  myErrorMessage = tr( "Cannot apply style to layer with a different geometry type" );
1210  return false;
1211  }
1212  }
1213 
1214  // use scale dependent visibility flag
1215  setScaleBasedVisibility( myRoot.attribute( "hasScaleBasedVisibilityFlag" ).toInt() == 1 );
1216  setMinimumScale( myRoot.attribute( "minimumScale" ).toFloat() );
1217  setMaximumScale( myRoot.attribute( "maximumScale" ).toFloat() );
1218 
1219 #if 0
1220  //read transparency level
1221  QDomNode transparencyNode = myRoot.namedItem( "transparencyLevelInt" );
1222  if ( ! transparencyNode.isNull() )
1223  {
1224  // set transparency level only if it's in project
1225  // (otherwise it sets the layer transparent)
1226  QDomElement myElement = transparencyNode.toElement();
1227  setTransparency( myElement.text().toInt() );
1228  }
1229 #endif
1230 
1231  return readSymbology( myRoot, myErrorMessage );
1232 }
1233 
1235 {
1236  QDomImplementation DomImplementation;
1237  QDomDocumentType documentType = DomImplementation.createDocumentType( "qgis", "http://mrcc.com/qgis.dtd", "SYSTEM" );
1238  QDomDocument myDocument( documentType );
1239 
1240  QDomElement myRootNode = myDocument.createElement( "qgis" );
1241  myRootNode.setAttribute( "version", QString( "%1" ).arg( QGis::QGIS_VERSION ) );
1242  myDocument.appendChild( myRootNode );
1243 
1244  myRootNode.setAttribute( "hasScaleBasedVisibilityFlag", hasScaleBasedVisibility() ? 1 : 0 );
1245  myRootNode.setAttribute( "minimumScale", QString::number( minimumScale() ) );
1246  myRootNode.setAttribute( "maximumScale", QString::number( maximumScale() ) );
1247 
1248 #if 0
1249  // <transparencyLevelInt>
1250  QDomElement transparencyLevelIntElement = myDocument.createElement( "transparencyLevelInt" );
1251  QDomText transparencyLevelIntText = myDocument.createTextNode( QString::number( getTransparency() ) );
1252  transparencyLevelIntElement.appendChild( transparencyLevelIntText );
1253  myRootNode.appendChild( transparencyLevelIntElement );
1254 #endif
1255 
1256  if ( !writeSymbology( myRootNode, myDocument, errorMsg ) )
1257  {
1258  errorMsg = QObject::tr( "Could not save symbology because:\n%1" ).arg( errorMsg );
1259  return;
1260  }
1261 
1262  /*
1263  * Check to see if the layer is vector - in which case we should also export its geometryType
1264  * to avoid eventually pasting to a layer with a different geometry
1265  */
1266  if ( type() == QgsMapLayer::VectorLayer )
1267  {
1268  //Getting the selectionLayer geometry
1269  QgsVectorLayer *vl = static_cast<QgsVectorLayer*>( this );
1270  QString geoType = QString::number( vl->geometryType() );
1271 
1272  //Adding geometryinformation
1273  QDomElement layerGeometryType = myDocument.createElement( "layerGeometryType" );
1274  QDomText type = myDocument.createTextNode( geoType );
1275 
1276  layerGeometryType.appendChild( type );
1277  myRootNode.appendChild( layerGeometryType );
1278  }
1279 
1280  doc = myDocument;
1281 }
1282 
1284 {
1285  return saveNamedStyle( styleURI(), theResultFlag );
1286 }
1287 
1288 QString QgsMapLayer::saveNamedStyle( const QString &theURI, bool &theResultFlag )
1289 {
1290  QString myErrorMessage;
1291  QDomDocument myDocument;
1292  exportNamedStyle( myDocument, myErrorMessage );
1293 
1294  // check if the uri is a file or ends with .qml,
1295  // which indicates that it should become one
1296  // everything else goes to the database
1297  QString filename;
1298 
1299  QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( this );
1300  if ( vlayer && vlayer->providerType() == "ogr" )
1301  {
1302  QStringList theURIParts = theURI.split( '|' );
1303  filename = theURIParts[0];
1304  }
1305  else if ( vlayer && vlayer->providerType() == "gpx" )
1306  {
1307  QStringList theURIParts = theURI.split( '?' );
1308  filename = theURIParts[0];
1309  }
1310  else if ( vlayer && vlayer->providerType() == "delimitedtext" )
1311  {
1312  filename = QUrl::fromEncoded( theURI.toAscii() ).toLocalFile();
1313  // toLocalFile() returns an empty string if theURI is a plain Windows-path, e.g. "C:/style.qml"
1314  if ( filename.isEmpty() )
1315  filename = theURI;
1316  }
1317  else
1318  {
1319  filename = theURI;
1320  }
1321 
1322  QFileInfo myFileInfo( filename );
1323  if ( myFileInfo.exists() || filename.endsWith( ".qml", Qt::CaseInsensitive ) )
1324  {
1325  QFileInfo myDirInfo( myFileInfo.path() ); //excludes file name
1326  if ( !myDirInfo.isWritable() )
1327  {
1328  return tr( "The directory containing your dataset needs to be writable!" );
1329  }
1330 
1331  // now construct the file name for our .qml style file
1332  QString myFileName = myFileInfo.path() + QDir::separator() + myFileInfo.completeBaseName() + ".qml";
1333 
1334  QFile myFile( myFileName );
1335  if ( myFile.open( QFile::WriteOnly | QFile::Truncate ) )
1336  {
1337  QTextStream myFileStream( &myFile );
1338  // save as utf-8 with 2 spaces for indents
1339  myDocument.save( myFileStream, 2 );
1340  myFile.close();
1341  theResultFlag = true;
1342  return tr( "Created default style file as %1" ).arg( myFileName );
1343  }
1344  else
1345  {
1346  theResultFlag = false;
1347  return tr( "ERROR: Failed to created default style file as %1. Check file permissions and retry." ).arg( myFileName );
1348  }
1349  }
1350  else
1351  {
1352  QString qml = myDocument.toString();
1353 
1354  // read from database
1355  sqlite3 *myDatabase;
1356  sqlite3_stmt *myPreparedStatement;
1357  const char *myTail;
1358  int myResult;
1359 
1360  myResult = sqlite3_open( QDir( QgsApplication::qgisSettingsDirPath() ).absoluteFilePath( "qgis.qmldb" ).toUtf8().data(), &myDatabase );
1361  if ( myResult != SQLITE_OK )
1362  {
1363  return tr( "User database could not be opened." );
1364  }
1365 
1366  QByteArray param0 = theURI.toUtf8();
1367  QByteArray param1 = qml.toUtf8();
1368 
1369  QString mySql = "create table if not exists tbl_styles(style varchar primary key,qml varchar)";
1370  myResult = sqlite3_prepare( myDatabase, mySql.toUtf8().data(), mySql.toUtf8().length(), &myPreparedStatement, &myTail );
1371  if ( myResult == SQLITE_OK )
1372  {
1373  if ( sqlite3_step( myPreparedStatement ) != SQLITE_DONE )
1374  {
1375  sqlite3_finalize( myPreparedStatement );
1376  sqlite3_close( myDatabase );
1377  theResultFlag = false;
1378  return tr( "The style table could not be created." );
1379  }
1380  }
1381 
1382  sqlite3_finalize( myPreparedStatement );
1383 
1384  mySql = "insert into tbl_styles(style,qml) values (?,?)";
1385  myResult = sqlite3_prepare( myDatabase, mySql.toUtf8().data(), mySql.toUtf8().length(), &myPreparedStatement, &myTail );
1386  if ( myResult == SQLITE_OK )
1387  {
1388  if ( sqlite3_bind_text( myPreparedStatement, 1, param0.data(), param0.length(), SQLITE_STATIC ) == SQLITE_OK &&
1389  sqlite3_bind_text( myPreparedStatement, 2, param1.data(), param1.length(), SQLITE_STATIC ) == SQLITE_OK &&
1390  sqlite3_step( myPreparedStatement ) == SQLITE_DONE )
1391  {
1392  theResultFlag = true;
1393  myErrorMessage = tr( "The style %1 was saved to database" ).arg( theURI );
1394  }
1395  }
1396 
1397  sqlite3_finalize( myPreparedStatement );
1398 
1399  if ( !theResultFlag )
1400  {
1401  QString mySql = "update tbl_styles set qml=? where style=?";
1402  myResult = sqlite3_prepare( myDatabase, mySql.toUtf8().data(), mySql.toUtf8().length(), &myPreparedStatement, &myTail );
1403  if ( myResult == SQLITE_OK )
1404  {
1405  if ( sqlite3_bind_text( myPreparedStatement, 2, param0.data(), param0.length(), SQLITE_STATIC ) == SQLITE_OK &&
1406  sqlite3_bind_text( myPreparedStatement, 1, param1.data(), param1.length(), SQLITE_STATIC ) == SQLITE_OK &&
1407  sqlite3_step( myPreparedStatement ) == SQLITE_DONE )
1408  {
1409  theResultFlag = true;
1410  myErrorMessage = tr( "The style %1 was updated in the database." ).arg( theURI );
1411  }
1412  else
1413  {
1414  theResultFlag = false;
1415  myErrorMessage = tr( "The style %1 could not be updated in the database." ).arg( theURI );
1416  }
1417  }
1418  else
1419  {
1420  theResultFlag = false;
1421  myErrorMessage = tr( "The style %1 could not be inserted into database." ).arg( theURI );
1422  }
1423 
1424  sqlite3_finalize( myPreparedStatement );
1425  }
1426 
1427  sqlite3_close( myDatabase );
1428  }
1429 
1430  return myErrorMessage;
1431 }
1432 
1434 {
1435  QDomDocument myDocument = QDomDocument();
1436 
1437  QDomNode header = myDocument.createProcessingInstruction( "xml", "version=\"1.0\" encoding=\"UTF-8\"" );
1438  myDocument.appendChild( header );
1439 
1440  // Create the root element
1441  QDomElement root = myDocument.createElementNS( "http://www.opengis.net/sld", "StyledLayerDescriptor" );
1442  root.setAttribute( "version", "1.1.0" );
1443  root.setAttribute( "units", "mm" ); // default qgsmaprenderer is Millimeters
1444  root.setAttribute( "xsi:schemaLocation", "http://www.opengis.net/sld http://schemas.opengis.net/sld/1.1.0/StyledLayerDescriptor.xsd" );
1445  root.setAttribute( "xmlns:ogc", "http://www.opengis.net/ogc" );
1446  root.setAttribute( "xmlns:se", "http://www.opengis.net/se" );
1447  root.setAttribute( "xmlns:xlink", "http://www.w3.org/1999/xlink" );
1448  root.setAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
1449  myDocument.appendChild( root );
1450 
1451  // Create the NamedLayer element
1452  QDomElement namedLayerNode = myDocument.createElement( "NamedLayer" );
1453  root.appendChild( namedLayerNode );
1454 
1455  QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( this );
1456  if ( !vlayer )
1457  {
1458  errorMsg = tr( "Could not save symbology because:\n%1" )
1459  .arg( "Non-vector layers not supported yet" );
1460  return;
1461  }
1462 
1463  if ( !vlayer->writeSld( namedLayerNode, myDocument, errorMsg ) )
1464  {
1465  errorMsg = tr( "Could not save symbology because:\n%1" ).arg( errorMsg );
1466  return;
1467  }
1468 
1469  doc = myDocument;
1470 }
1471 
1472 QString QgsMapLayer::saveSldStyle( const QString &theURI, bool &theResultFlag )
1473 {
1474  QString errorMsg;
1475  QDomDocument myDocument;
1476  exportSldStyle( myDocument, errorMsg );
1477  if ( !errorMsg.isNull() )
1478  {
1479  theResultFlag = false;
1480  return errorMsg;
1481  }
1482  QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( this );
1483 
1484  // check if the uri is a file or ends with .sld,
1485  // which indicates that it should become one
1486  QString filename;
1487  if ( vlayer->providerType() == "ogr" )
1488  {
1489  QStringList theURIParts = theURI.split( '|' );
1490  filename = theURIParts[0];
1491  }
1492  else if ( vlayer->providerType() == "gpx" )
1493  {
1494  QStringList theURIParts = theURI.split( '?' );
1495  filename = theURIParts[0];
1496  }
1497  else if ( vlayer->providerType() == "delimitedtext" )
1498  {
1499  filename = QUrl::fromEncoded( theURI.toAscii() ).toLocalFile();
1500  // toLocalFile() returns an empty string if theURI is a plain Windows-path, e.g. "C:/style.qml"
1501  if ( filename.isEmpty() )
1502  filename = theURI;
1503  }
1504  else
1505  {
1506  filename = theURI;
1507  }
1508 
1509  QFileInfo myFileInfo( filename );
1510  if ( myFileInfo.exists() || filename.endsWith( ".sld", Qt::CaseInsensitive ) )
1511  {
1512  QFileInfo myDirInfo( myFileInfo.path() ); //excludes file name
1513  if ( !myDirInfo.isWritable() )
1514  {
1515  return tr( "The directory containing your dataset needs to be writable!" );
1516  }
1517 
1518  // now construct the file name for our .sld style file
1519  QString myFileName = myFileInfo.path() + QDir::separator() + myFileInfo.completeBaseName() + ".sld";
1520 
1521  QFile myFile( myFileName );
1522  if ( myFile.open( QFile::WriteOnly | QFile::Truncate ) )
1523  {
1524  QTextStream myFileStream( &myFile );
1525  // save as utf-8 with 2 spaces for indents
1526  myDocument.save( myFileStream, 2 );
1527  myFile.close();
1528  theResultFlag = true;
1529  return tr( "Created default style file as %1" ).arg( myFileName );
1530  }
1531  }
1532 
1533  theResultFlag = false;
1534  return tr( "ERROR: Failed to created SLD style file as %1. Check file permissions and retry." ).arg( filename );
1535 }
1536 
1537 QString QgsMapLayer::loadSldStyle( const QString &theURI, bool &theResultFlag )
1538 {
1539  QgsDebugMsg( "Entered." );
1540 
1541  theResultFlag = false;
1542 
1543  QDomDocument myDocument;
1544 
1545  // location of problem associated with errorMsg
1546  int line, column;
1547  QString myErrorMessage;
1548 
1549  QFile myFile( theURI );
1550  if ( myFile.open( QFile::ReadOnly ) )
1551  {
1552  // read file
1553  theResultFlag = myDocument.setContent( &myFile, true, &myErrorMessage, &line, &column );
1554  if ( !theResultFlag )
1555  myErrorMessage = tr( "%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column );
1556  myFile.close();
1557  }
1558  else
1559  {
1560  myErrorMessage = tr( "Unable to open file %1" ).arg( theURI );
1561  }
1562 
1563  if ( !theResultFlag )
1564  {
1565  return myErrorMessage;
1566  }
1567 
1568  // check for root SLD element
1569  QDomElement myRoot = myDocument.firstChildElement( "StyledLayerDescriptor" );
1570  if ( myRoot.isNull() )
1571  {
1572  myErrorMessage = QString( "Error: StyledLayerDescriptor element not found in %1" ).arg( theURI );
1573  theResultFlag = false;
1574  return myErrorMessage;
1575  }
1576 
1577  // now get the style node out and pass it over to the layer
1578  // to deserialise...
1579  QDomElement namedLayerElem = myRoot.firstChildElement( "NamedLayer" );
1580  if ( namedLayerElem.isNull() )
1581  {
1582  myErrorMessage = QLatin1String( "Info: NamedLayer element not found." );
1583  theResultFlag = false;
1584  return myErrorMessage;
1585  }
1586 
1587  QString errorMsg;
1588  theResultFlag = readSld( namedLayerElem, errorMsg );
1589  if ( !theResultFlag )
1590  {
1591  myErrorMessage = tr( "Loading style file %1 failed because:\n%2" ).arg( theURI, errorMsg );
1592  return myErrorMessage;
1593  }
1594 
1595  return "";
1596 }
1597 
1598 
1600 {
1601  return &mUndoStack;
1602 }
1603 
1604 
1605 void QgsMapLayer::setCustomProperty( const QString& key, const QVariant& value )
1606 {
1607  mCustomProperties.setValue( key, value );
1608 }
1609 
1610 QVariant QgsMapLayer::customProperty( const QString& value, const QVariant& defaultValue ) const
1611 {
1612  return mCustomProperties.value( value, defaultValue );
1613 }
1614 
1616 {
1617  mCustomProperties.remove( key );
1618 }
1619 
1620 
1621 
1623 {
1624  return false;
1625 }
1626 
1627 void QgsMapLayer::setValid( bool valid )
1628 {
1629  mValid = valid;
1630 }
1631 
1633 {
1634  emit repaintRequested();
1635 }
1636 
1638 {
1639  if ( legend == mLegend )
1640  return;
1641 
1642  delete mLegend;
1643  mLegend = legend;
1644 
1645  if ( mLegend )
1646  connect( mLegend, SIGNAL( itemsChanged() ), this, SIGNAL( legendChanged() ) );
1647 
1648  emit legendChanged();
1649 }
1650 
1652 {
1653  return mLegend;
1654 }
1655 
1657 {
1658  return mStyleManager;
1659 }
1660 
1662 {
1663  emit repaintRequested();
1664 }
1665 
1667 {
1668  emit repaintRequested();
1669 }
1670 
1672 {
1673  return QString();
1674 }
1675 
1677 {
1678  mExtent = r;
1679 }
static const char * QGIS_VERSION
Definition: qgis.h:42
QString mShortName
Definition: qgsmaplayer.h:692
virtual QStringList subLayers() const
Returns the sublayers of this layer (Useful for providers that manage their own layers, such as WMS)
virtual bool isEditable() const
True if the layer can be edited.
QString fromAscii(const char *str, int size)
QString toString(Qt::DateFormat format) const
virtual QString saveNamedStyle(const QString &theURI, bool &theResultFlag)
Save the properties of this layer as a named style (either as a .qml file on disk or as a record in t...
QString database() const
Returns the database.
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
A rectangle specified with double values.
Definition: qgsrectangle.h:35
Base class for all map layer types.
Definition: qgsmaplayer.h:49
QString cap(int nth) const
QString writePath(const QString &filename, const QString &relativeBasePath=QString::null) const
Prepare a filename to save it to the project file.
QString toUpper() const
QDomProcessingInstruction createProcessingInstruction(const QString &target, const QString &data)
QgsMapLayer::LayerType type() const
Get the type of the layer.
Definition: qgsmaplayer.cpp:99
QString path() const
virtual QString metadata()
Obtain Metadata for this layer.
static QDomDocument asLayerDefinition(const QList< QgsMapLayer * > &layers, const QString &relativeBasePath=QString::null)
Returns the given layer as a layer definition document Layer definitions store the data source as wel...
virtual void drawLabels(QgsRenderContext &rendererContext)
Draw labels.
virtual QString loadSldStyle(const QString &theURI, bool &theResultFlag)
QString mAttributionUrl
Definition: qgsmaplayer.h:705
static QgsAuthManager * instance()
Enforce singleton pattern.
QString mKeywordList
Definition: qgsmaplayer.h:697
void readCustomProperties(const QDomNode &layerNode, const QString &keyStartsWith="")
Read custom properties from project file.
static QString qgisSettingsDirPath()
Returns the path to the settings directory in user&#39;s home dir.
QString name() const
Get the display name of the layer.
QString publicSource() const
Gets a version of the internal layer definition that has sensitive bits removed (for example...
QDomNode appendChild(const QDomNode &newChild)
void readXml(const QDomNode &parentNode, const QString &keyStartsWith=QString())
Read store contents from XML.
void reset()
Reset the style manager to a basic state - with one default style which is set as current...
virtual ~QgsMapLayer()
Destructor.
Definition: qgsmaplayer.cpp:92
QString mDataUrlFormat
Definition: qgsmaplayer.h:701
QString attribute(const QString &name, const QString &defValue) const
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
void validate()
Perform some validation on this CRS.
QString toString(int indent) const
static QString removePassword(const QString &aUri)
Removes password element from uris.
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
QgsMapLayerStyleManager * styleManager() const
Get access to the layer&#39;s style manager.
virtual Q_DECL_DEPRECATED QString lastError()
QString password() const
Returns the password.
QList< QPair< QString, QString > > queryItems() const
void setFile(const QString &file)
bool updateRevision(const QgsProjectVersion &version)
virtual bool readSymbology(const QDomNode &node, QString &errorMessage)=0
Read the symbology for the current layer from the Dom node supplied.
void layerNameChanged()
Emit a signal that the layer name has been changed.
void setDatabase(const QString &database)
Set database.
QString mLegendUrlFormat
Definition: qgsmaplayer.h:714
const T & at(int i) const
void setCustomProperty(const QString &key, const QVariant &value)
Set a custom property for layer.
float minimumScale() const
Returns the minimum scale denominator at which the layer is visible.
QString source() const
Returns the source for the layer.
QString join(const QString &separator) const
QString dataUrl() const
Get the DataUrl of the layer used by QGIS Server in GetCapabilities request DataUrl is a a link to th...
Definition: qgsmaplayer.h:151
int length() const
bool exists() const
QString & remove(int position, int n)
static CUSTOM_CRS_VALIDATION customSrsValidation()
Gets custom function.
QDomElement createElementNS(const QString &nsURI, const QString &qName)
virtual QString saveDefaultStyle(bool &theResultFlag)
Save the properties of this layer as the default style (either as a .qml file on disk or as a record ...
void chop(int n)
void setBlendMode(QPainter::CompositionMode blendMode)
Set the blending mode used for rendering a layer.
bool writeLayerXML(QDomElement &layerElement, QDomDocument &document, const QString &relativeBasePath=QString::null)
Stores state in Dom node.
QgsMapLayer(QgsMapLayer::LayerType type=VectorLayer, const QString &lyrname=QString::null, const QString &source=QString::null)
Constructor.
Definition: qgsmaplayer.cpp:53
QChar separator()
QString tr(const char *sourceText, const char *disambiguation, int n)
QString readPath(QString filename) const
Turn filename read from the project file to an absolute path.
void remove(const QString &key)
Remove a key (entry) from the store.
QString mLayerName
Name of the layer - used for display.
Definition: qgsmaplayer.h:686
virtual bool writeSymbology(QDomNode &node, QDomDocument &doc, QString &errorMessage) const =0
Write the symbology for the layer into the docment provided.
int size() const
bool isNull() const
static void setCustomSrsValidation(CUSTOM_CRS_VALIDATION f)
Sets custom function to force valid CRS QGIS uses implementation in QgisGui::customSrsValidation.
virtual bool draw(QgsRenderContext &rendererContext)
This is the method that does the actual work of drawing the layer onto a paint device.
static QString capitaliseLayerName(const QString &name)
A convenience function to (un)capitalise the layer name.
QDomElement toElement() const
void setPath(const QString &path)
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
Return value for the given key. If the key is not stored, default value will be used.
int indexIn(const QString &str, int offset, CaretMode caretMode) const
QgsRectangle mExtent
Extent of the layer.
Definition: qgsmaplayer.h:677
QString mMetadataUrl
MetadataUrl of the layer.
Definition: qgsmaplayer.h:708
void setValidationHint(const QString &html)
Set user hint for validation.
QString number(int n, int base)
QPainter::CompositionMode blendMode() const
Returns the current blending mode for a layer.
QString fromUtf8(const char *str, int size)
void setValue(const QString &key, const QVariant &value)
Add an entry to the store. If the entry with the keys exists already, it will be overwritten.
bool hasScaleBasedVisibility() const
Returns whether scale based visibility is enabled for the layer.
bool writeSld(QDomNode &node, QDomDocument &doc, QString &errorMessage) const
void layerCrsChanged()
Emit a signal that layer&#39;s CRS has been reset.
void readStyleManager(const QDomNode &layerNode)
Read style manager&#39;s configuration (if any).
QString text() const
virtual bool loadNamedStyleFromDb(const QString &db, const QString &theURI, QString &qml)
virtual bool writeXml(QDomNode &layer_node, QDomDocument &document)
Called by writeLayerXML(), used by children to write state specific to them to project files...
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:34
Q_DECL_DEPRECATED void setCacheImage(QImage *)
LayerType
Layers enum defining the types of layers that can be added to a map.
Definition: qgsmaplayer.h:55
bool readXML(const QDomNode &theNode)
Restores state from the given Dom node.
void setParam(const QString &key, const QString &value)
Set generic param (generic mode)
void setCrs(const QgsCoordinateReferenceSystem &srs, bool emitSignal=true)
Sets layer&#39;s spatial reference system.
void setAttribute(const QString &name, const QString &value)
void writeStyleManager(QDomNode &layerNode, QDomDocument &doc) const
Write style manager&#39;s configuration (if exists).
A class to describe the version of a project.
float maximumScale() const
Returns the maximum scale denominator at which the layer is visible.
static QList< QgsMapLayer * > fromLayerDefinition(QDomDocument &document, bool addToRegistry=false, bool addToLegend=false)
Creates a new layer from a layer defininition document.
int toInt(bool *ok, int base) const
virtual void setExtent(const QgsRectangle &rect)
Set the extent.
void readXml(const QDomElement &mgrElement)
Read configuration (for project loading)
bool isEmpty() const
QDomNodeList elementsByTagName(const QString &tagname) const
QString absoluteFilePath() const
bool isEmpty() const
QString mDataUrl
DataUrl of the layer.
Definition: qgsmaplayer.h:700
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
void setScheme(const QString &scheme)
void triggerRepaint()
Will advice the map canvas (and any other interested party) that this layer requires to be repainted...
virtual QString dataSourceUri(bool expandAuthConfig=false) const
Get the data source specification.
The QgsMapLayerLegend class is abstract interface for implementations of legends for one map layer...
QString path() const
void setConnection(const QString &aHost, const QString &aPort, const QString &aDatabase, const QString &aUsername, const QString &aPassword, SSLmode sslmode=SSLprefer, const QString &authConfigId=QString())
Set all connection related members at once.
QDir absoluteDir() const
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
bool setCurrent(const QString &path)
QString id() const
Get this layer&#39;s unique ID, this ID is used to access this layer from map layer registry.
QString attributionUrl() const
Get the attribution URL of the layer used by QGIS Server in GetCapabilities request Attribution indic...
Definition: qgsmaplayer.h:189
void writeCustomProperties(QDomNode &layerNode, QDomDocument &doc) const
Write custom properties to project file.
QString dataUrlFormat() const
Get the DataUrl format of the layer used by QGIS Server in GetCapabilities request DataUrl is a a lin...
Definition: qgsmaplayer.h:163
virtual Q_DECL_DEPRECATED QString lastErrorTitle()
QGis::GeometryType geometryType() const
Returns point, line or polygon.
void removeCustomProperty(const QString &key)
Remove a custom property from layer.
virtual bool importNamedStyle(QDomDocument &doc, QString &errorMsg)
Import the properties of this layer from a QDomDocument.
QVariant customProperty(const QString &value, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer.
virtual QString saveSldStyle(const QString &theURI, bool &theResultFlag)
bool isValid()
Return the status of the layer.
bool mValid
Indicates if the layer is valid and can be drawn.
Definition: qgsmaplayer.h:680
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
static QList< QgsMapLayer * > fromLayerDefinitionFile(const QString &qlrfile)
QString host() const
Returns the host.
QString mTitle
Definition: qgsmaplayer.h:693
virtual bool readSld(const QDomNode &node, QString &errorMessage)
Definition: qgsmaplayer.h:460
QString toLocalFile() const
QString mMetadataUrlFormat
Definition: qgsmaplayer.h:710
QDomText createTextNode(const QString &value)
virtual void connectNotify(const char *signal)
virtual void setSubLayerVisibility(const QString &name, bool vis)
Set the visibility of the given sublayer name.
bool exists() const
Class for storing the component parts of a PostgreSQL/RDBMS datasource URI.
static QString pkgDataPath()
Returns the common root path of all application data directories.
QDomNode namedItem(const QString &name) const
struct sqlite3 sqlite3
bool contains(QChar ch, Qt::CaseSensitivity cs) const
void setMaximumScale(const float theMaxScale)
Sets the maximum scale denominator at which the layer will be visible.
QString mAttribution
Attribution of the layer.
Definition: qgsmaplayer.h:704
QString mAbstract
Description of the layer.
Definition: qgsmaplayer.h:696
virtual void close()
void blendModeChanged(QPainter::CompositionMode blendMode)
Signal emitted when the blend mode is changed, through QgsMapLayer::setBlendMode() ...
QString metadataUrl() const
Get the metadata URL of the layer used by QGIS Server in GetCapabilities request MetadataUrl is a a l...
Definition: qgsmaplayer.h:203
QgsMapLayerLegend * legend() const
Can be null.
bool isNull() const
void setMinimumScale(const float theMinScale)
Sets the minimum scale denominator at which the layer will be visible.
virtual void exportNamedStyle(QDomDocument &doc, QString &errorMsg)
Export the properties of this layer as named style in a QDomDocument.
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
QString providerType() const
Return the provider type for this layer.
QString & replace(int position, int n, QChar after)
QVariant value(const QString &key, const QVariant &defaultValue) const
bool writeXML(QDomNode &theNode, QDomDocument &theDoc) const
Stores state to the given Dom node in the given document.
Contains information about the context of a rendering operation.
QString legendUrlFormat() const
Definition: qgsmaplayer.h:487
QDateTime currentDateTime()
void save(QTextStream &str, int indent) const
QDomNode firstChild() const
static QgsMapLayerRegistry * instance()
Returns the instance pointer, creating the object on the first call.
QString mid(int position, int n) const
Q_DECL_DEPRECATED void clearCacheImage()
Clear cached image.
QString mDataSource
Data source description string, varies by layer type.
Definition: qgsmaplayer.h:683
virtual QString loadDefaultStyle(bool &theResultFlag)
Retrieve the default style for this layer if one exists (either as a .qml file on disk or as a record...
void writeXml(QDomNode &parentNode, QDomDocument &doc) const
Write store contents to XML.
T takeFirst()
QString legendUrl() const
Definition: qgsmaplayer.h:485
bool setMasterPassword(bool verify=false)
Main call to initially set or continually check master password is set.
float toFloat(bool *ok) const
virtual void invalidTransformInput()
Event handler for when a coordinate transform fails due to bad vertex error.
void repaintRequested()
By emitting this signal the layer tells that either appearance or content have been changed and any v...
static QgsProject * instance()
access to canonical QgsProject instance
Definition: qgsproject.cpp:381
QDomElement firstChildElement(const QString &tagName) const
virtual bool readXml(const QDomNode &layer_node)
Called by readLayerXML(), used by children to read state specific to them from project files...
QString mLegendUrl
WMS legend.
Definition: qgsmaplayer.h:713
Class for storing a coordinate reference system (CRS)
QString originalName() const
Get the original name of the layer.
Definition: qgsmaplayer.h:93
QString absoluteFilePath(const QString &fileName) const
void legendChanged()
Signal emitted when legend of the layer has changed.
QString metadataUrlType() const
Get the metadata type of the layer used by QGIS Server in GetCapabilities request MetadataUrlType ind...
Definition: qgsmaplayer.h:215
int length() const
bool toBool() const
char * data()
QString left(int n) const
void setQueryItems(const QList< QPair< QString, QString > > &query)
QString completeBaseName() const
bool readLayerXML(const QDomElement &layerElement)
Sets state from Dom document.
void setLayerName(const QString &name)
Set the display name of the layer.
const QgsCoordinateReferenceSystem & crs() const
Returns layer&#39;s spatial reference system.
virtual QString loadNamedStyle(const QString &theURI, bool &theResultFlag)
Retrieve a named style for this layer if one exists (either as a .qml file on disk or as a record in ...
QByteArray encodedUri() const
return complete encoded uri (generic mode)
static QgsPluginLayerRegistry * instance()
Means of accessing canonical single instance.
QString providerType() const
[ data provider interface ] Which provider is being used for this Raster Layer?
QUndoStack * undoStack()
Return pointer to layer&#39;s undo stack.
virtual QDateTime timestamp() const
Time stamp of data source in the moment when data/metadata were loaded by provider.
Definition: qgsmaplayer.h:593
int size() const
void(* CUSTOM_CRS_VALIDATION)(QgsCoordinateReferenceSystem &)
QgsVectorDataProvider * dataProvider()
Returns the data provider.
QgsMapLayer * addMapLayer(QgsMapLayer *theMapLayer, bool addToLegend=true, bool takeOwnership=true)
Add a layer to the map of loaded layers.
QDomElement createElement(const QString &tagName)
QString keywordList() const
Get the keyword list of the layer used by QGIS Server in GetCapabilities request. ...
Definition: qgsmaplayer.h:137
void setLegend(QgsMapLayerLegend *legend)
Assign a legend controller to the map layer.
QString uri(bool expandAuthConfig=true) const
return complete uri
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
virtual QString styleURI()
Retrieve the style URI for this layer (either as a .qml file on disk or as a record in the users styl...
Management of styles for use with one map layer.
QDomDocumentType createDocumentType(const QString &qName, const QString &publicId, const QString &systemId)
virtual QgsRectangle extent()
Return the extent of the layer.
Represents a vector layer which manages a vector based data sets.
QString attribution() const
Get the attribution of the layer used by QGIS Server in GetCapabilities request Attribution indicates...
Definition: qgsmaplayer.h:177
virtual void setLayerOrder(const QStringList &layers)
Reorders the previously selected sublayers of this layer from bottom to top (Useful for providers tha...
bool exactMatch(const QString &str) const
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QString mLayerOrigName
Original name of the layer.
Definition: qgsmaplayer.h:690
void setValid(bool valid)
Set whether layer is valid or not - should be used in constructor.
QString baseName() const
QString username() const
Returns the username.
QString mMetadataUrlType
Definition: qgsmaplayer.h:709
QgsPluginLayer * createLayer(const QString &typeName, const QString &uri=QString())
Return new layer if corresponding plugin has been found, else return NULL.
QString metadataUrlFormat() const
Get the metadata format of the layer used by QGIS Server in GetCapabilities request MetadataUrlType i...
Definition: qgsmaplayer.h:227
virtual void exportSldStyle(QDomDocument &doc, QString &errorMsg)
Export the properties of this layer as SLD style in a QDomDocument.
QUrl fromEncoded(const QByteArray &input)
QByteArray toEncoded(QFlags< QUrl::FormattingOption > options) const
QString port() const
Returns the port.
QByteArray toAscii() const
QUrl fromLocalFile(const QString &localFile)
void setScaleBasedVisibility(const bool enabled)
Sets whether scale based visibility is enabled for the layer.
Q_DECL_DEPRECATED void toggleScaleBasedVisibility(bool theVisibilityFlag)
Accessor for the scale based visilibility flag.
bool setContent(const QByteArray &data, bool namespaceProcessing, QString *errorMsg, int *errorLine, int *errorColumn)
QDomNode at(int index) const
void writeXml(QDomElement &mgrElement) const
Write configuration (for project saving)
QByteArray toUtf8() const