Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Filter model respects algorithm flags
  • Loading branch information
nyalldawson committed Jul 16, 2018
1 parent a107e2f commit dab4f42
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 21 deletions.
Expand Up @@ -197,6 +197,9 @@ class QgsProcessingToolboxModel : QAbstractItemModel
%Docstring
A model for providers and algorithms shown within the Processing toolbox.

See QgsProcessingToolboxProxyModel for a sorted, filterable version
of this model.

.. versionadded:: 3.2
%End

Expand Down Expand Up @@ -263,7 +266,16 @@ a None if the index does not represent a provider.
Returns the algorithm which corresponds to a given ``index``, or
a None if the index does not represent an algorithm.

.. seealso:: :py:func:`isAlgorithm`

.. seealso:: :py:func:`providerForIndex`
%End

bool isAlgorithm( const QModelIndex &index ) const;
%Docstring
Returns true if ``index`` corresponds to an algorithm.

.. seealso:: :py:func:`algorithmForIndex`
%End

QModelIndex indexForProvider( QgsProcessingProvider *provider ) const;
Expand All @@ -284,7 +296,9 @@ Returns the index corresponding to the parent of a given node.
class QgsProcessingToolboxProxyModel: QSortFilterProxyModel
{
%Docstring
A sort/filter proxy model for providers and algorithms shown within the Processing toolbox.
A sort/filter proxy model for providers and algorithms shown within the Processing toolbox,
which automatically sorts the toolbox in a logical fashion and supports filtering
the results.

.. versionadded:: 3.2
%End
Expand All @@ -294,6 +308,14 @@ A sort/filter proxy model for providers and algorithms shown within the Processi
%End
public:

enum Filter
{
FilterToolbox,
FilterModeler,
};
typedef QFlags<QgsProcessingToolboxProxyModel::Filter> Filters;


explicit QgsProcessingToolboxProxyModel( QObject *parent /TransferThis/ = 0, QgsProcessingRegistry *registry = 0 );
%Docstring
Constructor for QgsProcessingToolboxProxyModel, with the given ``parent`` object.
Expand All @@ -302,6 +324,20 @@ If ``registry`` is specified then the model will show providers and algorithms
from the given registry. If no registry is specified, then the processing
registry attached to QgsApplication.processingRegistry() will be used
by the model.
%End

void setFilters( QgsProcessingToolboxProxyModel::Filters filters );
%Docstring
Set ``filters`` that affect how toolbox content is filtered.

.. seealso:: :py:func:`filters`
%End

Filters filters() const;
%Docstring
Returns any filters that affect how toolbox content is filtered.

.. seealso:: :py:func:`setFilters`
%End

virtual bool filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const;
Expand Down
40 changes: 39 additions & 1 deletion python/gui/processing/qgsprocessingtoolboxmodel.sip.in
Expand Up @@ -197,6 +197,9 @@ class QgsProcessingToolboxModel : QAbstractItemModel
%Docstring
A model for providers and algorithms shown within the Processing toolbox.

See QgsProcessingToolboxProxyModel for a sorted, filterable version
of this model.

.. versionadded:: 3.2
%End

Expand Down Expand Up @@ -263,7 +266,16 @@ a None if the index does not represent a provider.
Returns the algorithm which corresponds to a given ``index``, or
a None if the index does not represent an algorithm.

.. seealso:: :py:func:`isAlgorithm`

.. seealso:: :py:func:`providerForIndex`
%End

bool isAlgorithm( const QModelIndex &index ) const;
%Docstring
Returns true if ``index`` corresponds to an algorithm.

.. seealso:: :py:func:`algorithmForIndex`
%End

QModelIndex indexForProvider( QgsProcessingProvider *provider ) const;
Expand All @@ -284,7 +296,9 @@ Returns the index corresponding to the parent of a given node.
class QgsProcessingToolboxProxyModel: QSortFilterProxyModel
{
%Docstring
A sort/filter proxy model for providers and algorithms shown within the Processing toolbox.
A sort/filter proxy model for providers and algorithms shown within the Processing toolbox,
which automatically sorts the toolbox in a logical fashion and supports filtering
the results.

.. versionadded:: 3.2
%End
Expand All @@ -294,6 +308,14 @@ A sort/filter proxy model for providers and algorithms shown within the Processi
%End
public:

enum Filter
{
FilterToolbox,
FilterModeler,
};
typedef QFlags<QgsProcessingToolboxProxyModel::Filter> Filters;


explicit QgsProcessingToolboxProxyModel( QObject *parent /TransferThis/ = 0, QgsProcessingRegistry *registry = 0 );
%Docstring
Constructor for QgsProcessingToolboxProxyModel, with the given ``parent`` object.
Expand All @@ -304,6 +326,22 @@ registry attached to QgsApplication.processingRegistry() will be used
by the model.
%End

void setFilters( QgsProcessingToolboxProxyModel::Filters filters );
%Docstring
Set ``filters`` that affect how toolbox content is filtered.

.. seealso:: :py:func:`filters`
%End

Filters filters() const;
%Docstring
Returns any filters that affect how toolbox content is filtered.

.. seealso:: :py:func:`setFilters`
%End

virtual bool filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const;

virtual bool lessThan( const QModelIndex &left, const QModelIndex &right ) const;


Expand Down
58 changes: 51 additions & 7 deletions src/gui/processing/qgsprocessingtoolboxmodel.cpp
Expand Up @@ -377,6 +377,12 @@ const QgsProcessingAlgorithm *QgsProcessingToolboxModel::algorithmForIndex( cons
return qobject_cast< QgsProcessingToolboxModelAlgorithmNode * >( n )->algorithm();
}

bool QgsProcessingToolboxModel::isAlgorithm( const QModelIndex &index ) const
{
QgsProcessingToolboxModelNode *n = index2node( index );
return ( n && n->nodeType() == QgsProcessingToolboxModelNode::NodeAlgorithm );
}

QModelIndex QgsProcessingToolboxModel::indexForProvider( QgsProcessingProvider *provider ) const
{
if ( !provider )
Expand Down Expand Up @@ -429,19 +435,57 @@ QgsProcessingToolboxProxyModel::QgsProcessingToolboxProxyModel( QObject *parent,
sort( 0 );
}

void QgsProcessingToolboxProxyModel::setFilters( QgsProcessingToolboxProxyModel::Filters filters )
{
mFilters = filters;
invalidateFilter();
}

void QgsProcessingToolboxProxyModel::setFilterString( const QString &filter )
{
mFilterString = filter;
invalidateFilter();
}

bool QgsProcessingToolboxProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const
{
QModelIndex sourceIndex = mModel->index( sourceRow, 0, sourceParent );
if ( QgsProcessingProvider *provider = mModel->providerForIndex( sourceIndex ) )
if ( mModel->isAlgorithm( sourceIndex ) )
{
return provider->isActive(); // and has visible children!!
if ( mFilters & FilterModeler )
{
bool isHiddenFromModeler = sourceModel()->data( sourceIndex, QgsProcessingToolboxModel::RoleAlgorithmFlags ).toInt() & QgsProcessingAlgorithm::FlagHideFromModeler;
return !isHiddenFromModeler;
}
if ( mFilters & FilterToolbox )
{
bool isHiddenFromToolbox = sourceModel()->data( sourceIndex, QgsProcessingToolboxModel::RoleAlgorithmFlags ).toInt() & QgsProcessingAlgorithm::FlagHideFromToolbox;
return !isHiddenFromToolbox;
}
return true;
}

bool hasChildren = false;
// providers and groups are shown only if they have visible children
int count = sourceModel()->rowCount( sourceIndex );
for ( int i = 0; i < count; ++i )
{
if ( filterAcceptsRow( i, sourceIndex ) )
{
hasChildren = true;
break;
}
}

// TODO
// check flags - hide from toolbox/modeler
// check search string
// check group has visible children
return true;
if ( QgsProcessingProvider *provider = mModel->providerForIndex( sourceIndex ) )
{
return hasChildren && provider->isActive();
}
else
{
// group
return hasChildren;
}
}

bool QgsProcessingToolboxProxyModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
Expand Down
42 changes: 41 additions & 1 deletion src/gui/processing/qgsprocessingtoolboxmodel.h
Expand Up @@ -220,6 +220,10 @@ class GUI_EXPORT QgsProcessingToolboxModelAlgorithmNode : public QgsProcessingTo

/**
* A model for providers and algorithms shown within the Processing toolbox.
*
* See QgsProcessingToolboxProxyModel for a sorted, filterable version
* of this model.
*
* \ingroup gui
* \since QGIS 3.2
*/
Expand Down Expand Up @@ -278,10 +282,18 @@ class GUI_EXPORT QgsProcessingToolboxModel : public QAbstractItemModel
* Returns the algorithm which corresponds to a given \a index, or
* a nullptr if the index does not represent an algorithm.
*
* \see isAlgorithm()
* \see providerForIndex()
*/
const QgsProcessingAlgorithm *algorithmForIndex( const QModelIndex &index ) const;

/**
* Returns true if \a index corresponds to an algorithm.
*
* \see algorithmForIndex()
*/
bool isAlgorithm( const QModelIndex &index ) const;

/**
* Returns the index corresponding to the specified \a provider.
* \see providerForIndex()
Expand Down Expand Up @@ -321,7 +333,10 @@ class GUI_EXPORT QgsProcessingToolboxModel : public QAbstractItemModel


/**
* A sort/filter proxy model for providers and algorithms shown within the Processing toolbox.
* A sort/filter proxy model for providers and algorithms shown within the Processing toolbox,
* which automatically sorts the toolbox in a logical fashion and supports filtering
* the results.
*
* \ingroup gui
* \since QGIS 3.2
*/
Expand All @@ -331,6 +346,14 @@ class GUI_EXPORT QgsProcessingToolboxProxyModel: public QSortFilterProxyModel

public:

enum Filter
{
FilterToolbox = 1 << 1, //!< Filters out any algorithms and content which should not be shown in the toolbox
FilterModeler = 1 << 2, //!< Filters out any algorithms and content which should not be shown in the modeler
};
Q_DECLARE_FLAGS( Filters, Filter )
Q_FLAG( Filters )

/**
* Constructor for QgsProcessingToolboxProxyModel, with the given \a parent object.
*
Expand All @@ -341,12 +364,29 @@ class GUI_EXPORT QgsProcessingToolboxProxyModel: public QSortFilterProxyModel
*/
explicit QgsProcessingToolboxProxyModel( QObject *parent SIP_TRANSFERTHIS = nullptr, QgsProcessingRegistry *registry = nullptr );

/**
* Set \a filters that affect how toolbox content is filtered.
* \see filters()
*/
void setFilters( QgsProcessingToolboxProxyModel::Filters filters );

/**
* Returns any filters that affect how toolbox content is filtered.
* \see setFilters()
*/
Filters filters() const { return mFilters; }


void setFilterString( const QString &filter );

bool filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const override;
bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override;

private:

QgsProcessingToolboxModel *mModel = nullptr;
Filters mFilters = nullptr;
QString mFilterString;
};


Expand Down

0 comments on commit dab4f42

Please sign in to comment.