24 #include <QTextStream> 38 : QAbstractItemModel( parent )
39 , mRootNode( rootNode )
40 , mFlags( ShowLegend | AllowLegendChangeState | DeferredLegendInvalidation )
41 , mAutoCollapseLegendNodesCount( -1 )
42 , mLegendFilterByScale( 0 )
43 , mLegendFilterUsesExtent( false )
44 , mLegendMapViewMupp( 0 )
45 , mLegendMapViewDpi( 0 )
46 , mLegendMapViewScale( 0 )
63 if ( !index.isValid() )
66 QObject *obj =
reinterpret_cast<QObject *
>( index.internalPointer() );
99 if ( column < 0 || column >=
columnCount( parent ) ||
100 row < 0 || row >=
rowCount( parent ) )
101 return QModelIndex();
108 return QModelIndex();
115 return createIndex( row, column, static_cast<QObject *>( n->
children().at( row ) ) );
121 if ( !child.isValid() )
122 return QModelIndex();
135 return QModelIndex();
143 Q_ASSERT( parentNode );
146 if ( !grandParentNode )
147 return QModelIndex();
149 int row = grandParentNode->
children().indexOf( parentNode );
150 Q_ASSERT( row >= 0 );
152 return createIndex( row, 0, static_cast<QObject *>( parentNode ) );
158 if ( !index.isValid() || index.column() > 1 )
165 if ( role == Qt::DisplayRole || role == Qt::EditRole )
173 QString name = nodeLayer->
name();
174 if ( nodeLayer->
customProperty( QStringLiteral(
"showFeatureCount" ), 0 ).toInt() && role == Qt::DisplayRole )
178 name += QStringLiteral(
" [%1]" ).arg( vlayer->
featureCount() );
183 else if ( role == Qt::DecorationRole && index.column() == 0 )
231 QPixmap pixmap( icon.pixmap( iconSize, iconSize ) );
233 QPainter painter( &pixmap );
234 painter.drawPixmap( 0, 0, iconSize, iconSize,
QgsApplication::getThemePixmap( vlayer->
isModified() ? QStringLiteral(
"/mIconEditableEdits.svg" ) : QStringLiteral(
"/mActionToggleEditing.svg" ) ) );
237 icon = QIcon( pixmap );
243 else if ( role == Qt::CheckStateRole )
263 else if ( role == Qt::FontRole )
267 f.setUnderline(
true );
273 f.setItalic( !f.italic() );
278 else if ( role == Qt::ForegroundRole )
280 QBrush brush( qApp->palette().color( QPalette::Text ), Qt::SolidPattern );
286 QColor fadedTextColor = brush.color();
287 fadedTextColor.setAlpha( 66 );
288 brush.setColor( fadedTextColor );
293 else if ( role == Qt::ToolTipRole )
300 !layer->
title().isEmpty() ? layer->title() :
301 !layer->shortName().isEmpty() ? layer->shortName() :
304 title =
"<b>" + title.toHtmlEscaped() +
"</b>";
306 if ( layer->isSpatial() && layer->crs().isValid() )
308 if (
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer ) )
311 title += tr(
" (%1)" ).arg( layer->crs().authid() ).toHtmlEscaped();
317 if ( !layer->abstract().isEmpty() )
320 const QStringList abstractLines = layer->abstract().split(
'\n' );
321 for (
const auto &l : abstractLines )
323 parts << l.toHtmlEscaped();
328 QString source( layer->publicSource() );
329 if ( source.size() > 1024 )
331 source = source.left( 1023 ) + QStringLiteral(
"…" );
334 parts <<
"<i>" + source.toHtmlEscaped() +
"</i>";
336 return parts.join( QStringLiteral(
"<br/>" ) );
347 if ( !index.isValid() )
349 Qt::ItemFlags rootFlags = Qt::ItemFlags();
351 rootFlags |= Qt::ItemIsDropEnabled;
358 Qt::ItemFlags f = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
361 f |= Qt::ItemIsEditable;
364 bool isEmbedded = node->
customProperty( QStringLiteral(
"embedded" ) ).toInt();
369 if ( !isEmbedded || ( isEmbedded && node->
parent() && !node->
parent()->
customProperty( QStringLiteral(
"embedded" ) ).toInt() ) )
370 f |= Qt::ItemIsDragEnabled;
374 f |= Qt::ItemIsUserCheckable;
377 f |= Qt::ItemIsDropEnabled;
389 bool res = sym->
setData( value, role );
391 emit dataChanged( index, index );
397 return QAbstractItemModel::setData( index, value, role );
399 if ( role == Qt::CheckStateRole )
404 bool checked =
static_cast< Qt::CheckState
>( value.toInt() ) == Qt::Checked;
405 if ( checked && node->
children().isEmpty() )
425 else if ( role == Qt::EditRole )
433 layer->
setName( value.toString() );
434 emit dataChanged( index, index );
439 emit dataChanged( index, index );
443 return QAbstractItemModel::setData( index, value, role );
448 if ( !node || !node->
parent() )
449 return QModelIndex();
454 Q_ASSERT( row >= 0 );
455 return index( row, 0, parentIndex );
464 if ( child->
parent() == node )
467 return _isChildOfNode( child->
parent(), node );
470 static bool _isChildOfNodes(
QgsLayerTreeNode *child,
const QList<QgsLayerTreeNode *> &nodes )
474 if ( _isChildOfNode( child, n ) )
484 QList<QgsLayerTreeNode *> nodes;
485 Q_FOREACH (
const QModelIndex &
index, list )
498 QList<QgsLayerTreeNode *> nodesFinal;
501 if ( !_isChildOfNodes( node, nodes ) )
532 emit dataChanged( idx, idx );
536 beginRemoveRows( idx, 0, std::max( oldNodeCount - 1, 0 ) );
558 if ( oldIndex.isValid() )
559 emit dataChanged( oldIndex, oldIndex );
560 if ( currentIndex.isValid() )
561 emit dataChanged( currentIndex, currentIndex );
627 if ( useExpressions )
633 if ( enabled && !expr.isEmpty() )
635 exprs[ nodeLayer->
layerId()] = expr;
640 if ( useExpressions && !useExtent && !polygonValid )
705 QFontMetrics fm( ( QFont() ) );
706 const double scale = 1.1 * standardSize / 24;
707 return static_cast< int >( std::floor( std::max(
Qgis::UI_SCALE_FACTOR * fm.height() * scale,
static_cast< double >( standardSize ) ) ) );
713 beginInsertRows(
node2index( node ), indexFrom, indexTo );
716 static QList<QgsLayerTreeLayer *> _layerNodesInSubtree(
QgsLayerTreeNode *node,
int indexFrom,
int indexTo )
718 QList<QgsLayerTreeNode *> children = node->
children();
719 QList<QgsLayerTreeLayer *> newLayerNodes;
720 for (
int i = indexFrom; i <= indexTo; ++i )
728 return newLayerNodes;
737 Q_FOREACH (
QgsLayerTreeLayer *newLayerNode, _layerNodesInSubtree( node, indexFrom, indexTo ) )
745 beginRemoveRows(
node2index( node ), indexFrom, indexTo );
748 Q_FOREACH (
QgsLayerTreeLayer *nodeLayer, _layerNodesInSubtree( node, indexFrom, indexTo ) )
762 emit dataChanged( index, index );
771 emit dataChanged( index, index );
834 emit dataChanged( index, index );
836 if ( nodeLayer->
customProperty( QStringLiteral(
"showFeatureCount" ) ).toInt() )
848 if ( index.isValid() )
849 emit dataChanged( index, index );
855 if ( !nodeLayer->
layer() )
914 disconnect( nodeLayer,
nullptr,
this,
nullptr );
916 if ( !nodeLayer->
layer() )
927 disconnect( nodeLayer->
layer(),
nullptr,
this, nullptr );
971 disconnect(
mRootNode,
nullptr,
this,
nullptr );
982 int count = node->
children().count();
985 emit dataChanged(
index( 0, 0, idx ),
index( count - 1, 0, idx ) );
986 for (
int i = 0; i < count; ++i )
1001 emit dataChanged( idx, idx );
1004 int count = node->
children().count();
1005 for (
int i = 0; i < count; ++i )
1011 return Qt::CopyAction | Qt::MoveAction;
1017 types << QStringLiteral(
"application/qgis.layertreemodeldata" );
1025 QModelIndexList sortedIndexes = indexes;
1026 std::sort( sortedIndexes.begin(), sortedIndexes.end(), std::less<QModelIndex>() );
1028 QList<QgsLayerTreeNode *> nodesFinal =
indexes2nodes( sortedIndexes,
true );
1030 if ( nodesFinal.isEmpty() )
1033 QMimeData *
mimeData =
new QMimeData();
1036 QDomElement rootElem = doc.createElement( QStringLiteral(
"layer_tree_model_data" ) );
1039 doc.appendChild( rootElem );
1040 QString txt = doc.toString();
1042 mimeData->setData( QStringLiteral(
"application/qgis.layertreemodeldata" ), txt.toUtf8() );
1051 if ( action == Qt::IgnoreAction )
1054 if ( !data->hasFormat( QStringLiteral(
"application/qgis.layertreemodeldata" ) ) )
1064 QByteArray encodedData = data->data( QStringLiteral(
"application/qgis.layertreemodeldata" ) );
1067 if ( !doc.setContent( QString::fromUtf8( encodedData ) ) )
1070 QDomElement rootElem = doc.documentElement();
1071 if ( rootElem.tagName() != QLatin1String(
"layer_tree_model_data" ) )
1074 QList<QgsLayerTreeNode *> nodes;
1076 QDomElement elem = rootElem.firstChildElement();
1077 while ( !elem.isNull() )
1083 elem = elem.nextSiblingElement();
1086 if ( nodes.isEmpty() )
1089 if ( parent.isValid() && row == -1 )
1128 return mFlags.testFlag( f );
1138 QList<QgsLayerTreeModelLegendNode *> filtered;
1199 if (
mLegend.contains( nodeLayer ) )
1201 qDeleteAll(
mLegend[nodeLayer].originalNodes );
1202 delete mLegend[nodeLayer].tree;
1210 if ( !nodeL || !nodeL->
layer() )
1230 int widgetsCount = ml->
customProperty( QStringLiteral(
"embeddedWidgets/count" ), 0 ).toInt();
1231 while ( widgetsCount > 0 )
1233 lstNew.insert( 0,
new EmbeddedWidgetLegendNode( nodeL ) );
1238 QList<QgsLayerTreeModelLegendNode *> filteredLstNew =
filterLegendNodes( lstNew );
1242 n->setParent(
this );
1254 embeddedNode = legendNode;
1255 filteredLstNew.removeOne( legendNode );
1266 int count = legendTree ? legendTree->
children[
nullptr].count() : filteredLstNew.count();
1268 if ( !filteredLstNew.isEmpty() ) beginInsertRows(
node2index( nodeL ), 0, count - 1 );
1274 data.
tree = legendTree;
1278 if ( !filteredLstNew.isEmpty() ) endInsertRows();
1289 bool hasParentKeys =
false;
1294 hasParentKeys =
true;
1298 if ( !hasParentKeys )
1302 QHash<QString, QgsLayerTreeModelLegendNode *> rule2node;
1303 rule2node[QString()] =
nullptr;
1307 if ( ruleKey.isEmpty() )
1309 if ( rule2node.contains( ruleKey ) )
1311 rule2node[ruleKey] = n;
1336 context->setScaleFactor( dpi / 25.4 );
1337 context->setRendererScale( scale );
1339 return validData ? context.release() :
nullptr;
1357 int row = data.
tree->
children[parentLegendNode].indexOf( legendNode );
1358 return index( row, 0, parentIndex );
1363 int row = data.
tree->
children[
nullptr].indexOf( legendNode );
1364 return index( row, 0, parentIndex );
1369 Q_ASSERT( parentIndex.isValid() );
1372 return QModelIndex();
1374 return index( row, 0, parentIndex );
1390 if ( !
mLegend.contains( nL ) )
1404 Q_ASSERT(
mLegend.contains( nL ) );
1407 return createIndex( row, column, static_cast<QObject *>( data.tree->children[
nullptr].at( row ) ) );
1409 return createIndex( row, column, static_cast<QObject *>( data.activeNodes.at( row ) ) );
1417 return createIndex( row, column, static_cast<QObject *>( data.
tree->
children[node].at( row ) ) );
1419 return QModelIndex();
1432 int row = data.
tree->
children[grandParentNode].indexOf( parentNode );
1433 return createIndex( row, 0, static_cast<QObject *>( parentNode ) );
1447 return node->
data( role );
1453 Qt::ItemFlags f = node->
flags();
1455 f &= ~Qt::ItemIsUserCheckable;
1462 return static_cast< bool >(
mLegend[nodeLayer].embeddedNodeInParent );
1467 return mLegend[nodeLayer].embeddedNodeInParent;
1476 return QIcon( qvariant_cast<QPixmap>( legendNode->
data( Qt::DecorationRole ) ) );
1482 if ( !
mLegend.contains( nodeLayer ) )
1483 return QList<QgsLayerTreeModelLegendNode *>();
1486 QList<QgsLayerTreeModelLegendNode *> lst( data.
activeNodes );
1494 return mLegend.value( nodeLayer ).originalNodes;
1499 QMap<QgsLayerTreeLayer *, LayerLegendData>::const_iterator it =
mLegend.constBegin();
1500 for ( ; it !=
mLegend.constEnd(); ++it )
1503 if ( layer->
layerId() == layerId )
1537 std::unique_ptr<QgsRenderContext> context( createTemporaryRenderContext() );
1541 QList<QgsSymbolLegendNode *> symbolNodes;
1542 QMap<QString, int> widthMax;
1550 widthMax[parentKey] = std::max( sz.width(), widthMax.contains( parentKey ) ? widthMax[parentKey] : 0 );
1552 symbolNodes.append( n );
1558 Q_ASSERT( widthMax[parentKey] > 0 );
1559 const int twiceMarginWidth = 2;
1560 n->
setIconSize( QSize( widthMax[parentKey] + twiceMarginWidth, n->
iconSize().rheight() + twiceMarginWidth ) );
int legendNodeRowCount(QgsLayerTreeModelLegendNode *node) const
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.
QModelIndex legendParent(QgsLayerTreeModelLegendNode *legendNode) const
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
Base class for all map layer types.
QModelIndex currentIndex() const
Gets index of the item marked as current. Item marked as current is underlined.
QMimeData * mimeData(const QModelIndexList &indexes) const override
QIcon legendIconEmbeddedInParent(QgsLayerTreeLayer *nodeLayer) const
QgsMapLayer::LayerType type() const
Returns the type of the layer.
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.
QgsLayerTree * rootGroup() const
Returns pointer to the root node of the layer tree. Always a non-null pointer.
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() )...
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.
static const double UI_SCALE_FACTOR
UI scaling factor.
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)
Restore overridden layer style on destruction.
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...
virtual void writeXml(QDomElement &parentElement, const QgsReadWriteContext &context)=0
Write layer tree to XML.
QgsMapLayer * layer() const
Returns the map layer associated with this node.
Qt::ItemFlags legendNodeFlags(QgsLayerTreeModelLegendNode *node) const
virtual bool setData(const QVariant &value, int role)
Sets some data associated with the item. Default implementation does nothing and returns false...
void disconnectFromRootNode()
static QgsLayerTreeGroup * toGroup(QgsLayerTreeNode *node)
Cast node to a group.
bool itemVisibilityChecked() const
Returns whether a node is checked (independently of its ancestors or children)
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
void setIconSize(QSize sz)
Set the icon size.
bool legendEmbeddedInParent(QgsLayerTreeLayer *nodeLayer) const
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.
QString title() const
Returns the title of the layer used by QGIS Server in GetCapabilities request.
Implementation of legend node interface for displaying preview of vector symbols and their labels and...
Flags flags() const
Returns OR-ed combination of model flags.
QList< QgsLayerTreeModelLegendNode * > filterLegendNodes(const QList< QgsLayerTreeModelLegendNode * > &nodes)
Filter nodes from QgsMapLayerLegend according to the current filtering rules.
QgsLayerTreeModelLegendNode * embeddedNodeInParent
A legend node that is not displayed separately, its icon is instead shown within the layer node's ite...
A geometry is the spatial representation of a feature.
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
void recursivelyEmitDataChanged(const QModelIndex &index=QModelIndex())
emit dataChanged() for layer tree node items
void legendNodeDataChanged()
virtual Qt::ItemFlags flags() const
Returns item flags associated with the item. Default implementation returns Qt::ItemIsEnabled.
QMap< QgsLayerTreeModelLegendNode *, QList< QgsLayerTreeModelLegendNode * > > children
List of children for each active node. Top-level nodes are under null pointer key. Pointers are not owned.
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.
QVariant legendNodeData(QgsLayerTreeModelLegendNode *node, int role) const
bool mLegendFilterUsesExtent
whether to use map filtering
Flags mFlags
Sets of flags for the model.
static QIcon iconDefault()
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)
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.
virtual bool isEmbeddedInParent() const
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.
bool isVisible() const
Returns whether a node is really visible (ie checked and all its ancestors checked as well) ...
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Perform transforms between map coordinates and device coordinates.
bool isEditable() const FINAL
Returns true if the provider is in editing mode.
Allow renaming of groups and layers.
QMap< QString, QString > layerStyleOverrides() const
Gets map of map layer style overrides (key: layer ID, value: style name) where a different style shou...
Rule key of the parent legend node - for legends with tree hierarchy (QString). Added in 2...
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.
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
bool hasScaleBasedVisibility() const
Returns whether scale based visibility is enabled for the layer.
bool isInScaleRange(double scale) const
Tests whether the layer should be visible at the specified scale.
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)
static int scaleIconSize(int standardSize)
Scales an layer tree model icon size to compensate for display pixel density, making the icon size hi...
#define QgsDebugMsgLevel(str, level)
Rule key of the node (QString)
bool hasValidSettings() const
Check whether the map settings are valid and can be used for rendering.
QSize minimumIconSize() const
Calculates the minimum icon size to prevent cropping.
void setLegendFilterByMap(const QgsMapSettings *settings)
Force only display of legend nodes which are valid for given map settings.
int legendRootRowCount(QgsLayerTreeLayer *nL) const
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
QList< QgsLayerTreeLayer * > findLayers() const
Find all layer nodes.
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
void editingStopped()
Is emitted, when edited changes successfully have been written to the data provider.
int autoCollapseLegendNodes() const
Returns at what number of legend nodes the layer node should be collapsed. -1 means no auto-collapse ...
void setName(const QString &n) override
Sets the group's name.
QgsLayerTreeLayer * layerNode() const
Returns pointer to the parent layer node.
The QgsMapLayerLegend class is abstract interface for implementations of legends for one map layer...
static QByteArray layerTreeNodesToUriList(const QList< QgsLayerTreeNode * > &nodes)
Returns encoded URI list from a list of layer tree nodes.
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.
long featureCount(const QString &legendKey) const
Number of features rendered with specified legend key.
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
QString layerId() const
Returns the ID for the map layer associated with this node.
void setLegendFilterByScale(double scale)
Force only display of legend nodes which are valid for a given scale.
virtual bool isModified() const
Returns true if the provider has been modified since the last commit.
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...
QVariant customProperty(const QString &value, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer.
Class that runs a hit test with given map settings.
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...
void insertChildNodes(int index, const QList< QgsLayerTreeNode * > &nodes)
Insert existing nodes at specified position.
static void applyLayerNodeProperties(QgsLayerTreeLayer *nodeLayer, QList< QgsLayerTreeModelLegendNode * > &nodes)
update according to layer node's custom properties (order of items, user labels for items) ...
QPersistentModelIndex mCurrentIndex
Current index - will be underlined.
double mLegendMapViewScale
bool testFlag(Flag f) const
Check whether a flag is enabled.
double mLegendFilterByScale
scale denominator for filtering of legend nodes (<= 0 means no filtering)
void setName(const QString &n) override
Sets the layer's name.
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.
QgsMapLayerLegend * legend() const
Can be null.
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...
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.
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
void legendMapViewData(double *mapUnitsPerPixel, int *dpi, double *scale) const
Gets hints about map view - to be used in legend nodes.
int mAutoCollapseLegendNodesCount
Minimal number of nodes when legend should be automatically collapsed. -1 = disabled.
QModelIndex indexOfParentLayerTreeNode(QgsLayerTreeNode *parentNode) const
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.
virtual bool isScaleOK(double scale) const
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
QStringList mimeTypes() const override
void invalidateLegendMapBasedData()
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.
QgsLayerTreeModelLegendNode * legendNodeEmbeddedInParent(QgsLayerTreeLayer *nodeLayer) const
Returns legend node that may be embedded in parent (i.e.
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...
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.
QFont layerTreeNodeFont(int nodeType) const
Gets font for a particular type of layer tree node. nodeType should come from QgsLayerTreeNode::NodeT...
QgsWkbTypes::GeometryType geometryType() const
Returns point, line or polygon.
QString name() const override
Returns the group's name.
NodeType nodeType() const
Find out about type of the node. It is usually shorter to use convenience functions from QgsLayerTree...
void setLayerStyleOverrides(const QMap< QString, QString > &overrides)
Sets map of map layer style overrides (key: layer ID, value: style name) where a different style shou...
void customPropertyChanged(QgsLayerTreeNode *node, const QString &key)
Emitted when a custom property of a node within the tree has been changed or removed.
QgsLayerTreeNode * index2node(const QModelIndex &index) const
Returns layer tree node for given index.
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.
QgsLayerTreeLayer * findLayer(QgsMapLayer *layer) const
Find layer node representing the map layer.
static QIcon iconMesh()
Returns icon for mesh layer type.
QgsWkbTypes::GeometryType type() const
Returns type of the geometry as a QgsWkbTypes::GeometryType.
virtual bool isSpatial() const
Returns true if the layer is considered a spatial layer, ie it has some form of geometry associated w...
void layerModified()
This signal is emitted when modifications has been done on layer.
QModelIndex legendRootIndex(int row, int column, QgsLayerTreeLayer *nL) const
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)
LayerLegendTree * tryBuildLegendTree(const QList< QgsLayerTreeModelLegendNode * > &nodes)
void nodeLayerWillBeUnloaded()
QModelIndex legendNodeIndex(int row, int column, QgsLayerTreeModelLegendNode *node) const
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Layer tree node points to a map layer.
void nodeVisibilityChanged(QgsLayerTreeNode *node)
Structure that stores all data associated with one map layer.