33 #include <QDomDocument> 34 #include <QDomElement> 39 , mCustomLayerTree( nullptr )
40 , mComposerMap( nullptr )
41 , mLegendFilterByMap( false )
42 , mFilterOutAtlas( false )
43 , mFilterAskedForUpdate( false )
45 , mInitialMapScaleCalculated( false )
46 , mForceResize( false )
47 , mSizeToContents( true )
63 , mLegendModel2(
nullptr )
64 , mCustomLayerTree(
nullptr )
65 , mComposerMap(
nullptr )
66 , mLegendFilterByMap(
false )
67 , mLegendFilterByExpression(
false )
68 , mFilterOutAtlas(
false )
69 , mFilterAskedForUpdate(
false )
71 , mInitialMapScaleCalculated(
false )
72 , mForceResize(
false )
73 , mSizeToContents(
true )
81 delete mCustomLayerTree;
86 Q_UNUSED( itemStyle );
97 if ( mFilterAskedForUpdate )
99 mFilterAskedForUpdate =
false;
100 doUpdateFilterByMap();
104 double dotsPerMM = dpi / 25.4;
122 mInitialMapScaleCalculated =
true;
128 if ( mSizeToContents )
133 mForceResize =
false;
158 if ( !mSizeToContents )
179 if ( mFilterAskedForUpdate )
181 mFilterAskedForUpdate =
false;
182 doUpdateFilterByMap();
195 if ( !mSizeToContents )
198 if ( !mInitialMapScaleCalculated )
220 mSizeToContents = enabled;
225 return mSizeToContents;
232 delete mCustomLayerTree;
233 mCustomLayerTree = rootGroup;
259 return !mCustomLayerTree;
264 mLegendFilterByMap = enabled;
353 updateFilterByMap(
false );
389 composerLegendElem.
setAttribute(
"resizeToContents", mSizeToContents );
397 composerLegendElem.
appendChild( composerLegendStyles );
405 if ( mCustomLayerTree )
408 mCustomLayerTree->
writeXML( composerLegendElem );
411 if ( mLegendFilterByMap )
413 composerLegendElem.
setAttribute(
"legendFilterByMap",
"1" );
416 composerLegendElem.
setAttribute(
"legendFilterByAtlas", mFilterOutAtlas ?
"1" :
"0" );
418 return _writeXML( composerLegendElem, doc );
425 while ( !itemElem.
isNull() )
428 if ( itemElem.
tagName() ==
"LayerItem" )
441 if ( showFeatureCount.
toInt() )
447 else if ( itemElem.
tagName() ==
"GroupItem" )
475 if ( colCount < 1 ) colCount = 1;
481 if ( !stylesNodeList.
isEmpty() )
488 style.
readXML( styleElem, doc );
519 mSizeToContents = itemElem.
attribute(
"resizeToContents",
"1" ) !=
"0";
522 mLegendFilterByMap = itemElem.
attribute(
"legendFilterByMap",
"0" ).
toInt();
527 mFilterOutAtlas = itemElem.
attribute(
"legendFilterByAtlas",
"0" ).
toInt();
530 if ( !oldLegendModelElem.
isNull() )
535 setCustomLayerTree( nodeRoot );
546 if ( !composerItemList.
isEmpty() )
555 if ( !titleFontString.
isEmpty() )
561 if ( !groupFontString.
isEmpty() )
568 if ( !layerFontString.
isEmpty() )
574 if ( !itemFontString.
isEmpty() )
600 if ( !
id().isEmpty() )
609 return tr(
"<legend>" );
627 disconnect( mComposerMap, SIGNAL( extentChanged() ),
this, SLOT( updateFilterByMap() ) );
628 disconnect( mComposerMap, SIGNAL( layerStyleOverridesChanged() ),
this, SLOT( mapLayerStyleOverridesChanged() ) );
637 QObject::connect( map, SIGNAL( extentChanged() ),
this, SLOT( updateFilterByMap() ) );
638 QObject::connect( map, SIGNAL( layerStyleOverridesChanged() ),
this, SLOT( mapLayerStyleOverridesChanged() ) );
649 void QgsComposerLegend::mapLayerStyleOverridesChanged()
655 if ( mLegendFilterByMap )
659 updateFilterByMap(
false );
673 void QgsComposerLegend::updateFilterByMap(
bool redraw )
680 mFilterAskedForUpdate =
true;
686 void QgsComposerLegend::doUpdateFilterByMap()
696 if ( mComposerMap && ( mLegendFilterByMap || filterByExpression || mInAtlas ) )
713 mLegendModel2->
setLegendFilter( &ms, mInAtlas || mLegendFilterByMap, filterPolygon,
true );
723 mFilterOutAtlas = doFilter;
728 return mFilterOutAtlas;
731 void QgsComposerLegend::onAtlasFeature(
QgsFeature* feat )
735 mInAtlas = mFilterOutAtlas;
739 void QgsComposerLegend::onAtlasEnded()
761 if (
QgsLayerTree::isLayer( node ) && ( role == Qt::DisplayRole || role == Qt::EditRole ) && !node->customProperty(
"legend/title-label" ).isNull() )
764 QString name = node->customProperty(
"legend/title-label" ).toString();
765 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.
QDomNodeList elementsByTagName(const QString &tagname) const
void setLegendSize(QSizeF s)
Set the preferred resulting legend size.
QgsMapSettings mapSettings(const QgsRectangle &extent, QSizeF size, int dpi) const
Return map settings that would be used for drawing of the map.
QgsComposerLegend(QgsComposition *composition)
A rectangle specified with double values.
Base class for all map layer types.
void setExtent(const QgsRectangle &rect, bool magnified=true)
Set coordinates of the rectangle which should be rendered.
QgsLayerTreeGroup * rootGroup() const
Return pointer to the root node of the layer tree. Always a non-null pointer.
double mapUnitsToMM() const
Returns the conversion factor map units -> mm.
void setEqualColumnWidth(bool s)
QgsComposerLegendStyle style(QgsComposerLegendStyle::Style s) const
Returns style.
QgsLayerTreeGroup * addGroup(const QString &name)
Append a new group node with given name. Newly created node is owned by this group.
static QgsLayerTreeGroup * readXML(QDomElement &element, bool looseMatch=false)
Read group (tree) from XML element <layer-tree-group> and return the newly created group (or null on ...
const QgsComposerMap * getComposerMapById(const int id) const
Returns the composer map with specified id.
void setBoxSpace(double s)
void setMmPerMapUnit(double mmPerMapUnit)
bool shouldDrawItem() const
Returns whether the item should be drawn in the current context.
void readXML(const QDomElement &elem, const QDomDocument &doc)
void setRenderHint(RenderHint hint, bool on)
void setDrawRasterBorder(bool enabled)
Sets whether a border will be drawn around raster symbol items.
QDomNode appendChild(const QDomNode &newChild)
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.
QString attribute(const QString &name, const QString &defValue) 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 itemChanged()
Emitted when the item changes.
static QString encodeColor(const QColor &color)
void setSymbolSize(QSizeF s)
void drawLegend(QPainter *painter)
Draw the legend with given painter.
int printResolution() const
void setColumnSpace(double s)
A item that forms part of a map composition.
double rasterBorderWidth() const
Returns the border width (in millimeters) for the border drawn around raster symbol items...
void setOutputDpi(double dpi)
Set DPI used for conversion between real world units (e.g. mm) and pixels.
QRectF evalItemRect(const QRectF &newRect, const bool resizeOnly=false, const QgsExpressionContext *context=nullptr)
Evaluates an item's bounding rect to consider data defined position and size of item and reference po...
QDomElement nextSiblingElement(const QString &tagName) const
int id() const
Get identification number.
A geometry is the spatial representation of a feature.
QVariant data(const QModelIndex &index, int role) const override
QColor rasterBorderColor() const
Returns the border color for the border drawn around raster symbol items.
virtual void drawFrame(QPainter *p)
Draw black frame around item.
void setTitle(const QString &t)
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
bool _writeXML(QDomElement &itemElem, QDomDocument &doc) const
Writes parameter that are not subclass specific in document.
void setNamedColor(const QString &name)
double toDouble(bool *ok) const
void updateLegend()
Updates the model and all legend entries.
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
QDomNodeList childNodes() const
void writeXML(const QString &name, QDomElement &elem, QDomDocument &doc) const
QString tr(const char *sourceText, const char *disambiguation, int n)
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)
void setStyle(QgsComposerLegendStyle::Style s, const QgsComposerLegendStyle &style)
void setResizeToContents(bool enabled)
Sets whether the legend should automatically resize to fit its contents.
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 setHeight(qreal height)
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.
QDomElement toElement() const
long featureCount(QgsSymbolV2 *symbol)
Number of features rendered with specified symbol.
The QgsLayerTreeModel class is model implementation for Qt item views framework.
QSizeF wmsLegendSize() const
const char * name() const
QString number(int n, int base)
void setOutputSize(QSize size)
Set the size of the resulting map image.
void setRasterBorderWidth(double width)
Sets the border width for the border drawn around raster symbol items.
bool fromString(const QString &descrip)
int toInt(bool *ok) const
void setRasterBorderWidth(double width)
Sets the border width for the border drawn around raster symbol items.
virtual void updateItem()
Updates item, with the possibility to do custom update for subclasses.
bool equalColumnWidth() const
void setSymbolHeight(double h)
double rasterBorderWidth() const
Returns the border width (in millimeters) for the border drawn around raster symbol items...
void setLegendFilterOutAtlas(bool doFilter)
When set to true, during an atlas rendering, it will filter out legend elements where features are ou...
virtual void drawSelectionBoxes(QPainter *p)
Draws additional graphics on selected items.
void setPen(const QColor &color)
void setAttribute(const QString &name, const QString &value)
double scale() const
Return the calculated scale of the map.
void setLegendFilterByMap(const QgsMapSettings *settings)
Force only display of legend nodes which are valid for given map settings.
double width() const
Width of the rectangle.
int toInt(bool *ok, int base) const
static QgsLayerTreeModelLegendNode * index2legendNode(const QModelIndex &index)
Return legend node for given index.
bool useAdvancedEffects() const
Returns true if a composition should use advanced effects such as blend modes.
void setMapScale(double scale)
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer.
void setComposerMap(const QgsComposerMap *map)
QgsComposerLegendStyle style(QgsComposerLegendStyle::Style s) const
Returns style.
QPaintDevice * device() const
void synchronizeWithModel()
Data changed.
void setStyleFont(QgsComposerLegendStyle::Style s, const QFont &f)
Set style font.
void setTitle(const QString &t)
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.
QMap< QString, QString > layerStyleOverrides() const
Getter for stored overrides of styles for layers.
double symbolWidth() const
Qt::AlignmentFlag titleAlignment() const
Returns the alignment of the legend title.
double wmsLegendHeight() const
void setDrawRasterBorder(bool enabled)
Sets whether a border will be drawn around raster symbol items.
bool readXML(const QDomElement &itemElem, const QDomDocument &doc) override
Sets state from Dom document.
void setFontColor(const QColor &c)
static bool hasLegendFilterExpression(const QgsLayerTreeGroup &group)
Test if one of the layers in a group has an expression filter.
bool isLayer(QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
void setColumnSpace(double s)
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.
virtual void updateItem() override
Update() overloading.
QSizeF minimumSize()
Run the layout algorithm and determine the size required for legend.
void setLegendFilterByMapEnabled(bool enabled)
Set whether legend items should be filtered to show just the ones visible in the associated map...
const QgsMapSettings & mapSettings() const
Return setting of QGIS map canvas.
Object representing map window.
void setSymbolWidth(double w)
QSizeF paintAndDetermineSize(QPainter *painter)
Paints the legend and calculates its size.
void setRasterBorderColor(const QColor &color)
Sets the border color for the border drawn around raster symbol items.
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.
bool blockSignals(bool block)
bool autoUpdateModel() const
void refreshLayerLegend(QgsLayerTreeLayer *nodeLayer)
Force a refresh of legend nodes of a layer node.
virtual bool isRemoved() const
Returns whether this item has been removed from the composition.
void setWmsLegendWidth(double w)
QSizeF symbolSize() const
bool resizeToContents() const
Returns whether the legend should automatically resize to fit its contents.
QgsMapLayer * layer() const
double symbolHeight() const
QgsLegendModelV2(QgsLayerTreeGroup *rootNode, QObject *parent=nullptr)
QgsComposition * mComposition
Composer legend components style.
void setWmsLegendSize(QSizeF s)
QgsGeometry currentGeometry(const QgsCoordinateReferenceSystem &projectedTo=QgsCoordinateReferenceSystem()) const
Returns the current atlas geometry in the given projection system (default to the coverage layer's CR...
void setUseAdvancedEffects(bool use)
static QgsMapLayerRegistry * instance()
Returns the instance pointer, creating the object on the first call.
void setClipRect(const QRectF &rectangle, Qt::ClipOperation operation)
void setAutoUpdateModel(bool autoUpdate)
QgsLayerTreeNode * index2node(const QModelIndex &index) const
Return layer tree node for given index.
void setStyleMargin(QgsComposerLegendStyle::Style s, double margin)
Set style margin.
const QgsComposition * composition() const
Returns the composition the item is attached to.
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 setWidth(qreal width)
bool equalColumnWidth() const
virtual void drawBackground(QPainter *p)
Draw background.
void setColumnCount(int c)
double columnSpace() const
static QgsProject * instance()
Returns the QgsProject singleton instance.
QDomElement firstChildElement(const QString &tagName) const
void requestedExtent(QgsRectangle &extent) const
Calculates the extent to request and the yShift of the top-left point in case of rotation.
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...
void setEqualColumnWidth(bool s)
void setTitleAlignment(Qt::AlignmentFlag alignment)
Sets the alignment of the legend title.
QString left(int n) const
void setStyle(QgsComposerLegendStyle::Style s, const QgsComposerLegendStyle &style)
QgsAtlasComposition & atlasComposition()
double wmsLegendWidth() const
static QColor decodeColor(const QString &str)
void setLayerSet(const QStringList &layerIds, double scaleDenominator=-1, const QString &rule="")
Flags flags() const
Return OR-ed combination of model flags.
void setLayerStyleOverrides(const QMap< QString, QString > &overrides)
Set map of map layer style overrides (key: layer ID, value: style name) where a different style shoul...
void setColumnCount(int c)
QList< QgsLayerTreeLayer * > findLayers() const
Find all layer nodes. Searches recursively the whole sub-tree.
QgsComposerItem(QgsComposition *composition, bool manageZValue=true)
Constructor.
QColor rasterBorderColor() const
Returns the border color for the border drawn around raster symbol items.
QDomElement createElement(const QString &tagName)
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 setFontColor(const QColor &c)
bool legendFilterOutAtlas() const
Whether to filter out legend elements outside of the current atlas feature.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setLegendFilter(const QgsMapSettings *settings, bool useExtent=true, const QgsGeometry &polygon=QgsGeometry(), bool useExpressions=true)
Filter display of legend nodes for given map settings.
Represents a vector layer which manages a vector based data sets.
bool drawRasterBorder() const
Returns whether a border will be drawn around raster symbol items.
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QString id() const
Get item's id (which is not necessarly unique)
const QgsRectangle * currentMapExtent() const
Returns a pointer to the current map extent, which is either the original user specified extent or th...
void setBoxSpace(double s)
Allow reordering with drag'n'drop.
void destroyed(QObject *obj)
double columnSpace() const
static void _readOldLegendGroup(QDomElement &elem, QgsLayerTreeGroup *parentGroup)
void setSplitLayer(bool s)
bool drawRasterBorder() const
Returns whether a border will be drawn around raster symbol items.
void setRasterBorderColor(const QColor &color)
Sets the border color for the border drawn around raster symbol items.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
double height() const
Height of the rectangle.
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.
QDomNode at(int index) const
QFont styleFont(QgsComposerLegendStyle::Style s) const
Qt::AlignmentFlag titleAlignment() const
Returns the alignment of the legend title.
void setFont(const QFont &font)