Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Start on filter string handling
  • Loading branch information
nyalldawson committed Jul 16, 2018
1 parent dc737bf commit 6275960
Show file tree
Hide file tree
Showing 5 changed files with 271 additions and 9 deletions.
Expand Up @@ -223,6 +223,10 @@ of this model.
{
RoleNodeType,
RoleAlgorithmFlags,
RoleAlgorithmId,
RoleAlgorithmName,
RoleAlgorithmShortDescription,
RoleAlgorithmTags,
};

QgsProcessingToolboxModel( QObject *parent /TransferThis/ = 0, QgsProcessingRegistry *registry = 0 );
Expand Down Expand Up @@ -359,6 +363,24 @@ Set ``filters`` that affect how toolbox content is filtered.
Returns any filters that affect how toolbox content is filtered.

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

void setFilterString( const QString &filter );
%Docstring
Sets a ``filter`` string, such that only algorithms matching the
specified string will be shown.

Matches are performed using a variety of tests, including checking
against the algorithm name, short description, tags, etc.

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

QString filterString() const;
%Docstring
Returns the current filter string, if set.

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

virtual bool filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const;
Expand Down
13 changes: 13 additions & 0 deletions python/gui/processing/qgsprocessingtoolboxmodel.sip.in
Expand Up @@ -223,6 +223,9 @@ of this model.
{
RoleNodeType,
RoleAlgorithmFlags,
RoleAlgorithmId,
RoleAlgorithmName,
RoleAlgorithmTags,
};

QgsProcessingToolboxModel( QObject *parent /TransferThis/ = 0, QgsProcessingRegistry *registry = 0 );
Expand Down Expand Up @@ -361,6 +364,16 @@ Returns any filters that affect how toolbox content is filtered.
.. seealso:: :py:func:`setFilters`
%End

void setFilterString(const QString &filter);
%Docstring
Sets a ``filter`` string, such that only algorithms matching the
specified string will be shown.

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

QString filterString() const;

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

virtual bool lessThan( const QModelIndex &left, const QModelIndex &right ) const;
Expand Down
106 changes: 106 additions & 0 deletions src/gui/processing/qgsprocessingtoolboxmodel.cpp
Expand Up @@ -351,6 +351,70 @@ QVariant QgsProcessingToolboxModel::data( const QModelIndex &index, int role ) c
}
break;

case RoleAlgorithmId:
switch ( index.column() )
{
case 0:
{
if ( algorithm )
return algorithm->id();
else
return QVariant();
}

default:
return QVariant();
}
break;

case RoleAlgorithmName:
switch ( index.column() )
{
case 0:
{
if ( algorithm )
return algorithm->name();
else
return QVariant();
}

default:
return QVariant();
}
break;

case RoleAlgorithmTags:
switch ( index.column() )
{
case 0:
{
if ( algorithm )
return algorithm->tags();
else
return QVariant();
}

default:
return QVariant();
}
break;

case RoleAlgorithmShortDescription:
switch ( index.column() )
{
case 0:
{
if ( algorithm )
return algorithm->shortDescription();
else
return QVariant();
}

default:
return QVariant();
}
break;

default:
return QVariant();
}
Expand Down Expand Up @@ -499,6 +563,48 @@ bool QgsProcessingToolboxProxyModel::filterAcceptsRow( int sourceRow, const QMod
QModelIndex sourceIndex = mModel->index( sourceRow, 0, sourceParent );
if ( mModel->isAlgorithm( sourceIndex ) )
{
if ( !mFilterString.trimmed().isEmpty() )
{
const QString algId = sourceModel()->data( sourceIndex, QgsProcessingToolboxModel::RoleAlgorithmId ).toString();
const QString algName = sourceModel()->data( sourceIndex, QgsProcessingToolboxModel::RoleAlgorithmName ).toString();
const QStringList algTags = sourceModel()->data( sourceIndex, QgsProcessingToolboxModel::RoleAlgorithmTags ).toStringList();
const QString shortDesc = sourceModel()->data( sourceIndex, QgsProcessingToolboxModel::RoleAlgorithmShortDescription ).toString();

QStringList parentText;
QModelIndex parent = sourceIndex.parent();
while ( parent.isValid() )
{
const QStringList parentParts = sourceModel()->data( parent, Qt::DisplayRole ).toString().split( ' ' );
if ( !parentParts.empty() )
parentText.append( parentParts );
parent = parent.parent();
}

const QStringList partsToMatch = mFilterString.trimmed().split( ' ' );

QStringList partsToSearch = sourceModel()->data( sourceIndex, Qt::DisplayRole ).toString().split( ' ' );
partsToSearch << algId << algName;
partsToSearch.append( algTags );
if ( !shortDesc.isEmpty() )
partsToSearch.append( shortDesc.split( ' ' ) );
partsToSearch.append( parentText );

for ( const QString &part : partsToMatch )
{
bool found = false;
for ( const QString &partToSearch : qgis::as_const( partsToSearch ) )
{
if ( partToSearch.contains( part, Qt::CaseInsensitive ) )
{
found = true;
break;
}
}
if ( !found )
return false; // couldn't find a match for this word, so hide algorithm
}
}

if ( mFilters & FilterModeler )
{
bool isHiddenFromModeler = sourceModel()->data( sourceIndex, QgsProcessingToolboxModel::RoleAlgorithmFlags ).toInt() & QgsProcessingAlgorithm::FlagHideFromModeler;
Expand Down
21 changes: 20 additions & 1 deletion src/gui/processing/qgsprocessingtoolboxmodel.h
Expand Up @@ -250,6 +250,10 @@ class GUI_EXPORT QgsProcessingToolboxModel : public QAbstractItemModel
{
RoleNodeType = Qt::UserRole, //!< Corresponds to the node's type
RoleAlgorithmFlags, //!< Returns the node's algorithm flags, for algorithm nodes
RoleAlgorithmId, //!< Algorithm ID, for algorithm nodes
RoleAlgorithmName, //!< Untranslated algorithm name, for algorithm nodes
RoleAlgorithmShortDescription, //!< Short algorithm description, for algorithm nodes
RoleAlgorithmTags, //!< List of algorithm tags, for algorithm nodes
};

/**
Expand Down Expand Up @@ -398,9 +402,24 @@ class GUI_EXPORT QgsProcessingToolboxProxyModel: public QSortFilterProxyModel
*/
Filters filters() const { return mFilters; }


/**
* Sets a \a filter string, such that only algorithms matching the
* specified string will be shown.
*
* Matches are performed using a variety of tests, including checking
* against the algorithm name, short description, tags, etc.
*
* \see filterString()
*/
void setFilterString( const QString &filter );

/**
* Returns the current filter string, if set.
*
* \see setFilterString()
*/
QString filterString() const { return mFilterString; }

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

Expand Down

0 comments on commit 6275960

Please sign in to comment.