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