QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 
49 class GUI_EXPORT QgsLayerTreeView : public QTreeView
50 {
51 
52 #ifdef SIP_RUN
54  if ( sipCpp->inherits( "QgsLayerTreeView" ) )
55  sipType = sipType_QgsLayerTreeView;
56  else
57  sipType = 0;
58  SIP_END
59 #endif
60 
61 
62  Q_OBJECT
63  public:
64 
66  explicit QgsLayerTreeView( QWidget *parent SIP_TRANSFERTHIS = nullptr );
67  ~QgsLayerTreeView() override;
68 
70  void setModel( QAbstractItemModel *model ) override;
71 
73  QgsLayerTreeModel *layerTreeModel() const;
74 
76  QgsLayerTreeViewDefaultActions *defaultActions();
77 
79  void setMenuProvider( QgsLayerTreeViewMenuProvider *menuProvider SIP_TRANSFER );
81  QgsLayerTreeViewMenuProvider *menuProvider() const { return mMenuProvider; }
82 
88  QgsMapLayer *currentLayer() const;
89 
97  void setCurrentLayer( QgsMapLayer *layer );
98 
100  QgsLayerTreeNode *currentNode() const;
102  QgsLayerTreeGroup *currentGroupNode() const;
103 
108  QgsLayerTreeModelLegendNode *currentLegendNode() const;
109 
114  QList<QgsLayerTreeNode *> selectedNodes( bool skipInternal = false ) const;
116  QList<QgsLayerTreeLayer *> selectedLayerNodes() const;
117 
119  QList<QgsMapLayer *> selectedLayers() const;
120 
127  QList<QgsMapLayer *> selectedLayersRecursive() const;
128 
139  void addIndicator( QgsLayerTreeNode *node, QgsLayerTreeViewIndicator *indicator );
140 
147  void removeIndicator( QgsLayerTreeNode *node, QgsLayerTreeViewIndicator *indicator );
148 
155  QList<QgsLayerTreeViewIndicator *> indicators( QgsLayerTreeNode *node ) const;
156 
158 
168  static QStringList viewOnlyCustomProperties() SIP_SKIP;
170 
171  public slots:
173  void refreshLayerSymbology( const QString &layerId );
174 
179  void expandAllNodes();
180 
185  void collapseAllNodes();
186 
187  signals:
189  void currentLayerChanged( QgsMapLayer *layer );
190 
191  protected:
192  void contextMenuEvent( QContextMenuEvent *event ) override;
193 
194  void updateExpandedStateFromNode( QgsLayerTreeNode *node );
195 
196  QgsMapLayer *layerForIndex( const QModelIndex &index ) const;
197 
198  void mouseReleaseEvent( QMouseEvent *event ) override;
199  void keyPressEvent( QKeyEvent *event ) override;
200 
201  void dropEvent( QDropEvent *event ) override;
202 
203  protected slots:
204 
205  void modelRowsInserted( const QModelIndex &index, int start, int end );
206  void modelRowsRemoved();
207 
208  void updateExpandedStateToNode( const QModelIndex &index );
209 
210  void onCurrentChanged();
211  void onExpandedChanged( QgsLayerTreeNode *node, bool expanded );
212  void onModelReset();
213 
214  private slots:
215  void onCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
216 
217  protected:
219  QgsLayerTreeViewDefaultActions *mDefaultActions = nullptr;
221  QgsLayerTreeViewMenuProvider *mMenuProvider = nullptr;
225  QHash< QgsLayerTreeNode *, QList<QgsLayerTreeViewIndicator *> > mIndicators;
228 
229  // friend so it can access viewOptions() method and mLastReleaseMousePos without making them public
230  friend class QgsLayerTreeViewItemDelegate;
231 };
232 
233 
243 {
244  public:
245  virtual ~QgsLayerTreeViewMenuProvider() = default;
246 
248  virtual QMenu *createContextMenu() = 0 SIP_FACTORY;
249 };
250 
251 
252 #endif // QGSLAYERTREEVIEW_H
Layer tree group node serves as a container for layers and further groups.
Base class for all map layer types.
Definition: qgsmaplayer.h:78
The QgsLayerTreeView class extends QTreeView and provides some additional functionality when working ...
Implementation of this interface can be implemented to allow QgsLayerTreeView instance to provide cus...
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
QString mCurrentLayerID
Keeps track of current layer ID (to check when to emit signal about change of current layer) ...
The QgsLayerTreeViewDefaultActions class serves as a factory of actions that can be used together wit...
The QgsLayerTreeModel class is model implementation for Qt item views framework.
#define SIP_SKIP
Definition: qgis_sip.h:119
QPoint mLastReleaseMousePos
Used by the item delegate for identification of which indicator has been clicked. ...
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_END
Definition: qgis_sip.h:182
QHash< QgsLayerTreeNode *, QList< QgsLayerTreeViewIndicator * > > mIndicators
Storage of indicators used with the tree view.
This class is a base class for nodes in a layer tree.
#define SIP_FACTORY
Definition: qgis_sip.h:69
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:165
QgsLayerTreeViewMenuProvider * menuProvider() const
Returns pointer to the context menu provider. May be nullptr.
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
Indicator that can be used in a layer tree view to display icons next to items of the layer tree...
Layer tree node points to a map layer.