QString QgsMapLayer::styleURI( ) { QString myURI = publicSource(); // if file is using the VSIFILE mechanism, remove the prefix if ( myURI.startsWith( "/vsigzip/", Qt::CaseInsensitive ) ) { myURI.remove( 0, 9 ); } else if ( myURI.startsWith( "/vsizip/", Qt::CaseInsensitive ) && myURI.endsWith( ".zip", Qt::CaseInsensitive ) ) { // ideally we should look for .qml file inside zip file myURI.remove( 0, 8 ); } else if ( myURI.startsWith( "/vsitar/", Qt::CaseInsensitive ) && ( myURI.endsWith( ".tar", Qt::CaseInsensitive ) || myURI.endsWith( ".tar.gz", Qt::CaseInsensitive ) || myURI.endsWith( ".tgz", Qt::CaseInsensitive ) ) ) { // ideally we should look for .qml file inside tar file myURI.remove( 0, 8 ); } QFileInfo myFileInfo( myURI ); QString key; if ( myFileInfo.exists() ) { // if file is using the /vsizip/ or /vsigzip/ mechanism, cleanup the name if ( myURI.endsWith( ".gz", Qt::CaseInsensitive ) ) myURI.chop( 3 ); else if ( myURI.endsWith( ".zip", Qt::CaseInsensitive ) ) myURI.chop( 4 ); else if ( myURI.endsWith( ".tar", Qt::CaseInsensitive ) ) myURI.chop( 4 ); else if ( myURI.endsWith( ".tar.gz", Qt::CaseInsensitive ) ) myURI.chop( 7 ); else if ( myURI.endsWith( ".tgz", Qt::CaseInsensitive ) ) myURI.chop( 4 ); else if ( myURI.endsWith( ".gz", Qt::CaseInsensitive ) ) myURI.chop( 3 ); myFileInfo.setFile( myURI ); // get the file name for our .qml style file key = myFileInfo.path() + QDir::separator() + myFileInfo.completeBaseName() + ".qml"; } else { key = publicSource(); } return key; }
QString QgsMapLayer::loadDefaultStyle( bool & theResultFlag ) { QString myURI = publicSource(); QFileInfo myFileInfo( myURI ); QString key; if ( myFileInfo.exists() ) { // get the file name for our .qml style file key = myFileInfo.path() + QDir::separator() + myFileInfo.completeBaseName() + ".qml"; } else { key = myURI; } return loadNamedStyle( key, theResultFlag ); }
QString QgsMapLayer::loadNamedStyle( const QString &theURI, bool &theResultFlag ) { QgsDebugMsg( QString( "uri = %1 myURI = %2" ).arg( theURI ).arg( publicSource() ) ); theResultFlag = false; QDomDocument myDocument( "qgis" ); // location of problem associated with errorMsg int line, column; QString myErrorMessage; QFile myFile( theURI ); if ( myFile.open( QFile::ReadOnly ) ) { // read file theResultFlag = myDocument.setContent( &myFile, &myErrorMessage, &line, &column ); if ( !theResultFlag ) myErrorMessage = tr( "%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column ); myFile.close(); } else { QFileInfo project( QgsProject::instance()->fileName() ); QgsDebugMsg( QString( "project fileName: %1" ).arg( project.absoluteFilePath() ) ); QString qml; if ( loadNamedStyleFromDb( QDir( QgsApplication::qgisSettingsDirPath() ).absoluteFilePath( "qgis.qmldb" ), theURI, qml ) || ( project.exists() && loadNamedStyleFromDb( project.absoluteDir().absoluteFilePath( project.baseName() + ".qmldb" ), theURI, qml ) ) || loadNamedStyleFromDb( QDir( QgsApplication::pkgDataPath() ).absoluteFilePath( "resources/qgis.qmldb" ), theURI, qml ) ) { theResultFlag = myDocument.setContent( qml, &myErrorMessage, &line, &column ); if ( !theResultFlag ) { myErrorMessage = tr( "%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column ); } } else { myErrorMessage = tr( "style not found in database" ); } } if ( !theResultFlag ) { return myErrorMessage; } // get style file version string, if any QgsProjectVersion fileVersion( myDocument.firstChildElement( "qgis" ).attribute( "version" ) ); QgsProjectVersion thisVersion( QGis::QGIS_VERSION ); if ( thisVersion > fileVersion ) { QgsLogger::warning( "Loading a style file that was saved with an older " "version of qgis (saved in " + fileVersion.text() + ", loaded in " + QGis::QGIS_VERSION + "). Problems may occur." ); QgsProjectFileTransform styleFile( myDocument, fileVersion ); // styleFile.dump(); styleFile.updateRevision( thisVersion ); // styleFile.dump(); } // now get the layer node out and pass it over to the layer // to deserialise... QDomElement myRoot = myDocument.firstChildElement( "qgis" ); if ( myRoot.isNull() ) { myErrorMessage = tr( "Error: qgis element could not be found in %1" ).arg( theURI ); theResultFlag = false; return myErrorMessage; } // use scale dependent visibility flag toggleScaleBasedVisibility( myRoot.attribute( "hasScaleBasedVisibilityFlag" ).toInt() == 1 ); setMinimumScale( myRoot.attribute( "minimumScale" ).toFloat() ); setMaximumScale( myRoot.attribute( "maximumScale" ).toFloat() ); #if 0 //read transparency level QDomNode transparencyNode = myRoot.namedItem( "transparencyLevelInt" ); if ( ! transparencyNode.isNull() ) { // set transparency level only if it's in project // (otherwise it sets the layer transparent) QDomElement myElement = transparencyNode.toElement(); setTransparency( myElement.text().toInt() ); } #endif QString errorMsg; theResultFlag = readSymbology( myRoot, errorMsg ); if ( !theResultFlag ) { myErrorMessage = tr( "Loading style file %1 failed because:\n%2" ).arg( theURI ).arg( errorMsg ); return myErrorMessage; } return ""; }
QString QgsMapLayer::saveDefaultStyle( bool & theResultFlag ) { return saveNamedStyle( publicSource(), theResultFlag ); }