Skip to content

Commit

Permalink
Allow filtering QgsMapLayerComboBox by data provider
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 16, 2016
1 parent 9ee7873 commit 5c3aea3
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 1 deletion.
14 changes: 14 additions & 0 deletions python/core/qgsmaplayerproxymodel.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions python/gui/qgsmaplayercombobox.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
16 changes: 15 additions & 1 deletion src/core/qgsmaplayerproxymodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down Expand Up @@ -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 );
Expand All @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions src/core/qgsmaplayerproxymodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions src/gui/qgsmaplayercombobox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
15 changes: 15 additions & 0 deletions src/gui/qgsmaplayercombobox.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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()
Expand Down

0 comments on commit 5c3aea3

Please sign in to comment.