QGIS API Documentation 3.36.0-Maidenhead (09951dc0acf)
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
102class GUI_EXPORT QgsLayerTreeView : public QTreeView
103{
104
105#ifdef SIP_RUN
107 if ( sipCpp->inherits( "QgsLayerTreeView" ) )
108 sipType = sipType_QgsLayerTreeView;
109 else
110 sipType = 0;
111 SIP_END
112#endif
113
114
115 Q_OBJECT
116 public:
117
119 explicit QgsLayerTreeView( QWidget *parent SIP_TRANSFERTHIS = nullptr );
120 ~QgsLayerTreeView() override;
121
123 void setModel( QAbstractItemModel *model ) override;
124
126 QgsLayerTreeModel *layerTreeModel() const;
127
135 QgsLayerTreeProxyModel *proxyModel() const;
136
146 QgsLayerTreeNode *index2node( const QModelIndex &index ) const;
147
156 QModelIndex node2index( QgsLayerTreeNode *node ) const;
157
158
164 QModelIndex node2sourceIndex( QgsLayerTreeNode *node ) const;
165
166
175 QgsLayerTreeModelLegendNode *index2legendNode( const QModelIndex &index ) const;
176
186 QModelIndex legendNode2index( QgsLayerTreeModelLegendNode *legendNode );
187
194 QModelIndex legendNode2sourceIndex( QgsLayerTreeModelLegendNode *legendNode );
195
197 QgsLayerTreeViewDefaultActions *defaultActions();
198
200 void setMenuProvider( QgsLayerTreeViewMenuProvider *menuProvider SIP_TRANSFER );
202 QgsLayerTreeViewMenuProvider *menuProvider() const { return mMenuProvider; }
203
209 QgsMapLayer *currentLayer() const;
210
217 void setLayerVisible( QgsMapLayer *layer, bool visible );
218
226 void setCurrentLayer( QgsMapLayer *layer );
227
229 QgsLayerTreeNode *currentNode() const;
231 QgsLayerTreeGroup *currentGroupNode() const;
232
236 QgsLayerTreeModelLegendNode *currentLegendNode() const;
237
247 QList<QgsLayerTreeNode *> selectedNodes( bool skipInternal = false ) const;
248
256 QList<QgsLayerTreeLayer *> selectedLayerNodes() const;
257
265 QList<QgsMapLayer *> selectedLayers() const;
266
275 QList<QgsLayerTreeModelLegendNode *> selectedLegendNodes() const;
276
283 QList<QgsMapLayer *> selectedLayersRecursive() const;
284
295 void addIndicator( QgsLayerTreeNode *node, QgsLayerTreeViewIndicator *indicator );
296
303 void removeIndicator( QgsLayerTreeNode *node, QgsLayerTreeViewIndicator *indicator );
304
311 QList<QgsLayerTreeViewIndicator *> indicators( QgsLayerTreeNode *node ) const;
312
318 int layerMarkWidth() const { return mLayerMarkWidth; }
319
321
331 static QStringList viewOnlyCustomProperties() SIP_SKIP;
332
334
335 public slots:
337 void refreshLayerSymbology( const QString &layerId );
338
342 void expandAllNodes();
343
347 void collapseAllNodes();
348
354 void setLayerMarkWidth( int width ) { mLayerMarkWidth = width; }
355
360 void setMessageBar( QgsMessageBar *messageBar );
361
366 void setShowPrivateLayers( bool showPrivate );
367
372 bool showPrivateLayers( );
373
374 signals:
377
379 void datasetsDropped( QDropEvent *event );
380
388 void contextMenuAboutToShow( QMenu *menu );
389
390 protected:
391 void contextMenuEvent( QContextMenuEvent *event ) override;
392
393 void updateExpandedStateFromNode( QgsLayerTreeNode *node );
394
395 QgsMapLayer *layerForIndex( const QModelIndex &index ) const;
396
397 void mouseDoubleClickEvent( QMouseEvent *event ) override;
398 void mouseReleaseEvent( QMouseEvent *event ) override;
399 void keyPressEvent( QKeyEvent *event ) override;
400
401 void dragEnterEvent( QDragEnterEvent *event ) override;
402 void dragMoveEvent( QDragMoveEvent *event ) override;
403 void dropEvent( QDropEvent *event ) override;
404
405 void resizeEvent( QResizeEvent *event ) override;
406
407 protected slots:
408
409 void modelRowsInserted( const QModelIndex &index, int start, int end );
410 void modelRowsRemoved();
411
412 void updateExpandedStateToNode( const QModelIndex &index );
413
414 void onCurrentChanged();
415 void onExpandedChanged( QgsLayerTreeNode *node, bool expanded );
416 void onModelReset();
417
418 private slots:
419 void onCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
421 void onHorizontalScroll( int value );
422
423 void onDataChanged( const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles );
424
425 protected:
427 QgsLayerTreeViewDefaultActions *mDefaultActions = nullptr;
429 QgsLayerTreeViewMenuProvider *mMenuProvider = nullptr;
433 QHash< QgsLayerTreeNode *, QList<QgsLayerTreeViewIndicator *> > mIndicators;
436
439
440 private:
441 QgsLayerTreeProxyModel *mProxyModel = nullptr;
442
443 QgsMessageBar *mMessageBar = nullptr;
444
445 bool mShowPrivateLayers = false;
446
447 QTimer *mBlockDoubleClickTimer = nullptr;
448 // For model debugging
449 // void checkModel( );
450
451 // friend so it can access viewOptions() method and mLastReleaseMousePos without making them public
452 friend class QgsLayerTreeViewItemDelegate;
453};
454
455
464{
465 public:
466 virtual ~QgsLayerTreeViewMenuProvider() = default;
467
469 virtual QMenu *createContextMenu() = 0 SIP_FACTORY;
470};
471
472
473#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.
#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