QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
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
249 QList<QgsLayerTreeNode *> selectedNodes( bool skipInternal = false ) const;
250
258 QList<QgsLayerTreeLayer *> selectedLayerNodes() const;
259
267 QList<QgsMapLayer *> selectedLayers() const;
268
277 QList<QgsLayerTreeModelLegendNode *> selectedLegendNodes() const;
278
285 QList<QgsMapLayer *> selectedLayersRecursive() const;
286
297 void addIndicator( QgsLayerTreeNode *node, QgsLayerTreeViewIndicator *indicator );
298
305 void removeIndicator( QgsLayerTreeNode *node, QgsLayerTreeViewIndicator *indicator );
306
313 QList<QgsLayerTreeViewIndicator *> indicators( QgsLayerTreeNode *node ) const;
314
320 int layerMarkWidth() const { return mLayerMarkWidth; }
321
323
333 static QStringList viewOnlyCustomProperties() SIP_SKIP;
334
336
337 public slots:
339 void refreshLayerSymbology( const QString &layerId );
340
345 void expandAllNodes();
346
351 void collapseAllNodes();
352
358 void setLayerMarkWidth( int width ) { mLayerMarkWidth = width; }
359
364 void setMessageBar( QgsMessageBar *messageBar );
365
370 void setShowPrivateLayers( bool showPrivate );
371
376 bool showPrivateLayers( );
377
378 signals:
381
383 void datasetsDropped( QDropEvent *event );
384
392 void contextMenuAboutToShow( QMenu *menu );
393
394 protected:
395 void contextMenuEvent( QContextMenuEvent *event ) override;
396
397 void updateExpandedStateFromNode( QgsLayerTreeNode *node );
398
399 QgsMapLayer *layerForIndex( const QModelIndex &index ) const;
400
401 void mouseReleaseEvent( QMouseEvent *event ) override;
402 void keyPressEvent( QKeyEvent *event ) override;
403
404 void dragEnterEvent( QDragEnterEvent *event ) override;
405 void dragMoveEvent( QDragMoveEvent *event ) override;
406 void dropEvent( QDropEvent *event ) override;
407
408 void resizeEvent( QResizeEvent *event ) override;
409
410 protected slots:
411
412 void modelRowsInserted( const QModelIndex &index, int start, int end );
413 void modelRowsRemoved();
414
415 void updateExpandedStateToNode( const QModelIndex &index );
416
417 void onCurrentChanged();
418 void onExpandedChanged( QgsLayerTreeNode *node, bool expanded );
419 void onModelReset();
420
421 private slots:
422 void onCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
424 void onHorizontalScroll( int value );
425
426 void onDataChanged( const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles );
427
428 protected:
430 QgsLayerTreeViewDefaultActions *mDefaultActions = nullptr;
432 QgsLayerTreeViewMenuProvider *mMenuProvider = nullptr;
436 QHash< QgsLayerTreeNode *, QList<QgsLayerTreeViewIndicator *> > mIndicators;
439
442
443 private:
444 QgsLayerTreeProxyModel *mProxyModel = nullptr;
445
446 QgsMessageBar *mMessageBar = nullptr;
447
448 bool mShowPrivateLayers = false;
449
450 // For model debugging
451 // void checkModel( );
452
453 // friend so it can access viewOptions() method and mLastReleaseMousePos without making them public
454 friend class QgsLayerTreeViewItemDelegate;
455};
456
457
467{
468 public:
469 virtual ~QgsLayerTreeViewMenuProvider() = default;
470
472 virtual QMenu *createContextMenu() = 0 SIP_FACTORY;
473};
474
475
476#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:74
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