Skip to content
Permalink
Browse files

Wire in dialog to select embedded groups and layers

  • Loading branch information
mhugent committed Jun 8, 2011
1 parent e432d50 commit a9252db3f981a1589024a78bda7717cd7af26a5b
@@ -20,6 +20,7 @@ SET(QGIS_APP_SRCS
qgscustomprojectiondialog.cpp
qgsdbfilterproxymodel.cpp
qgsdbtablemodel.cpp
qgsembedlayerdialog.cpp
qgsformannotationdialog.cpp
qgsdelattrdialog.cpp
qgsdisplayangle.cpp
@@ -152,6 +153,7 @@ SET (QGIS_APP_MOC_HDRS
qgsdbtablemodel.h
qgsdelattrdialog.h
qgsdisplayangle.h
qgsembedlayerdialog.h
qgsfeatureaction.h
qgsfieldcalculator.h
qgsformannotationdialog.h
@@ -611,27 +611,27 @@ Qt::CheckState QgsLegend::layerCheckState( QgsMapLayer * layer )
return ll ? ll->checkState( 0 ) : Qt::Unchecked;
}

void QgsLegend::addEmbeddedGroup( const QString& groupName, const QString& projectFilePath, QgsLegendItem* parent )
QgsLegendGroup* 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;
return 0;
}

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

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

QList<QDomNode> brokenNodes;
@@ -667,8 +667,8 @@ void QgsLegend::addEmbeddedGroup( const QString& groupName, const QString& proje
if( tagName == "legendlayer" )
{
QString layerId = childElem.firstChildElement("filegroup").firstChildElement("legendlayerfile").attribute("layerid");
QgsProject::instance()->createEmbeddedLayer( layerId, projectFilePath, brokenNodes, vectorLayerList );
if( currentItem() )
QgsProject::instance()->createEmbeddedLayer( layerId, projectFilePath, brokenNodes, vectorLayerList, false );
if( currentItem() && currentItem() != group )
{
insertItem( currentItem(), group );
}
@@ -678,8 +678,10 @@ void QgsLegend::addEmbeddedGroup( const QString& groupName, const QString& proje
addEmbeddedGroup( childElem.attribute("name"), projectFilePath, group );
}
}
return group;
}
}
return 0;
}

int QgsLegend::getItemPos( QTreeWidgetItem* item )
@@ -1003,14 +1005,22 @@ bool QgsLegend::writeXML( QList<QTreeWidgetItem *> items, QDomNode &node, QDomDo
legendgroupnode.setAttribute( "checked", "Qt::PartiallyChecked" );
}

QList<QTreeWidgetItem *> children;
for ( int i = 0; i < currentItem->childCount(); i++ )
QHash< QString, QString >::const_iterator embedIt = mEmbeddedGroups.find( item->text( 0 ) );
if( embedIt != mEmbeddedGroups.constEnd() )
{
children << currentItem->child( i );
legendgroupnode.setAttribute("embedded", 1);
legendgroupnode.setAttribute("project", embedIt.value() );
}
else
{
QList<QTreeWidgetItem *> children;
for ( int i = 0; i < currentItem->childCount(); i++ )
{
children << currentItem->child( i );
}

writeXML( children, legendgroupnode, document );

writeXML( children, legendgroupnode, document );
}
node.appendChild( legendgroupnode );
}
else if ( item->type() == QgsLegendItem::LEGEND_LAYER )
@@ -1109,11 +1119,18 @@ bool QgsLegend::readXML( QgsLegendGroup *parent, const QDomNode &node )
//test every possibility of element...
if ( childelem.tagName() == "legendgroup" )
{
QgsLegendGroup *theGroup;
if ( parent )
theGroup = new QgsLegendGroup( parent, name );
QgsLegendGroup* theGroup = 0;
if( childelem.attribute("embedded") == "1" )
{
theGroup = addEmbeddedGroup( name, childelem.attribute( "project" ) );
}
else
theGroup = new QgsLegendGroup( this, name );
{
if ( parent )
theGroup = new QgsLegendGroup( parent, name );
else
theGroup = new QgsLegendGroup( this, name );
}

//set the checkbox of the legend group to the right state
blockSignals( true );
@@ -193,7 +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 );
/**Add group from other project file. Returns a pointer to the new group in case of success or 0 in case of error*/
QgsLegendGroup* addEmbeddedGroup( const QString& groupName, const QString& projectFilePath, QgsLegendItem* parent = 0 );

public slots:

@@ -369,7 +370,8 @@ class QgsLegend : public QTreeWidget
// The action when the mouse is released
enum { BEFORE, INSERT, AFTER } mDropAction;

// Groups defined in other project files
/** Groups defined in other project files.
Key: group name, value: absolute path to project file*/
QHash< QString, QString > mEmbeddedGroups;

/** Hide the line that indicates insertion position */
@@ -113,6 +113,7 @@
#include "qgscustomization.h"
#include "qgscustomprojectiondialog.h"
#include "qgsdatasourceuri.h"
#include "qgsembedlayerdialog.h"
#include "qgsencodingfiledialog.h"
#include "qgsexception.h"
#include "qgsfeature.h"
@@ -5061,9 +5062,32 @@ void QgisApp::embedLayers()
QList< QPair< QgsVectorLayer*, QDomElement > > vectorLayerList;
QgsProject::instance()->createEmbeddedLayer( id, filepath, brokenNodes, vectorLayerList );*/

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

QgsEmbedLayerDialog d;
if( d.exec() == QDialog::Accepted )
{
//groups
QList< QPair < QString, QString > > groups = d.embeddedGroups();
QList< QPair < QString, QString > >::const_iterator groupIt = groups.constBegin();
for(; groupIt != groups.constEnd(); ++groupIt )
{
mMapLegend->addEmbeddedGroup( groupIt->first, groupIt->second );
}

//layers
QList<QDomNode> brokenNodes;
QList< QPair< QgsVectorLayer*, QDomElement > > vectorLayerList;

QList< QPair < QString, QString > > layers = d.embeddedLayers();
QList< QPair < QString, QString > >::const_iterator layerIt = layers.constBegin();
for(; layerIt != layers.constEnd(); ++layerIt )
{
QgsProject::instance()->createEmbeddedLayer( layerIt->first, layerIt->second, brokenNodes, vectorLayerList );
}
}
}

void QgisApp::setExtent( QgsRectangle theRect )
@@ -0,0 +1,182 @@
#include "qgsembedlayerdialog.h"
#include <QDomDocument>
#include <QFileDialog>

QgsEmbedLayerDialog::QgsEmbedLayerDialog( QWidget * parent, Qt::WindowFlags f ): QDialog( parent, f )
{
setupUi( this );
}

QgsEmbedLayerDialog::~QgsEmbedLayerDialog()
{
}

QList< QPair < QString, QString > > QgsEmbedLayerDialog::embeddedGroups() const
{
QList< QPair < QString, QString > > result;

QList<QTreeWidgetItem*> items = mTreeWidget->selectedItems();
QList<QTreeWidgetItem*>::iterator itemIt = items.begin();
for(; itemIt != items.end(); ++itemIt )
{
if( (*itemIt)->data(0, Qt::UserRole).toString() == "group" )
{
result.push_back( qMakePair( (*itemIt)->text( 0 ), mProjectFileLineEdit->text() ) );
}
}

return result;
}

QList< QPair < QString, QString > > QgsEmbedLayerDialog::embeddedLayers() const
{
QList< QPair < QString, QString > > result;

QList<QTreeWidgetItem*> items = mTreeWidget->selectedItems();
QList<QTreeWidgetItem*>::iterator itemIt = items.begin();
for(; itemIt != items.end(); ++itemIt )
{
if( (*itemIt)->data(0, Qt::UserRole).toString() == "layer" )
{
result.push_back( qMakePair( (*itemIt)->data(0, Qt::UserRole + 1).toString(), mProjectFileLineEdit->text() ) );
}
}
return result;
}

void QgsEmbedLayerDialog::on_mBrowseFileToolButton_clicked()
{
QString projectFile = QFileDialog::getOpenFileName( 0, tr("Select project file"), "",tr("QGIS project files (*.qgs)") );
if( !projectFile.isEmpty() )
{
mProjectFileLineEdit->setText( projectFile );
}
changeProjectFile();
}

void QgsEmbedLayerDialog::on_mProjectFileLineEdit_editingFinished()
{
changeProjectFile();
}

void QgsEmbedLayerDialog::changeProjectFile()
{
mTreeWidget->clear();
QFile projectFile( mProjectFileLineEdit->text() );
if( !projectFile.exists() )
{
return;
}

//parse project file and fill tree
if( !projectFile.open( QIODevice::ReadOnly ) )
{
return;
}

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

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

QDomNodeList legendChildren = legendElem.childNodes();
QDomElement currentChildElem;

for( int i = 0; i < legendChildren.size(); ++i )
{
currentChildElem = legendChildren.at( i ).toElement();
if( currentChildElem.tagName() == "legendlayer" )
{
addLegendLayerToTreeWidget( currentChildElem );
}
else if( currentChildElem.tagName() == "legendgroup" )
{
addLegendGroupToTreeWidget( currentChildElem );
}
}
}

void QgsEmbedLayerDialog::addLegendGroupToTreeWidget( const QDomElement& groupElem, QTreeWidgetItem* parent )
{
QDomNodeList groupChildren = groupElem.childNodes();
QDomElement currentChildElem;

QTreeWidgetItem* groupItem = 0;
if( !parent )
{
groupItem = new QTreeWidgetItem( mTreeWidget );
}
else
{
groupItem = new QTreeWidgetItem( parent );
}
groupItem->setText( 0, groupElem.attribute("name") );
groupItem->setData( 0, Qt::UserRole, "group" );

for( int i = 0; i < groupChildren.size(); ++i )
{
currentChildElem = groupChildren.at( i ).toElement();
if( currentChildElem.tagName() == "legendlayer" )
{
addLegendLayerToTreeWidget( currentChildElem, groupItem );
}
else if( currentChildElem.tagName() == "legendgroup" )
{
addLegendGroupToTreeWidget( currentChildElem, groupItem );
}
}
}

void QgsEmbedLayerDialog::addLegendLayerToTreeWidget( const QDomElement& layerElem, QTreeWidgetItem* parent )
{
QTreeWidgetItem* item = 0;
if( parent )
{
item = new QTreeWidgetItem( parent );
}
else
{
item = new QTreeWidgetItem( mTreeWidget );
}
item->setText( 0, layerElem.attribute("name") );
item->setData( 0, Qt::UserRole, "layer" );
item->setData( 0, Qt::UserRole + 1, layerElem.firstChildElement("filegroup").firstChildElement("legendlayerfile").attribute("layerid") );
}

void QgsEmbedLayerDialog::on_mTreeWidget_itemSelectionChanged()
{
mTreeWidget->blockSignals( true );
QList<QTreeWidgetItem*> items = mTreeWidget->selectedItems();
QList<QTreeWidgetItem*>::iterator itemIt = items.begin();
for(; itemIt != items.end(); ++itemIt )
{
//deselect children recursively
unselectChildren( *itemIt );
}
mTreeWidget->blockSignals( false );
}

void QgsEmbedLayerDialog::unselectChildren( QTreeWidgetItem* item )
{
if( !item )
{
return;
}

QTreeWidgetItem* currentChild = 0;
for( int i = 0; i < item->childCount(); ++i )
{
currentChild = item->child( i );
currentChild->setSelected( false );
unselectChildren( currentChild );
}
}


0 comments on commit a9252db

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