24 #include <QTextStream> 37 : QAbstractItemModel( parent )
38 , mRootNode( rootNode )
39 , mFlags( ShowLegend | AllowLegendChangeState | DeferredLegendInvalidation )
40 , mAutoCollapseLegendNodesCount( -1 )
41 , mLegendFilterByScale( 0 )
42 , mLegendFilterUsesExtent( false )
43 , mLegendMapViewMupp( 0 )
44 , mLegendMapViewDpi( 0 )
45 , mLegendMapViewScale( 0 )
62 if ( !index.isValid() )
65 QObject *obj =
reinterpret_cast<QObject *
>( index.internalPointer() );
98 if ( column < 0 || column >=
columnCount( parent ) ||
99 row < 0 || row >=
rowCount( parent ) )
100 return QModelIndex();
107 return QModelIndex();
114 return createIndex( row, column, static_cast<QObject *>( n->
children().at( row ) ) );
120 if ( !child.isValid() )
121 return QModelIndex();
134 return QModelIndex();
142 Q_ASSERT( parentNode );
145 if ( !grandParentNode )
146 return QModelIndex();
148 int row = grandParentNode->
children().indexOf( parentNode );
149 Q_ASSERT( row >= 0 );
151 return createIndex( row, 0, static_cast<QObject *>( parentNode ) );
157 if ( !index.isValid() || index.column() > 1 )
164 if ( role == Qt::DisplayRole || role == Qt::EditRole )
172 QString name = nodeLayer->
name();
173 if ( nodeLayer->
customProperty( QStringLiteral(
"showFeatureCount" ), 0 ).toInt() && role == Qt::DisplayRole )
177 name += QStringLiteral(
" [%1]" ).arg( vlayer->
featureCount() );
182 else if ( role == Qt::DecorationRole && index.column() == 0 )
225 QPixmap pixmap( icon.pixmap( 16, 16 ) );
227 QPainter painter( &pixmap );
231 icon = QIcon( pixmap );
237 else if ( role == Qt::CheckStateRole )
257 else if ( role == Qt::FontRole )
261 f.setUnderline(
true );
264 else if ( role == Qt::ForegroundRole )
266 QBrush brush( Qt::black, Qt::SolidPattern );
272 brush.setColor( Qt::lightGray );
277 else if ( role == Qt::ToolTipRole )
284 !layer->
title().isEmpty() ? layer->title() :
285 !layer->shortName().isEmpty() ? layer->shortName() :
288 title =
"<b>" + title.toHtmlEscaped() +
"</b>";
290 if ( layer->crs().isValid() )
292 if (
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer ) )
295 title += tr(
" (%1)" ).arg( layer->crs().authid() ).toHtmlEscaped();
301 if ( !layer->abstract().isEmpty() )
304 const QStringList abstractLines = layer->abstract().split(
"\n" );
305 for (
const auto &l : abstractLines )
307 parts << l.toHtmlEscaped();
312 parts <<
"<i>" + layer->publicSource().toHtmlEscaped() +
"</i>";
314 return parts.join( QStringLiteral(
"<br/>" ) );
325 if ( !index.isValid() )
327 Qt::ItemFlags rootFlags = Qt::ItemFlags();
329 rootFlags |= Qt::ItemIsDropEnabled;
336 Qt::ItemFlags f = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
339 f |= Qt::ItemIsEditable;
342 bool isEmbedded = node->
customProperty( QStringLiteral(
"embedded" ) ).toInt();
347 if ( !isEmbedded || ( isEmbedded && node->
parent() && !node->
parent()->
customProperty( QStringLiteral(
"embedded" ) ).toInt() ) )
348 f |= Qt::ItemIsDragEnabled;
352 f |= Qt::ItemIsUserCheckable;
355 f |= Qt::ItemIsDropEnabled;
367 bool res = sym->
setData( value, role );
369 emit dataChanged( index, index );
375 return QAbstractItemModel::setData( index, value, role );
377 if ( role == Qt::CheckStateRole )
382 bool checked =
static_cast< Qt::CheckState
>( value.toInt() ) == Qt::Checked;
383 if ( checked && node->
children().isEmpty() )
403 else if ( role == Qt::EditRole )
411 layer->
setName( value.toString() );
412 emit dataChanged( index, index );
417 emit dataChanged( index, index );
421 return QAbstractItemModel::setData( index, value, role );
426 if ( !node || !node->
parent() )
427 return QModelIndex();
432 Q_ASSERT( row >= 0 );
433 return index( row, 0, parentIndex );
442 if ( child->
parent() == node )
445 return _isChildOfNode( child->
parent(), node );
448 static bool _isChildOfNodes(
QgsLayerTreeNode *child,
const QList<QgsLayerTreeNode *> &nodes )
452 if ( _isChildOfNode( child, n ) )
462 QList<QgsLayerTreeNode *> nodes;
463 Q_FOREACH (
const QModelIndex &
index, list )
476 QList<QgsLayerTreeNode *> nodesFinal;
479 if ( !_isChildOfNodes( node, nodes ) )
510 emit dataChanged( idx, idx );
514 beginRemoveRows( idx, 0, std::max( oldNodeCount - 1, 0 ) );
536 if ( oldIndex.isValid() )
537 emit dataChanged( oldIndex, oldIndex );
538 if ( currentIndex.isValid() )
539 emit dataChanged( currentIndex, currentIndex );
605 if ( useExpressions )
611 if ( enabled && !expr.isEmpty() )
613 exprs[ nodeLayer->
layerId()] = expr;
618 if ( useExpressions && !useExtent && !polygonValid )
684 beginInsertRows(
node2index( node ), indexFrom, indexTo );
687 static QList<QgsLayerTreeLayer *> _layerNodesInSubtree(
QgsLayerTreeNode *node,
int indexFrom,
int indexTo )
689 QList<QgsLayerTreeNode *> children = node->
children();
690 QList<QgsLayerTreeLayer *> newLayerNodes;
691 for (
int i = indexFrom; i <= indexTo; ++i )
699 return newLayerNodes;
708 Q_FOREACH (
QgsLayerTreeLayer *newLayerNode, _layerNodesInSubtree( node, indexFrom, indexTo ) )
716 beginRemoveRows(
node2index( node ), indexFrom, indexTo );
719 Q_FOREACH (
QgsLayerTreeLayer *nodeLayer, _layerNodesInSubtree( node, indexFrom, indexTo ) )
733 emit dataChanged( index, index );
742 emit dataChanged( index, index );
805 emit dataChanged( index, index );
807 if ( nodeLayer->
customProperty( QStringLiteral(
"showFeatureCount" ) ).toInt() )
819 if ( index.isValid() )
820 emit dataChanged( index, index );
826 if ( !nodeLayer->
layer() )
885 disconnect( nodeLayer,
nullptr,
this,
nullptr );
887 if ( !nodeLayer->
layer() )
898 disconnect( nodeLayer->
layer(),
nullptr,
this, nullptr );
942 disconnect(
mRootNode,
nullptr,
this,
nullptr );
953 int count = node->
children().count();
956 emit dataChanged(
index( 0, 0, idx ),
index( count - 1, 0, idx ) );
957 for (
int i = 0; i < count; ++i )
972 emit dataChanged( idx, idx );
975 int count = node->
children().count();
976 for (
int i = 0; i < count; ++i )
982 return Qt::CopyAction | Qt::MoveAction;
988 types << QStringLiteral(
"application/qgis.layertreemodeldata" );
996 QModelIndexList sortedIndexes = indexes;
997 std::sort( sortedIndexes.begin(), sortedIndexes.end(), std::less<QModelIndex>() );
999 QList<QgsLayerTreeNode *> nodesFinal =
indexes2nodes( sortedIndexes,
true );
1001 if ( nodesFinal.isEmpty() )
1004 QMimeData *
mimeData =
new QMimeData();
1007 QDomElement rootElem = doc.createElement( QStringLiteral(
"layer_tree_model_data" ) );
1010 doc.appendChild( rootElem );
1011 QString txt = doc.toString();
1013 mimeData->setData( QStringLiteral(
"application/qgis.layertreemodeldata" ), txt.toUtf8() );
1022 if ( action == Qt::IgnoreAction )
1025 if ( !data->hasFormat( QStringLiteral(
"application/qgis.layertreemodeldata" ) ) )
1035 QByteArray encodedData = data->data( QStringLiteral(
"application/qgis.layertreemodeldata" ) );
1038 if ( !doc.setContent( QString::fromUtf8( encodedData ) ) )
1041 QDomElement rootElem = doc.documentElement();
1042 if ( rootElem.tagName() != QLatin1String(
"layer_tree_model_data" ) )
1045 QList<QgsLayerTreeNode *> nodes;
1047 QDomElement elem = rootElem.firstChildElement();
1048 while ( !elem.isNull() )
1054 elem = elem.nextSiblingElement();
1057 if ( nodes.isEmpty() )
1060 if ( parent.isValid() && row == -1 )
1099 return mFlags.testFlag( f );
1109 QList<QgsLayerTreeModelLegendNode *> filtered;
1170 if (
mLegend.contains( nodeLayer ) )
1172 qDeleteAll(
mLegend[nodeLayer].originalNodes );
1173 delete mLegend[nodeLayer].tree;
1181 if ( !nodeL || !nodeL->
layer() )
1201 int widgetsCount = ml->
customProperty( QStringLiteral(
"embeddedWidgets/count" ), 0 ).toInt();
1202 while ( widgetsCount > 0 )
1204 lstNew.insert( 0,
new EmbeddedWidgetLegendNode( nodeL ) );
1209 QList<QgsLayerTreeModelLegendNode *> filteredLstNew =
filterLegendNodes( lstNew );
1213 n->setParent(
this );
1225 embeddedNode = legendNode;
1226 filteredLstNew.removeOne( legendNode );
1237 int count = legendTree ? legendTree->
children[
nullptr].count() : filteredLstNew.count();
1239 if ( !filteredLstNew.isEmpty() ) beginInsertRows(
node2index( nodeL ), 0, count - 1 );
1245 data.
tree = legendTree;
1249 if ( !filteredLstNew.isEmpty() ) endInsertRows();
1260 bool hasParentKeys =
false;
1265 hasParentKeys =
true;
1269 if ( !hasParentKeys )
1273 QHash<QString, QgsLayerTreeModelLegendNode *> rule2node;
1274 rule2node[QString()] =
nullptr;
1278 if ( ruleKey.isEmpty() )
1280 if ( rule2node.contains( ruleKey ) )
1282 rule2node[ruleKey] = n;
1307 context->setScaleFactor( dpi / 25.4 );
1308 context->setRendererScale( scale );
1310 return validData ? context.release() :
nullptr;
1328 int row = data.
tree->
children[parentLegendNode].indexOf( legendNode );
1329 return index( row, 0, parentIndex );
1334 int row = data.
tree->
children[
nullptr].indexOf( legendNode );
1335 return index( row, 0, parentIndex );
1340 Q_ASSERT( parentIndex.isValid() );
1343 return QModelIndex();
1345 return index( row, 0, parentIndex );
1361 if ( !
mLegend.contains( nL ) )
1375 Q_ASSERT(
mLegend.contains( nL ) );
1378 return createIndex( row, column, static_cast<QObject *>( data.tree->children[
nullptr].at( row ) ) );
1380 return createIndex( row, column, static_cast<QObject *>( data.activeNodes.at( row ) ) );
1388 return createIndex( row, column, static_cast<QObject *>( data.
tree->
children[node].at( row ) ) );
1390 return QModelIndex();
1403 int row = data.
tree->
children[grandParentNode].indexOf( parentNode );
1404 return createIndex( row, 0, static_cast<QObject *>( parentNode ) );
1418 return node->
data( role );
1424 Qt::ItemFlags f = node->
flags();
1426 f &= ~Qt::ItemIsUserCheckable;
1433 return static_cast< bool >(
mLegend[nodeLayer].embeddedNodeInParent );
1438 return mLegend[nodeLayer].embeddedNodeInParent;
1447 return QIcon( qvariant_cast<QPixmap>( legendNode->
data( Qt::DecorationRole ) ) );
1453 if ( !
mLegend.contains( nodeLayer ) )
1454 return QList<QgsLayerTreeModelLegendNode *>();
1457 QList<QgsLayerTreeModelLegendNode *> lst( data.
activeNodes );
1465 return mLegend.value( nodeLayer ).originalNodes;
1470 QMap<QgsLayerTreeLayer *, LayerLegendData>::const_iterator it =
mLegend.constBegin();
1471 for ( ; it !=
mLegend.constEnd(); ++it )
1474 if ( layer->
layerId() == layerId )
1508 std::unique_ptr<QgsRenderContext> context( createTemporaryRenderContext() );
1512 QList<QgsSymbolLegendNode *> symbolNodes;
1513 QMap<QString, int> widthMax;
1521 widthMax[parentKey] = std::max( sz.width(), widthMax.contains( parentKey ) ? widthMax[parentKey] : 0 );
1523 symbolNodes.append( n );
1529 Q_ASSERT( widthMax[parentKey] > 0 );
1530 const int twiceMarginWidth = 2;
1531 n->
setIconSize( QSize( widthMax[parentKey] + twiceMarginWidth, n->
iconSize().rheight() + twiceMarginWidth ) );
Layer tree group node serves as a container for layers and further groups.
void legendInvalidateMapBasedData()
double mLegendMapViewMupp
QgsLayerTreeModel(QgsLayerTree *rootNode, QObject *parent=nullptr)
Construct a new tree model with given layer tree (root node must not be null pointer).
The class is used as a container of context for various read/write operations on other objects...
static QIcon iconRaster()
void removeChildren(int from, int count)
Remove child nodes from index "from".
QModelIndex parent(const QModelIndex &child) const override
QMap< QgsLayerTreeLayer *, LayerLegendData > mLegend
Per layer data about layer's legend nodes.
void refreshScaleBasedLayers(const QModelIndex &index=QModelIndex())
Updates layer data for scale dependent layers, should be called when map scale changes.
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
Base class for all map layer types.
virtual bool isEmbeddedInParent() const
QMimeData * mimeData(const QModelIndexList &indexes) const override
virtual Qt::ItemFlags flags() const
Returns item flags associated with the item. Default implementation returns Qt::ItemIsEnabled.
void removeLegendFromLayer(QgsLayerTreeLayer *nodeLayer)
void connectToLayer(QgsLayerTreeLayer *nodeLayer)
QList< QgsLayerTreeModelLegendNode * > originalNodes
Data structure for storage of legend nodes.
static bool isGroup(QgsLayerTreeNode *node)
Check whether the node is a valid group node.
void setCurrentIndex(const QModelIndex ¤tIndex)
Sets index of the current item. May be used by view. Item marked as current is underlined.
QList< QgsLayerTreeNode * > indexes2nodes(const QModelIndexList &list, bool skipInternal=false) const
Convert a list of indexes to a list of layer tree nodes.
bool isNull() const
Returns true if the geometry is null (ie, contains no underlying geometry accessible via geometry() )...
bool itemVisibilityChecked() const
Returns whether a node is checked (independently of its ancestors or children)
LayerLegendTree * tree
Optional pointer to a tree structure - see LayerLegendTree for details.
QVariant data(int role) const override
Returns data associated with the item. Must be implemented in derived class.
QList< QgsLayerTreeModelLegendNode * > layerOriginalLegendNodes(QgsLayerTreeLayer *nodeLayer)
Returns 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.
Restore overridden layer style on destruction.
virtual void writeXml(QDomElement &parentElement, const QgsReadWriteContext &context)=0
Write layer tree to XML.
virtual bool setData(const QVariant &value, int role)
Sets some data associated with the item. Default implementation does nothing and returns false...
void disconnectFromRootNode()
QModelIndex indexOfParentLayerTreeNode(QgsLayerTreeNode *parentNode) const
static QgsLayerTreeGroup * toGroup(QgsLayerTreeNode *node)
Cast node to a group.
bool testFlag(Flag f) const
Check whether a flag is enabled.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
QgsLayerTreeModelLegendNode * legendNodeEmbeddedInParent(QgsLayerTreeLayer *nodeLayer) const
Returns legend node that may be embedded in parent (i.e.
void setIconSize(QSize sz)
Set the icon size.
Structure that stores tree representation of map layer's legend.
void willRemoveChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
Emitted when one or more nodes will be removed from a node within the tree.
Implementation of legend node interface for displaying preview of vector symbols and their labels and...
QgsWkbTypes::GeometryType geometryType() const
Returns point, line or polygon.
QgsLayerTreeLayer * layerNode() const
Returns pointer to the parent layer node.
QgsLayerTreeModelLegendNode * embeddedNodeInParent
A legend node that is not displayed separately, its icon is instead shown within the layer node's ite...
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
Gets font for a particular type of layer tree node. nodeType should come from QgsLayerTreeNode::NodeT...
bool isInScaleRange(double scale) const
Tests whether the layer should be visible at the specified scale.
A geometry is the spatial representation of a feature.
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
bool isVisible() const
Returns whether a node is really visible (ie checked and all its ancestors checked as well) ...
void recursivelyEmitDataChanged(const QModelIndex &index=QModelIndex())
emit dataChanged() for layer tree node items
void legendNodeDataChanged()
QMap< QgsLayerTreeModelLegendNode *, QList< QgsLayerTreeModelLegendNode * > > children
List of children for each active node. Top-level nodes are under null pointer key. Pointers are not owned.
LayerLegendTree * tryBuildLegendTree(const QList< QgsLayerTreeModelLegendNode *> &nodes)
std::unique_ptr< QgsMapHitTest > mLegendFilterHitTest
static QgsLayerTreeNode * readXml(QDomElement &element, const QgsReadWriteContext &context)
Read layer tree from XML.
void layerLoaded()
Emitted when a previously unavailable layer got loaded.
bool mLegendFilterUsesExtent
whether to use map filtering
Flags mFlags
Sets of flags for the model.
bool isEditable() const override
Returns true if the provider is in editing mode.
static QIcon iconDefault()
QModelIndex node2index(QgsLayerTreeNode *node) const
Returns index for a given node. If the node does not belong to the layer tree, the result is undefine...
QgsMapLayer::LayerType type() const
Returns the type of the layer.
std::unique_ptr< QgsMapSettings > mLegendFilterMapSettings
static QPixmap getThemePixmap(const QString &name)
Helper to get a theme icon as a pixmap.
void willAddChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
Emitted when one or more nodes will be added to a node within the tree.
void nodeAddedChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
bool legendEmbeddedInParent(QgsLayerTreeLayer *nodeLayer) const
void nodeWillAddChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
Allow check boxes for legend nodes (if supported by layer's legend)
void setFlags(QgsLayerTreeModel::Flags f)
Sets OR-ed combination of model flags.
void connectToLayers(QgsLayerTreeGroup *parentGroup)
~QgsLayerTreeModel() override
void layerLegendChanged()
void removedChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
Emitted when one or more nodes has been removed from a node within the tree.
The QgsMapSettings class contains configuration for rendering of the map.
QVariant legendNodeData(QgsLayerTreeModelLegendNode *node, int role) const
QSize minimumIconSize() const
Calculates the minimum icon size to prevent cropping.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Perform transforms between map coordinates and device coordinates.
Allow renaming of groups and layers.
Rule key of the parent legend node - for legends with tree hierarchy (QString). Added in 2...
QModelIndex legendParent(QgsLayerTreeModelLegendNode *legendNode) const
virtual void setItemVisibilityCheckedRecursive(bool checked)
Check or uncheck a node and all its children (taking into account exclusion rules) ...
void disconnectFromLayer(QgsLayerTreeLayer *nodeLayer)
virtual QList< QgsLayerTreeModelLegendNode * > createLayerTreeModelLegendNodes(QgsLayerTreeLayer *nodeLayer)=0
Returns list of legend nodes to be used for a particular layer tree layer node.
QString name() const override
Returns the layer's name.
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
virtual bool isScaleOK(double scale) const
QTimer mDeferLegendInvalidationTimer
Namespace with helper functions for layer tree operations.
QModelIndex legendNode2index(QgsLayerTreeModelLegendNode *legendNode)
Returns index for a given legend node.
static QIcon iconPolygon()
void disconnectFromLayers(QgsLayerTreeGroup *parentGroup)
#define QgsDebugMsgLevel(str, level)
Rule key of the node (QString)
void setLegendFilterByMap(const QgsMapSettings *settings)
Force only display of legend nodes which are valid for given map settings.
long featureCount(const QString &legendKey) const
Number of features rendered with specified legend key.
void addedChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
Emitted when one or more nodes have been added to a node within the tree.
static QgsLayerTreeModelLegendNode * index2legendNode(const QModelIndex &index)
Returns legend node for given index.
Qt::DropActions supportedDropActions() const override
QgsLayerTreeNode * parent()
Gets 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
Gets map of map layer style overrides (key: layer ID, value: style name) where a different style shou...
void editingStopped()
Is emitted, when edited changes successfully have been written to the data provider.
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer. Properties are stored in a map and saved in project file...
void setName(const QString &n) override
Sets the group's name.
The QgsMapLayerLegend class is abstract interface for implementations of legends for one map layer...
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
This class is a base class for nodes in a layer tree.
void setLegendFilterByScale(double scale)
Force only display of legend nodes which are valid for a given scale.
void insertChildNodes(int index, const QList< QgsLayerTreeNode *> &nodes)
Insert existing nodes at specified position.
bool hasValidSettings() const
Check whether the map settings are valid and can be used for rendering.
int autoCollapseLegendNodes() const
Returns at what number of legend nodes the layer node should be collapsed. -1 means no auto-collapse ...
Class that runs a hit test with given map settings.
int legendNodeRowCount(QgsLayerTreeModelLegendNode *node) const
void setLayerTreeNodeFont(int nodeType, const QFont &font)
Sets font for a particular type of layer tree node. nodeType should come from QgsLayerTreeNode::NodeT...
void setFlag(Flag f, bool on=true)
Enable or disable a model flag.
void editingStarted()
Is emitted, when editing on this layer has started.
void dataChanged()
Emitted on internal data change so the layer tree model can forward the signal to views...
QgsWkbTypes::GeometryType type() const
Returns type of the geometry as a QgsWkbTypes::GeometryType.
QModelIndex legendNodeIndex(int row, int column, QgsLayerTreeModelLegendNode *node) const
NodeType nodeType() const
Find out about type of the node. It is usually shorter to use convenience functions from QgsLayerTree...
QPersistentModelIndex mCurrentIndex
Current index - will be underlined.
double mLegendMapViewScale
double mLegendFilterByScale
scale denominator for filtering of legend nodes (<= 0 means no filtering)
void setName(const QString &n) override
Sets the layer's name.
QString title() const
Returns the title of the layer used by QGIS Server in GetCapabilities request.
Defer legend model invalidation.
void refreshLayerLegend(QgsLayerTreeLayer *nodeLayer)
Force a refresh of legend nodes of a layer node.
void setExpanded(bool expanded)
Sets 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...
QMap< QgsLayerTreeModelLegendNode *, QgsLayerTreeModelLegendNode * > parents
Pointer to parent for each active node. Top-level nodes have null parent. Pointers are not owned...
void setItemVisibilityCheckedParentRecursive(bool checked)
Check or uncheck a node and all its parents.
QgsMapLayer * layer() const
void setAutoCollapseLegendNodes(int nodeCount)
Sets at what number of legend nodes the layer node should be collapsed. Setting -1 disables the auto-...
Leaf node pointing to a layer.
QIcon legendIconEmbeddedInParent(QgsLayerTreeLayer *nodeLayer) const
Contains information about the context of a rendering operation.
QMap< QString, QString > LayerFilterExpression
Maps an expression string to a layer id.
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
Returns layer tree node for given index.
QList< QgsLayerTreeModelLegendNode * > filterLegendNodes(const QList< QgsLayerTreeModelLegendNode *> &nodes)
Filter nodes from QgsMapLayerLegend according to the current filtering rules.
QgsLayerTree * rootGroup() const
Returns pointer to the root node of the layer tree. Always a non-null pointer.
bool hasScaleBasedVisibility() const
Returns whether scale based visibility is enabled for the layer.
static void applyLayerNodeProperties(QgsLayerTreeLayer *nodeLayer, QList< QgsLayerTreeModelLegendNode *> &nodes)
update according to layer node's custom properties (order of items, user labels for items) ...
Layer nodes may optionally include extra embedded widgets (if used in QgsLayerTreeView). Added in 2.16.
void visibilityChanged(QgsLayerTreeNode *node)
Emitted when check state of a node within the tree has been changed.
void setRootGroup(QgsLayerTree *newRootGroup)
Reset the model and use a new root group node.
void nodeRemovedChildren()
static QgsProject * instance()
Returns the QgsProject singleton instance.
void nodeNameChanged(QgsLayerTreeNode *node, const QString &name)
Updates model when node's name has changed.
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
void legendChanged()
Signal emitted when legend of the layer has changed.
virtual QVariant data(int role) const =0
Returns data associated with the item. Must be implemented in derived class.
void legendMapViewData(double *mapUnitsPerPixel, int *dpi, double *scale) const
Gets hints about map view - to be used in legend nodes.
static QString displayString(Type type)
Returns a display string type for a WKB type, e.g., the geometry name used in WKT geometry representa...
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
void layerWillBeUnloaded()
Emitted when a previously available layer got unloaded (from layer registry).
Check/uncheck action has consequences on children (or parents for leaf node)
QList< QgsLayerTreeModelLegendNode * > layerLegendNodes(QgsLayerTreeLayer *nodeLayer, bool skipNodeEmbeddedInParent=false)
Returns filtered list of active legend nodes attached to a particular layer node (by default it retur...
virtual bool isSpatial() const
Returns true if the layer is considered a spatial layer, ie it has some form of geometry associated w...
void setItemVisibilityChecked(bool checked)
Check or uncheck a node (independently of its ancestors or children)
Allow user to set node visibility with a checkbox.
QList< QgsLayerTreeModelLegendNode * > activeNodes
Active legend nodes.
static QByteArray layerTreeNodesToUriList(const QList< QgsLayerTreeNode *> &nodes)
Returns encoded URI list from a list of layer tree nodes.
QString name() const override
Returns the group's name.
Flags flags() const
Returns OR-ed combination of model flags.
QVariant customProperty(const QString &value, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer.
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.
QgsLayerTreeLayer * findLayer(QgsMapLayer *layer) const
Find layer node representing the map layer.
void customPropertyChanged(QgsLayerTreeNode *node, const QString &key)
Emitted when a custom property of a node within the tree has been changed or removed.
QModelIndex legendRootIndex(int row, int column, QgsLayerTreeLayer *nL) const
Container of other groups and layers.
void setOverrideStyle(const QString &style)
Temporarily apply a different style to the layer.
void nameChanged(QgsLayerTreeNode *node, QString name)
Emitted when the name of the node is changed.
int columnCount(const QModelIndex &parent=QModelIndex()) const override
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.
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.
void layerModified()
This signal is emitted when modifications has been done on layer.
static QString legendFilterByExpression(const QgsLayerTreeLayer &layer, bool *enabled=nullptr)
Returns the expression filter of a legend layer.
Allow reordering with drag'n'drop.
Add legend nodes for layer nodes.
QgsLayerTree * mRootNode
Pointer to the root node of the layer tree. Not owned by the model.
virtual void invalidateMapBasedData()
Notification from model that information from associated map view has changed.
void nodeWillRemoveChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
void nodeLayerWillBeUnloaded()
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.
QModelIndex currentIndex() const
Gets index of the item marked as current. Item marked as current is underlined.
void nodeVisibilityChanged(QgsLayerTreeNode *node)
Structure that stores all data associated with one map layer.