Skip to content
Permalink
Browse files

Port remaining indicator methods to the new interface

  • Loading branch information
elpaso committed Oct 21, 2018
1 parent dffd3b7 commit 5dfa9924638009d432f9e6e1b5f48b8aea2fbdd2
@@ -29,8 +29,8 @@ class QgsLayerTreeViewBadLayerIndicatorProvider : public QgsLayerTreeViewIndicat
public:
explicit QgsLayerTreeViewBadLayerIndicatorProvider( QgsLayerTreeView *view );

private slots:
void onIndicatorClicked( const QModelIndex &index );
protected slots:
void onIndicatorClicked( const QModelIndex &index ) override;

private:
QString iconName( QgsMapLayer *layer ) override;
@@ -32,11 +32,12 @@ class QgsLayerTreeViewFilterIndicatorProvider : public QgsLayerTreeViewIndicator
QString iconName( QgsMapLayer *layer ) override;
QString tooltipText( QgsMapLayer *layer ) override;

// QgsLayerTreeViewIndicatorProvider interface
protected slots:
void onIndicatorClicked( const QModelIndex &index ) ;
void connectSignals( QgsMapLayer *layer ) ;
void disconnectSignals( QgsMapLayer *layer ) ;
void onIndicatorClicked( const QModelIndex &index ) override;

protected:
void connectSignals( QgsMapLayer *layer ) override ;
void disconnectSignals( QgsMapLayer *layer ) override;
};

#endif // QGSLAYERTREEVIEWFILTERINDICATOR_H
@@ -48,15 +48,18 @@ class QgsLayerTreeViewIndicatorProvider : public QObject

explicit QgsLayerTreeViewIndicatorProvider( QgsLayerTreeView *view );

protected slots:
protected:

// Subclasses MAY override:
//! Action on indicator clicked, default implementation does nothing
void onIndicatorClicked( const QModelIndex &index ) { Q_UNUSED( index ) }
//! Connect signals, default implementation connects vector layers to dataSourceChanged()
void connectSignals( QgsMapLayer *layer );
virtual void connectSignals( QgsMapLayer *layer );
//! Disconnect signals, default implementation disconnects vector layers from dataSourceChanged()
void disconnectSignals( QgsMapLayer *layer );
virtual void disconnectSignals( QgsMapLayer *layer );

protected slots:

//! Action on indicator clicked, default implementation does nothing
virtual void onIndicatorClicked( const QModelIndex &index ) { Q_UNUSED( index ) }
// End MAY overrides

//! Connects to signals of layers newly added to the tree
@@ -27,7 +27,7 @@ class QgsLayerTreeViewMemoryIndicatorProvider : public QgsLayerTreeViewIndicator

protected slots:

void onIndicatorClicked( const QModelIndex &index );
void onIndicatorClicked( const QModelIndex &index ) override;

private:
bool acceptLayer( QgsMapLayer *layer ) override;
@@ -22,149 +22,34 @@


QgsLayerTreeViewNonRemovableIndicatorProvider::QgsLayerTreeViewNonRemovableIndicatorProvider( QgsLayerTreeView *view )
: QObject( view )
, mLayerTreeView( view )
: QgsLayerTreeViewIndicatorProvider( view )
{
mIcon = QgsApplication::getThemeIcon( QStringLiteral( "/mIndicatorNonRemovable.svg" ) );

QgsLayerTree *tree = mLayerTreeView->layerTreeModel()->rootGroup();
onAddedChildren( tree, 0, tree->children().count() - 1 );

connect( tree, &QgsLayerTree::addedChildren, this, &QgsLayerTreeViewNonRemovableIndicatorProvider::onAddedChildren );
connect( tree, &QgsLayerTree::willRemoveChildren, this, &QgsLayerTreeViewNonRemovableIndicatorProvider::onWillRemoveChildren );
}

void QgsLayerTreeViewNonRemovableIndicatorProvider::onAddedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo )
QString QgsLayerTreeViewNonRemovableIndicatorProvider::iconName( QgsMapLayer *layer )
{
// recursively connect to providers' dataChanged() signal

QList<QgsLayerTreeNode *> children = node->children();
for ( int i = indexFrom; i <= indexTo; ++i )
{
QgsLayerTreeNode *childNode = children[i];

if ( QgsLayerTree::isGroup( childNode ) )
{
onAddedChildren( childNode, 0, childNode->children().count() - 1 );
}
else if ( QgsLayerTree::isLayer( childNode ) )
{
QgsLayerTreeLayer *childLayerNode = QgsLayerTree::toLayer( childNode );
if ( QgsMapLayer *layer = childLayerNode->layer() )
{
if ( QgsLayerTreeUtils::countMapLayerInTree( mLayerTreeView->layerTreeModel()->rootGroup(), layer ) == 1 )
connect( layer, &QgsMapLayer::flagsChanged, this, &QgsLayerTreeViewNonRemovableIndicatorProvider::onFlagsChanged );
addOrRemoveIndicator( childLayerNode, layer );
}
else if ( !childLayerNode->layer() )
{
// wait for layer to be loaded (e.g. when loading project, first the tree is loaded, afterwards the references to layers are resolved)
connect( childLayerNode, &QgsLayerTreeLayer::layerLoaded, this, &QgsLayerTreeViewNonRemovableIndicatorProvider::onLayerLoaded );
}
}
}
Q_UNUSED( layer );
return QStringLiteral( "/mIndicatorNonRemovable.svg" );
}


void QgsLayerTreeViewNonRemovableIndicatorProvider::onWillRemoveChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo )
QString QgsLayerTreeViewNonRemovableIndicatorProvider::tooltipText( QgsMapLayer *layer )
{
// recursively disconnect from providers' dataChanged() signal

QList<QgsLayerTreeNode *> children = node->children();
for ( int i = indexFrom; i <= indexTo; ++i )
{
QgsLayerTreeNode *childNode = children[i];

if ( QgsLayerTree::isGroup( childNode ) )
{
onWillRemoveChildren( childNode, 0, childNode->children().count() - 1 );
}
else if ( QgsLayerTree::isLayer( childNode ) )
{
QgsLayerTreeLayer *childLayerNode = QgsLayerTree::toLayer( childNode );
if ( childLayerNode->layer() )
{
if ( QgsLayerTreeUtils::countMapLayerInTree( mLayerTreeView->layerTreeModel()->rootGroup(), childLayerNode->layer() ) == 1 )
disconnect( childLayerNode->layer(), &QgsMapLayer::flagsChanged, this, &QgsLayerTreeViewNonRemovableIndicatorProvider::onFlagsChanged );
}
}
}
Q_UNUSED( layer );
return tr( "Layer required by the project" );
}


void QgsLayerTreeViewNonRemovableIndicatorProvider::onLayerLoaded()
bool QgsLayerTreeViewNonRemovableIndicatorProvider::acceptLayer( QgsMapLayer *layer )
{
QgsLayerTreeLayer *nodeLayer = qobject_cast<QgsLayerTreeLayer *>( sender() );
if ( !nodeLayer )
return;

if ( QgsMapLayer *layer = nodeLayer->layer() )
{
connect( layer, &QgsMapLayer::flagsChanged, this, &QgsLayerTreeViewNonRemovableIndicatorProvider::onFlagsChanged );
addOrRemoveIndicator( nodeLayer, layer );
}
return layer->flags() & QgsMapLayer::Removable;
}

void QgsLayerTreeViewNonRemovableIndicatorProvider::onFlagsChanged()
void QgsLayerTreeViewNonRemovableIndicatorProvider::connectSignals( QgsMapLayer *layer )
{
QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( sender() );
if ( !layer )
return;

// walk the tree and find layer node that needs to be updated
const QList<QgsLayerTreeLayer *> layerNodes = mLayerTreeView->layerTreeModel()->rootGroup()->findLayers();
for ( QgsLayerTreeLayer *node : layerNodes )
{
if ( node->layer() && node->layer() == layer )
{
addOrRemoveIndicator( node, layer );
break;
}
}
connect( layer, &QgsMapLayer::flagsChanged, this, &QgsLayerTreeViewNonRemovableIndicatorProvider::onLayerChanged );
}


std::unique_ptr<QgsLayerTreeViewIndicator> QgsLayerTreeViewNonRemovableIndicatorProvider::newIndicator()
void QgsLayerTreeViewNonRemovableIndicatorProvider::disconnectSignals( QgsMapLayer *layer )
{
std::unique_ptr< QgsLayerTreeViewIndicator > indicator = qgis::make_unique< QgsLayerTreeViewIndicator >( this );
indicator->setIcon( mIcon );
indicator->setToolTip( tr( "Layer required by the project" ) );
mIndicators.insert( indicator.get() );
return indicator;
disconnect( layer, &QgsMapLayer::flagsChanged, this, &QgsLayerTreeViewNonRemovableIndicatorProvider::onLayerChanged );
}

void QgsLayerTreeViewNonRemovableIndicatorProvider::addOrRemoveIndicator( QgsLayerTreeNode *node, QgsMapLayer *layer )
{
bool removable = layer->flags() & QgsMapLayer::Removable;
if ( !removable )
{
const QList<QgsLayerTreeViewIndicator *> nodeIndicators = mLayerTreeView->indicators( node );

// maybe the indicator exists already
for ( QgsLayerTreeViewIndicator *indicator : nodeIndicators )
{
if ( mIndicators.contains( indicator ) )
return;
}

// it does not exist: need to create a new one
mLayerTreeView->addIndicator( node, newIndicator().release() );
}
else
{
const QList<QgsLayerTreeViewIndicator *> nodeIndicators = mLayerTreeView->indicators( node );

// there may be existing indicator we need to get rid of
for ( QgsLayerTreeViewIndicator *indicator : nodeIndicators )
{
if ( mIndicators.contains( indicator ) )
{
mLayerTreeView->removeIndicator( node, indicator );
indicator->deleteLater();
return;
}
}

// no indicator was there before, nothing to do
}
}
@@ -16,39 +16,29 @@
#ifndef QGSLAYERTREEVIEWNONREMOVABLEINDICATOR_H
#define QGSLAYERTREEVIEWNONREMOVABLEINDICATOR_H

#include "qgslayertreeviewindicator.h"
#include "qgslayertreeviewindicatorprovider.h"

#include <QSet>
#include <memory>

class QgsLayerTreeNode;
class QgsLayerTreeView;
class QgsMapLayer;

class QgsLayerTreeViewNonRemovableIndicatorProvider : public QObject
class QgsLayerTreeViewNonRemovableIndicatorProvider : public QgsLayerTreeViewIndicatorProvider
{
Q_OBJECT
public:
explicit QgsLayerTreeViewNonRemovableIndicatorProvider( QgsLayerTreeView *view );

private slots:
//! Connects to signals of layers newly added to the tree
void onAddedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
//! Disconnects from layers about to be removed from the tree
void onWillRemoveChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
//! Starts listening to layer provider's dataChanged signal
void onLayerLoaded();
void onFlagsChanged();

private:
std::unique_ptr< QgsLayerTreeViewIndicator > newIndicator();
void addOrRemoveIndicator( QgsLayerTreeNode *node, QgsMapLayer *layer );
QString iconName( QgsMapLayer *layer ) override;
QString tooltipText( QgsMapLayer *layer ) override;
bool acceptLayer( QgsMapLayer *layer ) override;

protected:

void connectSignals( QgsMapLayer *layer ) override;
void disconnectSignals( QgsMapLayer *layer ) override;

private:
QgsLayerTreeView *mLayerTreeView = nullptr;
QIcon mIcon;
QSet<QgsLayerTreeViewIndicator *> mIndicators;
};



#endif // QGSLAYERTREEVIEWNONREMOVABLEINDICATOR_H

0 comments on commit 5dfa992

Please sign in to comment.
You can’t perform that action at this time.