QGIS API Documentation 3.41.0-Master (3440c17df1d)
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
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
244 void setCurrentNode( QgsLayerTreeNode *node );
245
253 void setCurrentLayer( QgsMapLayer *layer );
254
256 QgsLayerTreeNode *currentNode() const;
258 QgsLayerTreeGroup *currentGroupNode() const;
259
263 QgsLayerTreeModelLegendNode *currentLegendNode() const;
264
274 QList<QgsLayerTreeNode *> selectedNodes( bool skipInternal = false ) const;
275
283 QList<QgsLayerTreeLayer *> selectedLayerNodes() const;
284
292 QList<QgsMapLayer *> selectedLayers() const;
293
302 QList<QgsLayerTreeModelLegendNode *> selectedLegendNodes() const;
303
310 QList<QgsMapLayer *> selectedLayersRecursive() const;
311
322 void addIndicator( QgsLayerTreeNode *node, QgsLayerTreeViewIndicator *indicator );
323
330 void removeIndicator( QgsLayerTreeNode *node, QgsLayerTreeViewIndicator *indicator );
331
338 QList<QgsLayerTreeViewIndicator *> indicators( QgsLayerTreeNode *node ) const;
339
345 int layerMarkWidth() const { return mLayerMarkWidth; }
346
348
358 static QStringList viewOnlyCustomProperties() SIP_SKIP;
359
361
366 bool showPrivateLayers() const;
367
374 bool hideValidLayers() const;
375
376 public slots:
378 void refreshLayerSymbology( const QString &layerId );
379
383 void expandAllNodes();
384
388 void collapseAllNodes();
389
395 void setLayerMarkWidth( int width ) { mLayerMarkWidth = width; }
396
401 void setMessageBar( QgsMessageBar *messageBar );
402
407 void setShowPrivateLayers( bool showPrivate );
408
415 void setHideValidLayers( bool hideValid );
416
417 signals:
420
422 void datasetsDropped( QDropEvent *event );
423
431 void contextMenuAboutToShow( QMenu *menu );
432
433 protected:
434 void contextMenuEvent( QContextMenuEvent *event ) override;
435
436 void updateExpandedStateFromNode( QgsLayerTreeNode *node );
437
438 QgsMapLayer *layerForIndex( const QModelIndex &index ) const;
439
440 void mouseDoubleClickEvent( QMouseEvent *event ) override;
441 void mouseReleaseEvent( QMouseEvent *event ) override;
442 void keyPressEvent( QKeyEvent *event ) override;
443
444 void dragEnterEvent( QDragEnterEvent *event ) override;
445 void dragMoveEvent( QDragMoveEvent *event ) override;
446 void dropEvent( QDropEvent *event ) override;
447
448 void resizeEvent( QResizeEvent *event ) override;
449
450 protected slots:
451
452 void modelRowsInserted( const QModelIndex &index, int start, int end );
453 void modelRowsRemoved();
454
455 void updateExpandedStateToNode( const QModelIndex &index );
456
457 void onCurrentChanged();
458 void onExpandedChanged( QgsLayerTreeNode *node, bool expanded );
459 void onModelReset();
460
461 private slots:
462 void onCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key );
464 void onHorizontalScroll( int value );
465
466 void onDataChanged( const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles );
467
468 protected:
470 QgsLayerTreeViewDefaultActions *mDefaultActions = nullptr;
472 QgsLayerTreeViewMenuProvider *mMenuProvider = nullptr;
476 QHash< QgsLayerTreeNode *, QList<QgsLayerTreeViewIndicator *> > mIndicators;
479
482
483 private:
484 QgsLayerTreeProxyModel *mProxyModel = nullptr;
485
486 QgsMessageBar *mMessageBar = nullptr;
487
488 bool mShowPrivateLayers = false;
489 bool mHideValidLayers = false;
490
491 QTimer *mBlockDoubleClickTimer = nullptr;
492 // For model debugging
493 // void checkModel( );
494
495 // friend so it can access viewOptions() method and mLastReleaseMousePos without making them public
496 friend class QgsLayerTreeViewItemDelegate;
497};
498
499
508{
509 public:
510 virtual ~QgsLayerTreeViewMenuProvider() = default;
511
513 virtual QMenu *createContextMenu() = 0 SIP_FACTORY;
514};
515
516
517#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