QGIS API Documentation 3.41.0-Master (af5edcb665c)
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:
54 QgsLayerTreeProxyModel( QgsLayerTreeModel *treeModel, QObject *parent );
55
59 void setFilterText( const QString &filterText = QString() );
60
64 bool showPrivateLayers() const;
65
69 void setShowPrivateLayers( bool showPrivate );
70
77 bool hideValidLayers() const;
78
85 void setHideValidLayers( bool hideValid );
86
87 protected:
88 bool filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const override;
89
90 private:
91 bool nodeShown( QgsLayerTreeNode *node ) const;
92
93 QgsLayerTreeModel *mLayerTreeModel = nullptr;
94 QString mFilterText;
95 bool mShowPrivateLayers = false;
96 bool mHideValidLayers = false;
97};
98
99
115class GUI_EXPORT QgsLayerTreeView : public QTreeView
116{
117#ifdef SIP_RUN
119 if ( sipCpp->inherits( "QgsLayerTreeView" ) )
120 sipType = sipType_QgsLayerTreeView;
121 else
122 sipType = 0;
123 SIP_END
124#endif
125
126
127 Q_OBJECT
128 public:
130 explicit QgsLayerTreeView( QWidget *parent SIP_TRANSFERTHIS = nullptr );
131 ~QgsLayerTreeView() override;
132
134 void setModel( QAbstractItemModel *model ) override;
135
137 QgsLayerTreeModel *layerTreeModel() const;
138
146 QgsLayerTreeProxyModel *proxyModel() const;
147
157 QgsLayerTreeNode *index2node( const QModelIndex &index ) const;
158
167 QModelIndex node2index( QgsLayerTreeNode *node ) const;
168
169
175 QModelIndex node2sourceIndex( QgsLayerTreeNode *node ) const;
176
177
186 QgsLayerTreeModelLegendNode *index2legendNode( const QModelIndex &index ) const;
187
197 QModelIndex legendNode2index( QgsLayerTreeModelLegendNode *legendNode );
198
205 QModelIndex legendNode2sourceIndex( QgsLayerTreeModelLegendNode *legendNode );
206
208 QgsLayerTreeViewDefaultActions *defaultActions();
209
211 void setMenuProvider( QgsLayerTreeViewMenuProvider *menuProvider SIP_TRANSFER );
213 QgsLayerTreeViewMenuProvider *menuProvider() const { return mMenuProvider; }
214
220 QgsMapLayer *currentLayer() const;
221
228 void setLayerVisible( QgsMapLayer *layer, bool visible );
229
238 void setCurrentNode( QgsLayerTreeNode *node );
239
247 void setCurrentLayer( QgsMapLayer *layer );
248
250 QgsLayerTreeNode *currentNode() const;
252 QgsLayerTreeGroup *currentGroupNode() const;
253
257 QgsLayerTreeModelLegendNode *currentLegendNode() const;
258
268 QList<QgsLayerTreeNode *> selectedNodes( bool skipInternal = false ) const;
269
277 QList<QgsLayerTreeLayer *> selectedLayerNodes() const;
278
286 QList<QgsMapLayer *> selectedLayers() const;
287
296 QList<QgsLayerTreeModelLegendNode *> selectedLegendNodes() const;
297
304 QList<QgsMapLayer *> selectedLayersRecursive() const;
305
316 void addIndicator( QgsLayerTreeNode *node, QgsLayerTreeViewIndicator *indicator );
317
324 void removeIndicator( QgsLayerTreeNode *node, QgsLayerTreeViewIndicator *indicator );
325
332 QList<QgsLayerTreeViewIndicator *> indicators( QgsLayerTreeNode *node ) const;
333
339 int layerMarkWidth() const { return mLayerMarkWidth; }
340
342
352 static QStringList viewOnlyCustomProperties() SIP_SKIP;
353
355
360 bool showPrivateLayers() const;
361
368 bool hideValidLayers() const;
369
370 public slots:
372 void refreshLayerSymbology( const QString &layerId );
373
377 void expandAllNodes();
378
382 void collapseAllNodes();
383
389 void setLayerMarkWidth( int width ) { mLayerMarkWidth = width; }
390
395 void setMessageBar( QgsMessageBar *messageBar );
396
401 void setShowPrivateLayers( bool showPrivate );
402
409 void setHideValidLayers( bool hideValid );
410
411 signals:
414
416 void datasetsDropped( QDropEvent *event );
417
425 void contextMenuAboutToShow( QMenu *menu );
426
427 protected:
428 void contextMenuEvent( QContextMenuEvent *event ) override;
429
430 void updateExpandedStateFromNode( QgsLayerTreeNode *node );
431
432 QgsMapLayer *layerForIndex( const QModelIndex &index ) const;
433
434 void mouseDoubleClickEvent( QMouseEvent *event ) override;
435 void mouseReleaseEvent( QMouseEvent *event ) override;
436 void keyPressEvent( QKeyEvent *event ) override;
437
438 void dragEnterEvent( QDragEnterEvent *event ) override;
439 void dragMoveEvent( QDragMoveEvent *event ) override;
440 void dropEvent( QDropEvent *event ) override;
441
442 void resizeEvent( QResizeEvent *event ) override;
443
444 protected slots:
445
446 void modelRowsInserted( const QModelIndex &index, int start, int end );
447 void modelRowsRemoved();
448
449 void updateExpandedStateToNode( const QModelIndex &index );
450
451 void onCurrentChanged();
452 void onExpandedChanged( QgsLayerTreeNode *node, bool expanded );
453 void onModelReset();
454
455 private slots:
456 void onCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
458 void onHorizontalScroll( int value );
459
460 void onDataChanged( const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles );
461
462 protected:
464 QgsLayerTreeViewDefaultActions *mDefaultActions = nullptr;
466 QgsLayerTreeViewMenuProvider *mMenuProvider = nullptr;
470 QHash<QgsLayerTreeNode *, QList<QgsLayerTreeViewIndicator *>> mIndicators;
473
476
477 private:
478 QgsLayerTreeProxyModel *mProxyModel = nullptr;
479
480 QgsMessageBar *mMessageBar = nullptr;
481
482 bool mShowPrivateLayers = false;
483 bool mHideValidLayers = false;
484
485 QTimer *mBlockDoubleClickTimer = nullptr;
486 // For model debugging
487 // void checkModel( );
488
489 // friend so it can access viewOptions() method and mLastReleaseMousePos without making them public
490 friend class QgsLayerTreeViewItemDelegate;
491};
492
493
502{
503 public:
504 virtual ~QgsLayerTreeViewMenuProvider() = default;
505
507 virtual QMenu *createContextMenu() = 0 SIP_FACTORY;
508};
509
510
511#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:76
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