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@683
|
749 Document::releaseModel(ModelId modelId) // Will _not_ release main model!
|
Chris@45
|
750 {
|
Chris@683
|
751 //!!!
|
Chris@683
|
752
|
Chris@683
|
753 SVCERR << "Document::releaseModel(" << modelId << "): STILL TO REVIEW" << endl;
|
Chris@683
|
754
|
Chris@683
|
755 #ifdef NOT_DEFINED
|
Chris@683
|
756
|
Chris@683
|
757 if (modelId.isNone()) {
|
Chris@595
|
758 return;
|
Chris@45
|
759 }
|
Chris@45
|
760
|
Chris@683
|
761 auto model = ModelById::get(modelId);
|
Chris@683
|
762 if (!model) {
|
Chris@683
|
763 return;
|
Chris@683
|
764 }
|
Chris@683
|
765
|
Chris@665
|
766 #ifdef DEBUG_DOCUMENT
|
Chris@683
|
767 SVDEBUG << "Document::releaseModel(" << modelId << ", type "
|
Chris@665
|
768 << model->getTypeName() << ", name \""
|
Chris@665
|
769 << model->objectName() << "\")" << endl;
|
Chris@665
|
770 #endif
|
Chris@665
|
771
|
Chris@683
|
772 if (modelId == m_mainModel) {
|
Chris@595
|
773 return;
|
Chris@45
|
774 }
|
Chris@45
|
775
|
Chris@45
|
776 bool toDelete = false;
|
Chris@676
|
777 bool isInModelList = false; // should become true for any "normal" model
|
Chris@45
|
778
|
Chris@683
|
779 if (m_models.find(modelId) != m_models.end()) {
|
Chris@676
|
780
|
Chris@661
|
781 if (mitr->refcount == 0) {
|
Chris@595
|
782 SVCERR << "WARNING: Document::releaseModel: model " << model
|
Chris@588
|
783 << " reference count is zero already!" << endl;
|
Chris@595
|
784 } else {
|
Chris@664
|
785 #ifdef DEBUG_DOCUMENT
|
Chris@664
|
786 SVDEBUG << "Lowering refcount from " << mitr->refcount << endl;
|
Chris@664
|
787 #endif
|
Chris@661
|
788 if (--mitr->refcount == 0) {
|
Chris@595
|
789 toDelete = true;
|
Chris@595
|
790 }
|
Chris@595
|
791 }
|
Chris@676
|
792 isInModelList = true;
|
Chris@676
|
793
|
Chris@588
|
794 } else if (m_aggregateModels.find(model) != m_aggregateModels.end()) {
|
Chris@665
|
795 #ifdef DEBUG_DOCUMENT
|
Chris@588
|
796 SVDEBUG << "Document::releaseModel: is an aggregate model" << endl;
|
Chris@665
|
797 #endif
|
Chris@588
|
798 toDelete = true;
|
Chris@45
|
799 } else {
|
Chris@595
|
800 SVCERR << "WARNING: Document::releaseModel: Unfound model "
|
Chris@588
|
801 << model << endl;
|
Chris@595
|
802 toDelete = true;
|
Chris@45
|
803 }
|
Chris@45
|
804
|
Chris@45
|
805 if (toDelete) {
|
Chris@45
|
806
|
Chris@595
|
807 int sourceCount = 0;
|
Chris@45
|
808
|
Chris@661
|
809 for (auto &rec: m_models) {
|
Chris@661
|
810 if (rec.source == model) {
|
Chris@595
|
811 ++sourceCount;
|
Chris@661
|
812 rec.source = nullptr;
|
Chris@595
|
813 }
|
Chris@595
|
814 }
|
Chris@45
|
815
|
Chris@595
|
816 if (sourceCount > 0) {
|
Chris@595
|
817 SVDEBUG << "Document::releaseModel: Deleting model "
|
Chris@588
|
818 << model << " even though it is source for "
|
Chris@588
|
819 << sourceCount << " other derived model(s) -- resetting "
|
Chris@588
|
820 << "their source fields appropriately" << endl;
|
Chris@595
|
821 }
|
Chris@45
|
822
|
Chris@676
|
823 if (isInModelList) {
|
Chris@676
|
824 deleteModelFromList(model);
|
Chris@137
|
825
|
Chris@137
|
826 #ifdef DEBUG_DOCUMENT
|
Chris@676
|
827 SVDEBUG << "Document::releaseModel: Deleted model " << model << endl;
|
Chris@676
|
828 SVDEBUG << "Models now: ";
|
Chris@676
|
829 for (const auto &r: m_models) {
|
Chris@676
|
830 SVDEBUG << r.model << " ";
|
Chris@676
|
831 }
|
Chris@676
|
832 SVDEBUG << endl;
|
Chris@137
|
833 #endif
|
Chris@676
|
834 } else {
|
Chris@676
|
835 model->aboutToDelete();
|
Chris@676
|
836 emit modelAboutToBeDeleted(model);
|
Chris@676
|
837 delete model;
|
Chris@676
|
838
|
Chris@676
|
839 #ifdef DEBUG_DOCUMENT
|
Chris@676
|
840 SVDEBUG << "Document::releaseModel: Deleted awkward model " << model << endl;
|
Chris@676
|
841 #endif
|
Chris@676
|
842 }
|
Chris@45
|
843 }
|
Chris@683
|
844
|
Chris@683
|
845 #endif
|
Chris@45
|
846 }
|
Chris@45
|
847
|
Chris@45
|
848 void
|
Chris@45
|
849 Document::deleteLayer(Layer *layer, bool force)
|
Chris@45
|
850 {
|
Chris@45
|
851 if (m_layerViewMap.find(layer) != m_layerViewMap.end() &&
|
Chris@595
|
852 m_layerViewMap[layer].size() > 0) {
|
Chris@45
|
853
|
Chris@595
|
854 if (force) {
|
Chris@45
|
855
|
Chris@672
|
856 SVDEBUG << "NOTE: Document::deleteLayer: Layer "
|
Chris@672
|
857 << layer << " [" << layer->objectName() << "]"
|
Chris@672
|
858 << " is still used in " << m_layerViewMap[layer].size()
|
Chris@672
|
859 << " views. Force flag set, so removing from them" << endl;
|
Chris@672
|
860
|
Chris@595
|
861 for (std::set<View *>::iterator j = m_layerViewMap[layer].begin();
|
Chris@595
|
862 j != m_layerViewMap[layer].end(); ++j) {
|
Chris@595
|
863 // don't use removeLayerFromView, as it issues a command
|
Chris@595
|
864 layer->setLayerDormant(*j, true);
|
Chris@595
|
865 (*j)->removeLayer(layer);
|
Chris@595
|
866 }
|
Chris@595
|
867
|
Chris@595
|
868 m_layerViewMap.erase(layer);
|
Chris@45
|
869
|
Chris@595
|
870 } else {
|
Chris@672
|
871
|
Chris@672
|
872 SVCERR << "WARNING: Document::deleteLayer: Layer "
|
Chris@672
|
873 << layer << " [" << layer->objectName() << "]"
|
Chris@672
|
874 << " is still used in " << m_layerViewMap[layer].size()
|
Chris@672
|
875 << " views! Force flag is not set, so not deleting" << endl;
|
Chris@672
|
876
|
Chris@595
|
877 return;
|
Chris@595
|
878 }
|
Chris@45
|
879 }
|
Chris@45
|
880
|
Chris@663
|
881 bool found = false;
|
Chris@663
|
882 for (auto itr = m_layers.begin(); itr != m_layers.end(); ++itr) {
|
Chris@663
|
883 if (*itr == layer) {
|
Chris@663
|
884 found = true;
|
Chris@663
|
885 m_layers.erase(itr);
|
Chris@663
|
886 break;
|
Chris@663
|
887 }
|
Chris@663
|
888 }
|
Chris@663
|
889 if (!found) {
|
Chris@595
|
890 SVDEBUG << "Document::deleteLayer: Layer "
|
Chris@665
|
891 << layer << " (typeid " << typeid(layer).name() <<
|
Chris@212
|
892 ") does not exist, or has already been deleted "
|
Chris@595
|
893 << "(this may not be as serious as it sounds)" << endl;
|
Chris@595
|
894 return;
|
Chris@45
|
895 }
|
Chris@45
|
896
|
Chris@132
|
897 #ifdef DEBUG_DOCUMENT
|
Chris@665
|
898 SVDEBUG << "Document::deleteLayer: Removing (and about to release model), now have "
|
Chris@229
|
899 << m_layers.size() << " layers" << endl;
|
Chris@132
|
900 #endif
|
Chris@52
|
901
|
Chris@45
|
902 releaseModel(layer->getModel());
|
Chris@45
|
903 emit layerRemoved(layer);
|
Chris@45
|
904 emit layerAboutToBeDeleted(layer);
|
Chris@45
|
905 delete layer;
|
Chris@45
|
906 }
|
Chris@45
|
907
|
Chris@45
|
908 void
|
Chris@683
|
909 Document::setModel(Layer *layer, ModelId modelId)
|
Chris@45
|
910 {
|
Chris@683
|
911 if (!modelId.isNone() &&
|
Chris@683
|
912 modelId != m_mainModel &&
|
Chris@683
|
913 m_models.find(modelId) == m_models.end()) {
|
Chris@661
|
914 SVCERR << "ERROR: Document::setModel: Layer " << layer
|
Chris@683
|
915 << " (\"" << layer->objectName()
|
Chris@683
|
916 << "\") wants to use unregistered model " << modelId
|
Chris@683
|
917 << ": register the layer's model before setting it!"
|
Chris@683
|
918 << endl;
|
Chris@595
|
919 return;
|
Chris@45
|
920 }
|
Chris@45
|
921
|
Chris@683
|
922 ModelId previousModel = layer->getModel();
|
Chris@45
|
923
|
Chris@683
|
924 if (previousModel == modelId) {
|
Chris@233
|
925 SVDEBUG << "NOTE: Document::setModel: Layer " << layer << " (\""
|
Chris@683
|
926 << layer->objectName()
|
Chris@683
|
927 << "\") is already set to model "
|
Chris@683
|
928 << modelId << endl;
|
Chris@45
|
929 return;
|
Chris@45
|
930 }
|
Chris@683
|
931 /*!!!
|
Chris@45
|
932 if (model && model != m_mainModel) {
|
Chris@661
|
933 ModelList::iterator mitr = findModelInList(model);
|
Chris@661
|
934 if (mitr != m_models.end()) {
|
Chris@661
|
935 mitr->refcount ++;
|
Chris@661
|
936 }
|
Chris@45
|
937 }
|
Chris@683
|
938 */
|
Chris@683
|
939 if (!modelId.isNone() && !previousModel.isNone()) {
|
Chris@45
|
940 PlayParameterRepository::getInstance()->copyParameters
|
Chris@683
|
941 (previousModel.untyped, modelId.untyped);
|
Chris@45
|
942 }
|
Chris@45
|
943
|
Chris@683
|
944 LayerFactory::getInstance()->setModel(layer, modelId);
|
Chris@45
|
945
|
Chris@683
|
946 releaseModel(previousModel);
|
Chris@45
|
947 }
|
Chris@45
|
948
|
Chris@45
|
949 void
|
Chris@45
|
950 Document::setChannel(Layer *layer, int channel)
|
Chris@45
|
951 {
|
Chris@45
|
952 LayerFactory::getInstance()->setChannel(layer, channel);
|
Chris@45
|
953 }
|
Chris@45
|
954
|
Chris@45
|
955 void
|
Chris@45
|
956 Document::addLayerToView(View *view, Layer *layer)
|
Chris@45
|
957 {
|
Chris@683
|
958 ModelId modelId = layer->getModel();
|
Chris@683
|
959 if (modelId.isNone()) {
|
Chris@77
|
960 #ifdef DEBUG_DOCUMENT
|
Chris@595
|
961 SVDEBUG << "Document::addLayerToView: Layer (\""
|
Chris@660
|
962 << layer->objectName()
|
Chris@660
|
963 << "\") with no model being added to view: "
|
Chris@229
|
964 << "normally you want to set the model first" << endl;
|
Chris@77
|
965 #endif
|
Chris@45
|
966 } else {
|
Chris@683
|
967 if (modelId != m_mainModel &&
|
Chris@683
|
968 m_models.find(modelId) == m_models.end()) {
|
Chris@661
|
969 SVCERR << "ERROR: Document::addLayerToView: Layer " << layer
|
Chris@683
|
970 << " has unregistered model " << modelId
|
Chris@595
|
971 << " -- register the layer's model before adding the layer!" << endl;
|
Chris@595
|
972 return;
|
Chris@595
|
973 }
|
Chris@45
|
974 }
|
Chris@45
|
975
|
Chris@45
|
976 CommandHistory::getInstance()->addCommand
|
Chris@595
|
977 (new Document::AddLayerCommand(this, view, layer));
|
Chris@45
|
978 }
|
Chris@45
|
979
|
Chris@45
|
980 void
|
Chris@45
|
981 Document::removeLayerFromView(View *view, Layer *layer)
|
Chris@45
|
982 {
|
Chris@45
|
983 CommandHistory::getInstance()->addCommand
|
Chris@595
|
984 (new Document::RemoveLayerCommand(this, view, layer));
|
Chris@45
|
985 }
|
Chris@45
|
986
|
Chris@45
|
987 void
|
Chris@45
|
988 Document::addToLayerViewMap(Layer *layer, View *view)
|
Chris@45
|
989 {
|
Chris@45
|
990 bool firstView = (m_layerViewMap.find(layer) == m_layerViewMap.end() ||
|
Chris@45
|
991 m_layerViewMap[layer].empty());
|
Chris@45
|
992
|
Chris@45
|
993 if (m_layerViewMap[layer].find(view) !=
|
Chris@595
|
994 m_layerViewMap[layer].end()) {
|
Chris@660
|
995 SVCERR << "WARNING: Document::addToLayerViewMap:"
|
Chris@595
|
996 << " Layer " << layer << " -> view " << view << " already in"
|
Chris@595
|
997 << " layer view map -- internal inconsistency" << endl;
|
Chris@45
|
998 }
|
Chris@45
|
999
|
Chris@45
|
1000 m_layerViewMap[layer].insert(view);
|
Chris@45
|
1001
|
Chris@45
|
1002 if (firstView) emit layerInAView(layer, true);
|
Chris@45
|
1003 }
|
Chris@45
|
1004
|
Chris@45
|
1005 void
|
Chris@45
|
1006 Document::removeFromLayerViewMap(Layer *layer, View *view)
|
Chris@45
|
1007 {
|
Chris@45
|
1008 if (m_layerViewMap[layer].find(view) ==
|
Chris@595
|
1009 m_layerViewMap[layer].end()) {
|
Chris@660
|
1010 SVCERR << "WARNING: Document::removeFromLayerViewMap:"
|
Chris@595
|
1011 << " Layer " << layer << " -> view " << view << " not in"
|
Chris@595
|
1012 << " layer view map -- internal inconsistency" << endl;
|
Chris@45
|
1013 }
|
Chris@45
|
1014
|
Chris@45
|
1015 m_layerViewMap[layer].erase(view);
|
Chris@45
|
1016
|
Chris@45
|
1017 if (m_layerViewMap[layer].empty()) {
|
Chris@45
|
1018 m_layerViewMap.erase(layer);
|
Chris@45
|
1019 emit layerInAView(layer, false);
|
Chris@45
|
1020 }
|
Chris@45
|
1021 }
|
Chris@45
|
1022
|
Chris@45
|
1023 QString
|
Chris@45
|
1024 Document::getUniqueLayerName(QString candidate)
|
Chris@45
|
1025 {
|
Chris@45
|
1026 for (int count = 1; ; ++count) {
|
Chris@45
|
1027
|
Chris@45
|
1028 QString adjusted =
|
Chris@45
|
1029 (count > 1 ? QString("%1 <%2>").arg(candidate).arg(count) :
|
Chris@45
|
1030 candidate);
|
Chris@45
|
1031
|
Chris@45
|
1032 bool duplicate = false;
|
Chris@45
|
1033
|
Chris@663
|
1034 for (auto i = m_layers.begin(); i != m_layers.end(); ++i) {
|
Chris@45
|
1035 if ((*i)->objectName() == adjusted) {
|
Chris@45
|
1036 duplicate = true;
|
Chris@45
|
1037 break;
|
Chris@45
|
1038 }
|
Chris@45
|
1039 }
|
Chris@45
|
1040
|
Chris@45
|
1041 if (!duplicate) return adjusted;
|
Chris@45
|
1042 }
|
Chris@45
|
1043 }
|
Chris@45
|
1044
|
Chris@683
|
1045 std::vector<ModelId>
|
Chris@72
|
1046 Document::getTransformInputModels()
|
Chris@45
|
1047 {
|
Chris@683
|
1048 std::vector<ModelId> models;
|
Chris@45
|
1049
|
Chris@683
|
1050 if (m_mainModel.isNone()) return models;
|
Chris@45
|
1051
|
Chris@45
|
1052 models.push_back(m_mainModel);
|
Chris@45
|
1053
|
Chris@45
|
1054 //!!! This will pick up all models, including those that aren't visible...
|
Chris@45
|
1055
|
Chris@683
|
1056 for (auto rec: m_models) {
|
Chris@45
|
1057
|
Chris@683
|
1058 ModelId modelId = rec.first;
|
Chris@683
|
1059 if (modelId == m_mainModel) continue;
|
Chris@683
|
1060
|
Chris@683
|
1061 auto dtvm = ModelById::getAs<DenseTimeValueModel>(modelId);
|
Chris@45
|
1062 if (dtvm) {
|
Chris@683
|
1063 models.push_back(modelId);
|
Chris@45
|
1064 }
|
Chris@45
|
1065 }
|
Chris@45
|
1066
|
Chris@45
|
1067 return models;
|
Chris@45
|
1068 }
|
Chris@45
|
1069
|
Chris@683
|
1070 //!!! what is this used for?
|
Chris@50
|
1071 bool
|
Chris@683
|
1072 Document::isKnownModel(const ModelId modelId) const
|
Chris@77
|
1073 {
|
Chris@683
|
1074 if (modelId == m_mainModel) return true;
|
Chris@683
|
1075 for (auto rec: m_models) {
|
Chris@683
|
1076 if (rec.first == modelId) return true;
|
Chris@661
|
1077 }
|
Chris@661
|
1078 return false;
|
Chris@77
|
1079 }
|
Chris@77
|
1080
|
Chris@428
|
1081 bool
|
Chris@428
|
1082 Document::canAlign()
|
Chris@90
|
1083 {
|
Chris@428
|
1084 return Align::canAlign();
|
Chris@50
|
1085 }
|
Chris@50
|
1086
|
Chris@45
|
1087 void
|
Chris@683
|
1088 Document::alignModel(ModelId modelId, bool forceRecalculate)
|
Chris@45
|
1089 {
|
Chris@683
|
1090 SVDEBUG << "Document::alignModel(" << modelId << ", " << forceRecalculate
|
Chris@672
|
1091 << ") (main model is " << m_mainModel << ")" << endl;
|
Chris@683
|
1092
|
Chris@683
|
1093 auto rm = ModelById::getAs<RangeSummarisableTimeValueModel>(modelId);
|
Chris@387
|
1094 if (!rm) {
|
Chris@683
|
1095 SVDEBUG << "(model " << modelId << " is not an alignable sort)" << endl;
|
Chris@387
|
1096 return;
|
Chris@387
|
1097 }
|
Chris@48
|
1098
|
Chris@683
|
1099 if (m_mainModel.isNone()) {
|
Chris@672
|
1100 SVDEBUG << "(no main model to align to)" << endl;
|
Chris@683
|
1101 if (forceRecalculate && !rm->getAlignment().isNone()) {
|
Chris@672
|
1102 SVDEBUG << "(but model is aligned, and forceRecalculate is true, "
|
Chris@672
|
1103 << "so resetting alignment to nil)" << endl;
|
Chris@683
|
1104 rm->setAlignment({});
|
Chris@672
|
1105 }
|
Chris@672
|
1106 return;
|
Chris@672
|
1107 }
|
Chris@672
|
1108
|
Chris@86
|
1109 if (rm->getAlignmentReference() == m_mainModel) {
|
Chris@683
|
1110 SVDEBUG << "(model " << modelId << " is already aligned to main model "
|
Chris@672
|
1111 << m_mainModel << ")" << endl;
|
Chris@672
|
1112 if (!forceRecalculate) {
|
Chris@672
|
1113 return;
|
Chris@672
|
1114 } else {
|
Chris@672
|
1115 SVDEBUG << "(but forceRecalculate is true, so realigning anyway)"
|
Chris@672
|
1116 << endl;
|
Chris@672
|
1117 }
|
Chris@86
|
1118 }
|
Chris@45
|
1119
|
Chris@683
|
1120 if (modelId == m_mainModel) {
|
Chris@86
|
1121 // The reference has an empty alignment to itself. This makes
|
Chris@86
|
1122 // it possible to distinguish between the reference and any
|
Chris@86
|
1123 // unaligned model just by looking at the model itself,
|
Chris@86
|
1124 // without also knowing what the main model is
|
Chris@683
|
1125 SVDEBUG << "Document::alignModel(" << modelId
|
Chris@672
|
1126 << "): is main model, setting alignment to itself" << endl;
|
Chris@683
|
1127 auto alignment = std::make_shared<AlignmentModel>(modelId, modelId,
|
Chris@683
|
1128 ModelId());
|
Chris@687
|
1129
|
Chris@683
|
1130 //!!! hang on, who tracks alignment models?
|
Chris@687
|
1131 rm->setAlignment(ModelById::add(alignment));
|
Chris@86
|
1132 return;
|
Chris@86
|
1133 }
|
Chris@86
|
1134
|
Chris@683
|
1135 auto w = ModelById::getAs<WritableWaveFileModel>(modelId);
|
Chris@679
|
1136 if (w && w->getWriteProportion() < 100) {
|
Chris@683
|
1137 SVDEBUG << "Document::alignModel(" << modelId
|
Chris@679
|
1138 << "): model write is not complete, deferring"
|
Chris@679
|
1139 << endl;
|
Chris@687
|
1140 connect(w.get(), SIGNAL(writeCompleted(ModelId)),
|
Chris@687
|
1141 this, SLOT(performDeferredAlignment(ModelId)));
|
Chris@679
|
1142 return;
|
Chris@679
|
1143 }
|
Chris@679
|
1144
|
Chris@667
|
1145 SVDEBUG << "Document::alignModel: aligning..." << endl;
|
Chris@683
|
1146 if (!rm->getAlignmentReference().isNone()) {
|
Chris@667
|
1147 SVDEBUG << "(Note: model " << rm << " is currently aligned to model "
|
Chris@667
|
1148 << rm->getAlignmentReference() << "; this will replace that)"
|
Chris@667
|
1149 << endl;
|
Chris@667
|
1150 }
|
Chris@670
|
1151
|
Chris@670
|
1152 QString err;
|
Chris@683
|
1153 if (!m_align->alignModel(this, m_mainModel, modelId, err)) {
|
Chris@670
|
1154 SVCERR << "Alignment failed: " << err << endl;
|
Chris@670
|
1155 emit alignmentFailed(err);
|
Chris@64
|
1156 }
|
Chris@45
|
1157 }
|
Chris@45
|
1158
|
Chris@45
|
1159 void
|
Chris@687
|
1160 Document::performDeferredAlignment(ModelId modelId)
|
Chris@679
|
1161 {
|
Chris@683
|
1162 SVDEBUG << "Document::performDeferredAlignment: aligning..." << endl;
|
Chris@683
|
1163 alignModel(modelId);
|
Chris@679
|
1164 }
|
Chris@679
|
1165
|
Chris@679
|
1166 void
|
Chris@45
|
1167 Document::alignModels()
|
Chris@45
|
1168 {
|
Chris@683
|
1169 for (auto rec: m_models) {
|
Chris@683
|
1170 alignModel(rec.first);
|
Chris@45
|
1171 }
|
Chris@86
|
1172 alignModel(m_mainModel);
|
Chris@45
|
1173 }
|
Chris@45
|
1174
|
Chris@672
|
1175 void
|
Chris@672
|
1176 Document::realignModels()
|
Chris@672
|
1177 {
|
Chris@683
|
1178 for (auto rec: m_models) {
|
Chris@683
|
1179 alignModel(rec.first, true);
|
Chris@672
|
1180 }
|
Chris@672
|
1181 alignModel(m_mainModel);
|
Chris@672
|
1182 }
|
Chris@672
|
1183
|
Chris@45
|
1184 Document::AddLayerCommand::AddLayerCommand(Document *d,
|
Chris@595
|
1185 View *view,
|
Chris@595
|
1186 Layer *layer) :
|
Chris@45
|
1187 m_d(d),
|
Chris@45
|
1188 m_view(view),
|
Chris@45
|
1189 m_layer(layer),
|
Chris@45
|
1190 m_name(qApp->translate("AddLayerCommand", "Add %1 Layer").arg(layer->objectName())),
|
Chris@45
|
1191 m_added(false)
|
Chris@45
|
1192 {
|
Chris@45
|
1193 }
|
Chris@45
|
1194
|
Chris@45
|
1195 Document::AddLayerCommand::~AddLayerCommand()
|
Chris@45
|
1196 {
|
Chris@77
|
1197 #ifdef DEBUG_DOCUMENT
|
Chris@233
|
1198 SVDEBUG << "Document::AddLayerCommand::~AddLayerCommand" << endl;
|
Chris@77
|
1199 #endif
|
Chris@45
|
1200 if (!m_added) {
|
Chris@595
|
1201 m_d->deleteLayer(m_layer);
|
Chris@45
|
1202 }
|
Chris@45
|
1203 }
|
Chris@45
|
1204
|
Chris@159
|
1205 QString
|
Chris@159
|
1206 Document::AddLayerCommand::getName() const
|
Chris@159
|
1207 {
|
Chris@165
|
1208 #ifdef DEBUG_DOCUMENT
|
Chris@233
|
1209 SVDEBUG << "Document::AddLayerCommand::getName(): Name is "
|
Chris@229
|
1210 << m_name << endl;
|
Chris@165
|
1211 #endif
|
Chris@159
|
1212 return m_name;
|
Chris@159
|
1213 }
|
Chris@159
|
1214
|
Chris@45
|
1215 void
|
Chris@45
|
1216 Document::AddLayerCommand::execute()
|
Chris@45
|
1217 {
|
Chris@45
|
1218 for (int i = 0; i < m_view->getLayerCount(); ++i) {
|
Chris@595
|
1219 if (m_view->getLayer(i) == m_layer) {
|
Chris@595
|
1220 // already there
|
Chris@595
|
1221 m_layer->setLayerDormant(m_view, false);
|
Chris@595
|
1222 m_added = true;
|
Chris@595
|
1223 return;
|
Chris@595
|
1224 }
|
Chris@45
|
1225 }
|
Chris@45
|
1226
|
Chris@45
|
1227 m_view->addLayer(m_layer);
|
Chris@45
|
1228 m_layer->setLayerDormant(m_view, false);
|
Chris@45
|
1229
|
Chris@45
|
1230 m_d->addToLayerViewMap(m_layer, m_view);
|
Chris@45
|
1231 m_added = true;
|
Chris@45
|
1232 }
|
Chris@45
|
1233
|
Chris@45
|
1234 void
|
Chris@45
|
1235 Document::AddLayerCommand::unexecute()
|
Chris@45
|
1236 {
|
Chris@45
|
1237 m_view->removeLayer(m_layer);
|
Chris@45
|
1238 m_layer->setLayerDormant(m_view, true);
|
Chris@45
|
1239
|
Chris@45
|
1240 m_d->removeFromLayerViewMap(m_layer, m_view);
|
Chris@45
|
1241 m_added = false;
|
Chris@45
|
1242 }
|
Chris@45
|
1243
|
Chris@45
|
1244 Document::RemoveLayerCommand::RemoveLayerCommand(Document *d,
|
Chris@595
|
1245 View *view,
|
Chris@595
|
1246 Layer *layer) :
|
Chris@45
|
1247 m_d(d),
|
Chris@45
|
1248 m_view(view),
|
Chris@45
|
1249 m_layer(layer),
|
Chris@339
|
1250 m_wasDormant(layer->isLayerDormant(view)),
|
Chris@45
|
1251 m_name(qApp->translate("RemoveLayerCommand", "Delete %1 Layer").arg(layer->objectName())),
|
Chris@45
|
1252 m_added(true)
|
Chris@45
|
1253 {
|
Chris@45
|
1254 }
|
Chris@45
|
1255
|
Chris@45
|
1256 Document::RemoveLayerCommand::~RemoveLayerCommand()
|
Chris@45
|
1257 {
|
Chris@77
|
1258 #ifdef DEBUG_DOCUMENT
|
Chris@233
|
1259 SVDEBUG << "Document::RemoveLayerCommand::~RemoveLayerCommand" << endl;
|
Chris@77
|
1260 #endif
|
Chris@45
|
1261 if (!m_added) {
|
Chris@595
|
1262 m_d->deleteLayer(m_layer);
|
Chris@45
|
1263 }
|
Chris@45
|
1264 }
|
Chris@45
|
1265
|
Chris@159
|
1266 QString
|
Chris@159
|
1267 Document::RemoveLayerCommand::getName() const
|
Chris@159
|
1268 {
|
Chris@171
|
1269 #ifdef DEBUG_DOCUMENT
|
Chris@233
|
1270 SVDEBUG << "Document::RemoveLayerCommand::getName(): Name is "
|
Chris@229
|
1271 << m_name << endl;
|
Chris@171
|
1272 #endif
|
Chris@159
|
1273 return m_name;
|
Chris@159
|
1274 }
|
Chris@159
|
1275
|
Chris@45
|
1276 void
|
Chris@45
|
1277 Document::RemoveLayerCommand::execute()
|
Chris@45
|
1278 {
|
Chris@45
|
1279 bool have = false;
|
Chris@45
|
1280 for (int i = 0; i < m_view->getLayerCount(); ++i) {
|
Chris@595
|
1281 if (m_view->getLayer(i) == m_layer) {
|
Chris@595
|
1282 have = true;
|
Chris@595
|
1283 break;
|
Chris@595
|
1284 }
|
Chris@45
|
1285 }
|
Chris@45
|
1286
|
Chris@45
|
1287 if (!have) { // not there!
|
Chris@595
|
1288 m_layer->setLayerDormant(m_view, true);
|
Chris@595
|
1289 m_added = false;
|
Chris@595
|
1290 return;
|
Chris@45
|
1291 }
|
Chris@45
|
1292
|
Chris@45
|
1293 m_view->removeLayer(m_layer);
|
Chris@45
|
1294 m_layer->setLayerDormant(m_view, true);
|
Chris@45
|
1295
|
Chris@45
|
1296 m_d->removeFromLayerViewMap(m_layer, m_view);
|
Chris@45
|
1297 m_added = false;
|
Chris@45
|
1298 }
|
Chris@45
|
1299
|
Chris@45
|
1300 void
|
Chris@45
|
1301 Document::RemoveLayerCommand::unexecute()
|
Chris@45
|
1302 {
|
Chris@45
|
1303 m_view->addLayer(m_layer);
|
Chris@339
|
1304 m_layer->setLayerDormant(m_view, m_wasDormant);
|
Chris@45
|
1305
|
Chris@45
|
1306 m_d->addToLayerViewMap(m_layer, m_view);
|
Chris@45
|
1307 m_added = true;
|
Chris@45
|
1308 }
|
Chris@45
|
1309
|
Chris@45
|
1310 void
|
Chris@45
|
1311 Document::toXml(QTextStream &out, QString indent, QString extraAttributes) const
|
Chris@45
|
1312 {
|
Chris@226
|
1313 toXml(out, indent, extraAttributes, false);
|
Chris@226
|
1314 }
|
Chris@226
|
1315
|
Chris@226
|
1316 void
|
Chris@226
|
1317 Document::toXmlAsTemplate(QTextStream &out, QString indent, QString extraAttributes) const
|
Chris@226
|
1318 {
|
Chris@226
|
1319 toXml(out, indent, extraAttributes, true);
|
Chris@226
|
1320 }
|
Chris@226
|
1321
|
Chris@226
|
1322 void
|
Chris@226
|
1323 Document::toXml(QTextStream &out, QString indent, QString extraAttributes,
|
Chris@226
|
1324 bool asTemplate) const
|
Chris@226
|
1325 {
|
Chris@45
|
1326 out << indent + QString("<data%1%2>\n")
|
Chris@45
|
1327 .arg(extraAttributes == "" ? "" : " ").arg(extraAttributes);
|
Chris@45
|
1328
|
Chris@683
|
1329 auto mainModel = ModelById::getAs<WaveFileModel>(m_mainModel);
|
Chris@683
|
1330 if (mainModel) {
|
Chris@108
|
1331
|
Chris@660
|
1332 #ifdef DEBUG_DOCUMENT
|
Chris@660
|
1333 SVDEBUG << "Document::toXml: writing main model" << endl;
|
Chris@660
|
1334 #endif
|
Chris@660
|
1335
|
Chris@226
|
1336 if (asTemplate) {
|
Chris@226
|
1337 writePlaceholderMainModel(out, indent + " ");
|
Chris@226
|
1338 } else {
|
Chris@683
|
1339 mainModel->toXml(out, indent + " ", "mainModel=\"true\"");
|
Chris@226
|
1340 }
|
Chris@108
|
1341
|
Chris@686
|
1342 auto playParameters =
|
Chris@683
|
1343 PlayParameterRepository::getInstance()->getPlayParameters
|
Chris@683
|
1344 (m_mainModel.untyped);
|
Chris@108
|
1345 if (playParameters) {
|
Chris@108
|
1346 playParameters->toXml
|
Chris@108
|
1347 (out, indent + " ",
|
Chris@108
|
1348 QString("model=\"%1\"")
|
Chris@683
|
1349 .arg(mainModel->getExportId()));
|
Chris@108
|
1350 }
|
Chris@660
|
1351 } else {
|
Chris@660
|
1352 #ifdef DEBUG_DOCUMENT
|
Chris@660
|
1353 SVDEBUG << "Document::toXml: have no main model to write" << endl;
|
Chris@660
|
1354 #endif
|
Chris@45
|
1355 }
|
Chris@45
|
1356
|
Chris@45
|
1357 // Models that are not used in a layer that is in a view should
|
Chris@45
|
1358 // not be written. Get our list of required models first.
|
Chris@45
|
1359
|
Chris@683
|
1360 std::set<ModelId> used;
|
Chris@45
|
1361
|
Chris@45
|
1362 for (LayerViewMap::const_iterator i = m_layerViewMap.begin();
|
Chris@45
|
1363 i != m_layerViewMap.end(); ++i) {
|
Chris@45
|
1364
|
Chris@589
|
1365 if (i->first && !i->second.empty()) { // Layer exists, is in views
|
Chris@683
|
1366 ModelId modelId = i->first->getModel();
|
Chris@683
|
1367 if (auto model = ModelById::get(modelId)) {
|
Chris@683
|
1368 used.insert(modelId);
|
Chris@683
|
1369 if (!model->getSourceModel().isNone()) {
|
Chris@683
|
1370 used.insert(model->getSourceModel());
|
Chris@589
|
1371 }
|
Chris@589
|
1372 }
|
Chris@45
|
1373 }
|
Chris@45
|
1374 }
|
Chris@45
|
1375
|
Chris@589
|
1376 // Write aggregate models first, so that when re-reading
|
Chris@589
|
1377 // derivations we already know about their existence. But only
|
Chris@589
|
1378 // those that are actually used
|
Chris@629
|
1379 //
|
Chris@629
|
1380 // Later note: This turns out not to be a great idea - we can't
|
Chris@629
|
1381 // use an aggregate model to drive a derivation unless its
|
Chris@629
|
1382 // component models have all also already been loaded. So we
|
Chris@629
|
1383 // really should have written non-aggregate read-only
|
Chris@629
|
1384 // (i.e. non-derived) wave-type models first, then aggregate
|
Chris@629
|
1385 // models, then models that have derivations. But we didn't do
|
Chris@629
|
1386 // that, so existing sessions will always have the aggregate
|
Chris@629
|
1387 // models first and we might as well stick with that.
|
Chris@589
|
1388
|
Chris@683
|
1389 for (auto modelId: m_aggregateModels) {
|
Chris@589
|
1390
|
Chris@660
|
1391 #ifdef DEBUG_DOCUMENT
|
Chris@683
|
1392 SVDEBUG << "Document::toXml: checking aggregate model "
|
Chris@683
|
1393 << modelId << endl;
|
Chris@660
|
1394 #endif
|
Chris@683
|
1395
|
Chris@683
|
1396 auto aggregate = ModelById::getAs<AggregateWaveModel>(modelId);
|
Chris@589
|
1397 if (!aggregate) continue;
|
Chris@683
|
1398 if (used.find(modelId) == used.end()) {
|
Chris@660
|
1399 #ifdef DEBUG_DOCUMENT
|
Chris@589
|
1400 SVDEBUG << "(unused, skipping)" << endl;
|
Chris@660
|
1401 #endif
|
Chris@589
|
1402 continue;
|
Chris@589
|
1403 }
|
Chris@589
|
1404
|
Chris@660
|
1405 #ifdef DEBUG_DOCUMENT
|
Chris@589
|
1406 SVDEBUG << "(used, writing)" << endl;
|
Chris@660
|
1407 #endif
|
Chris@589
|
1408
|
Chris@589
|
1409 aggregate->toXml(out, indent + " ");
|
Chris@589
|
1410 }
|
Chris@589
|
1411
|
Chris@683
|
1412 std::set<ModelId> written;
|
Chris@111
|
1413
|
Chris@629
|
1414 // Now write the other models in two passes: first the models that
|
Chris@629
|
1415 // aren't derived from anything (in case they are source
|
Chris@629
|
1416 // components for an aggregate model, in which case we need to
|
Chris@629
|
1417 // have seen them before we see any models derived from aggregates
|
Chris@629
|
1418 // that use them - see the lament above) and then the models that
|
Chris@629
|
1419 // have derivations.
|
Chris@45
|
1420
|
Chris@629
|
1421 const int nonDerivedPass = 0, derivedPass = 1;
|
Chris@629
|
1422 for (int pass = nonDerivedPass; pass <= derivedPass; ++pass) {
|
Chris@629
|
1423
|
Chris@683
|
1424 for (auto rec: m_models) {
|
Chris@45
|
1425
|
Chris@683
|
1426 ModelId modelId = rec.first;
|
Chris@629
|
1427
|
Chris@683
|
1428 if (used.find(modelId) == used.end()) continue;
|
Chris@45
|
1429
|
Chris@683
|
1430 auto model = ModelById::get(modelId);
|
Chris@683
|
1431 if (!model) continue;
|
Chris@683
|
1432
|
Chris@660
|
1433 #ifdef DEBUG_DOCUMENT
|
Chris@683
|
1434 SVDEBUG << "Document::toXml: looking at model " << modelId
|
Chris@683
|
1435 << " [pass = " << pass << "]" << endl;
|
Chris@660
|
1436 #endif
|
Chris@660
|
1437
|
Chris@629
|
1438 // We need an intelligent way to determine which models
|
Chris@629
|
1439 // need to be streamed (i.e. have been edited, or are
|
Chris@629
|
1440 // small) and which should not be (i.e. remain as
|
Chris@629
|
1441 // generated by a transform, and are large).
|
Chris@629
|
1442 //
|
Chris@629
|
1443 // At the moment we can get away with deciding not to
|
Chris@629
|
1444 // stream dense 3d models or writable wave file models,
|
Chris@629
|
1445 // provided they were generated from a transform, because
|
Chris@629
|
1446 // at the moment there is no way to edit those model types
|
Chris@629
|
1447 // so it should be safe to regenerate them. That won't
|
Chris@629
|
1448 // always work in future though. It would be particularly
|
Chris@629
|
1449 // nice to be able to ask the user, as well as making an
|
Chris@629
|
1450 // intelligent guess.
|
Chris@45
|
1451
|
Chris@629
|
1452 bool writeModel = true;
|
Chris@629
|
1453 bool haveDerivation = false;
|
Chris@629
|
1454
|
Chris@683
|
1455 if (!rec.second.source.isNone() &&
|
Chris@683
|
1456 rec.second.transform.getIdentifier() != "") {
|
Chris@629
|
1457 haveDerivation = true;
|
Chris@629
|
1458 }
|
Chris@45
|
1459
|
Chris@629
|
1460 if (pass == nonDerivedPass) {
|
Chris@629
|
1461 if (haveDerivation) {
|
Chris@629
|
1462 SVDEBUG << "skipping derived model " << model->objectName() << " during nonDerivedPass" << endl;
|
Chris@629
|
1463 continue;
|
Chris@629
|
1464 }
|
Chris@629
|
1465 } else {
|
Chris@629
|
1466 if (!haveDerivation) {
|
Chris@629
|
1467 SVDEBUG << "skipping non-derived model " << model->objectName() << " during derivedPass" << endl;
|
Chris@629
|
1468 continue;
|
Chris@629
|
1469 }
|
Chris@629
|
1470 }
|
Chris@45
|
1471
|
Chris@629
|
1472 if (haveDerivation) {
|
Chris@683
|
1473 if (ModelById::isa<WritableWaveFileModel>(modelId) ||
|
Chris@683
|
1474 ModelById::isa<DenseThreeDimensionalModel>(modelId)) {
|
Chris@629
|
1475 writeModel = false;
|
Chris@629
|
1476 }
|
Chris@629
|
1477 }
|
Chris@629
|
1478
|
Chris@629
|
1479 if (writeModel) {
|
Chris@629
|
1480 model->toXml(out, indent + " ");
|
Chris@683
|
1481 written.insert(modelId);
|
Chris@629
|
1482 }
|
Chris@629
|
1483
|
Chris@629
|
1484 if (haveDerivation) {
|
Chris@629
|
1485 writeBackwardCompatibleDerivation(out, indent + " ",
|
Chris@683
|
1486 modelId, rec.second);
|
Chris@629
|
1487 }
|
Chris@629
|
1488
|
Chris@686
|
1489 auto playParameters =
|
Chris@683
|
1490 PlayParameterRepository::getInstance()->getPlayParameters
|
Chris@683
|
1491 (modelId.untyped);
|
Chris@629
|
1492 if (playParameters) {
|
Chris@629
|
1493 playParameters->toXml
|
Chris@629
|
1494 (out, indent + " ",
|
Chris@629
|
1495 QString("model=\"%1\"")
|
Chris@657
|
1496 .arg(model->getExportId()));
|
Chris@45
|
1497 }
|
Chris@45
|
1498 }
|
Chris@45
|
1499 }
|
Chris@111
|
1500
|
Chris@111
|
1501 // We should write out the alignment models here. AlignmentModel
|
Chris@111
|
1502 // needs a toXml that writes out the export IDs of its reference
|
Chris@111
|
1503 // and aligned models, and then streams its path model. Note that
|
Chris@111
|
1504 // this will only work when the alignment is complete, so we
|
Chris@111
|
1505 // should probably wait for it if it isn't already by this point.
|
Chris@111
|
1506
|
Chris@683
|
1507 for (auto modelId: written) {
|
Chris@111
|
1508
|
Chris@683
|
1509 auto model = ModelById::get(modelId);
|
Chris@683
|
1510 if (!model) continue;
|
Chris@683
|
1511
|
Chris@683
|
1512 auto alignment = ModelById::get(model->getAlignment());
|
Chris@111
|
1513 if (!alignment) continue;
|
Chris@111
|
1514
|
Chris@111
|
1515 alignment->toXml(out, indent + " ");
|
Chris@111
|
1516 }
|
Chris@111
|
1517
|
Chris@663
|
1518 for (auto i = m_layers.begin(); i != m_layers.end(); ++i) {
|
Chris@595
|
1519 (*i)->toXml(out, indent + " ");
|
Chris@45
|
1520 }
|
Chris@45
|
1521
|
Chris@45
|
1522 out << indent + "</data>\n";
|
Chris@45
|
1523 }
|
Chris@45
|
1524
|
Chris@72
|
1525 void
|
Chris@226
|
1526 Document::writePlaceholderMainModel(QTextStream &out, QString indent) const
|
Chris@226
|
1527 {
|
Chris@683
|
1528 auto mainModel = ModelById::get(m_mainModel);
|
Chris@683
|
1529 if (!mainModel) return;
|
Chris@226
|
1530 out << indent;
|
Chris@226
|
1531 out << QString("<model id=\"%1\" name=\"placeholder\" sampleRate=\"%2\" type=\"wavefile\" file=\":samples/silent.wav\" mainModel=\"true\"/>\n")
|
Chris@683
|
1532 .arg(mainModel->getExportId())
|
Chris@683
|
1533 .arg(mainModel->getSampleRate());
|
Chris@226
|
1534 }
|
Chris@226
|
1535
|
Chris@226
|
1536 void
|
Chris@72
|
1537 Document::writeBackwardCompatibleDerivation(QTextStream &out, QString indent,
|
Chris@683
|
1538 ModelId targetModelId,
|
Chris@72
|
1539 const ModelRecord &rec) const
|
Chris@72
|
1540 {
|
Chris@72
|
1541 // There is a lot of redundancy in the XML we output here, because
|
Chris@72
|
1542 // we want it to work with older SV session file reading code as
|
Chris@72
|
1543 // well.
|
Chris@72
|
1544 //
|
Chris@72
|
1545 // Formerly, a transform was described using a derivation element
|
Chris@72
|
1546 // which set out the source and target models, execution context
|
Chris@72
|
1547 // (step size, input channel etc) and transform id, containing a
|
Chris@72
|
1548 // plugin element which set out the transform parameters and so
|
Chris@72
|
1549 // on. (The plugin element came from a "configurationXml" string
|
Chris@72
|
1550 // obtained from PluginXml.)
|
Chris@72
|
1551 //
|
Chris@72
|
1552 // This has been replaced by a derivation element setting out the
|
Chris@72
|
1553 // source and target models and input channel, containing a
|
Chris@72
|
1554 // transform element which sets out everything in the Transform.
|
Chris@72
|
1555 //
|
Chris@72
|
1556 // In order to retain compatibility with older SV code, however,
|
Chris@72
|
1557 // we have to write out the same stuff into the derivation as
|
Chris@72
|
1558 // before, and manufacture an appropriate plugin element as well
|
Chris@72
|
1559 // as the transform element. In order that newer code knows it's
|
Chris@72
|
1560 // dealing with a newer format, we will also write an attribute
|
Chris@72
|
1561 // 'type="transform"' in the derivation element.
|
Chris@45
|
1562
|
Chris@72
|
1563 const Transform &transform = rec.transform;
|
Chris@72
|
1564
|
Chris@683
|
1565 //!!! in cases like these, where we think we have the model handle
|
Chris@683
|
1566 //!!! and nobody else should be releasing it, we probably ought to
|
Chris@683
|
1567 //!!! throw std::logic_error if !targetModel
|
Chris@683
|
1568
|
Chris@683
|
1569 auto targetModel = ModelById::get(targetModelId);
|
Chris@683
|
1570 if (!targetModel) return;
|
Chris@683
|
1571
|
Chris@72
|
1572 // Just for reference, this is what we would write if we didn't
|
Chris@72
|
1573 // have to be backward compatible:
|
Chris@72
|
1574 //
|
Chris@72
|
1575 // out << indent
|
Chris@72
|
1576 // << QString("<derivation type=\"transform\" source=\"%1\" "
|
Chris@72
|
1577 // "model=\"%2\" channel=\"%3\">\n")
|
Chris@657
|
1578 // .arg(rec.source->getExportId())
|
Chris@657
|
1579 // .arg(targetModel->getExportId())
|
Chris@72
|
1580 // .arg(rec.channel);
|
Chris@72
|
1581 //
|
Chris@72
|
1582 // transform.toXml(out, indent + " ");
|
Chris@72
|
1583 //
|
Chris@72
|
1584 // out << indent << "</derivation>\n";
|
Chris@72
|
1585 //
|
Chris@72
|
1586 // Unfortunately, we can't just do that. So we do this...
|
Chris@72
|
1587
|
Chris@72
|
1588 QString extentsAttributes;
|
Chris@72
|
1589 if (transform.getStartTime() != RealTime::zeroTime ||
|
Chris@72
|
1590 transform.getDuration() != RealTime::zeroTime) {
|
Chris@72
|
1591 extentsAttributes = QString("startFrame=\"%1\" duration=\"%2\" ")
|
Chris@72
|
1592 .arg(RealTime::realTime2Frame(transform.getStartTime(),
|
Chris@72
|
1593 targetModel->getSampleRate()))
|
Chris@72
|
1594 .arg(RealTime::realTime2Frame(transform.getDuration(),
|
Chris@72
|
1595 targetModel->getSampleRate()));
|
Chris@72
|
1596 }
|
Chris@595
|
1597
|
Chris@72
|
1598 out << indent;
|
Chris@72
|
1599 out << QString("<derivation type=\"transform\" source=\"%1\" "
|
Chris@72
|
1600 "model=\"%2\" channel=\"%3\" domain=\"%4\" "
|
Chris@72
|
1601 "stepSize=\"%5\" blockSize=\"%6\" %7windowType=\"%8\" "
|
Chris@72
|
1602 "transform=\"%9\">\n")
|
Chris@683
|
1603 .arg(ModelById::getExportId(rec.source))
|
Chris@657
|
1604 .arg(targetModel->getExportId())
|
Chris@72
|
1605 .arg(rec.channel)
|
Chris@72
|
1606 .arg(TransformFactory::getInstance()->getTransformInputDomain
|
Chris@72
|
1607 (transform.getIdentifier()))
|
Chris@72
|
1608 .arg(transform.getStepSize())
|
Chris@72
|
1609 .arg(transform.getBlockSize())
|
Chris@72
|
1610 .arg(extentsAttributes)
|
Chris@72
|
1611 .arg(int(transform.getWindowType()))
|
Chris@72
|
1612 .arg(XmlExportable::encodeEntities(transform.getIdentifier()));
|
Chris@72
|
1613
|
Chris@72
|
1614 transform.toXml(out, indent + " ");
|
Chris@72
|
1615
|
Chris@72
|
1616 out << indent << " "
|
Chris@72
|
1617 << TransformFactory::getInstance()->getPluginConfigurationXml(transform);
|
Chris@72
|
1618
|
Chris@72
|
1619 out << indent << "</derivation>\n";
|
Chris@72
|
1620 }
|
Chris@72
|
1621
|