Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix sorting of recently used algorithms
  • Loading branch information
nyalldawson committed Jul 16, 2018
1 parent ba41062 commit e304539
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 8 deletions.
Expand Up @@ -346,6 +346,13 @@ Returns the index corresponding to the specified ``providerId``.
QModelIndex indexOfParentTreeNode( QgsProcessingToolboxModelNode *parentNode ) const;
%Docstring
Returns the index corresponding to the parent of a given node.
%End

signals:

void recentAlgorithmAdded();
%Docstring
Emitted whenever recent algorithms are added to the model.
%End

};
Expand Down
30 changes: 25 additions & 5 deletions src/gui/processing/qgsprocessingtoolboxmodel.cpp
Expand Up @@ -135,7 +135,7 @@ void QgsProcessingToolboxModel::rebuild()
std::unique_ptr< QgsProcessingToolboxModelRecentNode > recentNode = qgis::make_unique< QgsProcessingToolboxModelRecentNode >();
mRecentNode = recentNode.get();
mRootNode->addChildNode( recentNode.release() );
repopulateRecentAlgorithms();
repopulateRecentAlgorithms( true );
}

const QList< QgsProcessingProvider * > providers = mRegistry->providers();
Expand Down Expand Up @@ -171,7 +171,10 @@ void QgsProcessingToolboxModel::repopulateRecentAlgorithms( bool resetting )
}

if ( recentAlgorithms.empty() )
{
emit recentAlgorithmAdded();
return;
}

if ( !resetting )
{
Expand All @@ -180,15 +183,15 @@ void QgsProcessingToolboxModel::repopulateRecentAlgorithms( bool resetting )

for ( const QgsProcessingAlgorithm *algorithm : qgis::as_const( recentAlgorithms ) )
{
std::unique_ptr< QgsProcessingToolboxModelAlgorithmNode > algorithmNode = qgis::make_unique< QgsProcessingToolboxModelAlgorithmNode >( algorithm, mRegistry );
std::unique_ptr< QgsProcessingToolboxModelAlgorithmNode > algorithmNode = qgis::make_unique< QgsProcessingToolboxModelAlgorithmNode >( algorithm );
mRecentNode->addChildNode( algorithmNode.release() );
}

if ( !resetting )
{
endInsertRows();
}

emit recentAlgorithmAdded();
}

void QgsProcessingToolboxModel::providerAdded( const QString &id )
Expand Down Expand Up @@ -615,6 +618,8 @@ QgsProcessingToolboxProxyModel::QgsProcessingToolboxProxyModel( QObject *parent,
setSortLocaleAware( true );
setFilterCaseSensitivity( Qt::CaseInsensitive );
sort( 0 );

connect( mModel, &QgsProcessingToolboxModel::recentAlgorithmAdded, this, [ = ] { invalidateFilter(); } );
}

void QgsProcessingToolboxProxyModel::setFilters( QgsProcessingToolboxProxyModel::Filters filters )
Expand Down Expand Up @@ -701,8 +706,6 @@ bool QgsProcessingToolboxProxyModel::filterAcceptsRow( int sourceRow, const QMod
}
}

bool isRecentNode = mModel->data( sourceIndex, QgsProcessingToolboxModel::RoleNodeType ).toInt() == QgsProcessingToolboxModelNode::NodeRecent;

if ( QgsProcessingProvider *provider = mModel->providerForIndex( sourceIndex ) )
{
return hasChildren && provider->isActive();
Expand Down Expand Up @@ -735,6 +738,23 @@ bool QgsProcessingToolboxProxyModel::lessThan( const QModelIndex &left, const QM
return true;
}

// if node represents a recent algorithm, it's not sorted at all
bool isRecentNode = false;
QModelIndex parent = left.parent();
while ( parent.isValid() )
{
if ( mModel->data( parent, QgsProcessingToolboxModel::RoleNodeType ).toInt() == QgsProcessingToolboxModelNode::NodeRecent )
{
isRecentNode = true;
break;
}
}
if ( isRecentNode )
{
return left.row() < right.row();
}


// default mode is alphabetical order
QString leftStr = sourceModel()->data( left ).toString();
QString rightStr = sourceModel()->data( right ).toString();
Expand Down
7 changes: 7 additions & 0 deletions src/gui/processing/qgsprocessingtoolboxmodel.h
Expand Up @@ -359,6 +359,13 @@ class GUI_EXPORT QgsProcessingToolboxModel : public QAbstractItemModel
*/
QModelIndex indexOfParentTreeNode( QgsProcessingToolboxModelNode *parentNode ) const;

signals:

/**
* Emitted whenever recent algorithms are added to the model.
*/
void recentAlgorithmAdded();

private slots:

void rebuild();
Expand Down
22 changes: 19 additions & 3 deletions tests/src/gui/testqgsprocessingmodel.cpp
Expand Up @@ -519,18 +519,34 @@ void TestQgsProcessingModel::testProxyModel()
QCOMPARE( model.rowCount(), 4 );

// check sort order of recent algorithms
recentLog.push( QStringLiteral( "qgis:a1" ) );
recentLog.push( QStringLiteral( "p2:a1" ) );
QCOMPARE( model.rowCount(), 5 );
QModelIndex recentIndex = model.index( 0, 0, QModelIndex() );
QCOMPARE( model.data( recentIndex, Qt::DisplayRole ).toString(), QStringLiteral( "Recently used" ) );
QCOMPARE( model.rowCount( recentIndex ), 1 );
QCOMPARE( model.data( model.index( 0, 0, recentIndex ), QgsProcessingToolboxModel::RoleAlgorithmId ).toString(), QStringLiteral( "p2:a1" ) );
recentLog.push( QStringLiteral( "p1:a2" ) );
QCOMPARE( model.rowCount( recentIndex ), 2 );
QCOMPARE( model.data( model.index( 0, 0, recentIndex ), QgsProcessingToolboxModel::RoleAlgorithmId ).toString(), QStringLiteral( "p1:a2" ) );
QCOMPARE( model.data( model.index( 1, 0, recentIndex ), QgsProcessingToolboxModel::RoleAlgorithmId ).toString(), QStringLiteral( "p2:a1" ) );
recentLog.push( QStringLiteral( "qgis:a1" ) );
QCOMPARE( model.rowCount( recentIndex ), 3 );
QCOMPARE( model.data( model.index( 0, 0, recentIndex ), QgsProcessingToolboxModel::RoleAlgorithmId ).toString(), QStringLiteral( "qgis:a1" ) );
QCOMPARE( model.data( model.index( 1, 0, recentIndex ), QgsProcessingToolboxModel::RoleAlgorithmId ).toString(), QStringLiteral( "p1:a2" ) );
QCOMPARE( model.data( model.index( 2, 0, recentIndex ), QgsProcessingToolboxModel::RoleAlgorithmId ).toString(), QStringLiteral( "p2:a1" ) );
recentLog.push( QStringLiteral( "p2:a1" ) );
QCOMPARE( model.rowCount( recentIndex ), 3 );
QCOMPARE( model.data( model.index( 0, 0, recentIndex ), QgsProcessingToolboxModel::RoleAlgorithmId ).toString(), QStringLiteral( "p2:a1" ) );
QCOMPARE( model.data( model.index( 1, 0, recentIndex ), QgsProcessingToolboxModel::RoleAlgorithmId ).toString(), QStringLiteral( "qgis:a1" ) );
QCOMPARE( model.data( model.index( 2, 0, recentIndex ), QgsProcessingToolboxModel::RoleAlgorithmId ).toString(), QStringLiteral( "p1:a2" ) );

// inactive provider - should not be visible
QCOMPARE( model.rowCount(), 4 );
QCOMPARE( model.rowCount(), 5 );
DummyAlgorithm *qgisA31 = new DummyAlgorithm( "a3", "group1" );
DummyProvider *p3 = new DummyProvider( "p3", "provider3", QList< QgsProcessingAlgorithm * >() << qgisA31 );
p3->mActive = false;
registry.addProvider( p3 );
QCOMPARE( model.rowCount(), 4 );
QCOMPARE( model.rowCount(), 5 );
}

QGSTEST_MAIN( TestQgsProcessingModel )
Expand Down

0 comments on commit e304539

Please sign in to comment.