@@ -45,14 +45,31 @@ class QgsLayerTreeModelSymbologyNode : public QObject
45
45
};
46
46
47
47
48
-
48
+ /* *
49
+ * The QgsLayerTreeModel class is model implementation for Qt item views framework.
50
+ * The model can be used in any QTreeView, it is however recommended to use it
51
+ * with QgsLayerTreeView which brings additional functionality specific to layer tree handling.
52
+ *
53
+ * The model listens to the changes in the layer tree and signals the changes as appropriate,
54
+ * so that any view that uses the model is updated accordingly.
55
+ *
56
+ * Behavior of the model can be customized with flags. For example, whether to show symbology or
57
+ * whether to allow changes to the layer tree.
58
+ *
59
+ * @see QgsLayerTreeView
60
+ * @note added in 2.4
61
+ */
49
62
class GUI_EXPORT QgsLayerTreeModel : public QAbstractItemModel
50
63
{
51
64
Q_OBJECT
52
65
public:
66
+ // ! Construct a new tree model with given layer tree (root node must not be null pointer).
67
+ // ! The root node is not transferred by the model.
53
68
explicit QgsLayerTreeModel ( QgsLayerTreeGroup* rootNode, QObject *parent = 0 );
54
69
~QgsLayerTreeModel ();
55
70
71
+ // Implementation of virtual functions from QAbstractItemModel
72
+
56
73
int rowCount ( const QModelIndex &parent = QModelIndex() ) const ;
57
74
int columnCount ( const QModelIndex &parent = QModelIndex() ) const ;
58
75
QModelIndex index ( int row, int column, const QModelIndex &parent = QModelIndex() ) const ;
@@ -64,9 +81,10 @@ class GUI_EXPORT QgsLayerTreeModel : public QAbstractItemModel
64
81
QStringList mimeTypes () const ;
65
82
QMimeData* mimeData ( const QModelIndexList& indexes ) const ;
66
83
bool dropMimeData ( const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent );
67
-
68
84
bool removeRows ( int row, int count, const QModelIndex& parent = QModelIndex() );
69
85
86
+ // New stuff
87
+
70
88
enum Flag
71
89
{
72
90
// display flags
@@ -79,24 +97,39 @@ class GUI_EXPORT QgsLayerTreeModel : public QAbstractItemModel
79
97
};
80
98
Q_DECLARE_FLAGS ( Flags, Flag )
81
99
82
- void setFlags ( Flags f ) { mFlags = f; }
83
- void setFlag ( Flag f, bool on = true ) { if ( on ) mFlags |= f; else mFlags &= ~f; }
84
- Flags flags () const { return mFlags ; }
85
- bool testFlag ( Flag f ) const { return mFlags .testFlag ( f ); }
86
-
87
- // conversion functions used by views
88
-
100
+ // ! Set OR-ed combination of model flags
101
+ void setFlags ( Flags f );
102
+ // ! Enable or disable a model flag
103
+ void setFlag ( Flag f, bool on = true );
104
+ // ! Return OR-ed combination of model flags
105
+ Flags flags () const ;
106
+ // ! Check whether a flag is enabled
107
+ bool testFlag ( Flag f ) const ;
108
+
109
+ // ! Return layer tree node for given index. Returns root node for invalid index.
110
+ // ! Returns null pointer if index does not refer to a layer tree node (e.g. it is a symbology item)
89
111
QgsLayerTreeNode* index2node ( const QModelIndex& index ) const ;
90
- static QgsLayerTreeModelSymbologyNode* index2symnode ( const QModelIndex& index );
112
+ // ! Return index for a given node. If the node does not belong to the layer tree, the result is undefined
91
113
QModelIndex node2index ( QgsLayerTreeNode* node ) const ;
92
-
114
+ // ! Convert a list of indexes to a list of layer tree nodes.
115
+ // ! Indices that do not represent layer tree nodes are skipped.
116
+ // ! @arg skipInternal If true, a node is included in the output list only if no parent node is in the list
93
117
QList<QgsLayerTreeNode*> indexes2nodes ( const QModelIndexList& list, bool skipInternal = false ) const ;
118
+ // ! Return true if index represents a symbology node (instead of layer node)
119
+ bool isIndexSymbologyNode ( const QModelIndex& index ) const ;
120
+ // ! Return layer node to which a symbology node belongs to. Returns null pointer if index is not a symbology node.
121
+ QgsLayerTreeLayer* layerNodeForSymbologyNode ( const QModelIndex& index ) const ;
94
122
95
- QgsLayerTreeGroup* rootGroup () { return mRootNode ; }
123
+ // ! Return pointer to the root node of the layer tree. Always a non-null pointer.
124
+ QgsLayerTreeGroup* rootGroup ();
96
125
126
+ // ! Force a refresh of symbology of layer node.
127
+ // ! Not necessary to call when layer's renderer is changed as the model listens to these events.
97
128
void refreshLayerSymbology ( QgsLayerTreeLayer* nodeLayer );
98
129
99
- QModelIndex currentIndex () const { return mCurrentIndex ; }
130
+ // ! Get index of the item marked as current. Item marked as current is underlined.
131
+ QModelIndex currentIndex () const ;
132
+ // ! Set index of the current item. May be used by view. Item marked as current is underlined.
100
133
void setCurrentIndex ( const QModelIndex& currentIndex );
101
134
102
135
signals:
@@ -124,14 +157,18 @@ class GUI_EXPORT QgsLayerTreeModel : public QAbstractItemModel
124
157
void connectToLayer ( QgsLayerTreeLayer* nodeLayer );
125
158
void disconnectFromLayer ( QgsLayerTreeLayer* nodeLayer );
126
159
160
+ static QgsLayerTreeModelSymbologyNode* index2symnode ( const QModelIndex& index );
161
+
127
162
static const QIcon& iconGroup ();
128
163
129
164
protected:
130
- QgsLayerTreeGroup* mRootNode ; // not owned!
165
+ // ! Pointer to the root node of the layer tree. Not owned by the model
166
+ QgsLayerTreeGroup* mRootNode ;
167
+ // ! Set of flags for the model
131
168
Flags mFlags ;
132
-
169
+ // ! Data structure for storage of symbology nodes for each layer
133
170
QMap<QgsLayerTreeLayer*, QList<QgsLayerTreeModelSymbologyNode*> > mSymbologyNodes ;
134
-
171
+ // ! Current index - will be underlined
135
172
QPersistentModelIndex mCurrentIndex ;
136
173
};
137
174
0 commit comments