[QGIS Commit] r9016 -
branches/advanced_printing_branch/src/app/composer
svn_qgis at osgeo.org
svn_qgis at osgeo.org
Thu Aug 7 03:27:56 EDT 2008
Author: mhugent
Date: 2008-08-07 03:27:56 -0400 (Thu, 07 Aug 2008)
New Revision: 9016
Modified:
branches/advanced_printing_branch/src/app/composer/qgscomposer.cpp
branches/advanced_printing_branch/src/app/composer/qgscomposeritem.cpp
branches/advanced_printing_branch/src/app/composer/qgscomposerlegend.cpp
branches/advanced_printing_branch/src/app/composer/qgslegendmodel.cpp
branches/advanced_printing_branch/src/app/composer/qgslegendmodel.h
Log:
save and restore composer legend from xml file
Modified: branches/advanced_printing_branch/src/app/composer/qgscomposer.cpp
===================================================================
--- branches/advanced_printing_branch/src/app/composer/qgscomposer.cpp 2008-08-07 06:57:36 UTC (rev 9015)
+++ branches/advanced_printing_branch/src/app/composer/qgscomposer.cpp 2008-08-07 07:27:56 UTC (rev 9016)
@@ -1219,7 +1219,6 @@
newLabel->readXML(currentComposerLabelElem, doc);
addComposerLabel(newLabel);
mComposition->addItem(newLabel);
- newLabel->setZValue(60);
mComposition->update();
}
@@ -1232,7 +1231,6 @@
newMap->readXML(currentComposerMapElem, doc);
addComposerMap(newMap);
mComposition->addItem(newMap);
- newMap->setZValue(50);
mComposition->update();
}
@@ -1245,9 +1243,20 @@
newScaleBar->readXML(currentScaleBarElem, doc);
addComposerScaleBar(newScaleBar);
mComposition->addItem(newScaleBar);
- newScaleBar->setZValue(60);
mComposition->update();
}
+
+ //composer legends
+ QDomNodeList composerLegendList = composerElem.elementsByTagName("ComposerLegend");
+ for(int i = 0; i < composerLegendList.size(); ++i)
+ {
+ QDomElement currentLegendElem = composerLegendList.at(i).toElement();
+ QgsComposerLegend* newLegend = new QgsComposerLegend(mComposition);
+ newLegend->readXML(currentLegendElem, doc);
+ addComposerLegend(newLegend);
+ mComposition->addItem(newLegend);
+ mComposition->update();
+ }
}
mView->setComposition(mComposition);
@@ -1293,7 +1302,6 @@
return;
}
- //todo: create a composer legend widget
QgsComposerLegendWidget* lWidget = new QgsComposerLegendWidget(legend);
mItemWidgetMap.insert(legend, lWidget);
}
Modified: branches/advanced_printing_branch/src/app/composer/qgscomposeritem.cpp
===================================================================
--- branches/advanced_printing_branch/src/app/composer/qgscomposeritem.cpp 2008-08-07 06:57:36 UTC (rev 9015)
+++ branches/advanced_printing_branch/src/app/composer/qgscomposeritem.cpp 2008-08-07 07:27:56 UTC (rev 9016)
@@ -83,6 +83,7 @@
composerItemElem.setAttribute("y", transform().dy());
composerItemElem.setAttribute("width", rect().width());
composerItemElem.setAttribute("height", rect().height());
+ composerItemElem.setAttribute("zValue", QString::number(zValue()));
itemElem.appendChild(composerItemElem);
@@ -122,6 +123,7 @@
}
setSceneRect(QRectF(x, y, width, height));
+ setZValue(itemElem.attribute("zValue").toDouble());
return true;
}
Modified: branches/advanced_printing_branch/src/app/composer/qgscomposerlegend.cpp
===================================================================
--- branches/advanced_printing_branch/src/app/composer/qgscomposerlegend.cpp 2008-08-07 06:57:36 UTC (rev 9015)
+++ branches/advanced_printing_branch/src/app/composer/qgscomposerlegend.cpp 2008-08-07 07:27:56 UTC (rev 9016)
@@ -19,6 +19,8 @@
#include "qgsmaplayer.h"
#include "qgsmaplayerregistry.h"
#include "qgssymbol.h"
+#include <QDomDocument>
+#include <QDomElement>
#include <QPainter>
QgsComposerLegend::QgsComposerLegend(QgsComposition* composition): QgsComposerItem(composition), mTitle(QObject::tr("Legend")), mBoxSpace(2), mLayerSpace(3), mSymbolSpace(2), mIconLabelSpace(2)
@@ -198,6 +200,7 @@
{
symbolIcon.paint(p, currentXCoord, currentYCoord, mSymbolWidth, mSymbolHeight);
}
+ currentXCoord += mSymbolWidth;
currentXCoord += mIconLabelSpace;
}
@@ -334,10 +337,84 @@
bool QgsComposerLegend::writeXML(QDomElement& elem, QDomDocument & doc)
{
- return true; //soon...
+ if(elem.isNull())
+ {
+ return false;
+ }
+
+ QDomElement composerLegendElem = doc.createElement("ComposerLegend");
+
+ //write general properties
+ composerLegendElem.setAttribute("title", mTitle);
+ composerLegendElem.setAttribute("titleFont", mTitleFont.toString());
+ composerLegendElem.setAttribute("layerFont", mLayerFont.toString());
+ composerLegendElem.setAttribute("itemFont", mItemFont.toString());
+ composerLegendElem.setAttribute("boxSpace", QString::number(mBoxSpace));
+ composerLegendElem.setAttribute("layerSpace", QString::number(mLayerSpace));
+ composerLegendElem.setAttribute("symbolSpace", QString::number(mSymbolSpace));
+ composerLegendElem.setAttribute("iconLabelSpace", QString::number(mIconLabelSpace));
+ composerLegendElem.setAttribute("symbolWidth", mSymbolWidth);
+ composerLegendElem.setAttribute("symbolHeight", mSymbolHeight);
+
+ //write model properties
+ mLegendModel.writeXML(composerLegendElem, doc);
+
+ elem.appendChild(composerLegendElem);
+ return _writeXML(composerLegendElem, doc);
}
bool QgsComposerLegend::readXML(const QDomElement& itemElem, const QDomDocument& doc)
{
- return false; //soon...
+ if(itemElem.isNull())
+ {
+ return false;
+ }
+
+ //read general properties
+ mTitle = itemElem.attribute("title");
+ //title font
+ QString titleFontString = itemElem.attribute("titleFont");
+ if(!titleFontString.isEmpty())
+ {
+ mTitleFont.fromString(titleFontString);
+ }
+ //layer font
+ QString layerFontString = itemElem.attribute("layerFont");
+ if(!layerFontString.isEmpty())
+ {
+ mLayerFont.fromString(layerFontString);
+ }
+ //item font
+ QString itemFontString = itemElem.attribute("itemFont");
+ if(!itemFontString.isEmpty())
+ {
+ mItemFont.fromString(itemFontString);
+ }
+
+ //spaces
+ mBoxSpace = itemElem.attribute("boxSpace", "2.0").toDouble();
+ mLayerSpace = itemElem.attribute("layerSpace", "3.0").toDouble();
+ mSymbolSpace = itemElem.attribute("symbolSpace", "2.0").toDouble();
+ mIconLabelSpace = itemElem.attribute("iconLabelSpace", "2.0").toDouble();
+ mSymbolWidth = itemElem.attribute("symbolWidth", "7.0").toDouble();
+ mSymbolHeight = itemElem.attribute("symbolHeight", "14.0").toDouble();
+
+ //read model properties
+ QDomNodeList modelNodeList = itemElem.elementsByTagName("Model");
+ if(modelNodeList.size() > 0)
+ {
+ QDomElement modelElem = modelNodeList.at(0).toElement();
+ mLegendModel.clear();
+ mLegendModel.readXML(modelElem, doc);
+ }
+
+ //restore general composer item properties
+ QDomNodeList composerItemList = itemElem.elementsByTagName("ComposerItem");
+ if(composerItemList.size() > 0)
+ {
+ QDomElement composerItemElem = composerItemList.at(0).toElement();
+ _readXML(composerItemElem, doc);
+ }
+
+ return true;
}
Modified: branches/advanced_printing_branch/src/app/composer/qgslegendmodel.cpp
===================================================================
--- branches/advanced_printing_branch/src/app/composer/qgslegendmodel.cpp 2008-08-07 06:57:36 UTC (rev 9015)
+++ branches/advanced_printing_branch/src/app/composer/qgslegendmodel.cpp 2008-08-07 07:27:56 UTC (rev 9016)
@@ -22,6 +22,8 @@
#include "qgsrenderer.h"
#include "qgssymbol.h"
#include "qgsvectorlayer.h"
+#include <QDomDocument>
+#include <QDomElement>
QgsLegendModel::QgsLegendModel(): QStandardItemModel()
{
@@ -33,11 +35,7 @@
QgsLegendModel::~QgsLegendModel()
{
- QSet<QgsSymbol*>::iterator it = mSymbols.begin();
- for(; it != mSymbols.end(); ++it)
- {
- delete *it;
- }
+ removeAllSymbols();
}
void QgsLegendModel::setLayerSet(const QStringList& layerIds)
@@ -190,10 +188,7 @@
{
delete (*it); //very unlikely
}
- else
- {
- mSymbols.insert(s);
- }
+ mSymbols.insert(s);
}
void QgsLegendModel::removeSymbol(QgsSymbol* s)
@@ -201,6 +196,16 @@
mSymbols.remove(s);
}
+void QgsLegendModel::removeAllSymbols()
+{
+ QSet<QgsSymbol*>::iterator it = mSymbols.begin();
+ for(; it != mSymbols.end(); ++it)
+ {
+ delete *it;
+ }
+ mSymbols.clear();
+}
+
void QgsLegendModel::updateLayerEntries(const QStringList& newLayerIds)
{
if(!invisibleRootItem())
@@ -270,3 +275,161 @@
}
}
}
+
+bool QgsLegendModel::writeXML(QDomElement& composerLegendElem, QDomDocument& doc)
+{
+ if(composerLegendElem.isNull())
+ {
+ return false;
+ }
+
+ QDomElement legendModelElem = doc.createElement("Model");
+
+ //iterate over all items...
+ QStandardItem* currentLayerItem = 0;
+ QStandardItem* currentClassificationItem = 0;
+ int numRootItems = rowCount();
+
+ for(int i = 0; i < numRootItems; ++i)
+ {
+ currentLayerItem = item(i);
+ QDomElement newLayerItem = doc.createElement("LayerItem");
+ newLayerItem.setAttribute("layerId", currentLayerItem->data().toString());
+ newLayerItem.setAttribute("text", currentLayerItem->text());
+
+ //add layer/classification items
+ int numClassItems = currentLayerItem->rowCount();
+ for(int j = 0; j < numClassItems; ++j)
+ {
+ currentClassificationItem = currentLayerItem->child(j);
+
+ //store text and QgsSymbol for vector classification items
+ QVariant symbolVariant = currentClassificationItem->data();
+ QgsSymbol* symbol = 0;
+ if(symbolVariant.canConvert<void*>())
+ {
+ void* symbolData = symbolVariant.value<void*>();
+ symbol = (QgsSymbol*)(symbolData);
+ }
+ if(symbol)
+ {
+ QDomElement vectorClassElem = doc.createElement("VectorClassificationItem");
+ vectorClassElem.setAttribute("text", currentClassificationItem->text());
+ symbol->writeXML(vectorClassElem, doc);
+ newLayerItem.appendChild(vectorClassElem);
+ continue;
+ }
+
+ //a text item
+ if(currentClassificationItem->icon().isNull())
+ {
+ QDomElement textItemElem = doc.createElement("TextItem");
+ textItemElem.setAttribute("text", currentClassificationItem->text());
+ newLayerItem.appendChild(textItemElem);
+ }
+
+ //else it can only be a raster item
+ QDomElement rasterClassElem = doc.createElement("RasterItem");
+ rasterClassElem.setAttribute("text", currentClassificationItem->text());
+ //storing the layer id also in the raster item makes parsing easier
+ rasterClassElem.setAttribute("layerId", currentLayerItem->data().toString());
+ newLayerItem.appendChild(rasterClassElem);
+ }
+
+ legendModelElem.appendChild(newLayerItem);
+ }
+
+ composerLegendElem.appendChild(legendModelElem);
+ return true;
+}
+
+bool QgsLegendModel::readXML(const QDomElement& legendModelElem, const QDomDocument& doc)
+{
+ if(legendModelElem.isNull())
+ {
+ return false;
+ }
+
+ //delete all stored symbols first
+ removeAllSymbols();
+
+ //iterate over layer items
+ QDomNodeList layerItemList = legendModelElem.elementsByTagName("LayerItem");
+ QgsMapLayer* currentLayer = 0; //store current layer to get
+
+ for(int i = 0; i < layerItemList.size(); ++i)
+ {
+ QDomElement layerItemElem = layerItemList.at(i).toElement();
+ QString layerId = layerItemElem.attribute("layerId");
+
+ QStandardItem* layerItem = new QStandardItem(layerItemElem.attribute("text"));
+
+ //set layer id as user data into the item
+ layerItem->setData(QVariant(layerId));
+ layerItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
+
+ currentLayer = QgsMapLayerRegistry::instance()->mapLayer(layerId);
+
+ //go through all children of layerItemElem
+ QDomElement currentChildElement = layerItemElem.firstChildElement();
+ while(!currentChildElement.isNull())
+ {
+ QStandardItem* childItem = new QStandardItem(currentChildElement.attribute("text"));
+ if(currentChildElement.tagName() == "RasterItem")
+ {
+ //get icon from current layer
+ QgsRasterLayer* rasterLayer = dynamic_cast<QgsRasterLayer*>(currentLayer);
+ if(rasterLayer)
+ {
+ childItem->setIcon(QIcon(rasterLayer->getLegendQPixmap(true)));
+ }
+ layerItem->setChild(layerItem->rowCount(), 0, childItem);
+ }
+ else if(currentChildElement.tagName() == "VectorClassificationItem")
+ {
+ //read QgsSymbol from xml and get icon
+ QgsVectorLayer* vectorLayer = dynamic_cast<QgsVectorLayer*>(currentLayer);
+ if(vectorLayer)
+ {
+ //look for symbol
+ QDomNodeList symbolNodeList = currentChildElement.elementsByTagName("symbol");
+ if(symbolNodeList.size() > 0)
+ {
+ QgsSymbol* symbol = new QgsSymbol(vectorLayer->vectorType());
+ QDomNode symbolNode = symbolNodeList.at(0);
+ symbol->readXML(symbolNode);
+ childItem->setData(QVariant::fromValue((void*)symbol));
+
+ //add icon
+ switch(symbol->type())
+ {
+ case QGis::Point:
+ childItem->setIcon(QIcon(QPixmap::fromImage(symbol->getPointSymbolAsImage())));
+ break;
+ case QGis::Line:
+ childItem->setIcon(QIcon(QPixmap::fromImage(symbol->getLineSymbolAsImage())));
+ break;
+ case QGis::Polygon:
+ childItem->setIcon(QIcon(QPixmap::fromImage(symbol->getPolygonSymbolAsImage())));
+ break;
+ }
+ insertSymbol(symbol);
+ }
+ }
+ layerItem->setChild(layerItem->rowCount(), 0, childItem);
+ }
+ else if(currentChildElement.tagName() == "TextItem")
+ {
+ layerItem->setChild(layerItem->rowCount(), 0, childItem);
+ }
+ else //unknown tag name, don't add item
+ {
+ delete childItem;
+ }
+
+ currentChildElement = currentChildElement.nextSiblingElement();
+ }
+
+ invisibleRootItem()->setChild (invisibleRootItem()->rowCount(), layerItem);
+ }
+}
Modified: branches/advanced_printing_branch/src/app/composer/qgslegendmodel.h
===================================================================
--- branches/advanced_printing_branch/src/app/composer/qgslegendmodel.h 2008-08-07 06:57:36 UTC (rev 9015)
+++ branches/advanced_printing_branch/src/app/composer/qgslegendmodel.h 2008-08-07 07:27:56 UTC (rev 9016)
@@ -22,6 +22,8 @@
#include <QStringList>
#include <QSet>
+class QDomDocument;
+class QDomElement;
class QgsMapLayer;
class QgsSymbol;
@@ -38,6 +40,9 @@
/**Apply added or removed layers to the model*/
void updateLayerEntries(const QStringList& newLayerIds);
+ bool writeXML(QDomElement& composerLegendElem, QDomDocument& doc);
+ bool readXML(const QDomElement& legendModelElem, const QDomDocument& doc);
+
public slots:
void removeLayer(const QString& layerId);
@@ -53,8 +58,12 @@
@return 0 in case of success*/
int addRasterLayerItem(QStandardItem* layerItem, QgsMapLayer* rlayer);
+ /**Insert a symbol into QgsLegendModel symbol storage*/
void insertSymbol(QgsSymbol* s);
+ /**Removes and deletes a symbol*/
void removeSymbol(QgsSymbol* s);
+ /**Removes and deletes all stored symbols*/
+ void removeAllSymbols();
/**Keep track of copied symbols to delete them if not used anymore*/
QSet<QgsSymbol*> mSymbols;
More information about the QGIS-commit
mailing list