31 #include <QDomDocument> 32 #include <QDomElement> 37 , mCustomLayerTree( 0 )
39 , mLegendFilterByMap( false )
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 )
void setTitleAlignment(Qt::AlignmentFlag alignment)
Sets the alignment of the legend title.
void setWrapChar(const QString &t)
Layer tree group node serves as a container for layers and further groups.
void setLegendSize(QSizeF s)
Set the preferred resulting legend size.
QgsComposerLegend(QgsComposition *composition)
A rectangle specified with double values.
Base class for all map layer types.
QgsComposerLegendStyle style(QgsComposerLegendStyle::Style s) const
Returns style.
void setEqualColumnWidth(bool s)
QgsLayerTreeGroup * addGroup(const QString &name)
Append a new group node with given name. Newly created node is owned by this group.
void setBoxSpace(double s)
void setMmPerMapUnit(double mmPerMapUnit)
double scale() const
Return the calculated scale of the map.
void readXML(const QDomElement &elem, const QDomDocument &doc)
double mapUnitsToMM() const
Returns the conversion factor map units -> mm.
QgsComposerLegendStyle & rstyle(QgsComposerLegendStyle::Style s)
Returns reference to modifiable style.
void setMargin(Side side, double margin)
Item model implementation based on layer tree model for composer legend.
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer.
static QgsLayerTreeGroup * readXML(QDomElement &element)
Read group (tree) from XML element <layer-tree-group> and return the newly created group (or null on ...
QgsMapLayer * layer() const
void setSplitLayer(bool s)
void paint(QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget) override
Reimplementation of QCanvasItem::paint.
QgsComposerModel * itemsModel()
Returns the items model attached to the composition.
void setOutputDpi(int dpi)
Set DPI used for conversion between real world units (e.g. mm) and pixels.
void itemChanged()
Emitted when the item changes.
void requestedExtent(QgsRectangle &extent) const
Calculates the extent to request and the yShift of the top-left point in case of rotation.
const QgsMapSettings & mapSettings() const
Return setting of QGIS map canvas.
void setSymbolSize(QSizeF s)
void drawLegend(QPainter *painter)
Draw the legend with given painter.
void setColumnSpace(double s)
static QgsMapLayerRegistry * instance()
A item that forms part of a map composition.
double columnSpace() const
double columnSpace() const
Flags flags() const
Return OR-ed combination of model flags.
QVariant data(const QModelIndex &index, int role) const override
double wmsLegendWidth() const
virtual void drawFrame(QPainter *p)
Draw black frame around item.
QgsComposerLegendStyle style(QgsComposerLegendStyle::Style s) const
Returns style.
void setTitle(const QString &t)
void updateLegend()
Updates the model and all legend entries.
void updateItemDisplayName(QgsComposerItem *item)
Must be called when an item's display name is modified.
QgsLayerTreeGroup * toGroup(QgsLayerTreeNode *node)
Cast node to a group. No type checking is done - use isGroup() to find out whether this operation is ...
void setWmsLegendHeight(double h)
Allow check boxes for legend nodes (if supported by layer's legend)
QgsComposerLegendStyle & rstyle(QgsComposerLegendStyle::Style s)
Returns reference to modifiable style.
QgsLayerTreeLayer * addLayer(QgsMapLayer *layer)
Append a new layer node for given map layer. Newly created node is owned by this group.
void adjustBoxSize()
Sets item box to the whole content.
The QgsMapSettings class contains configuration for rendering of the map.
bool _readXML(const QDomElement &itemElem, const QDomDocument &doc)
Reads parameter that are not subclass specific in document.
QgsLegendModelV2(QgsLayerTreeGroup *rootNode, QObject *parent=0)
The QgsLayerTreeModel class is model implementation for Qt item views framework.
void setLayerSet(const QStringList &layerIds, double scaleDenominator=-1, QString rule="")
void setSymbolHeight(double h)
virtual void drawSelectionBoxes(QPainter *p)
Draws additional graphics on selected items.
int printResolution() const
void setLegendFilterByMap(const QgsMapSettings *settings)
Force only display of legend nodes which are valid for given map settings.
const QgsComposition * composition() const
Returns the composition the item is attached to.
static QgsLayerTreeModelLegendNode * index2legendNode(const QModelIndex &index)
Return legend node for given index.
void setMapScale(double scale)
void setComposerMap(const QgsComposerMap *map)
void synchronizeWithModel()
Data changed.
void setStyleFont(QgsComposerLegendStyle::Style s, const QFont &f)
Set style font.
int pendingFeatureCount()
returns feature count after commit
void setTitle(const QString &t)
Qt::AlignmentFlag titleAlignment() const
Returns the alignment of the legend title.
This class is a base class for nodes in a layer tree.
bool writeXML(QDomElement &elem, QDomDocument &doc) const override
stores state in Dom node
bool shouldDrawItem() const
Returns whether the item should be drawn in the current context.
bool readXML(const QDomElement &itemElem, const QDomDocument &doc) override
sets state from Dom document
void setFontColor(const QColor &c)
bool isLayer(QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
void setColumnSpace(double s)
bool autoUpdateModel() const
void setFlag(Flag f, bool on=true)
Enable or disable a model flag.
virtual void writeXML(QDomElement &parentElement) override
Write group (tree) as XML element <layer-tree-group> and add it to the given parent element...
Graphics scene for map printing.
QSizeF minimumSize()
Run the layout algorithm and determine the size required for legend.
QFont styleFont(QgsComposerLegendStyle::Style s) const
void setLegendFilterByMapEnabled(bool enabled)
Set whether legend items should be filtered to show just the ones visible in the associated map...
Object representing map window.
void setSymbolWidth(double w)
QSizeF paintAndDetermineSize(QPainter *painter)
Paints the legend and calculates its size.
void setStyle(QgsComposerLegendStyle::Style s, const QgsComposerLegendStyle style)
void setRootGroup(QgsLayerTreeGroup *newRootGroup)
Reset the model and use a new root group node.
void invalidateCurrentMap()
Sets mCompositionMap to 0 if the map is deleted.
virtual QString displayName() const override
Get item display name.
QgsRectangle * currentMapExtent()
Returns a pointer to the current map extent, which is either the original user specified extent or th...
double symbolWidth() const
virtual bool isRemoved() const
Returns whether this item has been removed from the composition.
void setWmsLegendWidth(double w)
void writeXML(QString name, QDomElement &elem, QDomDocument &doc) const
Qt::AlignmentFlag titleAlignment() const
Returns the alignment of the legend title.
bool useAdvancedEffects() const
Returns true if a composition should use advanced effects such as blend modes.
int id() const
Get identification number.
QgsComposition * mComposition
Composer legend components style.
void setWmsLegendSize(QSizeF s)
QRectF evalItemRect(const QRectF &newRect, const bool resizeOnly=false)
Evaluates an item's bounding rect to consider data defined position and size of item and reference po...
void setUseAdvancedEffects(bool use)
void setAutoUpdateModel(bool autoUpdate)
bool _writeXML(QDomElement &itemElem, QDomDocument &doc) const
Writes parameter that are not subclass specific in document.
void setStyleMargin(QgsComposerLegendStyle::Style s, double margin)
Set style margin.
QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer. No type checking is done - use isLayer() to find out whether this operation is ...
void setWrapChar(const QString &t)
void setOutputSize(const QSize &size)
Set the size of the resulting map image.
virtual void drawBackground(QPainter *p)
Draw background.
void setColumnCount(int c)
double wmsLegendHeight() const
static QgsProject * instance()
access to canonical QgsProject instance
void setExtent(const QgsRectangle &rect)
Set coordinates of the rectangle which should be rendered.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
virtual void setSceneRect(const QRectF &rectangle)
Sets this items bound in scene coordinates such that 1 item size units corresponds to 1 scene size un...
int equalColumnWidth() const
void setEqualColumnWidth(bool s)
void setStyle(QgsComposerLegendStyle::Style s, const QgsComposerLegendStyle style)
void setTitleAlignment(Qt::AlignmentFlag alignment)
Sets the alignment of the legend title.
QStringList layers() const
Get list of layer IDs for map rendering The layers are stored in the reverse order of how they are re...
void setColumnCount(int c)
QgsComposerItem(QgsComposition *composition, bool manageZValue=true)
Constructor.
QSizeF symbolSize() const
QSizeF wmsLegendSize() const
QgsLayerTreeNode * index2node(const QModelIndex &index) const
Return layer tree node for given index.
void setFontColor(const QColor &c)
const QgsComposerMap * getComposerMapById(const int id) const
Returns the composer map with specified id.
double width() const
Width of the rectangle.
Represents a vector layer which manages a vector based data sets.
void setBoxSpace(double s)
Allow reordering with drag'n'drop.
double symbolHeight() const
int equalColumnWidth() const
static void _readOldLegendGroup(QDomElement &elem, QgsLayerTreeGroup *parentGroup)
void setSplitLayer(bool s)
double height() const
Height of the rectangle.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
void setCustomProperty(const QString &key, const QVariant &value)
Set a custom property for the node.
Layer tree node points to a map layer.
The QgsLegendRenderer class handles automatic layout and rendering of legend.
QgsMapSettings mapSettings(const QgsRectangle &extent, const QSizeF &size, int dpi) const
Return map settings that would be used for drawing of the map.
void setFont(const QFont &font)
QString id() const
Get item's id (which is not necessarly unique)