QGIS API Documentation 3.28.14-Firenze (exported)
Loading...
Searching...
No Matches
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;
307
308 public slots:
310 void refreshLayerSymbology( const QString &layerId );
311
316 void expandAllNodes();
317
322 void collapseAllNodes();
323
329 void setLayerMarkWidth( int width ) { mLayerMarkWidth = width; }
330
335 void setMessageBar( QgsMessageBar *messageBar );
336
341 void setShowPrivateLayers( bool showPrivate );
342
347 bool showPrivateLayers( );
348
349 signals:
352
360 void contextMenuAboutToShow( QMenu *menu );
361
362 protected:
363 void contextMenuEvent( QContextMenuEvent *event ) override;
364
365 void updateExpandedStateFromNode( QgsLayerTreeNode *node );
366
367 QgsMapLayer *layerForIndex( const QModelIndex &index ) const;
368
369 void mouseReleaseEvent( QMouseEvent *event ) override;
370 void keyPressEvent( QKeyEvent *event ) override;
371
372 void dropEvent( QDropEvent *event ) override;
373
374 void resizeEvent( QResizeEvent *event ) override;
375
376 protected slots:
377
378 void modelRowsInserted( const QModelIndex &index, int start, int end );
379 void modelRowsRemoved();
380
381 void updateExpandedStateToNode( const QModelIndex &index );
382
383 void onCurrentChanged();
384 void onExpandedChanged( QgsLayerTreeNode *node, bool expanded );
385 void onModelReset();
386
387 private slots:
388 void onCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
390 void onHorizontalScroll( int value );
391
392 void onDataChanged( const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles );
393
394 protected:
396 QgsLayerTreeViewDefaultActions *mDefaultActions = nullptr;
398 QgsLayerTreeViewMenuProvider *mMenuProvider = nullptr;
402 QHash< QgsLayerTreeNode *, QList<QgsLayerTreeViewIndicator *> > mIndicators;
405
408
409 private:
410 QgsLayerTreeProxyModel *mProxyModel = nullptr;
411
412 QgsMessageBar *mMessageBar = nullptr;
413
414 bool mShowPrivateLayers = false;
415
416 // For model debugging
417 // void checkModel( );
418
419 // friend so it can access viewOptions() method and mLastReleaseMousePos without making them public
420 friend class QgsLayerTreeViewItemDelegate;
421};
422
423
433{
434 public:
435 virtual ~QgsLayerTreeViewMenuProvider() = default;
436
438 virtual QMenu *createContextMenu() = 0 SIP_FACTORY;
439};
440
441
442#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)
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.
#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