Skip to content

Commit

Permalink
Port more model to c++
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 20, 2017
1 parent 4592441 commit 179a377
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 38 deletions.
19 changes: 19 additions & 0 deletions python/core/processing/qgsprocessingmodelalgorithm.sip
Expand Up @@ -541,6 +541,25 @@ Copies are protected to avoid slicing
Returns true if the algorithm could be removed, or false
if the algorithm could not be removed (e.g. due to other
child algorithms depending on it).
.. seealso:: deactivateChildAlgorithm()
:rtype: bool
%End

void deactivateChildAlgorithm( const QString &id );
%Docstring
Deactivates the child algorithm with matching ``id``.
All other child algorithms which depend on the child
algorithm will also be deactivated.
.. seealso:: removeChildAlgorithm()
.. seealso:: activateChildAlgorithm()
%End

bool activateChildAlgorithm( const QString &id );
%Docstring
Attempts to activate the child algorithm with matching ``id``.
If any child algorithms on which the child depends are not active,
then the child will not be activated and false will be returned.
.. seealso:: deactivateChildAlgorithm()
:rtype: bool
%End

Expand Down
35 changes: 0 additions & 35 deletions python/plugins/processing/modeler/ModelerAlgorithm.py
Expand Up @@ -185,19 +185,6 @@ def __init__(self):
# Geoalgorithms in this model. A dict of Algorithm objects, with names as keys
self.algs = {}

def updateAlgorithm(self, alg):
alg.setPosition(self.childAlgorithm(alg.childId()).position())
alg.setParametersCollapsed(self.childAlgorithm(alg.childId()).parametersCollapsed())
alg.setOutputsCollapsed(self.childAlgorithm(alg.childId()).outputsCollapsed())
self.setChildAlgorithm(alg)

from processing.modeler.ModelerGraphicItem import ModelerGraphicItem
for i, out in enumerate(alg.modelOutputs().keys()):
alg.modelOutput(out).setPosition(alg.modelOutput(out).position() or
alg.position() + QPointF(
ModelerGraphicItem.BOX_WIDTH,
(i + 1.5) * ModelerGraphicItem.BOX_HEIGHT))

def prepareAlgorithm(self, alg):
algInstance = alg.algorithm()
for param in algInstance.parameterDefinitions():
Expand Down Expand Up @@ -235,20 +222,6 @@ def prepareAlgorithm(self, alg):

return algInstance

def deactivateAlgorithm(self, algName):
dependent = self.dependentChildAlgorithms(algName)
for alg in dependent:
self.algs[alg].setActive(False)
self.algs[algName].setActive(False)

def activateAlgorithm(self, algName):
parents = self.dependsOnChildAlgorithms(algName)
for alg in parents:
if not self.childAlgorithm(alg).isActive():
return False
self.childAlgorithm(algName).setActive(True)
return True

def getSafeNameForOutput(self, algName, outName):
return outName + '_ALG' + algName

Expand Down Expand Up @@ -328,14 +301,6 @@ def shortHelpString(self):
return str(self.helpContent['ALG_DESC'])
return None

def getParameterDescriptions(self):
descs = {}
descriptions = self.helpContent
for param in self.parameters:
if param.name in descriptions:
descs[param.name] = str(descriptions[param.name])
return descs

def todict(self):
keys = ["inputs", "_group", "_name", "algs", "helpContent"]
return {k: v for k, v in list(self.__dict__.items()) if k in keys}
Expand Down
18 changes: 15 additions & 3 deletions python/plugins/processing/modeler/ModelerGraphicItem.py
Expand Up @@ -177,11 +177,11 @@ def contextMenuEvent(self, event):
popupmenu.exec_(event.screenPos())

def deactivateAlgorithm(self):
self.model.deactivateAlgorithm(self.element.childId())
self.model.deactivateChildAlgorithm(self.element.childId())
self.scene.dialog.repaintModel()

def activateAlgorithm(self):
if self.model.activateAlgorithm(self.element.childId()):
if self.model.activateChildAlgorithm(self.element.childId()):
self.scene.dialog.repaintModel()
else:
QMessageBox.warning(None, 'Could not activate Algorithm',
Expand Down Expand Up @@ -211,9 +211,21 @@ def editElement(self):
dlg.exec_()
if dlg.alg is not None:
dlg.alg.setChildId(self.element.childId())
self.model.updateAlgorithm(dlg.alg)
self.updateAlgorithm(dlg.alg)
self.scene.dialog.repaintModel()

def updateAlgorithm(self, alg):
existing_child = self.model.childAlgorithm(alg.childId())
alg.setPosition(existing_child.position())
alg.setParametersCollapsed(existing_child.parametersCollapsed())
alg.setOutputsCollapsed(existing_child.outputsCollapsed())
for i, out in enumerate(alg.modelOutputs().keys()):
alg.modelOutput(out).setPosition(alg.modelOutput(out).position() or
alg.position() + QPointF(
ModelerGraphicItem.BOX_WIDTH,
(i + 1.5) * ModelerGraphicItem.BOX_HEIGHT))
self.model.setChildAlgorithm(alg)

def removeElement(self):
if isinstance(self.element, QgsProcessingModelAlgorithm.ModelParameter):
if not self.model.removeModelParameter(self.element.parameterName()):
Expand Down
20 changes: 20 additions & 0 deletions src/core/processing/qgsprocessingmodelalgorithm.cpp
Expand Up @@ -260,6 +260,26 @@ bool QgsProcessingModelAlgorithm::removeChildAlgorithm( const QString &id )
return true;
}

void QgsProcessingModelAlgorithm::deactivateChildAlgorithm( const QString &id )
{
Q_FOREACH ( const QString &child, dependentChildAlgorithms( id ) )
{
childAlgorithm( child ).setActive( false );
}
childAlgorithm( id ).setActive( false );
}

bool QgsProcessingModelAlgorithm::activateChildAlgorithm( const QString &id )
{
Q_FOREACH ( const QString &child, dependsOnChildAlgorithms( id ) )
{
if ( !childAlgorithm( child ).isActive() )
return false;
}
childAlgorithm( id ).setActive( true );
return true;
}

void QgsProcessingModelAlgorithm::addModelParameter( QgsProcessingParameterDefinition *definition, const QgsProcessingModelAlgorithm::ModelParameter &component )
{
addParameter( definition );
Expand Down
18 changes: 18 additions & 0 deletions src/core/processing/qgsprocessingmodelalgorithm.h
Expand Up @@ -547,9 +547,27 @@ class CORE_EXPORT QgsProcessingModelAlgorithm : public QgsProcessingAlgorithm
* Returns true if the algorithm could be removed, or false
* if the algorithm could not be removed (e.g. due to other
* child algorithms depending on it).
* \see deactivateChildAlgorithm()
*/
bool removeChildAlgorithm( const QString &id );

/**
* Deactivates the child algorithm with matching \a id.
* All other child algorithms which depend on the child
* algorithm will also be deactivated.
* \see removeChildAlgorithm()
* \see activateChildAlgorithm()
*/
void deactivateChildAlgorithm( const QString &id );

/**
* Attempts to activate the child algorithm with matching \a id.
* If any child algorithms on which the child depends are not active,
* then the child will not be activated and false will be returned.
* \see deactivateChildAlgorithm()
*/
bool activateChildAlgorithm( const QString &id );

/**
* Returns a list of the child algorithm IDs depending on the child
* algorithm with the specified \a childId.
Expand Down
43 changes: 43 additions & 0 deletions tests/src/core/testqgsprocessing.cpp
Expand Up @@ -3226,6 +3226,49 @@ void TestQgsProcessing::modelerAlgorithm()
QVERIFY( alg3.dependsOnChildAlgorithms( "c9" ).contains( "c7" ) );
QVERIFY( alg3.dependsOnChildAlgorithms( "c9" ).contains( "c8" ) );

// (de)activate child algorithm
alg3.deactivateChildAlgorithm( "c9" );
QVERIFY( !alg3.childAlgorithm( "c9" ).isActive() );
QVERIFY( alg3.activateChildAlgorithm( "c9" ) );
QVERIFY( alg3.childAlgorithm( "c9" ).isActive() );
alg3.deactivateChildAlgorithm( "c8" );
QVERIFY( !alg3.childAlgorithm( "c9" ).isActive() );
QVERIFY( !alg3.childAlgorithm( "c8" ).isActive() );
QVERIFY( !alg3.activateChildAlgorithm( "c9" ) );
QVERIFY( !alg3.childAlgorithm( "c9" ).isActive() );
QVERIFY( !alg3.childAlgorithm( "c8" ).isActive() );
QVERIFY( alg3.activateChildAlgorithm( "c8" ) );
QVERIFY( !alg3.childAlgorithm( "c9" ).isActive() );
QVERIFY( alg3.childAlgorithm( "c8" ).isActive() );
QVERIFY( alg3.activateChildAlgorithm( "c9" ) );
QVERIFY( alg3.childAlgorithm( "c9" ).isActive() );
QVERIFY( alg3.childAlgorithm( "c8" ).isActive() );
alg3.deactivateChildAlgorithm( "c7" );
QVERIFY( !alg3.childAlgorithm( "c9" ).isActive() );
QVERIFY( !alg3.childAlgorithm( "c8" ).isActive() );
QVERIFY( !alg3.childAlgorithm( "c7" ).isActive() );
QVERIFY( !alg3.activateChildAlgorithm( "c9" ) );
QVERIFY( !alg3.activateChildAlgorithm( "c8" ) );
QVERIFY( !alg3.childAlgorithm( "c9" ).isActive() );
QVERIFY( !alg3.childAlgorithm( "c8" ).isActive() );
QVERIFY( !alg3.childAlgorithm( "c7" ).isActive() );
QVERIFY( !alg3.activateChildAlgorithm( "c8" ) );
QVERIFY( alg3.activateChildAlgorithm( "c7" ) );
QVERIFY( !alg3.childAlgorithm( "c9" ).isActive() );
QVERIFY( !alg3.childAlgorithm( "c8" ).isActive() );
QVERIFY( alg3.childAlgorithm( "c7" ).isActive() );
QVERIFY( !alg3.activateChildAlgorithm( "c9" ) );
QVERIFY( alg3.activateChildAlgorithm( "c8" ) );
QVERIFY( !alg3.childAlgorithm( "c9" ).isActive() );
QVERIFY( alg3.childAlgorithm( "c8" ).isActive() );
QVERIFY( alg3.childAlgorithm( "c7" ).isActive() );
QVERIFY( alg3.activateChildAlgorithm( "c9" ) );
QVERIFY( alg3.childAlgorithm( "c9" ).isActive() );
QVERIFY( alg3.childAlgorithm( "c8" ).isActive() );
QVERIFY( alg3.childAlgorithm( "c7" ).isActive() );



//remove child algorithm
QVERIFY( !alg3.removeChildAlgorithm( "c7" ) );
QVERIFY( !alg3.removeChildAlgorithm( "c8" ) );
Expand Down

0 comments on commit 179a377

Please sign in to comment.