Skip to content

Commit 179a377

Browse files
committed
Port more model to c++
1 parent 4592441 commit 179a377

File tree

6 files changed

+115
-38
lines changed

6 files changed

+115
-38
lines changed

python/core/processing/qgsprocessingmodelalgorithm.sip

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,25 @@ Copies are protected to avoid slicing
541541
Returns true if the algorithm could be removed, or false
542542
if the algorithm could not be removed (e.g. due to other
543543
child algorithms depending on it).
544+
.. seealso:: deactivateChildAlgorithm()
545+
:rtype: bool
546+
%End
547+
548+
void deactivateChildAlgorithm( const QString &id );
549+
%Docstring
550+
Deactivates the child algorithm with matching ``id``.
551+
All other child algorithms which depend on the child
552+
algorithm will also be deactivated.
553+
.. seealso:: removeChildAlgorithm()
554+
.. seealso:: activateChildAlgorithm()
555+
%End
556+
557+
bool activateChildAlgorithm( const QString &id );
558+
%Docstring
559+
Attempts to activate the child algorithm with matching ``id``.
560+
If any child algorithms on which the child depends are not active,
561+
then the child will not be activated and false will be returned.
562+
.. seealso:: deactivateChildAlgorithm()
544563
:rtype: bool
545564
%End
546565

python/plugins/processing/modeler/ModelerAlgorithm.py

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -185,19 +185,6 @@ def __init__(self):
185185
# Geoalgorithms in this model. A dict of Algorithm objects, with names as keys
186186
self.algs = {}
187187

188-
def updateAlgorithm(self, alg):
189-
alg.setPosition(self.childAlgorithm(alg.childId()).position())
190-
alg.setParametersCollapsed(self.childAlgorithm(alg.childId()).parametersCollapsed())
191-
alg.setOutputsCollapsed(self.childAlgorithm(alg.childId()).outputsCollapsed())
192-
self.setChildAlgorithm(alg)
193-
194-
from processing.modeler.ModelerGraphicItem import ModelerGraphicItem
195-
for i, out in enumerate(alg.modelOutputs().keys()):
196-
alg.modelOutput(out).setPosition(alg.modelOutput(out).position() or
197-
alg.position() + QPointF(
198-
ModelerGraphicItem.BOX_WIDTH,
199-
(i + 1.5) * ModelerGraphicItem.BOX_HEIGHT))
200-
201188
def prepareAlgorithm(self, alg):
202189
algInstance = alg.algorithm()
203190
for param in algInstance.parameterDefinitions():
@@ -235,20 +222,6 @@ def prepareAlgorithm(self, alg):
235222

236223
return algInstance
237224

238-
def deactivateAlgorithm(self, algName):
239-
dependent = self.dependentChildAlgorithms(algName)
240-
for alg in dependent:
241-
self.algs[alg].setActive(False)
242-
self.algs[algName].setActive(False)
243-
244-
def activateAlgorithm(self, algName):
245-
parents = self.dependsOnChildAlgorithms(algName)
246-
for alg in parents:
247-
if not self.childAlgorithm(alg).isActive():
248-
return False
249-
self.childAlgorithm(algName).setActive(True)
250-
return True
251-
252225
def getSafeNameForOutput(self, algName, outName):
253226
return outName + '_ALG' + algName
254227

@@ -328,14 +301,6 @@ def shortHelpString(self):
328301
return str(self.helpContent['ALG_DESC'])
329302
return None
330303

331-
def getParameterDescriptions(self):
332-
descs = {}
333-
descriptions = self.helpContent
334-
for param in self.parameters:
335-
if param.name in descriptions:
336-
descs[param.name] = str(descriptions[param.name])
337-
return descs
338-
339304
def todict(self):
340305
keys = ["inputs", "_group", "_name", "algs", "helpContent"]
341306
return {k: v for k, v in list(self.__dict__.items()) if k in keys}

python/plugins/processing/modeler/ModelerGraphicItem.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,11 @@ def contextMenuEvent(self, event):
177177
popupmenu.exec_(event.screenPos())
178178

179179
def deactivateAlgorithm(self):
180-
self.model.deactivateAlgorithm(self.element.childId())
180+
self.model.deactivateChildAlgorithm(self.element.childId())
181181
self.scene.dialog.repaintModel()
182182

183183
def activateAlgorithm(self):
184-
if self.model.activateAlgorithm(self.element.childId()):
184+
if self.model.activateChildAlgorithm(self.element.childId()):
185185
self.scene.dialog.repaintModel()
186186
else:
187187
QMessageBox.warning(None, 'Could not activate Algorithm',
@@ -211,9 +211,21 @@ def editElement(self):
211211
dlg.exec_()
212212
if dlg.alg is not None:
213213
dlg.alg.setChildId(self.element.childId())
214-
self.model.updateAlgorithm(dlg.alg)
214+
self.updateAlgorithm(dlg.alg)
215215
self.scene.dialog.repaintModel()
216216

217+
def updateAlgorithm(self, alg):
218+
existing_child = self.model.childAlgorithm(alg.childId())
219+
alg.setPosition(existing_child.position())
220+
alg.setParametersCollapsed(existing_child.parametersCollapsed())
221+
alg.setOutputsCollapsed(existing_child.outputsCollapsed())
222+
for i, out in enumerate(alg.modelOutputs().keys()):
223+
alg.modelOutput(out).setPosition(alg.modelOutput(out).position() or
224+
alg.position() + QPointF(
225+
ModelerGraphicItem.BOX_WIDTH,
226+
(i + 1.5) * ModelerGraphicItem.BOX_HEIGHT))
227+
self.model.setChildAlgorithm(alg)
228+
217229
def removeElement(self):
218230
if isinstance(self.element, QgsProcessingModelAlgorithm.ModelParameter):
219231
if not self.model.removeModelParameter(self.element.parameterName()):

src/core/processing/qgsprocessingmodelalgorithm.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,26 @@ bool QgsProcessingModelAlgorithm::removeChildAlgorithm( const QString &id )
260260
return true;
261261
}
262262

263+
void QgsProcessingModelAlgorithm::deactivateChildAlgorithm( const QString &id )
264+
{
265+
Q_FOREACH ( const QString &child, dependentChildAlgorithms( id ) )
266+
{
267+
childAlgorithm( child ).setActive( false );
268+
}
269+
childAlgorithm( id ).setActive( false );
270+
}
271+
272+
bool QgsProcessingModelAlgorithm::activateChildAlgorithm( const QString &id )
273+
{
274+
Q_FOREACH ( const QString &child, dependsOnChildAlgorithms( id ) )
275+
{
276+
if ( !childAlgorithm( child ).isActive() )
277+
return false;
278+
}
279+
childAlgorithm( id ).setActive( true );
280+
return true;
281+
}
282+
263283
void QgsProcessingModelAlgorithm::addModelParameter( QgsProcessingParameterDefinition *definition, const QgsProcessingModelAlgorithm::ModelParameter &component )
264284
{
265285
addParameter( definition );

src/core/processing/qgsprocessingmodelalgorithm.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,9 +547,27 @@ class CORE_EXPORT QgsProcessingModelAlgorithm : public QgsProcessingAlgorithm
547547
* Returns true if the algorithm could be removed, or false
548548
* if the algorithm could not be removed (e.g. due to other
549549
* child algorithms depending on it).
550+
* \see deactivateChildAlgorithm()
550551
*/
551552
bool removeChildAlgorithm( const QString &id );
552553

554+
/**
555+
* Deactivates the child algorithm with matching \a id.
556+
* All other child algorithms which depend on the child
557+
* algorithm will also be deactivated.
558+
* \see removeChildAlgorithm()
559+
* \see activateChildAlgorithm()
560+
*/
561+
void deactivateChildAlgorithm( const QString &id );
562+
563+
/**
564+
* Attempts to activate the child algorithm with matching \a id.
565+
* If any child algorithms on which the child depends are not active,
566+
* then the child will not be activated and false will be returned.
567+
* \see deactivateChildAlgorithm()
568+
*/
569+
bool activateChildAlgorithm( const QString &id );
570+
553571
/**
554572
* Returns a list of the child algorithm IDs depending on the child
555573
* algorithm with the specified \a childId.

tests/src/core/testqgsprocessing.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3226,6 +3226,49 @@ void TestQgsProcessing::modelerAlgorithm()
32263226
QVERIFY( alg3.dependsOnChildAlgorithms( "c9" ).contains( "c7" ) );
32273227
QVERIFY( alg3.dependsOnChildAlgorithms( "c9" ).contains( "c8" ) );
32283228

3229+
// (de)activate child algorithm
3230+
alg3.deactivateChildAlgorithm( "c9" );
3231+
QVERIFY( !alg3.childAlgorithm( "c9" ).isActive() );
3232+
QVERIFY( alg3.activateChildAlgorithm( "c9" ) );
3233+
QVERIFY( alg3.childAlgorithm( "c9" ).isActive() );
3234+
alg3.deactivateChildAlgorithm( "c8" );
3235+
QVERIFY( !alg3.childAlgorithm( "c9" ).isActive() );
3236+
QVERIFY( !alg3.childAlgorithm( "c8" ).isActive() );
3237+
QVERIFY( !alg3.activateChildAlgorithm( "c9" ) );
3238+
QVERIFY( !alg3.childAlgorithm( "c9" ).isActive() );
3239+
QVERIFY( !alg3.childAlgorithm( "c8" ).isActive() );
3240+
QVERIFY( alg3.activateChildAlgorithm( "c8" ) );
3241+
QVERIFY( !alg3.childAlgorithm( "c9" ).isActive() );
3242+
QVERIFY( alg3.childAlgorithm( "c8" ).isActive() );
3243+
QVERIFY( alg3.activateChildAlgorithm( "c9" ) );
3244+
QVERIFY( alg3.childAlgorithm( "c9" ).isActive() );
3245+
QVERIFY( alg3.childAlgorithm( "c8" ).isActive() );
3246+
alg3.deactivateChildAlgorithm( "c7" );
3247+
QVERIFY( !alg3.childAlgorithm( "c9" ).isActive() );
3248+
QVERIFY( !alg3.childAlgorithm( "c8" ).isActive() );
3249+
QVERIFY( !alg3.childAlgorithm( "c7" ).isActive() );
3250+
QVERIFY( !alg3.activateChildAlgorithm( "c9" ) );
3251+
QVERIFY( !alg3.activateChildAlgorithm( "c8" ) );
3252+
QVERIFY( !alg3.childAlgorithm( "c9" ).isActive() );
3253+
QVERIFY( !alg3.childAlgorithm( "c8" ).isActive() );
3254+
QVERIFY( !alg3.childAlgorithm( "c7" ).isActive() );
3255+
QVERIFY( !alg3.activateChildAlgorithm( "c8" ) );
3256+
QVERIFY( alg3.activateChildAlgorithm( "c7" ) );
3257+
QVERIFY( !alg3.childAlgorithm( "c9" ).isActive() );
3258+
QVERIFY( !alg3.childAlgorithm( "c8" ).isActive() );
3259+
QVERIFY( alg3.childAlgorithm( "c7" ).isActive() );
3260+
QVERIFY( !alg3.activateChildAlgorithm( "c9" ) );
3261+
QVERIFY( alg3.activateChildAlgorithm( "c8" ) );
3262+
QVERIFY( !alg3.childAlgorithm( "c9" ).isActive() );
3263+
QVERIFY( alg3.childAlgorithm( "c8" ).isActive() );
3264+
QVERIFY( alg3.childAlgorithm( "c7" ).isActive() );
3265+
QVERIFY( alg3.activateChildAlgorithm( "c9" ) );
3266+
QVERIFY( alg3.childAlgorithm( "c9" ).isActive() );
3267+
QVERIFY( alg3.childAlgorithm( "c8" ).isActive() );
3268+
QVERIFY( alg3.childAlgorithm( "c7" ).isActive() );
3269+
3270+
3271+
32293272
//remove child algorithm
32303273
QVERIFY( !alg3.removeChildAlgorithm( "c7" ) );
32313274
QVERIFY( !alg3.removeChildAlgorithm( "c8" ) );

0 commit comments

Comments
 (0)