Skip to content

Commit e304539

Browse files
committedJul 16, 2018
Fix sorting of recently used algorithms
1 parent ba41062 commit e304539

File tree

4 files changed

+58
-8
lines changed

4 files changed

+58
-8
lines changed
 

‎python/gui/auto_generated/processing/qgsprocessingtoolboxmodel.sip.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,13 @@ Returns the index corresponding to the specified ``providerId``.
346346
QModelIndex indexOfParentTreeNode( QgsProcessingToolboxModelNode *parentNode ) const;
347347
%Docstring
348348
Returns the index corresponding to the parent of a given node.
349+
%End
350+
351+
signals:
352+
353+
void recentAlgorithmAdded();
354+
%Docstring
355+
Emitted whenever recent algorithms are added to the model.
349356
%End
350357

351358
};

‎src/gui/processing/qgsprocessingtoolboxmodel.cpp

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ void QgsProcessingToolboxModel::rebuild()
135135
std::unique_ptr< QgsProcessingToolboxModelRecentNode > recentNode = qgis::make_unique< QgsProcessingToolboxModelRecentNode >();
136136
mRecentNode = recentNode.get();
137137
mRootNode->addChildNode( recentNode.release() );
138-
repopulateRecentAlgorithms();
138+
repopulateRecentAlgorithms( true );
139139
}
140140

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

173173
if ( recentAlgorithms.empty() )
174+
{
175+
emit recentAlgorithmAdded();
174176
return;
177+
}
175178

176179
if ( !resetting )
177180
{
@@ -180,15 +183,15 @@ void QgsProcessingToolboxModel::repopulateRecentAlgorithms( bool resetting )
180183

181184
for ( const QgsProcessingAlgorithm *algorithm : qgis::as_const( recentAlgorithms ) )
182185
{
183-
std::unique_ptr< QgsProcessingToolboxModelAlgorithmNode > algorithmNode = qgis::make_unique< QgsProcessingToolboxModelAlgorithmNode >( algorithm, mRegistry );
186+
std::unique_ptr< QgsProcessingToolboxModelAlgorithmNode > algorithmNode = qgis::make_unique< QgsProcessingToolboxModelAlgorithmNode >( algorithm );
184187
mRecentNode->addChildNode( algorithmNode.release() );
185188
}
186189

187190
if ( !resetting )
188191
{
189192
endInsertRows();
190193
}
191-
194+
emit recentAlgorithmAdded();
192195
}
193196

194197
void QgsProcessingToolboxModel::providerAdded( const QString &id )
@@ -615,6 +618,8 @@ QgsProcessingToolboxProxyModel::QgsProcessingToolboxProxyModel( QObject *parent,
615618
setSortLocaleAware( true );
616619
setFilterCaseSensitivity( Qt::CaseInsensitive );
617620
sort( 0 );
621+
622+
connect( mModel, &QgsProcessingToolboxModel::recentAlgorithmAdded, this, [ = ] { invalidateFilter(); } );
618623
}
619624

620625
void QgsProcessingToolboxProxyModel::setFilters( QgsProcessingToolboxProxyModel::Filters filters )
@@ -701,8 +706,6 @@ bool QgsProcessingToolboxProxyModel::filterAcceptsRow( int sourceRow, const QMod
701706
}
702707
}
703708

704-
bool isRecentNode = mModel->data( sourceIndex, QgsProcessingToolboxModel::RoleNodeType ).toInt() == QgsProcessingToolboxModelNode::NodeRecent;
705-
706709
if ( QgsProcessingProvider *provider = mModel->providerForIndex( sourceIndex ) )
707710
{
708711
return hasChildren && provider->isActive();
@@ -735,6 +738,23 @@ bool QgsProcessingToolboxProxyModel::lessThan( const QModelIndex &left, const QM
735738
return true;
736739
}
737740

741+
// if node represents a recent algorithm, it's not sorted at all
742+
bool isRecentNode = false;
743+
QModelIndex parent = left.parent();
744+
while ( parent.isValid() )
745+
{
746+
if ( mModel->data( parent, QgsProcessingToolboxModel::RoleNodeType ).toInt() == QgsProcessingToolboxModelNode::NodeRecent )
747+
{
748+
isRecentNode = true;
749+
break;
750+
}
751+
}
752+
if ( isRecentNode )
753+
{
754+
return left.row() < right.row();
755+
}
756+
757+
738758
// default mode is alphabetical order
739759
QString leftStr = sourceModel()->data( left ).toString();
740760
QString rightStr = sourceModel()->data( right ).toString();

‎src/gui/processing/qgsprocessingtoolboxmodel.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,13 @@ class GUI_EXPORT QgsProcessingToolboxModel : public QAbstractItemModel
359359
*/
360360
QModelIndex indexOfParentTreeNode( QgsProcessingToolboxModelNode *parentNode ) const;
361361

362+
signals:
363+
364+
/**
365+
* Emitted whenever recent algorithms are added to the model.
366+
*/
367+
void recentAlgorithmAdded();
368+
362369
private slots:
363370

364371
void rebuild();

‎tests/src/gui/testqgsprocessingmodel.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -519,18 +519,34 @@ void TestQgsProcessingModel::testProxyModel()
519519
QCOMPARE( model.rowCount(), 4 );
520520

521521
// check sort order of recent algorithms
522-
recentLog.push( QStringLiteral( "qgis:a1" ) );
522+
recentLog.push( QStringLiteral( "p2:a1" ) );
523523
QCOMPARE( model.rowCount(), 5 );
524524
QModelIndex recentIndex = model.index( 0, 0, QModelIndex() );
525525
QCOMPARE( model.data( recentIndex, Qt::DisplayRole ).toString(), QStringLiteral( "Recently used" ) );
526+
QCOMPARE( model.rowCount( recentIndex ), 1 );
527+
QCOMPARE( model.data( model.index( 0, 0, recentIndex ), QgsProcessingToolboxModel::RoleAlgorithmId ).toString(), QStringLiteral( "p2:a1" ) );
528+
recentLog.push( QStringLiteral( "p1:a2" ) );
529+
QCOMPARE( model.rowCount( recentIndex ), 2 );
530+
QCOMPARE( model.data( model.index( 0, 0, recentIndex ), QgsProcessingToolboxModel::RoleAlgorithmId ).toString(), QStringLiteral( "p1:a2" ) );
531+
QCOMPARE( model.data( model.index( 1, 0, recentIndex ), QgsProcessingToolboxModel::RoleAlgorithmId ).toString(), QStringLiteral( "p2:a1" ) );
532+
recentLog.push( QStringLiteral( "qgis:a1" ) );
533+
QCOMPARE( model.rowCount( recentIndex ), 3 );
534+
QCOMPARE( model.data( model.index( 0, 0, recentIndex ), QgsProcessingToolboxModel::RoleAlgorithmId ).toString(), QStringLiteral( "qgis:a1" ) );
535+
QCOMPARE( model.data( model.index( 1, 0, recentIndex ), QgsProcessingToolboxModel::RoleAlgorithmId ).toString(), QStringLiteral( "p1:a2" ) );
536+
QCOMPARE( model.data( model.index( 2, 0, recentIndex ), QgsProcessingToolboxModel::RoleAlgorithmId ).toString(), QStringLiteral( "p2:a1" ) );
537+
recentLog.push( QStringLiteral( "p2:a1" ) );
538+
QCOMPARE( model.rowCount( recentIndex ), 3 );
539+
QCOMPARE( model.data( model.index( 0, 0, recentIndex ), QgsProcessingToolboxModel::RoleAlgorithmId ).toString(), QStringLiteral( "p2:a1" ) );
540+
QCOMPARE( model.data( model.index( 1, 0, recentIndex ), QgsProcessingToolboxModel::RoleAlgorithmId ).toString(), QStringLiteral( "qgis:a1" ) );
541+
QCOMPARE( model.data( model.index( 2, 0, recentIndex ), QgsProcessingToolboxModel::RoleAlgorithmId ).toString(), QStringLiteral( "p1:a2" ) );
526542

527543
// inactive provider - should not be visible
528-
QCOMPARE( model.rowCount(), 4 );
544+
QCOMPARE( model.rowCount(), 5 );
529545
DummyAlgorithm *qgisA31 = new DummyAlgorithm( "a3", "group1" );
530546
DummyProvider *p3 = new DummyProvider( "p3", "provider3", QList< QgsProcessingAlgorithm * >() << qgisA31 );
531547
p3->mActive = false;
532548
registry.addProvider( p3 );
533-
QCOMPARE( model.rowCount(), 4 );
549+
QCOMPARE( model.rowCount(), 5 );
534550
}
535551

536552
QGSTEST_MAIN( TestQgsProcessingModel )

0 commit comments

Comments
 (0)