23#include "moc_qgslayertreefilterproxymodel.cpp"
26 : QSortFilterProxyModel(
parent )
43 mCheckedLayers = layers;
55 if ( idx.column() == 0 )
57 return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
59 return Qt::NoItemFlags;
64 QModelIndex newIndex = QSortFilterProxyModel::index( row, 0,
parent );
68 return createIndex( row, column, newIndex.internalId() );
73 return QSortFilterProxyModel::parent( createIndex( child.row(), 0, child.internalId() ) );
78 const QModelIndex
parent = idx.parent();
85 if ( idx.column() == 0 )
87 node = mLayerTreeModel->index2node( mapToSource( idx ) );
98 if ( filterText == mFilterText )
101 mFilterText = filterText;
107 return mLayerTreeModel;
118 return mShowPrivateLayers;
123 if ( showPrivate == mShowPrivateLayers )
126 mShowPrivateLayers = showPrivate;
138 QgsLayerTreeNode *node = mLayerTreeModel->index2node( mLayerTreeModel->index( sourceRow, 0, sourceParent ) );
144 return mCheckedLayers.contains( layer );
151 mCheckedLayers << layer;
155 mCheckedLayers.removeAll( layer );
159void QgsLayerTreeFilterProxyModel::setLayerCheckedPrivate(
QgsMapLayer *layer,
bool checked )
167 const QModelIndex
index = mapFromSource( mLayerTreeModel->
node2index( node ) );
174bool QgsLayerTreeFilterProxyModel::layerShown(
QgsMapLayer *layer )
const
186 const auto constChildren = node->
children();
201 if ( !mFilterText.isEmpty() && !layer->
name().contains( mFilterText, Qt::CaseInsensitive ) )
210 return layerShown( layer );
216 if ( idx.column() == 0 )
218 if ( role == Qt::CheckStateRole )
229 return Qt::Unchecked;
235 bool hasChecked =
false, hasUnchecked =
false;
237 for ( n = 0; !hasChecked || !hasUnchecked; n++ )
239 const QVariant v =
data(
index( n, 0, idx ), role );
245 case Qt::PartiallyChecked:
247 return Qt::PartiallyChecked;
261 return Qt::Unchecked;
264 if ( hasChecked && hasUnchecked )
265 return Qt::PartiallyChecked;
270 Q_ASSERT( hasUnchecked );
271 return Qt::Unchecked;
276 return mLayerTreeModel->data( mapToSource( idx ), role );
284 if (
index.column() == 0 )
286 if ( role == Qt::CheckStateRole )
292 if ( !child.isValid() )
305 if ( value.toInt() == Qt::Checked )
306 setLayerCheckedPrivate( layer,
true );
307 else if ( value.toInt() == Qt::Unchecked )
308 setLayerCheckedPrivate( layer,
false );
316 return mLayerTreeModel->setData( mapToSource(
index ), value, role );
QFlags< LayerFilter > LayerFilters
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
bool nodeShown(QgsLayerTreeNode *node) const
Returns true if the specified node will be shown in the model.
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
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,...
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.
void setShowPrivateLayers(bool showPrivate)
Determines if private layers are shown.
QModelIndex sibling(int row, int column, const QModelIndex &idx) const override
QModelIndex parent(const QModelIndex &child) const override
QVariant data(const QModelIndex &index, int role) const override
QgsLayerTreeFilterProxyModel(QObject *parent=nullptr)
Constructor.
Qt::ItemFlags flags(const QModelIndex &idx) const override
bool showPrivateLayers() const
Returns if private layers are shown.
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.
A model representing the layer tree, including layers and groups of layers.
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...
QgsLayerTree * rootGroup() const
Returns pointer to the root node of the layer tree. Always a non nullptr value.
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.
QgsMapLayer::LayerFlags flags
@ Private
Determines if the layer is meant to be exposed to the GUI, i.e. visible in the layer legend tree.
static QgsProject * instance()
Returns the QgsProject singleton instance.
void readProject(const QDomDocument &document)
Emitted when a project is being read.