QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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
78 bool hideValidLayers() const;
79
86 void setHideValidLayers( bool hideValid );
87
88 protected:
89
90 bool filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const override;
91
92 private:
93
94 bool nodeShown( QgsLayerTreeNode *node ) const;
95
96 QgsLayerTreeModel *mLayerTreeModel = nullptr;
97 QString mFilterText;
98 bool mShowPrivateLayers = false;
99 bool mHideValidLayers = false;
100
101};
102
103
119class GUI_EXPORT QgsLayerTreeView : public QTreeView
120{
121
122#ifdef SIP_RUN
124 if ( sipCpp->inherits( "QgsLayerTreeView" ) )
125 sipType = sipType_QgsLayerTreeView;
126 else
127 sipType = 0;
128 SIP_END
129#endif
130
131
132 Q_OBJECT
133 public:
134
136 explicit QgsLayerTreeView( QWidget *parent SIP_TRANSFERTHIS = nullptr );
137 ~QgsLayerTreeView() override;
138
140 void setModel( QAbstractItemModel *model ) override;
141
143 QgsLayerTreeModel *layerTreeModel() const;
144
152 QgsLayerTreeProxyModel *proxyModel() const;
153
163 QgsLayerTreeNode *index2node( const QModelIndex &index ) const;
164
173 QModelIndex node2index( QgsLayerTreeNode *node ) const;
174
175
181 QModelIndex node2sourceIndex( QgsLayerTreeNode *node ) const;
182
183
192 QgsLayerTreeModelLegendNode *index2legendNode( const QModelIndex &index ) const;
193
203 QModelIndex legendNode2index( QgsLayerTreeModelLegendNode *legendNode );
204
211 QModelIndex legendNode2sourceIndex( QgsLayerTreeModelLegendNode *legendNode );
212
214 QgsLayerTreeViewDefaultActions *defaultActions();
215
217 void setMenuProvider( QgsLayerTreeViewMenuProvider *menuProvider SIP_TRANSFER );
219 QgsLayerTreeViewMenuProvider *menuProvider() const { return mMenuProvider; }
220
226 QgsMapLayer *currentLayer() const;
227
234 void setLayerVisible( QgsMapLayer *layer, bool visible );
235
243 void setCurrentLayer( QgsMapLayer *layer );
244
246 QgsLayerTreeNode *currentNode() const;
248 QgsLayerTreeGroup *currentGroupNode() const;
249
253 QgsLayerTreeModelLegendNode *currentLegendNode() const;
254
264 QList<QgsLayerTreeNode *> selectedNodes( bool skipInternal = false ) const;
265
273 QList<QgsLayerTreeLayer *> selectedLayerNodes() const;
274
282 QList<QgsMapLayer *> selectedLayers() const;
283
292 QList<QgsLayerTreeModelLegendNode *> selectedLegendNodes() const;
293
300 QList<QgsMapLayer *> selectedLayersRecursive() const;
301
312 void addIndicator( QgsLayerTreeNode *node, QgsLayerTreeViewIndicator *indicator );
313
320 void removeIndicator( QgsLayerTreeNode *node, QgsLayerTreeViewIndicator *indicator );
321
328 QList<QgsLayerTreeViewIndicator *> indicators( QgsLayerTreeNode *node ) const;
329
335 int layerMarkWidth() const { return mLayerMarkWidth; }
336
338
348 static QStringList viewOnlyCustomProperties() SIP_SKIP;
349
351
356 bool showPrivateLayers() const;
357
364 bool hideValidLayers() const;
365
366 public slots:
368 void refreshLayerSymbology( const QString &layerId );
369
373 void expandAllNodes();
374
378 void collapseAllNodes();
379
385 void setLayerMarkWidth( int width ) { mLayerMarkWidth = width; }
386
391 void setMessageBar( QgsMessageBar *messageBar );
392
397 void setShowPrivateLayers( bool showPrivate );
398
405 void setHideValidLayers( bool hideValid );
406
407 signals:
410
412 void datasetsDropped( QDropEvent *event );
413
421 void contextMenuAboutToShow( QMenu *menu );
422
423 protected:
424 void contextMenuEvent( QContextMenuEvent *event ) override;
425
426 void updateExpandedStateFromNode( QgsLayerTreeNode *node );
427
428 QgsMapLayer *layerForIndex( const QModelIndex &index ) const;
429
430 void mouseDoubleClickEvent( QMouseEvent *event ) override;
431 void mouseReleaseEvent( QMouseEvent *event ) override;
432 void keyPressEvent( QKeyEvent *event ) override;
433
434 void dragEnterEvent( QDragEnterEvent *event ) override;
435 void dragMoveEvent( QDragMoveEvent *event ) override;
436 void dropEvent( QDropEvent *event ) override;
437
438 void resizeEvent( QResizeEvent *event ) override;
439
440 protected slots:
441
442 void modelRowsInserted( const QModelIndex &index, int start, int end );
443 void modelRowsRemoved();
444
445 void updateExpandedStateToNode( const QModelIndex &index );
446
447 void onCurrentChanged();
448 void onExpandedChanged( QgsLayerTreeNode *node, bool expanded );
449 void onModelReset();
450
451 private slots:
452 void onCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
454 void onHorizontalScroll( int value );
455
456 void onDataChanged( const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles );
457
458 protected:
460 QgsLayerTreeViewDefaultActions *mDefaultActions = nullptr;
462 QgsLayerTreeViewMenuProvider *mMenuProvider = nullptr;
466 QHash< QgsLayerTreeNode *, QList<QgsLayerTreeViewIndicator *> > mIndicators;
469
472
473 private:
474 QgsLayerTreeProxyModel *mProxyModel = nullptr;
475
476 QgsMessageBar *mMessageBar = nullptr;
477
478 bool mShowPrivateLayers = false;
479 bool mHideValidLayers = false;
480
481 QTimer *mBlockDoubleClickTimer = nullptr;
482 // For model debugging
483 // void checkModel( );
484
485 // friend so it can access viewOptions() method and mLastReleaseMousePos without making them public
486 friend class QgsLayerTreeViewItemDelegate;
487};
488
489
498{
499 public:
500 virtual ~QgsLayerTreeViewMenuProvider() = default;
501
503 virtual QMenu *createContextMenu() = 0 SIP_FACTORY;
504};
505
506
507#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.
void contextMenuAboutToShow(QMenu *menu)
Emitted when the context menu is about to show.
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:75
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:191
#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:208