Skip to content
Permalink
Browse files

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
  • Loading branch information
olivierdalang authored and nyalldawson committed Sep 15, 2018
1 parent 3724f9e commit 340631f85057193e0cdf045c25f164ebc73eaddc
Showing with 10 additions and 14 deletions.
  1. +10 −14 python/plugins/processing/modeler/ProjectProvider.py
@@ -45,7 +45,7 @@ def __init__(self, project=None):
else:
self.project = project

self.model_definitions = [] # list of maps defining models
self.model_definitions = {} # dict of models in project
self.is_loading = False

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

def add_model(self, model):
@@ -77,7 +77,7 @@ def add_model(self, model):
:param model: model to add
"""
definition = model.toVariant()
self.model_definitions.append(definition)
self.model_definitions[model.name()] = definition
self.refreshAlgorithms()

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

filtered_model_definitions = []
for m in self.model_definitions:
algorithm = QgsProcessingModelAlgorithm()
if algorithm.loadVariant(m) and algorithm.name() == model.name():
# found matching model definition, skip it
continue
filtered_model_definitions.append(m)
if model.name() in self.model_definitions:
del self.model_definitions[model.name()]

self.model_definitions = filtered_model_definitions
self.refreshAlgorithms()

def read_project(self, doc):
"""
Reads the project model definitions from the project DOM document
:param doc: DOM document
"""
self.model_definitions = []
self.model_definitions = {}
project_models_nodes = doc.elementsByTagName('projectModels')
if project_models_nodes:
project_models_node = project_models_nodes.at(0)
model_nodes = project_models_node.childNodes()
for n in range(model_nodes.count()):
model_element = model_nodes.at(n).toElement()
definition = QgsXmlUtils.readVariant(model_element)
self.model_definitions.append(definition)
algorithm = QgsProcessingModelAlgorithm()
if algorithm.loadVariant(definition):
self.model_definitions[algorithm.name()] = definition

self.refreshAlgorithms()

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

for definition in self.model_definitions:
for definition in self.model_definitions.values():
algorithm = QgsProcessingModelAlgorithm()
if algorithm.loadVariant(definition):
self.addAlgorithm(algorithm)

0 comments on commit 340631f

Please sign in to comment.
You can’t perform that action at this time.