Skip to content

Commit 33c63d5

Browse files
authoredJun 30, 2017
Merge pull request #4798 from nyalldawson/intput_list
Convert model child algorithm parameter sources to a list
2 parents bc4aae7 + 77588b9 commit 33c63d5

File tree

7 files changed

+285
-116
lines changed

7 files changed

+285
-116
lines changed
 

‎python/core/conversions.sip

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,112 @@ template<double, TYPE>
11981198
};
11991199

12001200

1201+
template<TYPE2>
1202+
%MappedType QMap<QString, QList<TYPE2> >
1203+
{
1204+
%TypeHeaderCode
1205+
#include <QMap>
1206+
#include <QList>
1207+
%End
1208+
1209+
%ConvertFromTypeCode
1210+
// Create the dictionary.
1211+
PyObject *d = PyDict_New();
1212+
1213+
if (!d)
1214+
return NULL;
1215+
1216+
const sipMappedType *qlist_type = sipFindMappedType("QList<TYPE2>");
1217+
1218+
// Set the dictionary elements.
1219+
QMap<QString, QList< TYPE2 > >::const_iterator i;
1220+
1221+
for (i = sipCpp->constBegin(); i != sipCpp->constEnd(); ++i)
1222+
{
1223+
QString *t1 = new QString(i.key());
1224+
1225+
PyObject *t1obj = sipConvertFromNewType(t1, sipType_QString, sipTransferObj);
1226+
1227+
QList< TYPE2 > *t2 = new QList< TYPE2 >( i.value() );
1228+
1229+
PyObject *t2obj = sipConvertFromMappedType(t2, qlist_type, sipTransferObj);
1230+
1231+
if (t1obj == NULL || t2obj == NULL || PyDict_SetItem(d, t1obj, t2obj) < 0)
1232+
{
1233+
Py_DECREF(d);
1234+
1235+
if (t1obj)
1236+
Py_DECREF(t1obj);
1237+
1238+
if (t2obj)
1239+
Py_DECREF(t2obj);
1240+
else
1241+
delete t2;
1242+
1243+
return NULL;
1244+
}
1245+
1246+
Py_DECREF(t1obj);
1247+
Py_DECREF(t2obj);
1248+
}
1249+
1250+
return d;
1251+
%End
1252+
1253+
%ConvertToTypeCode
1254+
PyObject *t1obj, *t2obj;
1255+
Py_ssize_t i = 0;
1256+
1257+
1258+
const sipMappedType *qlist_type = sipFindMappedType("QList<TYPE2>");
1259+
1260+
1261+
// Check the type if that is all that is required.
1262+
if (sipIsErr == NULL)
1263+
{
1264+
if (!PyDict_Check(sipPy))
1265+
return 0;
1266+
1267+
while (PyDict_Next(sipPy, &i, &t1obj, &t2obj))
1268+
{
1269+
if (!sipCanConvertToType(t1obj, sipType_QString, SIP_NOT_NONE))
1270+
return 0;
1271+
}
1272+
1273+
return 1;
1274+
}
1275+
1276+
QMap<QString, QList< TYPE2 > > *qm = new QMap<QString, QList< TYPE2 > >;
1277+
1278+
while (PyDict_Next(sipPy, &i, &t1obj, &t2obj))
1279+
{
1280+
int state;
1281+
1282+
QString *t1 = reinterpret_cast<QString *>(sipConvertToType(t1obj, sipType_QString, sipTransferObj, SIP_NOT_NONE, &state, sipIsErr));
1283+
1284+
QList<TYPE2> *t2 = reinterpret_cast< QList<TYPE2> * >(sipConvertToMappedType(t2obj, qlist_type, sipTransferObj, SIP_NOT_NONE, &state, sipIsErr));
1285+
if (*sipIsErr)
1286+
{
1287+
sipReleaseType(t2, sipType_TYPE2, state);
1288+
delete qm;
1289+
return 0;
1290+
}
1291+
1292+
if ( t2 )
1293+
qm->insert(*t1, *t2);
1294+
else
1295+
qm->insert(*t1, QList<TYPE2>() );
1296+
1297+
sipReleaseType(t1, sipType_QString, state);
1298+
1299+
sipReleaseType(t2, sipType_TYPE2, state);
1300+
}
1301+
1302+
*sipCppPtr = qm;
1303+
1304+
return sipGetState(sipTransferObj);
1305+
%End
1306+
};
12011307

12021308
template<double, TYPE2>
12031309
%MappedType QMultiMap<double, TYPE2>

‎python/core/processing/qgsprocessingmodelalgorithm.sip

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ class QgsProcessingModelAlgorithm : QgsProcessingAlgorithm
161161

162162
};
163163

164+
164165
class Component
165166
{
166167
%Docstring
@@ -415,30 +416,30 @@ Copies are protected to avoid slicing
415416
:rtype: QgsProcessingAlgorithm
416417
%End
417418

418-
QMap< QString, QgsProcessingModelAlgorithm::ChildParameterSource > parameterSources() const;
419+
QMap< QString, QList< QgsProcessingModelAlgorithm::ChildParameterSource > > parameterSources() const;
419420
%Docstring
420421
Returns a map of parameter sources. The keys are the child algorithm
421-
parameter names, the values are the source for that parameter.
422+
parameter names, the values are the sources for that parameter.
422423
.. seealso:: setParameterSources()
423-
.. seealso:: addParameterSource()
424-
:rtype: QMap< str, QgsProcessingModelAlgorithm.ChildParameterSource >
424+
.. seealso:: addParameterSources()
425+
:rtype: QMap< str, QList< QgsProcessingModelAlgorithm.ChildParameterSource > >
425426
%End
426427

427-
void setParameterSources( const QMap< QString, QgsProcessingModelAlgorithm::ChildParameterSource > &sources );
428+
void setParameterSources( const QMap< QString, QList< QgsProcessingModelAlgorithm::ChildParameterSource > > &sources );
428429
%Docstring
429430
Sets the map of parameter ``sources``. The keys are the child algorithm
430-
parameter names, the values are the source for that parameter.
431+
parameter names, the values are the sources for that parameter.
431432
.. seealso:: parameterSources()
432-
.. seealso:: addParameterSource()
433+
.. seealso:: addParameterSources()
433434
%End
434435

435-
void addParameterSource( const QString &name, const QgsProcessingModelAlgorithm::ChildParameterSource &source );
436+
void addParameterSources( const QString &name, const QList< QgsProcessingModelAlgorithm::ChildParameterSource > &source );
436437
%Docstring
437438
Adds a parameter source. The ``name`` argument should match
438-
one of the child algorithm's parameter names, and the ``source``
439-
argument is used to set the source for that parameter.
439+
one of the child algorithm's parameter names, and the ``sources``
440+
argument is used to set the sources for that parameter.
440441

441-
Any existing parameter source with matching name will be replaced.
442+
Any existing parameter sources with matching name will be replaced.
442443
.. seealso:: parameterSources()
443444
.. seealso:: setParameterSources()
444445
%End

‎python/plugins/processing/modeler/ModelerParametersDialog.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,8 @@ def setPreviousValues(self):
285285
continue
286286
if param.name() in alg.parameterSources():
287287
value = alg.parameterSources()[param.name()]
288+
if isinstance(value, list) and len(value) == 1:
289+
value = value[0]
288290
else:
289291
value = param.defaultValue()
290292

@@ -325,9 +327,11 @@ def createAlgorithm(self):
325327
if val is None:
326328
continue
327329
elif isinstance(val, QgsProcessingModelAlgorithm.ChildParameterSource):
328-
alg.addParameterSource(param.name(), val)
330+
alg.addParameterSources(param.name(), [val])
331+
elif isinstance(val, list):
332+
alg.addParameterSources(param.name(), val)
329333
else:
330-
alg.addParameterSource(param.name(), QgsProcessingModelAlgorithm.ChildParameterSource.fromStaticValue(val))
334+
alg.addParameterSources(param.name(), [QgsProcessingModelAlgorithm.ChildParameterSource.fromStaticValue(val)])
331335

332336
outputs = {}
333337
for dest in self._alg.destinationParameterDefinitions():

‎python/plugins/processing/modeler/ModelerScene.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,18 @@ def paintModel(self, model, controls=True):
109109
for parameter in alg.algorithm().parameterDefinitions():
110110
if not parameter.isDestination() and not parameter.flags() & QgsProcessingParameterDefinition.FlagHidden:
111111
if parameter.name() in alg.parameterSources():
112-
value = alg.parameterSources()[parameter.name()]
112+
sources = alg.parameterSources()[parameter.name()]
113113
else:
114-
value = None
115-
sourceItems = self.getItemsFromParamValue(value)
116-
for sourceItem, sourceIdx in sourceItems:
117-
arrow = ModelerArrowItem(sourceItem, sourceIdx, self.algItems[alg.childId()], idx)
118-
sourceItem.addArrow(arrow)
119-
self.algItems[alg.childId()].addArrow(arrow)
120-
arrow.updatePath()
121-
self.addItem(arrow)
122-
idx += 1
114+
sources = []
115+
for source in sources:
116+
sourceItems = self.getItemsFromParamValue(source)
117+
for sourceItem, sourceIdx in sourceItems:
118+
arrow = ModelerArrowItem(sourceItem, sourceIdx, self.algItems[alg.childId()], idx)
119+
sourceItem.addArrow(arrow)
120+
self.algItems[alg.childId()].addArrow(arrow)
121+
arrow.updatePath()
122+
self.addItem(arrow)
123+
idx += 1
123124
for depend in alg.dependencies():
124125
arrow = ModelerArrowItem(self.algItems[depend], -1,
125126
self.algItems[alg.childId()], -1)

0 commit comments

Comments
 (0)
Please sign in to comment.