Skip to content
Permalink
Browse files

Start on filter string handling

  • Loading branch information
nyalldawson committed Jul 2, 2018
1 parent dc737bf commit 6275960df8f8f4feac9256e15ffe59bd0d3f701d
@@ -223,6 +223,10 @@ of this model.
{
RoleNodeType,
RoleAlgorithmFlags,
RoleAlgorithmId,
RoleAlgorithmName,
RoleAlgorithmShortDescription,
RoleAlgorithmTags,
};

QgsProcessingToolboxModel( QObject *parent /TransferThis/ = 0, QgsProcessingRegistry *registry = 0 );
@@ -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;
@@ -223,6 +223,9 @@ of this model.
{
RoleNodeType,
RoleAlgorithmFlags,
RoleAlgorithmId,
RoleAlgorithmName,
RoleAlgorithmTags,
};

QgsProcessingToolboxModel( QObject *parent /TransferThis/ = 0, QgsProcessingRegistry *registry = 0 );
@@ -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;
@@ -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();
}
@@ -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;
@@ -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
};

/**
@@ -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;

0 comments on commit 6275960

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