QModelIndex QgsLayerTreeModel::parent( const QModelIndex &child ) const { if ( !child.isValid() ) return QModelIndex(); QgsLayerTreeNode* n = index2node( child ); QgsLayerTreeNode* parentNode = 0; if ( !n ) { QgsLayerTreeModelLegendNode* sym = index2symnode( child ); Q_ASSERT( sym ); parentNode = sym->parent(); } else parentNode = n->parent(); // must not be null Q_ASSERT( parentNode ); QgsLayerTreeNode* grandParentNode = parentNode->parent(); if ( !grandParentNode ) return QModelIndex(); // root node -> invalid index int row = grandParentNode->children().indexOf( parentNode ); Q_ASSERT( row >= 0 ); return createIndex( row, 0, static_cast<QObject*>( parentNode ) ); }
Qt::ItemFlags QgsLayerTreeModel::flags( const QModelIndex& index ) const { if ( !index.isValid() ) { Qt::ItemFlags rootFlags = Qt::ItemIsEnabled | Qt::ItemIsSelectable; if ( testFlag( AllowNodeReorder ) ) rootFlags |= Qt::ItemIsDropEnabled; return rootFlags; } if ( QgsLayerTreeModelLegendNode* symn = index2symnode( index ) ) { Qt::ItemFlags f = symn->flags(); if ( !testFlag( AllowSymbologyChangeState ) ) f &= ~Qt::ItemIsUserCheckable; return f; } Qt::ItemFlags f = Qt::ItemIsEnabled | Qt::ItemIsSelectable; if ( testFlag( AllowNodeRename ) ) f |= Qt::ItemIsEditable; QgsLayerTreeNode* node = index2node( index ); bool isEmbedded = node->customProperty( "embedded" ).toInt(); if ( testFlag( AllowNodeReorder ) ) { // only root embedded nodes can be reordered if ( !isEmbedded || ( isEmbedded && node->parent() && !node->parent()->customProperty( "embedded" ).toInt() ) ) f |= Qt::ItemIsDragEnabled; } if ( testFlag( AllowNodeChangeVisibility ) && ( QgsLayerTree::isLayer( node ) || QgsLayerTree::isGroup( node ) ) ) f |= Qt::ItemIsUserCheckable; if ( testFlag( AllowNodeReorder ) && QgsLayerTree::isGroup( node ) && !isEmbedded ) f |= Qt::ItemIsDropEnabled; return f; }
QgsLayerTreeGroup* QgsLayerTreeView::currentGroupNode() const { QgsLayerTreeNode* node = currentNode(); if ( QgsLayerTree::isGroup( node ) ) return QgsLayerTree::toGroup( node ); else if ( QgsLayerTree::isLayer( node ) ) { QgsLayerTreeNode* parent = node->parent(); if ( QgsLayerTree::isGroup( parent ) ) return QgsLayerTree::toGroup( parent ); } if ( QgsLayerTreeModelLegendNode* legendNode = layerTreeModel()->index2legendNode( selectionModel()->currentIndex() ) ) { QgsLayerTreeLayer* parent = legendNode->layerNode(); if ( QgsLayerTree::isGroup( parent->parent() ) ) return QgsLayerTree::toGroup( parent->parent() ); } return 0; }