Skip to content
Permalink
Browse files

Allow algorithms with empty groupId to be placed directly under provi…

…der node
  • Loading branch information
nyalldawson committed Jul 2, 2018
1 parent f6075ca commit 812df0717452d8aecdd689e07fe9cdb01f63da9b
@@ -160,7 +160,7 @@ QModelIndex QgsProcessingToolboxModel::node2index( QgsProcessingToolboxModelNode
void QgsProcessingToolboxModel::addProvider( QgsProcessingProvider *provider )
{
QgsProcessingToolboxModelNode *parentNode = nullptr;
if ( provider->id() != "qgis" && provider->id() != "native" && provider->id() != "3d" )
if ( !isTopLevelProvider( provider ) )
{
std::unique_ptr< QgsProcessingToolboxModelProviderNode > node = qgis::make_unique< QgsProcessingToolboxModelProviderNode >( provider );
parentNode = node.get();
@@ -174,24 +174,38 @@ void QgsProcessingToolboxModel::addProvider( QgsProcessingProvider *provider )
const QList< const QgsProcessingAlgorithm * > algorithms = provider->algorithms();
for ( const QgsProcessingAlgorithm *algorithm : algorithms )
{
std::unique_ptr< QgsProcessingToolboxModelAlgorithmNode > algorithmNode = qgis::make_unique< QgsProcessingToolboxModelAlgorithmNode >( algorithm );

const QString groupId = algorithm->groupId();
QgsProcessingToolboxModelGroupNode *groupNode = parentNode->getChildGroupNode( groupId );
if ( !groupNode )
if ( !groupId.isEmpty() )
{
groupNode = new QgsProcessingToolboxModelGroupNode( algorithm->groupId(), algorithm->group() );
parentNode->addChildNode( groupNode );
QgsProcessingToolboxModelGroupNode *groupNode = parentNode->getChildGroupNode( groupId );
if ( !groupNode )
{
groupNode = new QgsProcessingToolboxModelGroupNode( algorithm->groupId(), algorithm->group() );
parentNode->addChildNode( groupNode );
}
groupNode->addChildNode( algorithmNode.release() );
}
else
{
// "top level" algorithm - no group
parentNode->addChildNode( algorithmNode.release() );
}

std::unique_ptr< QgsProcessingToolboxModelAlgorithmNode > algorithmNode = qgis::make_unique< QgsProcessingToolboxModelAlgorithmNode >( algorithm );
groupNode->addChildNode( algorithmNode.release() );
}
}

bool QgsProcessingToolboxModel::isTopLevelProvider( QgsProcessingProvider *provider )
{
return provider->id() == QLatin1String( "qgis" ) ||
provider->id() == QLatin1String( "native" ) ||
provider->id() == QLatin1String( "3d" );
}

Qt::ItemFlags QgsProcessingToolboxModel::flags( const QModelIndex &index ) const
{
if ( !index.isValid() )
return 0;
return nullptr;

return QAbstractItemModel::flags( index );
}
@@ -274,7 +288,7 @@ QVariant QgsProcessingToolboxModel::data( const QModelIndex &index, int role ) c
case 0:
{
if ( algorithm )
return algorithm->flags();
return static_cast< int >( algorithm->flags() );
else
return QVariant();
}
@@ -306,6 +306,12 @@ class GUI_EXPORT QgsProcessingToolboxModel : public QAbstractItemModel

void addProvider( QgsProcessingProvider *provider );

/**
* Returns true if \a provider is a "top-level" provider, which shows
* groups directly under the root node and not under a provider child node.
*/
static bool isTopLevelProvider( QgsProcessingProvider *provider );

};


@@ -330,7 +336,7 @@ class GUI_EXPORT QgsProcessingToolboxProxyModel: public QSortFilterProxyModel
*/
explicit QgsProcessingToolboxProxyModel( QObject *parent SIP_TRANSFERTHIS = nullptr, QgsProcessingRegistry *registry = nullptr );

// bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override;
// bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override;
bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override;

private:
@@ -208,6 +208,27 @@ void TestQgsProcessingModel::testModel()
QCOMPARE( model.algorithmForIndex( model.index( 0, 0, groupIndex ) ), a3 );
QCOMPARE( model.algorithmForIndex( model.index( 1, 0, groupIndex ) ), a4 );

// provider with algs with no groups
DummyAlgorithm *a5 = new DummyAlgorithm( "a5", "group1" );
DummyAlgorithm *a6 = new DummyAlgorithm( "a6", QString() );
DummyAlgorithm *a7 = new DummyAlgorithm( "a7", "group2" );
DummyProvider *p5 = new DummyProvider( "p5", "provider5", QList< QgsProcessingAlgorithm * >() << a5 << a6 << a7 );
registry.addProvider( p5 );
QModelIndex p5ProviderIndex = model.indexForProvider( p5 );
QCOMPARE( model.rowCount( p5ProviderIndex ), 3 );

groupIndex = model.index( 0, 0, p5ProviderIndex );
QCOMPARE( model.data( groupIndex, Qt::DisplayRole ).toString(), QStringLiteral( "group1" ) );
QCOMPARE( model.rowCount( groupIndex ), 1 );
QCOMPARE( model.algorithmForIndex( model.index( 0, 0, groupIndex ) ), a5 );

QCOMPARE( model.algorithmForIndex( model.index( 1, 0, p5ProviderIndex ) ), a6 );

groupIndex = model.index( 2, 0, p5ProviderIndex );
QCOMPARE( model.data( groupIndex, Qt::DisplayRole ).toString(), QStringLiteral( "group2" ) );
QCOMPARE( model.rowCount( groupIndex ), 1 );
QCOMPARE( model.algorithmForIndex( model.index( 0, 0, groupIndex ) ), a7 );

// qgis native algorithms put groups at top level
QgsProcessingRegistry registry2;
QgsProcessingToolboxModel model2( nullptr, &registry2 );
@@ -233,6 +254,12 @@ void TestQgsProcessingModel::testModel()
QCOMPARE( model2.algorithmForIndex( model2.index( 0, 0, group2Index ) ), qgisA2 );
QCOMPARE( model2.rowCount( group3Index ), 1 );
QCOMPARE( model2.algorithmForIndex( model2.index( 0, 0, group3Index ) ), qgisA4 );

// remove a provider


// reload provider

}

void TestQgsProcessingModel::testProxyModel()

0 comments on commit 812df07

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