QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 class QgsMessageBar;
33 
34 
51 class GUI_EXPORT QgsLayerTreeView : public QTreeView
52 {
53 
54 #ifdef SIP_RUN
56  if ( sipCpp->inherits( "QgsLayerTreeView" ) )
57  sipType = sipType_QgsLayerTreeView;
58  else
59  sipType = 0;
60  SIP_END
61 #endif
62 
63 
64  Q_OBJECT
65  public:
66 
68  explicit QgsLayerTreeView( QWidget *parent SIP_TRANSFERTHIS = nullptr );
69  ~QgsLayerTreeView() override;
70 
72  void setModel( QAbstractItemModel *model ) override;
73 
75  QgsLayerTreeModel *layerTreeModel() const;
76 
78  QgsLayerTreeViewDefaultActions *defaultActions();
79 
81  void setMenuProvider( QgsLayerTreeViewMenuProvider *menuProvider SIP_TRANSFER );
83  QgsLayerTreeViewMenuProvider *menuProvider() const { return mMenuProvider; }
84 
90  QgsMapLayer *currentLayer() const;
91 
98  void setLayerVisible( QgsMapLayer *layer, bool visible );
99 
107  void setCurrentLayer( QgsMapLayer *layer );
108 
110  QgsLayerTreeNode *currentNode() const;
112  QgsLayerTreeGroup *currentGroupNode() const;
113 
118  QgsLayerTreeModelLegendNode *currentLegendNode() const;
119 
124  QList<QgsLayerTreeNode *> selectedNodes( bool skipInternal = false ) const;
126  QList<QgsLayerTreeLayer *> selectedLayerNodes() const;
127 
129  QList<QgsMapLayer *> selectedLayers() const;
130 
137  QList<QgsMapLayer *> selectedLayersRecursive() const;
138 
149  void addIndicator( QgsLayerTreeNode *node, QgsLayerTreeViewIndicator *indicator );
150 
157  void removeIndicator( QgsLayerTreeNode *node, QgsLayerTreeViewIndicator *indicator );
158 
165  QList<QgsLayerTreeViewIndicator *> indicators( QgsLayerTreeNode *node ) const;
166 
172  int layerMarkWidth() const { return mLayerMarkWidth; }
173 
175 
185  static QStringList viewOnlyCustomProperties() SIP_SKIP;
187 
188  public slots:
190  void refreshLayerSymbology( const QString &layerId );
191 
196  void expandAllNodes();
197 
202  void collapseAllNodes();
203 
209  void setLayerMarkWidth( int width ) { mLayerMarkWidth = width; }
210 
215  void setMessageBar( QgsMessageBar *messageBar );
216 
217  signals:
219  void currentLayerChanged( QgsMapLayer *layer );
220 
221  protected:
222  void contextMenuEvent( QContextMenuEvent *event ) override;
223 
224  void updateExpandedStateFromNode( QgsLayerTreeNode *node );
225 
226  QgsMapLayer *layerForIndex( const QModelIndex &index ) const;
227 
228  void mouseReleaseEvent( QMouseEvent *event ) override;
229  void keyPressEvent( QKeyEvent *event ) override;
230 
231  void dropEvent( QDropEvent *event ) override;
232 
233  void resizeEvent( QResizeEvent *event ) override;
234 
235  protected slots:
236 
237  void modelRowsInserted( const QModelIndex &index, int start, int end );
238  void modelRowsRemoved();
239 
240  void updateExpandedStateToNode( const QModelIndex &index );
241 
242  void onCurrentChanged();
243  void onExpandedChanged( QgsLayerTreeNode *node, bool expanded );
244  void onModelReset();
245 
246  private slots:
247  void onCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
249  void onHorizontalScroll( int value );
250 
251  protected:
253  QgsLayerTreeViewDefaultActions *mDefaultActions = nullptr;
255  QgsLayerTreeViewMenuProvider *mMenuProvider = nullptr;
259  QHash< QgsLayerTreeNode *, QList<QgsLayerTreeViewIndicator *> > mIndicators;
262 
265 
266  private:
267  QgsMessageBar *mMessageBar = nullptr;
268 
269  // friend so it can access viewOptions() method and mLastReleaseMousePos without making them public
270  friend class QgsLayerTreeViewItemDelegate;
271 };
272 
273 
283 {
284  public:
285  virtual ~QgsLayerTreeViewMenuProvider() = default;
286 
288  virtual QMenu *createContextMenu() = 0 SIP_FACTORY;
289 };
290 
291 
292 #endif // QGSLAYERTREEVIEW_H
QgsLayerTreeNode
Definition: qgslayertreenode.h:74
qgis.h
QgsLayerTreeViewMenuProvider
Definition: qgslayertreeview.h:282
QgsLayerTreeView::menuProvider
QgsLayerTreeViewMenuProvider * menuProvider() const
Returns pointer to the context menu provider. May be nullptr.
Definition: qgslayertreeview.h:83
QgsLayerTreeModel
Definition: qgslayertreemodel.h:53
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
SIP_CONVERT_TO_SUBCLASS_CODE
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:172
QgsLayerTreeViewIndicator
Definition: qgslayertreeviewindicator.h:35
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsLayerTreeView::layerMarkWidth
int layerMarkWidth() const
Returns width of contextual menu mark, at right of layer node items.
Definition: qgslayertreeview.h:172
QgsLayerTreeLayer
Definition: qgslayertreelayer.h:43
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QgsLayerTreeGroup
Definition: qgslayertreegroup.h:34
QgsMessageBar
Definition: qgsmessagebar.h:60
QgsLayerTreeViewDefaultActions
Definition: qgslayertreeviewdefaultactions.h:39
QgsMapLayer
Definition: qgsmaplayer.h:81
QgsLayerTreeView::mLayerMarkWidth
int mLayerMarkWidth
Width of contextual menu mark for layer nodes.
Definition: qgslayertreeview.h:264
QgsLayerTreeView
Definition: qgslayertreeview.h:51
QgsLayerTreeView::mCurrentLayerID
QString mCurrentLayerID
Keeps track of current layer ID (to check when to emit signal about change of current layer)
Definition: qgslayertreeview.h:257
SIP_END
#define SIP_END
Definition: qgis_sip.h:189
SIP_TRANSFERTHIS
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
QgsLayerTreeView::mLastReleaseMousePos
QPoint mLastReleaseMousePos
Used by the item delegate for identification of which indicator has been clicked.
Definition: qgslayertreeview.h:261
QgsLayerTreeView::mIndicators
QHash< QgsLayerTreeNode *, QList< QgsLayerTreeViewIndicator * > > mIndicators
Storage of indicators used with the tree view.
Definition: qgslayertreeview.h:259
QgsLayerTreeModelLegendNode
Definition: qgslayertreemodellegendnode.h:50