24  : QSortFilterProxyModel( parent )
 
 
   37  mCheckedLayers = layers;
 
 
   49  if ( idx.column() == 0 )
 
   51    return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
 
   53  return Qt::NoItemFlags;
 
 
   58  QModelIndex newIndex = QSortFilterProxyModel::index( row, 0, 
parent );
 
   62  return createIndex( row, column, newIndex.internalId() );
 
 
   67  return QSortFilterProxyModel::parent( createIndex( child.row(), 0, child.internalId() ) );
 
 
   72  const QModelIndex 
parent = idx.parent();
 
 
   79  if ( idx.column() == 0 )
 
   81    node = mLayerTreeModel->
index2node( mapToSource( idx ) );
 
 
   92  if ( filterText == mFilterText )
 
   95  mFilterText = filterText;
 
 
  101  return mLayerTreeModel;
 
 
  119  return nodeShown( node );
 
 
  124  return mCheckedLayers.contains( layer );
 
 
  131    mCheckedLayers << layer;
 
  135    mCheckedLayers.removeAll( layer );
 
 
  139void QgsLayerTreeFilterProxyModel::setLayerCheckedPrivate( 
QgsMapLayer *layer, 
bool checked )
 
  147  const QModelIndex 
index = mapFromSource( mLayerTreeModel->
node2index( node ) );
 
  154bool QgsLayerTreeFilterProxyModel::layerShown( 
QgsMapLayer *layer )
 const 
  166    const auto constChildren = node->
children();
 
  169      if ( nodeShown( child ) )
 
  181    if ( !mFilterText.isEmpty() && !layer->
name().contains( mFilterText, Qt::CaseInsensitive ) )
 
  186    return layerShown( layer );
 
  192  if ( idx.column() == 0 )
 
  194    if ( role == Qt::CheckStateRole )
 
  205          return Qt::Unchecked;
 
  211        bool hasChecked = 
false, hasUnchecked = 
false;
 
  213        for ( n = 0; !hasChecked || !hasUnchecked; n++ )
 
  215          const QVariant v = 
data( 
index( n, 0, idx ), role );
 
  221            case Qt::PartiallyChecked:
 
  223              return Qt::PartiallyChecked;
 
  237          return Qt::Unchecked;
 
  240        if ( hasChecked &&  hasUnchecked )
 
  241          return Qt::PartiallyChecked;
 
  246        Q_ASSERT( hasUnchecked );
 
  247        return Qt::Unchecked;
 
  252      return mLayerTreeModel->
data( mapToSource( idx ), role );
 
 
  260  if ( 
index.column() == 0 )
 
  262    if ( role == Qt::CheckStateRole )
 
  268        if ( !child.isValid() )
 
  281        if ( value.toInt() == Qt::Checked )
 
  282          setLayerCheckedPrivate( layer, 
true );
 
  283        else if ( value.toInt() == Qt::Unchecked )
 
  284          setLayerCheckedPrivate( layer, 
false );
 
  292    return mLayerTreeModel->
setData( mapToSource( 
index ), value, role );
 
 
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
virtual void setLayerChecked(QgsMapLayer *layer, bool checked)
This will set if the layer is checked or not.
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override
virtual bool setData(const QModelIndex &index, const QVariant &value, int role) override
void setFilters(Qgis::LayerFilters filters)
Defines the type layers (vector, raster, etc) shown in the tree If the list is empty,...
virtual int columnCount(const QModelIndex &parent) const override
QgsMapLayer * mapLayer(const QModelIndex &idx) const
Returns the map layer at a given index.
void setLayerTreeModel(QgsLayerTreeModel *layerTreeModel)
Sets the layer tree model.
virtual bool isLayerChecked(QgsMapLayer *layer) const
Returns if the layer is checked or not.
virtual void setFilterText(const QString &filterText=QString())
Sets the filter text to search for a layer in the tree.
void setCheckedLayers(const QList< QgsMapLayer * > layers)
Initialize the list of checked layers.
QModelIndex sibling(int row, int column, const QModelIndex &idx) const override
QModelIndex parent(const QModelIndex &child) const override
virtual QVariant data(const QModelIndex &index, int role) const override
QgsLayerTreeFilterProxyModel(QObject *parent=nullptr)
Constructor.
virtual Qt::ItemFlags flags(const QModelIndex &idx) const override
QgsLayerTreeModel * layerTreeModel() const
Rerturns the layer tree model.
QgsLayerTreeLayer * findLayer(QgsMapLayer *layer) const
Find layer node representing the map layer.
QgsMapLayer * layer() const
Returns the map layer associated with this node.
The QgsLayerTreeModel class is model implementation for Qt item views framework.
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...
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
QgsLayerTree * rootGroup() const
Returns pointer to the root node of the layer tree. Always a non nullptr value.
QgsLayerTreeNode * index2node(const QModelIndex &index) const
Returns layer tree node for given index.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
This class is a base class for nodes in a layer tree.
@ NodeGroup
Container of other groups and layers.
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
NodeType nodeType() const
Find out about type of the node. It is usually shorter to use convenience functions from QgsLayerTree...
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
static bool layerMatchesFilters(const QgsMapLayer *layer, const Qgis::LayerFilters &filters)
Returns if the layer matches the given filters.
Base class for all map layer types.
static QgsProject * instance()
Returns the QgsProject singleton instance.
void readProject(const QDomDocument &)
Emitted when a project is being read.