QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 
96  void setLayerVisible( QgsMapLayer *layer, bool visible );
97 
105  void setCurrentLayer( QgsMapLayer *layer );
106 
108  QgsLayerTreeNode *currentNode() const;
110  QgsLayerTreeGroup *currentGroupNode() const;
111 
116  QgsLayerTreeModelLegendNode *currentLegendNode() const;
117 
122  QList<QgsLayerTreeNode *> selectedNodes( bool skipInternal = false ) const;
124  QList<QgsLayerTreeLayer *> selectedLayerNodes() const;
125 
127  QList<QgsMapLayer *> selectedLayers() const;
128 
135  QList<QgsMapLayer *> selectedLayersRecursive() const;
136 
147  void addIndicator( QgsLayerTreeNode *node, QgsLayerTreeViewIndicator *indicator );
148 
155  void removeIndicator( QgsLayerTreeNode *node, QgsLayerTreeViewIndicator *indicator );
156 
163  QList<QgsLayerTreeViewIndicator *> indicators( QgsLayerTreeNode *node ) const;
164 
170  int layerMarkWidth() const { return mLayerMarkWidth; }
171 
173 
183  static QStringList viewOnlyCustomProperties() SIP_SKIP;
185 
186  public slots:
188  void refreshLayerSymbology( const QString &layerId );
189 
194  void expandAllNodes();
195 
200  void collapseAllNodes();
201 
207  void setLayerMarkWidth( int width ) { mLayerMarkWidth = width; }
208 
209  signals:
211  void currentLayerChanged( QgsMapLayer *layer );
212 
213  protected:
214  void contextMenuEvent( QContextMenuEvent *event ) override;
215 
216  void updateExpandedStateFromNode( QgsLayerTreeNode *node );
217 
218  QgsMapLayer *layerForIndex( const QModelIndex &index ) const;
219 
220  void mouseReleaseEvent( QMouseEvent *event ) override;
221  void keyPressEvent( QKeyEvent *event ) override;
222 
223  void dropEvent( QDropEvent *event ) override;
224 
225  void resizeEvent( QResizeEvent *event ) override;
226 
227  protected slots:
228 
229  void modelRowsInserted( const QModelIndex &index, int start, int end );
230  void modelRowsRemoved();
231 
232  void updateExpandedStateToNode( const QModelIndex &index );
233 
234  void onCurrentChanged();
235  void onExpandedChanged( QgsLayerTreeNode *node, bool expanded );
236  void onModelReset();
237 
238  private slots:
239  void onCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
240 
241  protected:
243  QgsLayerTreeViewDefaultActions *mDefaultActions = nullptr;
245  QgsLayerTreeViewMenuProvider *mMenuProvider = nullptr;
249  QHash< QgsLayerTreeNode *, QList<QgsLayerTreeViewIndicator *> > mIndicators;
252 
255 
256  // friend so it can access viewOptions() method and mLastReleaseMousePos without making them public
257  friend class QgsLayerTreeViewItemDelegate;
258 };
259 
260 
270 {
271  public:
272  virtual ~QgsLayerTreeViewMenuProvider() = default;
273 
275  virtual QMenu *createContextMenu() = 0 SIP_FACTORY;
276 };
277 
278 
279 #endif // QGSLAYERTREEVIEW_H
int layerMarkWidth() const
Returns width of contextual menu mark, at right of layer node items.
Layer tree group node serves as a container for layers and further groups.
Base class for all map layer types.
Definition: qgsmaplayer.h:79
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:53
int mLayerMarkWidth
Width of contextual menu mark for layer nodes.
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:126
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:189
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:76
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:172
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...
void setLayerMarkWidth(int width)
Set width of contextual menu mark, at right of layer node items.
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.