bool QgsMeshLayer::writeXml( QDomNode &layer_node, QDomDocument &document, const QgsReadWriteContext &context ) const { // first get the layer element so that we can append the type attribute QDomElement mapLayerNode = layer_node.toElement(); if ( mapLayerNode.isNull() || ( QLatin1String( "maplayer" ) != mapLayerNode.nodeName() ) ) { QgsDebugMsgLevel( QStringLiteral( "can't find <maplayer>" ), 2 ); return false; } mapLayerNode.setAttribute( QStringLiteral( "type" ), QStringLiteral( "mesh" ) ); // add provider node if ( mDataProvider ) { QDomElement provider = document.createElement( QStringLiteral( "provider" ) ); QDomText providerText = document.createTextNode( providerType() ); provider.appendChild( providerText ); layer_node.appendChild( provider ); } // renderer specific settings QString errorMsg; return writeSymbology( layer_node, document, errorMsg, context ); }
void QgsMapLayer::exportNamedStyle( QDomDocument &doc, QString &errorMsg ) { 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 ); myRootNode.setAttribute( "hasScaleBasedVisibilityFlag", hasScaleBasedVisibility() ? 1 : 0 ); myRootNode.setAttribute( "minimumScale", QString::number( minimumScale() ) ); myRootNode.setAttribute( "maximumScale", QString::number( maximumScale() ) ); #if 0 // <transparencyLevelInt> QDomElement transparencyLevelIntElement = myDocument.createElement( "transparencyLevelInt" ); QDomText transparencyLevelIntText = myDocument.createTextNode( QString::number( getTransparency() ) ); transparencyLevelIntElement.appendChild( transparencyLevelIntText ); myRootNode.appendChild( transparencyLevelIntElement ); #endif if ( !writeSymbology( myRootNode, myDocument, errorMsg ) ) { errorMsg = QObject::tr( "Could not save symbology because:\n%1" ).arg( errorMsg ); return; } doc = myDocument; }
bool QgsMeshLayer::writeXml( QDomNode &layer_node, QDomDocument &document, const QgsReadWriteContext &context ) const { // first get the layer element so that we can append the type attribute QDomElement mapLayerNode = layer_node.toElement(); if ( mapLayerNode.isNull() || ( QLatin1String( "maplayer" ) != mapLayerNode.nodeName() ) ) { QgsDebugMsgLevel( QStringLiteral( "can't find <maplayer>" ), 2 ); return false; } mapLayerNode.setAttribute( QStringLiteral( "type" ), QStringLiteral( "mesh" ) ); // add provider node if ( mDataProvider ) { QDomElement provider = document.createElement( QStringLiteral( "provider" ) ); QDomText providerText = document.createTextNode( providerType() ); provider.appendChild( providerText ); layer_node.appendChild( provider ); const QStringList extraDatasetUris = mDataProvider->extraDatasets(); QDomElement elemExtraDatasets = document.createElement( QStringLiteral( "extra-datasets" ) ); for ( const QString &uri : extraDatasetUris ) { QString path = context.pathResolver().writePath( uri ); QDomElement elemUri = document.createElement( QStringLiteral( "uri" ) ); elemUri.appendChild( document.createTextNode( path ) ); elemExtraDatasets.appendChild( elemUri ); } layer_node.appendChild( elemExtraDatasets ); } // renderer specific settings QString errorMsg; return writeSymbology( layer_node, document, errorMsg, context ); }
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; }