-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add class for handling QLR files. Add sip bindings Funded by Nicholas Duggan
- Loading branch information
Showing
7 changed files
with
172 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class CORE_EXPORT QgsLayerDefinition | ||
{ | ||
%TypeHeaderCode | ||
#include <qgslayerdefinition.h> | ||
%End | ||
public: | ||
static bool openLayerDefinition( const QString & path, QgsLayerTreeGroup* rootGroup, QString &errorMessage /Out/ ); | ||
static bool openLayerDefinition( QDomDocument doc, QgsLayerTreeGroup* rootGroup, QString &errorMessage /Out/ ); | ||
static bool exportLayerDefinition( QString path, QList<QgsLayerTreeNode*> selectedTreeNodes, QString &errorMessage /Out/ ); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
#include <QDomNode> | ||
#include <QFileInfo> | ||
#include <QFile> | ||
#include <QDir> | ||
#include <QTextStream> | ||
|
||
#include "qgslogger.h" | ||
#include "qgsmaplayer.h" | ||
#include "qgslayertree.h" | ||
#include "qgsmaplayerregistry.h" | ||
#include "qgslayerdefinition.h" | ||
|
||
bool QgsLayerDefinition::loadLayerDefinition( const QString &path, QgsLayerTreeGroup *rootGroup, QString &errorMessage ) | ||
{ | ||
QFile file( path ); | ||
if ( !file.open( QIODevice::ReadOnly ) ) | ||
{ | ||
errorMessage = QString( "Can not open file" ); | ||
return false; | ||
} | ||
|
||
QDomDocument doc; | ||
QString message; | ||
if ( !doc.setContent( &file, &message ) ) | ||
{ | ||
errorMessage = message; | ||
return false; | ||
} | ||
|
||
QFileInfo fileinfo( file ); | ||
QDir::setCurrent( fileinfo.absoluteDir().path() ); | ||
|
||
return loadLayerDefinition( doc, rootGroup, errorMessage ); | ||
} | ||
|
||
bool QgsLayerDefinition::loadLayerDefinition( QDomDocument doc, QgsLayerTreeGroup *rootGroup, QString &errorMessage ) | ||
{ | ||
QgsLayerTreeGroup* root = new QgsLayerTreeGroup; | ||
// We have to replace the IDs before we load them because it's too late once they are loaded | ||
QDomNodeList ids = doc.elementsByTagName( "id" ); | ||
for ( int i = 0; i < ids.size(); ++i ) | ||
{ | ||
QDomNode idnode = ids.at( i ); | ||
QDomElement idElem = idnode.toElement(); | ||
QString oldid = idElem.text(); | ||
// Strip the date part because we will replace it. | ||
QString layername = oldid.left( oldid.length() - 17 ); | ||
QDateTime dt = QDateTime::currentDateTime(); | ||
QString newid = layername + dt.toString( "yyyyMMddhhmmsszzz" ); | ||
idElem.firstChild().setNodeValue( newid ); | ||
QDomNodeList treeLayerNodes = doc.elementsByTagName( "layer-tree-layer" ); | ||
|
||
for ( int i = 0; i < treeLayerNodes.count(); ++i ) | ||
{ | ||
QDomNode layerNode = treeLayerNodes.at( i ); | ||
QDomElement layerElem = layerNode.toElement(); | ||
if ( layerElem.attribute( "id" ) == oldid ) | ||
{ | ||
layerNode.toElement().setAttribute( "id", newid ); | ||
} | ||
} | ||
} | ||
|
||
QDomElement layerTreeElem = doc.documentElement().firstChildElement( "layer-tree-group" ); | ||
bool loadInLegend = true; | ||
if ( !layerTreeElem.isNull() ) | ||
{ | ||
root->readChildrenFromXML( layerTreeElem ); | ||
loadInLegend = false; | ||
} | ||
|
||
QList<QgsMapLayer*> layers = QgsMapLayer::fromLayerDefinition( doc ); | ||
QgsMapLayerRegistry::instance()->addMapLayers( layers, loadInLegend ); | ||
|
||
QList<QgsLayerTreeNode*> nodes = root->children(); | ||
rootGroup->insertChildNodes( -1, nodes ); | ||
return true; | ||
|
||
} | ||
|
||
bool QgsLayerDefinition::exportLayerDefinition( QString path, QList<QgsLayerTreeNode*> selectedTreeNodes, QString &errorMessage ) | ||
{ | ||
if ( !path.endsWith( ".qlr" ) ) | ||
path = path.append( ".qlr" ); | ||
|
||
QFile file( path ); | ||
QFileInfo fileinfo( file ); | ||
|
||
QDomDocument doc( "qgis-layer-definition" ); | ||
QDomElement qgiselm = doc.createElement( "qlr" ); | ||
doc.appendChild( qgiselm ); | ||
QList<QgsLayerTreeNode*> nodes = selectedTreeNodes; | ||
QgsLayerTreeGroup* root = new QgsLayerTreeGroup; | ||
foreach ( QgsLayerTreeNode* node, nodes ) | ||
{ | ||
QgsLayerTreeNode* newnode = node->clone(); | ||
root->addChildNode( newnode ); | ||
} | ||
root->writeXML( qgiselm ); | ||
|
||
QDomElement layerselm = doc.createElement( "maplayers" ); | ||
QList<QgsLayerTreeLayer*> layers = root->findLayers(); | ||
foreach ( QgsLayerTreeLayer* layer, layers ) | ||
{ | ||
QDomElement layerelm = doc.createElement( "maplayer" ); | ||
layer->layer()->writeLayerXML( layerelm, doc, fileinfo.canonicalFilePath() ); | ||
layerselm.appendChild( layerelm ); | ||
} | ||
qgiselm.appendChild( layerselm ); | ||
|
||
if ( file.open( QFile::WriteOnly | QFile::Truncate ) ) | ||
{ | ||
QTextStream qlayerstream( &file ); | ||
doc.save( qlayerstream, 2 ); | ||
return true; | ||
} | ||
else | ||
{ | ||
errorMessage = file.errorString(); | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#ifndef QGSLAYERDEFINITION_H | ||
#define QGSLAYERDEFINITION_H | ||
|
||
#include "qgslayertreegroup.h" | ||
|
||
/** | ||
* @brief The QgsLayerDefinition class holds generic methods for loading/exporting QLR files. | ||
*/ | ||
class CORE_EXPORT QgsLayerDefinition | ||
{ | ||
public: | ||
/* Loads the QLR at path into QGIS. New layers are added to rootGroup and the map layer registry*/ | ||
static bool loadLayerDefinition( const QString & path, QgsLayerTreeGroup* rootGroup, QString &errorMessage); | ||
/* Loads the QLR from the XML document. New layers are added to rootGroup and the map layer registry */ | ||
static bool loadLayerDefinition( QDomDocument doc, QgsLayerTreeGroup* rootGroup, QString &errorMessage); | ||
/* Export the selected layer tree nodes to a QLR file */ | ||
static bool exportLayerDefinition( QString path, QList<QgsLayerTreeNode*> selectedTreeNodes, QString &errorMessage ); | ||
}; | ||
|
||
#endif // QGSLAYERDEFINITION_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters