QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
qgslayertreeview.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayertreeview.h
3  --------------------------------------
4  Date : May 2014
5  Copyright : (C) 2014 by Martin Dobias
6  Email : wonder dot sk at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #ifndef QGSLAYERTREEVIEW_H
17 #define QGSLAYERTREEVIEW_H
18 
19 #include <QTreeView>
20 #include "qgis.h"
21 #include "qgis_gui.h"
22 
23 class QgsLayerTreeGroup;
24 class QgsLayerTreeLayer;
25 class QgsLayerTreeModel;
26 class QgsLayerTreeNode;
31 class QgsMapLayer;
32 class QgsMessageBar;
34 
35 
36 #include <QSortFilterProxyModel>
37 
46 class GUI_EXPORT QgsLayerTreeProxyModel : public QSortFilterProxyModel
47 {
48  Q_OBJECT
49 
50  public:
51 
55  QgsLayerTreeProxyModel( QgsLayerTreeModel *treeModel, QObject *parent );
56 
60  void setFilterText( const QString &filterText = QString() );
61 
65  bool showPrivateLayers() const;
66 
70  void setShowPrivateLayers( bool showPrivate );
71 
72  protected:
73 
74  bool filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const override;
75 
76  private:
77 
78  bool nodeShown( QgsLayerTreeNode *node ) const;
79 
80  QgsLayerTreeModel *mLayerTreeModel = nullptr;
81  QString mFilterText;
82  bool mShowPrivateLayers = false;
83 
84 };
85 
86 
103 class GUI_EXPORT QgsLayerTreeView : public QTreeView
104 {
105 
106 #ifdef SIP_RUN
108  if ( sipCpp->inherits( "QgsLayerTreeView" ) )
109  sipType = sipType_QgsLayerTreeView;
110  else
111  sipType = 0;
112  SIP_END
113 #endif
114 
115 
116  Q_OBJECT
117  public:
118 
120  explicit QgsLayerTreeView( QWidget *parent SIP_TRANSFERTHIS = nullptr );
121  ~QgsLayerTreeView() override;
122 
124  void setModel( QAbstractItemModel *model ) override;
125 
127  QgsLayerTreeModel *layerTreeModel() const;
128 
136  QgsLayerTreeProxyModel *proxyModel() const;
137 
147  QgsLayerTreeNode *index2node( const QModelIndex &index ) const;
148 
157  QModelIndex node2index( QgsLayerTreeNode *node ) const;
158 
159 
165  QModelIndex node2sourceIndex( QgsLayerTreeNode *node ) const;
166 
167 
176  QgsLayerTreeModelLegendNode *index2legendNode( const QModelIndex &index ) const;
177 
187  QModelIndex legendNode2index( QgsLayerTreeModelLegendNode *legendNode );
188 
195  QModelIndex legendNode2sourceIndex( QgsLayerTreeModelLegendNode *legendNode );
196 
198  QgsLayerTreeViewDefaultActions *defaultActions();
199 
201  void setMenuProvider( QgsLayerTreeViewMenuProvider *menuProvider SIP_TRANSFER );
203  QgsLayerTreeViewMenuProvider *menuProvider() const { return mMenuProvider; }
204 
210  QgsMapLayer *currentLayer() const;
211 
218  void setLayerVisible( QgsMapLayer *layer, bool visible );
219 
227  void setCurrentLayer( QgsMapLayer *layer );
228 
230  QgsLayerTreeNode *currentNode() const;
232  QgsLayerTreeGroup *currentGroupNode() const;
233 
238  QgsLayerTreeModelLegendNode *currentLegendNode() const;
239 
244  QList<QgsLayerTreeNode *> selectedNodes( bool skipInternal = false ) const;
246  QList<QgsLayerTreeLayer *> selectedLayerNodes() const;
247 
249  QList<QgsMapLayer *> selectedLayers() const;
250 
257  QList<QgsMapLayer *> selectedLayersRecursive() const;
258 
269  void addIndicator( QgsLayerTreeNode *node, QgsLayerTreeViewIndicator *indicator );
270 
277  void removeIndicator( QgsLayerTreeNode *node, QgsLayerTreeViewIndicator *indicator );
278 
285  QList<QgsLayerTreeViewIndicator *> indicators( QgsLayerTreeNode *node ) const;
286 
292  int layerMarkWidth() const { return mLayerMarkWidth; }
293 
295 
305  static QStringList viewOnlyCustomProperties() SIP_SKIP;
307 
308  public slots:
310  void refreshLayerSymbology( const QString &layerId );
311 
316  void expandAllNodes();
317 
322  void collapseAllNodes();
323 
329  void setLayerMarkWidth( int width ) { mLayerMarkWidth = width; }
330 
335  void setMessageBar( QgsMessageBar *messageBar );
336 
341  void setShowPrivateLayers( bool showPrivate );
342 
347  bool showPrivateLayers( );
348 
349  signals:
352 
353  protected:
354  void contextMenuEvent( QContextMenuEvent *event ) override;
355 
356  void updateExpandedStateFromNode( QgsLayerTreeNode *node );
357 
358  QgsMapLayer *layerForIndex( const QModelIndex &index ) const;
359 
360  void mouseReleaseEvent( QMouseEvent *event ) override;
361  void keyPressEvent( QKeyEvent *event ) override;
362 
363  void dropEvent( QDropEvent *event ) override;
364 
365  void resizeEvent( QResizeEvent *event ) override;
366 
367  protected slots:
368 
369  void modelRowsInserted( const QModelIndex &index, int start, int end );
370  void modelRowsRemoved();
371 
372  void updateExpandedStateToNode( const QModelIndex &index );
373 
374  void onCurrentChanged();
375  void onExpandedChanged( QgsLayerTreeNode *node, bool expanded );
376  void onModelReset();
377 
378  private slots:
379  void onCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
381  void onHorizontalScroll( int value );
382 
383  void onDataChanged( const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles );
384 
385  protected:
387  QgsLayerTreeViewDefaultActions *mDefaultActions = nullptr;
389  QgsLayerTreeViewMenuProvider *mMenuProvider = nullptr;
393  QHash< QgsLayerTreeNode *, QList<QgsLayerTreeViewIndicator *> > mIndicators;
396 
399 
400  private:
401  QgsLayerTreeProxyModel *mProxyModel = nullptr;
402 
403  QgsMessageBar *mMessageBar = nullptr;
404 
405  bool mShowPrivateLayers = false;
406 
407  // For model debugging
408  // void checkModel( );
409 
410  // friend so it can access viewOptions() method and mLastReleaseMousePos without making them public
411  friend class QgsLayerTreeViewItemDelegate;
412 };
413 
414 
424 {
425  public:
426  virtual ~QgsLayerTreeViewMenuProvider() = default;
427 
429  virtual QMenu *createContextMenu() = 0 SIP_FACTORY;
430 };
431 
432 
433 #endif // QGSLAYERTREEVIEW_H
QgsLayerTreeFilterProxyModel is a sort filter proxy model to easily reproduce the legend/layer tree i...
Layer tree group node serves as a container for layers and further groups.
Layer tree node points to a map layer.
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
The QgsLayerTreeModel class is model implementation for Qt item views framework.
This class is a base class for nodes in a layer tree.
The QgsLayerTreeProxyModel class is a proxy model for QgsLayerTreeModel, supports private layers and ...
The QgsLayerTreeViewDefaultActions class serves as a factory of actions that can be used together wit...
Indicator that can be used in a layer tree view to display icons next to items of the layer tree.
Implementation of this interface can be implemented to allow QgsLayerTreeView instance to provide cus...
virtual QMenu * createContextMenu()=0
Returns a newly created menu instance (or nullptr on error)
virtual ~QgsLayerTreeViewMenuProvider()=default
The QgsLayerTreeView class extends QTreeView and provides some additional functionality when working ...
int mLayerMarkWidth
Width of contextual menu mark for layer nodes.
void currentLayerChanged(QgsMapLayer *layer)
Emitted when a current layer is changed.
QString mCurrentLayerID
Keeps track of current layer ID (to check when to emit signal about change of current layer)
QHash< QgsLayerTreeNode *, QList< QgsLayerTreeViewIndicator * > > mIndicators
Storage of indicators used with the tree view.
QgsLayerTreeViewMenuProvider * menuProvider() const
Returns pointer to the context menu provider. May be nullptr.
QPoint mLastReleaseMousePos
Used by the item delegate for identification of which indicator has been clicked.
int layerMarkWidth() const
Returns width of contextual menu mark, at right of layer node items.
Base class for all map layer types.
Definition: qgsmaplayer.h:73
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:61
QgsLayerTreeModelLegendNode * legendNode(const QString &rule, QgsLayerTreeModel &model)
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:177
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_FACTORY
Definition: qgis_sip.h:76
#define SIP_END
Definition: qgis_sip.h:194