Skip to content

Commit 340631f

Browse files
olivierdalangnyalldawson
authored andcommitted
fix #19824 where modifications to project models were not saved
Models definitions are now stored in a dict rather than list which allows for less verbose code to reference models by name
1 parent 3724f9e commit 340631f

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

python/plugins/processing/modeler/ProjectProvider.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def __init__(self, project=None):
4545
else:
4646
self.project = project
4747

48-
self.model_definitions = [] # list of maps defining models
48+
self.model_definitions = {} # dict of models in project
4949
self.is_loading = False
5050

5151
# must reload models if providers list is changed - previously unavailable algorithms
@@ -67,7 +67,7 @@ def clear(self):
6767
"""
6868
Remove all algorithms from the provider
6969
"""
70-
self.model_definitions = []
70+
self.model_definitions = {}
7171
self.refreshAlgorithms()
7272

7373
def add_model(self, model):
@@ -77,7 +77,7 @@ def add_model(self, model):
7777
:param model: model to add
7878
"""
7979
definition = model.toVariant()
80-
self.model_definitions.append(definition)
80+
self.model_definitions[model.name()] = definition
8181
self.refreshAlgorithms()
8282

8383
def remove_model(self, model):
@@ -89,31 +89,27 @@ def remove_model(self, model):
8989
if model is None:
9090
return
9191

92-
filtered_model_definitions = []
93-
for m in self.model_definitions:
94-
algorithm = QgsProcessingModelAlgorithm()
95-
if algorithm.loadVariant(m) and algorithm.name() == model.name():
96-
# found matching model definition, skip it
97-
continue
98-
filtered_model_definitions.append(m)
92+
if model.name() in self.model_definitions:
93+
del self.model_definitions[model.name()]
9994

100-
self.model_definitions = filtered_model_definitions
10195
self.refreshAlgorithms()
10296

10397
def read_project(self, doc):
10498
"""
10599
Reads the project model definitions from the project DOM document
106100
:param doc: DOM document
107101
"""
108-
self.model_definitions = []
102+
self.model_definitions = {}
109103
project_models_nodes = doc.elementsByTagName('projectModels')
110104
if project_models_nodes:
111105
project_models_node = project_models_nodes.at(0)
112106
model_nodes = project_models_node.childNodes()
113107
for n in range(model_nodes.count()):
114108
model_element = model_nodes.at(n).toElement()
115109
definition = QgsXmlUtils.readVariant(model_element)
116-
self.model_definitions.append(definition)
110+
algorithm = QgsProcessingModelAlgorithm()
111+
if algorithm.loadVariant(definition):
112+
self.model_definitions[algorithm.name()] = definition
117113

118114
self.refreshAlgorithms()
119115

@@ -158,7 +154,7 @@ def loadAlgorithms(self):
158154
return
159155
self.is_loading = True
160156

161-
for definition in self.model_definitions:
157+
for definition in self.model_definitions.values():
162158
algorithm = QgsProcessingModelAlgorithm()
163159
if algorithm.loadVariant(definition):
164160
self.addAlgorithm(algorithm)

0 commit comments

Comments
 (0)