QGIS API Documentation 3.29.0-Master (ade4f0cf0f)
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
31class QgsMapLayer;
32class QgsMessageBar;
34
35
36#include <QSortFilterProxyModel>
37
46class GUI_EXPORT QgsLayerTreeProxyModel : public QSortFilterProxyModel
47{
48 Q_OBJECT
49
50 public:
51
55 QgsLayerTreeProxyModel( QgsLayerTreeModel *treeModel, QObject *parent );
56
60 void setFilterText( const QString &filterText = QString() );
61
65 bool showPrivateLayers() const;
66
70 void setShowPrivateLayers( bool showPrivate );
71
72 protected:
73
74 bool filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const override;
75
76 private:
77
78 bool nodeShown( QgsLayerTreeNode *node ) const;
79
80 QgsLayerTreeModel *mLayerTreeModel = nullptr;
81 QString mFilterText;
82 bool mShowPrivateLayers = false;
83
84};
85
86
103class GUI_EXPORT QgsLayerTreeView : public QTreeView
104{
105
106#ifdef SIP_RUN
108 if ( sipCpp->inherits( "QgsLayerTreeView" ) )
109 sipType = sipType_QgsLayerTreeView;
110 else
111 sipType = 0;
112 SIP_END
113#endif
114
115
116 Q_OBJECT
117 public:
118
120 explicit QgsLayerTreeView( QWidget *parent SIP_TRANSFERTHIS = nullptr );
121 ~QgsLayerTreeView() override;
122
124 void setModel( QAbstractItemModel *model ) override;
125
127 QgsLayerTreeModel *layerTreeModel() const;
128
136 QgsLayerTreeProxyModel *proxyModel() const;
137
147 QgsLayerTreeNode *index2node( const QModelIndex &index ) const;
148
157 QModelIndex node2index( QgsLayerTreeNode *node ) const;
158
159
165 QModelIndex node2sourceIndex( QgsLayerTreeNode *node ) const;
166
167
176 QgsLayerTreeModelLegendNode *index2legendNode( const QModelIndex &index ) const;
177
187 QModelIndex legendNode2index( QgsLayerTreeModelLegendNode *legendNode );
188
195 QModelIndex legendNode2sourceIndex( QgsLayerTreeModelLegendNode *legendNode );
196
198 QgsLayerTreeViewDefaultActions *defaultActions();
199
201 void setMenuProvider( QgsLayerTreeViewMenuProvider *menuProvider SIP_TRANSFER );
203 QgsLayerTreeViewMenuProvider *menuProvider() const { return mMenuProvider; }
204
210 QgsMapLayer *currentLayer() const;
211
218 void setLayerVisible( QgsMapLayer *layer, bool visible );
219
227 void setCurrentLayer( QgsMapLayer *layer );
228
230 QgsLayerTreeNode *currentNode() const;
232 QgsLayerTreeGroup *currentGroupNode() const;
233
238 QgsLayerTreeModelLegendNode *currentLegendNode() const;
239
244 QList<QgsLayerTreeNode *> selectedNodes( bool skipInternal = false ) const;
246 QList<QgsLayerTreeLayer *> selectedLayerNodes() const;
247
249 QList<QgsMapLayer *> selectedLayers() const;
250
257 QList<QgsMapLayer *> selectedLayersRecursive() const;
258
269 void addIndicator( QgsLayerTreeNode *node, QgsLayerTreeViewIndicator *indicator );
270
277 void removeIndicator( QgsLayerTreeNode *node, QgsLayerTreeViewIndicator *indicator );
278
285 QList<QgsLayerTreeViewIndicator *> indicators( QgsLayerTreeNode *node ) const;
286
292 int layerMarkWidth() const { return mLayerMarkWidth; }
293
295
305 static QStringList viewOnlyCustomProperties() SIP_SKIP;
306
308
309 public slots:
311 void refreshLayerSymbology( const QString &layerId );
312
317 void expandAllNodes();
318
323 void collapseAllNodes();
324
330 void setLayerMarkWidth( int width ) { mLayerMarkWidth = width; }
331
336 void setMessageBar( QgsMessageBar *messageBar );
337
342 void setShowPrivateLayers( bool showPrivate );
343
348 bool showPrivateLayers( );
349
350 signals:
353
355 void datasetsDropped( QDropEvent *event );
356
357 protected:
358 void contextMenuEvent( QContextMenuEvent *event ) override;
359
360 void updateExpandedStateFromNode( QgsLayerTreeNode *node );
361
362 QgsMapLayer *layerForIndex( const QModelIndex &index ) const;
363
364 void mouseReleaseEvent( QMouseEvent *event ) override;
365 void keyPressEvent( QKeyEvent *event ) override;
366
367 void dragEnterEvent( QDragEnterEvent *event ) override;
368 void dragMoveEvent( QDragMoveEvent *event ) override;
369 void dropEvent( QDropEvent *event ) override;
370
371 void resizeEvent( QResizeEvent *event ) override;
372
373 protected slots:
374
375 void modelRowsInserted( const QModelIndex &index, int start, int end );
376 void modelRowsRemoved();
377
378 void updateExpandedStateToNode( const QModelIndex &index );
379
380 void onCurrentChanged();
381 void onExpandedChanged( QgsLayerTreeNode *node, bool expanded );
382 void onModelReset();
383
384 private slots:
385 void onCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
387 void onHorizontalScroll( int value );
388
389 void onDataChanged( const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles );
390
391 protected:
393 QgsLayerTreeViewDefaultActions *mDefaultActions = nullptr;
395 QgsLayerTreeViewMenuProvider *mMenuProvider = nullptr;
399 QHash< QgsLayerTreeNode *, QList<QgsLayerTreeViewIndicator *> > mIndicators;
402
405
406 private:
407 QgsLayerTreeProxyModel *mProxyModel = nullptr;
408
409 QgsMessageBar *mMessageBar = nullptr;
410
411 bool mShowPrivateLayers = false;
412
413 // For model debugging
414 // void checkModel( );
415
416 // friend so it can access viewOptions() method and mLastReleaseMousePos without making them public
417 friend class QgsLayerTreeViewItemDelegate;
418};
419
420
430{
431 public:
432 virtual ~QgsLayerTreeViewMenuProvider() = default;
433
435 virtual QMenu *createContextMenu() = 0 SIP_FACTORY;
436};
437
438
439#endif // QGSLAYERTREEVIEW_H
QgsLayerTreeFilterProxyModel is a sort filter proxy model to easily reproduce the legend/layer tree i...
Layer tree group node serves as a container for layers and further groups.
Layer tree node points to a map layer.
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
The QgsLayerTreeModel class is model implementation for Qt item views framework.
This class is a base class for nodes in a layer tree.
The QgsLayerTreeProxyModel class is a proxy model for QgsLayerTreeModel, supports private layers and ...
The QgsLayerTreeViewDefaultActions class serves as a factory of actions that can be used together wit...
Indicator that can be used in a layer tree view to display icons next to items of the layer tree.
Implementation of this interface can be implemented to allow QgsLayerTreeView instance to provide cus...
virtual QMenu * createContextMenu()=0
Returns a newly created menu instance (or nullptr on error)
virtual ~QgsLayerTreeViewMenuProvider()=default
The QgsLayerTreeView class extends QTreeView and provides some additional functionality when working ...
int mLayerMarkWidth
Width of contextual menu mark for layer nodes.
void currentLayerChanged(QgsMapLayer *layer)
Emitted when a current layer is changed.
QString mCurrentLayerID
Keeps track of current layer ID (to check when to emit signal about change of current layer)
void datasetsDropped(QDropEvent *event)
Emitted when datasets are dropped onto the layer tree view.
QHash< QgsLayerTreeNode *, QList< QgsLayerTreeViewIndicator * > > mIndicators
Storage of indicators used with the tree view.
QgsLayerTreeViewMenuProvider * menuProvider() const
Returns pointer to the context menu provider. May be nullptr.
QPoint mLastReleaseMousePos
Used by the item delegate for identification of which indicator has been clicked.
int layerMarkWidth() const
Returns width of contextual menu mark, at right of layer node items.
Base class for all map layer types.
Definition: qgsmaplayer.h:73
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:61
QgsLayerTreeModelLegendNode * legendNode(const QString &rule, QgsLayerTreeModel &model)
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:186
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_FACTORY
Definition: qgis_sip.h:76
#define SIP_END
Definition: qgis_sip.h:203