Skip to content
Permalink
Browse files

Safer node handling, allow specification of recent algorithm log

  • Loading branch information
nyalldawson committed Jul 16, 2018
1 parent 1235c35 commit 3e9648fcda0f5e149c173b2cc49cd8ef2ebbc541
@@ -139,7 +139,7 @@ Processing toolbox model node corresponding to a Processing provider.
%End
public:

QgsProcessingToolboxModelProviderNode( QgsProcessingProvider *provider );
QgsProcessingToolboxModelProviderNode( QgsProcessingProvider *provider, QgsProcessingRegistry *registry );
%Docstring
Constructor for QgsProcessingToolboxModelProviderNode, linked to the
specified ``provider``.
@@ -216,7 +216,7 @@ Processing toolbox model node corresponding to an algorithm.
%End
public:

QgsProcessingToolboxModelAlgorithmNode( const QgsProcessingAlgorithm *algorithm );
QgsProcessingToolboxModelAlgorithmNode( const QgsProcessingAlgorithm *algorithm, QgsProcessingRegistry *registry );
%Docstring
Constructor for QgsProcessingToolboxModelAlgorithmNode, associated
with the specified ``algorithm``.
@@ -258,14 +258,18 @@ of this model.
RoleAlgorithmTags,
};

QgsProcessingToolboxModel( QObject *parent /TransferThis/ = 0, QgsProcessingRegistry *registry = 0 );
QgsProcessingToolboxModel( QObject *parent /TransferThis/ = 0, QgsProcessingRegistry *registry = 0,
QgsProcessingRecentAlgorithmLog *recentLog = 0 );
%Docstring
Constructor for QgsProcessingToolboxModel, with the given ``parent`` object.

If ``registry`` is specified then the model will show providers and algorithms
from the given registry. If no registry is specified, then the processing
registry attached to QgsApplication.processingRegistry() will be used
by the model.

If \recentLog is specified then it will be used to create a "Recently used" top
level group containing recently used algorithms.
%End

virtual Qt::ItemFlags flags( const QModelIndex &index ) const;
@@ -71,14 +71,14 @@ void QgsProcessingToolboxModelNode::deleteChildren()
// QgsProcessingToolboxModelProviderNode
//

QgsProcessingToolboxModelProviderNode::QgsProcessingToolboxModelProviderNode( QgsProcessingProvider *provider )
QgsProcessingToolboxModelProviderNode::QgsProcessingToolboxModelProviderNode( QgsProcessingProvider *provider, QgsProcessingRegistry *registry )
: mProviderId( provider->id() )
, mProvider( provider )
, mRegistry( registry )
{}

QgsProcessingProvider *QgsProcessingToolboxModelProviderNode::provider()
{
return mProvider;
return mRegistry->providerById( mProviderId );
}

//
@@ -94,22 +94,24 @@ QgsProcessingToolboxModelGroupNode::QgsProcessingToolboxModelGroupNode( const QS
// QgsProcessingToolboxModelAlgorithmNode
//

QgsProcessingToolboxModelAlgorithmNode::QgsProcessingToolboxModelAlgorithmNode( const QgsProcessingAlgorithm *algorithm )
: mAlgorithm( algorithm )
QgsProcessingToolboxModelAlgorithmNode::QgsProcessingToolboxModelAlgorithmNode( const QgsProcessingAlgorithm *algorithm, QgsProcessingRegistry *registry )
: mAlgorithmId( algorithm->id() )
, mRegistry( registry )
{}

const QgsProcessingAlgorithm *QgsProcessingToolboxModelAlgorithmNode::algorithm() const
{
return mAlgorithm;
return mRegistry->algorithmById( mAlgorithmId );
}

//
// QgsProcessingToolboxModel
//

QgsProcessingToolboxModel::QgsProcessingToolboxModel( QObject *parent, QgsProcessingRegistry *registry )
QgsProcessingToolboxModel::QgsProcessingToolboxModel( QObject *parent, QgsProcessingRegistry *registry, QgsProcessingRecentAlgorithmLog *recentLog )
: QAbstractItemModel( parent )
, mRegistry( registry ? registry : QgsApplication::processingRegistry() )
, mRecentLog( recentLog )
, mRootNode( qgis::make_unique< QgsProcessingToolboxModelGroupNode >( QString(), QString() ) )
{
rebuild();
@@ -124,7 +126,10 @@ void QgsProcessingToolboxModel::rebuild()

mRootNode->deleteChildren();

mRootNode->addChildNode( new QgsProcessingToolboxModelRecentNode() );
if ( mRecentLog )
{
mRootNode->addChildNode( new QgsProcessingToolboxModelRecentNode() );
}

const QList< QgsProcessingProvider * > providers = mRegistry->providers();
for ( QgsProcessingProvider *provider : providers )
@@ -208,7 +213,7 @@ void QgsProcessingToolboxModel::addProvider( QgsProcessingProvider *provider )
QgsProcessingToolboxModelNode *parentNode = nullptr;
if ( !isTopLevelProvider( provider->id() ) )
{
std::unique_ptr< QgsProcessingToolboxModelProviderNode > node = qgis::make_unique< QgsProcessingToolboxModelProviderNode >( provider );
std::unique_ptr< QgsProcessingToolboxModelProviderNode > node = qgis::make_unique< QgsProcessingToolboxModelProviderNode >( provider, mRegistry );
parentNode = node.get();
mRootNode->addChildNode( node.release() );
}
@@ -220,7 +225,7 @@ 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 );
std::unique_ptr< QgsProcessingToolboxModelAlgorithmNode > algorithmNode = qgis::make_unique< QgsProcessingToolboxModelAlgorithmNode >( algorithm, mRegistry );

const QString groupId = algorithm->groupId();
if ( !groupId.isEmpty() )
@@ -25,6 +25,7 @@ class QgsProcessingRegistry;
class QgsProcessingProvider;
class QgsProcessingAlgorithm;
class QgsProcessingToolboxModelGroupNode;
class QgsProcessingRecentAlgorithmLog;

///@cond PRIVATE

@@ -161,7 +162,7 @@ class GUI_EXPORT QgsProcessingToolboxModelProviderNode : public QgsProcessingToo
* Constructor for QgsProcessingToolboxModelProviderNode, linked to the
* specified \a provider.
*/
QgsProcessingToolboxModelProviderNode( QgsProcessingProvider *provider );
QgsProcessingToolboxModelProviderNode( QgsProcessingProvider *provider, QgsProcessingRegistry *registry );

NodeType nodeType() const override { return NodeProvider; }

@@ -178,8 +179,7 @@ class GUI_EXPORT QgsProcessingToolboxModelProviderNode : public QgsProcessingToo
private:

QString mProviderId;
QgsProcessingProvider *mProvider = nullptr;

QgsProcessingRegistry *mRegistry = nullptr;
};

/**
@@ -237,7 +237,7 @@ class GUI_EXPORT QgsProcessingToolboxModelAlgorithmNode : public QgsProcessingTo
* Constructor for QgsProcessingToolboxModelAlgorithmNode, associated
* with the specified \a algorithm.
*/
QgsProcessingToolboxModelAlgorithmNode( const QgsProcessingAlgorithm *algorithm );
QgsProcessingToolboxModelAlgorithmNode( const QgsProcessingAlgorithm *algorithm, QgsProcessingRegistry *registry );

NodeType nodeType() const override { return NodeAlgorithm; }

@@ -248,7 +248,8 @@ class GUI_EXPORT QgsProcessingToolboxModelAlgorithmNode : public QgsProcessingTo

private:

const QgsProcessingAlgorithm *mAlgorithm = nullptr;
const QString mAlgorithmId;
QgsProcessingRegistry *mRegistry = nullptr;

};

@@ -287,8 +288,12 @@ class GUI_EXPORT QgsProcessingToolboxModel : public QAbstractItemModel
* from the given registry. If no registry is specified, then the processing
* registry attached to QgsApplication::processingRegistry() will be used
* by the model.
*
* If \recentLog is specified then it will be used to create a "Recently used" top
* level group containing recently used algorithms.
*/
QgsProcessingToolboxModel( QObject *parent SIP_TRANSFERTHIS = nullptr, QgsProcessingRegistry *registry = nullptr );
QgsProcessingToolboxModel( QObject *parent SIP_TRANSFERTHIS = nullptr, QgsProcessingRegistry *registry = nullptr,
QgsProcessingRecentAlgorithmLog *recentLog = nullptr );

Qt::ItemFlags flags( const QModelIndex &index ) const override;
QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
@@ -363,6 +368,7 @@ class GUI_EXPORT QgsProcessingToolboxModel : public QAbstractItemModel
private:

QgsProcessingRegistry *mRegistry = nullptr;
QgsProcessingRecentAlgorithmLog *mRecentLog = nullptr;

std::unique_ptr< QgsProcessingToolboxModelGroupNode > mRootNode;

@@ -17,6 +17,8 @@

#include "qgsprocessingregistry.h"
#include "qgsprocessingtoolboxmodel.h"
#include "qgsprocessingrecentalgorithmlog.h"

#include <QtTest/QSignalSpy>
#include "qgstest.h"
class DummyAlgorithm : public QgsProcessingAlgorithm
@@ -126,7 +128,8 @@ void TestQgsProcessingModel::cleanupTestCase()
void TestQgsProcessingModel::testModel()
{
QgsProcessingRegistry registry;
QgsProcessingToolboxModel model( nullptr, &registry );
QgsProcessingRecentAlgorithmLog recentLog;
QgsProcessingToolboxModel model( nullptr, &registry, &recentLog );

QCOMPARE( model.columnCount(), 1 );
QCOMPARE( model.rowCount(), 1 );
@@ -324,10 +327,10 @@ void TestQgsProcessingModel::testModel()
DummyProvider *qgisP = new DummyProvider( "qgis", "qgis_provider", QList< QgsProcessingAlgorithm * >() << qgisA1 << qgisA2 << qgisA3 << qgisA4 );
registry2.addProvider( qgisP );

QCOMPARE( model2.rowCount(), 4 );
group1Index = model2.index( 1, 0 );
group2Index = model2.index( 2, 0 );
QModelIndex group3Index = model2.index( 3, 0 );
QCOMPARE( model2.rowCount(), 3 );
group1Index = model2.index( 0, 0 );
group2Index = model2.index( 1, 0 );
QModelIndex group3Index = model2.index( 2, 0 );
QCOMPARE( model2.data( group1Index, Qt::DisplayRole ).toString(), QStringLiteral( "group1" ) );
QCOMPARE( model2.data( group2Index, Qt::DisplayRole ).toString(), QStringLiteral( "group2" ) );
QCOMPARE( model2.data( group3Index, Qt::DisplayRole ).toString(), QStringLiteral( "group3" ) );

0 comments on commit 3e9648f

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