31 #include <QDomDocument>
32 #include <QDomElement>
37 , mCustomLayerTree( 0 )
39 , mLegendFilterByMap( false )
48 QgsComposerLegend::QgsComposerLegend()
51 , mCustomLayerTree( 0 )
53 , mLegendFilterByMap( false )
61 delete mCustomLayerTree;
66 Q_UNUSED( itemStyle );
77 int dpi = painter->device()->logicalDpiX();
78 double dotsPerMM = dpi / 25.4;
91 ms.
setOutputSize( QSizeF( mComposerMap->rect().width() * dotsPerMM, mComposerMap->rect().height() * dotsPerMM ).toSize() );
100 painter->setRenderHint( QPainter::Antialiasing,
true );
101 painter->setPen( QPen( QColor( 0, 0, 0 ) ) );
108 if ( size.height() > rect().height() || size.width() > rect().width() )
111 QRectF targetRect = QRectF( pos().x(), pos().y(), rect().width(), rect().height() );
112 if ( size.height() > targetRect.height() )
113 targetRect.setHeight( size.height() );
114 if ( size.width() > rect().width() )
115 targetRect.setWidth( size.width() );
147 QgsDebugMsg( QString(
"width = %1 height = %2" ).arg( size.width() ).arg( size.height() ) );
148 if ( size.isValid() )
150 QRectF targetRect = QRectF( pos().x(), pos().y(), size.width(), size.height() );
161 delete mCustomLayerTree;
162 mCustomLayerTree = rootGroup;
177 return !mCustomLayerTree;
182 mLegendFilterByMap = enabled;
267 QDomElement composerLegendElem = doc.createElement(
"ComposerLegend" );
268 elem.appendChild( composerLegendElem );
271 composerLegendElem.setAttribute(
"title", mSettings.
title() );
272 composerLegendElem.setAttribute(
"titleAlignment", QString::number((
int ) mSettings.
titleAlignment() ) );
273 composerLegendElem.setAttribute(
"columnCount", QString::number( mSettings.
columnCount() ) );
274 composerLegendElem.setAttribute(
"splitLayer", QString::number( mSettings.
splitLayer() ) );
275 composerLegendElem.setAttribute(
"equalColumnWidth", QString::number( mSettings.
equalColumnWidth() ) );
277 composerLegendElem.setAttribute(
"boxSpace", QString::number( mSettings.
boxSpace() ) );
278 composerLegendElem.setAttribute(
"columnSpace", QString::number( mSettings.
columnSpace() ) );
280 composerLegendElem.setAttribute(
"symbolWidth", QString::number( mSettings.
symbolSize().width() ) );
281 composerLegendElem.setAttribute(
"symbolHeight", QString::number( mSettings.
symbolSize().height() ) );
282 composerLegendElem.setAttribute(
"wmsLegendWidth", QString::number( mSettings.
wmsLegendSize().width() ) );
283 composerLegendElem.setAttribute(
"wmsLegendHeight", QString::number( mSettings.
wmsLegendSize().height() ) );
284 composerLegendElem.setAttribute(
"wrapChar", mSettings.
wrapChar() );
285 composerLegendElem.setAttribute(
"fontColor", mSettings.
fontColor().name() );
289 composerLegendElem.setAttribute(
"map", mComposerMap->
id() );
292 QDomElement composerLegendStyles = doc.createElement(
"styles" );
293 composerLegendElem.appendChild( composerLegendStyles );
301 if ( mCustomLayerTree )
304 mCustomLayerTree->
writeXML( composerLegendElem );
307 if ( mLegendFilterByMap )
308 composerLegendElem.setAttribute(
"legendFilterByMap",
"1" );
310 return _writeXML( composerLegendElem, doc );
315 QDomElement itemElem = elem.firstChildElement();
317 while ( !itemElem.isNull() )
320 if ( itemElem.tagName() ==
"LayerItem" )
322 QString layerId = itemElem.attribute(
"layerId" );
326 QString userText = itemElem.attribute(
"userText" );
327 if ( !userText.isEmpty() )
329 QString style = itemElem.attribute(
"style" );
330 if ( !style.isEmpty() )
332 QString showFeatureCount = itemElem.attribute(
"showFeatureCount" );
333 if ( showFeatureCount.toInt() )
339 else if ( itemElem.tagName() ==
"GroupItem" )
342 QString style = itemElem.attribute(
"style" );
343 if ( !style.isEmpty() )
349 itemElem = itemElem.nextSiblingElement();
355 if ( itemElem.isNull() )
361 mSettings.
setTitle( itemElem.attribute(
"title" ) );
362 if ( !itemElem.attribute(
"titleAlignment" ).isEmpty() )
364 mSettings.
setTitleAlignment(( Qt::AlignmentFlag )itemElem.attribute(
"titleAlignment" ).toInt() );
366 int colCount = itemElem.attribute(
"columnCount",
"1" ).toInt();
367 if ( colCount < 1 ) colCount = 1;
369 mSettings.
setSplitLayer( itemElem.attribute(
"splitLayer",
"0" ).toInt() == 1 );
370 mSettings.
setEqualColumnWidth( itemElem.attribute(
"equalColumnWidth",
"0" ).toInt() == 1 );
372 QDomNodeList stylesNodeList = itemElem.elementsByTagName(
"styles" );
373 if ( stylesNodeList.size() > 0 )
375 QDomNode stylesNode = stylesNodeList.at( 0 );
376 for (
int i = 0; i < stylesNode.childNodes().size(); i++ )
378 QDomElement styleElem = stylesNode.childNodes().at( i ).toElement();
380 style.
readXML( styleElem, doc );
381 QString name = styleElem.attribute(
"name" );
395 fontClr.setNamedColor( itemElem.attribute(
"fontColor",
"#000000" ) );
399 mSettings.
setBoxSpace( itemElem.attribute(
"boxSpace",
"2.0" ).toDouble() );
400 mSettings.
setColumnSpace( itemElem.attribute(
"columnSpace",
"2.0" ).toDouble() );
402 mSettings.
setSymbolSize( QSizeF( itemElem.attribute(
"symbolWidth",
"7.0" ).toDouble(), itemElem.attribute(
"symbolHeight",
"14.0" ).toDouble() ) );
403 mSettings.
setWmsLegendSize( QSizeF( itemElem.attribute(
"wmsLegendWidth",
"50" ).toDouble(), itemElem.attribute(
"wmsLegendHeight",
"25" ).toDouble() ) );
405 mSettings.
setWrapChar( itemElem.attribute(
"wrapChar" ) );
408 mLegendFilterByMap = itemElem.attribute(
"legendFilterByMap",
"0" ).toInt();
409 if ( !itemElem.attribute(
"map" ).isEmpty() )
414 QDomElement oldLegendModelElem = itemElem.firstChildElement(
"Model" );
415 if ( !oldLegendModelElem.isNull() )
420 setCustomLayerTree( nodeRoot );
425 QDomElement layerTreeElem = itemElem.firstChildElement(
"layer-tree-group" );
430 QDomNodeList composerItemList = itemElem.elementsByTagName(
"ComposerItem" );
431 if ( composerItemList.size() > 0 )
433 QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
439 QString titleFontString = itemElem.attribute(
"titleFont" );
440 if ( !titleFontString.isEmpty() )
445 QString groupFontString = itemElem.attribute(
"groupFont" );
446 if ( !groupFontString.isEmpty() )
452 QString layerFontString = itemElem.attribute(
"layerFont" );
453 if ( !layerFontString.isEmpty() )
458 QString itemFontString = itemElem.attribute(
"itemFont" );
459 if ( !itemFontString.isEmpty() )
464 if ( !itemElem.attribute(
"groupSpace" ).isEmpty() )
468 if ( !itemElem.attribute(
"layerSpace" ).isEmpty() )
472 if ( !itemElem.attribute(
"symbolSpace" ).isEmpty() )
485 if ( !
id().isEmpty() )
491 QString text = mSettings.
title();
492 if ( text.isEmpty() )
494 return tr(
"<legend>" );
496 if ( text.length() > 25 )
498 return QString(
tr(
"%1..." ) ).arg( text.left( 25 ) );
510 disconnect( mComposerMap, SIGNAL( destroyed( QObject* ) ),
this, SLOT(
invalidateCurrentMap() ) );
511 disconnect( mComposerMap, SIGNAL(
itemChanged() ),
this, SLOT( updateFilterByMap() ) );
512 disconnect( mComposerMap, SIGNAL( extentChanged() ),
this, SLOT( updateFilterByMap() ) );
520 QObject::connect( map, SIGNAL(
itemChanged() ),
this, SLOT( updateFilterByMap() ) );
521 QObject::connect( map, SIGNAL( extentChanged() ),
this, SLOT( updateFilterByMap() ) );
532 void QgsComposerLegend::updateFilterByMap()
537 if ( mComposerMap && mLegendFilterByMap )
544 QSizeF theSize( requestRectangle.
width(), requestRectangle.
height() );
574 if (
QgsLayerTree::isLayer( node ) && ( role == Qt::DisplayRole || role == Qt::EditRole ) && !node->customProperty(
"legend/title-label" ).isNull() )
577 QString name = node->customProperty(
"legend/title-label" ).toString();
578 if ( nodeLayer->
customProperty(
"showFeatureCount", 0 ).toInt() && role == Qt::DisplayRole )