@@ -45,7 +45,7 @@ def __init__(self, project=None):
45
45
else :
46
46
self .project = project
47
47
48
- self .model_definitions = [] # list of maps defining models
48
+ self .model_definitions = {} # dict of models in project
49
49
self .is_loading = False
50
50
51
51
# must reload models if providers list is changed - previously unavailable algorithms
@@ -67,7 +67,7 @@ def clear(self):
67
67
"""
68
68
Remove all algorithms from the provider
69
69
"""
70
- self .model_definitions = []
70
+ self .model_definitions = {}
71
71
self .refreshAlgorithms ()
72
72
73
73
def add_model (self , model ):
@@ -77,7 +77,7 @@ def add_model(self, model):
77
77
:param model: model to add
78
78
"""
79
79
definition = model .toVariant ()
80
- self .model_definitions . append ( definition )
80
+ self .model_definitions [ model . name ()] = definition
81
81
self .refreshAlgorithms ()
82
82
83
83
def remove_model (self , model ):
@@ -89,31 +89,27 @@ def remove_model(self, model):
89
89
if model is None :
90
90
return
91
91
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 ()]
99
94
100
- self .model_definitions = filtered_model_definitions
101
95
self .refreshAlgorithms ()
102
96
103
97
def read_project (self , doc ):
104
98
"""
105
99
Reads the project model definitions from the project DOM document
106
100
:param doc: DOM document
107
101
"""
108
- self .model_definitions = []
102
+ self .model_definitions = {}
109
103
project_models_nodes = doc .elementsByTagName ('projectModels' )
110
104
if project_models_nodes :
111
105
project_models_node = project_models_nodes .at (0 )
112
106
model_nodes = project_models_node .childNodes ()
113
107
for n in range (model_nodes .count ()):
114
108
model_element = model_nodes .at (n ).toElement ()
115
109
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
117
113
118
114
self .refreshAlgorithms ()
119
115
@@ -158,7 +154,7 @@ def loadAlgorithms(self):
158
154
return
159
155
self .is_loading = True
160
156
161
- for definition in self .model_definitions :
157
+ for definition in self .model_definitions . values () :
162
158
algorithm = QgsProcessingModelAlgorithm ()
163
159
if algorithm .loadVariant (definition ):
164
160
self .addAlgorithm (algorithm )
0 commit comments