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