25 #include <QTextStream> 39 : QAbstractItemModel( parent )
40 , mRootNode( rootNode )
41 , mFlags( ShowLegend | AllowLegendChangeState | DeferredLegendInvalidation )
42 , mAutoCollapseLegendNodesCount( -1 )
43 , mLegendFilterByScale( 0 )
44 , mLegendFilterUsesExtent( false )
45 , mLegendMapViewMupp( 0 )
46 , mLegendMapViewDpi( 0 )
47 , mLegendMapViewScale( 0 )
64 if ( !index.isValid() )
67 QObject *obj =
reinterpret_cast<QObject *
>( index.internalPointer() );
100 if ( column < 0 || column >=
columnCount( parent ) ||
101 row < 0 || row >=
rowCount( parent ) )
102 return QModelIndex();
109 return QModelIndex();
116 return createIndex( row, column, static_cast<QObject *>( n->
children().at( row ) ) );
122 if ( !child.isValid() )
123 return QModelIndex();
136 return QModelIndex();
144 Q_ASSERT( parentNode );
147 if ( !grandParentNode )
148 return QModelIndex();
150 int row = grandParentNode->
children().indexOf( parentNode );
151 Q_ASSERT( row >= 0 );
153 return createIndex( row, 0, static_cast<QObject *>( parentNode ) );
159 if ( !index.isValid() || index.column() > 1 )
166 if ( role == Qt::DisplayRole || role == Qt::EditRole )
174 QString name = nodeLayer->
name();
175 if ( nodeLayer->
customProperty( QStringLiteral(
"showFeatureCount" ), 0 ).toInt() && role == Qt::DisplayRole )
179 name += QStringLiteral(
" [%1]" ).arg( vlayer->
featureCount() );
184 else if ( role == Qt::DecorationRole && index.column() == 0 )
198 switch ( layer->
type() )
236 QPixmap pixmap( icon.pixmap( iconSize, iconSize ) );
238 QPainter painter( &pixmap );
239 painter.drawPixmap( 0, 0, iconSize, iconSize,
QgsApplication::getThemePixmap( vlayer->
isModified() ? QStringLiteral(
"/mIconEditableEdits.svg" ) : QStringLiteral(
"/mActionToggleEditing.svg" ) ) );
242 icon = QIcon( pixmap );
248 else if ( role == Qt::CheckStateRole )
268 else if ( role == Qt::FontRole )
272 f.setUnderline(
true );
278 f.setItalic( !f.italic() );
283 else if ( role == Qt::ForegroundRole )
285 QBrush brush( qApp->palette().color( QPalette::Text ), Qt::SolidPattern );
291 QColor fadedTextColor = brush.color();
292 fadedTextColor.setAlpha( 128 );
293 brush.setColor( fadedTextColor );
298 else if ( role == Qt::ToolTipRole )
305 !layer->
title().isEmpty() ? layer->title() :
306 !layer->shortName().isEmpty() ? layer->shortName() :
309 title =
"<b>" + title.toHtmlEscaped() +
"</b>";
311 if ( layer->isSpatial() && layer->crs().isValid() )
313 if (
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer ) )
316 title += tr(
" (%1)" ).arg( layer->crs().authid() ).toHtmlEscaped();
322 if ( !layer->abstract().isEmpty() )
325 const QStringList abstractLines = layer->abstract().split(
'\n' );
326 for (
const auto &l : abstractLines )
328 parts << l.toHtmlEscaped();
333 QString source( layer->publicSource() );
334 if ( source.size() > 1024 )
336 source = source.left( 1023 ) + QStringLiteral(
"…" );
339 parts <<
"<i>" + source.toHtmlEscaped() +
"</i>";
341 return parts.join( QStringLiteral(
"<br/>" ) );
352 if ( !index.isValid() )
354 Qt::ItemFlags rootFlags = Qt::ItemFlags();
356 rootFlags |= Qt::ItemIsDropEnabled;
363 Qt::ItemFlags f = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
366 f |= Qt::ItemIsEditable;
369 bool isEmbedded = node->
customProperty( QStringLiteral(
"embedded" ) ).toInt();
374 if ( !isEmbedded || ( isEmbedded && node->
parent() && !node->
parent()->
customProperty( QStringLiteral(
"embedded" ) ).toInt() ) )
375 f |= Qt::ItemIsDragEnabled;
379 f |= Qt::ItemIsUserCheckable;
382 f |= Qt::ItemIsDropEnabled;
394 bool res = sym->
setData( value, role );
396 emit dataChanged( index, index );
402 return QAbstractItemModel::setData( index, value, role );
404 if ( role == Qt::CheckStateRole )
409 bool checked =
static_cast< Qt::CheckState
>( value.toInt() ) == Qt::Checked;
410 if ( checked && node->
children().isEmpty() )
430 else if ( role == Qt::EditRole )
438 layer->
setName( value.toString() );
439 emit dataChanged( index, index );
444 emit dataChanged( index, index );
448 return QAbstractItemModel::setData( index, value, role );
453 if ( !node || !node->
parent() )
454 return QModelIndex();
459 Q_ASSERT( row >= 0 );
460 return index( row, 0, parentIndex );
469 if ( child->
parent() == node )
472 return _isChildOfNode( child->
parent(), node );
475 static bool _isChildOfNodes(
QgsLayerTreeNode *child,
const QList<QgsLayerTreeNode *> &nodes )
479 if ( _isChildOfNode( child, n ) )
489 QList<QgsLayerTreeNode *> nodes;
490 const auto constList = list;
491 for (
const QModelIndex &
index : constList )
504 QList<QgsLayerTreeNode *> nodesFinal;
507 if ( !_isChildOfNodes( node, nodes ) )
538 emit dataChanged( idx, idx );
542 if ( oldNodeCount > 0 )
544 beginRemoveRows( idx, 0, oldNodeCount - 1 );
567 if ( oldIndex.isValid() )
568 emit dataChanged( oldIndex, oldIndex );
569 if ( currentIndex.isValid() )
570 emit dataChanged( currentIndex, currentIndex );
637 if ( useExpressions )
644 if ( enabled && !expr.isEmpty() )
646 exprs[ nodeLayer->layerId()] = expr;
651 if ( useExpressions && !useExtent && !polygonValid )
717 QFontMetrics fm( ( QFont() ) );
718 const double scale = 1.1 * standardSize / 24;
719 return static_cast< int >( std::floor( std::max(
Qgis::UI_SCALE_FACTOR * fm.height() * scale,
static_cast< double >( standardSize ) ) ) );
725 beginInsertRows(
node2index( node ), indexFrom, indexTo );
728 static QList<QgsLayerTreeLayer *> _layerNodesInSubtree(
QgsLayerTreeNode *node,
int indexFrom,
int indexTo )
730 QList<QgsLayerTreeNode *> children = node->
children();
731 QList<QgsLayerTreeLayer *> newLayerNodes;
732 for (
int i = indexFrom; i <= indexTo; ++i )
740 return newLayerNodes;
749 const auto subNodes = _layerNodesInSubtree( node, indexFrom, indexTo );
758 beginRemoveRows(
node2index( node ), indexFrom, indexTo );
761 const auto subNodes = _layerNodesInSubtree( node, indexFrom, indexTo );
776 emit dataChanged( index, index );
785 emit dataChanged( index, index );
848 emit dataChanged( index, index );
850 if ( nodeLayer->
customProperty( QStringLiteral(
"showFeatureCount" ) ).toInt() )
862 if ( index.isValid() )
863 emit dataChanged( index, index );
869 if ( !nodeLayer->
layer() )
911 const auto constChildren = group->
children();
929 disconnect( nodeLayer,
nullptr,
this,
nullptr );
931 if ( !nodeLayer->
layer() )
942 disconnect( nodeLayer->
layer(),
nullptr,
this, nullptr );
948 const auto constChildren = parentGroup->
children();
960 const auto constChildren = parentGroup->
children();
988 disconnect(
mRootNode,
nullptr,
this,
nullptr );
999 int count = node->
children().count();
1002 emit dataChanged(
index( 0, 0, idx ),
index( count - 1, 0, idx ) );
1003 for (
int i = 0; i < count; ++i )
1018 emit dataChanged( idx, idx );
1021 int count = node->
children().count();
1022 for (
int i = 0; i < count; ++i )
1028 return Qt::CopyAction | Qt::MoveAction;
1034 types << QStringLiteral(
"application/qgis.layertreemodeldata" );
1042 QModelIndexList sortedIndexes = indexes;
1043 std::sort( sortedIndexes.begin(), sortedIndexes.end(), std::less<QModelIndex>() );
1045 QList<QgsLayerTreeNode *> nodesFinal =
indexes2nodes( sortedIndexes,
true );
1047 if ( nodesFinal.isEmpty() )
1050 QMimeData *
mimeData =
new QMimeData();
1053 QDomElement rootElem = doc.createElement( QStringLiteral(
"layer_tree_model_data" ) );
1056 doc.appendChild( rootElem );
1057 QString txt = doc.toString();
1059 mimeData->setData( QStringLiteral(
"application/qgis.layertreemodeldata" ), txt.toUtf8() );
1068 if ( action == Qt::IgnoreAction )
1071 if ( !data->hasFormat( QStringLiteral(
"application/qgis.layertreemodeldata" ) ) )
1081 QByteArray encodedData = data->data( QStringLiteral(
"application/qgis.layertreemodeldata" ) );
1084 if ( !doc.setContent( QString::fromUtf8( encodedData ) ) )
1087 QDomElement rootElem = doc.documentElement();
1088 if ( rootElem.tagName() != QLatin1String(
"layer_tree_model_data" ) )
1091 QList<QgsLayerTreeNode *> nodes;
1093 QDomElement elem = rootElem.firstChildElement();
1094 while ( !elem.isNull() )
1100 elem = elem.nextSiblingElement();
1103 if ( nodes.isEmpty() )
1106 if ( parent.isValid() && row == -1 )
1145 return mFlags.testFlag( f );
1155 QList<QgsLayerTreeModelLegendNode *> filtered;
1175 if (
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( node->layerNode()->layer() ) )
1205 const auto constMLegend =
mLegend;
1208 qDeleteAll(
data.originalNodes );
1217 if (
mLegend.contains( nodeLayer ) )
1219 qDeleteAll(
mLegend[nodeLayer].originalNodes );
1220 delete mLegend[nodeLayer].tree;
1228 if ( !nodeL || !nodeL->
layer() )
1248 int widgetsCount = ml->
customProperty( QStringLiteral(
"embeddedWidgets/count" ), 0 ).toInt();
1249 while ( widgetsCount > 0 )
1251 lstNew.insert( 0,
new EmbeddedWidgetLegendNode( nodeL ) );
1256 QList<QgsLayerTreeModelLegendNode *> filteredLstNew =
filterLegendNodes( lstNew );
1258 const auto constLstNew = lstNew;
1261 n->setParent(
this );
1269 const auto constFilteredLstNew = filteredLstNew;
1286 int count = legendTree ? legendTree->
children[
nullptr].count() : filteredLstNew.count();
1288 if ( !filteredLstNew.isEmpty() ) beginInsertRows(
node2index( nodeL ), 0, count - 1 );
1294 data.
tree = legendTree;
1298 if ( !filteredLstNew.isEmpty() ) endInsertRows();
1309 bool hasParentKeys =
false;
1314 hasParentKeys =
true;
1318 if ( !hasParentKeys )
1322 QHash<QString, QgsLayerTreeModelLegendNode *> rule2node;
1323 rule2node[QString()] =
nullptr;
1327 if ( ruleKey.isEmpty() )
1329 if ( rule2node.contains( ruleKey ) )
1331 rule2node[ruleKey] = n;
1356 context->setScaleFactor( dpi / 25.4 );
1357 context->setRendererScale( scale );
1359 return validData ? context.release() :
nullptr;
1377 int row = data.
tree->
children[parentLegendNode].indexOf( legendNode );
1378 return index( row, 0, parentIndex );
1383 int row = data.
tree->
children[
nullptr].indexOf( legendNode );
1384 return index( row, 0, parentIndex );
1389 Q_ASSERT( parentIndex.isValid() );
1392 return QModelIndex();
1394 return index( row, 0, parentIndex );
1410 if ( !
mLegend.contains( nL ) )
1424 Q_ASSERT(
mLegend.contains( nL ) );
1427 return createIndex( row, column, static_cast<QObject *>( data.tree->children[
nullptr].at( row ) ) );
1429 return createIndex( row, column, static_cast<QObject *>( data.activeNodes.at( row ) ) );
1437 return createIndex( row, column, static_cast<QObject *>( data.
tree->
children[node].at( row ) ) );
1439 return QModelIndex();
1452 int row = data.
tree->
children[grandParentNode].indexOf( parentNode );
1453 return createIndex( row, 0, static_cast<QObject *>( parentNode ) );
1467 return node->
data( role );
1473 Qt::ItemFlags f = node->
flags();
1475 f &= ~Qt::ItemIsUserCheckable;
1482 return static_cast< bool >(
mLegend[nodeLayer].embeddedNodeInParent );
1487 return mLegend[nodeLayer].embeddedNodeInParent;
1496 return QIcon( qvariant_cast<QPixmap>( legendNode->
data( Qt::DecorationRole ) ) );
1502 if ( !
mLegend.contains( nodeLayer ) )
1503 return QList<QgsLayerTreeModelLegendNode *>();
1506 QList<QgsLayerTreeModelLegendNode *> lst( data.
activeNodes );
1514 return mLegend.value( nodeLayer ).originalNodes;
1519 QMap<QgsLayerTreeLayer *, LayerLegendData>::const_iterator it =
mLegend.constBegin();
1520 for ( ; it !=
mLegend.constEnd(); ++it )
1523 if ( layer->
layerId() == layerId )
1525 const auto activeNodes =
mLegend.value( layer ).activeNodes;
1558 std::unique_ptr<QgsRenderContext> context( createTemporaryRenderContext() );
1560 const auto constMLegend =
mLegend;
1563 QList<QgsSymbolLegendNode *> symbolNodes;
1564 QMap<QString, int> widthMax;
1572 widthMax[parentKey] = std::max( sz.width(), widthMax.contains( parentKey ) ? widthMax[parentKey] : 0 );
1574 symbolNodes.append( n );
1577 const auto constSymbolNodes = symbolNodes;
1581 Q_ASSERT( widthMax[parentKey] > 0 );
1582 const int twiceMarginWidth = 2;
1583 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 nullptr). ...
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.
QgsMapLayerType type() const
Returns the type of the layer.
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.
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)
QgsMapLayerLegend * legend() const
Can be nullptr.
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 nullptr 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
QString layerId() const
Returns the ID for the map layer associated with this node.
Flags mFlags
Sets of flags for the model.
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...
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.
bool isEditable() const FINAL
Returns true if the provider is in editing mode.
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.
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)
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 nullptr, 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()
Emitted when edited changes have been successfully 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...
QgsLayerTreeModelLegendNode * legendNode(const QString &rule, QgsLayerTreeModel &model)
void setFlag(Flag f, bool on=true)
Enable or disable a model flag.
void editingStarted()
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...
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.
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 nullptr parent. Pointers are not owned...
void setItemVisibilityCheckedParentRecursive(bool checked)
Check or uncheck a node and all its parents.
QgsMapLayer * layer() const
Returns the map layer associated with this node.
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 nullptr value.
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.
QgsWkbTypes::GeometryType type
QVariant customProperty(const QString &value, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer.
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...
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.
static QIcon iconMesh()
Returns icon for mesh layer type.
void layerModified()
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.