mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-17 00:04:02 -04:00
Allow QgsMapLayerProxyModel to be filtered by string
This commit is contained in:
parent
5582a57918
commit
51a63b98fb
@ -93,6 +93,15 @@ Returns the list of data providers which are excluded from the model.
|
|||||||
.. seealso:: :py:func:`setExcludedProviders`
|
.. seealso:: :py:func:`setExcludedProviders`
|
||||||
|
|
||||||
.. versionadded:: 3.0
|
.. versionadded:: 3.0
|
||||||
|
%End
|
||||||
|
|
||||||
|
QString filterString() const;
|
||||||
|
%Docstring
|
||||||
|
Returns the current filter string, if set.
|
||||||
|
|
||||||
|
.. seealso:: :py:func:`setFilterString`
|
||||||
|
|
||||||
|
.. versionadded:: 3.4
|
||||||
%End
|
%End
|
||||||
|
|
||||||
virtual bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const;
|
virtual bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const;
|
||||||
@ -100,6 +109,18 @@ Returns the list of data providers which are excluded from the model.
|
|||||||
virtual bool lessThan( const QModelIndex &left, const QModelIndex &right ) const;
|
virtual bool lessThan( const QModelIndex &left, const QModelIndex &right ) const;
|
||||||
|
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
|
||||||
|
void setFilterString( const QString &filter );
|
||||||
|
%Docstring
|
||||||
|
Sets a ``filter`` string, such that only layers with names matching the
|
||||||
|
specified string will be shown.
|
||||||
|
|
||||||
|
.. seealso:: :py:func:`filterString`
|
||||||
|
|
||||||
|
.. versionadded:: 3.4
|
||||||
|
%End
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
QFlags<QgsMapLayerProxyModel::Filter> operator|(QgsMapLayerProxyModel::Filter f1, QFlags<QgsMapLayerProxyModel::Filter> f2);
|
QFlags<QgsMapLayerProxyModel::Filter> operator|(QgsMapLayerProxyModel::Filter f1, QFlags<QgsMapLayerProxyModel::Filter> f2);
|
||||||
|
@ -80,9 +80,15 @@ void QgsMapLayerProxyModel::setExcludedProviders( const QStringList &providers )
|
|||||||
invalidateFilter();
|
invalidateFilter();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void QgsMapLayerProxyModel::setFilterString( const QString &filter )
|
||||||
|
{
|
||||||
|
mFilterString = filter;
|
||||||
|
invalidateFilter();
|
||||||
|
}
|
||||||
|
|
||||||
bool QgsMapLayerProxyModel::filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const
|
bool QgsMapLayerProxyModel::filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const
|
||||||
{
|
{
|
||||||
if ( mFilters.testFlag( All ) && mExceptList.isEmpty() && mExcludedProviders.isEmpty() )
|
if ( mFilters.testFlag( All ) && mExceptList.isEmpty() && mExcludedProviders.isEmpty() && mFilterString.isEmpty() )
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
|
QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
|
||||||
@ -104,6 +110,9 @@ bool QgsMapLayerProxyModel::filterAcceptsRow( int source_row, const QModelIndex
|
|||||||
if ( mFilters.testFlag( WritableLayer ) && layer->readOnly() )
|
if ( mFilters.testFlag( WritableLayer ) && layer->readOnly() )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
if ( !layer->name().contains( mFilterString, Qt::CaseInsensitive ) )
|
||||||
|
return false;
|
||||||
|
|
||||||
// layer type
|
// layer type
|
||||||
if ( ( mFilters.testFlag( RasterLayer ) && layer->type() == QgsMapLayer::RasterLayer ) ||
|
if ( ( mFilters.testFlag( RasterLayer ) && layer->type() == QgsMapLayer::RasterLayer ) ||
|
||||||
( mFilters.testFlag( VectorLayer ) && layer->type() == QgsMapLayer::VectorLayer ) ||
|
( mFilters.testFlag( VectorLayer ) && layer->type() == QgsMapLayer::VectorLayer ) ||
|
||||||
|
@ -98,14 +98,34 @@ class CORE_EXPORT QgsMapLayerProxyModel : public QSortFilterProxyModel
|
|||||||
*/
|
*/
|
||||||
QStringList excludedProviders() const { return mExcludedProviders; }
|
QStringList excludedProviders() const { return mExcludedProviders; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the current filter string, if set.
|
||||||
|
*
|
||||||
|
* \see setFilterString()
|
||||||
|
* \since QGIS 3.4
|
||||||
|
*/
|
||||||
|
QString filterString() const { return mFilterString; }
|
||||||
|
|
||||||
bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override;
|
bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override;
|
||||||
bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override;
|
bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override;
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a \a filter string, such that only layers with names matching the
|
||||||
|
* specified string will be shown.
|
||||||
|
*
|
||||||
|
* \see filterString()
|
||||||
|
* \since QGIS 3.4
|
||||||
|
*/
|
||||||
|
void setFilterString( const QString &filter );
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Filters mFilters;
|
Filters mFilters;
|
||||||
QList<QgsMapLayer *> mExceptList;
|
QList<QgsMapLayer *> mExceptList;
|
||||||
QgsMapLayerModel *mModel = nullptr;
|
QgsMapLayerModel *mModel = nullptr;
|
||||||
QStringList mExcludedProviders;
|
QStringList mExcludedProviders;
|
||||||
|
QString mFilterString;
|
||||||
};
|
};
|
||||||
|
|
||||||
Q_DECLARE_OPERATORS_FOR_FLAGS( QgsMapLayerProxyModel::Filters )
|
Q_DECLARE_OPERATORS_FOR_FLAGS( QgsMapLayerProxyModel::Filters )
|
||||||
|
Loading…
x
Reference in New Issue
Block a user