QString QgsMapLayer::saveSldStyle( const QString &theURI, bool &theResultFlag ) { QString errorMsg; QDomDocument myDocument; exportSldStyle( myDocument, errorMsg ); if ( !errorMsg.isNull() ) { theResultFlag = false; return errorMsg; } QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( this ); // check if the uri is a file or ends with .sld, // which indicates that it should become one QString filename; if ( vlayer->providerType() == "ogr" ) { QStringList theURIParts = theURI.split( "|" ); filename = theURIParts[0]; } else if ( vlayer->providerType() == "delimitedtext" ) { filename = QUrl::fromEncoded( theURI.toAscii() ).toLocalFile(); } else { filename = theURI; } QFileInfo myFileInfo( filename ); if ( myFileInfo.exists() || filename.endsWith( ".sld", Qt::CaseInsensitive ) ) { QFileInfo myDirInfo( myFileInfo.path() ); //excludes file name if ( !myDirInfo.isWritable() ) { return tr( "The directory containing your dataset needs to be writable!" ); } // now construct the file name for our .sld style file QString myFileName = myFileInfo.path() + QDir::separator() + myFileInfo.completeBaseName() + ".sld"; QFile myFile( myFileName ); if ( myFile.open( QFile::WriteOnly | QFile::Truncate ) ) { QTextStream myFileStream( &myFile ); // save as utf-8 with 2 spaces for indents myDocument.save( myFileStream, 2 ); myFile.close(); theResultFlag = true; return tr( "Created default style file as %1" ).arg( myFileName ); } } theResultFlag = false; return tr( "ERROR: Failed to created SLD style file as %1. Check file permissions and retry." ).arg( filename ); }
QString QgsMapLayer::saveSldStyle( const QString theURI, bool & theResultFlag ) { QDomDocument myDocument = QDomDocument(); QDomNode header = myDocument.createProcessingInstruction( "xml", "version=\"1.0\" encoding=\"UTF-8\"" ); myDocument.appendChild( header ); // Create the root element QDomElement root = myDocument.createElementNS( "http://www.opengis.net/sld", "StyledLayerDescriptor" ); root.setAttribute( "version", "1.1.0" ); root.setAttribute( "xsi:schemaLocation", "http://www.opengis.net/sld http://schemas.opengis.net/sld/1.1.0/StyledLayerDescriptor.xsd" ); root.setAttribute( "xmlns:ogc", "http://www.opengis.net/ogc" ); root.setAttribute( "xmlns:se", "http://www.opengis.net/se" ); root.setAttribute( "xmlns:xlink", "http://www.w3.org/1999/xlink" ); root.setAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" ); myDocument.appendChild( root ); // Create the NamedLayer element QDomElement namedLayerNode = myDocument.createElement( "NamedLayer" ); root.appendChild( namedLayerNode ); QString errorMsg; QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( this ); if ( !vlayer ) { theResultFlag = false; return tr( "Could not save symbology because:\n%1" ).arg( "Non-vector layers not supported yet" ); } if ( !vlayer->writeSld( namedLayerNode, myDocument, errorMsg ) ) { theResultFlag = false; return tr( "Could not save symbology because:\n%1" ).arg( errorMsg ); } // check if the uri is a file or ends with .sld, // which indicates that it should become one QString filename; if ( vlayer->providerType() == "ogr" ) { QStringList theURIParts = theURI.split( "|" ); filename = theURIParts[0]; } else if ( vlayer->providerType() == "delimitedtext" ) { filename = QUrl::fromEncoded( theURI.toAscii() ).toLocalFile(); } else { filename = theURI; } QFileInfo myFileInfo( filename ); if ( myFileInfo.exists() || filename.endsWith( ".sld", Qt::CaseInsensitive ) ) { QFileInfo myDirInfo( myFileInfo.path() ); //excludes file name if ( !myDirInfo.isWritable() ) { return tr( "The directory containing your dataset needs to be writable!" ); } // now construct the file name for our .sld style file QString myFileName = myFileInfo.path() + QDir::separator() + myFileInfo.completeBaseName() + ".sld"; QFile myFile( myFileName ); if ( myFile.open( QFile::WriteOnly | QFile::Truncate ) ) { QTextStream myFileStream( &myFile ); // save as utf-8 with 2 spaces for indents myDocument.save( myFileStream, 2 ); myFile.close(); theResultFlag = true; return tr( "Created default style file as %1" ).arg( myFileName ); } } theResultFlag = false; return tr( "ERROR: Failed to created SLD style file as %1. Check file permissions and retry." ).arg( filename ); }
QString QgsMapLayer::saveNamedStyle( const QString &theURI, bool &theResultFlag ) { QString myErrorMessage; QDomDocument myDocument; exportNamedStyle( myDocument, myErrorMessage ); // check if the uri is a file or ends with .qml, // which indicates that it should become one // everything else goes to the database QString filename; QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( this ); if ( vlayer && vlayer->providerType() == "ogr" ) { QStringList theURIParts = theURI.split( "|" ); filename = theURIParts[0]; } else if ( vlayer && vlayer->providerType() == "delimitedtext" ) { filename = QUrl::fromEncoded( theURI.toAscii() ).toLocalFile(); } else { filename = theURI; } QFileInfo myFileInfo( filename ); if ( myFileInfo.exists() || filename.endsWith( ".qml", Qt::CaseInsensitive ) ) { QFileInfo myDirInfo( myFileInfo.path() ); //excludes file name if ( !myDirInfo.isWritable() ) { return tr( "The directory containing your dataset needs to be writable!" ); } // now construct the file name for our .qml style file QString myFileName = myFileInfo.path() + QDir::separator() + myFileInfo.completeBaseName() + ".qml"; QFile myFile( myFileName ); if ( myFile.open( QFile::WriteOnly | QFile::Truncate ) ) { QTextStream myFileStream( &myFile ); // save as utf-8 with 2 spaces for indents myDocument.save( myFileStream, 2 ); myFile.close(); theResultFlag = true; return tr( "Created default style file as %1" ).arg( myFileName ); } else { theResultFlag = false; return tr( "ERROR: Failed to created default style file as %1. Check file permissions and retry." ).arg( myFileName ); } } else { QString qml = myDocument.toString(); // read from database sqlite3 *myDatabase; sqlite3_stmt *myPreparedStatement; const char *myTail; int myResult; myResult = sqlite3_open( QDir( QgsApplication::qgisSettingsDirPath() ).absoluteFilePath( "qgis.qmldb" ).toUtf8().data(), &myDatabase ); if ( myResult != SQLITE_OK ) { return tr( "User database could not be opened." ); } QByteArray param0 = theURI.toUtf8(); QByteArray param1 = qml.toUtf8(); QString mySql = "create table if not exists tbl_styles(style varchar primary key,qml varchar)"; myResult = sqlite3_prepare( myDatabase, mySql.toUtf8().data(), mySql.toUtf8().length(), &myPreparedStatement, &myTail ); if ( myResult == SQLITE_OK ) { if ( sqlite3_step( myPreparedStatement ) != SQLITE_DONE ) { sqlite3_finalize( myPreparedStatement ); sqlite3_close( myDatabase ); theResultFlag = false; return tr( "The style table could not be created." ); } } sqlite3_finalize( myPreparedStatement ); mySql = "insert into tbl_styles(style,qml) values (?,?)"; myResult = sqlite3_prepare( myDatabase, mySql.toUtf8().data(), mySql.toUtf8().length(), &myPreparedStatement, &myTail ); if ( myResult == SQLITE_OK ) { if ( sqlite3_bind_text( myPreparedStatement, 1, param0.data(), param0.length(), SQLITE_STATIC ) == SQLITE_OK && sqlite3_bind_text( myPreparedStatement, 2, param1.data(), param1.length(), SQLITE_STATIC ) == SQLITE_OK && sqlite3_step( myPreparedStatement ) == SQLITE_DONE ) { theResultFlag = true; myErrorMessage = tr( "The style %1 was saved to database" ).arg( theURI ); } } sqlite3_finalize( myPreparedStatement ); if ( !theResultFlag ) { QString mySql = "update tbl_styles set qml=? where style=?"; myResult = sqlite3_prepare( myDatabase, mySql.toUtf8().data(), mySql.toUtf8().length(), &myPreparedStatement, &myTail ); if ( myResult == SQLITE_OK ) { if ( sqlite3_bind_text( myPreparedStatement, 2, param0.data(), param0.length(), SQLITE_STATIC ) == SQLITE_OK && sqlite3_bind_text( myPreparedStatement, 1, param1.data(), param1.length(), SQLITE_STATIC ) == SQLITE_OK && sqlite3_step( myPreparedStatement ) == SQLITE_DONE ) { theResultFlag = true; myErrorMessage = tr( "The style %1 was updated in the database." ).arg( theURI ); } else { theResultFlag = false; myErrorMessage = tr( "The style %1 could not be updated in the database." ).arg( theURI ); } } else { theResultFlag = false; myErrorMessage = tr( "The style %1 could not be inserted into database." ).arg( theURI ); } sqlite3_finalize( myPreparedStatement ); } sqlite3_close( myDatabase ); } return myErrorMessage; }
QString QgsMapLayer::saveNamedStyle( const QString theURI, bool & theResultFlag ) { QString myErrorMessage; QDomImplementation DomImplementation; QDomDocumentType documentType = DomImplementation.createDocumentType( "qgis", "http://mrcc.com/qgis.dtd", "SYSTEM" ); QDomDocument myDocument( documentType ); QDomElement myRootNode = myDocument.createElement( "qgis" ); myRootNode.setAttribute( "version", QString( "%1" ).arg( QGis::QGIS_VERSION ) ); myDocument.appendChild( myRootNode ); // use scale dependent visibility flag myRootNode.setAttribute( "hasScaleBasedVisibilityFlag", hasScaleBasedVisibility() ? 1 : 0 ); myRootNode.setAttribute( "minimumScale", minimumScale() ); myRootNode.setAttribute( "maximumScale", maximumScale() ); // <transparencyLevelInt> QDomElement transparencyLevelIntElement = myDocument.createElement( "transparencyLevelInt" ); QDomText transparencyLevelIntText = myDocument.createTextNode( QString::number( getTransparency() ) ); transparencyLevelIntElement.appendChild( transparencyLevelIntText ); myRootNode.appendChild( transparencyLevelIntElement ); // now append layer node to map layer node QString errorMsg; if ( !writeSymbology( myRootNode, myDocument, errorMsg ) ) { return tr( "Could not save symbology because:\n%1" ).arg( errorMsg ); } // check if the uri is a file or ends with .qml, // which indicates that it should become one // everything else goes to the database QString filename; QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( this ); if ( vlayer && vlayer->providerType() == "ogr" ) { QStringList theURIParts = theURI.split( "|" ); filename = theURIParts[0]; } else if ( vlayer && vlayer->providerType() == "delimitedtext" ) { filename = QUrl::fromEncoded( theURI.toAscii() ).toLocalFile(); } else { filename = theURI; } QFileInfo myFileInfo( filename ); if ( myFileInfo.exists() || filename.endsWith( ".qml", Qt::CaseInsensitive ) ) { QFileInfo myDirInfo( myFileInfo.path() ); //excludes file name if ( !myDirInfo.isWritable() ) { return tr( "The directory containing your dataset needs to be writable!" ); } // now construct the file name for our .qml style file QString myFileName = myFileInfo.path() + QDir::separator() + myFileInfo.completeBaseName() + ".qml"; QFile myFile( myFileName ); if ( myFile.open( QFile::WriteOnly | QFile::Truncate ) ) { QTextStream myFileStream( &myFile ); // save as utf-8 with 2 spaces for indents myDocument.save( myFileStream, 2 ); myFile.close(); theResultFlag = true; return tr( "Created default style file as %1" ).arg( myFileName ); } else { theResultFlag = false; return tr( "ERROR: Failed to created default style file as %1. Check file permissions and retry." ).arg( myFileName ); } } else { QString qml = myDocument.toString(); // read from database sqlite3 *myDatabase; sqlite3_stmt *myPreparedStatement; const char *myTail; int myResult; myResult = sqlite3_open( QDir( QgsApplication::qgisSettingsDirPath() ).absoluteFilePath( "qgis.qmldb" ).toUtf8().data(), &myDatabase ); if ( myResult != SQLITE_OK ) { return tr( "User database could not be opened." ); } QByteArray param0 = theURI.toUtf8(); QByteArray param1 = qml.toUtf8(); QString mySql = "create table if not exists tbl_styles(style varchar primary key,qml varchar)"; myResult = sqlite3_prepare( myDatabase, mySql.toUtf8().data(), mySql.toUtf8().length(), &myPreparedStatement, &myTail ); if ( myResult == SQLITE_OK ) { if ( sqlite3_step( myPreparedStatement ) != SQLITE_DONE ) { sqlite3_finalize( myPreparedStatement ); sqlite3_close( myDatabase ); theResultFlag = false; return tr( "The style table could not be created." ); } } sqlite3_finalize( myPreparedStatement ); mySql = "insert into tbl_styles(style,qml) values (?,?)"; myResult = sqlite3_prepare( myDatabase, mySql.toUtf8().data(), mySql.toUtf8().length(), &myPreparedStatement, &myTail ); if ( myResult == SQLITE_OK ) { if ( sqlite3_bind_text( myPreparedStatement, 1, param0.data(), param0.length(), SQLITE_STATIC ) == SQLITE_OK && sqlite3_bind_text( myPreparedStatement, 2, param1.data(), param1.length(), SQLITE_STATIC ) == SQLITE_OK && sqlite3_step( myPreparedStatement ) == SQLITE_DONE ) { theResultFlag = true; myErrorMessage = tr( "The style %1 was saved to database" ).arg( theURI ); } } sqlite3_finalize( myPreparedStatement ); if ( !theResultFlag ) { QString mySql = "update tbl_styles set qml=? where style=?"; myResult = sqlite3_prepare( myDatabase, mySql.toUtf8().data(), mySql.toUtf8().length(), &myPreparedStatement, &myTail ); if ( myResult == SQLITE_OK ) { if ( sqlite3_bind_text( myPreparedStatement, 2, param0.data(), param0.length(), SQLITE_STATIC ) == SQLITE_OK && sqlite3_bind_text( myPreparedStatement, 1, param1.data(), param1.length(), SQLITE_STATIC ) == SQLITE_OK && sqlite3_step( myPreparedStatement ) == SQLITE_DONE ) { theResultFlag = true; myErrorMessage = tr( "The style %1 was updated in the database." ).arg( theURI ); } else { theResultFlag = false; myErrorMessage = tr( "The style %1 could not be updated in the database." ).arg( theURI ); } } else { theResultFlag = false; myErrorMessage = tr( "The style %1 could not be inserted into database." ).arg( theURI ); } sqlite3_finalize( myPreparedStatement ); } sqlite3_close( myDatabase ); } return myErrorMessage; }