QGIS API Documentation  3.0.2-Girona (307d082)
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;
30 class QgsMapLayer;
31 
48 class GUI_EXPORT QgsLayerTreeView : public QTreeView
49 {
50 
51 #ifdef SIP_RUN
53  if ( sipCpp->inherits( "QgsLayerTreeView" ) )
54  sipType = sipType_QgsLayerTreeView;
55  else
56  sipType = 0;
57  SIP_END
58 #endif
59 
60 
61  Q_OBJECT
62  public:
63 
65  explicit QgsLayerTreeView( QWidget *parent SIP_TRANSFERTHIS = nullptr );
66  ~QgsLayerTreeView() override;
67 
69  void setModel( QAbstractItemModel *model ) override;
70 
72  QgsLayerTreeModel *layerTreeModel() const;
73 
75  QgsLayerTreeViewDefaultActions *defaultActions();
76 
78  void setMenuProvider( QgsLayerTreeViewMenuProvider *menuProvider SIP_TRANSFER );
80  QgsLayerTreeViewMenuProvider *menuProvider() const { return mMenuProvider; }
81 
83  QgsMapLayer *currentLayer() const;
85  void setCurrentLayer( QgsMapLayer *layer );
86 
88  QgsLayerTreeNode *currentNode() const;
90  QgsLayerTreeGroup *currentGroupNode() const;
91 
96  QgsLayerTreeModelLegendNode *currentLegendNode() const;
97 
102  QList<QgsLayerTreeNode *> selectedNodes( bool skipInternal = false ) const;
104  QList<QgsLayerTreeLayer *> selectedLayerNodes() const;
105 
107  QList<QgsMapLayer *> selectedLayers() const;
108 
109  public slots:
111  void refreshLayerSymbology( const QString &layerId );
112 
117  void expandAllNodes();
118 
123  void collapseAllNodes();
124 
125  signals:
127  void currentLayerChanged( QgsMapLayer *layer );
128 
129  protected:
130  void contextMenuEvent( QContextMenuEvent *event ) override;
131 
132  void updateExpandedStateFromNode( QgsLayerTreeNode *node );
133 
134  QgsMapLayer *layerForIndex( const QModelIndex &index ) const;
135 
136  void mouseReleaseEvent( QMouseEvent *event ) override;
137  void keyPressEvent( QKeyEvent *event ) override;
138 
139  void dropEvent( QDropEvent *event ) override;
140 
141 
142  protected slots:
143 
144  void modelRowsInserted( const QModelIndex &index, int start, int end );
145  void modelRowsRemoved();
146 
147  void updateExpandedStateToNode( const QModelIndex &index );
148 
149  void onCurrentChanged();
150  void onExpandedChanged( QgsLayerTreeNode *node, bool expanded );
151  void onModelReset();
152 
153  protected:
155  QgsLayerTreeViewDefaultActions *mDefaultActions = nullptr;
157  QgsLayerTreeViewMenuProvider *mMenuProvider = nullptr;
160 };
161 
162 
172 {
173  public:
174  virtual ~QgsLayerTreeViewMenuProvider() = default;
175 
177  virtual QMenu *createContextMenu() = 0 SIP_FACTORY;
178 };
179 
180 
181 #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:56
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_TRANSFER
Definition: qgis_sip.h:36
#define SIP_END
Definition: qgis_sip.h:175
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
Return pointer to the context menu provider. May be null.
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
Layer tree node points to a map layer.