Skip to content

Commit df09910

Browse files
committed
[layertree] Added QgsLayerTreeModel doxygen docs + minor API changes
1 parent b49ce33 commit df09910

File tree

4 files changed

+101
-21
lines changed

4 files changed

+101
-21
lines changed

src/app/qgisapp.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ void QgisApp::emitCustomSrsValidation( QgsCoordinateReferenceSystem &srs )
397397
void QgisApp::layerTreeViewDoubleClicked( const QModelIndex& index )
398398
{
399399
// temporary solution for WMS legend
400-
if ( mLayerTreeView->layerTreeModel()->index2symnode( index ) )
400+
if ( mLayerTreeView->layerTreeModel()->isIndexSymbologyNode( index ) )
401401
{
402402
QModelIndex parent = mLayerTreeView->layerTreeModel()->parent( index );
403403
QgsLayerTreeNode* node = mLayerTreeView->layerTreeModel()->index2node( parent );
@@ -9556,7 +9556,7 @@ void QgisApp::writeProject( QDomDocument &doc )
95569556
QgsLayerTreeNode* clonedRoot = QgsProject::instance()->layerTreeRoot()->clone();
95579557
QgsLayerTreeUtils::removeChildrenOfEmbeddedGroups( QgsLayerTree::toGroup( clonedRoot ) );
95589558
QDomElement oldLegendElem = QgsLayerTreeUtils::writeOldLegend( doc, QgsLayerTree::toGroup( clonedRoot ),
9559-
mLayerTreeCanvasBridge->hasCustomLayerOrder(), mLayerTreeCanvasBridge->customLayerOrder() );
9559+
mLayerTreeCanvasBridge->hasCustomLayerOrder(), mLayerTreeCanvasBridge->customLayerOrder() );
95609560
delete clonedRoot;
95619561
doc.firstChildElement( "qgis" ).appendChild( oldLegendElem );
95629562

src/gui/layertree/qgslayertreemodel.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,22 @@ QList<QgsLayerTreeNode*> QgsLayerTreeModel::indexes2nodes( const QModelIndexList
398398
return nodesFinal;
399399
}
400400

401+
bool QgsLayerTreeModel::isIndexSymbologyNode( const QModelIndex& index ) const
402+
{
403+
return index2symnode( index ) != 0;
404+
}
405+
406+
QgsLayerTreeLayer* QgsLayerTreeModel::layerNodeForSymbologyNode( const QModelIndex& index ) const
407+
{
408+
QgsLayerTreeModelSymbologyNode* symNode = index2symnode( index );
409+
return symNode ? symNode->parent() : 0;
410+
}
411+
412+
QgsLayerTreeGroup*QgsLayerTreeModel::rootGroup()
413+
{
414+
return mRootNode;
415+
}
416+
401417
void QgsLayerTreeModel::refreshLayerSymbology( QgsLayerTreeLayer* nodeLayer )
402418
{
403419
// update title
@@ -412,6 +428,11 @@ void QgsLayerTreeModel::refreshLayerSymbology( QgsLayerTreeLayer* nodeLayer )
412428
addSymbologyToLayer( nodeLayer );
413429
}
414430

431+
QModelIndex QgsLayerTreeModel::currentIndex() const
432+
{
433+
return mCurrentIndex;
434+
}
435+
415436
void QgsLayerTreeModel::setCurrentIndex( const QModelIndex& currentIndex )
416437
{
417438
QModelIndex oldIndex = mCurrentIndex;
@@ -808,6 +829,29 @@ bool QgsLayerTreeModel::removeRows( int row, int count, const QModelIndex& paren
808829
return false;
809830
}
810831

832+
void QgsLayerTreeModel::setFlags( QgsLayerTreeModel::Flags f )
833+
{
834+
mFlags = f;
835+
}
836+
837+
void QgsLayerTreeModel::setFlag( QgsLayerTreeModel::Flag f, bool on )
838+
{
839+
if ( on )
840+
mFlags |= f;
841+
else
842+
mFlags &= ~f;
843+
}
844+
845+
QgsLayerTreeModel::Flags QgsLayerTreeModel::flags() const
846+
{
847+
return mFlags;
848+
}
849+
850+
bool QgsLayerTreeModel::testFlag( QgsLayerTreeModel::Flag f ) const
851+
{
852+
return mFlags.testFlag( f );
853+
}
854+
811855

812856
const QIcon& QgsLayerTreeModel::iconGroup()
813857
{

src/gui/layertree/qgslayertreemodel.h

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,31 @@ class QgsLayerTreeModelSymbologyNode : public QObject
4545
};
4646

4747

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+
*/
4962
class GUI_EXPORT QgsLayerTreeModel : public QAbstractItemModel
5063
{
5164
Q_OBJECT
5265
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.
5368
explicit QgsLayerTreeModel( QgsLayerTreeGroup* rootNode, QObject *parent = 0 );
5469
~QgsLayerTreeModel();
5570

71+
// Implementation of virtual functions from QAbstractItemModel
72+
5673
int rowCount( const QModelIndex &parent = QModelIndex() ) const;
5774
int columnCount( const QModelIndex &parent = QModelIndex() ) const;
5875
QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const;
@@ -64,9 +81,10 @@ class GUI_EXPORT QgsLayerTreeModel : public QAbstractItemModel
6481
QStringList mimeTypes() const;
6582
QMimeData* mimeData( const QModelIndexList& indexes ) const;
6683
bool dropMimeData( const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent );
67-
6884
bool removeRows( int row, int count, const QModelIndex& parent = QModelIndex() );
6985

86+
// New stuff
87+
7088
enum Flag
7189
{
7290
// display flags
@@ -79,24 +97,39 @@ class GUI_EXPORT QgsLayerTreeModel : public QAbstractItemModel
7997
};
8098
Q_DECLARE_FLAGS( Flags, Flag )
8199

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)
89111
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
91113
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
93117
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;
94122

95-
QgsLayerTreeGroup* rootGroup() { return mRootNode; }
123+
//! Return pointer to the root node of the layer tree. Always a non-null pointer.
124+
QgsLayerTreeGroup* rootGroup();
96125

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.
97128
void refreshLayerSymbology( QgsLayerTreeLayer* nodeLayer );
98129

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.
100133
void setCurrentIndex( const QModelIndex& currentIndex );
101134

102135
signals:
@@ -124,14 +157,18 @@ class GUI_EXPORT QgsLayerTreeModel : public QAbstractItemModel
124157
void connectToLayer( QgsLayerTreeLayer* nodeLayer );
125158
void disconnectFromLayer( QgsLayerTreeLayer* nodeLayer );
126159

160+
static QgsLayerTreeModelSymbologyNode* index2symnode( const QModelIndex& index );
161+
127162
static const QIcon& iconGroup();
128163

129164
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
131168
Flags mFlags;
132-
169+
//! Data structure for storage of symbology nodes for each layer
133170
QMap<QgsLayerTreeLayer*, QList<QgsLayerTreeModelSymbologyNode*> > mSymbologyNodes;
134-
171+
//! Current index - will be underlined
135172
QPersistentModelIndex mCurrentIndex;
136173
};
137174

src/gui/layertree/qgslayertreeview.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,8 @@ QgsMapLayer* QgsLayerTreeView::layerForIndex( const QModelIndex& index ) const
177177
else
178178
{
179179
// possibly a symbology node
180-
QgsLayerTreeModelSymbologyNode* symnode = layerTreeModel()->index2symnode( index );
181-
if ( symnode )
182-
return symnode->parent()->layer();
180+
if ( layerTreeModel()->isIndexSymbologyNode( index ) )
181+
return layerTreeModel()->layerNodeForSymbologyNode( index )->layer();
183182
}
184183

185184
return 0;

0 commit comments

Comments
 (0)