Skip to content

Commit

Permalink
Add QgsMapLayer::importNamedStyle() functions
Browse files Browse the repository at this point in the history
Like this we do not need to use a temporary file to load a style
  • Loading branch information
wonder-sk committed Jan 16, 2015
1 parent cd77256 commit 11d331d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 24 deletions.
11 changes: 10 additions & 1 deletion python/core/qgsmaplayer.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 14 additions & 12 deletions src/core/qgsmaplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() ) );
Expand Down Expand Up @@ -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 );
Expand All @@ -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
Expand All @@ -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 )
Expand Down
11 changes: 10 additions & 1 deletion src/core/qgsmaplayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 9 additions & 10 deletions src/core/qgsmaplayerstylemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "qgsmaplayer.h"

#include <QDomElement>
#include <QTemporaryFile>
#include <QTextStream>

QgsMapLayerStyleManager::QgsMapLayerStyleManager( QgsMapLayer* layer )
Expand Down Expand Up @@ -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();
QDomDocument doc( "qgis" );
if ( !doc.setContent( mXmlData ) )
{
QgsDebugMsg( "Failed to parse XML of previously stored XML data - this should not happen!" );
return;
}

bool res;
QString status = layer->loadNamedStyle( f.fileName(), res );
if ( !res )
QString errorMsg;
if ( !layer->importNamedStyle( doc, errorMsg ) )
{
QgsDebugMsg( "Failed to import style to layer: " + status );
QgsDebugMsg( "Failed to import style to layer: " + errorMsg );
}
}

Expand Down

0 comments on commit 11d331d

Please sign in to comment.