Chris@45
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@45
|
2
|
Chris@45
|
3 /*
|
Chris@45
|
4 Sonic Visualiser
|
Chris@45
|
5 An audio file viewer and annotation editor.
|
Chris@45
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@45
|
7 This file copyright 2006 Chris Cannam and QMUL.
|
Chris@45
|
8
|
Chris@45
|
9 This program is free software; you can redistribute it and/or
|
Chris@45
|
10 modify it under the terms of the GNU General Public License as
|
Chris@45
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@45
|
12 License, or (at your option) any later version. See the file
|
Chris@45
|
13 COPYING included with this distribution for more information.
|
Chris@45
|
14 */
|
Chris@45
|
15
|
Chris@45
|
16 #include "Document.h"
|
Chris@45
|
17
|
Chris@420
|
18 #include "Align.h"
|
Chris@420
|
19
|
Chris@45
|
20 #include "data/model/WaveFileModel.h"
|
Chris@45
|
21 #include "data/model/WritableWaveFileModel.h"
|
Chris@45
|
22 #include "data/model/DenseThreeDimensionalModel.h"
|
Chris@45
|
23 #include "data/model/DenseTimeValueModel.h"
|
Chris@588
|
24 #include "data/model/AggregateWaveModel.h"
|
gyorgyf@270
|
25
|
Chris@45
|
26 #include "layer/Layer.h"
|
Chris@105
|
27 #include "widgets/CommandHistory.h"
|
Chris@45
|
28 #include "base/Command.h"
|
Chris@45
|
29 #include "view/View.h"
|
Chris@45
|
30 #include "base/PlayParameterRepository.h"
|
Chris@45
|
31 #include "base/PlayParameters.h"
|
Chris@106
|
32 #include "transform/TransformFactory.h"
|
Chris@106
|
33 #include "transform/ModelTransformerFactory.h"
|
gyorgyf@270
|
34 #include "transform/FeatureExtractionModelTransformer.h"
|
Chris@45
|
35 #include <QApplication>
|
Chris@45
|
36 #include <QTextStream>
|
Chris@90
|
37 #include <QSettings>
|
Chris@45
|
38 #include <iostream>
|
Chris@212
|
39 #include <typeinfo>
|
Chris@45
|
40
|
Chris@45
|
41 #include "data/model/AlignmentModel.h"
|
Chris@423
|
42 #include "Align.h"
|
Chris@45
|
43
|
Chris@297
|
44 using std::vector;
|
Chris@297
|
45
|
Chris@138
|
46 //#define DEBUG_DOCUMENT 1
|
Chris@77
|
47
|
Chris@45
|
48 //!!! still need to handle command history, documentRestored/documentModified
|
Chris@45
|
49
|
Chris@45
|
50 Document::Document() :
|
Chris@636
|
51 m_mainModel(nullptr),
|
Chris@423
|
52 m_autoAlignment(false),
|
Chris@601
|
53 m_align(new Align()),
|
Chris@601
|
54 m_isIncomplete(false)
|
Chris@45
|
55 {
|
Chris@456
|
56 connect(this,
|
Chris@456
|
57 SIGNAL(modelAboutToBeDeleted(Model *)),
|
Chris@54
|
58 ModelTransformerFactory::getInstance(),
|
Chris@45
|
59 SLOT(modelAboutToBeDeleted(Model *)));
|
Chris@456
|
60
|
Chris@456
|
61 connect(ModelTransformerFactory::getInstance(),
|
Chris@456
|
62 SIGNAL(transformFailed(QString, QString)),
|
Chris@456
|
63 this,
|
Chris@456
|
64 SIGNAL(modelGenerationFailed(QString, QString)));
|
Chris@459
|
65
|
Chris@428
|
66 connect(m_align, SIGNAL(alignmentComplete(AlignmentModel *)),
|
Chris@428
|
67 this, SIGNAL(alignmentComplete(AlignmentModel *)));
|
Chris@45
|
68 }
|
Chris@45
|
69
|
Chris@45
|
70 Document::~Document()
|
Chris@45
|
71 {
|
Chris@45
|
72 //!!! Document should really own the command history. atm we
|
Chris@45
|
73 //still refer to it in various places that don't have access to
|
Chris@45
|
74 //the document, be nice to fix that
|
Chris@45
|
75
|
Chris@77
|
76 #ifdef DEBUG_DOCUMENT
|
Chris@660
|
77 SVDEBUG << "\n\nDocument::~Document: about to clear command history" << endl;
|
Chris@77
|
78 #endif
|
Chris@45
|
79 CommandHistory::getInstance()->clear();
|
Chris@45
|
80
|
Chris@77
|
81 #ifdef DEBUG_DOCUMENT
|
Chris@233
|
82 SVDEBUG << "Document::~Document: about to delete layers" << endl;
|
Chris@77
|
83 #endif
|
Chris@45
|
84 while (!m_layers.empty()) {
|
Chris@595
|
85 deleteLayer(*m_layers.begin(), true);
|
Chris@45
|
86 }
|
Chris@45
|
87
|
Chris@45
|
88 if (!m_models.empty()) {
|
Chris@595
|
89 SVDEBUG << "Document::~Document: WARNING: "
|
Chris@595
|
90 << m_models.size() << " model(s) still remain -- "
|
Chris@595
|
91 << "should have been garbage collected when deleting layers"
|
Chris@595
|
92 << endl;
|
Chris@661
|
93 for (ModelRecord &rec: m_models) {
|
Chris@661
|
94 Model *model = rec.model;
|
Chris@595
|
95 if (model == m_mainModel) {
|
Chris@595
|
96 // just in case!
|
Chris@595
|
97 SVDEBUG << "Document::~Document: WARNING: Main model is also"
|
Chris@595
|
98 << " in models list!" << endl;
|
Chris@595
|
99 } else if (model) {
|
Chris@79
|
100 model->aboutToDelete();
|
Chris@595
|
101 emit modelAboutToBeDeleted(model);
|
Chris@595
|
102 delete model;
|
Chris@595
|
103 }
|
Chris@595
|
104 }
|
Chris@661
|
105 m_models.clear();
|
Chris@45
|
106 }
|
Chris@45
|
107
|
Chris@77
|
108 #ifdef DEBUG_DOCUMENT
|
Chris@233
|
109 SVDEBUG << "Document::~Document: About to get rid of main model"
|
Chris@595
|
110 << endl;
|
Chris@77
|
111 #endif
|
Chris@45
|
112 if (m_mainModel) {
|
Chris@79
|
113 m_mainModel->aboutToDelete();
|
Chris@45
|
114 emit modelAboutToBeDeleted(m_mainModel);
|
Chris@45
|
115 }
|
Chris@45
|
116
|
Chris@636
|
117 emit mainModelChanged(nullptr);
|
Chris@45
|
118 delete m_mainModel;
|
Chris@45
|
119 }
|
Chris@45
|
120
|
Chris@45
|
121 Layer *
|
Chris@45
|
122 Document::createLayer(LayerFactory::LayerType type)
|
Chris@45
|
123 {
|
Chris@45
|
124 Layer *newLayer = LayerFactory::getInstance()->createLayer(type);
|
Chris@636
|
125 if (!newLayer) return nullptr;
|
Chris@45
|
126
|
Chris@45
|
127 newLayer->setObjectName(getUniqueLayerName(newLayer->objectName()));
|
Chris@45
|
128
|
Chris@663
|
129 m_layers.push_back(newLayer);
|
Chris@52
|
130
|
Chris@77
|
131 #ifdef DEBUG_DOCUMENT
|
Chris@233
|
132 SVDEBUG << "Document::createLayer: Added layer of type " << type
|
Chris@229
|
133 << ", now have " << m_layers.size() << " layers" << endl;
|
Chris@77
|
134 #endif
|
Chris@52
|
135
|
Chris@45
|
136 emit layerAdded(newLayer);
|
Chris@45
|
137
|
Chris@45
|
138 return newLayer;
|
Chris@45
|
139 }
|
Chris@45
|
140
|
Chris@45
|
141 Layer *
|
Chris@45
|
142 Document::createMainModelLayer(LayerFactory::LayerType type)
|
Chris@45
|
143 {
|
Chris@45
|
144 Layer *newLayer = createLayer(type);
|
Chris@636
|
145 if (!newLayer) return nullptr;
|
Chris@45
|
146 setModel(newLayer, m_mainModel);
|
Chris@45
|
147 return newLayer;
|
Chris@45
|
148 }
|
Chris@45
|
149
|
Chris@45
|
150 Layer *
|
Chris@45
|
151 Document::createImportedLayer(Model *model)
|
Chris@45
|
152 {
|
Chris@45
|
153 LayerFactory::LayerTypeSet types =
|
Chris@595
|
154 LayerFactory::getInstance()->getValidLayerTypes(model);
|
Chris@45
|
155
|
Chris@45
|
156 if (types.empty()) {
|
Chris@660
|
157 SVCERR << "WARNING: Document::importLayer: no valid display layer for model" << endl;
|
Chris@636
|
158 return nullptr;
|
Chris@45
|
159 }
|
Chris@45
|
160
|
Chris@45
|
161 //!!! for now, just use the first suitable layer type
|
Chris@45
|
162 LayerFactory::LayerType type = *types.begin();
|
Chris@45
|
163
|
Chris@45
|
164 Layer *newLayer = LayerFactory::getInstance()->createLayer(type);
|
Chris@636
|
165 if (!newLayer) return nullptr;
|
Chris@45
|
166
|
Chris@45
|
167 newLayer->setObjectName(getUniqueLayerName(newLayer->objectName()));
|
Chris@45
|
168
|
Chris@45
|
169 addImportedModel(model);
|
Chris@45
|
170 setModel(newLayer, model);
|
Chris@45
|
171
|
Chris@45
|
172 //!!! and all channels
|
Chris@45
|
173 setChannel(newLayer, -1);
|
Chris@45
|
174
|
Chris@663
|
175 m_layers.push_back(newLayer);
|
Chris@52
|
176
|
Chris@77
|
177 #ifdef DEBUG_DOCUMENT
|
Chris@233
|
178 SVDEBUG << "Document::createImportedLayer: Added layer of type " << type
|
Chris@229
|
179 << ", now have " << m_layers.size() << " layers" << endl;
|
Chris@77
|
180 #endif
|
Chris@52
|
181
|
Chris@45
|
182 emit layerAdded(newLayer);
|
Chris@45
|
183 return newLayer;
|
Chris@45
|
184 }
|
Chris@45
|
185
|
Chris@45
|
186 Layer *
|
Chris@45
|
187 Document::createEmptyLayer(LayerFactory::LayerType type)
|
Chris@45
|
188 {
|
Chris@636
|
189 if (!m_mainModel) return nullptr;
|
Chris@61
|
190
|
Chris@45
|
191 Model *newModel =
|
Chris@595
|
192 LayerFactory::getInstance()->createEmptyModel(type, m_mainModel);
|
Chris@636
|
193 if (!newModel) return nullptr;
|
Chris@45
|
194
|
Chris@45
|
195 Layer *newLayer = createLayer(type);
|
Chris@45
|
196 if (!newLayer) {
|
Chris@595
|
197 delete newModel;
|
Chris@636
|
198 return nullptr;
|
Chris@45
|
199 }
|
Chris@45
|
200
|
Chris@45
|
201 addImportedModel(newModel);
|
Chris@45
|
202 setModel(newLayer, newModel);
|
Chris@45
|
203
|
Chris@45
|
204 return newLayer;
|
Chris@45
|
205 }
|
Chris@45
|
206
|
Chris@45
|
207 Layer *
|
Chris@45
|
208 Document::createDerivedLayer(LayerFactory::LayerType type,
|
Chris@595
|
209 TransformId transform)
|
Chris@45
|
210 {
|
Chris@45
|
211 Layer *newLayer = createLayer(type);
|
Chris@636
|
212 if (!newLayer) return nullptr;
|
Chris@45
|
213
|
Chris@45
|
214 newLayer->setObjectName(getUniqueLayerName
|
Chris@54
|
215 (TransformFactory::getInstance()->
|
Chris@54
|
216 getTransformFriendlyName(transform)));
|
Chris@45
|
217
|
Chris@45
|
218 return newLayer;
|
Chris@45
|
219 }
|
Chris@297
|
220
|
Chris@45
|
221 Layer *
|
Chris@72
|
222 Document::createDerivedLayer(const Transform &transform,
|
Chris@72
|
223 const ModelTransformer::Input &input)
|
Chris@45
|
224 {
|
Chris@297
|
225 Transforms transforms;
|
Chris@297
|
226 transforms.push_back(transform);
|
Chris@297
|
227 vector<Layer *> layers = createDerivedLayers(transforms, input);
|
Chris@636
|
228 if (layers.empty()) return nullptr;
|
Chris@297
|
229 else return layers[0];
|
Chris@297
|
230 }
|
Chris@297
|
231
|
Chris@297
|
232 vector<Layer *>
|
Chris@297
|
233 Document::createDerivedLayers(const Transforms &transforms,
|
Chris@297
|
234 const ModelTransformer::Input &input)
|
Chris@297
|
235 {
|
Chris@78
|
236 QString message;
|
Chris@636
|
237 vector<Model *> newModels = addDerivedModels(transforms, input, message, nullptr);
|
Chris@297
|
238
|
Chris@297
|
239 if (newModels.empty()) {
|
Chris@297
|
240 //!!! This identifier may be wrong!
|
Chris@297
|
241 emit modelGenerationFailed(transforms[0].getIdentifier(), message);
|
Chris@297
|
242 return vector<Layer *>();
|
Chris@78
|
243 } else if (message != "") {
|
Chris@297
|
244 //!!! This identifier may be wrong!
|
Chris@297
|
245 emit modelGenerationWarning(transforms[0].getIdentifier(), message);
|
Chris@45
|
246 }
|
Chris@45
|
247
|
Chris@329
|
248 QStringList names;
|
Chris@366
|
249 for (int i = 0; i < (int)newModels.size(); ++i) {
|
Chris@329
|
250 names.push_back(getUniqueLayerName
|
Chris@329
|
251 (TransformFactory::getInstance()->
|
Chris@329
|
252 getTransformFriendlyName
|
Chris@329
|
253 (transforms[i].getIdentifier())));
|
Chris@329
|
254 }
|
Chris@329
|
255
|
Chris@329
|
256 vector<Layer *> layers = createLayersForDerivedModels(newModels, names);
|
Chris@329
|
257 return layers;
|
Chris@329
|
258 }
|
Chris@329
|
259
|
Chris@329
|
260 class AdditionalModelConverter :
|
Chris@329
|
261 public ModelTransformerFactory::AdditionalModelHandler
|
Chris@329
|
262 {
|
Chris@329
|
263 public:
|
Chris@329
|
264 AdditionalModelConverter(Document *doc,
|
Chris@329
|
265 Document::LayerCreationHandler *handler) :
|
Chris@329
|
266 m_doc(doc),
|
Chris@329
|
267 m_handler(handler) {
|
Chris@329
|
268 }
|
Chris@329
|
269
|
Chris@633
|
270 ~AdditionalModelConverter() override { }
|
Chris@329
|
271
|
Chris@329
|
272 void
|
Chris@329
|
273 setPrimaryLayers(vector<Layer *> layers) {
|
Chris@329
|
274 m_primary = layers;
|
Chris@329
|
275 }
|
Chris@329
|
276
|
Chris@329
|
277 void
|
Chris@633
|
278 moreModelsAvailable(vector<Model *> models) override {
|
Chris@660
|
279 SVDEBUG << "AdditionalModelConverter::moreModelsAvailable: " << models.size() << " model(s)" << endl;
|
Chris@329
|
280 // We can't automatically regenerate the additional models on
|
Chris@329
|
281 // reload -- we should delete them instead
|
Chris@329
|
282 QStringList names;
|
Chris@329
|
283 foreach (Model *model, models) {
|
Chris@329
|
284 m_doc->addAdditionalModel(model);
|
Chris@329
|
285 names.push_back(QString());
|
Chris@329
|
286 }
|
Chris@329
|
287 vector<Layer *> layers = m_doc->createLayersForDerivedModels
|
Chris@329
|
288 (models, names);
|
Chris@363
|
289 m_handler->layersCreated(this, m_primary, layers);
|
Chris@329
|
290 delete this;
|
Chris@329
|
291 }
|
Chris@329
|
292
|
Chris@329
|
293 void
|
Chris@633
|
294 noMoreModelsAvailable() override {
|
Chris@660
|
295 SVDEBUG << "AdditionalModelConverter::noMoreModelsAvailable" << endl;
|
Chris@363
|
296 m_handler->layersCreated(this, m_primary, vector<Layer *>());
|
Chris@329
|
297 delete this;
|
Chris@329
|
298 }
|
Chris@329
|
299
|
Chris@363
|
300 void cancel() {
|
Chris@363
|
301 foreach (Layer *layer, m_primary) {
|
Chris@363
|
302 Model *model = layer->getModel();
|
Chris@363
|
303 if (model) {
|
Chris@363
|
304 model->abandon();
|
Chris@363
|
305 }
|
Chris@363
|
306 }
|
Chris@363
|
307 }
|
Chris@363
|
308
|
Chris@329
|
309 private:
|
Chris@329
|
310 Document *m_doc;
|
Chris@329
|
311 vector<Layer *> m_primary;
|
Chris@329
|
312 Document::LayerCreationHandler *m_handler; //!!! how to handle destruction of this?
|
Chris@329
|
313 };
|
Chris@329
|
314
|
Chris@363
|
315 Document::LayerCreationAsyncHandle
|
Chris@329
|
316 Document::createDerivedLayersAsync(const Transforms &transforms,
|
Chris@329
|
317 const ModelTransformer::Input &input,
|
Chris@329
|
318 LayerCreationHandler *handler)
|
Chris@329
|
319 {
|
Chris@329
|
320 QString message;
|
Chris@329
|
321
|
Chris@329
|
322 AdditionalModelConverter *amc = new AdditionalModelConverter(this, handler);
|
Chris@329
|
323
|
Chris@329
|
324 vector<Model *> newModels = addDerivedModels
|
Chris@329
|
325 (transforms, input, message, amc);
|
Chris@329
|
326
|
Chris@329
|
327 QStringList names;
|
Chris@366
|
328 for (int i = 0; i < (int)newModels.size(); ++i) {
|
Chris@329
|
329 names.push_back(getUniqueLayerName
|
Chris@329
|
330 (TransformFactory::getInstance()->
|
Chris@329
|
331 getTransformFriendlyName
|
Chris@329
|
332 (transforms[i].getIdentifier())));
|
Chris@329
|
333 }
|
Chris@329
|
334
|
Chris@329
|
335 vector<Layer *> layers = createLayersForDerivedModels(newModels, names);
|
Chris@329
|
336 amc->setPrimaryLayers(layers);
|
Chris@329
|
337
|
Chris@329
|
338 if (newModels.empty()) {
|
Chris@329
|
339 //!!! This identifier may be wrong!
|
Chris@329
|
340 emit modelGenerationFailed(transforms[0].getIdentifier(), message);
|
Chris@363
|
341 //!!! what to do with amc?
|
Chris@329
|
342 } else if (message != "") {
|
Chris@329
|
343 //!!! This identifier may be wrong!
|
Chris@329
|
344 emit modelGenerationWarning(transforms[0].getIdentifier(), message);
|
Chris@363
|
345 //!!! what to do with amc?
|
Chris@329
|
346 }
|
Chris@363
|
347
|
Chris@363
|
348 return amc;
|
Chris@363
|
349 }
|
Chris@363
|
350
|
Chris@363
|
351 void
|
Chris@363
|
352 Document::cancelAsyncLayerCreation(Document::LayerCreationAsyncHandle h)
|
Chris@363
|
353 {
|
Chris@363
|
354 AdditionalModelConverter *conv = static_cast<AdditionalModelConverter *>(h);
|
Chris@363
|
355 conv->cancel();
|
Chris@329
|
356 }
|
Chris@329
|
357
|
Chris@329
|
358 vector<Layer *>
|
Chris@329
|
359 Document::createLayersForDerivedModels(vector<Model *> newModels,
|
Chris@329
|
360 QStringList names)
|
Chris@329
|
361 {
|
Chris@297
|
362 vector<Layer *> layers;
|
Chris@329
|
363
|
Chris@297
|
364 for (int i = 0; i < (int)newModels.size(); ++i) {
|
Chris@297
|
365
|
Chris@297
|
366 Model *newModel = newModels[i];
|
Chris@297
|
367
|
Chris@297
|
368 LayerFactory::LayerTypeSet types =
|
Chris@297
|
369 LayerFactory::getInstance()->getValidLayerTypes(newModel);
|
Chris@297
|
370
|
Chris@297
|
371 if (types.empty()) {
|
Chris@660
|
372 SVCERR << "WARNING: Document::createLayerForTransformer: no valid display layer for output of transform " << names[i] << endl;
|
Chris@297
|
373 //!!! inadequate cleanup:
|
Chris@661
|
374 deleteModelFromList(newModel);
|
Chris@297
|
375 return vector<Layer *>();
|
Chris@297
|
376 }
|
Chris@297
|
377
|
Chris@297
|
378 //!!! for now, just use the first suitable layer type
|
Chris@297
|
379
|
Chris@297
|
380 Layer *newLayer = createLayer(*types.begin());
|
Chris@297
|
381 setModel(newLayer, newModel);
|
Chris@297
|
382
|
Chris@297
|
383 //!!! We need to clone the model when adding the layer, so that it
|
Chris@297
|
384 //can be edited without affecting other layers that are based on
|
Chris@297
|
385 //the same model. Unfortunately we can't just clone it now,
|
Chris@297
|
386 //because it probably hasn't been completed yet -- the transform
|
Chris@297
|
387 //runs in the background. Maybe the transform has to handle
|
Chris@297
|
388 //cloning and cacheing models itself.
|
Chris@297
|
389 //
|
Chris@297
|
390 // Once we do clone models here, of course, we'll have to avoid
|
Chris@297
|
391 // leaking them too.
|
Chris@297
|
392 //
|
Chris@297
|
393 // We want the user to be able to add a model to a second layer
|
Chris@297
|
394 // _while it's still being calculated in the first_ and have it
|
Chris@297
|
395 // work quickly. That means we need to put the same physical
|
Chris@297
|
396 // model pointer in both layers, so they can't actually be cloned.
|
Chris@297
|
397
|
Chris@297
|
398 if (newLayer) {
|
Chris@329
|
399 newLayer->setObjectName(names[i]);
|
Chris@297
|
400 }
|
Chris@297
|
401
|
Chris@297
|
402 emit layerAdded(newLayer);
|
Chris@297
|
403 layers.push_back(newLayer);
|
Chris@45
|
404 }
|
Chris@45
|
405
|
Chris@297
|
406 return layers;
|
Chris@45
|
407 }
|
Chris@45
|
408
|
Chris@45
|
409 void
|
Chris@45
|
410 Document::setMainModel(WaveFileModel *model)
|
Chris@45
|
411 {
|
Chris@45
|
412 Model *oldMainModel = m_mainModel;
|
Chris@45
|
413 m_mainModel = model;
|
Chris@160
|
414
|
Chris@45
|
415 emit modelAdded(m_mainModel);
|
Chris@160
|
416 if (model) {
|
Chris@160
|
417 emit activity(tr("Set main model to %1").arg(model->objectName()));
|
Chris@160
|
418 } else {
|
Chris@160
|
419 emit activity(tr("Clear main model"));
|
Chris@160
|
420 }
|
Chris@45
|
421
|
Chris@45
|
422 std::vector<Layer *> obsoleteLayers;
|
Chris@53
|
423 std::set<QString> failedTransformers;
|
Chris@45
|
424
|
Chris@45
|
425 // We need to ensure that no layer is left using oldMainModel or
|
Chris@45
|
426 // any of the old derived models as its model. Either replace the
|
Chris@45
|
427 // model, or delete the layer for each layer that is currently
|
Chris@45
|
428 // using one of these. Carry out this replacement before we
|
Chris@45
|
429 // delete any of the models.
|
Chris@45
|
430
|
Chris@77
|
431 #ifdef DEBUG_DOCUMENT
|
Chris@660
|
432 SVDEBUG << "Document::setMainModel: Have "
|
Chris@293
|
433 << m_layers.size() << " layers" << endl;
|
Chris@661
|
434 SVDEBUG << "Models now: ";
|
Chris@661
|
435 for (const auto &r: m_models) {
|
Chris@661
|
436 SVDEBUG << r.model << " ";
|
Chris@664
|
437 }
|
Chris@660
|
438 SVDEBUG << endl;
|
Chris@660
|
439 SVDEBUG << "Old main model: " << oldMainModel << endl;
|
Chris@77
|
440 #endif
|
Chris@52
|
441
|
Chris@663
|
442 for (Layer *layer: m_layers) {
|
Chris@45
|
443
|
Chris@595
|
444 Model *model = layer->getModel();
|
Chris@45
|
445
|
Chris@77
|
446 #ifdef DEBUG_DOCUMENT
|
Chris@660
|
447 SVDEBUG << "Document::setMainModel: inspecting model "
|
Chris@660
|
448 << (model ? model->objectName(): "(null)") << " in layer "
|
Chris@660
|
449 << layer->objectName() << endl;
|
Chris@77
|
450 #endif
|
Chris@45
|
451
|
Chris@595
|
452 if (model == oldMainModel) {
|
Chris@77
|
453 #ifdef DEBUG_DOCUMENT
|
Chris@660
|
454 SVDEBUG << "... it uses the old main model, replacing" << endl;
|
Chris@77
|
455 #endif
|
Chris@595
|
456 LayerFactory::getInstance()->setModel(layer, m_mainModel);
|
Chris@595
|
457 continue;
|
Chris@595
|
458 }
|
Chris@45
|
459
|
Chris@137
|
460 if (!model) {
|
Chris@660
|
461 SVCERR << "WARNING: Document::setMainModel: Null model in layer "
|
Chris@660
|
462 << layer << endl;
|
Chris@595
|
463 // get rid of this hideous degenerate
|
Chris@595
|
464 obsoleteLayers.push_back(layer);
|
Chris@595
|
465 continue;
|
Chris@595
|
466 }
|
Chris@137
|
467
|
Chris@661
|
468 auto mitr = findModelInList(model);
|
Chris@661
|
469
|
Chris@661
|
470 if (mitr == m_models.end()) {
|
Chris@661
|
471 SVCERR << "WARNING: Document::setMainModel: Unknown model "
|
Chris@661
|
472 << model << " in layer " << layer << endl;
|
Chris@595
|
473 // and this one
|
Chris@595
|
474 obsoleteLayers.push_back(layer);
|
Chris@595
|
475 continue;
|
Chris@595
|
476 }
|
Chris@661
|
477
|
Chris@661
|
478 ModelRecord record = *mitr;
|
Chris@661
|
479
|
Chris@661
|
480 if (record.source && (record.source == oldMainModel)) {
|
Chris@45
|
481
|
Chris@77
|
482 #ifdef DEBUG_DOCUMENT
|
Chris@660
|
483 SVDEBUG << "... it uses a model derived from the old main model, regenerating" << endl;
|
Chris@77
|
484 #endif
|
Chris@45
|
485
|
Chris@595
|
486 // This model was derived from the previous main
|
Chris@595
|
487 // model: regenerate it.
|
Chris@595
|
488
|
Chris@661
|
489 const Transform &transform = record.transform;
|
Chris@72
|
490 QString transformId = transform.getIdentifier();
|
Chris@595
|
491
|
Chris@72
|
492 //!!! We have a problem here if the number of channels in
|
Chris@72
|
493 //the main model has changed.
|
Chris@72
|
494
|
Chris@78
|
495 QString message;
|
Chris@595
|
496 Model *replacementModel =
|
Chris@45
|
497 addDerivedModel(transform,
|
Chris@72
|
498 ModelTransformer::Input
|
Chris@661
|
499 (m_mainModel, record.channel),
|
Chris@78
|
500 message);
|
Chris@595
|
501
|
Chris@595
|
502 if (!replacementModel) {
|
Chris@660
|
503 SVCERR << "WARNING: Document::setMainModel: Failed to regenerate model for transform \""
|
Chris@660
|
504 << transformId << "\"" << " in layer " << layer << endl;
|
Chris@72
|
505 if (failedTransformers.find(transformId)
|
Chris@72
|
506 == failedTransformers.end()) {
|
Chris@45
|
507 emit modelRegenerationFailed(layer->objectName(),
|
Chris@78
|
508 transformId,
|
Chris@78
|
509 message);
|
Chris@72
|
510 failedTransformers.insert(transformId);
|
Chris@45
|
511 }
|
Chris@595
|
512 obsoleteLayers.push_back(layer);
|
Chris@595
|
513 } else {
|
Chris@78
|
514 if (message != "") {
|
Chris@78
|
515 emit modelRegenerationWarning(layer->objectName(),
|
Chris@78
|
516 transformId,
|
Chris@78
|
517 message);
|
Chris@78
|
518 }
|
Chris@77
|
519 #ifdef DEBUG_DOCUMENT
|
Chris@660
|
520 SVDEBUG << "Replacing model " << model << " (type "
|
Chris@660
|
521 << typeid(*model).name() << ") with model "
|
Chris@660
|
522 << replacementModel << " (type "
|
Chris@660
|
523 << typeid(*replacementModel).name() << ") in layer "
|
Chris@660
|
524 << layer << " (name " << layer->objectName() << ")"
|
Chris@660
|
525 << endl;
|
Chris@366
|
526
|
Chris@45
|
527 RangeSummarisableTimeValueModel *rm =
|
Chris@45
|
528 dynamic_cast<RangeSummarisableTimeValueModel *>(replacementModel);
|
Chris@45
|
529 if (rm) {
|
Chris@660
|
530 SVDEBUG << "new model has " << rm->getChannelCount() << " channels " << endl;
|
Chris@45
|
531 } else {
|
Chris@660
|
532 SVDEBUG << "new model " << replacementModel << " is not a RangeSummarisableTimeValueModel!" << endl;
|
Chris@45
|
533 }
|
Chris@77
|
534 #endif
|
Chris@595
|
535 setModel(layer, replacementModel);
|
Chris@595
|
536 }
|
Chris@595
|
537 }
|
Chris@45
|
538 }
|
Chris@45
|
539
|
Chris@45
|
540 for (size_t k = 0; k < obsoleteLayers.size(); ++k) {
|
Chris@595
|
541 deleteLayer(obsoleteLayers[k], true);
|
Chris@45
|
542 }
|
Chris@45
|
543
|
Chris@661
|
544 std::set<Model *> additionalModels;
|
Chris@661
|
545 for (const auto &rec : m_models) {
|
Chris@661
|
546 if (rec.additional) {
|
Chris@661
|
547 additionalModels.insert(rec.model);
|
Chris@329
|
548 }
|
Chris@329
|
549 }
|
Chris@661
|
550 for (Model *a: additionalModels) {
|
Chris@661
|
551 deleteModelFromList(a);
|
Chris@661
|
552 }
|
Chris@329
|
553
|
Chris@661
|
554 for (const auto &rec : m_models) {
|
Chris@86
|
555
|
Chris@661
|
556 Model *m = rec.model;
|
Chris@137
|
557
|
Chris@137
|
558 #ifdef DEBUG_DOCUMENT
|
Chris@233
|
559 SVDEBUG << "considering alignment for model " << m << " (name \""
|
Chris@229
|
560 << m->objectName() << "\")" << endl;
|
Chris@137
|
561 #endif
|
Chris@137
|
562
|
Chris@86
|
563 if (m_autoAlignment) {
|
Chris@86
|
564
|
Chris@137
|
565 alignModel(m);
|
Chris@86
|
566
|
Chris@86
|
567 } else if (oldMainModel &&
|
Chris@137
|
568 (m->getAlignmentReference() == oldMainModel)) {
|
Chris@86
|
569
|
Chris@137
|
570 alignModel(m);
|
Chris@48
|
571 }
|
Chris@48
|
572 }
|
Chris@48
|
573
|
Chris@77
|
574 if (oldMainModel) {
|
Chris@79
|
575 oldMainModel->aboutToDelete();
|
Chris@77
|
576 emit modelAboutToBeDeleted(oldMainModel);
|
Chris@77
|
577 }
|
Chris@77
|
578
|
Chris@86
|
579 if (m_autoAlignment) {
|
Chris@387
|
580 SVDEBUG << "Document::setMainModel: auto-alignment is on, aligning model if possible" << endl;
|
Chris@86
|
581 alignModel(m_mainModel);
|
Chris@667
|
582 } else {
|
Chris@667
|
583 SVDEBUG << "Document::setMainModel: auto-alignment is off" << endl;
|
Chris@86
|
584 }
|
Chris@86
|
585
|
Chris@45
|
586 emit mainModelChanged(m_mainModel);
|
Chris@45
|
587
|
Chris@45
|
588 delete oldMainModel;
|
Chris@45
|
589 }
|
Chris@45
|
590
|
Chris@45
|
591 void
|
Chris@329
|
592 Document::addAlreadyDerivedModel(const Transform &transform,
|
Chris@329
|
593 const ModelTransformer::Input &input,
|
Chris@329
|
594 Model *outputModelToAdd)
|
Chris@45
|
595 {
|
Chris@661
|
596 if (findModelInList(outputModelToAdd) != m_models.end()) {
|
Chris@661
|
597 SVCERR << "WARNING: Document::addAlreadyDerivedModel: Model already added"
|
Chris@595
|
598 << endl;
|
Chris@595
|
599 return;
|
Chris@45
|
600 }
|
Chris@45
|
601
|
Chris@77
|
602 #ifdef DEBUG_DOCUMENT
|
Chris@248
|
603 if (input.getModel()) {
|
Chris@660
|
604 SVDEBUG << "Document::addAlreadyDerivedModel: source is " << input.getModel() << " \"" << input.getModel()->objectName() << "\"" << endl;
|
Chris@248
|
605 } else {
|
Chris@660
|
606 SVDEBUG << "Document::addAlreadyDerivedModel: source is " << input.getModel() << endl;
|
Chris@248
|
607 }
|
Chris@77
|
608 #endif
|
Chris@45
|
609
|
Chris@45
|
610 ModelRecord rec;
|
Chris@661
|
611 rec.model = outputModelToAdd;
|
Chris@72
|
612 rec.source = input.getModel();
|
Chris@72
|
613 rec.channel = input.getChannel();
|
Chris@45
|
614 rec.transform = transform;
|
Chris@329
|
615 rec.additional = false;
|
Chris@45
|
616 rec.refcount = 0;
|
Chris@45
|
617
|
Chris@72
|
618 outputModelToAdd->setSourceModel(input.getModel());
|
Chris@45
|
619
|
Chris@661
|
620 m_models.push_back(rec);
|
Chris@45
|
621
|
Chris@137
|
622 #ifdef DEBUG_DOCUMENT
|
Chris@661
|
623 SVDEBUG << "Document::addAlreadyDerivedModel: Added model " << outputModelToAdd << endl;
|
Chris@661
|
624 SVDEBUG << "Models now: ";
|
Chris@661
|
625 for (const auto &rec : m_models) {
|
Chris@661
|
626 SVDEBUG << rec.model << " ";
|
Chris@137
|
627 }
|
Chris@660
|
628 SVDEBUG << endl;
|
Chris@137
|
629 #endif
|
Chris@137
|
630
|
Chris@45
|
631 emit modelAdded(outputModelToAdd);
|
Chris@45
|
632 }
|
Chris@45
|
633
|
Chris@45
|
634
|
Chris@45
|
635 void
|
Chris@45
|
636 Document::addImportedModel(Model *model)
|
Chris@45
|
637 {
|
Chris@661
|
638 if (findModelInList(model) != m_models.end()) {
|
Chris@661
|
639 SVCERR << "WARNING: Document::addImportedModel: Model already added"
|
Chris@595
|
640 << endl;
|
Chris@595
|
641 return;
|
Chris@45
|
642 }
|
Chris@45
|
643
|
Chris@45
|
644 ModelRecord rec;
|
Chris@661
|
645 rec.model = model;
|
Chris@636
|
646 rec.source = nullptr;
|
Chris@408
|
647 rec.channel = 0;
|
Chris@45
|
648 rec.refcount = 0;
|
Chris@329
|
649 rec.additional = false;
|
Chris@45
|
650
|
Chris@661
|
651 m_models.push_back(rec);
|
Chris@45
|
652
|
Chris@137
|
653 #ifdef DEBUG_DOCUMENT
|
Chris@233
|
654 SVDEBUG << "Document::addImportedModel: Added model " << model << endl;
|
Chris@661
|
655 SVDEBUG << "Models now: ";
|
Chris@661
|
656 for (const auto &rec : m_models) {
|
Chris@661
|
657 SVDEBUG << rec.model << " ";
|
Chris@137
|
658 }
|
Chris@660
|
659 SVDEBUG << endl;
|
Chris@137
|
660 #endif
|
Chris@137
|
661
|
Chris@387
|
662 if (m_autoAlignment) {
|
Chris@387
|
663 SVDEBUG << "Document::addImportedModel: auto-alignment is on, aligning model if possible" << endl;
|
Chris@387
|
664 alignModel(model);
|
Chris@387
|
665 } else {
|
Chris@387
|
666 SVDEBUG << "Document(" << this << "): addImportedModel: auto-alignment is off" << endl;
|
Chris@387
|
667 }
|
Chris@47
|
668
|
Chris@45
|
669 emit modelAdded(model);
|
Chris@45
|
670 }
|
Chris@45
|
671
|
Chris@329
|
672 void
|
Chris@329
|
673 Document::addAdditionalModel(Model *model)
|
Chris@329
|
674 {
|
Chris@661
|
675 if (findModelInList(model) != m_models.end()) {
|
Chris@661
|
676 SVCERR << "WARNING: Document::addAdditionalModel: Model already added"
|
Chris@595
|
677 << endl;
|
Chris@595
|
678 return;
|
Chris@329
|
679 }
|
Chris@329
|
680
|
Chris@329
|
681 ModelRecord rec;
|
Chris@661
|
682 rec.model = model;
|
Chris@636
|
683 rec.source = nullptr;
|
Chris@408
|
684 rec.channel = 0;
|
Chris@329
|
685 rec.refcount = 0;
|
Chris@329
|
686 rec.additional = true;
|
Chris@329
|
687
|
Chris@661
|
688 m_models.push_back(rec);
|
Chris@329
|
689
|
Chris@329
|
690 #ifdef DEBUG_DOCUMENT
|
Chris@329
|
691 SVDEBUG << "Document::addAdditionalModel: Added model " << model << endl;
|
Chris@661
|
692 SVDEBUG << "Models now: ";
|
Chris@661
|
693 for (const auto &rec : m_models) {
|
Chris@661
|
694 SVDEBUG << rec.model << " ";
|
Chris@329
|
695 }
|
Chris@660
|
696 SVDEBUG << endl;
|
Chris@329
|
697 #endif
|
Chris@329
|
698
|
Chris@387
|
699 if (m_autoAlignment) {
|
Chris@387
|
700 SVDEBUG << "Document::addAdditionalModel: auto-alignment is on, aligning model if possible" << endl;
|
Chris@387
|
701 alignModel(model);
|
Chris@387
|
702 }
|
Chris@329
|
703
|
Chris@329
|
704 emit modelAdded(model);
|
Chris@329
|
705 }
|
Chris@329
|
706
|
Chris@588
|
707 void
|
Chris@588
|
708 Document::addAggregateModel(AggregateWaveModel *model)
|
Chris@588
|
709 {
|
Chris@588
|
710 connect(model, SIGNAL(modelInvalidated()),
|
Chris@588
|
711 this, SLOT(aggregateModelInvalidated()));
|
Chris@588
|
712 m_aggregateModels.insert(model);
|
Chris@655
|
713 SVDEBUG << "Document::addAggregateModel(" << model << ")" << endl;
|
Chris@588
|
714 }
|
Chris@588
|
715
|
Chris@588
|
716 void
|
Chris@588
|
717 Document::aggregateModelInvalidated()
|
Chris@588
|
718 {
|
Chris@588
|
719 QObject *s = sender();
|
Chris@588
|
720 AggregateWaveModel *aggregate = qobject_cast<AggregateWaveModel *>(s);
|
Chris@655
|
721 SVDEBUG << "Document::aggregateModelInvalidated(" << aggregate << ")" << endl;
|
Chris@588
|
722 if (aggregate) releaseModel(aggregate);
|
Chris@588
|
723 }
|
Chris@588
|
724
|
Chris@45
|
725 Model *
|
Chris@72
|
726 Document::addDerivedModel(const Transform &transform,
|
Chris@78
|
727 const ModelTransformer::Input &input,
|
Chris@296
|
728 QString &message)
|
Chris@45
|
729 {
|
Chris@661
|
730 for (auto &rec : m_models) {
|
Chris@661
|
731 if (rec.transform == transform &&
|
Chris@661
|
732 rec.source == input.getModel() &&
|
Chris@661
|
733 rec.channel == input.getChannel()) {
|
Chris@661
|
734 SVDEBUG << "derived model taken from map " << endl;
|
Chris@661
|
735 return rec.model;
|
Chris@297
|
736 }
|
Chris@45
|
737 }
|
Chris@45
|
738
|
Chris@297
|
739 Transforms tt;
|
Chris@297
|
740 tt.push_back(transform);
|
Chris@636
|
741 vector<Model *> mm = addDerivedModels(tt, input, message, nullptr);
|
Chris@636
|
742 if (mm.empty()) return nullptr;
|
Chris@297
|
743 else return mm[0];
|
Chris@297
|
744 }
|
Chris@45
|
745
|
Chris@297
|
746 vector<Model *>
|
Chris@297
|
747 Document::addDerivedModels(const Transforms &transforms,
|
Chris@297
|
748 const ModelTransformer::Input &input,
|
Chris@329
|
749 QString &message,
|
Chris@329
|
750 AdditionalModelConverter *amc)
|
Chris@297
|
751 {
|
Chris@297
|
752 vector<Model *> mm =
|
Chris@297
|
753 ModelTransformerFactory::getInstance()->transformMultiple
|
Chris@329
|
754 (transforms, input, message, amc);
|
Chris@83
|
755
|
Chris@297
|
756 for (int j = 0; j < (int)mm.size(); ++j) {
|
Chris@83
|
757
|
Chris@297
|
758 Model *model = mm[j];
|
Chris@297
|
759
|
Chris@297
|
760 // The transform we actually used was presumably identical to
|
Chris@297
|
761 // the one asked for, except that the version of the plugin
|
Chris@297
|
762 // may differ. It's possible that the returned message
|
Chris@297
|
763 // contains a warning about this; that doesn't concern us
|
Chris@297
|
764 // here, but we do need to ensure that the transform we
|
Chris@297
|
765 // remember is correct for what was actually applied, with the
|
Chris@297
|
766 // current plugin version.
|
Chris@297
|
767
|
Chris@536
|
768 //!!! would be nice to short-circuit this -- the version is
|
Chris@536
|
769 //!!! static data, shouldn't have to construct a plugin for it
|
Chris@536
|
770 //!!! (which may be expensive in Piper-world)
|
Chris@536
|
771
|
Chris@297
|
772 Transform applied = transforms[j];
|
Chris@297
|
773 applied.setPluginVersion
|
Chris@297
|
774 (TransformFactory::getInstance()->
|
Chris@297
|
775 getDefaultTransformFor(applied.getIdentifier(),
|
Chris@436
|
776 applied.getSampleRate())
|
Chris@297
|
777 .getPluginVersion());
|
Chris@297
|
778
|
Chris@297
|
779 if (!model) {
|
Chris@660
|
780 SVCERR << "WARNING: Document::addDerivedModel: no output model for transform " << applied.getIdentifier() << endl;
|
Chris@297
|
781 } else {
|
Chris@329
|
782 addAlreadyDerivedModel(applied, input, model);
|
Chris@297
|
783 }
|
Chris@45
|
784 }
|
Chris@595
|
785
|
Chris@297
|
786 return mm;
|
Chris@45
|
787 }
|
Chris@45
|
788
|
Chris@45
|
789 void
|
Chris@45
|
790 Document::releaseModel(Model *model) // Will _not_ release main model!
|
Chris@45
|
791 {
|
Chris@636
|
792 if (model == nullptr) {
|
Chris@595
|
793 return;
|
Chris@45
|
794 }
|
Chris@45
|
795
|
Chris@665
|
796 #ifdef DEBUG_DOCUMENT
|
Chris@665
|
797 SVDEBUG << "Document::releaseModel(" << model << ", type "
|
Chris@665
|
798 << model->getTypeName() << ", name \""
|
Chris@665
|
799 << model->objectName() << "\")" << endl;
|
Chris@665
|
800 #endif
|
Chris@665
|
801
|
Chris@45
|
802 if (model == m_mainModel) {
|
Chris@595
|
803 return;
|
Chris@45
|
804 }
|
Chris@45
|
805
|
Chris@45
|
806 bool toDelete = false;
|
Chris@45
|
807
|
Chris@661
|
808 ModelList::iterator mitr = findModelInList(model);
|
Chris@661
|
809
|
Chris@661
|
810 if (mitr != m_models.end()) {
|
Chris@661
|
811 if (mitr->refcount == 0) {
|
Chris@595
|
812 SVCERR << "WARNING: Document::releaseModel: model " << model
|
Chris@588
|
813 << " reference count is zero already!" << endl;
|
Chris@595
|
814 } else {
|
Chris@664
|
815 #ifdef DEBUG_DOCUMENT
|
Chris@664
|
816 SVDEBUG << "Lowering refcount from " << mitr->refcount << endl;
|
Chris@664
|
817 #endif
|
Chris@661
|
818 if (--mitr->refcount == 0) {
|
Chris@595
|
819 toDelete = true;
|
Chris@595
|
820 }
|
Chris@595
|
821 }
|
Chris@588
|
822 } else if (m_aggregateModels.find(model) != m_aggregateModels.end()) {
|
Chris@665
|
823 #ifdef DEBUG_DOCUMENT
|
Chris@588
|
824 SVDEBUG << "Document::releaseModel: is an aggregate model" << endl;
|
Chris@665
|
825 #endif
|
Chris@588
|
826 toDelete = true;
|
Chris@45
|
827 } else {
|
Chris@595
|
828 SVCERR << "WARNING: Document::releaseModel: Unfound model "
|
Chris@588
|
829 << model << endl;
|
Chris@595
|
830 toDelete = true;
|
Chris@45
|
831 }
|
Chris@45
|
832
|
Chris@45
|
833 if (toDelete) {
|
Chris@45
|
834
|
Chris@595
|
835 int sourceCount = 0;
|
Chris@45
|
836
|
Chris@661
|
837 for (auto &rec: m_models) {
|
Chris@661
|
838 if (rec.source == model) {
|
Chris@595
|
839 ++sourceCount;
|
Chris@661
|
840 rec.source = nullptr;
|
Chris@595
|
841 }
|
Chris@595
|
842 }
|
Chris@45
|
843
|
Chris@595
|
844 if (sourceCount > 0) {
|
Chris@595
|
845 SVDEBUG << "Document::releaseModel: Deleting model "
|
Chris@588
|
846 << model << " even though it is source for "
|
Chris@588
|
847 << sourceCount << " other derived model(s) -- resetting "
|
Chris@588
|
848 << "their source fields appropriately" << endl;
|
Chris@595
|
849 }
|
Chris@45
|
850
|
Chris@661
|
851 deleteModelFromList(model);
|
Chris@137
|
852
|
Chris@137
|
853 #ifdef DEBUG_DOCUMENT
|
Chris@233
|
854 SVDEBUG << "Document::releaseModel: Deleted model " << model << endl;
|
Chris@661
|
855 SVDEBUG << "Models now: ";
|
Chris@661
|
856 for (const auto &r: m_models) {
|
Chris@661
|
857 SVDEBUG << r.model << " ";
|
Chris@137
|
858 }
|
Chris@660
|
859 SVDEBUG << endl;
|
Chris@137
|
860 #endif
|
Chris@45
|
861 }
|
Chris@45
|
862 }
|
Chris@45
|
863
|
Chris@45
|
864 void
|
Chris@45
|
865 Document::deleteLayer(Layer *layer, bool force)
|
Chris@45
|
866 {
|
Chris@45
|
867 if (m_layerViewMap.find(layer) != m_layerViewMap.end() &&
|
Chris@595
|
868 m_layerViewMap[layer].size() > 0) {
|
Chris@45
|
869
|
Chris@660
|
870 SVCERR << "WARNING: Document::deleteLayer: Layer "
|
Chris@595
|
871 << layer << " [" << layer->objectName() << "]"
|
Chris@595
|
872 << " is still used in " << m_layerViewMap[layer].size()
|
Chris@595
|
873 << " views!" << endl;
|
Chris@45
|
874
|
Chris@595
|
875 if (force) {
|
Chris@45
|
876
|
Chris@77
|
877 #ifdef DEBUG_DOCUMENT
|
Chris@660
|
878 SVCERR << "(force flag set -- deleting from all views)" << endl;
|
Chris@77
|
879 #endif
|
Chris@45
|
880
|
Chris@595
|
881 for (std::set<View *>::iterator j = m_layerViewMap[layer].begin();
|
Chris@595
|
882 j != m_layerViewMap[layer].end(); ++j) {
|
Chris@595
|
883 // don't use removeLayerFromView, as it issues a command
|
Chris@595
|
884 layer->setLayerDormant(*j, true);
|
Chris@595
|
885 (*j)->removeLayer(layer);
|
Chris@595
|
886 }
|
Chris@595
|
887
|
Chris@595
|
888 m_layerViewMap.erase(layer);
|
Chris@45
|
889
|
Chris@595
|
890 } else {
|
Chris@595
|
891 return;
|
Chris@595
|
892 }
|
Chris@45
|
893 }
|
Chris@45
|
894
|
Chris@663
|
895 bool found = false;
|
Chris@663
|
896 for (auto itr = m_layers.begin(); itr != m_layers.end(); ++itr) {
|
Chris@663
|
897 if (*itr == layer) {
|
Chris@663
|
898 found = true;
|
Chris@663
|
899 m_layers.erase(itr);
|
Chris@663
|
900 break;
|
Chris@663
|
901 }
|
Chris@663
|
902 }
|
Chris@663
|
903 if (!found) {
|
Chris@595
|
904 SVDEBUG << "Document::deleteLayer: Layer "
|
Chris@665
|
905 << layer << " (typeid " << typeid(layer).name() <<
|
Chris@212
|
906 ") does not exist, or has already been deleted "
|
Chris@595
|
907 << "(this may not be as serious as it sounds)" << endl;
|
Chris@595
|
908 return;
|
Chris@45
|
909 }
|
Chris@45
|
910
|
Chris@132
|
911 #ifdef DEBUG_DOCUMENT
|
Chris@665
|
912 SVDEBUG << "Document::deleteLayer: Removing (and about to release model), now have "
|
Chris@229
|
913 << m_layers.size() << " layers" << endl;
|
Chris@132
|
914 #endif
|
Chris@52
|
915
|
Chris@45
|
916 releaseModel(layer->getModel());
|
Chris@45
|
917 emit layerRemoved(layer);
|
Chris@45
|
918 emit layerAboutToBeDeleted(layer);
|
Chris@45
|
919 delete layer;
|
Chris@45
|
920 }
|
Chris@45
|
921
|
Chris@45
|
922 void
|
Chris@45
|
923 Document::setModel(Layer *layer, Model *model)
|
Chris@45
|
924 {
|
Chris@45
|
925 if (model &&
|
Chris@595
|
926 model != m_mainModel &&
|
Chris@661
|
927 findModelInList(model) == m_models.end()) {
|
Chris@661
|
928 SVCERR << "ERROR: Document::setModel: Layer " << layer
|
Chris@595
|
929 << " (\"" << layer->objectName()
|
Chris@45
|
930 << "\") wants to use unregistered model " << model
|
Chris@595
|
931 << ": register the layer's model before setting it!"
|
Chris@595
|
932 << endl;
|
Chris@595
|
933 return;
|
Chris@45
|
934 }
|
Chris@45
|
935
|
Chris@45
|
936 Model *previousModel = layer->getModel();
|
Chris@45
|
937
|
Chris@45
|
938 if (previousModel == model) {
|
Chris@233
|
939 SVDEBUG << "NOTE: Document::setModel: Layer " << layer << " (\""
|
Chris@229
|
940 << layer->objectName() << "\") is already set to model "
|
Chris@45
|
941 << model << " (\""
|
Chris@229
|
942 << (model ? model->objectName(): "(null)")
|
Chris@229
|
943 << "\")" << endl;
|
Chris@45
|
944 return;
|
Chris@45
|
945 }
|
Chris@45
|
946
|
Chris@45
|
947 if (model && model != m_mainModel) {
|
Chris@661
|
948 ModelList::iterator mitr = findModelInList(model);
|
Chris@661
|
949 if (mitr != m_models.end()) {
|
Chris@661
|
950 mitr->refcount ++;
|
Chris@661
|
951 }
|
Chris@45
|
952 }
|
Chris@45
|
953
|
Chris@45
|
954 if (model && previousModel) {
|
Chris@45
|
955 PlayParameterRepository::getInstance()->copyParameters
|
Chris@45
|
956 (previousModel, model);
|
Chris@45
|
957 }
|
Chris@45
|
958
|
Chris@45
|
959 LayerFactory::getInstance()->setModel(layer, model);
|
Chris@45
|
960
|
Chris@45
|
961 if (previousModel) {
|
Chris@45
|
962 releaseModel(previousModel);
|
Chris@45
|
963 }
|
Chris@45
|
964 }
|
Chris@45
|
965
|
Chris@45
|
966 void
|
Chris@45
|
967 Document::setChannel(Layer *layer, int channel)
|
Chris@45
|
968 {
|
Chris@45
|
969 LayerFactory::getInstance()->setChannel(layer, channel);
|
Chris@45
|
970 }
|
Chris@45
|
971
|
Chris@45
|
972 void
|
Chris@45
|
973 Document::addLayerToView(View *view, Layer *layer)
|
Chris@45
|
974 {
|
Chris@45
|
975 Model *model = layer->getModel();
|
Chris@45
|
976 if (!model) {
|
Chris@77
|
977 #ifdef DEBUG_DOCUMENT
|
Chris@595
|
978 SVDEBUG << "Document::addLayerToView: Layer (\""
|
Chris@660
|
979 << layer->objectName()
|
Chris@660
|
980 << "\") with no model being added to view: "
|
Chris@229
|
981 << "normally you want to set the model first" << endl;
|
Chris@77
|
982 #endif
|
Chris@45
|
983 } else {
|
Chris@595
|
984 if (model != m_mainModel &&
|
Chris@661
|
985 findModelInList(model) == m_models.end()) {
|
Chris@661
|
986 SVCERR << "ERROR: Document::addLayerToView: Layer " << layer
|
Chris@595
|
987 << " has unregistered model " << model
|
Chris@595
|
988 << " -- register the layer's model before adding the layer!" << endl;
|
Chris@595
|
989 return;
|
Chris@595
|
990 }
|
Chris@45
|
991 }
|
Chris@45
|
992
|
Chris@45
|
993 CommandHistory::getInstance()->addCommand
|
Chris@595
|
994 (new Document::AddLayerCommand(this, view, layer));
|
Chris@45
|
995 }
|
Chris@45
|
996
|
Chris@45
|
997 void
|
Chris@45
|
998 Document::removeLayerFromView(View *view, Layer *layer)
|
Chris@45
|
999 {
|
Chris@45
|
1000 CommandHistory::getInstance()->addCommand
|
Chris@595
|
1001 (new Document::RemoveLayerCommand(this, view, layer));
|
Chris@45
|
1002 }
|
Chris@45
|
1003
|
Chris@45
|
1004 void
|
Chris@45
|
1005 Document::addToLayerViewMap(Layer *layer, View *view)
|
Chris@45
|
1006 {
|
Chris@45
|
1007 bool firstView = (m_layerViewMap.find(layer) == m_layerViewMap.end() ||
|
Chris@45
|
1008 m_layerViewMap[layer].empty());
|
Chris@45
|
1009
|
Chris@45
|
1010 if (m_layerViewMap[layer].find(view) !=
|
Chris@595
|
1011 m_layerViewMap[layer].end()) {
|
Chris@660
|
1012 SVCERR << "WARNING: Document::addToLayerViewMap:"
|
Chris@595
|
1013 << " Layer " << layer << " -> view " << view << " already in"
|
Chris@595
|
1014 << " layer view map -- internal inconsistency" << endl;
|
Chris@45
|
1015 }
|
Chris@45
|
1016
|
Chris@45
|
1017 m_layerViewMap[layer].insert(view);
|
Chris@45
|
1018
|
Chris@45
|
1019 if (firstView) emit layerInAView(layer, true);
|
Chris@45
|
1020 }
|
Chris@45
|
1021
|
Chris@45
|
1022 void
|
Chris@45
|
1023 Document::removeFromLayerViewMap(Layer *layer, View *view)
|
Chris@45
|
1024 {
|
Chris@45
|
1025 if (m_layerViewMap[layer].find(view) ==
|
Chris@595
|
1026 m_layerViewMap[layer].end()) {
|
Chris@660
|
1027 SVCERR << "WARNING: Document::removeFromLayerViewMap:"
|
Chris@595
|
1028 << " Layer " << layer << " -> view " << view << " not in"
|
Chris@595
|
1029 << " layer view map -- internal inconsistency" << endl;
|
Chris@45
|
1030 }
|
Chris@45
|
1031
|
Chris@45
|
1032 m_layerViewMap[layer].erase(view);
|
Chris@45
|
1033
|
Chris@45
|
1034 if (m_layerViewMap[layer].empty()) {
|
Chris@45
|
1035 m_layerViewMap.erase(layer);
|
Chris@45
|
1036 emit layerInAView(layer, false);
|
Chris@45
|
1037 }
|
Chris@45
|
1038 }
|
Chris@45
|
1039
|
Chris@45
|
1040 QString
|
Chris@45
|
1041 Document::getUniqueLayerName(QString candidate)
|
Chris@45
|
1042 {
|
Chris@45
|
1043 for (int count = 1; ; ++count) {
|
Chris@45
|
1044
|
Chris@45
|
1045 QString adjusted =
|
Chris@45
|
1046 (count > 1 ? QString("%1 <%2>").arg(candidate).arg(count) :
|
Chris@45
|
1047 candidate);
|
Chris@45
|
1048
|
Chris@45
|
1049 bool duplicate = false;
|
Chris@45
|
1050
|
Chris@663
|
1051 for (auto i = m_layers.begin(); i != m_layers.end(); ++i) {
|
Chris@45
|
1052 if ((*i)->objectName() == adjusted) {
|
Chris@45
|
1053 duplicate = true;
|
Chris@45
|
1054 break;
|
Chris@45
|
1055 }
|
Chris@45
|
1056 }
|
Chris@45
|
1057
|
Chris@45
|
1058 if (!duplicate) return adjusted;
|
Chris@45
|
1059 }
|
Chris@45
|
1060 }
|
Chris@45
|
1061
|
Chris@45
|
1062 std::vector<Model *>
|
Chris@72
|
1063 Document::getTransformInputModels()
|
Chris@45
|
1064 {
|
Chris@45
|
1065 std::vector<Model *> models;
|
Chris@45
|
1066
|
Chris@45
|
1067 if (!m_mainModel) return models;
|
Chris@45
|
1068
|
Chris@45
|
1069 models.push_back(m_mainModel);
|
Chris@45
|
1070
|
Chris@45
|
1071 //!!! This will pick up all models, including those that aren't visible...
|
Chris@45
|
1072
|
Chris@661
|
1073 for (ModelRecord &rec: m_models) {
|
Chris@45
|
1074
|
Chris@661
|
1075 Model *model = rec.model;
|
Chris@45
|
1076 if (!model || model == m_mainModel) continue;
|
Chris@45
|
1077 DenseTimeValueModel *dtvm = dynamic_cast<DenseTimeValueModel *>(model);
|
Chris@45
|
1078
|
Chris@45
|
1079 if (dtvm) {
|
Chris@45
|
1080 models.push_back(dtvm);
|
Chris@45
|
1081 }
|
Chris@45
|
1082 }
|
Chris@45
|
1083
|
Chris@45
|
1084 return models;
|
Chris@45
|
1085 }
|
Chris@45
|
1086
|
Chris@50
|
1087 bool
|
Chris@77
|
1088 Document::isKnownModel(const Model *model) const
|
Chris@77
|
1089 {
|
Chris@77
|
1090 if (model == m_mainModel) return true;
|
Chris@661
|
1091 for (const ModelRecord &rec: m_models) {
|
Chris@661
|
1092 if (rec.model == model) return true;
|
Chris@661
|
1093 }
|
Chris@661
|
1094 return false;
|
Chris@77
|
1095 }
|
Chris@77
|
1096
|
Chris@428
|
1097 bool
|
Chris@428
|
1098 Document::canAlign()
|
Chris@90
|
1099 {
|
Chris@428
|
1100 return Align::canAlign();
|
Chris@50
|
1101 }
|
Chris@50
|
1102
|
Chris@45
|
1103 void
|
Chris@45
|
1104 Document::alignModel(Model *model)
|
Chris@45
|
1105 {
|
Chris@667
|
1106 SVDEBUG << "Document::alignModel(" << model << ") (main model is " << m_mainModel << ")" << endl;
|
Chris@387
|
1107
|
Chris@387
|
1108 if (!m_mainModel) {
|
Chris@387
|
1109 SVDEBUG << "(no main model to align to)" << endl;
|
Chris@387
|
1110 return;
|
Chris@387
|
1111 }
|
Chris@45
|
1112
|
Chris@45
|
1113 RangeSummarisableTimeValueModel *rm =
|
Chris@45
|
1114 dynamic_cast<RangeSummarisableTimeValueModel *>(model);
|
Chris@387
|
1115 if (!rm) {
|
Chris@667
|
1116 SVDEBUG << "(model " << rm << " is not an alignable sort)" << endl;
|
Chris@387
|
1117 return;
|
Chris@387
|
1118 }
|
Chris@48
|
1119
|
Chris@86
|
1120 if (rm->getAlignmentReference() == m_mainModel) {
|
Chris@387
|
1121 SVDEBUG << "(model " << rm << " is already aligned to main model " << m_mainModel << ")" << endl;
|
Chris@86
|
1122 return;
|
Chris@86
|
1123 }
|
Chris@45
|
1124
|
Chris@86
|
1125 if (model == m_mainModel) {
|
Chris@86
|
1126 // The reference has an empty alignment to itself. This makes
|
Chris@86
|
1127 // it possible to distinguish between the reference and any
|
Chris@86
|
1128 // unaligned model just by looking at the model itself,
|
Chris@86
|
1129 // without also knowing what the main model is
|
Chris@667
|
1130 SVDEBUG << "Document::alignModel(" << model << "): is main model, setting alignment to itself appropriately" << endl;
|
Chris@665
|
1131 rm->setAlignment(new AlignmentModel(model, model, nullptr));
|
Chris@86
|
1132 return;
|
Chris@86
|
1133 }
|
Chris@86
|
1134
|
Chris@667
|
1135 SVDEBUG << "Document::alignModel: aligning..." << endl;
|
Chris@667
|
1136 if (rm->getAlignmentReference() != nullptr) {
|
Chris@667
|
1137 SVDEBUG << "(Note: model " << rm << " is currently aligned to model "
|
Chris@667
|
1138 << rm->getAlignmentReference() << "; this will replace that)"
|
Chris@667
|
1139 << endl;
|
Chris@667
|
1140 }
|
Chris@667
|
1141
|
Chris@665
|
1142 if (!m_align->alignModel(this, m_mainModel, rm)) {
|
Chris@665
|
1143 SVCERR << "Alignment failed: " << m_align->getError() << endl;
|
Chris@423
|
1144 emit alignmentFailed(m_align->getError());
|
Chris@64
|
1145 }
|
Chris@45
|
1146 }
|
Chris@45
|
1147
|
Chris@45
|
1148 void
|
Chris@45
|
1149 Document::alignModels()
|
Chris@45
|
1150 {
|
Chris@661
|
1151 for (const ModelRecord &rec: m_models) {
|
Chris@661
|
1152 alignModel(rec.model);
|
Chris@45
|
1153 }
|
Chris@86
|
1154 alignModel(m_mainModel);
|
Chris@45
|
1155 }
|
Chris@45
|
1156
|
Chris@45
|
1157 Document::AddLayerCommand::AddLayerCommand(Document *d,
|
Chris@595
|
1158 View *view,
|
Chris@595
|
1159 Layer *layer) :
|
Chris@45
|
1160 m_d(d),
|
Chris@45
|
1161 m_view(view),
|
Chris@45
|
1162 m_layer(layer),
|
Chris@45
|
1163 m_name(qApp->translate("AddLayerCommand", "Add %1 Layer").arg(layer->objectName())),
|
Chris@45
|
1164 m_added(false)
|
Chris@45
|
1165 {
|
Chris@45
|
1166 }
|
Chris@45
|
1167
|
Chris@45
|
1168 Document::AddLayerCommand::~AddLayerCommand()
|
Chris@45
|
1169 {
|
Chris@77
|
1170 #ifdef DEBUG_DOCUMENT
|
Chris@233
|
1171 SVDEBUG << "Document::AddLayerCommand::~AddLayerCommand" << endl;
|
Chris@77
|
1172 #endif
|
Chris@45
|
1173 if (!m_added) {
|
Chris@595
|
1174 m_d->deleteLayer(m_layer);
|
Chris@45
|
1175 }
|
Chris@45
|
1176 }
|
Chris@45
|
1177
|
Chris@159
|
1178 QString
|
Chris@159
|
1179 Document::AddLayerCommand::getName() const
|
Chris@159
|
1180 {
|
Chris@165
|
1181 #ifdef DEBUG_DOCUMENT
|
Chris@233
|
1182 SVDEBUG << "Document::AddLayerCommand::getName(): Name is "
|
Chris@229
|
1183 << m_name << endl;
|
Chris@165
|
1184 #endif
|
Chris@159
|
1185 return m_name;
|
Chris@159
|
1186 }
|
Chris@159
|
1187
|
Chris@45
|
1188 void
|
Chris@45
|
1189 Document::AddLayerCommand::execute()
|
Chris@45
|
1190 {
|
Chris@45
|
1191 for (int i = 0; i < m_view->getLayerCount(); ++i) {
|
Chris@595
|
1192 if (m_view->getLayer(i) == m_layer) {
|
Chris@595
|
1193 // already there
|
Chris@595
|
1194 m_layer->setLayerDormant(m_view, false);
|
Chris@595
|
1195 m_added = true;
|
Chris@595
|
1196 return;
|
Chris@595
|
1197 }
|
Chris@45
|
1198 }
|
Chris@45
|
1199
|
Chris@45
|
1200 m_view->addLayer(m_layer);
|
Chris@45
|
1201 m_layer->setLayerDormant(m_view, false);
|
Chris@45
|
1202
|
Chris@45
|
1203 m_d->addToLayerViewMap(m_layer, m_view);
|
Chris@45
|
1204 m_added = true;
|
Chris@45
|
1205 }
|
Chris@45
|
1206
|
Chris@45
|
1207 void
|
Chris@45
|
1208 Document::AddLayerCommand::unexecute()
|
Chris@45
|
1209 {
|
Chris@45
|
1210 m_view->removeLayer(m_layer);
|
Chris@45
|
1211 m_layer->setLayerDormant(m_view, true);
|
Chris@45
|
1212
|
Chris@45
|
1213 m_d->removeFromLayerViewMap(m_layer, m_view);
|
Chris@45
|
1214 m_added = false;
|
Chris@45
|
1215 }
|
Chris@45
|
1216
|
Chris@45
|
1217 Document::RemoveLayerCommand::RemoveLayerCommand(Document *d,
|
Chris@595
|
1218 View *view,
|
Chris@595
|
1219 Layer *layer) :
|
Chris@45
|
1220 m_d(d),
|
Chris@45
|
1221 m_view(view),
|
Chris@45
|
1222 m_layer(layer),
|
Chris@339
|
1223 m_wasDormant(layer->isLayerDormant(view)),
|
Chris@45
|
1224 m_name(qApp->translate("RemoveLayerCommand", "Delete %1 Layer").arg(layer->objectName())),
|
Chris@45
|
1225 m_added(true)
|
Chris@45
|
1226 {
|
Chris@45
|
1227 }
|
Chris@45
|
1228
|
Chris@45
|
1229 Document::RemoveLayerCommand::~RemoveLayerCommand()
|
Chris@45
|
1230 {
|
Chris@77
|
1231 #ifdef DEBUG_DOCUMENT
|
Chris@233
|
1232 SVDEBUG << "Document::RemoveLayerCommand::~RemoveLayerCommand" << endl;
|
Chris@77
|
1233 #endif
|
Chris@45
|
1234 if (!m_added) {
|
Chris@595
|
1235 m_d->deleteLayer(m_layer);
|
Chris@45
|
1236 }
|
Chris@45
|
1237 }
|
Chris@45
|
1238
|
Chris@159
|
1239 QString
|
Chris@159
|
1240 Document::RemoveLayerCommand::getName() const
|
Chris@159
|
1241 {
|
Chris@171
|
1242 #ifdef DEBUG_DOCUMENT
|
Chris@233
|
1243 SVDEBUG << "Document::RemoveLayerCommand::getName(): Name is "
|
Chris@229
|
1244 << m_name << endl;
|
Chris@171
|
1245 #endif
|
Chris@159
|
1246 return m_name;
|
Chris@159
|
1247 }
|
Chris@159
|
1248
|
Chris@45
|
1249 void
|
Chris@45
|
1250 Document::RemoveLayerCommand::execute()
|
Chris@45
|
1251 {
|
Chris@45
|
1252 bool have = false;
|
Chris@45
|
1253 for (int i = 0; i < m_view->getLayerCount(); ++i) {
|
Chris@595
|
1254 if (m_view->getLayer(i) == m_layer) {
|
Chris@595
|
1255 have = true;
|
Chris@595
|
1256 break;
|
Chris@595
|
1257 }
|
Chris@45
|
1258 }
|
Chris@45
|
1259
|
Chris@45
|
1260 if (!have) { // not there!
|
Chris@595
|
1261 m_layer->setLayerDormant(m_view, true);
|
Chris@595
|
1262 m_added = false;
|
Chris@595
|
1263 return;
|
Chris@45
|
1264 }
|
Chris@45
|
1265
|
Chris@45
|
1266 m_view->removeLayer(m_layer);
|
Chris@45
|
1267 m_layer->setLayerDormant(m_view, true);
|
Chris@45
|
1268
|
Chris@45
|
1269 m_d->removeFromLayerViewMap(m_layer, m_view);
|
Chris@45
|
1270 m_added = false;
|
Chris@45
|
1271 }
|
Chris@45
|
1272
|
Chris@45
|
1273 void
|
Chris@45
|
1274 Document::RemoveLayerCommand::unexecute()
|
Chris@45
|
1275 {
|
Chris@45
|
1276 m_view->addLayer(m_layer);
|
Chris@339
|
1277 m_layer->setLayerDormant(m_view, m_wasDormant);
|
Chris@45
|
1278
|
Chris@45
|
1279 m_d->addToLayerViewMap(m_layer, m_view);
|
Chris@45
|
1280 m_added = true;
|
Chris@45
|
1281 }
|
Chris@45
|
1282
|
Chris@45
|
1283 void
|
Chris@45
|
1284 Document::toXml(QTextStream &out, QString indent, QString extraAttributes) const
|
Chris@45
|
1285 {
|
Chris@226
|
1286 toXml(out, indent, extraAttributes, false);
|
Chris@226
|
1287 }
|
Chris@226
|
1288
|
Chris@226
|
1289 void
|
Chris@226
|
1290 Document::toXmlAsTemplate(QTextStream &out, QString indent, QString extraAttributes) const
|
Chris@226
|
1291 {
|
Chris@226
|
1292 toXml(out, indent, extraAttributes, true);
|
Chris@226
|
1293 }
|
Chris@226
|
1294
|
Chris@226
|
1295 void
|
Chris@226
|
1296 Document::toXml(QTextStream &out, QString indent, QString extraAttributes,
|
Chris@226
|
1297 bool asTemplate) const
|
Chris@226
|
1298 {
|
Chris@45
|
1299 out << indent + QString("<data%1%2>\n")
|
Chris@45
|
1300 .arg(extraAttributes == "" ? "" : " ").arg(extraAttributes);
|
Chris@45
|
1301
|
Chris@45
|
1302 if (m_mainModel) {
|
Chris@108
|
1303
|
Chris@660
|
1304 #ifdef DEBUG_DOCUMENT
|
Chris@660
|
1305 SVDEBUG << "Document::toXml: writing main model" << endl;
|
Chris@660
|
1306 #endif
|
Chris@660
|
1307
|
Chris@226
|
1308 if (asTemplate) {
|
Chris@226
|
1309 writePlaceholderMainModel(out, indent + " ");
|
Chris@226
|
1310 } else {
|
Chris@226
|
1311 m_mainModel->toXml(out, indent + " ", "mainModel=\"true\"");
|
Chris@226
|
1312 }
|
Chris@108
|
1313
|
Chris@108
|
1314 PlayParameters *playParameters =
|
Chris@108
|
1315 PlayParameterRepository::getInstance()->getPlayParameters(m_mainModel);
|
Chris@108
|
1316 if (playParameters) {
|
Chris@108
|
1317 playParameters->toXml
|
Chris@108
|
1318 (out, indent + " ",
|
Chris@108
|
1319 QString("model=\"%1\"")
|
Chris@657
|
1320 .arg(m_mainModel->getExportId()));
|
Chris@108
|
1321 }
|
Chris@660
|
1322 } else {
|
Chris@660
|
1323 #ifdef DEBUG_DOCUMENT
|
Chris@660
|
1324 SVDEBUG << "Document::toXml: have no main model to write" << endl;
|
Chris@660
|
1325 #endif
|
Chris@45
|
1326 }
|
Chris@45
|
1327
|
Chris@45
|
1328 // Models that are not used in a layer that is in a view should
|
Chris@45
|
1329 // not be written. Get our list of required models first.
|
Chris@45
|
1330
|
Chris@45
|
1331 std::set<const Model *> used;
|
Chris@45
|
1332
|
Chris@45
|
1333 for (LayerViewMap::const_iterator i = m_layerViewMap.begin();
|
Chris@45
|
1334 i != m_layerViewMap.end(); ++i) {
|
Chris@45
|
1335
|
Chris@589
|
1336 if (i->first && !i->second.empty()) { // Layer exists, is in views
|
Chris@589
|
1337 Model *m = i->first->getModel();
|
Chris@589
|
1338 if (m) {
|
Chris@589
|
1339 used.insert(m);
|
Chris@589
|
1340 if (m->getSourceModel()) {
|
Chris@589
|
1341 used.insert(m->getSourceModel());
|
Chris@589
|
1342 }
|
Chris@589
|
1343 }
|
Chris@45
|
1344 }
|
Chris@45
|
1345 }
|
Chris@45
|
1346
|
Chris@589
|
1347 // Write aggregate models first, so that when re-reading
|
Chris@589
|
1348 // derivations we already know about their existence. But only
|
Chris@589
|
1349 // those that are actually used
|
Chris@629
|
1350 //
|
Chris@629
|
1351 // Later note: This turns out not to be a great idea - we can't
|
Chris@629
|
1352 // use an aggregate model to drive a derivation unless its
|
Chris@629
|
1353 // component models have all also already been loaded. So we
|
Chris@629
|
1354 // really should have written non-aggregate read-only
|
Chris@629
|
1355 // (i.e. non-derived) wave-type models first, then aggregate
|
Chris@629
|
1356 // models, then models that have derivations. But we didn't do
|
Chris@629
|
1357 // that, so existing sessions will always have the aggregate
|
Chris@629
|
1358 // models first and we might as well stick with that.
|
Chris@589
|
1359
|
Chris@589
|
1360 for (std::set<Model *>::iterator i = m_aggregateModels.begin();
|
Chris@589
|
1361 i != m_aggregateModels.end(); ++i) {
|
Chris@589
|
1362
|
Chris@660
|
1363 #ifdef DEBUG_DOCUMENT
|
Chris@660
|
1364 SVDEBUG << "Document::toXml: checking aggregate model " << *i << endl;
|
Chris@660
|
1365 #endif
|
Chris@589
|
1366
|
Chris@589
|
1367 AggregateWaveModel *aggregate = qobject_cast<AggregateWaveModel *>(*i);
|
Chris@589
|
1368 if (!aggregate) continue;
|
Chris@589
|
1369 if (used.find(aggregate) == used.end()) {
|
Chris@660
|
1370 #ifdef DEBUG_DOCUMENT
|
Chris@589
|
1371 SVDEBUG << "(unused, skipping)" << endl;
|
Chris@660
|
1372 #endif
|
Chris@589
|
1373 continue;
|
Chris@589
|
1374 }
|
Chris@589
|
1375
|
Chris@660
|
1376 #ifdef DEBUG_DOCUMENT
|
Chris@589
|
1377 SVDEBUG << "(used, writing)" << endl;
|
Chris@660
|
1378 #endif
|
Chris@589
|
1379
|
Chris@589
|
1380 aggregate->toXml(out, indent + " ");
|
Chris@589
|
1381 }
|
Chris@589
|
1382
|
Chris@111
|
1383 std::set<Model *> written;
|
Chris@111
|
1384
|
Chris@629
|
1385 // Now write the other models in two passes: first the models that
|
Chris@629
|
1386 // aren't derived from anything (in case they are source
|
Chris@629
|
1387 // components for an aggregate model, in which case we need to
|
Chris@629
|
1388 // have seen them before we see any models derived from aggregates
|
Chris@629
|
1389 // that use them - see the lament above) and then the models that
|
Chris@629
|
1390 // have derivations.
|
Chris@45
|
1391
|
Chris@629
|
1392 const int nonDerivedPass = 0, derivedPass = 1;
|
Chris@629
|
1393 for (int pass = nonDerivedPass; pass <= derivedPass; ++pass) {
|
Chris@629
|
1394
|
Chris@661
|
1395 for (const ModelRecord &rec: m_models) {
|
Chris@45
|
1396
|
Chris@661
|
1397 Model *model = rec.model;
|
Chris@629
|
1398
|
Chris@629
|
1399 if (used.find(model) == used.end()) continue;
|
Chris@45
|
1400
|
Chris@660
|
1401 #ifdef DEBUG_DOCUMENT
|
Chris@660
|
1402 SVDEBUG << "Document::toXml: looking at model " << model
|
Chris@660
|
1403 << " (" << model->getTypeName() << ", \""
|
Chris@660
|
1404 << model->objectName() << "\") [pass = "
|
Chris@660
|
1405 << pass << "]" << endl;
|
Chris@660
|
1406 #endif
|
Chris@660
|
1407
|
Chris@629
|
1408 // We need an intelligent way to determine which models
|
Chris@629
|
1409 // need to be streamed (i.e. have been edited, or are
|
Chris@629
|
1410 // small) and which should not be (i.e. remain as
|
Chris@629
|
1411 // generated by a transform, and are large).
|
Chris@629
|
1412 //
|
Chris@629
|
1413 // At the moment we can get away with deciding not to
|
Chris@629
|
1414 // stream dense 3d models or writable wave file models,
|
Chris@629
|
1415 // provided they were generated from a transform, because
|
Chris@629
|
1416 // at the moment there is no way to edit those model types
|
Chris@629
|
1417 // so it should be safe to regenerate them. That won't
|
Chris@629
|
1418 // always work in future though. It would be particularly
|
Chris@629
|
1419 // nice to be able to ask the user, as well as making an
|
Chris@629
|
1420 // intelligent guess.
|
Chris@45
|
1421
|
Chris@629
|
1422 bool writeModel = true;
|
Chris@629
|
1423 bool haveDerivation = false;
|
Chris@629
|
1424
|
Chris@629
|
1425 if (rec.source && rec.transform.getIdentifier() != "") {
|
Chris@629
|
1426 haveDerivation = true;
|
Chris@629
|
1427 }
|
Chris@45
|
1428
|
Chris@629
|
1429 if (pass == nonDerivedPass) {
|
Chris@629
|
1430 if (haveDerivation) {
|
Chris@629
|
1431 SVDEBUG << "skipping derived model " << model->objectName() << " during nonDerivedPass" << endl;
|
Chris@629
|
1432 continue;
|
Chris@629
|
1433 }
|
Chris@629
|
1434 } else {
|
Chris@629
|
1435 if (!haveDerivation) {
|
Chris@629
|
1436 SVDEBUG << "skipping non-derived model " << model->objectName() << " during derivedPass" << endl;
|
Chris@629
|
1437 continue;
|
Chris@629
|
1438 }
|
Chris@629
|
1439 }
|
Chris@45
|
1440
|
Chris@629
|
1441 if (haveDerivation) {
|
Chris@629
|
1442 if (dynamic_cast<const WritableWaveFileModel *>(model)) {
|
Chris@629
|
1443 writeModel = false;
|
Chris@629
|
1444 } else if (dynamic_cast<const DenseThreeDimensionalModel *>(model)) {
|
Chris@629
|
1445 writeModel = false;
|
Chris@629
|
1446 }
|
Chris@629
|
1447 }
|
Chris@629
|
1448
|
Chris@629
|
1449 if (writeModel) {
|
Chris@629
|
1450 model->toXml(out, indent + " ");
|
Chris@629
|
1451 written.insert(model);
|
Chris@629
|
1452 }
|
Chris@629
|
1453
|
Chris@629
|
1454 if (haveDerivation) {
|
Chris@629
|
1455 writeBackwardCompatibleDerivation(out, indent + " ",
|
Chris@629
|
1456 model, rec);
|
Chris@629
|
1457 }
|
Chris@629
|
1458
|
Chris@629
|
1459 //!!! We should probably own the PlayParameterRepository
|
Chris@629
|
1460 PlayParameters *playParameters =
|
Chris@629
|
1461 PlayParameterRepository::getInstance()->getPlayParameters(model);
|
Chris@629
|
1462 if (playParameters) {
|
Chris@629
|
1463 playParameters->toXml
|
Chris@629
|
1464 (out, indent + " ",
|
Chris@629
|
1465 QString("model=\"%1\"")
|
Chris@657
|
1466 .arg(model->getExportId()));
|
Chris@45
|
1467 }
|
Chris@45
|
1468 }
|
Chris@45
|
1469 }
|
Chris@111
|
1470
|
Chris@111
|
1471 // We should write out the alignment models here. AlignmentModel
|
Chris@111
|
1472 // needs a toXml that writes out the export IDs of its reference
|
Chris@111
|
1473 // and aligned models, and then streams its path model. Note that
|
Chris@111
|
1474 // this will only work when the alignment is complete, so we
|
Chris@111
|
1475 // should probably wait for it if it isn't already by this point.
|
Chris@111
|
1476
|
Chris@111
|
1477 for (std::set<Model *>::const_iterator i = written.begin();
|
Chris@595
|
1478 i != written.end(); ++i) {
|
Chris@111
|
1479
|
Chris@111
|
1480 const Model *model = *i;
|
Chris@111
|
1481 const AlignmentModel *alignment = model->getAlignment();
|
Chris@111
|
1482 if (!alignment) continue;
|
Chris@111
|
1483
|
Chris@111
|
1484 alignment->toXml(out, indent + " ");
|
Chris@111
|
1485 }
|
Chris@111
|
1486
|
Chris@663
|
1487 for (auto i = m_layers.begin(); i != m_layers.end(); ++i) {
|
Chris@595
|
1488 (*i)->toXml(out, indent + " ");
|
Chris@45
|
1489 }
|
Chris@45
|
1490
|
Chris@45
|
1491 out << indent + "</data>\n";
|
Chris@45
|
1492 }
|
Chris@45
|
1493
|
Chris@72
|
1494 void
|
Chris@226
|
1495 Document::writePlaceholderMainModel(QTextStream &out, QString indent) const
|
Chris@226
|
1496 {
|
Chris@226
|
1497 out << indent;
|
Chris@226
|
1498 out << QString("<model id=\"%1\" name=\"placeholder\" sampleRate=\"%2\" type=\"wavefile\" file=\":samples/silent.wav\" mainModel=\"true\"/>\n")
|
Chris@657
|
1499 .arg(m_mainModel->getExportId())
|
Chris@226
|
1500 .arg(m_mainModel->getSampleRate());
|
Chris@226
|
1501 }
|
Chris@226
|
1502
|
Chris@226
|
1503 void
|
Chris@72
|
1504 Document::writeBackwardCompatibleDerivation(QTextStream &out, QString indent,
|
Chris@72
|
1505 Model *targetModel,
|
Chris@72
|
1506 const ModelRecord &rec) const
|
Chris@72
|
1507 {
|
Chris@72
|
1508 // There is a lot of redundancy in the XML we output here, because
|
Chris@72
|
1509 // we want it to work with older SV session file reading code as
|
Chris@72
|
1510 // well.
|
Chris@72
|
1511 //
|
Chris@72
|
1512 // Formerly, a transform was described using a derivation element
|
Chris@72
|
1513 // which set out the source and target models, execution context
|
Chris@72
|
1514 // (step size, input channel etc) and transform id, containing a
|
Chris@72
|
1515 // plugin element which set out the transform parameters and so
|
Chris@72
|
1516 // on. (The plugin element came from a "configurationXml" string
|
Chris@72
|
1517 // obtained from PluginXml.)
|
Chris@72
|
1518 //
|
Chris@72
|
1519 // This has been replaced by a derivation element setting out the
|
Chris@72
|
1520 // source and target models and input channel, containing a
|
Chris@72
|
1521 // transform element which sets out everything in the Transform.
|
Chris@72
|
1522 //
|
Chris@72
|
1523 // In order to retain compatibility with older SV code, however,
|
Chris@72
|
1524 // we have to write out the same stuff into the derivation as
|
Chris@72
|
1525 // before, and manufacture an appropriate plugin element as well
|
Chris@72
|
1526 // as the transform element. In order that newer code knows it's
|
Chris@72
|
1527 // dealing with a newer format, we will also write an attribute
|
Chris@72
|
1528 // 'type="transform"' in the derivation element.
|
Chris@45
|
1529
|
Chris@72
|
1530 const Transform &transform = rec.transform;
|
Chris@72
|
1531
|
Chris@72
|
1532 // Just for reference, this is what we would write if we didn't
|
Chris@72
|
1533 // have to be backward compatible:
|
Chris@72
|
1534 //
|
Chris@72
|
1535 // out << indent
|
Chris@72
|
1536 // << QString("<derivation type=\"transform\" source=\"%1\" "
|
Chris@72
|
1537 // "model=\"%2\" channel=\"%3\">\n")
|
Chris@657
|
1538 // .arg(rec.source->getExportId())
|
Chris@657
|
1539 // .arg(targetModel->getExportId())
|
Chris@72
|
1540 // .arg(rec.channel);
|
Chris@72
|
1541 //
|
Chris@72
|
1542 // transform.toXml(out, indent + " ");
|
Chris@72
|
1543 //
|
Chris@72
|
1544 // out << indent << "</derivation>\n";
|
Chris@72
|
1545 //
|
Chris@72
|
1546 // Unfortunately, we can't just do that. So we do this...
|
Chris@72
|
1547
|
Chris@72
|
1548 QString extentsAttributes;
|
Chris@72
|
1549 if (transform.getStartTime() != RealTime::zeroTime ||
|
Chris@72
|
1550 transform.getDuration() != RealTime::zeroTime) {
|
Chris@72
|
1551 extentsAttributes = QString("startFrame=\"%1\" duration=\"%2\" ")
|
Chris@72
|
1552 .arg(RealTime::realTime2Frame(transform.getStartTime(),
|
Chris@72
|
1553 targetModel->getSampleRate()))
|
Chris@72
|
1554 .arg(RealTime::realTime2Frame(transform.getDuration(),
|
Chris@72
|
1555 targetModel->getSampleRate()));
|
Chris@72
|
1556 }
|
Chris@595
|
1557
|
Chris@72
|
1558 out << indent;
|
Chris@72
|
1559 out << QString("<derivation type=\"transform\" source=\"%1\" "
|
Chris@72
|
1560 "model=\"%2\" channel=\"%3\" domain=\"%4\" "
|
Chris@72
|
1561 "stepSize=\"%5\" blockSize=\"%6\" %7windowType=\"%8\" "
|
Chris@72
|
1562 "transform=\"%9\">\n")
|
Chris@657
|
1563 .arg(rec.source->getExportId())
|
Chris@657
|
1564 .arg(targetModel->getExportId())
|
Chris@72
|
1565 .arg(rec.channel)
|
Chris@72
|
1566 .arg(TransformFactory::getInstance()->getTransformInputDomain
|
Chris@72
|
1567 (transform.getIdentifier()))
|
Chris@72
|
1568 .arg(transform.getStepSize())
|
Chris@72
|
1569 .arg(transform.getBlockSize())
|
Chris@72
|
1570 .arg(extentsAttributes)
|
Chris@72
|
1571 .arg(int(transform.getWindowType()))
|
Chris@72
|
1572 .arg(XmlExportable::encodeEntities(transform.getIdentifier()));
|
Chris@72
|
1573
|
Chris@72
|
1574 transform.toXml(out, indent + " ");
|
Chris@72
|
1575
|
Chris@72
|
1576 out << indent << " "
|
Chris@72
|
1577 << TransformFactory::getInstance()->getPluginConfigurationXml(transform);
|
Chris@72
|
1578
|
Chris@72
|
1579 out << indent << "</derivation>\n";
|
Chris@72
|
1580 }
|
Chris@72
|
1581
|