Branch data Line data Source code
1 : : /*************************************************************************** 2 : : qgslayertreeregistrybridge.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 QGSLAYERTREEREGISTRYBRIDGE_H 17 : : #define QGSLAYERTREEREGISTRYBRIDGE_H 18 : : 19 : : #include <QObject> 20 : : #include <QStringList> 21 : : 22 : : #include "qgis_core.h" 23 : : #include "qgis_sip.h" 24 : : 25 : : class QgsLayerTreeGroup; 26 : : class QgsLayerTreeNode; 27 : : class QgsMapLayer; 28 : : class QgsProject; 29 : : 30 : : 31 : : /** 32 : : * \ingroup core 33 : : * \brief Listens to the updates in map layer registry and does changes in layer tree. 34 : : * 35 : : * When connected to a layer tree, any layers added to the map layer registry 36 : : * will be also added to the layer tree. Similarly, map layers that are removed 37 : : * from registry will be removed from the layer tree. 38 : : * 39 : : * If a layer is completely removed from the layer tree, it will be also removed 40 : : * from the map layer registry. 41 : : * 42 : : * \since QGIS 2.4 43 : : */ 44 : : class CORE_EXPORT QgsLayerTreeRegistryBridge : public QObject 45 : : { 46 : : Q_OBJECT 47 : : public: 48 : : 49 : : /** 50 : : * A structure to define the insertion point to the layer tree. 51 : : * This represents the current layer tree group and index where newly added map layers should be inserted into. 52 : : * \since QGIS 3.10 53 : : */ 54 : : struct InsertionPoint 55 : : { 56 : : //! Constructs an insertion point as layer tree group with its corresponding position. 57 : 5 : InsertionPoint( QgsLayerTreeGroup *group, int position ) 58 : 5 : : group( group ), position( position ) {} 59 : : 60 : : QgsLayerTreeGroup *group = nullptr; 61 : : int position = 0; 62 : : }; 63 : : 64 : : //! Create the instance that synchronizes given project with a layer tree root 65 : : explicit QgsLayerTreeRegistryBridge( QgsLayerTreeGroup *root, QgsProject *project, QObject *parent SIP_TRANSFERTHIS = nullptr ); 66 : : 67 : 0 : void setEnabled( bool enabled ) { mEnabled = enabled; } 68 : : bool isEnabled() const { return mEnabled; } 69 : : 70 : : void setNewLayersVisible( bool enabled ) { mNewLayersVisible = enabled; } 71 : : bool newLayersVisible() const { return mNewLayersVisible; } 72 : : 73 : : /** 74 : : * Set where the new layers should be inserted - can be used to follow current selection. 75 : : * By default it is root group with zero index. 76 : : * \deprecated since QGIS 3.10 use setLayerInsertionPoint( const InsertionPoint &insertionPoint ) instead 77 : : */ 78 : : Q_DECL_DEPRECATED void setLayerInsertionPoint( QgsLayerTreeGroup *parentGroup, int index ) SIP_DEPRECATED; 79 : : 80 : : /** 81 : : * Set where the new layers should be inserted - can be used to follow current selection. 82 : : * By default it is root group with zero index. 83 : : * \since QGIS 3.10 84 : : */ 85 : : void setLayerInsertionPoint( const InsertionPoint &insertionPoint ); 86 : : 87 : : signals: 88 : : 89 : : /** 90 : : * Tell others we have just added layers to the tree (used in QGIS to auto-select first newly added layer) 91 : : * \since QGIS 2.6 92 : : */ 93 : : void addedLayersToLayerTree( const QList<QgsMapLayer *> &layers ); 94 : : 95 : : protected slots: 96 : : void layersAdded( const QList<QgsMapLayer *> &layers ); 97 : : void layersWillBeRemoved( const QStringList &layerIds ); 98 : : 99 : : void groupWillRemoveChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo ); 100 : : void groupRemovedChildren(); 101 : : 102 : : void removeLayersFromRegistry( const QStringList &layerIds ); 103 : : 104 : : protected: 105 : : QgsLayerTreeGroup *mRoot = nullptr; 106 : : QgsProject *mProject = nullptr; 107 : : QStringList mLayerIdsForRemoval; 108 : : bool mRegistryRemovingLayers; 109 : : bool mEnabled; 110 : : bool mNewLayersVisible; 111 : : 112 : : InsertionPoint mInsertionPoint; 113 : : }; 114 : : 115 : : #endif // QGSLAYERTREEREGISTRYBRIDGE_H