Skip to content

Commit fa9e3fe

Browse files
author
stevehalasz
committedOct 29, 2004
* Write layers to projects files in the proper order by iterating over the
zOrder in the map canvas. Fixes bug #1054332. * Remove the <zorder> tag from the dtd. It is superfluous. git-svn-id: http://svn.osgeo.org/qgis/trunk@2179 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 53ec7a8 commit fa9e3fe

File tree

5 files changed

+134
-130
lines changed

5 files changed

+134
-130
lines changed
 

‎qgis.dtd

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@
2626
-- General Map Layer Properties
2727
-- (apply to both vector and raster)
2828
-->
29-
<!ELEMENT maplayer (layername, datasource, zorder, (singlesymbol|singlemarker|graduatedsymbol|continuoussymbol|graduatedmarker|rasterproperties) >
29+
<!ELEMENT maplayer (layername, datasource, (singlesymbol|singlemarker|graduatedsymbol|continuoussymbol|graduatedmarker|rasterproperties) >
3030
<!ELEMENT layername (#PCDATA) >
3131
<!ELEMENT datasource (#PCDATA) >
32-
<!ELEMENT zorder (#PCDATA) >
3332
<!-- Attribute Lists -->
3433
<!--Raster : flag indicating whether the layer should be represented in overview or not -->
3534
<!ELEMENT showInOverviewFlag>

‎src/qgsmapcanvas.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
// #include <qguardedptr.h>
3131
// #endif
3232

33+
#ifndef QDOM_H
34+
#include <qdom.h>
35+
#endif
36+
3337
#ifndef QLISTVIEW_H
3438
#include <qlistview.h>
3539
#endif
@@ -1748,3 +1752,51 @@ void QgsMapCanvas::connectNotify( const char * signal )
17481752
#endif
17491753
} // QgsMapCanvas::connectNotify( const char * signal )
17501754

1755+
1756+
bool QgsMapCanvas::writeXML(QDomNode & layerNode, QDomDocument & doc)
1757+
{
1758+
// Write current view extents
1759+
QDomElement extentNode = doc.createElement("extent");
1760+
layerNode.appendChild(extentNode);
1761+
1762+
QDomElement xMin = doc.createElement("xmin");
1763+
QDomElement yMin = doc.createElement("ymin");
1764+
QDomElement xMax = doc.createElement("xmax");
1765+
QDomElement yMax = doc.createElement("ymax");
1766+
1767+
QDomText xMinText = doc.createTextNode(QString::number(mCanvasProperties->currentExtent.xMin(), 'f'));
1768+
QDomText yMinText = doc.createTextNode(QString::number(mCanvasProperties->currentExtent.yMin(), 'f'));
1769+
QDomText xMaxText = doc.createTextNode(QString::number(mCanvasProperties->currentExtent.xMax(), 'f'));
1770+
QDomText yMaxText = doc.createTextNode(QString::number(mCanvasProperties->currentExtent.yMax(), 'f'));
1771+
1772+
xMin.appendChild(xMinText);
1773+
yMin.appendChild(yMinText);
1774+
xMax.appendChild(xMaxText);
1775+
yMax.appendChild(yMaxText);
1776+
1777+
extentNode.appendChild(xMin);
1778+
extentNode.appendChild(yMin);
1779+
extentNode.appendChild(xMax);
1780+
extentNode.appendChild(yMax);
1781+
1782+
// Iterate over layers in zOrder
1783+
// Call writeXML() on each
1784+
QDomElement projectLayersNode = doc.createElement("projectlayers");
1785+
projectLayersNode.setAttribute("layercount", mCanvasProperties->layers.size());
1786+
1787+
std::list < QString >::iterator li = mCanvasProperties->zOrder.begin();
1788+
while (li != mCanvasProperties->zOrder.end())
1789+
{
1790+
QgsMapLayer *ml = mCanvasProperties->layers[*li];
1791+
1792+
if (ml)
1793+
{
1794+
ml->writeXML(projectLayersNode, doc);
1795+
}
1796+
li++;
1797+
}
1798+
1799+
layerNode.appendChild(projectLayersNode);
1800+
1801+
return true;
1802+
}

‎src/qgsmapcanvas.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424

2525
// double sentinals to get round gcc 3.3.3 pre-processor bug
2626

27+
#ifndef QDOM_H
28+
#include <qdom.h>
29+
#endif
30+
2731
#ifndef QWIDGET_H
2832
#include <qwidget.h>
2933
#endif
@@ -158,6 +162,18 @@ class QgsMapCanvas : public QWidget
158162
//! Declare the legend class as a friend of the map canvas
159163
//friend class QgsLegend;
160164

165+
/** stores state in DOM node
166+
layerNode is DOM node corresponding to ``qgis'' tag
167+
168+
The DOM node corresponds to a DOM document project file XML element to be
169+
written by QgsProject.
170+
171+
Invoked by QgsProject::write().
172+
173+
returns true if successful
174+
*/
175+
bool writeXML( QDomNode & layerNode, QDomDocument & doc );
176+
161177
public slots:
162178

163179
/*! Adds a layer to the map canvas.

‎src/qgsmaplayer.cpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -241,19 +241,8 @@ bool QgsMapLayer::writeXML( QDomNode & layer_node, QDomDocument & document )
241241
maplayer.appendChild( layerName );
242242

243243
// zorder
244-
245-
// XXX Where do I get this information? Since it's not map layer
246-
// XXX specific, and specific to map canvas, shouldn't that be in
247-
// XXX QgsMapCanvas::writeXML() instead? For that matter, if we read and
248-
// XXX write layers in a known order, then we don't need to store a z order,
249-
// XXX right?
250-
251-
QDomElement zOrder = document.createElement( "zorder" );
252-
QDomText zOrderText = document.createTextNode( "0" ); // XXX hard-coded to zero
253-
zOrder.appendChild( zOrderText );
254-
255-
maplayer.appendChild( zOrder );
256-
244+
// This is no longer stored in the project file. It is superflous since the layers
245+
// are written and read in the proper order.
257246

258247
// now append layer node to map layer node
259248

‎src/qgsproject.cpp

Lines changed: 63 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ _getExtents( QDomDocument const & doc, QgsRect & aoi )
220220

221221

222222
/**
223-
Get the project title
223+
Get the project map units
224224
225225
XML in file has this form:
226226
<units>feet</units>
@@ -363,6 +363,44 @@ _findQgisApp()
363363

364364

365365

366+
/**
367+
locate a qgsMapCanvas object
368+
*/
369+
static QgsMapCanvas *_findMapCanvas(QString const &canonicalMapCanvasName)
370+
{
371+
QgsMapCanvas *theMapCanvas;
372+
373+
QWidgetList *list = QApplication::topLevelWidgets();
374+
QWidgetListIt it(*list); // iterate over the widgets
375+
QWidget *w;
376+
377+
while ((w = it.current()) != 0)
378+
{ // for each top level widget...
379+
++it;
380+
theMapCanvas = dynamic_cast < QgsMapCanvas * >(w->child(canonicalMapCanvasName, 0, true));
381+
382+
if (theMapCanvas)
383+
{
384+
break;
385+
}
386+
}
387+
delete list; // delete the list, not the widgets
388+
389+
if (theMapCanvas)
390+
{
391+
return theMapCanvas;
392+
}
393+
else
394+
{
395+
qDebug("Unable to find canvas widget " + canonicalMapCanvasName);
396+
397+
return 0x0; // XXX some sort of error value? Exception?
398+
}
399+
400+
} // _findMapCanvas
401+
402+
403+
366404
/**
367405
Read map layers from project file
368406
@@ -408,6 +446,8 @@ static
408446
bool
409447
_getMapLayers( QDomDocument const & doc )
410448
{
449+
// Layer order is implicit in the order they are stored in the project file
450+
411451
QDomNodeList nl = doc.elementsByTagName("maplayer");
412452

413453
// XXX what is this used for? QString layerCount( QString::number(nl.count()) );
@@ -469,9 +509,6 @@ _getMapLayers( QDomDocument const & doc )
469509
// }
470510
// }
471511

472-
// XXX set z order here? Or in readXML()? Leaning to latter.
473-
// XXX Or, how about Z order being implicit in order of layer
474-
// XXX information stored in project file?
475512
}
476513

477514
return true;
@@ -491,24 +528,8 @@ static
491528
void
492529
_setCanvasExtent( QString const & canonicalMapCanvasName, QgsRect const & newExtent )
493530
{
494-
// first find the canonical map canvas
495-
496-
QgsMapCanvas * theMapCanvas;
497-
498-
QWidgetList * list = QApplication::topLevelWidgets();
499-
QWidgetListIt it( *list ); // iterate over the widgets
500-
QWidget * w;
501-
502-
while ( (w=it.current()) != 0 )
503-
{ // for each top level widget...
504-
++it;
505-
theMapCanvas = dynamic_cast<QgsMapCanvas*>(w->child( canonicalMapCanvasName, 0, true ));
506-
507-
if ( theMapCanvas )
508-
{ break; }
509-
}
510-
delete list; // delete the list, not the widgets
511-
531+
// first find the canonical map canvas
532+
QgsMapCanvas *theMapCanvas = _findMapCanvas(canonicalMapCanvasName);
512533

513534
if( ! theMapCanvas )
514535
{
@@ -544,24 +565,8 @@ QgsRect _getFullExtent( QString const & canonicalMapCanvasName )
544565
{
545566
// XXX since this is a cut-n-paste from above, maybe generalize to a
546567
// XXX separate function?
547-
// first find the canonical map canvas
548-
549-
QgsMapCanvas * theMapCanvas;
550-
551-
QWidgetList * list = QApplication::topLevelWidgets();
552-
QWidgetListIt it( *list ); // iterate over the widgets
553-
QWidget * w;
554-
555-
while ( (w=it.current()) != 0 )
556-
{ // for each top level widget...
557-
++it;
558-
theMapCanvas = dynamic_cast<QgsMapCanvas*>(w->child( canonicalMapCanvasName, 0, true ));
559-
560-
if ( theMapCanvas )
561-
{ break; }
562-
}
563-
delete list; // delete the list, not the widgets
564-
568+
// first find the canonical map canvas
569+
QgsMapCanvas *theMapCanvas = _findMapCanvas(canonicalMapCanvasName);
565570

566571
if( ! theMapCanvas )
567572
{
@@ -596,24 +601,8 @@ QgsRect _getExtent( QString const & canonicalMapCanvasName )
596601
{
597602
// XXX since this is a cut-n-paste from above, maybe generalize to a
598603
// XXX separate function?
599-
// first find the canonical map canvas
600-
601-
QgsMapCanvas * theMapCanvas;
602-
603-
QWidgetList * list = QApplication::topLevelWidgets();
604-
QWidgetListIt it( *list ); // iterate over the widgets
605-
QWidget * w;
606-
607-
while ( (w=it.current()) != 0 )
608-
{ // for each top level widget...
609-
++it;
610-
theMapCanvas = dynamic_cast<QgsMapCanvas*>(w->child( canonicalMapCanvasName, 0, true ));
611-
612-
if ( theMapCanvas )
613-
{ break; }
614-
}
615-
delete list; // delete the list, not the widgets
616-
604+
// first find the canonical map canvas
605+
QgsMapCanvas *theMapCanvas = _findMapCanvas(canonicalMapCanvasName);
617606

618607
if( ! theMapCanvas )
619608
{
@@ -706,9 +695,6 @@ QgsProject::read( )
706695
return false;
707696
}
708697

709-
// XXX insert code for handling z order
710-
711-
712698
// restore the canvas' area of interest
713699

714700
// restor the area of interest, or extent
@@ -785,25 +771,25 @@ QgsProject::write( )
785771

786772
QDomDocumentType documentType = DOMImplementation.createDocumentType("qgis","http://mrcc.com/qgis.dtd","SYSTEM");
787773
std::auto_ptr<QDomDocument> doc =
788-
std::auto_ptr<QDomDocument>( new QDomDocument( documentType ) );
774+
std::auto_ptr<QDomDocument>( new QDomDocument( documentType ) );
789775

790776

791-
QDomElement qgis = doc->createElement( "qgis" );
792-
qgis.setAttribute( "projectname", title() );
777+
QDomElement qgisNode = doc->createElement( "qgis" );
778+
qgisNode.setAttribute( "projectname", title() );
793779

794-
doc->appendChild( qgis );
780+
doc->appendChild( qgisNode );
795781

796782
// title
797783
QDomElement titleNode = doc->createElement( "title" );
798-
qgis.appendChild( titleNode );
784+
qgisNode.appendChild( titleNode );
799785

800786
QDomText titleText = doc->createTextNode( title() ); // XXX why have title TWICE?
801787
titleNode.appendChild( titleText );
802788

803789
// units
804790

805791
QDomElement unitsNode = doc->createElement( "units" );
806-
qgis.appendChild( unitsNode );
792+
qgisNode.appendChild( unitsNode );
807793

808794
QString unitsString;
809795

@@ -825,52 +811,17 @@ QgsProject::write( )
825811

826812
QDomText unitsText = doc->createTextNode( unitsString );
827813
unitsNode.appendChild( unitsText );
828-
829-
// extent
830-
831-
// XXX there should eventually be a QgsMapCanvas::writeXML() that does this
832-
QDomElement extentNode = doc->createElement( "extent" );
833-
qgis.appendChild( extentNode );
834-
835-
QDomElement xMin = doc->createElement( "xmin" );
836-
QDomElement yMin = doc->createElement( "ymin" );
837-
QDomElement xMax = doc->createElement( "xmax" );
838-
QDomElement yMax = doc->createElement( "ymax" );
839-
840-
QgsRect mapCanvasExtent = _getExtent( "theMapCanvas" );
841-
QDomText xMinText = doc->createTextNode( QString::number(mapCanvasExtent.xMin(),'f') );
842-
QDomText yMinText = doc->createTextNode( QString::number(mapCanvasExtent.yMin(),'f') );
843-
QDomText xMaxText = doc->createTextNode( QString::number(mapCanvasExtent.xMax(),'f') );
844-
QDomText yMaxText = doc->createTextNode( QString::number(mapCanvasExtent.yMax(),'f') );
845-
846-
xMin.appendChild( xMinText );
847-
yMin.appendChild( yMinText );
848-
xMax.appendChild( xMaxText );
849-
yMax.appendChild( yMaxText );
850-
851-
extentNode.appendChild( xMin );
852-
extentNode.appendChild( yMin );
853-
extentNode.appendChild( xMax );
854-
extentNode.appendChild( yMax );
855-
856-
// layers
857-
858-
// XXX QgsMapLayerRegistry should have a writeXML(), which then calls
859-
// XXX QgsMapLayer::writeXML()
860-
QDomElement projectLayersNode = doc->createElement( "projectlayers" );
861-
projectLayersNode.setAttribute( "layercount",
862-
QgsMapLayerRegistry::instance()->mapLayers().size() );
863-
864-
qgis.appendChild( projectLayersNode );
865-
866-
867-
for ( std::map<QString,QgsMapLayer*>::iterator i =
868-
QgsMapLayerRegistry::instance()->mapLayers().begin();
869-
i != QgsMapLayerRegistry::instance()->mapLayers().end();
870-
i++ )
814+
815+
// extents and layers info are written by the map canvas
816+
// find the canonical map canvas
817+
QgsMapCanvas *theMapCanvas = _findMapCanvas( "theMapCanvas" );
818+
theMapCanvas->writeXML(qgisNode, *doc);
819+
820+
if( ! theMapCanvas )
871821
{
872-
if ( i->first )
873-
{ i->second->writeXML( projectLayersNode, *doc ); }
822+
qDebug( "Unable to find canvas widget theMapCanvas" );
823+
824+
return false; // XXX some sort of error value? Exception?
874825
}
875826

876827
doc->normalize(); // XXX I'm not entirely sure what this does
@@ -900,6 +851,3 @@ QgsProject::properties()
900851
{
901852
return imp_->properties_;
902853
} // QgsProject::properties
903-
904-
905-

0 commit comments

Comments
 (0)
Please sign in to comment.