Skip to content

Commit 819084d

Browse files
committed
Wire in dialog to select embedded groups and layers
1 parent 097881f commit 819084d

File tree

9 files changed

+400
-116
lines changed

9 files changed

+400
-116
lines changed

src/app/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ SET(QGIS_APP_SRCS
2020
qgscustomprojectiondialog.cpp
2121
qgsdbfilterproxymodel.cpp
2222
qgsdbtablemodel.cpp
23+
qgsembedlayerdialog.cpp
2324
qgsformannotationdialog.cpp
2425
qgsdelattrdialog.cpp
2526
qgsdisplayangle.cpp
@@ -152,6 +153,7 @@ SET (QGIS_APP_MOC_HDRS
152153
qgsdbtablemodel.h
153154
qgsdelattrdialog.h
154155
qgsdisplayangle.h
156+
qgsembedlayerdialog.h
155157
qgsfeatureaction.h
156158
qgsfieldcalculator.h
157159
qgsformannotationdialog.h

src/app/legend/qgslegend.cpp

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -611,27 +611,27 @@ Qt::CheckState QgsLegend::layerCheckState( QgsMapLayer * layer )
611611
return ll ? ll->checkState( 0 ) : Qt::Unchecked;
612612
}
613613

614-
void QgsLegend::addEmbeddedGroup( const QString& groupName, const QString& projectFilePath, QgsLegendItem* parent )
614+
QgsLegendGroup* QgsLegend::addEmbeddedGroup( const QString& groupName, const QString& projectFilePath, QgsLegendItem* parent )
615615
{
616616
mEmbeddedGroups.insert( groupName, projectFilePath );
617617

618618
//open project file, get layer ids in group, add the layers
619619
QFile projectFile( projectFilePath );
620620
if( !projectFile.open( QIODevice::ReadOnly ) )
621621
{
622-
return;
622+
return 0;
623623
}
624624

625625
QDomDocument projectDocument;
626626
if( !projectDocument.setContent( &projectFile ) )
627627
{
628-
return;
628+
return 0;
629629
}
630630

631631
QDomElement legendElem = projectDocument.documentElement().firstChildElement("legend");
632632
if( legendElem.isNull() )
633633
{
634-
return;
634+
return 0;
635635
}
636636

637637
QList<QDomNode> brokenNodes;
@@ -667,8 +667,8 @@ void QgsLegend::addEmbeddedGroup( const QString& groupName, const QString& proje
667667
if( tagName == "legendlayer" )
668668
{
669669
QString layerId = childElem.firstChildElement("filegroup").firstChildElement("legendlayerfile").attribute("layerid");
670-
QgsProject::instance()->createEmbeddedLayer( layerId, projectFilePath, brokenNodes, vectorLayerList );
671-
if( currentItem() )
670+
QgsProject::instance()->createEmbeddedLayer( layerId, projectFilePath, brokenNodes, vectorLayerList, false );
671+
if( currentItem() && currentItem() != group )
672672
{
673673
insertItem( currentItem(), group );
674674
}
@@ -678,8 +678,10 @@ void QgsLegend::addEmbeddedGroup( const QString& groupName, const QString& proje
678678
addEmbeddedGroup( childElem.attribute("name"), projectFilePath, group );
679679
}
680680
}
681+
return group;
681682
}
682683
}
684+
return 0;
683685
}
684686

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

1006-
QList<QTreeWidgetItem *> children;
1007-
for ( int i = 0; i < currentItem->childCount(); i++ )
1008+
QHash< QString, QString >::const_iterator embedIt = mEmbeddedGroups.find( item->text( 0 ) );
1009+
if( embedIt != mEmbeddedGroups.constEnd() )
10081010
{
1009-
children << currentItem->child( i );
1011+
legendgroupnode.setAttribute("embedded", 1);
1012+
legendgroupnode.setAttribute("project", embedIt.value() );
10101013
}
1014+
else
1015+
{
1016+
QList<QTreeWidgetItem *> children;
1017+
for ( int i = 0; i < currentItem->childCount(); i++ )
1018+
{
1019+
children << currentItem->child( i );
1020+
}
10111021

1012-
writeXML( children, legendgroupnode, document );
1013-
1022+
writeXML( children, legendgroupnode, document );
1023+
}
10141024
node.appendChild( legendgroupnode );
10151025
}
10161026
else if ( item->type() == QgsLegendItem::LEGEND_LAYER )
@@ -1109,11 +1119,18 @@ bool QgsLegend::readXML( QgsLegendGroup *parent, const QDomNode &node )
11091119
//test every possibility of element...
11101120
if ( childelem.tagName() == "legendgroup" )
11111121
{
1112-
QgsLegendGroup *theGroup;
1113-
if ( parent )
1114-
theGroup = new QgsLegendGroup( parent, name );
1122+
QgsLegendGroup* theGroup = 0;
1123+
if( childelem.attribute("embedded") == "1" )
1124+
{
1125+
theGroup = addEmbeddedGroup( name, childelem.attribute( "project" ) );
1126+
}
11151127
else
1116-
theGroup = new QgsLegendGroup( this, name );
1128+
{
1129+
if ( parent )
1130+
theGroup = new QgsLegendGroup( parent, name );
1131+
else
1132+
theGroup = new QgsLegendGroup( this, name );
1133+
}
11171134

11181135
//set the checkbox of the legend group to the right state
11191136
blockSignals( true );

src/app/legend/qgslegend.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ class QgsLegend : public QTreeWidget
193193
/**Returns a layers check state*/
194194
Qt::CheckState layerCheckState( QgsMapLayer * layer );
195195

196-
void addEmbeddedGroup( const QString& groupName, const QString& projectFilePath, QgsLegendItem* parent = 0 );
196+
/**Add group from other project file. Returns a pointer to the new group in case of success or 0 in case of error*/
197+
QgsLegendGroup* addEmbeddedGroup( const QString& groupName, const QString& projectFilePath, QgsLegendItem* parent = 0 );
197198

198199
public slots:
199200

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

372-
// Groups defined in other project files
373+
/** Groups defined in other project files.
374+
Key: group name, value: absolute path to project file*/
373375
QHash< QString, QString > mEmbeddedGroups;
374376

375377
/** Hide the line that indicates insertion position */

src/app/qgisapp.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
#include "qgscustomization.h"
114114
#include "qgscustomprojectiondialog.h"
115115
#include "qgsdatasourceuri.h"
116+
#include "qgsembedlayerdialog.h"
116117
#include "qgsencodingfiledialog.h"
117118
#include "qgsexception.h"
118119
#include "qgsfeature.h"
@@ -5061,9 +5062,32 @@ void QgisApp::embedLayers()
50615062
QList< QPair< QgsVectorLayer*, QDomElement > > vectorLayerList;
50625063
QgsProject::instance()->createEmbeddedLayer( id, filepath, brokenNodes, vectorLayerList );*/
50635064

5064-
QString filepath="/home/marco/geodaten/projekte/rasters.qgs";
5065+
/*QString filepath="/home/marco/geodaten/projekte/rasters.qgs";
50655066
QString groupname="Karten";
5066-
mMapLegend->addEmbeddedGroup( groupname, filepath );
5067+
mMapLegend->addEmbeddedGroup( groupname, filepath );*/
5068+
5069+
QgsEmbedLayerDialog d;
5070+
if( d.exec() == QDialog::Accepted )
5071+
{
5072+
//groups
5073+
QList< QPair < QString, QString > > groups = d.embeddedGroups();
5074+
QList< QPair < QString, QString > >::const_iterator groupIt = groups.constBegin();
5075+
for(; groupIt != groups.constEnd(); ++groupIt )
5076+
{
5077+
mMapLegend->addEmbeddedGroup( groupIt->first, groupIt->second );
5078+
}
5079+
5080+
//layers
5081+
QList<QDomNode> brokenNodes;
5082+
QList< QPair< QgsVectorLayer*, QDomElement > > vectorLayerList;
5083+
5084+
QList< QPair < QString, QString > > layers = d.embeddedLayers();
5085+
QList< QPair < QString, QString > >::const_iterator layerIt = layers.constBegin();
5086+
for(; layerIt != layers.constEnd(); ++layerIt )
5087+
{
5088+
QgsProject::instance()->createEmbeddedLayer( layerIt->first, layerIt->second, brokenNodes, vectorLayerList );
5089+
}
5090+
}
50675091
}
50685092

50695093
void QgisApp::setExtent( QgsRectangle theRect )

src/app/qgsembedlayerdialog.cpp

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
#include "qgsembedlayerdialog.h"
2+
#include <QDomDocument>
3+
#include <QFileDialog>
4+
5+
QgsEmbedLayerDialog::QgsEmbedLayerDialog( QWidget * parent, Qt::WindowFlags f ): QDialog( parent, f )
6+
{
7+
setupUi( this );
8+
}
9+
10+
QgsEmbedLayerDialog::~QgsEmbedLayerDialog()
11+
{
12+
}
13+
14+
QList< QPair < QString, QString > > QgsEmbedLayerDialog::embeddedGroups() const
15+
{
16+
QList< QPair < QString, QString > > result;
17+
18+
QList<QTreeWidgetItem*> items = mTreeWidget->selectedItems();
19+
QList<QTreeWidgetItem*>::iterator itemIt = items.begin();
20+
for(; itemIt != items.end(); ++itemIt )
21+
{
22+
if( (*itemIt)->data(0, Qt::UserRole).toString() == "group" )
23+
{
24+
result.push_back( qMakePair( (*itemIt)->text( 0 ), mProjectFileLineEdit->text() ) );
25+
}
26+
}
27+
28+
return result;
29+
}
30+
31+
QList< QPair < QString, QString > > QgsEmbedLayerDialog::embeddedLayers() const
32+
{
33+
QList< QPair < QString, QString > > result;
34+
35+
QList<QTreeWidgetItem*> items = mTreeWidget->selectedItems();
36+
QList<QTreeWidgetItem*>::iterator itemIt = items.begin();
37+
for(; itemIt != items.end(); ++itemIt )
38+
{
39+
if( (*itemIt)->data(0, Qt::UserRole).toString() == "layer" )
40+
{
41+
result.push_back( qMakePair( (*itemIt)->data(0, Qt::UserRole + 1).toString(), mProjectFileLineEdit->text() ) );
42+
}
43+
}
44+
return result;
45+
}
46+
47+
void QgsEmbedLayerDialog::on_mBrowseFileToolButton_clicked()
48+
{
49+
QString projectFile = QFileDialog::getOpenFileName( 0, tr("Select project file"), "",tr("QGIS project files (*.qgs)") );
50+
if( !projectFile.isEmpty() )
51+
{
52+
mProjectFileLineEdit->setText( projectFile );
53+
}
54+
changeProjectFile();
55+
}
56+
57+
void QgsEmbedLayerDialog::on_mProjectFileLineEdit_editingFinished()
58+
{
59+
changeProjectFile();
60+
}
61+
62+
void QgsEmbedLayerDialog::changeProjectFile()
63+
{
64+
mTreeWidget->clear();
65+
QFile projectFile( mProjectFileLineEdit->text() );
66+
if( !projectFile.exists() )
67+
{
68+
return;
69+
}
70+
71+
//parse project file and fill tree
72+
if( !projectFile.open( QIODevice::ReadOnly ) )
73+
{
74+
return;
75+
}
76+
77+
QDomDocument projectDom;
78+
if( !projectDom.setContent( &projectFile ) )
79+
{
80+
return;
81+
}
82+
83+
QDomElement legendElem = projectDom.documentElement().firstChildElement("legend");
84+
if( legendElem.isNull() )
85+
{
86+
return;
87+
}
88+
89+
QDomNodeList legendChildren = legendElem.childNodes();
90+
QDomElement currentChildElem;
91+
92+
for( int i = 0; i < legendChildren.size(); ++i )
93+
{
94+
currentChildElem = legendChildren.at( i ).toElement();
95+
if( currentChildElem.tagName() == "legendlayer" )
96+
{
97+
addLegendLayerToTreeWidget( currentChildElem );
98+
}
99+
else if( currentChildElem.tagName() == "legendgroup" )
100+
{
101+
addLegendGroupToTreeWidget( currentChildElem );
102+
}
103+
}
104+
}
105+
106+
void QgsEmbedLayerDialog::addLegendGroupToTreeWidget( const QDomElement& groupElem, QTreeWidgetItem* parent )
107+
{
108+
QDomNodeList groupChildren = groupElem.childNodes();
109+
QDomElement currentChildElem;
110+
111+
QTreeWidgetItem* groupItem = 0;
112+
if( !parent )
113+
{
114+
groupItem = new QTreeWidgetItem( mTreeWidget );
115+
}
116+
else
117+
{
118+
groupItem = new QTreeWidgetItem( parent );
119+
}
120+
groupItem->setText( 0, groupElem.attribute("name") );
121+
groupItem->setData( 0, Qt::UserRole, "group" );
122+
123+
for( int i = 0; i < groupChildren.size(); ++i )
124+
{
125+
currentChildElem = groupChildren.at( i ).toElement();
126+
if( currentChildElem.tagName() == "legendlayer" )
127+
{
128+
addLegendLayerToTreeWidget( currentChildElem, groupItem );
129+
}
130+
else if( currentChildElem.tagName() == "legendgroup" )
131+
{
132+
addLegendGroupToTreeWidget( currentChildElem, groupItem );
133+
}
134+
}
135+
}
136+
137+
void QgsEmbedLayerDialog::addLegendLayerToTreeWidget( const QDomElement& layerElem, QTreeWidgetItem* parent )
138+
{
139+
QTreeWidgetItem* item = 0;
140+
if( parent )
141+
{
142+
item = new QTreeWidgetItem( parent );
143+
}
144+
else
145+
{
146+
item = new QTreeWidgetItem( mTreeWidget );
147+
}
148+
item->setText( 0, layerElem.attribute("name") );
149+
item->setData( 0, Qt::UserRole, "layer" );
150+
item->setData( 0, Qt::UserRole + 1, layerElem.firstChildElement("filegroup").firstChildElement("legendlayerfile").attribute("layerid") );
151+
}
152+
153+
void QgsEmbedLayerDialog::on_mTreeWidget_itemSelectionChanged()
154+
{
155+
mTreeWidget->blockSignals( true );
156+
QList<QTreeWidgetItem*> items = mTreeWidget->selectedItems();
157+
QList<QTreeWidgetItem*>::iterator itemIt = items.begin();
158+
for(; itemIt != items.end(); ++itemIt )
159+
{
160+
//deselect children recursively
161+
unselectChildren( *itemIt );
162+
}
163+
mTreeWidget->blockSignals( false );
164+
}
165+
166+
void QgsEmbedLayerDialog::unselectChildren( QTreeWidgetItem* item )
167+
{
168+
if( !item )
169+
{
170+
return;
171+
}
172+
173+
QTreeWidgetItem* currentChild = 0;
174+
for( int i = 0; i < item->childCount(); ++i )
175+
{
176+
currentChild = item->child( i );
177+
currentChild->setSelected( false );
178+
unselectChildren( currentChild );
179+
}
180+
}
181+
182+

0 commit comments

Comments
 (0)