Branch data Line data Source code
1 : : /*************************************************************************** 2 : : qgslayertreegroup.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 QGSLAYERTREEGROUP_H 17 : : #define QGSLAYERTREEGROUP_H 18 : : 19 : : #include "qgis_core.h" 20 : : #include "qgis_sip.h" 21 : : #include "qgslayertreenode.h" 22 : : 23 : : class QgsMapLayer; 24 : : class QgsLayerTreeLayer; 25 : : 26 : : /** 27 : : * \ingroup core 28 : : * \brief Layer tree group node serves as a container for layers and further groups. 29 : : * 30 : : * Group names do not need to be unique within one tree nor within one parent. 31 : : * 32 : : * \since QGIS 2.4 33 : : */ 34 : 0 : class CORE_EXPORT QgsLayerTreeGroup : public QgsLayerTreeNode 35 : : { 36 : : Q_OBJECT 37 : : public: 38 : : 39 : : /** 40 : : * Constructor 41 : : */ 42 : : QgsLayerTreeGroup( const QString &name = QString(), bool checked = true ); 43 : : 44 : : #ifndef SIP_RUN 45 : : QgsLayerTreeGroup( const QgsLayerTreeGroup &other ); 46 : : #endif 47 : : 48 : : /** 49 : : * Returns the group's name. 50 : : */ 51 : : QString name() const override; 52 : : 53 : : /** 54 : : * Sets the group's name. 55 : : */ 56 : : void setName( const QString &n ) override; 57 : : 58 : : /** 59 : : * Insert a new group node with given name at specified position. The newly created node is owned by this group. 60 : : */ 61 : : QgsLayerTreeGroup *insertGroup( int index, const QString &name ); 62 : : 63 : : /** 64 : : * Append a new group node with given name. Newly created node is owned by this group. 65 : : */ 66 : : QgsLayerTreeGroup *addGroup( const QString &name ); 67 : : 68 : : /** 69 : : * Insert a new layer node for given map layer at specified position. The newly created node is owned by this group. 70 : : */ 71 : : QgsLayerTreeLayer *insertLayer( int index, QgsMapLayer *layer ); 72 : : 73 : : /** 74 : : * Append a new layer node for given map layer. The newly created node is owned by this group. 75 : : */ 76 : : QgsLayerTreeLayer *addLayer( QgsMapLayer *layer ); 77 : : 78 : : /** 79 : : * Insert existing nodes at specified position. The nodes must not have a parent yet. The nodes will be owned by this group. 80 : : */ 81 : : void insertChildNodes( int index, const QList<QgsLayerTreeNode *> &nodes SIP_TRANSFER ); 82 : : 83 : : /** 84 : : * Insert existing node at specified position. The node must not have a parent yet. The node will be owned by this group. 85 : : */ 86 : : void insertChildNode( int index, QgsLayerTreeNode *node SIP_TRANSFER ); 87 : : 88 : : /** 89 : : * Append an existing node. The node must not have a parent yet. The node will be owned by this group. 90 : : */ 91 : : void addChildNode( QgsLayerTreeNode *node SIP_TRANSFER ); 92 : : 93 : : /** 94 : : * Remove a child node from this group. The node will be deleted. 95 : : */ 96 : : void removeChildNode( QgsLayerTreeNode *node ); 97 : : 98 : : /** 99 : : * Remove map layer's node from this group. The node will be deleted. 100 : : */ 101 : : void removeLayer( QgsMapLayer *layer ); 102 : : 103 : : /** 104 : : * Remove child nodes from index "from". The nodes will be deleted. 105 : : */ 106 : : void removeChildren( int from, int count ); 107 : : 108 : : /** 109 : : * Remove all child group nodes without layers. The groupnodes will be deleted. 110 : : */ 111 : : void removeChildrenGroupWithoutLayers(); 112 : : 113 : : /** 114 : : * Remove all child nodes. The nodes will be deleted. 115 : : */ 116 : : void removeAllChildren(); 117 : : 118 : : /** 119 : : * Find layer node representing the map layer. Searches recursively the whole sub-tree. 120 : : * \since QGIS 3.0 121 : : */ 122 : : QgsLayerTreeLayer *findLayer( QgsMapLayer *layer ) const; 123 : : 124 : : /** 125 : : * Find layer node representing the map layer specified by its ID. Searches recursively the whole sub-tree. 126 : : */ 127 : : QgsLayerTreeLayer *findLayer( const QString &layerId ) const; 128 : : 129 : : /** 130 : : * Find all layer nodes. Searches recursively the whole sub-tree. 131 : : */ 132 : : QList<QgsLayerTreeLayer *> findLayers() const; 133 : : 134 : : /** 135 : : * Find layer IDs used in all layer nodes. Searches recursively the whole sub-tree. 136 : : */ 137 : : QStringList findLayerIds() const; 138 : : 139 : : /** 140 : : * Find group node with specified name. Searches recursively the whole sub-tree. 141 : : */ 142 : : QgsLayerTreeGroup *findGroup( const QString &name ); 143 : : 144 : : /** 145 : : * Find group layer nodes. Searches recursively the whole sub-tree, if recursive is set. 146 : : */ 147 : : QList<QgsLayerTreeGroup *> findGroups( bool recursive = false ) const; 148 : : 149 : : /** 150 : : * Read group (tree) from XML element <layer-tree-group> and return the newly created group (or NULLPTR on error). 151 : : * Does not resolve textual references to layers. Call resolveReferences() afterwards to do it. 152 : : */ 153 : : static QgsLayerTreeGroup *readXml( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY; 154 : : 155 : : /** 156 : : * Read group (tree) from XML element <layer-tree-group> and return the newly created group (or NULLPTR on error). 157 : : * Also resolves textual references to layers from the project (calls resolveReferences() internally). 158 : : * \since QGIS 3.0 159 : : */ 160 : : static QgsLayerTreeGroup *readXml( QDomElement &element, const QgsProject *project, const QgsReadWriteContext &context ) SIP_FACTORY; 161 : : 162 : : /** 163 : : * Write group (tree) as XML element <layer-tree-group> and add it to the given parent element 164 : : */ 165 : : void writeXml( QDomElement &parentElement, const QgsReadWriteContext &context ) override; 166 : : 167 : : /** 168 : : * Read children from XML and append them to the group. 169 : : * Does not resolve textual references to layers. Call resolveReferences() afterwards to do it. 170 : : */ 171 : : void readChildrenFromXml( QDomElement &element, const QgsReadWriteContext &context ); 172 : : 173 : : /** 174 : : * Returns text representation of the tree. For debugging purposes only. 175 : : */ 176 : : QString dump() const override; 177 : : 178 : : /** 179 : : * Returns a clone of the group. The children are cloned too. 180 : : */ 181 : : QgsLayerTreeGroup *clone() const override SIP_FACTORY; 182 : : 183 : : /** 184 : : * Calls resolveReferences() on child tree nodes 185 : : * \since QGIS 3.0 186 : : */ 187 : : void resolveReferences( const QgsProject *project, bool looseMatching = false ) override; 188 : : 189 : : /** 190 : : * Check or uncheck a node and all its children (taking into account exclusion rules) 191 : : */ 192 : : void setItemVisibilityCheckedRecursive( bool checked ) override; 193 : : 194 : : /** 195 : : * Returns whether the group is mutually exclusive (only one child can be checked at a time) 196 : : * \since QGIS 2.12 197 : : */ 198 : : bool isMutuallyExclusive() const; 199 : : 200 : : /** 201 : : * Set whether the group is mutually exclusive (only one child can be checked at a time). 202 : : * The initial child index determines which child should be initially checked. The default value 203 : : * of -1 will determine automatically (either first one currently checked or none) 204 : : * \since QGIS 2.12 205 : : */ 206 : : void setIsMutuallyExclusive( bool enabled, int initialChildIndex = -1 ); 207 : : 208 : : protected slots: 209 : : 210 : : void nodeVisibilityChanged( QgsLayerTreeNode *node ); 211 : : 212 : : protected: 213 : : 214 : : /** 215 : : * Set check state of children - if mutually exclusive 216 : : */ 217 : : void updateChildVisibilityMutuallyExclusive(); 218 : : 219 : : QString mName; 220 : : 221 : : bool mChangingChildVisibility = false; 222 : : 223 : : //! Whether the group is mutually exclusive (i.e. only one child can be checked at a time) 224 : : bool mMutuallyExclusive = false; 225 : : 226 : : /** 227 : : * Keeps track which child has been most recently selected 228 : : * (so if the whole group is unchecked and checked again, we know which child to check) 229 : : */ 230 : : int mMutuallyExclusiveChildIndex = -1; 231 : : 232 : : //! Sets parent to NULLPTR and disconnects all external and forwarded signals 233 : : virtual void makeOrphan() override SIP_SKIP; 234 : : 235 : : private: 236 : : 237 : : #ifdef SIP_RUN 238 : : 239 : : /** 240 : : * Copies are not allowed 241 : : */ 242 : : QgsLayerTreeGroup( const QgsLayerTreeGroup &other ); 243 : : #endif 244 : : 245 : : QgsLayerTreeGroup &operator= ( const QgsLayerTreeGroup & ) = delete; 246 : : 247 : : 248 : : }; 249 : : 250 : : 251 : : #endif // QGSLAYERTREEGROUP_H