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