27 #include <QDomDocument>
28 #include <QDomElement>
33 , mTitle( tr(
"Legend" ) )
37 , mIconLabelSpace( 2 ), mComposerMap( 0 )
77 QStandardItem* rootItem =
mLegendModel.invisibleRootItem();
88 painter->setPen( QPen( QColor( 0, 0, 0 ) ) );
91 int numLayerItems = rootItem->rowCount();
92 QStandardItem* currentLayerItem = 0;
101 painter->setPen( QColor( 0, 0, 0 ) );
107 double currentItemMaxX = 0;
108 for (
int i = 0; i < numLayerItems; ++i )
110 currentLayerItem = rootItem->child( i );
112 if ( currentLegendItem )
117 drawGroupItem( painter, dynamic_cast<QgsComposerGroupItem*>( currentLegendItem ), currentYCoordinate, currentItemMaxX );
118 maxXCoord = qMax( maxXCoord, currentItemMaxX );
122 drawLayerItem( painter, dynamic_cast<QgsComposerLayerItem*>( currentLegendItem ), currentYCoordinate, currentItemMaxX );
123 maxXCoord = qMax( maxXCoord, currentItemMaxX );
130 size.setHeight( currentYCoordinate );
131 size.setWidth( maxXCoord );
134 if ( painter && currentYCoordinate > rect().height() )
136 setSceneRect( QRectF( transform().dx(), transform().dy(), rect().width(), currentYCoordinate ) );
138 if ( painter && maxXCoord > rect().width() )
140 setSceneRect( QRectF( transform().dx(), transform().dy(), maxXCoord, rect().height() ) );
160 if ( !p || !groupItem )
168 p->setPen( QColor( 0, 0, 0 ) );
173 maxXCoord = qMax( currentMaxXCoord, maxXCoord );
176 int numChildItems = groupItem->rowCount();
177 QStandardItem* currentChildItem = 0;
179 for (
int i = 0; i < numChildItems; ++i )
181 currentChildItem = groupItem->child( i );
186 drawGroupItem( p, dynamic_cast<QgsComposerGroupItem*>( currentLegendItem ), currentYCoord, currentMaxXCoord );
187 maxXCoord = qMax( currentMaxXCoord, maxXCoord );
191 drawLayerItem( p, dynamic_cast<QgsComposerLayerItem*>( currentLegendItem ), currentYCoord, currentMaxXCoord );
192 maxXCoord = qMax( currentMaxXCoord, maxXCoord );
212 if ( !layerItem->text().isEmpty() )
220 p->setPen( QColor( 0, 0, 0 ) );
239 if ( size.isValid() )
241 setSceneRect( QRectF( transform().dx(), transform().dy(), size.width(), size.height() ) );
253 QList<double> childYCoords;
254 QList<double> realItemHeights;
259 double textAlignCoord = 0;
261 QStandardItem* currentItem;
263 int numChildren = layerItem->rowCount();
265 for (
int i = 0; i < numChildren; ++i )
268 double realSymbolHeight;
269 double realItemHeight = itemHeight;
274 currentItem = layerItem->child( i, 0 );
285 symbol = symbolItem->
symbol();
292 symbolNg = symbolV2Item->
symbolV2();
299 drawSymbol( p, symbol, currentYCoord + ( itemHeight -
mSymbolHeight ) / 2, currentXCoord, realSymbolHeight, layerOpacity );
300 realItemHeight = qMax( realSymbolHeight, itemHeight );
305 drawSymbolV2( p, symbolNg, currentYCoord + ( itemHeight -
mSymbolHeight ) / 2, currentXCoord, realSymbolHeight, layerOpacity );
306 realItemHeight = qMax( realSymbolHeight, itemHeight );
309 else if ( rasterItem )
313 p->setBrush( rasterItem->
color() );
321 QIcon symbolIcon = currentItem->icon();
322 if ( !symbolIcon.isNull() && p )
330 childYCoords.push_back( currentYCoord );
331 realItemHeights.push_back( realItemHeight );
332 currentYCoord += realItemHeight;
333 textAlignCoord = qMax( currentXCoord, textAlignCoord );
336 maxXCoord = qMax( maxXCoord, textAlignCoord );
337 for (
int i = 0; i < numChildren; ++i )
341 p->setPen( QColor( 0, 0, 0 ) );
342 drawText( p, textAlignCoord, childYCoords.at( i ) + textHeight + ( realItemHeights.at( i ) - textHeight ) / 2, layerItem->child( i, 0 )->text(),
mItemFont );
356 switch ( symbolType )
359 drawPointSymbol( p, s, currentYCoord, currentXPosition, symbolHeight, layerOpacity );
362 drawLineSymbol( p, s, currentYCoord, currentXPosition, layerOpacity );
383 double rasterScaleFactor = 1.0;
386 QPaintDevice* paintDevice = p->device();
391 rasterScaleFactor = ( paintDevice->logicalDpiX() + paintDevice->logicalDpiY() ) / 2.0 / 25.4;
396 double mmPerMapUnit = 1;
408 double widthOffset = 0;
409 double heightOffset = 0;
413 size = markerSymbol->
size();
418 height *= mmPerMapUnit;
419 width *= mmPerMapUnit;
420 markerSymbol->
setSize( width );
433 p->translate( currentXPosition + widthOffset, currentYCoord + heightOffset );
434 p->scale( 1.0 / rasterScaleFactor, 1.0 / rasterScaleFactor );
436 if ( markerSymbol && sizeInMapUnits )
440 s->
drawPreviewIcon( p, QSize( width * rasterScaleFactor, height * rasterScaleFactor ) );
442 if ( markerSymbol && sizeInMapUnits )
449 currentXPosition += width;
450 currentXPosition += 2 * widthOffset;
451 symbolHeight = height + 2 * heightOffset;
462 double rasterScaleFactor = 1.0;
465 QPaintDevice* paintDevice = p->device();
471 rasterScaleFactor = ( paintDevice->logicalDpiX() + paintDevice->logicalDpiY() ) / 2.0 / 25.4;
475 pointImage = s->
getPointSymbolAsImage( 1.0,
false, Qt::yellow, 1.0, 0.0, rasterScaleFactor, opacity / 255.0 );
480 p->scale( 1.0 / rasterScaleFactor, 1.0 / rasterScaleFactor );
482 QPointF imageTopLeft( currentXPosition * rasterScaleFactor, currentYCoord * rasterScaleFactor );
483 p->drawImage( imageTopLeft, pointImage );
503 QPen symbolPen = s->
pen();
504 QColor penColor = symbolPen.color();
505 penColor.setAlpha( opacity );
506 symbolPen.setColor( penColor );
507 symbolPen.setCapStyle( Qt::FlatCap );
508 p->setPen( symbolPen );
509 p->drawLine( QPointF( currentXPosition, yCoord ), QPointF( currentXPosition +
mSymbolWidth, yCoord ) );
526 QBrush symbolBrush = s->
brush();
527 QColor brushColor = symbolBrush.color();
528 brushColor.setAlpha( opacity );
529 symbolBrush.setColor( brushColor );
530 QPaintDevice* paintDevice = p->device();
533 double rasterScaleFactor = ( paintDevice->logicalDpiX() + paintDevice->logicalDpiY() ) / 2.0 / 25.4;
536 p->setBrush( symbolBrush );
538 QPen symbolPen = s->
pen();
539 QColor penColor = symbolPen.color();
540 penColor.setAlpha( opacity );
541 symbolPen.setColor( penColor );
542 p->setPen( symbolPen );
561 return QStringList();
632 QDomElement composerLegendElem = doc.createElement(
"ComposerLegend" );
635 composerLegendElem.setAttribute(
"title",
mTitle );
636 composerLegendElem.setAttribute(
"titleFont",
mTitleFont.toString() );
637 composerLegendElem.setAttribute(
"groupFont",
mGroupFont.toString() );
638 composerLegendElem.setAttribute(
"layerFont",
mLayerFont.toString() );
639 composerLegendElem.setAttribute(
"itemFont",
mItemFont.toString() );
640 composerLegendElem.setAttribute(
"boxSpace", QString::number(
mBoxSpace ) );
641 composerLegendElem.setAttribute(
"layerSpace", QString::number(
mLayerSpace ) );
642 composerLegendElem.setAttribute(
"symbolSpace", QString::number(
mSymbolSpace ) );
643 composerLegendElem.setAttribute(
"iconLabelSpace", QString::number(
mIconLabelSpace ) );
644 composerLegendElem.setAttribute(
"symbolWidth",
mSymbolWidth );
645 composerLegendElem.setAttribute(
"symbolHeight",
mSymbolHeight );
655 elem.appendChild( composerLegendElem );
656 return _writeXML( composerLegendElem, doc );
661 if ( itemElem.isNull() )
667 mTitle = itemElem.attribute(
"title" );
669 QString titleFontString = itemElem.attribute(
"titleFont" );
670 if ( !titleFontString.isEmpty() )
675 QString groupFontString = itemElem.attribute(
"groupFont" );
676 if ( !groupFontString.isEmpty() )
682 QString layerFontString = itemElem.attribute(
"layerFont" );
683 if ( !layerFontString.isEmpty() )
688 QString itemFontString = itemElem.attribute(
"itemFont" );
689 if ( !itemFontString.isEmpty() )
695 mBoxSpace = itemElem.attribute(
"boxSpace",
"2.0" ).toDouble();
696 mLayerSpace = itemElem.attribute(
"layerSpace",
"3.0" ).toDouble();
697 mSymbolSpace = itemElem.attribute(
"symbolSpace",
"2.0" ).toDouble();
698 mIconLabelSpace = itemElem.attribute(
"iconLabelSpace",
"2.0" ).toDouble();
699 mSymbolWidth = itemElem.attribute(
"symbolWidth",
"7.0" ).toDouble();
700 mSymbolHeight = itemElem.attribute(
"symbolHeight",
"14.0" ).toDouble();
703 if ( !itemElem.attribute(
"map" ).isEmpty() )
709 QDomNodeList modelNodeList = itemElem.elementsByTagName(
"Model" );
710 if ( modelNodeList.size() > 0 )
712 QDomElement modelElem = modelNodeList.at( 0 ).toElement();
717 QDomNodeList composerItemList = itemElem.elementsByTagName(
"ComposerItem" );
718 if ( composerItemList.size() > 0 )
720 QDomElement composerItemElem = composerItemList.at( 0 ).toElement();