|
| 1 | +/*************************************************************************** |
| 2 | + qgslayertreeviewembeddedindicator.h |
| 3 | + -------------------------------------- |
| 4 | + Date : June 2018 |
| 5 | + Copyright : (C) 2018 by Nyall Dawson |
| 6 | + Email : nyall dot dawson at gmail dot com |
| 7 | + *************************************************************************** |
| 8 | + * * |
| 9 | + * This program is free software; you can redistribute it and/or modify * |
| 10 | + * it under the terms of the GNU General Public License as published by * |
| 11 | + * the Free Software Foundation; either version 2 of the License, or * |
| 12 | + * (at your option) any later version. * |
| 13 | + * * |
| 14 | + ***************************************************************************/ |
| 15 | + |
| 16 | +#include "qgslayertreeviewembeddedindicator.h" |
| 17 | +#include "qgslayertree.h" |
| 18 | +#include "qgslayertreemodel.h" |
| 19 | +#include "qgslayertreeview.h" |
| 20 | + |
| 21 | +QgsLayerTreeViewEmbeddedIndicatorProvider::QgsLayerTreeViewEmbeddedIndicatorProvider( QgsLayerTreeView *view ) |
| 22 | + : QObject( view ) |
| 23 | + , mLayerTreeView( view ) |
| 24 | +{ |
| 25 | + mIcon = QgsApplication::getThemeIcon( QStringLiteral( "/mIndicatorFilter.svg" ) ); |
| 26 | + |
| 27 | + QgsLayerTree *tree = mLayerTreeView->layerTreeModel()->rootGroup(); |
| 28 | + onAddedChildren( tree, 0, tree->children().count() - 1 ); |
| 29 | + |
| 30 | + connect( tree, &QgsLayerTree::addedChildren, this, &QgsLayerTreeViewEmbeddedIndicatorProvider::onAddedChildren ); |
| 31 | +} |
| 32 | + |
| 33 | +void QgsLayerTreeViewEmbeddedIndicatorProvider::onAddedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo ) |
| 34 | +{ |
| 35 | + // recursively populate indicators |
| 36 | + QList<QgsLayerTreeNode *> children = node->children(); |
| 37 | + for ( int i = indexFrom; i <= indexTo; ++i ) |
| 38 | + { |
| 39 | + QgsLayerTreeNode *childNode = children[i]; |
| 40 | + |
| 41 | + if ( QgsLayerTree::isGroup( childNode ) ) |
| 42 | + { |
| 43 | + onAddedChildren( childNode, 0, childNode->children().count() - 1 ); |
| 44 | + } |
| 45 | + else if ( QgsLayerTree::isLayer( childNode ) && childNode->customProperty( QStringLiteral( "embedded" ) ).toInt() ) |
| 46 | + { |
| 47 | + QgsLayerTreeLayer *childLayerNode = QgsLayerTree::toLayer( childNode ); |
| 48 | + addIndicatorForEmbeddedLayer( childLayerNode ); |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +QgsLayerTreeViewIndicator *QgsLayerTreeViewEmbeddedIndicatorProvider::newIndicator( const QString &project ) |
| 54 | +{ |
| 55 | + QgsLayerTreeViewIndicator *indicator = new QgsLayerTreeViewIndicator( this ); |
| 56 | + indicator->setIcon( mIcon ); |
| 57 | + indicator->setToolTip( tr( "Embedded from <b>%1</b>" ).arg( project ) ); |
| 58 | + mIndicators.insert( indicator ); |
| 59 | + return indicator; |
| 60 | +} |
| 61 | + |
| 62 | +void QgsLayerTreeViewEmbeddedIndicatorProvider::addIndicatorForEmbeddedLayer( QgsLayerTreeNode *node ) |
| 63 | +{ |
| 64 | + const QList<QgsLayerTreeViewIndicator *> nodeIndicators = mLayerTreeView->indicators( node ); |
| 65 | + |
| 66 | + // maybe the indicator exists already |
| 67 | + for ( QgsLayerTreeViewIndicator *indicator : nodeIndicators ) |
| 68 | + { |
| 69 | + if ( mIndicators.contains( indicator ) ) |
| 70 | + { |
| 71 | + return; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // it does not exist: need to create a new one |
| 76 | + mLayerTreeView->addIndicator( node, newIndicator( node->customProperty( QStringLiteral( "embedded_project" ) ).toString() ) ); |
| 77 | +} |
0 commit comments