Skip to content
Permalink
Browse files

Allow filtering QgsMapLayerComboBox by data provider

  • Loading branch information
nyalldawson committed Nov 16, 2016
1 parent 9ee7873 commit 5c3aea33b8e50ebe778c1c24ead770340413ff82
@@ -54,6 +54,20 @@ class QgsMapLayerProxyModel : QSortFilterProxyModel
//! Get the list of maplayer ids which are excluded from the list
QStringList exceptedLayerIds() const;

/**
* Sets a list of data providers which should be excluded from the model.
* @note added in QGIS 3.0
* @see excludedProviders()
*/
void setExcludedProviders( const QStringList& providers );

/**
* Returns the list of data providers which are excluded from the model.
* @see setExcludedProviders()
* @note added in QGIS 3.0
*/
QStringList excludedProviders() const;

// QSortFilterProxyModel interface
public:
bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const;
@@ -28,6 +28,20 @@ class QgsMapLayerComboBox : QComboBox
//! returns the list of excepted layers
QList<QgsMapLayer*> exceptedLayerList() const;

/**
* Sets a list of data providers which should be excluded from the combobox.
* @note added in QGIS 3.0
* @see excludedProviders()
*/
void setExcludedProviders( const QStringList& providers );

/**
* Returns the list of data providers which are excluded from the combobox.
* @see setExcludedProviders()
* @note added in QGIS 3.0
*/
QStringList excludedProviders() const;

/**
* Sets whether an optional empty layer ("not set") option is shown in the combo box.
* @see allowEmptyLayer()
@@ -18,6 +18,9 @@
#include "qgsmaplayer.h"
#include "qgsmaplayerregistry.h"
#include "qgsvectorlayer.h"
#include "qgsrasterlayer.h"
#include "qgsvectordataprovider.h"
#include "qgsrasterdataprovider.h"

QgsMapLayerProxyModel::QgsMapLayerProxyModel( QObject *parent )
: QSortFilterProxyModel( parent )
@@ -71,9 +74,15 @@ QStringList QgsMapLayerProxyModel::exceptedLayerIds() const
return lst;
}

void QgsMapLayerProxyModel::setExcludedProviders( const QStringList& providers )
{
mExcludedProviders = providers;
invalidateFilter();
}

bool QgsMapLayerProxyModel::filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const
{
if ( mFilters.testFlag( All ) && mExceptList.isEmpty() )
if ( mFilters.testFlag( All ) && mExceptList.isEmpty() && mExcludedProviders.isEmpty() )
return true;

QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
@@ -90,6 +99,11 @@ bool QgsMapLayerProxyModel::filterAcceptsRow( int source_row, const QModelIndex
return false;

QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( layer );
if ( vl && mExcludedProviders.contains( vl->dataProvider()->name() ) )
return false;
QgsRasterLayer* rl = qobject_cast<QgsRasterLayer*>( layer );
if ( rl && mExcludedProviders.contains( rl->dataProvider()->name() ) )
return false;

if ( mFilters.testFlag( WritableLayer ) && layer->readOnly() )
return false;
@@ -80,10 +80,25 @@ class CORE_EXPORT QgsMapLayerProxyModel : public QSortFilterProxyModel
//! Get the list of maplayer ids which are excluded from the list
QStringList exceptedLayerIds() const;

/**
* Sets a list of data providers which should be excluded from the model.
* @note added in QGIS 3.0
* @see excludedProviders()
*/
void setExcludedProviders( const QStringList& providers );

/**
* Returns the list of data providers which are excluded from the model.
* @see setExcludedProviders()
* @note added in QGIS 3.0
*/
QStringList excludedProviders() const { return mExcludedProviders; }

private:
Filters mFilters;
QList<QgsMapLayer*> mExceptList;
QgsMapLayerModel* mModel;
QStringList mExcludedProviders;

// QSortFilterProxyModel interface
public:
@@ -28,6 +28,16 @@ QgsMapLayerComboBox::QgsMapLayerComboBox( QWidget *parent )
connect( mProxyModel, SIGNAL( rowsRemoved( QModelIndex, int, int ) ), this, SLOT( rowsChanged() ) );
}

void QgsMapLayerComboBox::setExcludedProviders( const QStringList& providers )
{
mProxyModel->setExcludedProviders( providers );
}

QStringList QgsMapLayerComboBox::excludedProviders() const
{
return mProxyModel->excludedProviders();
}

void QgsMapLayerComboBox::setAllowEmptyLayer( bool allowEmpty )
{
mProxyModel->sourceLayerModel()->setAllowEmptyLayer( allowEmpty );
@@ -34,6 +34,7 @@ class GUI_EXPORT QgsMapLayerComboBox : public QComboBox
Q_PROPERTY( QgsMapLayerProxyModel::Filters filters READ filters WRITE setFilters )
Q_PROPERTY( bool allowEmptyLayer READ allowEmptyLayer WRITE setAllowEmptyLayer )
Q_PROPERTY( bool showCrs READ showCrs WRITE setShowCrs )
Q_PROPERTY( QStringList excludedProviders READ excludedProviders WRITE setExcludedProviders )

public:

@@ -55,6 +56,20 @@ class GUI_EXPORT QgsMapLayerComboBox : public QComboBox
//! returns the list of excepted layers
QList<QgsMapLayer*> exceptedLayerList() const {return mProxyModel->exceptedLayerList();}

/**
* Sets a list of data providers which should be excluded from the combobox.
* @note added in QGIS 3.0
* @see excludedProviders()
*/
void setExcludedProviders( const QStringList& providers );

/**
* Returns the list of data providers which are excluded from the combobox.
* @see setExcludedProviders()
* @note added in QGIS 3.0
*/
QStringList excludedProviders() const;

/**
* Sets whether an optional empty layer ("not set") option is shown in the combo box.
* @see allowEmptyLayer()

0 comments on commit 5c3aea3

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