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