QGIS API Documentation  3.6.0-Noosa (5873452)
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 
84  QgsMapLayer *currentLayer() const;
86  void setCurrentLayer( QgsMapLayer *layer );
87 
89  QgsLayerTreeNode *currentNode() const;
91  QgsLayerTreeGroup *currentGroupNode() const;
92 
97  QgsLayerTreeModelLegendNode *currentLegendNode() const;
98 
103  QList<QgsLayerTreeNode *> selectedNodes( bool skipInternal = false ) const;
105  QList<QgsLayerTreeLayer *> selectedLayerNodes() const;
106 
108  QList<QgsMapLayer *> selectedLayers() const;
109 
116  QList<QgsMapLayer *> selectedLayersRecursive() const;
117 
128  void addIndicator( QgsLayerTreeNode *node, QgsLayerTreeViewIndicator *indicator );
129 
136  void removeIndicator( QgsLayerTreeNode *node, QgsLayerTreeViewIndicator *indicator );
137 
144  QList<QgsLayerTreeViewIndicator *> indicators( QgsLayerTreeNode *node ) const;
145 
147 
157  static QStringList viewOnlyCustomProperties() SIP_SKIP;
159 
160  public slots:
162  void refreshLayerSymbology( const QString &layerId );
163 
168  void expandAllNodes();
169 
174  void collapseAllNodes();
175 
176  signals:
178  void currentLayerChanged( QgsMapLayer *layer );
179 
180  protected:
181  void contextMenuEvent( QContextMenuEvent *event ) override;
182 
183  void updateExpandedStateFromNode( QgsLayerTreeNode *node );
184 
185  QgsMapLayer *layerForIndex( const QModelIndex &index ) const;
186 
187  void mouseReleaseEvent( QMouseEvent *event ) override;
188  void keyPressEvent( QKeyEvent *event ) override;
189 
190  void dropEvent( QDropEvent *event ) override;
191 
192  protected slots:
193 
194  void modelRowsInserted( const QModelIndex &index, int start, int end );
195  void modelRowsRemoved();
196 
197  void updateExpandedStateToNode( const QModelIndex &index );
198 
199  void onCurrentChanged();
200  void onExpandedChanged( QgsLayerTreeNode *node, bool expanded );
201  void onModelReset();
202 
203  private slots:
204  void onCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
205 
206  protected:
208  QgsLayerTreeViewDefaultActions *mDefaultActions = nullptr;
210  QgsLayerTreeViewMenuProvider *mMenuProvider = nullptr;
214  QHash< QgsLayerTreeNode *, QList<QgsLayerTreeViewIndicator *> > mIndicators;
217 
218  // friend so it can access viewOptions() method and mLastReleaseMousePos without making them public
219  friend class QgsLayerTreeViewItemDelegate;
220 };
221 
222 
232 {
233  public:
234  virtual ~QgsLayerTreeViewMenuProvider() = default;
235 
237  virtual QMenu *createContextMenu() = 0 SIP_FACTORY;
238 };
239 
240 
241 #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:64
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 null.
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.