Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix order of embedded layers in groups
  • Loading branch information
mhugent committed Jun 9, 2011
1 parent fba3086 commit b7ec033
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 58 deletions.
86 changes: 45 additions & 41 deletions src/app/legend/qgslegend.cpp
Expand Up @@ -186,6 +186,7 @@ int QgsLegend::addGroup( QString name, bool expand, int groupIndex )
void QgsLegend::removeAll()
{
clear();
mEmbeddedGroups.clear();
mPixmapWidthValues.clear();
mPixmapHeightValues.clear();
updateMapCanvasLayerSet();
Expand Down Expand Up @@ -297,7 +298,7 @@ void QgsLegend::mouseMoveEvent( QMouseEvent * e )
{
//prevent to drag out content under groups that are embedded from other
//project files.
if( parentGroupEmbedded( item ) )
if ( parentGroupEmbedded( item ) )
{
continue;
}
Expand Down Expand Up @@ -372,7 +373,7 @@ void QgsLegend::mouseMoveEvent( QMouseEvent * e )
mDropTarget = layer;

//prevent inserting content into embedded groups
if( !parentGroupEmbedded( litem ) )
if ( !parentGroupEmbedded( litem ) )
{
if ( e->y() < ( y0 + y1 ) / 2 )
{
Expand All @@ -388,14 +389,14 @@ void QgsLegend::mouseMoveEvent( QMouseEvent * e )
}
}
}
else if ( group )
{
else if ( group )
{
if ( yCoordAboveCenter( litem, e->y() ) ) //over center of item
{
QgsDebugMsg( "insert before group" );

//prevent inserting content into embedded groups
if( !parentGroupEmbedded( item ) )
if ( !parentGroupEmbedded( item ) )
{
line_y = visualItemRect( item ).top() + 1;
mDropTarget = item;
Expand All @@ -404,7 +405,7 @@ void QgsLegend::mouseMoveEvent( QMouseEvent * e )
}
else // below center of item
{
if( !groupEmbedded( item ) )
if ( !groupEmbedded( item ) )
{
QgsDebugMsg( "insert into group" );

Expand Down Expand Up @@ -601,7 +602,7 @@ void QgsLegend::handleRightClickEvent( QTreeWidgetItem* item, const QPoint& posi
tr( "&Set group CRS" ), this, SLOT( legendGroupSetCRS() ) );
}

if ( ( li->type() == QgsLegendItem::LEGEND_LAYER || li->type() == QgsLegendItem::LEGEND_GROUP ) && !groupEmbedded( li ) && !parentGroupEmbedded( li ) )
if (( li->type() == QgsLegendItem::LEGEND_LAYER || li->type() == QgsLegendItem::LEGEND_GROUP ) && !groupEmbedded( li ) && !parentGroupEmbedded( li ) )
{
theMenu.addAction( tr( "Re&name" ), this, SLOT( openEditor() ) );
}
Expand Down Expand Up @@ -635,19 +636,19 @@ QgsLegendGroup* QgsLegend::addEmbeddedGroup( const QString& groupName, const QSt

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

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

QDomElement legendElem = projectDocument.documentElement().firstChildElement("legend");
if( legendElem.isNull() )
QDomElement legendElem = projectDocument.documentElement().firstChildElement( "legend" );
if ( legendElem.isNull() )
{
return 0;
}
Expand All @@ -656,21 +657,21 @@ QgsLegendGroup* QgsLegend::addEmbeddedGroup( const QString& groupName, const QSt
QList< QPair< QgsVectorLayer*, QDomElement > > vectorLayerList;
QSettings settings;

QDomNodeList legendGroupList = legendElem.elementsByTagName("legendgroup");
for( int i = 0; i < legendGroupList.size(); ++i )
QDomNodeList legendGroupList = legendElem.elementsByTagName( "legendgroup" );
for ( int i = 0; i < legendGroupList.size(); ++i )
{
QDomElement legendElem = legendGroupList.at(i).toElement();
if( legendElem.attribute("name") == groupName )
QDomElement legendElem = legendGroupList.at( i ).toElement();
if ( legendElem.attribute( "name" ) == groupName )
{
//embedded groups cannot be embedded again
if( legendElem.attribute("embedded") == "1" )
if ( legendElem.attribute( "embedded" ) == "1" )
{
mEmbeddedGroups.remove( groupName );
return 0;
}

QgsLegendGroup* group = 0;
if( parent )
if ( parent )
{
group = new QgsLegendGroup( parent, groupName );
}
Expand All @@ -685,22 +686,24 @@ QgsLegendGroup* QgsLegend::addEmbeddedGroup( const QString& groupName, const QSt
setCurrentItem( group );

QDomNodeList groupChildren = legendElem.childNodes();
for( int j = 0; j < groupChildren.size(); ++j )
for ( int j = 0; j < groupChildren.size(); ++j )
{
QDomElement childElem = groupChildren.at( j ).toElement();
QString tagName = childElem.tagName();
if( tagName == "legendlayer" )
if ( tagName == "legendlayer" )
{
QString layerId = childElem.firstChildElement("filegroup").firstChildElement("legendlayerfile").attribute("layerid");
QString layerId = childElem.firstChildElement( "filegroup" ).firstChildElement( "legendlayerfile" ).attribute( "layerid" );
QgsProject::instance()->createEmbeddedLayer( layerId, projectFilePath, brokenNodes, vectorLayerList, false );
if( currentItem() && currentItem() != group )
QTreeWidgetItem* cItem = currentItem();
if ( cItem && cItem != group )
{
insertItem( currentItem(), group );
removeItem( cItem );
group->insertChild( group->childCount(), cItem );
}
}
else if( tagName == "legendgroup" )
else if ( tagName == "legendgroup" )
{
addEmbeddedGroup( childElem.attribute("name"), projectFilePath, group );
addEmbeddedGroup( childElem.attribute( "name" ), projectFilePath, group );
}
}
checkLayerOrderUpdate();
Expand Down Expand Up @@ -735,7 +738,7 @@ void QgsLegend::addLayer( QgsMapLayer * layer )
}

QgsLegendLayer* llayer = new QgsLegendLayer( layer );
if( !QgsProject::instance()->layerIsEmbedded( layer->id() ).isEmpty() )
if ( !QgsProject::instance()->layerIsEmbedded( layer->id() ).isEmpty() )
{
QFont itemFont;
itemFont.setItalic( true );
Expand Down Expand Up @@ -1032,10 +1035,10 @@ bool QgsLegend::writeXML( QList<QTreeWidgetItem *> items, QDomNode &node, QDomDo
}

QHash< QString, QString >::const_iterator embedIt = mEmbeddedGroups.find( item->text( 0 ) );
if( embedIt != mEmbeddedGroups.constEnd() )
if ( embedIt != mEmbeddedGroups.constEnd() )
{
legendgroupnode.setAttribute("embedded", 1);
legendgroupnode.setAttribute("project", embedIt.value() );
legendgroupnode.setAttribute( "embedded", 1 );
legendgroupnode.setAttribute( "project", embedIt.value() );
}
else
{
Expand Down Expand Up @@ -1119,7 +1122,7 @@ bool QgsLegend::writeXML( QList<QTreeWidgetItem *> items, QDomNode &node, QDomDo
layerfilegroupnode.appendChild( legendlayerfilenode );

//embedded layer?
if( !QgsProject::instance()->layerIsEmbedded( layer->id() ).isEmpty() )
if ( !QgsProject::instance()->layerIsEmbedded( layer->id() ).isEmpty() )
{
legendlayerfilenode.setAttribute( "embedded", "1" );
}
Expand Down Expand Up @@ -1152,7 +1155,7 @@ bool QgsLegend::readXML( QgsLegendGroup *parent, const QDomNode &node )
if ( childelem.tagName() == "legendgroup" )
{
QgsLegendGroup* theGroup = 0;
if( childelem.attribute("embedded") == "1" )
if ( childelem.attribute( "embedded" ) == "1" )
{
theGroup = addEmbeddedGroup( name, childelem.attribute( "project" ) );
}
Expand All @@ -1164,7 +1167,7 @@ bool QgsLegend::readXML( QgsLegendGroup *parent, const QDomNode &node )
theGroup = new QgsLegendGroup( this, name );
}

if( !theGroup )
if ( !theGroup )
{
continue;
}
Expand Down Expand Up @@ -1264,6 +1267,7 @@ bool QgsLegend::readXML( QgsLegendGroup *parent, const QDomNode &node )
bool QgsLegend::readXML( QDomNode& legendnode )
{
clear(); //remove all items first
mEmbeddedGroups.clear();
return readXML( 0, legendnode );
}

Expand Down Expand Up @@ -1851,7 +1855,7 @@ void QgsLegend::openEditor()
QTreeWidgetItem* theItem = currentItem();
if ( theItem )
{
if( !groupEmbedded( theItem ) && !parentGroupEmbedded( theItem ) )
if ( !groupEmbedded( theItem ) && !parentGroupEmbedded( theItem ) )
{
editItem( theItem, 0 );
}
Expand All @@ -1863,7 +1867,7 @@ void QgsLegend::makeToTopLevelItem()
QgsLegendItem* theItem = dynamic_cast<QgsLegendItem *>( currentItem() );
if ( theItem )
{
if( !parentGroupEmbedded( theItem ) )
if ( !parentGroupEmbedded( theItem ) )
{
theItem->storeAppearanceSettings();
removeItem( theItem );
Expand Down Expand Up @@ -2176,17 +2180,17 @@ void QgsLegend::setCRSForSelectedLayers( const QgsCoordinateReferenceSystem &crs

bool QgsLegend::parentGroupEmbedded( QTreeWidgetItem* item ) const
{
if( !item )
if ( !item )
{
return false;
}

QgsLegendItem* lItem = dynamic_cast<QgsLegendItem*>(item);
if( lItem && lItem->parent() )
QgsLegendItem* lItem = dynamic_cast<QgsLegendItem*>( item );
if ( lItem && lItem->parent() )
{
QgsLegendGroup* parentGroup = dynamic_cast<QgsLegendGroup*>( lItem->parent() );
if( parentGroup && parentGroup->type() == QgsLegendItem::LEGEND_GROUP
&& mEmbeddedGroups.contains( parentGroup->text( 0 ) ) )
if ( parentGroup && parentGroup->type() == QgsLegendItem::LEGEND_GROUP
&& mEmbeddedGroups.contains( parentGroup->text( 0 ) ) )
{
return true;
}
Expand All @@ -2196,13 +2200,13 @@ bool QgsLegend::parentGroupEmbedded( QTreeWidgetItem* item ) const

bool QgsLegend::groupEmbedded( QTreeWidgetItem* item ) const
{
if( !item )
if ( !item )
{
return false;
}

QgsLegendGroup* gItem = dynamic_cast<QgsLegendGroup*>(item);
if( !gItem )
QgsLegendGroup* gItem = dynamic_cast<QgsLegendGroup*>( item );
if ( !gItem )
{
return false;
}
Expand Down
27 changes: 10 additions & 17 deletions src/app/qgisapp.cpp
Expand Up @@ -5053,26 +5053,14 @@ void QgisApp::addMapLayer( QgsMapLayer *theMapLayer )
void QgisApp::embedLayers()
{
//dialog to select groups/layers from other project files

//hardcoded for debugging
/*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 );*/

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

QgsEmbedLayerDialog d;
if( d.exec() == QDialog::Accepted )
if ( d.exec() == QDialog::Accepted )
{
mMapCanvas->freeze( true );
//groups
QList< QPair < QString, QString > > groups = d.embeddedGroups();
QList< QPair < QString, QString > >::const_iterator groupIt = groups.constBegin();
for(; groupIt != groups.constEnd(); ++groupIt )
for ( ; groupIt != groups.constEnd(); ++groupIt )
{
mMapLegend->addEmbeddedGroup( groupIt->first, groupIt->second );
}
Expand All @@ -5083,10 +5071,15 @@ void QgisApp::embedLayers()

QList< QPair < QString, QString > > layers = d.embeddedLayers();
QList< QPair < QString, QString > >::const_iterator layerIt = layers.constBegin();
for(; layerIt != layers.constEnd(); ++layerIt )
for ( ; layerIt != layers.constEnd(); ++layerIt )
{
QgsProject::instance()->createEmbeddedLayer( layerIt->first, layerIt->second, brokenNodes, vectorLayerList );
}
mMapCanvas->freeze( false );
if ( groups.size() > 0 || layers.size() > 0 )
{
mMapCanvas->refresh();
}
}
}

Expand Down Expand Up @@ -6555,7 +6548,7 @@ void QgisApp::showLayerProperties( QgsMapLayer *ml )
if ( !ml )
return;

if( !QgsProject::instance()->layerIsEmbedded( ml->id() ).isEmpty() )
if ( !QgsProject::instance()->layerIsEmbedded( ml->id() ).isEmpty() )
{
return; //don't show properties of embedded layers
}
Expand Down

0 comments on commit b7ec033

Please sign in to comment.