mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-14 00:07:35 -04:00
Add QgsMapLayer::importNamedStyle() functions
Like this we do not need to use a temporary file to load a style
This commit is contained in:
parent
cd77256714
commit
11d331dd66
@ -279,7 +279,16 @@ class QgsMapLayer : QObject
|
||||
|
||||
virtual bool loadNamedStyleFromDb( const QString &db, const QString &theURI, QString &qml /Out/ );
|
||||
|
||||
//TODO edit infos
|
||||
/**
|
||||
* Import the properties of this layer from a QDomDocument
|
||||
* @param doc source QDomDocument
|
||||
* @param errorMsg this QString will be initialized on error
|
||||
* during the execution of readSymbology
|
||||
* @return true on success
|
||||
* @note added in 2.8
|
||||
*/
|
||||
virtual bool importNamedStyle( QDomDocument& doc, QString &errorMsg /Out/ );
|
||||
|
||||
/**
|
||||
* Export the properties of this layer as named style in a QDomDocument
|
||||
* @param doc the target QDomDocument
|
||||
|
@ -947,6 +947,7 @@ bool QgsMapLayer::loadNamedStyleFromDb( const QString &db, const QString &theURI
|
||||
return theResultFlag;
|
||||
}
|
||||
|
||||
|
||||
QString QgsMapLayer::loadNamedStyle( const QString &theURI, bool &theResultFlag )
|
||||
{
|
||||
QgsDebugMsg( QString( "uri = %1 myURI = %2" ).arg( theURI ).arg( publicSource() ) );
|
||||
@ -995,6 +996,16 @@ QString QgsMapLayer::loadNamedStyle( const QString &theURI, bool &theResultFlag
|
||||
return myErrorMessage;
|
||||
}
|
||||
|
||||
theResultFlag = importNamedStyle( myDocument, myErrorMessage );
|
||||
if ( !theResultFlag )
|
||||
myErrorMessage = tr( "Loading style file %1 failed because:\n%2" ).arg( theURI ).arg( myErrorMessage );
|
||||
|
||||
return myErrorMessage;
|
||||
}
|
||||
|
||||
|
||||
bool QgsMapLayer::importNamedStyle( QDomDocument& myDocument, QString& myErrorMessage )
|
||||
{
|
||||
// get style file version string, if any
|
||||
QgsProjectVersion fileVersion( myDocument.firstChildElement( "qgis" ).attribute( "version" ) );
|
||||
QgsProjectVersion thisVersion( QGis::QGIS_VERSION );
|
||||
@ -1017,9 +1028,8 @@ QString QgsMapLayer::loadNamedStyle( const QString &theURI, bool &theResultFlag
|
||||
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;
|
||||
myErrorMessage = tr( "Root <qgis> element could not be found" );
|
||||
return false;
|
||||
}
|
||||
|
||||
// use scale dependent visibility flag
|
||||
@ -1039,15 +1049,7 @@ QString QgsMapLayer::loadNamedStyle( const QString &theURI, bool &theResultFlag
|
||||
}
|
||||
#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 "";
|
||||
return readSymbology( myRoot, myErrorMessage );
|
||||
}
|
||||
|
||||
void QgsMapLayer::exportNamedStyle( QDomDocument &doc, QString &errorMsg )
|
||||
|
@ -295,7 +295,16 @@ class CORE_EXPORT QgsMapLayer : public QObject
|
||||
|
||||
virtual bool loadNamedStyleFromDb( const QString &db, const QString &theURI, QString &qml );
|
||||
|
||||
//TODO edit infos
|
||||
/**
|
||||
* Import the properties of this layer from a QDomDocument
|
||||
* @param doc source QDomDocument
|
||||
* @param errorMsg this QString will be initialized on error
|
||||
* during the execution of readSymbology
|
||||
* @return true on success
|
||||
* @note added in 2.8
|
||||
*/
|
||||
virtual bool importNamedStyle( QDomDocument& doc, QString &errorMsg );
|
||||
|
||||
/**
|
||||
* Export the properties of this layer as named style in a QDomDocument
|
||||
* @param doc the target QDomDocument
|
||||
|
@ -19,7 +19,6 @@
|
||||
#include "qgsmaplayer.h"
|
||||
|
||||
#include <QDomElement>
|
||||
#include <QTemporaryFile>
|
||||
#include <QTextStream>
|
||||
|
||||
QgsMapLayerStyleManager::QgsMapLayerStyleManager( QgsMapLayer* layer )
|
||||
@ -196,17 +195,17 @@ void QgsMapLayerStyle::readFromLayer( QgsMapLayer* layer )
|
||||
|
||||
void QgsMapLayerStyle::writeToLayer( QgsMapLayer* layer ) const
|
||||
{
|
||||
// QgsMapLayer does not have a importNamedStyle() method - working it around like this
|
||||
QTemporaryFile f;
|
||||
f.open();
|
||||
f.write( mXmlData );
|
||||
f.flush();
|
||||
|
||||
bool res;
|
||||
QString status = layer->loadNamedStyle( f.fileName(), res );
|
||||
if ( !res )
|
||||
QDomDocument doc( "qgis" );
|
||||
if ( !doc.setContent( mXmlData ) )
|
||||
{
|
||||
QgsDebugMsg( "Failed to import style to layer: " + status );
|
||||
QgsDebugMsg( "Failed to parse XML of previously stored XML data - this should not happen!" );
|
||||
return;
|
||||
}
|
||||
|
||||
QString errorMsg;
|
||||
if ( !layer->importNamedStyle( doc, errorMsg ) )
|
||||
{
|
||||
QgsDebugMsg( "Failed to import style to layer: " + errorMsg );
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user