Quantum GIS API Documentation  1.7.4
src/core/composer/qgscomposerlegend.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                          qgscomposerlegend.cpp  -  description
00003                          ---------------------
00004     begin                : June 2008
00005     copyright            : (C) 2008 by Marco Hugentobler
00006     email                : marco dot hugentobler at karto dot baug dot ethz dot ch
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include "qgscomposerlegend.h"
00019 #include "qgscomposerlegenditem.h"
00020 #include "qgscomposermap.h"
00021 #include "qgsmaplayer.h"
00022 #include "qgsmaplayerregistry.h"
00023 #include "qgsmaprenderer.h"
00024 #include "qgsrenderer.h" //for brush scaling
00025 #include "qgssymbol.h"
00026 #include "qgssymbolv2.h"
00027 #include <QDomDocument>
00028 #include <QDomElement>
00029 #include <QPainter>
00030 
00031 QgsComposerLegend::QgsComposerLegend( QgsComposition* composition )
00032     : QgsComposerItem( composition )
00033     , mTitle( tr( "Legend" ) )
00034     , mBoxSpace( 2 )
00035     , mLayerSpace( 2 )
00036     , mSymbolSpace( 2 )
00037     , mIconLabelSpace( 2 ), mComposerMap( 0 )
00038 {
00039   //QStringList idList = layerIdList();
00040   //mLegendModel.setLayerSet( idList );
00041 
00042   mTitleFont.setPointSizeF( 16.0 );
00043   mGroupFont.setPointSizeF( 14.0 );
00044   mLayerFont.setPointSizeF( 12.0 );
00045   mItemFont.setPointSizeF( 12.0 );
00046 
00047   mSymbolWidth = 7;
00048   mSymbolHeight = 4;
00049   adjustBoxSize();
00050 
00051   connect( &mLegendModel, SIGNAL( layersChanged() ), this, SLOT( synchronizeWithModel() ) );
00052 }
00053 
00054 QgsComposerLegend::QgsComposerLegend(): QgsComposerItem( 0 ), mComposerMap( 0 )
00055 {
00056 
00057 }
00058 
00059 QgsComposerLegend::~QgsComposerLegend()
00060 {
00061 
00062 }
00063 
00064 void QgsComposerLegend::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
00065 {
00066   paintAndDetermineSize( painter );
00067 }
00068 
00069 QSizeF QgsComposerLegend::paintAndDetermineSize( QPainter* painter )
00070 {
00071   QSizeF size;
00072   double maxXCoord = 0;
00073 
00074 
00075 
00076   //go through model...
00077   QStandardItem* rootItem = mLegendModel.invisibleRootItem();
00078   if ( !rootItem )
00079   {
00080     return size;
00081   }
00082 
00083 
00084   if ( painter )
00085   {
00086     painter->save();
00087     drawBackground( painter );
00088     painter->setPen( QPen( QColor( 0, 0, 0 ) ) );
00089   }
00090 
00091   int numLayerItems = rootItem->rowCount();
00092   QStandardItem* currentLayerItem = 0;
00093   double currentYCoordinate = mBoxSpace;
00094 
00095   //font metrics
00096 
00097   //draw title
00098   currentYCoordinate += fontAscentMillimeters( mTitleFont );
00099   if ( painter )
00100   {
00101     painter->setPen( QColor( 0, 0, 0 ) );
00102     drawText( painter, mBoxSpace, currentYCoordinate, mTitle, mTitleFont );
00103   }
00104 
00105   maxXCoord = 2 * mBoxSpace + textWidthMillimeters( mTitleFont, mTitle );
00106 
00107   double currentItemMaxX = 0; //maximum x-coordinate for current item
00108   for ( int i = 0; i < numLayerItems; ++i )
00109   {
00110     currentLayerItem = rootItem->child( i );
00111     QgsComposerLegendItem* currentLegendItem = dynamic_cast<QgsComposerLegendItem*>( currentLayerItem );
00112     if ( currentLegendItem )
00113     {
00114       QgsComposerLegendItem::ItemType type = currentLegendItem->itemType();
00115       if ( type == QgsComposerLegendItem::GroupItem )
00116       {
00117         drawGroupItem( painter, dynamic_cast<QgsComposerGroupItem*>( currentLegendItem ), currentYCoordinate, currentItemMaxX );
00118         maxXCoord = qMax( maxXCoord, currentItemMaxX );
00119       }
00120       else if ( type == QgsComposerLegendItem::LayerItem )
00121       {
00122         drawLayerItem( painter, dynamic_cast<QgsComposerLayerItem*>( currentLegendItem ), currentYCoordinate, currentItemMaxX );
00123         maxXCoord = qMax( maxXCoord, currentItemMaxX );
00124       }
00125     }
00126   }
00127 
00128   currentYCoordinate += mBoxSpace;
00129 
00130   size.setHeight( currentYCoordinate );
00131   size.setWidth( maxXCoord );
00132 
00133   //adjust box if width or height is to small
00134   if ( painter && currentYCoordinate > rect().height() )
00135   {
00136     setSceneRect( QRectF( transform().dx(), transform().dy(), rect().width(), currentYCoordinate ) );
00137   }
00138   if ( painter && maxXCoord > rect().width() )
00139   {
00140     setSceneRect( QRectF( transform().dx(), transform().dy(), maxXCoord, rect().height() ) );
00141   }
00142 
00143   if ( painter )
00144   {
00145     painter->restore();
00146 
00147     //draw frame and selection boxes if necessary
00148     drawFrame( painter );
00149     if ( isSelected() )
00150     {
00151       drawSelectionBoxes( painter );
00152     }
00153   }
00154 
00155   return size;
00156 }
00157 
00158 void QgsComposerLegend::drawGroupItem( QPainter* p, QgsComposerGroupItem* groupItem, double& currentYCoord, double& maxXCoord )
00159 {
00160   if ( !p || !groupItem )
00161   {
00162     return;
00163   }
00164 
00165   currentYCoord += mLayerSpace;
00166   currentYCoord += fontAscentMillimeters( mGroupFont );
00167 
00168   p->setPen( QColor( 0, 0, 0 ) );
00169   drawText( p, mBoxSpace, currentYCoord, groupItem->text(), mGroupFont );
00170 
00171   //maximum x-coordinate of current item
00172   double currentMaxXCoord = 2 * mBoxSpace + textWidthMillimeters( mGroupFont, groupItem->text() );
00173   maxXCoord = qMax( currentMaxXCoord, maxXCoord );
00174 
00175   //children can be other group items or layer items
00176   int numChildItems = groupItem->rowCount();
00177   QStandardItem* currentChildItem = 0;
00178 
00179   for ( int i = 0; i < numChildItems; ++i )
00180   {
00181     currentChildItem = groupItem->child( i );
00182     QgsComposerLegendItem* currentLegendItem = dynamic_cast<QgsComposerLegendItem*>( currentChildItem );
00183     QgsComposerLegendItem::ItemType type = currentLegendItem->itemType();
00184     if ( type == QgsComposerLegendItem::GroupItem )
00185     {
00186       drawGroupItem( p, dynamic_cast<QgsComposerGroupItem*>( currentLegendItem ), currentYCoord, currentMaxXCoord );
00187       maxXCoord = qMax( currentMaxXCoord, maxXCoord );
00188     }
00189     else if ( type == QgsComposerLegendItem::LayerItem )
00190     {
00191       drawLayerItem( p, dynamic_cast<QgsComposerLayerItem*>( currentLegendItem ), currentYCoord, currentMaxXCoord );
00192       maxXCoord = qMax( currentMaxXCoord, maxXCoord );
00193     }
00194   }
00195 }
00196 
00197 void QgsComposerLegend::drawLayerItem( QPainter* p, QgsComposerLayerItem* layerItem, double& currentYCoord, double& maxXCoord )
00198 {
00199   if ( !layerItem )
00200   {
00201     return;
00202   }
00203 
00204   int opacity = 255;
00205   QgsMapLayer* currentLayer = QgsMapLayerRegistry::instance()->mapLayer( layerItem->layerID() );
00206   if ( currentLayer )
00207   {
00208     opacity = currentLayer->getTransparency();
00209   }
00210 
00211   //Let the user omit the layer title item by having an empty layer title string
00212   if ( !layerItem->text().isEmpty() )
00213   {
00214     currentYCoord += mLayerSpace;
00215     currentYCoord += fontAscentMillimeters( mLayerFont );
00216 
00217     //draw layer Item
00218     if ( p )
00219     {
00220       p->setPen( QColor( 0, 0, 0 ) );
00221       drawText( p, mBoxSpace, currentYCoord, layerItem->text(), mLayerFont );
00222     }
00223 
00224     maxXCoord = qMax( maxXCoord, 2 * mBoxSpace + textWidthMillimeters( mLayerFont, layerItem->text() ) );
00225   }
00226   else //layer title omited
00227   {
00228     //symbol space will be added before the item later
00229     currentYCoord += ( mLayerSpace - mSymbolSpace );
00230   }
00231 
00232   //and child items
00233   drawLayerChildItems( p, layerItem, currentYCoord, maxXCoord, opacity );
00234 }
00235 
00236 void QgsComposerLegend::adjustBoxSize()
00237 {
00238   QSizeF size = paintAndDetermineSize( 0 );
00239   if ( size.isValid() )
00240   {
00241     setSceneRect( QRectF( transform().dx(), transform().dy(), size.width(), size.height() ) );
00242   }
00243 }
00244 
00245 void QgsComposerLegend::drawLayerChildItems( QPainter* p, QStandardItem* layerItem, double& currentYCoord, double& maxXCoord, int layerOpacity )
00246 {
00247   if ( !layerItem )
00248   {
00249     return;
00250   }
00251 
00252   //Draw all symbols first and the texts after (to find out the x coordinate to have the text aligned)
00253   QList<double> childYCoords;
00254   QList<double> realItemHeights;
00255 
00256   double textHeight = fontHeightCharacterMM( mItemFont, QChar( '0' ) );
00257   double itemHeight = qMax( mSymbolHeight, textHeight );
00258 
00259   double textAlignCoord = 0; //alignment for legend text
00260 
00261   QStandardItem* currentItem;
00262 
00263   int numChildren = layerItem->rowCount();
00264 
00265   for ( int i = 0; i < numChildren; ++i )
00266   {
00267     //real symbol height. Can be different from standard height in case of point symbols
00268     double realSymbolHeight;
00269     double realItemHeight = itemHeight; //will be adjusted if realSymbolHeight turns out to be larger
00270 
00271     currentYCoord += mSymbolSpace;
00272     double currentXCoord = mBoxSpace;
00273 
00274     currentItem = layerItem->child( i, 0 );
00275 
00276     if ( !currentItem )
00277     {
00278       continue;
00279     }
00280 
00281     QgsSymbol* symbol = 0;
00282     QgsComposerSymbolItem* symbolItem = dynamic_cast<QgsComposerSymbolItem*>( currentItem );
00283     if ( symbolItem )
00284     {
00285       symbol = symbolItem->symbol();
00286     }
00287 
00288     QgsSymbolV2* symbolNg = 0;
00289     QgsComposerSymbolV2Item* symbolV2Item = dynamic_cast<QgsComposerSymbolV2Item*>( currentItem );
00290     if ( symbolV2Item )
00291     {
00292       symbolNg = symbolV2Item->symbolV2();
00293     }
00294     QgsComposerRasterSymbolItem* rasterItem = dynamic_cast<QgsComposerRasterSymbolItem*>( currentItem );
00295 
00296     if ( symbol )  //item with symbol?
00297     {
00298       //draw symbol
00299       drawSymbol( p, symbol, currentYCoord + ( itemHeight - mSymbolHeight ) / 2, currentXCoord, realSymbolHeight, layerOpacity );
00300       realItemHeight = qMax( realSymbolHeight, itemHeight );
00301       currentXCoord += mIconLabelSpace;
00302     }
00303     else if ( symbolNg ) //item with symbol NG?
00304     {
00305       drawSymbolV2( p, symbolNg, currentYCoord + ( itemHeight - mSymbolHeight ) / 2, currentXCoord, realSymbolHeight, layerOpacity );
00306       realItemHeight = qMax( realSymbolHeight, itemHeight );
00307       currentXCoord += mIconLabelSpace;
00308     }
00309     else if ( rasterItem )
00310     {
00311       if ( p )
00312       {
00313         p->setBrush( rasterItem->color() );
00314         p->drawRect( QRectF( currentXCoord, currentYCoord + ( itemHeight - mSymbolHeight ) / 2, mSymbolWidth, mSymbolHeight ) );
00315       }
00316       currentXCoord += mSymbolWidth;
00317       currentXCoord += mIconLabelSpace;
00318     }
00319     else //item with icon?
00320     {
00321       QIcon symbolIcon = currentItem->icon();
00322       if ( !symbolIcon.isNull() && p )
00323       {
00324         symbolIcon.paint( p, currentXCoord, currentYCoord + ( itemHeight - mSymbolHeight ) / 2, mSymbolWidth, mSymbolHeight );
00325         currentXCoord += mSymbolWidth;
00326         currentXCoord += mIconLabelSpace;
00327       }
00328     }
00329 
00330     childYCoords.push_back( currentYCoord );
00331     realItemHeights.push_back( realItemHeight );
00332     currentYCoord += realItemHeight;
00333     textAlignCoord = qMax( currentXCoord, textAlignCoord );
00334   }
00335 
00336   maxXCoord = qMax( maxXCoord, textAlignCoord );
00337   for ( int i = 0; i < numChildren; ++i )
00338   {
00339     if ( p )
00340     {
00341       p->setPen( QColor( 0, 0, 0 ) );
00342       drawText( p, textAlignCoord, childYCoords.at( i ) + textHeight + ( realItemHeights.at( i ) - textHeight ) / 2, layerItem->child( i, 0 )->text(), mItemFont );
00343       maxXCoord = qMax( maxXCoord, textAlignCoord + mBoxSpace + textWidthMillimeters( mItemFont,  layerItem->child( i, 0 )->text() ) );
00344     }
00345   }
00346 }
00347 
00348 void QgsComposerLegend::drawSymbol( QPainter* p, QgsSymbol* s, double currentYCoord, double& currentXPosition, double& symbolHeight, int layerOpacity ) const
00349 {
00350   if ( !s )
00351   {
00352     return;
00353   }
00354 
00355   QGis::GeometryType symbolType = s->type();
00356   switch ( symbolType )
00357   {
00358     case QGis::Point:
00359       drawPointSymbol( p, s, currentYCoord, currentXPosition, symbolHeight, layerOpacity );
00360       break;
00361     case QGis::Line:
00362       drawLineSymbol( p, s, currentYCoord, currentXPosition, layerOpacity );
00363       symbolHeight = mSymbolHeight;
00364       break;
00365     case QGis::Polygon:
00366       drawPolygonSymbol( p, s, currentYCoord, currentXPosition, layerOpacity );
00367       symbolHeight = mSymbolHeight;
00368       break;
00369     case QGis::UnknownGeometry:
00370     case QGis::NoGeometry:
00371       // shouldn't occur
00372       break;
00373   }
00374 }
00375 
00376 void QgsComposerLegend::drawSymbolV2( QPainter* p, QgsSymbolV2* s, double currentYCoord, double& currentXPosition, double& symbolHeight, int layerOpacity ) const
00377 {
00378   if ( !p || !s )
00379   {
00380     return;
00381   }
00382 
00383   double rasterScaleFactor = 1.0;
00384   if ( p )
00385   {
00386     QPaintDevice* paintDevice = p->device();
00387     if ( !paintDevice )
00388     {
00389       return;
00390     }
00391     rasterScaleFactor = ( paintDevice->logicalDpiX() + paintDevice->logicalDpiY() ) / 2.0 / 25.4;
00392   }
00393 
00394   //consider relation to composer map for symbol sizes in mm
00395   bool sizeInMapUnits = s->outputUnit() == QgsSymbolV2::MapUnit;
00396   double mmPerMapUnit = 1;
00397   if ( mComposerMap )
00398   {
00399     mmPerMapUnit = mComposerMap->mapUnitsToMM();
00400   }
00401   QgsMarkerSymbolV2* markerSymbol = dynamic_cast<QgsMarkerSymbolV2*>( s );
00402 
00403   //Consider symbol size for point markers
00404   double height = mSymbolHeight;
00405   double width = mSymbolWidth;
00406   double size = 0;
00407   //Center small marker symbols
00408   double widthOffset = 0;
00409   double heightOffset = 0;
00410 
00411   if ( markerSymbol )
00412   {
00413     size = markerSymbol->size();
00414     height = size;
00415     width = size;
00416     if ( mComposerMap && sizeInMapUnits )
00417     {
00418       height *= mmPerMapUnit;
00419       width *= mmPerMapUnit;
00420       markerSymbol->setSize( width );
00421     }
00422     if ( width < mSymbolWidth )
00423     {
00424       widthOffset = ( mSymbolWidth - width ) / 2.0;
00425     }
00426     if ( height < mSymbolHeight )
00427     {
00428       heightOffset = ( mSymbolHeight - height ) / 2.0;
00429     }
00430   }
00431 
00432   p->save();
00433   p->translate( currentXPosition + widthOffset, currentYCoord + heightOffset );
00434   p->scale( 1.0 / rasterScaleFactor, 1.0 / rasterScaleFactor );
00435 
00436   if ( markerSymbol && sizeInMapUnits )
00437   {
00438     s->setOutputUnit( QgsSymbolV2::MM );
00439   }
00440   s->drawPreviewIcon( p, QSize( width * rasterScaleFactor, height * rasterScaleFactor ) );
00441 
00442   if ( markerSymbol && sizeInMapUnits )
00443   {
00444     s->setOutputUnit( QgsSymbolV2::MapUnit );
00445     markerSymbol->setSize( size );
00446   }
00447 
00448   p->restore();
00449   currentXPosition += width;
00450   currentXPosition += 2 * widthOffset;
00451   symbolHeight = height + 2 * heightOffset;
00452 }
00453 
00454 void QgsComposerLegend::drawPointSymbol( QPainter* p, QgsSymbol* s, double currentYCoord, double& currentXPosition, double& symbolHeight, int opacity ) const
00455 {
00456   if ( !s )
00457   {
00458     return;
00459   }
00460 
00461   QImage pointImage;
00462   double rasterScaleFactor = 1.0;
00463   if ( p )
00464   {
00465     QPaintDevice* paintDevice = p->device();
00466     if ( !paintDevice )
00467     {
00468       return;
00469     }
00470 
00471     rasterScaleFactor = ( paintDevice->logicalDpiX() + paintDevice->logicalDpiY() ) / 2.0 / 25.4;
00472   }
00473 
00474   //width scale is 1.0
00475   pointImage = s->getPointSymbolAsImage( 1.0, false, Qt::yellow, 1.0, 0.0, rasterScaleFactor, opacity / 255.0 );
00476 
00477   if ( p )
00478   {
00479     p->save();
00480     p->scale( 1.0 / rasterScaleFactor, 1.0 / rasterScaleFactor );
00481 
00482     QPointF imageTopLeft( currentXPosition * rasterScaleFactor, currentYCoord * rasterScaleFactor );
00483     p->drawImage( imageTopLeft, pointImage );
00484     p->restore();
00485   }
00486 
00487   currentXPosition += s->pointSize(); //pointImage.width() / rasterScaleFactor;
00488   symbolHeight = s->pointSize(); //pointImage.height() / rasterScaleFactor;
00489 }
00490 
00491 void QgsComposerLegend::drawLineSymbol( QPainter* p, QgsSymbol* s, double currentYCoord, double& currentXPosition, int opacity ) const
00492 {
00493   if ( !s )
00494   {
00495     return;
00496   }
00497 
00498   double yCoord = currentYCoord + mSymbolHeight / 2;
00499 
00500   if ( p )
00501   {
00502     p->save();
00503     QPen symbolPen = s->pen();
00504     QColor penColor = symbolPen.color();
00505     penColor.setAlpha( opacity );
00506     symbolPen.setColor( penColor );
00507     symbolPen.setCapStyle( Qt::FlatCap );
00508     p->setPen( symbolPen );
00509     p->drawLine( QPointF( currentXPosition, yCoord ), QPointF( currentXPosition + mSymbolWidth, yCoord ) );
00510     p->restore();
00511   }
00512 
00513   currentXPosition += mSymbolWidth;
00514 }
00515 
00516 void QgsComposerLegend::drawPolygonSymbol( QPainter* p, QgsSymbol* s, double currentYCoord, double& currentXPosition, int opacity ) const
00517 {
00518   if ( !s )
00519   {
00520     return;
00521   }
00522 
00523   if ( p )
00524   {
00525     //scale brush and set transparencies
00526     QBrush symbolBrush = s->brush();
00527     QColor brushColor = symbolBrush.color();
00528     brushColor.setAlpha( opacity );
00529     symbolBrush.setColor( brushColor );
00530     QPaintDevice* paintDevice = p->device();
00531     if ( paintDevice )
00532     {
00533       double rasterScaleFactor = ( paintDevice->logicalDpiX() + paintDevice->logicalDpiY() ) / 2.0 / 25.4;
00534       QgsRenderer::scaleBrush( symbolBrush, rasterScaleFactor );
00535     }
00536     p->setBrush( symbolBrush );
00537 
00538     QPen symbolPen = s->pen();
00539     QColor penColor = symbolPen.color();
00540     penColor.setAlpha( opacity );
00541     symbolPen.setColor( penColor );
00542     p->setPen( symbolPen );
00543 
00544     p->drawRect( QRectF( currentXPosition, currentYCoord, mSymbolWidth, mSymbolHeight ) );
00545   }
00546 
00547   currentXPosition += mSymbolWidth;
00548 }
00549 
00550 QStringList QgsComposerLegend::layerIdList() const
00551 {
00552   //take layer list from map renderer (to have legend order)
00553   if ( mComposition )
00554   {
00555     QgsMapRenderer* r = mComposition->mapRenderer();
00556     if ( r )
00557     {
00558       return r->layerSet();
00559     }
00560   }
00561   return QStringList();
00562 }
00563 
00564 void QgsComposerLegend::synchronizeWithModel()
00565 {
00566   adjustBoxSize();
00567   update();
00568 }
00569 
00570 void QgsComposerLegend::setTitleFont( const QFont& f )
00571 {
00572   mTitleFont = f;
00573   adjustBoxSize();
00574   update();
00575 }
00576 
00577 void QgsComposerLegend::setGroupFont( const QFont& f )
00578 {
00579   mGroupFont = f;
00580   adjustBoxSize();
00581   update();
00582 }
00583 
00584 void QgsComposerLegend::setLayerFont( const QFont& f )
00585 {
00586   mLayerFont = f;
00587   adjustBoxSize();
00588   update();
00589 }
00590 
00591 void QgsComposerLegend::setItemFont( const QFont& f )
00592 {
00593   mItemFont = f;
00594   adjustBoxSize();
00595   update();
00596 }
00597 
00598 QFont QgsComposerLegend::titleFont() const
00599 {
00600   return mTitleFont;
00601 }
00602 
00603 QFont QgsComposerLegend::groupFont() const
00604 {
00605   return mGroupFont;
00606 }
00607 
00608 QFont QgsComposerLegend::layerFont() const
00609 {
00610   return mLayerFont;
00611 }
00612 
00613 QFont QgsComposerLegend::itemFont() const
00614 {
00615   return mItemFont;
00616 }
00617 
00618 void QgsComposerLegend::updateLegend()
00619 {
00620   mLegendModel.setLayerSet( layerIdList() );
00621   adjustBoxSize();
00622   update();
00623 }
00624 
00625 bool QgsComposerLegend::writeXML( QDomElement& elem, QDomDocument & doc ) const
00626 {
00627   if ( elem.isNull() )
00628   {
00629     return false;
00630   }
00631 
00632   QDomElement composerLegendElem = doc.createElement( "ComposerLegend" );
00633 
00634   //write general properties
00635   composerLegendElem.setAttribute( "title", mTitle );
00636   composerLegendElem.setAttribute( "titleFont", mTitleFont.toString() );
00637   composerLegendElem.setAttribute( "groupFont", mGroupFont.toString() );
00638   composerLegendElem.setAttribute( "layerFont", mLayerFont.toString() );
00639   composerLegendElem.setAttribute( "itemFont", mItemFont.toString() );
00640   composerLegendElem.setAttribute( "boxSpace", QString::number( mBoxSpace ) );
00641   composerLegendElem.setAttribute( "layerSpace", QString::number( mLayerSpace ) );
00642   composerLegendElem.setAttribute( "symbolSpace", QString::number( mSymbolSpace ) );
00643   composerLegendElem.setAttribute( "iconLabelSpace", QString::number( mIconLabelSpace ) );
00644   composerLegendElem.setAttribute( "symbolWidth", mSymbolWidth );
00645   composerLegendElem.setAttribute( "symbolHeight", mSymbolHeight );
00646 
00647   if ( mComposerMap )
00648   {
00649     composerLegendElem.setAttribute( "map", mComposerMap->id() );
00650   }
00651 
00652   //write model properties
00653   mLegendModel.writeXML( composerLegendElem, doc );
00654 
00655   elem.appendChild( composerLegendElem );
00656   return _writeXML( composerLegendElem, doc );
00657 }
00658 
00659 bool QgsComposerLegend::readXML( const QDomElement& itemElem, const QDomDocument& doc )
00660 {
00661   if ( itemElem.isNull() )
00662   {
00663     return false;
00664   }
00665 
00666   //read general properties
00667   mTitle = itemElem.attribute( "title" );
00668   //title font
00669   QString titleFontString = itemElem.attribute( "titleFont" );
00670   if ( !titleFontString.isEmpty() )
00671   {
00672     mTitleFont.fromString( titleFontString );
00673   }
00674   //group font
00675   QString groupFontString = itemElem.attribute( "groupFont" );
00676   if ( !groupFontString.isEmpty() )
00677   {
00678     mGroupFont.fromString( groupFontString );
00679   }
00680 
00681   //layer font
00682   QString layerFontString = itemElem.attribute( "layerFont" );
00683   if ( !layerFontString.isEmpty() )
00684   {
00685     mLayerFont.fromString( layerFontString );
00686   }
00687   //item font
00688   QString itemFontString = itemElem.attribute( "itemFont" );
00689   if ( !itemFontString.isEmpty() )
00690   {
00691     mItemFont.fromString( itemFontString );
00692   }
00693 
00694   //spaces
00695   mBoxSpace = itemElem.attribute( "boxSpace", "2.0" ).toDouble();
00696   mLayerSpace = itemElem.attribute( "layerSpace", "3.0" ).toDouble();
00697   mSymbolSpace = itemElem.attribute( "symbolSpace", "2.0" ).toDouble();
00698   mIconLabelSpace = itemElem.attribute( "iconLabelSpace", "2.0" ).toDouble();
00699   mSymbolWidth = itemElem.attribute( "symbolWidth", "7.0" ).toDouble();
00700   mSymbolHeight = itemElem.attribute( "symbolHeight", "14.0" ).toDouble();
00701 
00702   //composer map
00703   if ( !itemElem.attribute( "map" ).isEmpty() )
00704   {
00705     mComposerMap = mComposition->getComposerMapById( itemElem.attribute( "map" ).toInt() );
00706   }
00707 
00708   //read model properties
00709   QDomNodeList modelNodeList = itemElem.elementsByTagName( "Model" );
00710   if ( modelNodeList.size() > 0 )
00711   {
00712     QDomElement modelElem = modelNodeList.at( 0 ).toElement();
00713     mLegendModel.readXML( modelElem, doc );
00714   }
00715 
00716   //restore general composer item properties
00717   QDomNodeList composerItemList = itemElem.elementsByTagName( "ComposerItem" );
00718   if ( composerItemList.size() > 0 )
00719   {
00720     QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
00721     _readXML( composerItemElem, doc );
00722   }
00723 
00724   emit itemChanged();
00725   return true;
00726 }
00727 
00728 void QgsComposerLegend::setComposerMap( const QgsComposerMap* map )
00729 {
00730   mComposerMap = map;
00731   QObject::connect( map, SIGNAL( destroyed( QObject* ) ), this, SLOT( invalidateCurrentMap() ) );
00732 }
00733 
00734 void QgsComposerLegend::invalidateCurrentMap()
00735 {
00736   disconnect( mComposerMap, SIGNAL( destroyed( QObject* ) ), this, SLOT( invalidateCurrentMap() ) );
00737   mComposerMap = 0;
00738 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines