23 #include <QTextStream> 38 , mRootNode( rootNode )
39 , mFlags( ShowLegend | AllowLegendChangeState | DeferredLegendInvalidation )
40 , mAutoCollapseLegendNodesCount( -1 )
41 , mLegendFilterByScale( 0 )
42 , mLegendMapViewMupp( 0 )
43 , mLegendMapViewDpi( 0 )
44 , mLegendMapViewScale( 0 )
97 if ( column < 0 || column >=
columnCount( parent ) ||
98 row < 0 || row >=
rowCount( parent ) )
141 Q_ASSERT( parentNode );
144 if ( !grandParentNode )
148 Q_ASSERT( row >= 0 );
150 return createIndex( row, 0, static_cast<QObject*>( parentNode ) );
163 if ( role == Qt::DisplayRole || role == Qt::EditRole )
172 if ( nodeLayer->
customProperty(
"showFeatureCount", 0 ).
toInt() && role == Qt::DisplayRole )
181 else if ( role == Qt::DecorationRole && index.
column() == 0 )
238 icon =
QIcon( pixmap );
244 else if ( role == Qt::CheckStateRole )
265 else if ( role == Qt::FontRole )
274 else if ( role == Qt::ToolTipRole )
279 return layer->publicSource();
293 rootFlags |= Qt::ItemIsDropEnabled;
303 f |= Qt::ItemIsEditable;
312 f |= Qt::ItemIsDragEnabled;
316 f |= Qt::ItemIsUserCheckable;
319 f |= Qt::ItemIsDropEnabled;
331 bool res = sym->
setData( value, role );
341 if ( role == Qt::CheckStateRole )
362 else if ( role == Qt::EditRole )
391 Q_ASSERT( row >= 0 );
392 return index( row, 0, parentIndex );
401 if ( child->
parent() == node )
453 return symNode ? symNode->
layerNode() :
nullptr;
574 if ( useExpressions )
580 if ( enabled && !expr.
isEmpty() )
582 exprs[ nodeLayer->
layerId()] = expr;
587 if ( useExpressions && !useExtent && !polygonValid )
658 for (
int i = indexFrom; i <= indexTo; ++i )
666 return newLayerNodes;
781 if ( !nodeLayer->
layer() )
840 disconnect( nodeLayer,
nullptr,
this,
nullptr );
842 if ( !nodeLayer->
layer() )
911 for (
int i = 0; i < count; ++i )
918 return Qt::CopyAction | Qt::MoveAction;
924 types <<
"application/qgis.layertreemodeldata";
932 QModelIndexList sortedIndexes = indexes;
933 qSort( sortedIndexes.begin(), sortedIndexes.end(), qLess<QModelIndex>() );
949 mimeData->
setData(
"application/qgis.layertreemodeldata", txt.
toUtf8() );
955 if ( action == Qt::IgnoreAction )
958 if ( !data->
hasFormat(
"application/qgis.layertreemodeldata" ) )
968 QByteArray encodedData = data->
data(
"application/qgis.layertreemodeldata" );
975 if ( rootElem.
tagName() !=
"layer_tree_model_data" )
993 if ( parent.
isValid() && row == -1 )
1032 return mFlags.testFlag( f );
1101 if (
mLegend.contains( nodeLayer ) )
1103 qDeleteAll(
mLegend[nodeLayer].originalNodes );
1104 delete mLegend[nodeLayer].tree;
1112 if ( !nodeL->
layer() )
1121 if ( hasStyleOverride )
1131 bool isEmbedded = filteredLstNew.
count() == 1 && filteredLstNew[0]->isEmbeddedInParent();
1142 data.
tree =
nullptr;
1156 if ( hasStyleOverride )
1168 bool hasParentKeys =
false;
1173 hasParentKeys =
true;
1177 if ( !hasParentKeys )
1182 rule2node[
QString()] =
nullptr;
1188 if ( rule2node.
contains( ruleKey ) )
1190 rule2node[ruleKey] = n;
1219 int row = data.
tree->
children[parentLegendNode].indexOf( legendNode );
1220 return index( row, 0, parentIndex );
1225 int row = data.
tree->
children[
nullptr].indexOf( legendNode );
1226 return index( row, 0, parentIndex );
1231 Q_ASSERT( parentIndex.
isValid() );
1235 return index( row, 0, parentIndex );
1254 if ( !
mLegend.contains( nL ) )
1267 Q_ASSERT(
mLegend.contains( nL ) );
1270 return createIndex( row, column, static_cast<QObject*>( data.tree->children[
nullptr].at( row ) ) );
1272 return createIndex( row, column, static_cast<QObject*>( data.activeNodes.at( row ) ) );
1295 int row = data.
tree->
children[grandParentNode].indexOf( parentNode );
1296 return createIndex( row, 0, static_cast<QObject*>( parentNode ) );
1310 return node->
data( role );
1318 f &= ~
Qt::ItemIsUserCheckable;
1332 return QIcon( qvariant_cast<QPixmap>(
mLegend[nodeLayer].activeNodes[0]->
data( Qt::DecorationRole ) ) );
1338 return mLegend.value( nodeLayer ).activeNodes;
1343 return mLegend.value( nodeLayer ).originalNodes;
1349 for ( ; it !=
mLegend.constEnd(); ++it )
1352 if ( layer->
layerId() == layerId )
1399 widthMax[parentKey] = qMax( sz.width(), widthMax.contains( parentKey ) ? widthMax[parentKey] : 0 );
1407 Q_ASSERT( widthMax[parentKey] > 0 );
1408 const int twiceMarginWidth = 2;
bool restoreOverrideStyle()
Restore the original store after a call to setOverrideStyle()
QObject * child(const char *objName, const char *inheritsClass, bool recursiveSearch) const
Layer tree group node serves as a container for layers and further groups.
void legendInvalidateMapBasedData()
double mLegendMapViewMupp
static const QIcon & iconGroup()
void removeChildren(int from, int count)
Remove child nodes from index "from". The nodes will be deleted.
QList< QgsLayerTreeModelLegendNode * > originalNodes
Data structure for storage of legend nodes.
Base class for all map layer types.
QgsLayerTreeGroup * rootGroup() const
Return pointer to the root node of the layer tree. Always a non-null pointer.
void setLayerName(const QString &n)
QMimeData * mimeData(const QModelIndexList &indexes) const override
QList< QgsLayerTreeModelLegendNode * > layerLegendNodes(QgsLayerTreeLayer *nodeLayer)
Return filtered list of active legend nodes attached to a particular layer node.
QByteArray data(const QString &mimeType) const
virtual Qt::ItemFlags flags() const
Return item flags associated with the item.
void removeLegendFromLayer(QgsLayerTreeLayer *nodeLayer)
void connectToLayer(QgsLayerTreeLayer *nodeLayer)
Q_DECL_DEPRECATED bool isIndexSymbologyNode(const QModelIndex &index) const
Return true if index represents a legend node (instead of layer node)
void setCurrentIndex(const QModelIndex ¤tIndex)
Set index of the current item. May be used by view. Item marked as current is underlined.
bool contains(const Key &key) const
QList< QgsLayerTreeNode * > indexes2nodes(const QModelIndexList &list, bool skipInternal=false) const
Convert a list of indexes to a list of layer tree nodes.
LayerLegendTree * tree
Optional pointer to a tree structure - see LayerLegendTree for details.
QDomNode appendChild(const QDomNode &newChild)
static const QIcon & iconDefault()
QList< QgsLayerTreeModelLegendNode * > layerOriginalLegendNodes(QgsLayerTreeLayer *nodeLayer)
Return original (unfiltered) list of legend nodes attached to a particular layer node.
void nodeCustomPropertyChanged(QgsLayerTreeNode *node, const QString &key)
void addLegendToLayer(QgsLayerTreeLayer *nodeL)
QgsMapLayerLegend * legend() const
Can be null.
static const QIcon & iconPoint()
virtual bool setData(const QVariant &value, int role)
Set some data associated with the item.
void disconnectFromRootNode()
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
QModelIndex indexOfParentLayerTreeNode(QgsLayerTreeNode *parentNode) const
QString toString(int indent) const
bool testFlag(Flag f) const
Check whether a flag is enabled.
static QIcon getThemeIcon(const QString &theName)
Helper to get a theme icon.
static int _numLayerCount(QgsLayerTreeGroup *group, const QString &layerId)
const T & at(int i) const
const QObjectList & children() const
Qt::CheckState isVisible() const
Structure that stores tree representation of map layer's legend.
void setUnderline(bool enable)
QDomElement nextSiblingElement(const QString &tagName) const
QgsLayerTreeLayer * layerNode() const
Return pointer to the parent layer node.
QgsLayerTreeModelLegendNode * findLegendNode(const QString &layerId, const QString &ruleKey) const
Searches through the layer tree to find a legend node with a matching layer ID and rule key...
QFont layerTreeNodeFont(int nodeType) const
Get font for a particular type of layer tree node. nodeType should come from QgsLayerTreeNode::NodeTy...
QPixmap fromImage(const QImage &image, QFlags< Qt::ImageConversionFlag > flags)
A geometry is the spatial representation of a feature.
void recursivelyEmitDataChanged(const QModelIndex &index=QModelIndex())
emit dataChanged() for layer tree node items
static bool _isChildOfNode(QgsLayerTreeNode *child, QgsLayerTreeNode *node)
void legendNodeDataChanged()
QDomElement documentElement() const
static const QIcon & iconPolygon()
static bool _isChildOfNodes(QgsLayerTreeNode *child, const QList< QgsLayerTreeNode *> &nodes)
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
Flags mFlags
Set of flags for the model.
virtual bool isEditable() const override
Returns true if the provider is in editing mode.
QModelIndex node2index(QgsLayerTreeNode *node) const
Return index for a given node. If the node does not belong to the layer tree, the result is undefined...
QgsMapLayer::LayerType type() const
Get the type of the layer.
void tryBuildLegendTree(LayerLegendData &data)
static QPixmap getThemePixmap(const QString &theName)
Helper to get a theme icon as a pixmap.
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
virtual void writeXML(QDomElement &parentElement)=0
Write layer tree to XML.
void nodeAddedChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
bool legendEmbeddedInParent(QgsLayerTreeLayer *nodeLayer) const
QgsLayerTreeGroup * toGroup(QgsLayerTreeNode *node)
Cast node to a group. No type checking is done - use isGroup() to find out whether this operation is ...
void nodeWillAddChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
Allow check boxes for legend nodes (if supported by layer's legend)
QPixmap pixmap(const QSize &size, Mode mode, State state) const
void connectToLayers(QgsLayerTreeGroup *parentGroup)
void layerLegendChanged()
The QgsMapSettings class contains configuration for rendering of the map.
int indexOf(const T &value, int from) const
QVariant legendNodeData(QgsLayerTreeModelLegendNode *node, int role) const
Q_DECL_DEPRECATED QgsLayerTreeLayer * layerNodeForSymbologyNode(const QModelIndex &index) const
Return layer node to which a legend node belongs to.
QgsMapLayerStyleManager * styleManager() const
Get access to the layer's style manager.
void setBold(bool enable)
long featureCount(QgsSymbolV2 *symbol)
Number of features rendered with specified symbol.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Allow renaming of groups and layers.
const char * name() const
rule key of the parent legend node - for legends with tree hierarchy (QString). Added in 2...
QModelIndex legendParent(QgsLayerTreeModelLegendNode *legendNode) const
int count(const T &value) const
void append(const T &value)
QString fromUtf8(const char *str, int size)
void disconnectFromLayer(QgsLayerTreeLayer *nodeLayer)
QString id() const
Get this layer's unique ID, this ID is used to access this layer from map layer registry.
QMap< QgsLayerTreeModelLegendNode *, QgsLayerTreeModelLegendNode * > parents
Pointer to parent for each active node. Top-level nodes have null parent. Pointers are not owned...
virtual bool isScaleOK(double scale) const
QTimer mDeferLegendInvalidationTimer
int toInt(bool *ok) const
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
QModelIndex legendNode2index(QgsLayerTreeModelLegendNode *legendNode)
Return index for a given legend node.
static const QIcon & iconRaster()
void disconnectFromLayers(QgsLayerTreeGroup *parentGroup)
rule key of the node (QString)
void setLegendFilterByMap(const QgsMapSettings *settings)
Force only display of legend nodes which are valid for given map settings.
virtual QVariant data(int role) const override
Return data associated with the item.
static QgsLayerTreeModelLegendNode * index2legendNode(const QModelIndex &index)
Return legend node for given index.
Qt::DropActions supportedDropActions() const override
QgsLayerTreeNode * parent()
Get pointer to the parent. If parent is a null pointer, the node is a root node.
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
QMap< QString, QString > layerStyleOverrides() const
Get map of map layer style overrides (key: layer ID, value: style name) where a different style shoul...
void beginRemoveRows(const QModelIndex &parent, int first, int last)
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer.
void legendMapViewData(double *mapUnitsPerPixel, int *dpi, double *scale)
Get hints about map view - to be used in legend nodes.
void setName(const QString &n)
Set group's name.
The QgsMapLayerLegend class is abstract interface for implementations of legends for one map layer...
bool isEmpty() const
Returns true if the geometry is empty (ie, contains no underlying geometry accessible via geometry)...
void * internalPointer() const
This class is a base class for nodes in a layer tree.
QgsLayerTreeGroup * mRootNode
Pointer to the root node of the layer tree. Not owned by the model.
void setVisible(Qt::CheckState visible)
void insertChildNodes(int index, const QList< QgsLayerTreeNode *> &nodes)
Insert existing nodes at specified position. The nodes must not have a parent yet. The nodes will be owned by this group.
bool hasValidSettings() const
Check whether the map settings are valid and can be used for rendering.
QGis::GeometryType geometryType() const
Returns point, line or polygon.
int autoCollapseLegendNodes() const
Return at what number of legend nodes the layer node should be collapsed. -1 means no auto-collapse (...
QMap< QgsLayerTreeModelLegendNode *, QList< QgsLayerTreeModelLegendNode * > > children
List of children for each active node. Top-level nodes are under null pointer key. Pointers are not owned.
static QgsLayerTreeNode * readXML(QDomElement &element)
Read layer tree from XML. Returns new instance.
Class that runs a hit test with given map settings.
int legendNodeRowCount(QgsLayerTreeModelLegendNode *node) const
void setLegendFilterByScale(double scaleDenominator)
Force only display of legend nodes which are valid for given scale denominator.
QList< QgsLayerTreeNode * > children()
Get list of children of the node. Children are owned by the parent.
QGis::GeometryType type() const
Returns type of the geometry as a QGis::GeometryType.
bool isLayer(QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
void setLayerTreeNodeFont(int nodeType, const QFont &font)
Set font for a particular type of layer tree node. nodeType should come from QgsLayerTreeNode::NodeTy...
void setFlag(Flag f, bool on=true)
Enable or disable a model flag.
QModelIndex legendNodeIndex(int row, int column, QgsLayerTreeModelLegendNode *node) const
QModelIndex createIndex(int row, int column, void *ptr) const
QPersistentModelIndex mCurrentIndex
Current index - will be underlined.
void setRootGroup(QgsLayerTreeGroup *newRootGroup)
Reset the model and use a new root group node.
const T value(const Key &key) const
double mLegendMapViewScale
double mLegendFilterByScale
scale denominator for filtering of legend nodes (<= 0 means no filtering)
void setParent(QObject *parent)
defer legend model invalidation
void refreshLayerLegend(QgsLayerTreeLayer *nodeLayer)
Force a refresh of legend nodes of a layer node.
void setExpanded(bool expanded)
Set whether the node should be shown as expanded or collapsed in GUI.
void setLegendMapViewData(double mapUnitsPerPixel, int dpi, double scale)
Give the layer tree model hints about the currently associated map view so that legend nodes that use...
void setItalic(bool enable)
QgsLayerTreeLayer * findLayer(const QString &layerId) const
Find layer node representing the map layer specified by its ID. Searches recursively the whole sub-tr...
void beginInsertRows(const QModelIndex &parent, int first, int last)
QgsMapLayer * layer() const
static const QIcon & iconTable()
void setAutoCollapseLegendNodes(int nodeCount)
Set at what number of legend nodes the layer node should be collapsed. Setting -1 disables the auto-c...
leaf node pointing to a layer
QImage previewAsImage(QSize size, const QColor &bgColor=Qt::white, QImage::Format format=QImage::Format_ARGB32_Premultiplied)
Draws a preview of the rasterlayer into a QImage.
QIcon legendIconEmbeddedInParent(QgsLayerTreeLayer *nodeLayer) const
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
int mAutoCollapseLegendNodesCount
Minimal number of nodes when legend should be automatically collapsed. -1 = disabled.
QgsLayerTreeNode * index2node(const QModelIndex &index) const
Return layer tree node for given index.
QList< QgsLayerTreeModelLegendNode * > filterLegendNodes(const QList< QgsLayerTreeModelLegendNode *> &nodes)
Filter nodes from QgsMapLayerLegend according to the current filtering rules.
QSize minimumIconSize() const
Get the minimum icon size to prevent cropping.
QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer. No type checking is done - use isLayer() to find out whether this operation is ...
static void applyLayerNodeProperties(QgsLayerTreeLayer *nodeLayer, QList< QgsLayerTreeModelLegendNode *> &nodes)
update according to layer node's custom properties (order of items, user labels for items) ...
QString name() const
Get group's name.
void nodeRemovedChildren()
QDomElement firstChildElement(const QString &tagName) const
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
virtual bool isModified() const
Returns true if the provider has been modified since the last commit.
QStringList mimeTypes() const override
void invalidateLegendMapBasedData()
int legendRootRowCount(QgsLayerTreeLayer *nL) const
virtual QVariant data(int role) const =0
Return data associated with the item.
static const QIcon & iconLine()
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
Qt::CheckState isVisible() const
Return the check state of the group node.
virtual bool setData(const QModelIndex &index, const QVariant &value, int role)
for QgsSymbolV2LegendNode only - legacy rule key (void ptr, to be cast to QgsSymbolV2 ptr) ...
QString layerName() const
Allow user to set node visibility with a check box.
Implementation of legend node interface for displaying preview of vector symbols and their labels and...
bool contains(const Key &key) const
Flags flags() const
Return OR-ed combination of model flags.
bool setOverrideStyle(const QString &styleDef)
Temporarily apply a different style to the layer.
void setData(const QString &mimeType, const QByteArray &data)
QScopedPointer< QgsMapHitTest > mLegendFilterHitTest
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...
QList< QgsLayerTreeLayer * > findLayers() const
Find all layer nodes. Searches recursively the whole sub-tree.
QDomElement createElement(const QString &tagName)
QModelIndex legendRootIndex(int row, int column, QgsLayerTreeLayer *nL) const
container of other groups and layers
int columnCount(const QModelIndex &parent=QModelIndex()) const override
QList< QgsLayerTreeModelLegendNode * > activeNodes
Active legend nodes.
For legends that support it, will show them in a tree instead of a list (needs also ShowLegend)...
QMap< QString, QString > mLayerStyleOverrides
Overrides of map layers' styles: key = layer ID, value = style XML.
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.
Will use real preview of raster layer as icon (may be slow)
void setVisible(Qt::CheckState state)
Set check state of the group node - will also update children.
void setFlags(const QgsLayerTreeModel::Flags &f)
Set OR-ed combination of model flags.
QScopedPointer< QgsMapSettings > mLegendFilterMapSettings
bool isGroup(QgsLayerTreeNode *node)
Check whether the node is a valid group node.
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
int count(const Key &key) const
void setIconSize(QSize sz)
Set the icon size.
static QList< QgsLayerTreeLayer * > _layerNodesInSubtree(QgsLayerTreeNode *node, int indexFrom, int indexTo)
static QString legendFilterByExpression(const QgsLayerTreeLayer &layer, bool *enabled=nullptr)
Return the expression filter of a legend layer.
Allow reordering with drag'n'drop.
Add legend nodes for layer nodes.
virtual void invalidateMapBasedData()
Notification from model that information from associated map view has changed.
void nodeWillRemoveChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
QgsLayerTreeModel(QgsLayerTreeGroup *rootNode, QObject *parent=nullptr)
Construct a new tree model with given layer tree (root node must not be null pointer).
void nodeLayerWillBeUnloaded()
virtual QList< QgsLayerTreeModelLegendNode * > createLayerTreeModelLegendNodes(QgsLayerTreeLayer *nodeLayer)=0
Return list of legend nodes to be used for a particular layer tree layer node.
QMap< QgsLayerTreeLayer *, LayerLegendData > mLegend
Per layer data about layer's legend nodes.
Qt::ItemFlags legendNodeFlags(QgsLayerTreeModelLegendNode *node) const
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Layer tree node points to a map layer.
bool setContent(const QByteArray &data, bool namespaceProcessing, QString *errorMsg, int *errorLine, int *errorColumn)
QModelIndex currentIndex() const
Get index of the item marked as current. Item marked as current is underlined.
const T value(const Key &key) const
void setSingleShot(bool singleShot)
void nodeVisibilityChanged(QgsLayerTreeNode *node)
Structure that stores all data associated with one map layer.
QByteArray toUtf8() const