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