Skip to content

Commit

Permalink
Add support for embedded legend groups
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Jun 9, 2011
1 parent 733df17 commit f025228
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 11 deletions.
71 changes: 71 additions & 0 deletions src/app/legend/qgslegend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/app/legend/qgslegend.h
Original file line number Diff line number Diff line change
Expand Up @@ -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*/
Expand Down Expand Up @@ -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();

Expand Down
8 changes: 6 additions & 2 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
5 changes: 0 additions & 5 deletions src/core/qgsproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
4 changes: 0 additions & 4 deletions src/core/qgsproject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit f025228

Please sign in to comment.