|
| 1 | +/** |
| 2 | + * This class is a base class for nodes in a layer tree. |
| 3 | + * Layer tree is a hierarchical structure consisting of group and layer nodes: |
| 4 | + * - group nodes are containers and may contain children (layer and group nodes) |
| 5 | + * - layer nodes point to map layers, they do not contain further children |
| 6 | + * |
| 7 | + * Layer trees may be used for organization of layers, typically a layer tree |
| 8 | + * is exposed to the user using QgsLayerTreeView widget which shows the tree |
| 9 | + * and allows manipulation with the tree. |
| 10 | + * |
| 11 | + * Ownership of nodes: every node is owned by its parent. Therefore once node |
| 12 | + * is added to a layer tree, it is the responsibility of the parent to delete it |
| 13 | + * when the node is not needed anymore. Deletion of root node of a tree will |
| 14 | + * delete all nodes of the tree. |
| 15 | + * |
| 16 | + * Signals: signals are propagated from children to parent. That means it is |
| 17 | + * sufficient to connect to root node in order to get signals about updates |
| 18 | + * in the whole layer tree. When adding or removing a node that contains further |
| 19 | + * children (i.e. a whole subtree), the addition/removal signals are emitted |
| 20 | + * only for the root node of the subtree that is being added or removed. |
| 21 | + * |
| 22 | + * Custom properties: Every node may have some custom properties assigned to it. |
| 23 | + * This mechanism allows third parties store additional data with the nodes. |
| 24 | + * The properties are used within QGIS code (whether to show layer in overview, |
| 25 | + * whether the node is embedded from another project etc), but may be also |
| 26 | + * used by third party plugins. Custom properties are stored also in the project |
| 27 | + * file. The storage is not efficient for large amount of data. |
| 28 | + * |
| 29 | + * Custom properties that have already been used within QGIS: |
| 30 | + * - "overview" - whether to show a layer in overview |
| 31 | + * - "showFeatureCount" - whether to show feature counts in layer tree (vector only) |
| 32 | + * - "embedded" - whether the node comes from an external project |
| 33 | + * - "embedded_project" - path to the external project (embedded root node only) |
| 34 | + * |
| 35 | + * @see also QgsLayerTree, QgsLayerTreeLayer, QgsLayerTreeGroup |
| 36 | + * @note added in 2.4 |
| 37 | + */ |
| 38 | +class QgsLayerTreeNode : QObject |
| 39 | +{ |
| 40 | +%TypeHeaderCode |
| 41 | +#include <qgslayertree.h> |
| 42 | +%End |
| 43 | + |
| 44 | +%ConvertToSubClassCode |
| 45 | + if (sipCpp->inherits("QgsLayerTreeNode")) |
| 46 | + { |
| 47 | + sipType = sipType_QgsLayerTreeNode; |
| 48 | + QgsLayerTreeNode* node = qobject_cast<QgsLayerTreeNode*>(sipCpp); |
| 49 | + if (QgsLayerTree::isLayer(node)) |
| 50 | + sipType = sipType_QgsLayerTreeLayer; |
| 51 | + else if (QgsLayerTree::isGroup(node)) |
| 52 | + sipType = sipType_QgsLayerTreeGroup; |
| 53 | + } |
| 54 | + else |
| 55 | + sipType = 0; |
| 56 | +%End |
| 57 | + |
| 58 | + public: |
| 59 | + |
| 60 | + //! Enumeration of possible tree node types |
| 61 | + enum NodeType |
| 62 | + { |
| 63 | + NodeGroup, //!< container of other groups and layers |
| 64 | + NodeLayer //!< leaf node pointing to a layer |
| 65 | + }; |
| 66 | + |
| 67 | + ~QgsLayerTreeNode(); |
| 68 | + |
| 69 | + //! Find out about type of the node. It is usually shorter to use convenience functions from QgsLayerTree namespace for that |
| 70 | + NodeType nodeType(); |
| 71 | + //! Get pointer to the parent. If parent is a null pointer, the node is a root node |
| 72 | + QgsLayerTreeNode* parent(); |
| 73 | + //! Get list of children of the node. Children are owned by the parent |
| 74 | + QList<QgsLayerTreeNode*> children(); |
| 75 | + |
| 76 | + //! Read layer tree from XML. Returns new instance |
| 77 | + static QgsLayerTreeNode* readXML( QDomElement& element ); |
| 78 | + //! Write layer tree to XML |
| 79 | + virtual void writeXML( QDomElement& parentElement ) = 0; |
| 80 | + |
| 81 | + //! Return string with layer tree structure. For debug purposes only |
| 82 | + virtual QString dump() const = 0; |
| 83 | + |
| 84 | + //! Create a copy of the node. Returns new instance |
| 85 | + virtual QgsLayerTreeNode* clone() const = 0 /Factory/; |
| 86 | + |
| 87 | + //! Return whether the node should be shown as expanded or collapsed in GUI |
| 88 | + bool isExpanded() const; |
| 89 | + //! Set whether the node should be shown as expanded or collapsed in GUI |
| 90 | + void setExpanded( bool expanded ); |
| 91 | + |
| 92 | + /** Set a custom property for the node. Properties are stored in a map and saved in project file. */ |
| 93 | + void setCustomProperty( const QString& key, const QVariant& value ); |
| 94 | + /** Read a custom property from layer. Properties are stored in a map and saved in project file. */ |
| 95 | + QVariant customProperty( const QString& key, const QVariant& defaultValue = QVariant() ) const; |
| 96 | + /** Remove a custom property from layer. Properties are stored in a map and saved in project file. */ |
| 97 | + void removeCustomProperty( const QString& key ); |
| 98 | + /** Return list of keys stored in custom properties */ |
| 99 | + QStringList customProperties() const; |
| 100 | + |
| 101 | + signals: |
| 102 | + |
| 103 | + //! Emitted when one or more nodes will be added to a node within the tree |
| 104 | + void willAddChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo ); |
| 105 | + //! Emitted when one or more nodes have been added to a node within the tree |
| 106 | + void addedChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo ); |
| 107 | + //! Emitted when one or more nodes will be removed from a node within the tree |
| 108 | + void willRemoveChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo ); |
| 109 | + //! Emitted when one or more nodes has been removed from a node within the tree |
| 110 | + void removedChildren( QgsLayerTreeNode* node, int indexFrom, int indexTo ); |
| 111 | + //! Emitted when check state of a node within the tree has been changed |
| 112 | + void visibilityChanged( QgsLayerTreeNode* node, Qt::CheckState state ); |
| 113 | + //! Emitted when a custom property of a node within the tree has been changed or removed |
| 114 | + void customPropertyChanged( QgsLayerTreeNode* node, QString key ); |
| 115 | + |
| 116 | + protected: |
| 117 | + |
| 118 | + QgsLayerTreeNode( NodeType t ); |
| 119 | + |
| 120 | + private: |
| 121 | + QgsLayerTreeNode( const QgsLayerTreeNode& other ); |
| 122 | + |
| 123 | +}; |
0 commit comments