diff --git a/src/gui/processing/qgsprocessingtoolboxmodel.cpp b/src/gui/processing/qgsprocessingtoolboxmodel.cpp index caccbbe4cca8..c561d9c214bb 100644 --- a/src/gui/processing/qgsprocessingtoolboxmodel.cpp +++ b/src/gui/processing/qgsprocessingtoolboxmodel.cpp @@ -169,12 +169,15 @@ void QgsProcessingToolboxModel::repopulateRecentAlgorithms( bool resetting ) recentAlgorithms << algorithm; } - if ( !resetting && !recentAlgorithms.empty() ) + if ( recentAlgorithms.empty() ) + return; + + if ( !resetting ) { beginInsertRows( index( 0, 0 ), 0, recentAlgorithms.count() - 1 ); } - for ( const QgsProcessingAlgorithm *algorithm : recentAlgorithms ) + for ( const QgsProcessingAlgorithm *algorithm : qgis::as_const( recentAlgorithms ) ) { std::unique_ptr< QgsProcessingToolboxModelAlgorithmNode > algorithmNode = qgis::make_unique< QgsProcessingToolboxModelAlgorithmNode >( algorithm, mRegistry ); mRecentNode->addChildNode( algorithmNode.release() ); @@ -230,6 +233,8 @@ void QgsProcessingToolboxModel::providerRemoved( const QString &id ) beginRemoveRows( QModelIndex(), index.row(), index.row() ); delete mRootNode->takeChild( node ); endRemoveRows(); + + repopulateRecentAlgorithms(); } } diff --git a/tests/src/gui/testqgsprocessingmodel.cpp b/tests/src/gui/testqgsprocessingmodel.cpp index ff207f606650..acb92940bf7b 100644 --- a/tests/src/gui/testqgsprocessingmodel.cpp +++ b/tests/src/gui/testqgsprocessingmodel.cpp @@ -18,6 +18,7 @@ #include "qgsprocessingregistry.h" #include "qgsprocessingtoolboxmodel.h" #include "qgsprocessingrecentalgorithmlog.h" +#include "qgssettings.h" #include #include "qgstest.h" @@ -118,6 +119,8 @@ void TestQgsProcessingModel::initTestCase() QCoreApplication::setOrganizationName( QStringLiteral( "QGIS" ) ); QCoreApplication::setOrganizationDomain( QStringLiteral( "qgis.org" ) ); QCoreApplication::setApplicationName( QStringLiteral( "QGIS-TEST" ) ); + + QgsSettings().clear(); } void TestQgsProcessingModel::cleanupTestCase() @@ -135,6 +138,7 @@ void TestQgsProcessingModel::testModel() QCOMPARE( model.rowCount(), 1 ); QVERIFY( model.hasChildren() ); QCOMPARE( model.data( model.index( 0, 0, QModelIndex() ), Qt::DisplayRole ).toString(), QStringLiteral( "Recently used" ) ); + QCOMPARE( model.rowCount( model.index( 0, 0, QModelIndex() ) ), 0 ); QVERIFY( !model.providerForIndex( model.index( 0, 0, QModelIndex() ) ) ); QVERIFY( !model.providerForIndex( model.index( 1, 0, QModelIndex() ) ) ); QVERIFY( !model.indexForProvider( nullptr ).isValid() ); @@ -296,6 +300,21 @@ void TestQgsProcessingModel::testModel() alg2Index = model.index( 0, 0, group2Index ); QCOMPARE( model.algorithmForIndex( alg2Index )->id(), QStringLiteral( "p3:a2" ) ); + // recent algorithms + QModelIndex recentIndex = model.index( 0, 0, QModelIndex() ); + QCOMPARE( model.data( recentIndex, Qt::DisplayRole ).toString(), QStringLiteral( "Recently used" ) ); + QCOMPARE( model.rowCount( recentIndex ), 0 ); + recentLog.push( QStringLiteral( "p5:a5" ) ); + QCOMPARE( model.rowCount( recentIndex ), 1 ); + QCOMPARE( model.data( model.index( 0, 0, recentIndex ), QgsProcessingToolboxModel::RoleAlgorithmId ).toString(), QStringLiteral( "p5:a5" ) ); + recentLog.push( QStringLiteral( "not valid" ) ); + QCOMPARE( model.rowCount( recentIndex ), 1 ); + QCOMPARE( model.data( model.index( 0, 0, recentIndex ), QgsProcessingToolboxModel::RoleAlgorithmId ).toString(), QStringLiteral( "p5:a5" ) ); + recentLog.push( QStringLiteral( "p4:a3" ) ); + QCOMPARE( model.rowCount( recentIndex ), 2 ); + QCOMPARE( model.data( model.index( 0, 0, recentIndex ), QgsProcessingToolboxModel::RoleAlgorithmId ).toString(), QStringLiteral( "p4:a3" ) ); + QCOMPARE( model.data( model.index( 1, 0, recentIndex ), QgsProcessingToolboxModel::RoleAlgorithmId ).toString(), QStringLiteral( "p5:a5" ) ); + // remove a provider registry.removeProvider( p1 ); QCOMPARE( model.rowCount(), 5 ); @@ -304,11 +323,18 @@ void TestQgsProcessingModel::testModel() QCOMPARE( model.data( model.index( 1, 0, QModelIndex() ), Qt::DisplayRole ).toString(), QStringLiteral( "provider2" ) ); registry.removeProvider( p5 ); QCOMPARE( model.rowCount(), 4 ); + recentIndex = model.index( 0, 0, QModelIndex() ); + QCOMPARE( model.rowCount( recentIndex ), 1 ); + QCOMPARE( model.data( model.index( 0, 0, recentIndex ), QgsProcessingToolboxModel::RoleAlgorithmId ).toString(), QStringLiteral( "p4:a3" ) ); registry.removeProvider( p2 ); QCOMPARE( model.rowCount(), 3 ); registry.removeProvider( p3 ); QCOMPARE( model.rowCount(), 2 ); + recentIndex = model.index( 0, 0, QModelIndex() ); + QCOMPARE( model.rowCount( recentIndex ), 1 ); registry.removeProvider( p4 ); + recentIndex = model.index( 0, 0, QModelIndex() ); + QCOMPARE( model.rowCount( recentIndex ), 0 ); QCOMPARE( model.rowCount(), 1 ); QCOMPARE( model.columnCount(), 1 ); QVERIFY( model.hasChildren() );