QGIS API Documentation  3.2.0-Bonn (bc43194)
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 
120  void addIndicator( QgsLayerTreeNode *node, QgsLayerTreeViewIndicator *indicator );
121 
128  void removeIndicator( QgsLayerTreeNode *node, QgsLayerTreeViewIndicator *indicator );
129 
136  QList<QgsLayerTreeViewIndicator *> indicators( QgsLayerTreeNode *node ) const;
137 
139 
149  static QStringList viewOnlyCustomProperties() SIP_SKIP;
151 
152  public slots:
154  void refreshLayerSymbology( const QString &layerId );
155 
160  void expandAllNodes();
161 
166  void collapseAllNodes();
167 
168  signals:
170  void currentLayerChanged( QgsMapLayer *layer );
171 
172  protected:
173  void contextMenuEvent( QContextMenuEvent *event ) override;
174 
175  void updateExpandedStateFromNode( QgsLayerTreeNode *node );
176 
177  QgsMapLayer *layerForIndex( const QModelIndex &index ) const;
178 
179  void mouseReleaseEvent( QMouseEvent *event ) override;
180  void keyPressEvent( QKeyEvent *event ) override;
181 
182  void dropEvent( QDropEvent *event ) override;
183 
184  protected slots:
185 
186  void modelRowsInserted( const QModelIndex &index, int start, int end );
187  void modelRowsRemoved();
188 
189  void updateExpandedStateToNode( const QModelIndex &index );
190 
191  void onCurrentChanged();
192  void onExpandedChanged( QgsLayerTreeNode *node, bool expanded );
193  void onModelReset();
194 
195  private slots:
196  void onCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
197 
198  protected:
200  QgsLayerTreeViewDefaultActions *mDefaultActions = nullptr;
202  QgsLayerTreeViewMenuProvider *mMenuProvider = nullptr;
206  QHash< QgsLayerTreeNode *, QList<QgsLayerTreeViewIndicator *> > mIndicators;
209 
210  // friend so it can access viewOptions() method and mLastReleaseMousePos without making them public
211  friend class QgsLayerTreeViewItemDelegate;
212 };
213 
214 
224 {
225  public:
226  virtual ~QgsLayerTreeViewMenuProvider() = default;
227 
229  virtual QMenu *createContextMenu() = 0 SIP_FACTORY;
230 };
231 
232 
233 #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:61
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.