@@ -109,21 +109,6 @@ void AutomappingManager::autoMapInternal(const QRegion &where,
109
109
110
110
const bool automatic = touchedLayer != nullptr ;
111
111
112
- if (!mLoaded ) {
113
- if (mRulesFile .isEmpty ()) {
114
- mError = tr (" No AutoMapping rules provided. Save the map or refer to a rule file in the project properties." );
115
- emit errorsOccurred (automatic);
116
- return ;
117
- }
118
-
119
- if (loadFile (mRulesFile )) {
120
- mLoaded = true ;
121
- } else {
122
- emit errorsOccurred (automatic);
123
- return ;
124
- }
125
- }
126
-
127
112
// Even if no AutoMapper instance will be executed, we still want to report
128
113
// any warnings or errors that might have been reported while interpreting
129
114
// the rule maps.
@@ -135,14 +120,29 @@ void AutomappingManager::autoMapInternal(const QRegion &where,
135
120
emit errorsOccurred (automatic);
136
121
});
137
122
123
+ if (!mLoaded ) {
124
+ if (mRulesFile .isEmpty ()) {
125
+ mError = tr (" No AutoMapping rules provided. Save the map or refer to a rule file in the project properties." );
126
+ return ;
127
+ }
128
+
129
+ if (!loadFile (mRulesFile ))
130
+ return ;
131
+
132
+ mLoaded = true ;
133
+ }
134
+
138
135
// Determine the list of AutoMappers that is relevant for this map
139
136
const QString mapFileName = QFileInfo (mMapDocument ->fileName ()).fileName ();
137
+
140
138
QVector<const AutoMapper*> autoMappers;
141
- autoMappers.reserve (mActiveAutoMappers .size ());
142
- for (auto autoMapper : mActiveAutoMappers ) {
143
- const auto &mapNameFilter = autoMapper->mapNameFilter ();
139
+ autoMappers.reserve (mRuleMapReferences .size ());
140
+
141
+ for (auto &ruleMap : std::as_const (mRuleMapReferences )) {
142
+ const auto &mapNameFilter = ruleMap.mapNameFilter ;
144
143
if (!mapNameFilter.isValid () || mapNameFilter.match (mapFileName).hasMatch ())
145
- autoMappers.append (autoMapper);
144
+ if (const AutoMapper *autoMapper = findAutoMapper (ruleMap.filePath ))
145
+ autoMappers.append (autoMapper);
146
146
}
147
147
148
148
if (autoMappers.isEmpty ())
@@ -164,6 +164,24 @@ void AutomappingManager::autoMapInternal(const QRegion &where,
164
164
mMapDocument ->undoStack ()->push (aw);
165
165
}
166
166
167
+ /* *
168
+ * Returns the AutoMapper instance for the given rules file, loading it if
169
+ * necessary. Returns nullptr if the file could not be loaded.
170
+ */
171
+ const AutoMapper *AutomappingManager::findAutoMapper (const QString &filePath)
172
+ {
173
+ auto it = mLoadedAutoMappers .find (filePath);
174
+ if (it != mLoadedAutoMappers .end ())
175
+ return it->second .get ();
176
+
177
+ auto autoMapper = loadRuleMap (filePath);
178
+ if (!autoMapper)
179
+ return nullptr ;
180
+
181
+ auto result = mLoadedAutoMappers .emplace (filePath, std::move (autoMapper));
182
+ return result.first ->second .get ();
183
+ }
184
+
167
185
/* *
168
186
* This function parses a rules file or loads a rules map file.
169
187
*
@@ -183,7 +201,8 @@ bool AutomappingManager::loadFile(const QString &filePath)
183
201
return loadRulesFile (filePath);
184
202
}
185
203
186
- return loadRuleMap (filePath);
204
+ mRuleMapReferences .append (RuleMapReference { filePath, mMapNameFilter });
205
+ return true ;
187
206
}
188
207
189
208
bool AutomappingManager::loadRulesFile (const QString &filePath)
@@ -251,41 +270,30 @@ bool AutomappingManager::loadRulesFile(const QString &filePath)
251
270
return ret;
252
271
}
253
272
254
- bool AutomappingManager::loadRuleMap (const QString &filePath)
273
+ std::unique_ptr<AutoMapper> AutomappingManager::loadRuleMap (const QString &filePath)
255
274
{
256
- auto it = mLoadedAutoMappers .find (filePath);
257
- if (it != mLoadedAutoMappers .end ()) {
258
- mActiveAutoMappers .push_back (it->second .get ());
259
- return true ;
260
- }
261
-
262
275
QString errorString;
263
- std::unique_ptr<Map> rules { readMap (filePath, &errorString) };
264
-
265
- if (!rules) {
276
+ auto rulesMap = readMap (filePath, &errorString);
277
+ if (!rulesMap) {
266
278
QString error = tr (" Opening rules map '%1' failed: %2" )
267
279
.arg (filePath, errorString);
268
280
ERROR (error);
269
281
270
282
mError += error;
271
283
mError += QLatin1Char (' \n ' );
272
- return false ;
284
+ return {} ;
273
285
}
274
286
275
- std::unique_ptr<AutoMapper> autoMapper { new AutoMapper (std::move (rules), mMapNameFilter ) };
287
+ mWatcher .addPath (filePath);
288
+
289
+ auto autoMapper = std::make_unique<AutoMapper>(std::move (rulesMap));
276
290
277
291
mWarning += autoMapper->warningString ();
278
292
const QString error = autoMapper->errorString ();
279
- if (error.isEmpty ()) {
280
- auto autoMapperPtr = autoMapper.get ();
281
- mLoadedAutoMappers .insert (std::make_pair (filePath, std::move (autoMapper)));
282
- mActiveAutoMappers .push_back (autoMapperPtr);
283
- mWatcher .addPath (filePath);
284
- } else {
293
+ if (!error.isEmpty ())
285
294
mError += error;
286
- }
287
295
288
- return true ;
296
+ return autoMapper ;
289
297
}
290
298
291
299
/* *
@@ -346,7 +354,7 @@ void AutomappingManager::refreshRulesFile(const QString &ruleFileOverride)
346
354
347
355
void AutomappingManager::cleanUp ()
348
356
{
349
- mActiveAutoMappers .clear ();
357
+ mRuleMapReferences .clear ();
350
358
mLoaded = false ;
351
359
}
352
360
@@ -358,7 +366,9 @@ void AutomappingManager::onFileChanged(const QString &path)
358
366
// File will be re-added when it is still relevant
359
367
mWatcher .removePath (path);
360
368
361
- cleanUp ();
369
+ // Re-parse the rules file(s) when any of them changed
370
+ if (path.endsWith (QLatin1String (" .txt" ), Qt::CaseInsensitive))
371
+ cleanUp ();
362
372
}
363
373
364
374
#include " moc_automappingmanager.cpp"
0 commit comments