Skip to content

Commit

Permalink
[processing] Generalise modeler multi-step feedback proxy for use
Browse files Browse the repository at this point in the history
outside of modeler

This can be useful too for individual algorithms
  • Loading branch information
nyalldawson committed Nov 23, 2017
1 parent 94a1e68 commit e6d86bb
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 110 deletions.
2 changes: 0 additions & 2 deletions python/core/processing/models/qgsprocessingmodelalgorithm.sip
Expand Up @@ -374,8 +374,6 @@ Translated description of variable
};




/************************************************************************
* This file has been generated automatically from *
* *
Expand Down
46 changes: 46 additions & 0 deletions python/core/processing/qgsprocessingfeedback.sip
Expand Up @@ -80,6 +80,52 @@ class QgsProcessingFeedback : QgsFeedback
};


class QgsProcessingMultiStepFeedback : QgsProcessingFeedback
{
%Docstring

Processing feedback object for multi-step operations.

A processing feedback object which proxies its calls to an underlying
feedback object, but scales overall progress reports to account
for a number of child steps which each report their own feedback.

.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgsprocessingfeedback.h"
%End
public:

QgsProcessingMultiStepFeedback( int steps, QgsProcessingFeedback *feedback );
%Docstring
Constructor for QgsProcessingMultiStepFeedback, for a process with the specified
number of ``steps``. This feedback object will proxy calls
to the specified ``feedback`` object.
%End

void setCurrentStep( int step );
%Docstring
Sets the ``step`` which is being executed. This is used
to scale the current progress to account for progress through the overall process.
%End

virtual void setProgressText( const QString &text );

virtual void reportError( const QString &error );

virtual void pushInfo( const QString &info );

virtual void pushCommandInfo( const QString &info );

virtual void pushDebugInfo( const QString &info );

virtual void pushConsoleInfo( const QString &info );


};



/************************************************************************
Expand Down
1 change: 1 addition & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -100,6 +100,7 @@ SET(QGIS_CORE_SRCS

processing/qgsprocessingalgorithm.cpp
processing/qgsprocessingalgrunnertask.cpp
processing/qgsprocessingfeedback.cpp
processing/qgsprocessingoutputs.cpp
processing/qgsprocessingparameters.cpp
processing/qgsprocessingprovider.cpp
Expand Down
62 changes: 1 addition & 61 deletions src/core/processing/models/qgsprocessingmodelalgorithm.cpp
Expand Up @@ -213,7 +213,7 @@ QVariantMap QgsProcessingModelAlgorithm::processAlgorithm( const QVariantMap &pa
QTime totalTime;
totalTime.start();

QgsProcessingModelFeedback modelFeedback( toExecute.count(), feedback );
QgsProcessingMultiStepFeedback modelFeedback( toExecute.count(), feedback );
QgsExpressionContext baseContext = createExpressionContext( parameters, context );

QVariantMap childResults;
Expand Down Expand Up @@ -1146,65 +1146,5 @@ QgsProcessingAlgorithm *QgsProcessingModelAlgorithm::createInstance() const
return alg;
}




//
// QgsProcessingModelFeedback
//

QgsProcessingModelFeedback::QgsProcessingModelFeedback( int childAlgorithmCount, QgsProcessingFeedback *feedback )
: mChildSteps( childAlgorithmCount )
, mFeedback( feedback )
{
connect( mFeedback, &QgsFeedback::canceled, this, &QgsFeedback::cancel, Qt::DirectConnection );
connect( this, &QgsFeedback::progressChanged, this, &QgsProcessingModelFeedback::updateOverallProgress );
}

void QgsProcessingModelFeedback::setCurrentStep( int step )
{
mCurrentStep = step;
mFeedback->setProgress( 100.0 * static_cast< double >( mCurrentStep ) / mChildSteps );
}

void QgsProcessingModelFeedback::setProgressText( const QString &text )
{
mFeedback->setProgressText( text );
}

void QgsProcessingModelFeedback::reportError( const QString &error )
{
mFeedback->reportError( error );
}

void QgsProcessingModelFeedback::pushInfo( const QString &info )
{
mFeedback->pushInfo( info );
}

void QgsProcessingModelFeedback::pushCommandInfo( const QString &info )
{
mFeedback->pushCommandInfo( info );
}

void QgsProcessingModelFeedback::pushDebugInfo( const QString &info )
{
mFeedback->pushDebugInfo( info );
}

void QgsProcessingModelFeedback::pushConsoleInfo( const QString &info )
{
mFeedback->pushConsoleInfo( info );
}

void QgsProcessingModelFeedback::updateOverallProgress( double progress )
{
double baseProgress = 100.0 * static_cast< double >( mCurrentStep ) / mChildSteps;
double currentAlgorithmProgress = progress / mChildSteps;
mFeedback->setProgress( baseProgress + currentAlgorithmProgress );
}



///@endcond

47 changes: 0 additions & 47 deletions src/core/processing/models/qgsprocessingmodelalgorithm.h
Expand Up @@ -403,53 +403,6 @@ class CORE_EXPORT QgsProcessingModelAlgorithm : public QgsProcessingAlgorithm
friend class TestQgsProcessing;
};


#ifndef SIP_RUN

/**
* Model algorithm feedback which proxies its calls to an underlying
* feedback object, but scales overall progress reports to account
* for the number of child steps in a model.
*/
class QgsProcessingModelFeedback : public QgsProcessingFeedback
{
Q_OBJECT

public:

/**
* Constructor for QgsProcessingModelFeedback, for a model with the specified
* number of active child algorithms. This feedback object will proxy calls
* to the specified \a feedback object.
*/
QgsProcessingModelFeedback( int childAlgorithmCount, QgsProcessingFeedback *feedback );

/**
* Sets the current child algorithm \a step which is being executed. This is used
* to scale the overall progress to account for progress through the overall model.
*/
void setCurrentStep( int step );

void setProgressText( const QString &text ) override;
void reportError( const QString &error ) override;
void pushInfo( const QString &info ) override;
void pushCommandInfo( const QString &info ) override;
void pushDebugInfo( const QString &info ) override;
void pushConsoleInfo( const QString &info ) override;

private slots:

void updateOverallProgress( double progress );

private:

int mChildSteps = 0;
int mCurrentStep = 0;
QgsProcessingFeedback *mFeedback = nullptr;
};

#endif

///@endcond

#endif // QGSPROCESSINGMODELALGORITHM_H
71 changes: 71 additions & 0 deletions src/core/processing/qgsprocessingfeedback.cpp
@@ -0,0 +1,71 @@
/***************************************************************************
qgsprocessingfeedback.cpp
-------------------------
begin : June 2017
copyright : (C) 2017 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsprocessingfeedback.h"

QgsProcessingMultiStepFeedback::QgsProcessingMultiStepFeedback( int childAlgorithmCount, QgsProcessingFeedback *feedback )
: mChildSteps( childAlgorithmCount )
, mFeedback( feedback )
{
connect( mFeedback, &QgsFeedback::canceled, this, &QgsFeedback::cancel, Qt::DirectConnection );
connect( this, &QgsFeedback::progressChanged, this, &QgsProcessingMultiStepFeedback::updateOverallProgress );
}

void QgsProcessingMultiStepFeedback::setCurrentStep( int step )
{
mCurrentStep = step;
mFeedback->setProgress( 100.0 * static_cast< double >( mCurrentStep ) / mChildSteps );
}

void QgsProcessingMultiStepFeedback::setProgressText( const QString &text )
{
mFeedback->setProgressText( text );
}

void QgsProcessingMultiStepFeedback::reportError( const QString &error )
{
mFeedback->reportError( error );
}

void QgsProcessingMultiStepFeedback::pushInfo( const QString &info )
{
mFeedback->pushInfo( info );
}

void QgsProcessingMultiStepFeedback::pushCommandInfo( const QString &info )
{
mFeedback->pushCommandInfo( info );
}

void QgsProcessingMultiStepFeedback::pushDebugInfo( const QString &info )
{
mFeedback->pushDebugInfo( info );
}

void QgsProcessingMultiStepFeedback::pushConsoleInfo( const QString &info )
{
mFeedback->pushConsoleInfo( info );
}

void QgsProcessingMultiStepFeedback::updateOverallProgress( double progress )
{
double baseProgress = 100.0 * static_cast< double >( mCurrentStep ) / mChildSteps;
double currentAlgorithmProgress = progress / mChildSteps;
mFeedback->setProgress( baseProgress + currentAlgorithmProgress );
}


49 changes: 49 additions & 0 deletions src/core/processing/qgsprocessingfeedback.h
Expand Up @@ -93,6 +93,55 @@ class CORE_EXPORT QgsProcessingFeedback : public QgsFeedback
};


/**
* \class QgsProcessingMultiStepFeedback
* \ingroup core
*
* Processing feedback object for multi-step operations.
*
* A processing feedback object which proxies its calls to an underlying
* feedback object, but scales overall progress reports to account
* for a number of child steps which each report their own feedback.
*
* \since QGIS 3.0
*/
class CORE_EXPORT QgsProcessingMultiStepFeedback : public QgsProcessingFeedback
{
Q_OBJECT

public:

/**
* Constructor for QgsProcessingMultiStepFeedback, for a process with the specified
* number of \a steps. This feedback object will proxy calls
* to the specified \a feedback object.
*/
QgsProcessingMultiStepFeedback( int steps, QgsProcessingFeedback *feedback );

/**
* Sets the \a step which is being executed. This is used
* to scale the current progress to account for progress through the overall process.
*/
void setCurrentStep( int step );

void setProgressText( const QString &text ) override;
void reportError( const QString &error ) override;
void pushInfo( const QString &info ) override;
void pushCommandInfo( const QString &info ) override;
void pushDebugInfo( const QString &info ) override;
void pushConsoleInfo( const QString &info ) override;

private slots:

void updateOverallProgress( double progress );

private:

int mChildSteps = 0;
int mCurrentStep = 0;
QgsProcessingFeedback *mFeedback = nullptr;
};

#endif // QGSPROCESSINGFEEDBACK_H


0 comments on commit e6d86bb

Please sign in to comment.