Skip to content

Commit

Permalink
Fix QgsLayerTreeModel naming conventions and update Python bindings
Browse files Browse the repository at this point in the history
I was using a mixture of terms 'symbology items' and 'legend nodes' for the same thing which was confusing
  • Loading branch information
wonder-sk committed Sep 5, 2014
1 parent 854730b commit 86b4fcc
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 115 deletions.
59 changes: 45 additions & 14 deletions python/core/layertree/qgslayertreemodel.sip
Expand Up @@ -6,7 +6,7 @@
* The model listens to the changes in the layer tree and signals the changes as appropriate,
* so that any view that uses the model is updated accordingly.
*
* Behavior of the model can be customized with flags. For example, whether to show symbology or
* Behavior of the model can be customized with flags. For example, whether to show legend or
* whether to allow changes to the layer tree.
*
* @see QgsLayerTreeView
Expand Down Expand Up @@ -51,13 +51,15 @@ class QgsLayerTreeModel : QAbstractItemModel
enum Flag
{
// display flags
ShowSymbology, //!< Add symbology items for layer nodes
ShowLegend, //!< Add legend nodes for layer nodes
ShowSymbology, //!< deprecated - use ShowLegend

// behavioral flags
AllowNodeReorder, //!< Allow reordering with drag'n'drop
AllowNodeRename, //!< Allow renaming of groups and layers
AllowNodeChangeVisibility, //!< Allow user to set node visibility with a check box
AllowSymbologyChangeState, //!< Allow check boxes for symbology items (if supported by layer's legend)
AllowLegendChangeState, //!< Allow check boxes for legend nodes (if supported by layer's legend)
AllowSymbologyChangeState, //!< deprecated - use AllowLegendChangeState
};
typedef QFlags<QgsLayerTreeModel::Flag> Flags;

Expand All @@ -71,25 +73,35 @@ class QgsLayerTreeModel : QAbstractItemModel
bool testFlag( Flag f ) const;

//! Return layer tree node for given index. Returns root node for invalid index.
//! Returns null pointer if index does not refer to a layer tree node (e.g. it is a symbology item)
//! Returns null pointer if index does not refer to a layer tree node (e.g. it is a legend node)
QgsLayerTreeNode* index2node( const QModelIndex& index ) const;
//! Return index for a given node. If the node does not belong to the layer tree, the result is undefined
QModelIndex node2index( QgsLayerTreeNode* node ) const;
//! Convert a list of indexes to a list of layer tree nodes.
//! Indices that do not represent layer tree nodes are skipped.
//! @arg skipInternal If true, a node is included in the output list only if no parent node is in the list
QList<QgsLayerTreeNode*> indexes2nodes( const QModelIndexList& list, bool skipInternal = false ) const;
//! Return true if index represents a symbology node (instead of layer node)
bool isIndexSymbologyNode( const QModelIndex& index ) const;
//! Return layer node to which a symbology node belongs to. Returns null pointer if index is not a symbology node.
QgsLayerTreeLayer* layerNodeForSymbologyNode( const QModelIndex& index ) const;

//! Return legend node for given index. Returns null for invalid index
//! @note added in 2.6
static QgsLayerTreeModelLegendNode* index2legendNode( const QModelIndex& index );
//! Return index for a given legend node. If the legend node does not belong to the layer tree, the result is undefined
//! @note added in 2.6
QModelIndex legendNode2index( QgsLayerTreeModelLegendNode* legendNode );

//! Return list of legend nodes attached to a particular layer node
//! @note added in 2.6
QList<QgsLayerTreeModelLegendNode*> layerLegendNodes( QgsLayerTreeLayer* nodeLayer );

//! Return pointer to the root node of the layer tree. Always a non-null pointer.
QgsLayerTreeGroup* rootGroup();
//! Reset the model and use a new root group node
//! @note added in 2.6
void setRootGroup( QgsLayerTreeGroup* newRootGroup );

//! Force a refresh of symbology of layer node.
//! Force a refresh of legend nodes of a layer node.
//! Not necessary to call when layer's renderer is changed as the model listens to these events.
void refreshLayerSymbology( QgsLayerTreeLayer* nodeLayer );
void refreshLayerLegend( QgsLayerTreeLayer* nodeLayer );

//! Get index of the item marked as current. Item marked as current is underlined.
QModelIndex currentIndex() const;
Expand All @@ -101,10 +113,29 @@ class QgsLayerTreeModel : QAbstractItemModel
//! Get font for a particular type of layer tree node. nodeType should come from QgsLayerTreeNode::NodeType enumeration
QFont layerTreeNodeFont( int nodeType ) const;

//! Set at what number of symbology nodes the layer node should be collapsed. Setting -1 disables the auto-collapse (default).
void setAutoCollapseSymbologyNodes( int nodeCount );
//! Return at what number of symbology nodes the layer node should be collapsed. -1 means no auto-collapse (default).
int autoCollapseSymbologyNodes() const;
//! Set at what number of legend nodes the layer node should be collapsed. Setting -1 disables the auto-collapse (default).
void setAutoCollapseLegendNodes( int nodeCount );
//! Return at what number of legend nodes the layer node should be collapsed. -1 means no auto-collapse (default).
int autoCollapseLegendNodes() const;

//! Force only display of legend nodes which are valid for given scale denominator.
//! Setting value <= 0 will disable the functionality
//! @note added in 2.6
void setLegendFilterByScale( double scaleDenominator );
double legendFilterByScale() const;

//! Return true if index represents a legend node (instead of layer node)
//! @deprecated use index2legendNode()
bool isIndexSymbologyNode( const QModelIndex& index ) const /Deprecated/;
//! Return layer node to which a legend node belongs to. Returns null pointer if index is not a legend node.
//! @deprecated use index2legendNode()->parent()
QgsLayerTreeLayer* layerNodeForSymbologyNode( const QModelIndex& index ) const /Deprecated/;
//! @deprecated use refreshLayerLegend()
void refreshLayerSymbology( QgsLayerTreeLayer* nodeLayer ) /Deprecated/;
//! @deprecated use setAutoCollapseLegendNodes()
void setAutoCollapseSymbologyNodes( int nodeCount ) /Deprecated/;
//! @deprecated use autoCollapseLegendNodes()
int autoCollapseSymbologyNodes() const /Deprecated/;

signals:

Expand Down
20 changes: 10 additions & 10 deletions src/app/composer/qgscomposerlegendwidget.cpp
Expand Up @@ -517,7 +517,7 @@ void QgsComposerLegendWidget::on_mMoveDownToolButton_clicked()
return;

QgsLayerTreeNode* node = mItemTreeView->layerTreeModel()->index2node( index );
QgsLayerTreeModelLegendNode* legendNode = mItemTreeView->layerTreeModel()->index2symnode( index );
QgsLayerTreeModelLegendNode* legendNode = mItemTreeView->layerTreeModel()->index2legendNode( index );
if ( !node && !legendNode )
return;

Expand All @@ -532,7 +532,7 @@ void QgsComposerLegendWidget::on_mMoveDownToolButton_clicked()
else // legend node
{
_moveLegendNode( legendNode->parent(), index.row(), 1 );
mItemTreeView->layerTreeModel()->refreshLayerSymbology( legendNode->parent() );
mItemTreeView->layerTreeModel()->refreshLayerLegend( legendNode->parent() );
}

mItemTreeView->setCurrentIndex( mItemTreeView->layerTreeModel()->index( index.row() + 1, 0, parentIndex ) );
Expand All @@ -554,7 +554,7 @@ void QgsComposerLegendWidget::on_mMoveUpToolButton_clicked()
return;

QgsLayerTreeNode* node = mItemTreeView->layerTreeModel()->index2node( index );
QgsLayerTreeModelLegendNode* legendNode = mItemTreeView->layerTreeModel()->index2symnode( index );
QgsLayerTreeModelLegendNode* legendNode = mItemTreeView->layerTreeModel()->index2legendNode( index );
if ( !node && !legendNode )
return;

Expand All @@ -569,7 +569,7 @@ void QgsComposerLegendWidget::on_mMoveUpToolButton_clicked()
else // legend node
{
_moveLegendNode( legendNode->parent(), index.row(), -1 );
mItemTreeView->layerTreeModel()->refreshLayerSymbology( legendNode->parent() );
mItemTreeView->layerTreeModel()->refreshLayerLegend( legendNode->parent() );
}

mItemTreeView->setCurrentIndex( mItemTreeView->layerTreeModel()->index( index.row() - 1, 0, parentIndex ) );
Expand Down Expand Up @@ -687,7 +687,7 @@ void QgsComposerLegendWidget::on_mRemoveToolButton_clicked()
QHash<QgsLayerTreeLayer*, QList<int> > nodesWithRemoval;
foreach ( const QPersistentModelIndex index, indexes )
{
if ( QgsLayerTreeModelLegendNode* legendNode = mItemTreeView->layerTreeModel()->index2symnode( index ) )
if ( QgsLayerTreeModelLegendNode* legendNode = mItemTreeView->layerTreeModel()->index2legendNode( index ) )
{
QgsLayerTreeLayer* nodeLayer = legendNode->parent();
nodesWithRemoval[nodeLayer].append( index.row() );
Expand All @@ -706,7 +706,7 @@ void QgsComposerLegendWidget::on_mRemoveToolButton_clicked()
}

QgsMapLayerLegendUtils::setLegendNodeOrder( nodeLayer, order );
mItemTreeView->layerTreeModel()->refreshLayerSymbology( nodeLayer );
mItemTreeView->layerTreeModel()->refreshLayerLegend( nodeLayer );
}

// then remove layer tree nodes
Expand All @@ -731,7 +731,7 @@ void QgsComposerLegendWidget::on_mEditPushButton_clicked()
QModelIndex idx = mItemTreeView->selectionModel()->currentIndex();
QgsLayerTreeModel* model = mItemTreeView->layerTreeModel();
QgsLayerTreeNode* currentNode = model->index2node( idx );
QgsLayerTreeModelLegendNode* legendNode = model->index2symnode( idx );
QgsLayerTreeModelLegendNode* legendNode = model->index2legendNode( idx );
QString currentText;

if ( QgsLayerTree::isGroup( currentNode ) )
Expand Down Expand Up @@ -776,7 +776,7 @@ void QgsComposerLegendWidget::on_mEditPushButton_clicked()
QList<int> order = QgsMapLayerLegendUtils::legendNodeOrder( legendNode->parent() );
int originalIndex = ( idx.row() >= 0 && idx.row() < order.count() ? order[idx.row()] : -1 );
QgsMapLayerLegendUtils::setLegendNodeUserLabel( legendNode->parent(), originalIndex, newText );
model->refreshLayerSymbology( legendNode->parent() );
model->refreshLayerLegend( legendNode->parent() );
}

mLegend->adjustBoxSize();
Expand Down Expand Up @@ -804,7 +804,7 @@ void QgsComposerLegendWidget::resetLayerNodeToDefaults()
if ( QgsLayerTree::isLayer( node ) )
nodeLayer = QgsLayerTree::toLayer( node );
}
if ( QgsLayerTreeModelLegendNode* legendNode = mItemTreeView->layerTreeModel()->index2symnode( currentIndex ) )
if ( QgsLayerTreeModelLegendNode* legendNode = mItemTreeView->layerTreeModel()->index2legendNode( currentIndex ) )
{
nodeLayer = legendNode->parent();
}
Expand All @@ -820,7 +820,7 @@ void QgsComposerLegendWidget::resetLayerNodeToDefaults()
nodeLayer->removeCustomProperty( key );
}

mItemTreeView->layerTreeModel()->refreshLayerSymbology( nodeLayer );
mItemTreeView->layerTreeModel()->refreshLayerLegend( nodeLayer );

mLegend->update();
mLegend->adjustBoxSize();
Expand Down
4 changes: 2 additions & 2 deletions src/app/qgisapp.cpp
Expand Up @@ -397,7 +397,7 @@ void QgisApp::emitCustomSrsValidation( QgsCoordinateReferenceSystem &srs )
void QgisApp::layerTreeViewDoubleClicked( const QModelIndex& index )
{
// temporary solution for WMS legend
if ( mLayerTreeView->layerTreeModel()->isIndexSymbologyNode( index ) )
if ( mLayerTreeView->layerTreeModel()->index2legendNode( index ) )
{
QModelIndex parent = mLayerTreeView->layerTreeModel()->parent( index );
QgsLayerTreeNode* node = mLayerTreeView->layerTreeModel()->index2node( parent );
Expand Down Expand Up @@ -2306,7 +2306,7 @@ void QgisApp::initLayerTreeView()
model->setFlag( QgsLayerTreeModel::AllowNodeReorder );
model->setFlag( QgsLayerTreeModel::AllowNodeRename );
model->setFlag( QgsLayerTreeModel::AllowNodeChangeVisibility );
model->setAutoCollapseSymbologyNodes( 10 );
model->setAutoCollapseLegendNodes( 10 );

mLayerTreeView->setModel( model );
mLayerTreeView->setMenuProvider( new QgsAppLayerTreeViewMenuProvider( mLayerTreeView, mMapCanvas ) );
Expand Down
4 changes: 2 additions & 2 deletions src/core/composer/qgscomposerlegend.cpp
Expand Up @@ -439,7 +439,7 @@ void QgsComposerLegend::invalidateCurrentMap()
QgsLegendModelV2::QgsLegendModelV2( QgsLayerTreeGroup* rootNode, QObject* parent )
: QgsLayerTreeModel( rootNode, parent )
{
setFlag( QgsLayerTreeModel::AllowSymbologyChangeState, false );
setFlag( QgsLayerTreeModel::AllowLegendChangeState, false );
setFlag( QgsLayerTreeModel::AllowNodeReorder, true );
}

Expand Down Expand Up @@ -468,7 +468,7 @@ QVariant QgsLegendModelV2::data( const QModelIndex& index, int role ) const
Qt::ItemFlags QgsLegendModelV2::flags( const QModelIndex& index ) const
{
// make the legend nodes selectable even if they are not by default
if ( index2symnode( index ) )
if ( index2legendNode( index ) )
return QgsLayerTreeModel::flags( index ) | Qt::ItemIsSelectable;

return QgsLayerTreeModel::flags( index );
Expand Down

0 comments on commit 86b4fcc

Please sign in to comment.