Skip to content
Permalink
Browse files

Add support for embedded legend groups

  • Loading branch information
mhugent committed Jun 9, 2011
1 parent 733df17 commit f0252285729f91da93e002c0d511ac93d9a40688
Showing with 82 additions and 11 deletions.
  1. +71 −0 src/app/legend/qgslegend.cpp
  2. +5 −0 src/app/legend/qgslegend.h
  3. +6 −2 src/app/qgisapp.cpp
  4. +0 −5 src/core/qgsproject.cpp
  5. +0 −4 src/core/qgsproject.h
@@ -611,6 +611,77 @@ Qt::CheckState QgsLegend::layerCheckState( QgsMapLayer * layer )
return ll ? ll->checkState( 0 ) : Qt::Unchecked;
}

void QgsLegend::addEmbeddedGroup( const QString& groupName, const QString& projectFilePath, QgsLegendItem* parent )
{
mEmbeddedGroups.insert( groupName, projectFilePath );

//open project file, get layer ids in group, add the layers
QFile projectFile( projectFilePath );
if( !projectFile.open( QIODevice::ReadOnly ) )
{
return;
}

QDomDocument projectDocument;
if( !projectDocument.setContent( &projectFile ) )
{
return;
}

QDomElement legendElem = projectDocument.documentElement().firstChildElement("legend");
if( legendElem.isNull() )
{
return;
}

QList<QDomNode> brokenNodes;
QList< QPair< QgsVectorLayer*, QDomElement > > vectorLayerList;
QSettings settings;

QDomNodeList legendGroupList = legendElem.elementsByTagName("legendgroup");
for( int i = 0; i < legendGroupList.size(); ++i )
{
QDomElement legendElem = legendGroupList.at(i).toElement();
if( legendElem.attribute("name") == groupName )
{
QgsLegendGroup* group = 0;
if( parent )
{
group = new QgsLegendGroup( parent, groupName );
}
else
{
group = new QgsLegendGroup( this, groupName );
}

QFont groupFont;
groupFont.setItalic( true );
group->setFont( 0, groupFont );
setCurrentItem( group );

QDomNodeList groupChildren = legendElem.childNodes();
for( int j = 0; j < groupChildren.size(); ++j )
{
QDomElement childElem = groupChildren.at( j ).toElement();
QString tagName = childElem.tagName();
if( tagName == "legendlayer" )
{
QString layerId = childElem.firstChildElement("filegroup").firstChildElement("legendlayerfile").attribute("layerid");
QgsProject::instance()->createEmbeddedLayer( layerId, projectFilePath, brokenNodes, vectorLayerList );
if( currentItem() )
{
insertItem( currentItem(), group );
}
}
else if( tagName == "legendgroup" )
{
addEmbeddedGroup( childElem.attribute("name"), projectFilePath, group );
}
}
}
}
}

int QgsLegend::getItemPos( QTreeWidgetItem* item )
{
int counter = 1;
@@ -193,6 +193,8 @@ class QgsLegend : public QTreeWidget
/**Returns a layers check state*/
Qt::CheckState layerCheckState( QgsMapLayer * layer );

void addEmbeddedGroup( const QString& groupName, const QString& projectFilePath, QgsLegendItem* parent = 0 );

public slots:

/*!Adds a new layer group with the maplayer to the canvas*/
@@ -367,6 +369,9 @@ class QgsLegend : public QTreeWidget
// The action when the mouse is released
enum { BEFORE, INSERT, AFTER } mDropAction;

// Groups defined in other project files
QHash< QString, QString > mEmbeddedGroups;

/** Hide the line that indicates insertion position */
void hideLine();

@@ -5054,12 +5054,16 @@ void QgisApp::embedLayers()
//dialog to select groups/layers from other project files

//hardcoded for debugging
QString filepath="/home/marco/geodaten/projekte/composertest.qgs";
/*QString filepath="/home/marco/geodaten/projekte/composertest.qgs";
QString id="komb113320110531113659299";
QList<QDomNode> brokenNodes;
QList< QPair< QgsVectorLayer*, QDomElement > > vectorLayerList;
QgsProject::instance()->createEmbeddedLayer( id, filepath, brokenNodes, vectorLayerList );
QgsProject::instance()->createEmbeddedLayer( id, filepath, brokenNodes, vectorLayerList );*/

QString filepath="/home/marco/geodaten/projekte/rasters.qgs";
QString groupname="Karten";
mMapLegend->addEmbeddedGroup( groupname, filepath );
}

void QgisApp::setExtent( QgsRectangle theRect )
@@ -1610,11 +1610,6 @@ void QgsProject::setBadLayerHandler( QgsProjectBadLayerHandler* handler )
mBadLayerHandler = handler;
}

void QgsProject::addEmbeddedLayer( const QString& layerId, const QString& projectFilePath )
{
mEmbeddedLayers.insert( layerId, projectFilePath );
}

QString QgsProject::layerIsEmbedded( const QString& id ) const
{
QHash< QString, QString >::const_iterator it = mEmbeddedLayers.find( id );
@@ -276,10 +276,6 @@ class CORE_EXPORT QgsProject : public QObject
@note added in 1.4 */
void setBadLayerHandler( QgsProjectBadLayerHandler* handler );

/**Adds layer to list of embedded layers
@note: added in version 1.8*/
void addEmbeddedLayer( const QString& layerId, const QString& projectFilePath );

/**Returns project file path if layer is embedded from other project file. Returns empty string if layer is not embedded*/
QString layerIsEmbedded( const QString& id ) const;

0 comments on commit f025228

Please sign in to comment.
You can’t perform that action at this time.