21 #include <QDomDocument> 22 #include <QDomElement> 23 #include <QDomImplementation> 28 #include <QTextStream> 55 , mDataSource( source )
56 , mLayerOrigName( lyrname )
59 , mBlendMode( QPainter::CompositionMode_SourceOver )
71 QDateTime dt = QDateTime::currentDateTime();
72 mID = lyrname + dt.toString(
"yyyyMMddhhmmsszzz" );
79 mID.replace( QRegExp(
"[\\W]" ),
"_" );
83 mMaxScale = 100000000;
84 mScaleBasedVisibility =
false;
108 QgsDebugMsg(
"new original name: '" + name +
"'" );
157 Q_UNUSED( rendererContext );
163 Q_UNUSED( rendererContext );
177 mnl = layerElement.namedItem(
"provider" );
178 mne = mnl.toElement();
179 provider = mne.text();
182 mnl = layerElement.namedItem(
"datasource" );
183 mne = mnl.toElement();
187 if ( provider ==
"spatialite" )
193 else if ( provider ==
"ogr" )
195 QStringList theURIParts =
mDataSource.split(
"|" );
199 else if ( provider ==
"gpx" )
201 QStringList theURIParts =
mDataSource.split(
"?" );
205 else if ( provider ==
"delimitedtext" )
207 QUrl urlSource = QUrl::fromEncoded(
mDataSource.toAscii() );
212 urlSource.setScheme(
"file" );
213 urlSource.setPath( file.path() );
216 QUrl urlDest = QUrl::fromLocalFile(
QgsProject::instance()->readPath( urlSource.toLocalFile() ) );
217 urlDest.setQueryItems( urlSource.queryItems() );
218 mDataSource = QString::fromAscii( urlDest.toEncoded() );
220 else if ( provider ==
"wms" )
235 QgsDebugMsg(
"Old WMS URI format detected -> converting to new format" );
240 QStringListIterator iter( parts );
241 while ( iter.hasNext() )
243 QString item = iter.next();
244 if ( item.startsWith(
"username=" ) )
246 uri.
setParam(
"username", item.mid( 9 ) );
248 else if ( item.startsWith(
"password=" ) )
250 uri.
setParam(
"password", item.mid( 9 ) );
252 else if ( item.startsWith(
"tiled=" ) )
258 QStringList params = item.mid( 6 ).split(
";" );
260 if ( params.size() == 2 )
262 uri.
setParam(
"maxWidth", params.takeFirst() );
263 uri.
setParam(
"maxHeight", params.takeFirst() );
265 else if ( params.size() > 2 )
269 uri.
setParam(
"tileMatrixSet",
"" );
272 else if ( item.startsWith(
"featureCount=" ) )
274 uri.
setParam(
"featureCount", item.mid( 13 ) );
276 else if ( item.startsWith(
"url=" ) )
278 uri.
setParam(
"url", item.mid( 4 ) );
280 else if ( item.startsWith(
"ignoreUrl=" ) )
282 uri.
setParam(
"ignoreUrl", item.mid( 10 ).split(
";" ) );
298 bool handled =
false;
300 if ( provider ==
"gdal" )
306 QRegExp r(
"NETCDF:(.+):([^:]+)" );
309 QString filename = r.cap( 1 );
310 if ( filename.startsWith(
'"' ) && filename.endsWith(
'"' ) )
311 filename = filename.mid( 1, filename.length() - 2 );
320 QRegExp r(
"HDF4_SDS:([^:]+):(.+):([^:]+)" );
323 QString filename = r.cap( 2 );
324 if ( filename.startsWith(
'"' ) && filename.endsWith(
'"' ) )
325 filename = filename.mid( 1, filename.length() - 2 );
334 QRegExp r(
"HDF5:(.+):([^:]+)" );
337 QString filename = r.cap( 1 );
338 if ( filename.startsWith(
'"' ) && filename.endsWith(
'"' ) )
339 filename = filename.mid( 1, filename.length() - 2 );
344 else if (
mDataSource.contains( QRegExp(
"^(NITF_IM|RADARSAT_2_CALIB):" ) ) )
348 QRegExp r(
"([^:]+):([^:]+):(.+)" );
365 mnl = layerElement.namedItem(
"layername" );
366 mne = mnl.toElement();
368 QDomNode srsNode = layerElement.namedItem(
"srs" );
380 layerError = !
readXml( layerElement );
399 mnl = layerElement.namedItem(
"id" );
400 if ( ! mnl.isNull() )
402 mne = mnl.toElement();
403 if ( ! mne.isNull() && mne.text().length() > 10 )
415 mnl = layerElement.namedItem(
"layername" );
416 mne = mnl.toElement();
420 QDomElement titleElem = layerElement.firstChildElement(
"title" );
421 if ( !titleElem.isNull() )
423 mTitle = titleElem.text();
427 QDomElement abstractElem = layerElement.firstChildElement(
"abstract" );
428 if ( !abstractElem.isNull() )
434 QDomElement keywordListElem = layerElement.firstChildElement(
"keywordList" );
435 if ( !keywordListElem.isNull() )
438 for ( QDomNode n = keywordListElem.firstChild(); !n.isNull(); n = n.nextSibling() )
440 kwdList << n.toElement().text();
446 QDomElement dataUrlElem = layerElement.firstChildElement(
"dataUrl" );
447 if ( !dataUrlElem.isNull() )
454 QDomElement legendUrlElem = layerElement.firstChildElement(
"legendUrl" );
455 if ( !legendUrlElem.isNull() )
462 QDomElement attribElem = layerElement.firstChildElement(
"attribution" );
463 if ( !attribElem.isNull() )
470 QDomElement metaUrlElem = layerElement.firstChildElement(
"metadataUrl" );
471 if ( !metaUrlElem.isNull() )
480 QDomNode transparencyNode = layer_node.namedItem(
"transparencyLevelInt" );
481 if ( ! transparencyNode.isNull() )
485 QDomElement myElement = transparencyNode.toElement();
486 setTransparency( myElement.text().toInt() );
498 Q_UNUSED( layer_node );
510 layerElement.setAttribute(
"minimumScale", QString::number(
minimumScale() ) );
511 layerElement.setAttribute(
"maximumScale", QString::number(
maximumScale() ) );
514 QDomElement layerId = document.createElement(
"id" );
515 QDomText layerIdText = document.createTextNode(
id() );
516 layerId.appendChild( layerIdText );
518 layerElement.appendChild( layerId );
521 QDomElement dataSource = document.createElement(
"datasource" );
536 QStringList theURIParts = src.split(
"|" );
538 src = theURIParts.join(
"|" );
542 QStringList theURIParts = src.split(
"?" );
544 src = theURIParts.join(
"?" );
546 else if ( vlayer && vlayer->
providerType() ==
"delimitedtext" )
548 QUrl urlSource = QUrl::fromEncoded( src.toAscii() );
549 QUrl urlDest = QUrl::fromLocalFile(
QgsProject::instance()->writePath( urlSource.toLocalFile(), relativeBasePath ) );
550 urlDest.setQueryItems( urlSource.queryItems() );
551 src = QString::fromAscii( urlDest.toEncoded() );
553 else if ( vlayer && vlayer->
providerType() ==
"memory" )
560 bool handled =
false;
568 if ( src.startsWith(
"NETCDF:" ) )
572 QRegExp r(
"NETCDF:(.+):([^:]+)" );
573 if ( r.exactMatch( src ) )
575 QString filename = r.cap( 1 );
576 if ( filename.startsWith(
'"' ) && filename.endsWith(
'"' ) )
577 filename = filename.mid( 1, filename.length() - 2 );
582 else if ( src.startsWith(
"HDF4_SDS:" ) )
586 QRegExp r(
"HDF4_SDS:([^:]+):(.+):([^:]+)" );
587 if ( r.exactMatch( src ) )
589 QString filename = r.cap( 2 );
590 if ( filename.startsWith(
'"' ) && filename.endsWith(
'"' ) )
591 filename = filename.mid( 1, filename.length() - 2 );
596 else if ( src.startsWith(
"HDF5:" ) )
600 QRegExp r(
"HDF5:(.+):([^:]+)" );
601 if ( r.exactMatch( src ) )
603 QString filename = r.cap( 1 );
604 if ( filename.startsWith(
'"' ) && filename.endsWith(
'"' ) )
605 filename = filename.mid( 1, filename.length() - 2 );
610 else if ( src.contains( QRegExp(
"^(NITF_IM|RADARSAT_2_CALIB):" ) ) )
614 QRegExp r(
"([^:]+):([^:]+):(.+)" );
615 if ( r.exactMatch( src ) )
628 QDomText dataSourceText = document.createTextNode( src );
629 dataSource.appendChild( dataSourceText );
631 layerElement.appendChild( dataSource );
635 QDomElement layerName = document.createElement(
"layername" );
636 QDomText layerNameText = document.createTextNode(
originalName() );
637 layerName.appendChild( layerNameText );
640 QDomElement layerTitle = document.createElement(
"title" );
641 QDomText layerTitleText = document.createTextNode(
title() );
642 layerTitle.appendChild( layerTitleText );
645 QDomElement layerAbstract = document.createElement(
"abstract" );
646 QDomText layerAbstractText = document.createTextNode(
abstract() );
647 layerAbstract.appendChild( layerAbstractText );
649 layerElement.appendChild( layerName );
650 layerElement.appendChild( layerTitle );
651 layerElement.appendChild( layerAbstract );
654 QStringList keywordStringList =
keywordList().split(
"," );
655 if ( keywordStringList.size() > 0 )
657 QDomElement layerKeywordList = document.createElement(
"keywordList" );
658 for (
int i = 0; i < keywordStringList.size(); ++i )
660 QDomElement layerKeywordValue = document.createElement(
"value" );
661 QDomText layerKeywordText = document.createTextNode( keywordStringList.at( i ).trimmed() );
662 layerKeywordValue.appendChild( layerKeywordText );
663 layerKeywordList.appendChild( layerKeywordValue );
665 layerElement.appendChild( layerKeywordList );
670 if ( !aDataUrl.isEmpty() )
672 QDomElement layerDataUrl = document.createElement(
"dataUrl" );
673 QDomText layerDataUrlText = document.createTextNode( aDataUrl );
674 layerDataUrl.appendChild( layerDataUrlText );
676 layerElement.appendChild( layerDataUrl );
681 if ( !aLegendUrl.isEmpty() )
683 QDomElement layerLegendUrl = document.createElement(
"legendUrl" );
684 QDomText layerLegendUrlText = document.createTextNode( aLegendUrl );
685 layerLegendUrl.appendChild( layerLegendUrlText );
687 layerElement.appendChild( layerLegendUrl );
692 if ( !aAttribution.isEmpty() )
694 QDomElement layerAttribution = document.createElement(
"attribution" );
695 QDomText layerAttributionText = document.createTextNode( aAttribution );
696 layerAttribution.appendChild( layerAttributionText );
698 layerElement.appendChild( layerAttribution );
703 if ( !aMetadataUrl.isEmpty() )
705 QDomElement layerMetadataUrl = document.createElement(
"metadataUrl" );
706 QDomText layerMetadataUrlText = document.createTextNode( aMetadataUrl );
707 layerMetadataUrl.appendChild( layerMetadataUrlText );
710 layerElement.appendChild( layerMetadataUrl );
716 QDomElement stamp = document.createElement(
"timestamp" );
717 QDomText stampText = document.createTextNode(
timestamp().toString( Qt::ISODate ) );
718 stamp.appendChild( stampText );
719 layerElement.appendChild( stamp );
722 layerElement.appendChild( layerName );
729 QDomElement mySrsElement = document.createElement(
"srs" );
730 mCRS->
writeXML( mySrsElement, document );
731 layerElement.appendChild( mySrsElement );
735 QDomElement transparencyLevelIntElement = document.createElement(
"transparencyLevelInt" );
736 QDomText transparencyLevelIntText = document.createTextNode( QString::number( getTransparency() ) );
737 transparencyLevelIntElement.appendChild( transparencyLevelIntText );
738 maplayer.appendChild( transparencyLevelIntElement );
745 return writeXml( layerElement, document );
751 QDomDocument doc(
"qgis-layer-definition" );
752 QDomElement qgiselm = doc.createElement(
"qlr" );
753 doc.appendChild( qgiselm );
754 QDomElement layerselm = doc.createElement(
"maplayers" );
757 QDomElement layerelm = doc.createElement(
"maplayer" );
759 layerselm.appendChild( layerelm );
761 qgiselm.appendChild( layerselm );
767 QList<QgsMapLayer*> layers;
768 QDomNodeList layernodes = document.elementsByTagName(
"maplayer" );
769 for (
int i = 0; i < layernodes.size(); ++i )
771 QDomNode layernode = layernodes.at( i );
772 QDomElement layerElem = layernode.toElement();
774 QString
type = layerElem.attribute(
"type" );
778 if ( type ==
"vector" )
782 else if ( type ==
"raster" )
786 else if ( type ==
"plugin" )
788 QString typeName = layerElem.attribute(
"name" );
804 QFile file( qlrfile );
805 if ( !file.open( QIODevice::ReadOnly ) )
808 return QList<QgsMapLayer*>();
812 if ( !doc.setContent( &file ) )
815 return QList<QgsMapLayer*>();
818 QFileInfo fileinfo( file );
819 QDir::setCurrent( fileinfo.absoluteDir().path() );
826 Q_UNUSED( layer_node );
827 Q_UNUSED( document );
836 mCustomProperties.
readXml( layerNode, keyStartsWith );
841 mCustomProperties.
writeXml( layerNode, doc );
846 QDomElement styleMgrElem = layerNode.firstChildElement(
"map-layer-style-manager" );
847 if ( !styleMgrElem.isNull() )
848 mStyleManager->
readXml( styleMgrElem );
850 mStyleManager->
reset();
857 QDomElement styleMgrElem = doc.createElement(
"map-layer-style-manager" );
858 mStyleManager->
writeXml( styleMgrElem );
859 layerNode.appendChild( styleMgrElem );
890 void QgsMapLayer::connectNotify(
const char * signal )
905 return mScaleBasedVisibility;
910 mMinScale = theMinScale;
921 mMaxScale = theMaxScale;
926 mScaleBasedVisibility = enabled;
936 return QStringList();
976 settings.value(
"/qgis/capitaliseLayerName", QVariant(
false ) ).toBool();
978 QString layerName( name );
980 if ( capitaliseLayerName )
981 layerName = layerName.left( 1 ).toUpper() + layerName.mid( 1 );
991 if ( myURI.startsWith(
"/vsigzip/", Qt::CaseInsensitive ) )
993 myURI.remove( 0, 9 );
995 else if ( myURI.startsWith(
"/vsizip/", Qt::CaseInsensitive ) &&
996 myURI.endsWith(
".zip", Qt::CaseInsensitive ) )
999 myURI.remove( 0, 8 );
1001 else if ( myURI.startsWith(
"/vsitar/", Qt::CaseInsensitive ) &&
1002 ( myURI.endsWith(
".tar", Qt::CaseInsensitive ) ||
1003 myURI.endsWith(
".tar.gz", Qt::CaseInsensitive ) ||
1004 myURI.endsWith(
".tgz", Qt::CaseInsensitive ) ) )
1007 myURI.remove( 0, 8 );
1010 QFileInfo myFileInfo( myURI );
1013 if ( myFileInfo.exists() )
1016 if ( myURI.endsWith(
".gz", Qt::CaseInsensitive ) )
1018 else if ( myURI.endsWith(
".zip", Qt::CaseInsensitive ) )
1020 else if ( myURI.endsWith(
".tar", Qt::CaseInsensitive ) )
1022 else if ( myURI.endsWith(
".tar.gz", Qt::CaseInsensitive ) )
1024 else if ( myURI.endsWith(
".tgz", Qt::CaseInsensitive ) )
1026 myFileInfo.setFile( myURI );
1028 key = myFileInfo.path() + QDir::separator() + myFileInfo.completeBaseName() +
".qml";
1045 QgsDebugMsg( QString(
"db = %1 uri = %2" ).arg( db ).arg( theURI ) );
1047 bool theResultFlag =
false;
1051 sqlite3_stmt *myPreparedStatement;
1055 QgsDebugMsg( QString(
"Trying to load style for \"%1\" from \"%2\"" ).arg( theURI ).arg( db ) );
1057 if ( db.isEmpty() || !QFile( db ).exists() )
1060 myResult = sqlite3_open_v2( db.toUtf8().data(), &myDatabase, SQLITE_OPEN_READONLY, NULL );
1061 if ( myResult != SQLITE_OK )
1066 QString mySql =
"select qml from tbl_styles where style=?";
1067 myResult = sqlite3_prepare( myDatabase, mySql.toUtf8().data(), mySql.toUtf8().length(), &myPreparedStatement, &myTail );
1068 if ( myResult == SQLITE_OK )
1070 QByteArray param = theURI.toUtf8();
1072 if ( sqlite3_bind_text( myPreparedStatement, 1, param.data(), param.length(), SQLITE_STATIC ) == SQLITE_OK &&
1073 sqlite3_step( myPreparedStatement ) == SQLITE_ROW )
1075 qml = QString::fromUtf8((
char * )sqlite3_column_text( myPreparedStatement, 0 ) );
1076 theResultFlag =
true;
1079 sqlite3_finalize( myPreparedStatement );
1082 sqlite3_close( myDatabase );
1084 return theResultFlag;
1092 theResultFlag =
false;
1094 QDomDocument myDocument(
"qgis" );
1098 QString myErrorMessage;
1100 QFile myFile( theURI );
1101 if ( myFile.open( QFile::ReadOnly ) )
1104 theResultFlag = myDocument.setContent( &myFile, &myErrorMessage, &line, &column );
1105 if ( !theResultFlag )
1106 myErrorMessage =
tr(
"%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column );
1112 QgsDebugMsg( QString(
"project fileName: %1" ).arg( project.absoluteFilePath() ) );
1116 ( project.exists() &&
loadNamedStyleFromDb( project.absoluteDir().absoluteFilePath( project.baseName() +
".qmldb" ), theURI, qml ) ) ||
1119 theResultFlag = myDocument.setContent( qml, &myErrorMessage, &line, &column );
1120 if ( !theResultFlag )
1122 myErrorMessage =
tr(
"%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column );
1127 myErrorMessage =
tr(
"Style not found in database" );
1131 if ( !theResultFlag )
1133 return myErrorMessage;
1137 if ( !theResultFlag )
1138 myErrorMessage =
tr(
"Loading style file %1 failed because:\n%2" ).arg( theURI ).arg( myErrorMessage );
1140 return myErrorMessage;
1147 QgsProjectVersion fileVersion( myDocument.firstChildElement(
"qgis" ).attribute(
"version" ) );
1150 if ( thisVersion > fileVersion )
1153 "version of qgis (saved in " + fileVersion.text() +
1155 "). Problems may occur." );
1159 styleFile.updateRevision( thisVersion );
1165 QDomElement myRoot = myDocument.firstChildElement(
"qgis" );
1166 if ( myRoot.isNull() )
1168 myErrorMessage =
tr(
"Root <qgis> element could not be found" );
1179 QDomNode transparencyNode = myRoot.namedItem(
"transparencyLevelInt" );
1180 if ( ! transparencyNode.isNull() )
1184 QDomElement myElement = transparencyNode.toElement();
1185 setTransparency( myElement.text().toInt() );
1194 QDomImplementation DomImplementation;
1195 QDomDocumentType documentType = DomImplementation.createDocumentType(
"qgis",
"http://mrcc.com/qgis.dtd",
"SYSTEM" );
1196 QDomDocument myDocument( documentType );
1198 QDomElement myRootNode = myDocument.createElement(
"qgis" );
1200 myDocument.appendChild( myRootNode );
1203 myRootNode.setAttribute(
"minimumScale", QString::number(
minimumScale() ) );
1204 myRootNode.setAttribute(
"maximumScale", QString::number(
maximumScale() ) );
1208 QDomElement transparencyLevelIntElement = myDocument.createElement(
"transparencyLevelInt" );
1209 QDomText transparencyLevelIntText = myDocument.createTextNode( QString::number( getTransparency() ) );
1210 transparencyLevelIntElement.appendChild( transparencyLevelIntText );
1211 myRootNode.appendChild( transparencyLevelIntElement );
1216 errorMsg =
QObject::tr(
"Could not save symbology because:\n%1" ).arg( errorMsg );
1229 QString myErrorMessage;
1230 QDomDocument myDocument;
1241 QStringList theURIParts = theURI.split(
"|" );
1242 filename = theURIParts[0];
1246 QStringList theURIParts = theURI.split(
"?" );
1247 filename = theURIParts[0];
1249 else if ( vlayer && vlayer->
providerType() ==
"delimitedtext" )
1251 filename = QUrl::fromEncoded( theURI.toAscii() ).toLocalFile();
1258 QFileInfo myFileInfo( filename );
1259 if ( myFileInfo.exists() || filename.endsWith(
".qml", Qt::CaseInsensitive ) )
1261 QFileInfo myDirInfo( myFileInfo.path() );
1262 if ( !myDirInfo.isWritable() )
1264 return tr(
"The directory containing your dataset needs to be writable!" );
1268 QString myFileName = myFileInfo.path() + QDir::separator() + myFileInfo.completeBaseName() +
".qml";
1270 QFile myFile( myFileName );
1271 if ( myFile.open( QFile::WriteOnly | QFile::Truncate ) )
1273 QTextStream myFileStream( &myFile );
1275 myDocument.save( myFileStream, 2 );
1277 theResultFlag =
true;
1278 return tr(
"Created default style file as %1" ).arg( myFileName );
1282 theResultFlag =
false;
1283 return tr(
"ERROR: Failed to created default style file as %1. Check file permissions and retry." ).arg( myFileName );
1288 QString qml = myDocument.toString();
1292 sqlite3_stmt *myPreparedStatement;
1297 if ( myResult != SQLITE_OK )
1299 return tr(
"User database could not be opened." );
1302 QByteArray param0 = theURI.toUtf8();
1303 QByteArray param1 = qml.toUtf8();
1305 QString mySql =
"create table if not exists tbl_styles(style varchar primary key,qml varchar)";
1306 myResult = sqlite3_prepare( myDatabase, mySql.toUtf8().data(), mySql.toUtf8().length(), &myPreparedStatement, &myTail );
1307 if ( myResult == SQLITE_OK )
1309 if ( sqlite3_step( myPreparedStatement ) != SQLITE_DONE )
1311 sqlite3_finalize( myPreparedStatement );
1312 sqlite3_close( myDatabase );
1313 theResultFlag =
false;
1314 return tr(
"The style table could not be created." );
1318 sqlite3_finalize( myPreparedStatement );
1320 mySql =
"insert into tbl_styles(style,qml) values (?,?)";
1321 myResult = sqlite3_prepare( myDatabase, mySql.toUtf8().data(), mySql.toUtf8().length(), &myPreparedStatement, &myTail );
1322 if ( myResult == SQLITE_OK )
1324 if ( sqlite3_bind_text( myPreparedStatement, 1, param0.data(), param0.length(), SQLITE_STATIC ) == SQLITE_OK &&
1325 sqlite3_bind_text( myPreparedStatement, 2, param1.data(), param1.length(), SQLITE_STATIC ) == SQLITE_OK &&
1326 sqlite3_step( myPreparedStatement ) == SQLITE_DONE )
1328 theResultFlag =
true;
1329 myErrorMessage =
tr(
"The style %1 was saved to database" ).arg( theURI );
1333 sqlite3_finalize( myPreparedStatement );
1335 if ( !theResultFlag )
1337 QString mySql =
"update tbl_styles set qml=? where style=?";
1338 myResult = sqlite3_prepare( myDatabase, mySql.toUtf8().data(), mySql.toUtf8().length(), &myPreparedStatement, &myTail );
1339 if ( myResult == SQLITE_OK )
1341 if ( sqlite3_bind_text( myPreparedStatement, 2, param0.data(), param0.length(), SQLITE_STATIC ) == SQLITE_OK &&
1342 sqlite3_bind_text( myPreparedStatement, 1, param1.data(), param1.length(), SQLITE_STATIC ) == SQLITE_OK &&
1343 sqlite3_step( myPreparedStatement ) == SQLITE_DONE )
1345 theResultFlag =
true;
1346 myErrorMessage =
tr(
"The style %1 was updated in the database." ).arg( theURI );
1350 theResultFlag =
false;
1351 myErrorMessage =
tr(
"The style %1 could not be updated in the database." ).arg( theURI );
1356 theResultFlag =
false;
1357 myErrorMessage =
tr(
"The style %1 could not be inserted into database." ).arg( theURI );
1360 sqlite3_finalize( myPreparedStatement );
1363 sqlite3_close( myDatabase );
1366 return myErrorMessage;
1371 QDomDocument myDocument = QDomDocument();
1373 QDomNode header = myDocument.createProcessingInstruction(
"xml",
"version=\"1.0\" encoding=\"UTF-8\"" );
1374 myDocument.appendChild( header );
1377 QDomElement root = myDocument.createElementNS(
"http://www.opengis.net/sld",
"StyledLayerDescriptor" );
1378 root.setAttribute(
"version",
"1.1.0" );
1379 root.setAttribute(
"units",
"mm" );
1380 root.setAttribute(
"xsi:schemaLocation",
"http://www.opengis.net/sld http://schemas.opengis.net/sld/1.1.0/StyledLayerDescriptor.xsd" );
1381 root.setAttribute(
"xmlns:ogc",
"http://www.opengis.net/ogc" );
1382 root.setAttribute(
"xmlns:se",
"http://www.opengis.net/se" );
1383 root.setAttribute(
"xmlns:xlink",
"http://www.w3.org/1999/xlink" );
1384 root.setAttribute(
"xmlns:xsi",
"http://www.w3.org/2001/XMLSchema-instance" );
1385 myDocument.appendChild( root );
1388 QDomElement namedLayerNode = myDocument.createElement(
"NamedLayer" );
1389 root.appendChild( namedLayerNode );
1394 errorMsg =
tr(
"Could not save symbology because:\n%1" )
1395 .arg(
"Non-vector layers not supported yet" );
1399 if ( !vlayer->
writeSld( namedLayerNode, myDocument, errorMsg ) )
1401 errorMsg =
tr(
"Could not save symbology because:\n%1" ).arg( errorMsg );
1411 QDomDocument myDocument;
1413 if ( !errorMsg.isNull() )
1415 theResultFlag =
false;
1425 QStringList theURIParts = theURI.split(
"|" );
1426 filename = theURIParts[0];
1430 QStringList theURIParts = theURI.split(
"?" );
1431 filename = theURIParts[0];
1435 filename = QUrl::fromEncoded( theURI.toAscii() ).toLocalFile();
1442 QFileInfo myFileInfo( filename );
1443 if ( myFileInfo.exists() || filename.endsWith(
".sld", Qt::CaseInsensitive ) )
1445 QFileInfo myDirInfo( myFileInfo.path() );
1446 if ( !myDirInfo.isWritable() )
1448 return tr(
"The directory containing your dataset needs to be writable!" );
1452 QString myFileName = myFileInfo.path() + QDir::separator() + myFileInfo.completeBaseName() +
".sld";
1454 QFile myFile( myFileName );
1455 if ( myFile.open( QFile::WriteOnly | QFile::Truncate ) )
1457 QTextStream myFileStream( &myFile );
1459 myDocument.save( myFileStream, 2 );
1461 theResultFlag =
true;
1462 return tr(
"Created default style file as %1" ).arg( myFileName );
1466 theResultFlag =
false;
1467 return tr(
"ERROR: Failed to created SLD style file as %1. Check file permissions and retry." ).arg( filename );
1474 theResultFlag =
false;
1476 QDomDocument myDocument;
1480 QString myErrorMessage;
1482 QFile myFile( theURI );
1483 if ( myFile.open( QFile::ReadOnly ) )
1486 theResultFlag = myDocument.setContent( &myFile,
true, &myErrorMessage, &line, &column );
1487 if ( !theResultFlag )
1488 myErrorMessage =
tr(
"%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column );
1493 myErrorMessage =
tr(
"Unable to open file %1" ).arg( theURI );
1496 if ( !theResultFlag )
1498 return myErrorMessage;
1502 QDomElement myRoot = myDocument.firstChildElement(
"StyledLayerDescriptor" );
1503 if ( myRoot.isNull() )
1505 myErrorMessage = QString(
"Error: StyledLayerDescriptor element not found in %1" ).arg( theURI );
1506 theResultFlag =
false;
1507 return myErrorMessage;
1512 QDomElement namedLayerElem = myRoot.firstChildElement(
"NamedLayer" );
1513 if ( namedLayerElem.isNull() )
1515 myErrorMessage = QString(
"Info: NamedLayer element not found." );
1516 theResultFlag =
false;
1517 return myErrorMessage;
1521 theResultFlag =
readSld( namedLayerElem, errorMsg );
1522 if ( !theResultFlag )
1524 myErrorMessage =
tr(
"Loading style file %1 failed because:\n%2" ).arg( theURI ).arg( errorMsg );
1525 return myErrorMessage;
1540 mCustomProperties.
setValue( key, value );
1545 return mCustomProperties.
value( value, defaultValue );
1550 mCustomProperties.
remove( key );
1572 if ( legend == mLegend )
1579 connect( mLegend, SIGNAL( itemsChanged() ),
this, SIGNAL(
legendChanged() ) );
1591 return mStyleManager;
static const char * QGIS_VERSION
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.
static const QString pkgDataPath()
Returns the common root path of all application data directories.
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...
A rectangle specified with double values.
Base class for all map layer types.
QString writePath(QString filename, QString relativeBasePath=QString::null) const
prepare a filename to save it to the project file
QgsPluginLayer * createLayer(QString typeName)
return new layer if corresponding plugin has been found, else return NULL
QgsMapLayer::LayerType type() const
Get the type of the layer.
virtual QString metadata()
Obtain Metadata for this layer.
void setValidationHint(QString html)
virtual void drawLabels(QgsRenderContext &rendererContext)
Draw labels.
virtual QString loadSldStyle(const QString &theURI, bool &theResultFlag)
void readCustomProperties(const QDomNode &layerNode, const QString &keyStartsWith="")
Read custom properties from project file.
QString publicSource() const
void readXml(const QDomNode &parentNode, const QString &keyStartsWith=QString())
Read store contents from XML.
bool writeLayerXML(QDomElement &layerElement, QDomDocument &document, QString relativeBasePath=QString::null)
stores state in Dom node
void reset()
Reset the style manager to a basic state - with one default style which is set as current...
virtual ~QgsMapLayer()
Destructor.
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
static QString removePassword(const QString &aUri)
Removes password element from uris.
QgsMapLayerStyleManager * styleManager() const
Get access to the layer's style manager.
virtual Q_DECL_DEPRECATED QString lastError()
const QString & originalName() const
Get the original name of the layer.
static void warning(const QString &msg)
Goes to qWarning.
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.
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.
void blendModeChanged(const QPainter::CompositionMode &blendMode)
Signal emitted when the blend mode is changed, through QgsMapLayer::setBlendMode() ...
const QString & attribution() const
static CUSTOM_CRS_VALIDATION customSrsValidation()
Gets custom function.
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 ...
static const QString qgisSettingsDirPath()
Returns the path to the settings directory in user's home dir.
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.
void setBlendMode(const QPainter::CompositionMode &blendMode)
Write blend mode for layer.
QString mLayerName
Name of the layer - used for display.
virtual bool writeSymbology(QDomNode &node, QDomDocument &doc, QString &errorMessage) const =0
Write the symbology for the layer into the docment provided.
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.
const QString & legendUrl() const
static QString capitaliseLayerName(const QString &name)
A convenience function to (un)capitalise the layer name.
void setConnection(const QString &aHost, const QString &aPort, const QString &aDatabase, const QString &aUsername, const QString &aPassword, SSLmode sslmode=SSLprefer)
Set all connection related members at once.
const QString & name() const
Get the display name of the layer.
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.
QgsRectangle mExtent
Extent of the layer.
QString mMetadataUrl
MetadataUrl of the layer.
QPainter::CompositionMode blendMode() const
Read blend mode for layer.
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's CRS has been reset.
const QString & dataUrl() const
void readStyleManager(const QDomNode &layerNode)
Read style manager's configuration (if any).
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...
QString uri() const
return complete uri
const QString & metadataUrlType() const
#define QgsDebugMsgLevel(str, level)
Q_DECL_DEPRECATED void setCacheImage(QImage *)
const QString & source() const
Returns the source for the layer.
LayerType
Layers enum defining the types of layers that can be added to a map.
void setParam(const QString &key, const QString &value)
Set generic param (generic mode)
void setCrs(const QgsCoordinateReferenceSystem &srs, bool emitSignal=true)
Sets layer's spatial reference system.
void writeStyleManager(QDomNode &layerNode, QDomDocument &doc) const
Write style manager'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.
virtual void setExtent(const QgsRectangle &rect)
Set the extent.
void readXml(const QDomElement &mgrElement)
Read configuration (for project loading)
QString mDataUrl
DataUrl of the layer.
void triggerRepaint()
Will advice the map canvas (and any other interested party) that this layer requires to be repainted...
const QString & metadataUrl() const
The QgsMapLayerLegend class is abstract interface for implementations of legends for one map layer...
QString id() const
Get this layer's unique ID, this ID is used to access this layer from map layer registry.
virtual void setSubLayerVisibility(QString name, bool vis)
Set the visibility of the given sublayer name.
void writeCustomProperties(QDomNode &layerNode, QDomDocument &doc) const
Write custom properties to project file.
virtual Q_DECL_DEPRECATED QString lastErrorTitle()
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 mValid
Indicates if the layer is valid and can be drawn.
static QList< QgsMapLayer * > fromLayerDefinitionFile(const QString &qlrfile)
static QDomDocument asLayerDefinition(QList< QgsMapLayer * > layers, QString relativeBasePath=QString::null)
Returns the given layer as a layer definition document Layer definitions store the data source as wel...
virtual bool readSld(const QDomNode &node, QString &errorMessage)
QString mMetadataUrlFormat
Class for storing the component parts of a PostgreSQL/RDBMS datasource URI.
void setMaximumScale(const float theMaxScale)
Sets the maximum scale denominator at which the layer will be visible.
QString mAttribution
Attribution of the layer.
QString mAbstract
Description of the layer.
QgsMapLayerLegend * legend() const
Can be null.
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.
QString providerType() const
Return the provider type for this layer.
bool writeXML(QDomNode &theNode, QDomDocument &theDoc) const
Contains information about the context of a rendering operation.
Q_DECL_DEPRECATED void clearCacheImage()
Clear cached image.
QString mDataSource
data source description string, varies by layer type
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.
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
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.
Class for storing a coordinate reference system (CRS)
void legendChanged()
Signal emitted when legend of the layer has changed.
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'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)
bool readXML(QDomNode &theNode)
static QList< QgsMapLayer * > fromLayerDefinition(QDomDocument &document)
Creates a new layer from a layer defininition document.
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's undo stack.
virtual QDateTime timestamp() const
Time stamp of data source in the moment when data/metadata were loaded by provider.
void(* CUSTOM_CRS_VALIDATION)(QgsCoordinateReferenceSystem &)
QgsVectorDataProvider * dataProvider()
Returns the data provider.
const QString & attributionUrl() const
void setLegend(QgsMapLayerLegend *legend)
Assign a legend controller to the map layer.
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.
const QString & metadataUrlFormat() const
const QString & legendUrlFormat() const
virtual QgsRectangle extent()
Return the extent of the layer.
Represents a vector layer which manages a vector based data sets.
virtual void setLayerOrder(const QStringList &layers)
Reorders the previously selected sublayers of this layer from bottom to top (Useful for providers tha...
const QString & title() const
QString mLayerOrigName
Original name of the layer.
void setValid(bool valid)
set whether layer is valid or not - should be used in constructor.
const QString & keywordList() const
virtual void exportSldStyle(QDomDocument &doc, QString &errorMsg)
Export the properties of this layer as SLD style in a QDomDocument.
virtual QString dataSourceUri() const
Get the data source specification.
const QString & dataUrlFormat() const
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.
QgsMapLayer(QgsMapLayer::LayerType type=VectorLayer, QString lyrname=QString::null, QString source=QString::null)
Constructor.
void writeXml(QDomElement &mgrElement) const
Write configuration (for project saving)