Chris@45
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@45
|
2
|
Chris@45
|
3 /*
|
Chris@45
|
4 Sonic Visualiser
|
Chris@45
|
5 An audio file viewer and annotation editor.
|
Chris@45
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@45
|
7 This file copyright 2006 Chris Cannam and QMUL.
|
Chris@45
|
8
|
Chris@45
|
9 This program is free software; you can redistribute it and/or
|
Chris@45
|
10 modify it under the terms of the GNU General Public License as
|
Chris@45
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@45
|
12 License, or (at your option) any later version. See the file
|
Chris@45
|
13 COPYING included with this distribution for more information.
|
Chris@45
|
14 */
|
Chris@45
|
15
|
Chris@45
|
16 #include "Document.h"
|
Chris@45
|
17
|
Chris@45
|
18 #include "data/model/WaveFileModel.h"
|
Chris@45
|
19 #include "data/model/WritableWaveFileModel.h"
|
Chris@45
|
20 #include "data/model/DenseThreeDimensionalModel.h"
|
Chris@45
|
21 #include "data/model/DenseTimeValueModel.h"
|
Chris@45
|
22 #include "layer/Layer.h"
|
Chris@105
|
23 #include "widgets/CommandHistory.h"
|
Chris@45
|
24 #include "base/Command.h"
|
Chris@45
|
25 #include "view/View.h"
|
Chris@45
|
26 #include "base/PlayParameterRepository.h"
|
Chris@45
|
27 #include "base/PlayParameters.h"
|
Chris@106
|
28 #include "transform/TransformFactory.h"
|
Chris@106
|
29 #include "transform/ModelTransformerFactory.h"
|
Chris@45
|
30 #include <QApplication>
|
Chris@45
|
31 #include <QTextStream>
|
Chris@90
|
32 #include <QSettings>
|
Chris@45
|
33 #include <iostream>
|
Chris@45
|
34
|
Chris@45
|
35 // For alignment:
|
Chris@45
|
36 #include "data/model/AggregateWaveModel.h"
|
Chris@45
|
37 #include "data/model/SparseTimeValueModel.h"
|
Chris@45
|
38 #include "data/model/AlignmentModel.h"
|
Chris@45
|
39
|
Chris@81
|
40 //#define DEBUG_DOCUMENT 1
|
Chris@77
|
41
|
Chris@45
|
42 //!!! still need to handle command history, documentRestored/documentModified
|
Chris@45
|
43
|
Chris@45
|
44 Document::Document() :
|
Chris@47
|
45 m_mainModel(0),
|
Chris@47
|
46 m_autoAlignment(false)
|
Chris@45
|
47 {
|
Chris@45
|
48 connect(this, SIGNAL(modelAboutToBeDeleted(Model *)),
|
Chris@54
|
49 ModelTransformerFactory::getInstance(),
|
Chris@45
|
50 SLOT(modelAboutToBeDeleted(Model *)));
|
Chris@45
|
51 }
|
Chris@45
|
52
|
Chris@45
|
53 Document::~Document()
|
Chris@45
|
54 {
|
Chris@45
|
55 //!!! Document should really own the command history. atm we
|
Chris@45
|
56 //still refer to it in various places that don't have access to
|
Chris@45
|
57 //the document, be nice to fix that
|
Chris@45
|
58
|
Chris@77
|
59 #ifdef DEBUG_DOCUMENT
|
Chris@77
|
60 std::cerr << "\n\nDocument::~Document: about to clear command history" << std::endl;
|
Chris@77
|
61 #endif
|
Chris@45
|
62 CommandHistory::getInstance()->clear();
|
Chris@45
|
63
|
Chris@77
|
64 #ifdef DEBUG_DOCUMENT
|
Chris@52
|
65 std::cerr << "Document::~Document: about to delete layers" << std::endl;
|
Chris@77
|
66 #endif
|
Chris@45
|
67 while (!m_layers.empty()) {
|
Chris@45
|
68 deleteLayer(*m_layers.begin(), true);
|
Chris@45
|
69 }
|
Chris@45
|
70
|
Chris@45
|
71 if (!m_models.empty()) {
|
Chris@45
|
72 std::cerr << "Document::~Document: WARNING: "
|
Chris@45
|
73 << m_models.size() << " model(s) still remain -- "
|
Chris@45
|
74 << "should have been garbage collected when deleting layers"
|
Chris@45
|
75 << std::endl;
|
Chris@45
|
76 while (!m_models.empty()) {
|
Chris@45
|
77 Model *model = m_models.begin()->first;
|
Chris@45
|
78 if (model == m_mainModel) {
|
Chris@45
|
79 // just in case!
|
Chris@45
|
80 std::cerr << "Document::~Document: WARNING: Main model is also"
|
Chris@45
|
81 << " in models list!" << std::endl;
|
Chris@45
|
82 } else if (model) {
|
Chris@79
|
83 model->aboutToDelete();
|
Chris@45
|
84 emit modelAboutToBeDeleted(model);
|
Chris@45
|
85 delete model;
|
Chris@45
|
86 }
|
Chris@45
|
87 m_models.erase(m_models.begin());
|
Chris@45
|
88 }
|
Chris@45
|
89 }
|
Chris@45
|
90
|
Chris@77
|
91 #ifdef DEBUG_DOCUMENT
|
Chris@77
|
92 std::cerr << "Document::~Document: About to get rid of main model"
|
Chris@77
|
93 << std::endl;
|
Chris@77
|
94 #endif
|
Chris@45
|
95 if (m_mainModel) {
|
Chris@79
|
96 m_mainModel->aboutToDelete();
|
Chris@45
|
97 emit modelAboutToBeDeleted(m_mainModel);
|
Chris@45
|
98 }
|
Chris@45
|
99
|
Chris@45
|
100 emit mainModelChanged(0);
|
Chris@45
|
101 delete m_mainModel;
|
Chris@45
|
102
|
Chris@45
|
103 }
|
Chris@45
|
104
|
Chris@45
|
105 Layer *
|
Chris@45
|
106 Document::createLayer(LayerFactory::LayerType type)
|
Chris@45
|
107 {
|
Chris@45
|
108 Layer *newLayer = LayerFactory::getInstance()->createLayer(type);
|
Chris@45
|
109 if (!newLayer) return 0;
|
Chris@45
|
110
|
Chris@45
|
111 newLayer->setObjectName(getUniqueLayerName(newLayer->objectName()));
|
Chris@45
|
112
|
Chris@45
|
113 m_layers.insert(newLayer);
|
Chris@52
|
114
|
Chris@77
|
115 #ifdef DEBUG_DOCUMENT
|
Chris@77
|
116 std::cerr << "Document::createLayer: Added layer of type " << type
|
Chris@77
|
117 << ", now have " << m_layers.size() << " layers" << std::endl;
|
Chris@77
|
118 #endif
|
Chris@52
|
119
|
Chris@45
|
120 emit layerAdded(newLayer);
|
Chris@45
|
121
|
Chris@45
|
122 return newLayer;
|
Chris@45
|
123 }
|
Chris@45
|
124
|
Chris@45
|
125 Layer *
|
Chris@45
|
126 Document::createMainModelLayer(LayerFactory::LayerType type)
|
Chris@45
|
127 {
|
Chris@45
|
128 Layer *newLayer = createLayer(type);
|
Chris@45
|
129 if (!newLayer) return 0;
|
Chris@45
|
130 setModel(newLayer, m_mainModel);
|
Chris@45
|
131 return newLayer;
|
Chris@45
|
132 }
|
Chris@45
|
133
|
Chris@45
|
134 Layer *
|
Chris@45
|
135 Document::createImportedLayer(Model *model)
|
Chris@45
|
136 {
|
Chris@45
|
137 LayerFactory::LayerTypeSet types =
|
Chris@45
|
138 LayerFactory::getInstance()->getValidLayerTypes(model);
|
Chris@45
|
139
|
Chris@45
|
140 if (types.empty()) {
|
Chris@45
|
141 std::cerr << "WARNING: Document::importLayer: no valid display layer for model" << std::endl;
|
Chris@45
|
142 return 0;
|
Chris@45
|
143 }
|
Chris@45
|
144
|
Chris@45
|
145 //!!! for now, just use the first suitable layer type
|
Chris@45
|
146 LayerFactory::LayerType type = *types.begin();
|
Chris@45
|
147
|
Chris@45
|
148 Layer *newLayer = LayerFactory::getInstance()->createLayer(type);
|
Chris@45
|
149 if (!newLayer) return 0;
|
Chris@45
|
150
|
Chris@45
|
151 newLayer->setObjectName(getUniqueLayerName(newLayer->objectName()));
|
Chris@45
|
152
|
Chris@45
|
153 addImportedModel(model);
|
Chris@45
|
154 setModel(newLayer, model);
|
Chris@45
|
155
|
Chris@45
|
156 //!!! and all channels
|
Chris@45
|
157 setChannel(newLayer, -1);
|
Chris@45
|
158
|
Chris@45
|
159 m_layers.insert(newLayer);
|
Chris@52
|
160
|
Chris@77
|
161 #ifdef DEBUG_DOCUMENT
|
Chris@52
|
162 std::cerr << "Document::createImportedLayer: Added layer of type " << type
|
Chris@52
|
163 << ", now have " << m_layers.size() << " layers" << std::endl;
|
Chris@77
|
164 #endif
|
Chris@52
|
165
|
Chris@45
|
166 emit layerAdded(newLayer);
|
Chris@45
|
167 return newLayer;
|
Chris@45
|
168 }
|
Chris@45
|
169
|
Chris@45
|
170 Layer *
|
Chris@45
|
171 Document::createEmptyLayer(LayerFactory::LayerType type)
|
Chris@45
|
172 {
|
Chris@61
|
173 if (!m_mainModel) return 0;
|
Chris@61
|
174
|
Chris@45
|
175 Model *newModel =
|
Chris@45
|
176 LayerFactory::getInstance()->createEmptyModel(type, m_mainModel);
|
Chris@45
|
177 if (!newModel) return 0;
|
Chris@45
|
178
|
Chris@45
|
179 Layer *newLayer = createLayer(type);
|
Chris@45
|
180 if (!newLayer) {
|
Chris@45
|
181 delete newModel;
|
Chris@45
|
182 return 0;
|
Chris@45
|
183 }
|
Chris@45
|
184
|
Chris@45
|
185 addImportedModel(newModel);
|
Chris@45
|
186 setModel(newLayer, newModel);
|
Chris@45
|
187
|
Chris@45
|
188 return newLayer;
|
Chris@45
|
189 }
|
Chris@45
|
190
|
Chris@45
|
191 Layer *
|
Chris@45
|
192 Document::createDerivedLayer(LayerFactory::LayerType type,
|
Chris@54
|
193 TransformId transform)
|
Chris@45
|
194 {
|
Chris@45
|
195 Layer *newLayer = createLayer(type);
|
Chris@45
|
196 if (!newLayer) return 0;
|
Chris@45
|
197
|
Chris@45
|
198 newLayer->setObjectName(getUniqueLayerName
|
Chris@54
|
199 (TransformFactory::getInstance()->
|
Chris@54
|
200 getTransformFriendlyName(transform)));
|
Chris@45
|
201
|
Chris@45
|
202 return newLayer;
|
Chris@45
|
203 }
|
Chris@45
|
204
|
Chris@45
|
205 Layer *
|
Chris@72
|
206 Document::createDerivedLayer(const Transform &transform,
|
Chris@72
|
207 const ModelTransformer::Input &input)
|
Chris@45
|
208 {
|
Chris@78
|
209 QString message;
|
Chris@78
|
210 Model *newModel = addDerivedModel(transform, input, message);
|
Chris@45
|
211 if (!newModel) {
|
Chris@78
|
212 emit modelGenerationFailed(transform.getIdentifier(), message);
|
Chris@45
|
213 return 0;
|
Chris@78
|
214 } else if (message != "") {
|
Chris@78
|
215 emit modelGenerationWarning(transform.getIdentifier(), message);
|
Chris@45
|
216 }
|
Chris@45
|
217
|
Chris@45
|
218 LayerFactory::LayerTypeSet types =
|
Chris@45
|
219 LayerFactory::getInstance()->getValidLayerTypes(newModel);
|
Chris@45
|
220
|
Chris@45
|
221 if (types.empty()) {
|
Chris@72
|
222 std::cerr << "WARNING: Document::createLayerForTransformer: no valid display layer for output of transform " << transform.getIdentifier().toStdString() << std::endl;
|
Chris@136
|
223 newModel->aboutToDelete();
|
Chris@136
|
224 emit modelAboutToBeDeleted(newModel);
|
Chris@136
|
225 m_models.erase(newModel);
|
Chris@45
|
226 delete newModel;
|
Chris@45
|
227 return 0;
|
Chris@45
|
228 }
|
Chris@45
|
229
|
Chris@45
|
230 //!!! for now, just use the first suitable layer type
|
Chris@45
|
231
|
Chris@45
|
232 Layer *newLayer = createLayer(*types.begin());
|
Chris@45
|
233 setModel(newLayer, newModel);
|
Chris@45
|
234
|
Chris@45
|
235 //!!! We need to clone the model when adding the layer, so that it
|
Chris@45
|
236 //can be edited without affecting other layers that are based on
|
Chris@45
|
237 //the same model. Unfortunately we can't just clone it now,
|
Chris@45
|
238 //because it probably hasn't been completed yet -- the transform
|
Chris@45
|
239 //runs in the background. Maybe the transform has to handle
|
Chris@45
|
240 //cloning and cacheing models itself.
|
Chris@45
|
241 //
|
Chris@45
|
242 // Once we do clone models here, of course, we'll have to avoid
|
Chris@45
|
243 // leaking them too.
|
Chris@45
|
244 //
|
Chris@45
|
245 // We want the user to be able to add a model to a second layer
|
Chris@45
|
246 // _while it's still being calculated in the first_ and have it
|
Chris@45
|
247 // work quickly. That means we need to put the same physical
|
Chris@45
|
248 // model pointer in both layers, so they can't actually be cloned.
|
Chris@45
|
249
|
Chris@45
|
250 if (newLayer) {
|
Chris@45
|
251 newLayer->setObjectName(getUniqueLayerName
|
Chris@54
|
252 (TransformFactory::getInstance()->
|
Chris@72
|
253 getTransformFriendlyName
|
Chris@72
|
254 (transform.getIdentifier())));
|
Chris@45
|
255 }
|
Chris@45
|
256
|
Chris@45
|
257 emit layerAdded(newLayer);
|
Chris@45
|
258 return newLayer;
|
Chris@45
|
259 }
|
Chris@45
|
260
|
Chris@45
|
261 void
|
Chris@45
|
262 Document::setMainModel(WaveFileModel *model)
|
Chris@45
|
263 {
|
Chris@45
|
264 Model *oldMainModel = m_mainModel;
|
Chris@45
|
265 m_mainModel = model;
|
Chris@45
|
266
|
Chris@45
|
267 emit modelAdded(m_mainModel);
|
Chris@45
|
268
|
Chris@45
|
269 std::vector<Layer *> obsoleteLayers;
|
Chris@53
|
270 std::set<QString> failedTransformers;
|
Chris@45
|
271
|
Chris@45
|
272 // We need to ensure that no layer is left using oldMainModel or
|
Chris@45
|
273 // any of the old derived models as its model. Either replace the
|
Chris@45
|
274 // model, or delete the layer for each layer that is currently
|
Chris@45
|
275 // using one of these. Carry out this replacement before we
|
Chris@45
|
276 // delete any of the models.
|
Chris@45
|
277
|
Chris@77
|
278 #ifdef DEBUG_DOCUMENT
|
Chris@77
|
279 std::cerr << "Document::setMainModel: Have "
|
Chris@77
|
280 << m_layers.size() << " layers" << std::endl;
|
Chris@77
|
281 #endif
|
Chris@52
|
282
|
Chris@45
|
283 for (LayerSet::iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
|
Chris@45
|
284
|
Chris@45
|
285 Layer *layer = *i;
|
Chris@45
|
286 Model *model = layer->getModel();
|
Chris@45
|
287
|
Chris@77
|
288 #ifdef DEBUG_DOCUMENT
|
Chris@77
|
289 std::cerr << "Document::setMainModel: inspecting model "
|
Chris@77
|
290 << (model ? model->objectName().toStdString() : "(null)") << " in layer "
|
Chris@77
|
291 << layer->objectName().toStdString() << std::endl;
|
Chris@77
|
292 #endif
|
Chris@45
|
293
|
Chris@70
|
294 if (model == oldMainModel) {
|
Chris@77
|
295 #ifdef DEBUG_DOCUMENT
|
Chris@77
|
296 std::cerr << "... it uses the old main model, replacing" << std::endl;
|
Chris@77
|
297 #endif
|
Chris@45
|
298 LayerFactory::getInstance()->setModel(layer, m_mainModel);
|
Chris@45
|
299 continue;
|
Chris@45
|
300 }
|
Chris@45
|
301
|
Chris@70
|
302 if (model && (m_models.find(model) == m_models.end())) {
|
Chris@45
|
303 std::cerr << "WARNING: Document::setMainModel: Unknown model "
|
Chris@45
|
304 << model << " in layer " << layer << std::endl;
|
Chris@45
|
305 // get rid of this hideous degenerate
|
Chris@45
|
306 obsoleteLayers.push_back(layer);
|
Chris@45
|
307 continue;
|
Chris@45
|
308 }
|
Chris@45
|
309
|
Chris@70
|
310 if (m_models[model].source &&
|
Chris@70
|
311 (m_models[model].source == oldMainModel)) {
|
Chris@45
|
312
|
Chris@77
|
313 #ifdef DEBUG_DOCUMENT
|
Chris@77
|
314 std::cerr << "... it uses a model derived from the old main model, regenerating" << std::endl;
|
Chris@77
|
315 #endif
|
Chris@45
|
316
|
Chris@45
|
317 // This model was derived from the previous main
|
Chris@45
|
318 // model: regenerate it.
|
Chris@45
|
319
|
Chris@72
|
320 const Transform &transform = m_models[model].transform;
|
Chris@72
|
321 QString transformId = transform.getIdentifier();
|
Chris@45
|
322
|
Chris@72
|
323 //!!! We have a problem here if the number of channels in
|
Chris@72
|
324 //the main model has changed.
|
Chris@72
|
325
|
Chris@78
|
326 QString message;
|
Chris@45
|
327 Model *replacementModel =
|
Chris@45
|
328 addDerivedModel(transform,
|
Chris@72
|
329 ModelTransformer::Input
|
Chris@78
|
330 (m_mainModel, m_models[model].channel),
|
Chris@78
|
331 message);
|
Chris@45
|
332
|
Chris@45
|
333 if (!replacementModel) {
|
Chris@45
|
334 std::cerr << "WARNING: Document::setMainModel: Failed to regenerate model for transform \""
|
Chris@72
|
335 << transformId.toStdString() << "\"" << " in layer " << layer << std::endl;
|
Chris@72
|
336 if (failedTransformers.find(transformId)
|
Chris@72
|
337 == failedTransformers.end()) {
|
Chris@45
|
338 emit modelRegenerationFailed(layer->objectName(),
|
Chris@78
|
339 transformId,
|
Chris@78
|
340 message);
|
Chris@72
|
341 failedTransformers.insert(transformId);
|
Chris@45
|
342 }
|
Chris@45
|
343 obsoleteLayers.push_back(layer);
|
Chris@45
|
344 } else {
|
Chris@78
|
345 if (message != "") {
|
Chris@78
|
346 emit modelRegenerationWarning(layer->objectName(),
|
Chris@78
|
347 transformId,
|
Chris@78
|
348 message);
|
Chris@78
|
349 }
|
Chris@77
|
350 #ifdef DEBUG_DOCUMENT
|
Chris@77
|
351 std::cerr << "Replacing model " << model << " (type "
|
Chris@77
|
352 << typeid(*model).name() << ") with model "
|
Chris@77
|
353 << replacementModel << " (type "
|
Chris@77
|
354 << typeid(*replacementModel).name() << ") in layer "
|
Chris@77
|
355 << layer << " (name " << layer->objectName().toStdString() << ")"
|
Chris@77
|
356 << std::endl;
|
Chris@77
|
357 #endif
|
Chris@45
|
358 RangeSummarisableTimeValueModel *rm =
|
Chris@45
|
359 dynamic_cast<RangeSummarisableTimeValueModel *>(replacementModel);
|
Chris@77
|
360 #ifdef DEBUG_DOCUMENT
|
Chris@45
|
361 if (rm) {
|
Chris@45
|
362 std::cerr << "new model has " << rm->getChannelCount() << " channels " << std::endl;
|
Chris@45
|
363 } else {
|
Chris@45
|
364 std::cerr << "new model is not a RangeSummarisableTimeValueModel!" << std::endl;
|
Chris@45
|
365 }
|
Chris@77
|
366 #endif
|
Chris@45
|
367 setModel(layer, replacementModel);
|
Chris@45
|
368 }
|
Chris@45
|
369 }
|
Chris@45
|
370 }
|
Chris@45
|
371
|
Chris@45
|
372 for (size_t k = 0; k < obsoleteLayers.size(); ++k) {
|
Chris@45
|
373 deleteLayer(obsoleteLayers[k], true);
|
Chris@45
|
374 }
|
Chris@45
|
375
|
Chris@48
|
376 for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) {
|
Chris@86
|
377
|
Chris@86
|
378 if (m_autoAlignment) {
|
Chris@86
|
379
|
Chris@86
|
380 alignModel(i->first);
|
Chris@86
|
381
|
Chris@86
|
382 } else if (oldMainModel &&
|
Chris@86
|
383 (i->first->getAlignmentReference() == oldMainModel)) {
|
Chris@86
|
384
|
Chris@48
|
385 alignModel(i->first);
|
Chris@48
|
386 }
|
Chris@48
|
387 }
|
Chris@48
|
388
|
Chris@77
|
389 if (oldMainModel) {
|
Chris@79
|
390 oldMainModel->aboutToDelete();
|
Chris@77
|
391 emit modelAboutToBeDeleted(oldMainModel);
|
Chris@77
|
392 }
|
Chris@77
|
393
|
Chris@86
|
394 if (m_autoAlignment) {
|
Chris@86
|
395 alignModel(m_mainModel);
|
Chris@86
|
396 }
|
Chris@86
|
397
|
Chris@45
|
398 emit mainModelChanged(m_mainModel);
|
Chris@45
|
399
|
Chris@45
|
400 delete oldMainModel;
|
Chris@45
|
401 }
|
Chris@45
|
402
|
Chris@45
|
403 void
|
Chris@72
|
404 Document::addDerivedModel(const Transform &transform,
|
Chris@72
|
405 const ModelTransformer::Input &input,
|
Chris@72
|
406 Model *outputModelToAdd)
|
Chris@45
|
407 {
|
Chris@45
|
408 if (m_models.find(outputModelToAdd) != m_models.end()) {
|
Chris@45
|
409 std::cerr << "WARNING: Document::addDerivedModel: Model already added"
|
Chris@45
|
410 << std::endl;
|
Chris@45
|
411 return;
|
Chris@45
|
412 }
|
Chris@45
|
413
|
Chris@77
|
414 #ifdef DEBUG_DOCUMENT
|
Chris@77
|
415 std::cerr << "Document::addDerivedModel: source is " << input.getModel() << " \"" << input.getModel()->objectName().toStdString() << "\"" << std::endl;
|
Chris@77
|
416 #endif
|
Chris@45
|
417
|
Chris@45
|
418 ModelRecord rec;
|
Chris@72
|
419 rec.source = input.getModel();
|
Chris@72
|
420 rec.channel = input.getChannel();
|
Chris@45
|
421 rec.transform = transform;
|
Chris@45
|
422 rec.refcount = 0;
|
Chris@45
|
423
|
Chris@72
|
424 outputModelToAdd->setSourceModel(input.getModel());
|
Chris@45
|
425
|
Chris@45
|
426 m_models[outputModelToAdd] = rec;
|
Chris@45
|
427
|
Chris@45
|
428 emit modelAdded(outputModelToAdd);
|
Chris@45
|
429 }
|
Chris@45
|
430
|
Chris@45
|
431
|
Chris@45
|
432 void
|
Chris@45
|
433 Document::addImportedModel(Model *model)
|
Chris@45
|
434 {
|
Chris@45
|
435 if (m_models.find(model) != m_models.end()) {
|
Chris@45
|
436 std::cerr << "WARNING: Document::addImportedModel: Model already added"
|
Chris@45
|
437 << std::endl;
|
Chris@45
|
438 return;
|
Chris@45
|
439 }
|
Chris@45
|
440
|
Chris@45
|
441 ModelRecord rec;
|
Chris@45
|
442 rec.source = 0;
|
Chris@45
|
443 rec.refcount = 0;
|
Chris@45
|
444
|
Chris@45
|
445 m_models[model] = rec;
|
Chris@45
|
446
|
Chris@47
|
447 if (m_autoAlignment) alignModel(model);
|
Chris@47
|
448
|
Chris@45
|
449 emit modelAdded(model);
|
Chris@45
|
450 }
|
Chris@45
|
451
|
Chris@45
|
452 Model *
|
Chris@72
|
453 Document::addDerivedModel(const Transform &transform,
|
Chris@78
|
454 const ModelTransformer::Input &input,
|
Chris@78
|
455 QString &message)
|
Chris@45
|
456 {
|
Chris@45
|
457 Model *model = 0;
|
Chris@45
|
458
|
Chris@45
|
459 for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) {
|
Chris@45
|
460 if (i->second.transform == transform &&
|
Chris@72
|
461 i->second.source == input.getModel() &&
|
Chris@72
|
462 i->second.channel == input.getChannel()) {
|
Chris@45
|
463 return i->first;
|
Chris@45
|
464 }
|
Chris@45
|
465 }
|
Chris@45
|
466
|
Chris@78
|
467 model = ModelTransformerFactory::getInstance()->transform
|
Chris@78
|
468 (transform, input, message);
|
Chris@45
|
469
|
Chris@83
|
470 // The transform we actually used was presumably identical to the
|
Chris@83
|
471 // one asked for, except that the version of the plugin may
|
Chris@83
|
472 // differ. It's possible that the returned message contains a
|
Chris@83
|
473 // warning about this; that doesn't concern us here, but we do
|
Chris@83
|
474 // need to ensure that the transform we remember is correct for
|
Chris@83
|
475 // what was actually applied, with the current plugin version.
|
Chris@83
|
476
|
Chris@83
|
477 Transform applied = transform;
|
Chris@83
|
478 applied.setPluginVersion
|
Chris@83
|
479 (TransformFactory::getInstance()->
|
Chris@83
|
480 getDefaultTransformFor(transform.getIdentifier(),
|
Chris@83
|
481 lrintf(transform.getSampleRate()))
|
Chris@83
|
482 .getPluginVersion());
|
Chris@83
|
483
|
Chris@45
|
484 if (!model) {
|
Chris@72
|
485 std::cerr << "WARNING: Document::addDerivedModel: no output model for transform " << transform.getIdentifier().toStdString() << std::endl;
|
Chris@45
|
486 } else {
|
Chris@83
|
487 addDerivedModel(applied, input, model);
|
Chris@45
|
488 }
|
Chris@45
|
489
|
Chris@45
|
490 return model;
|
Chris@45
|
491 }
|
Chris@45
|
492
|
Chris@45
|
493 void
|
Chris@45
|
494 Document::releaseModel(Model *model) // Will _not_ release main model!
|
Chris@45
|
495 {
|
Chris@45
|
496 if (model == 0) {
|
Chris@45
|
497 return;
|
Chris@45
|
498 }
|
Chris@45
|
499
|
Chris@45
|
500 if (model == m_mainModel) {
|
Chris@45
|
501 return;
|
Chris@45
|
502 }
|
Chris@45
|
503
|
Chris@45
|
504 bool toDelete = false;
|
Chris@45
|
505
|
Chris@45
|
506 if (m_models.find(model) != m_models.end()) {
|
Chris@45
|
507
|
Chris@45
|
508 if (m_models[model].refcount == 0) {
|
Chris@45
|
509 std::cerr << "WARNING: Document::releaseModel: model " << model
|
Chris@45
|
510 << " reference count is zero already!" << std::endl;
|
Chris@45
|
511 } else {
|
Chris@45
|
512 if (--m_models[model].refcount == 0) {
|
Chris@45
|
513 toDelete = true;
|
Chris@45
|
514 }
|
Chris@45
|
515 }
|
Chris@45
|
516 } else {
|
Chris@45
|
517 std::cerr << "WARNING: Document::releaseModel: Unfound model "
|
Chris@45
|
518 << model << std::endl;
|
Chris@45
|
519 toDelete = true;
|
Chris@45
|
520 }
|
Chris@45
|
521
|
Chris@45
|
522 if (toDelete) {
|
Chris@45
|
523
|
Chris@45
|
524 int sourceCount = 0;
|
Chris@45
|
525
|
Chris@45
|
526 for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) {
|
Chris@45
|
527 if (i->second.source == model) {
|
Chris@45
|
528 ++sourceCount;
|
Chris@45
|
529 i->second.source = 0;
|
Chris@45
|
530 }
|
Chris@45
|
531 }
|
Chris@45
|
532
|
Chris@45
|
533 if (sourceCount > 0) {
|
Chris@45
|
534 std::cerr << "Document::releaseModel: Deleting model "
|
Chris@45
|
535 << model << " even though it is source for "
|
Chris@45
|
536 << sourceCount << " other derived model(s) -- resetting "
|
Chris@45
|
537 << "their source fields appropriately" << std::endl;
|
Chris@45
|
538 }
|
Chris@45
|
539
|
Chris@79
|
540 model->aboutToDelete();
|
Chris@45
|
541 emit modelAboutToBeDeleted(model);
|
Chris@45
|
542 m_models.erase(model);
|
Chris@45
|
543 delete model;
|
Chris@45
|
544 }
|
Chris@45
|
545 }
|
Chris@45
|
546
|
Chris@45
|
547 void
|
Chris@45
|
548 Document::deleteLayer(Layer *layer, bool force)
|
Chris@45
|
549 {
|
Chris@45
|
550 if (m_layerViewMap.find(layer) != m_layerViewMap.end() &&
|
Chris@45
|
551 m_layerViewMap[layer].size() > 0) {
|
Chris@45
|
552
|
Chris@45
|
553 std::cerr << "WARNING: Document::deleteLayer: Layer "
|
Chris@45
|
554 << layer << " [" << layer->objectName().toStdString() << "]"
|
Chris@45
|
555 << " is still used in " << m_layerViewMap[layer].size()
|
Chris@45
|
556 << " views!" << std::endl;
|
Chris@45
|
557
|
Chris@45
|
558 if (force) {
|
Chris@45
|
559
|
Chris@77
|
560 #ifdef DEBUG_DOCUMENT
|
Chris@45
|
561 std::cerr << "(force flag set -- deleting from all views)" << std::endl;
|
Chris@77
|
562 #endif
|
Chris@45
|
563
|
Chris@45
|
564 for (std::set<View *>::iterator j = m_layerViewMap[layer].begin();
|
Chris@45
|
565 j != m_layerViewMap[layer].end(); ++j) {
|
Chris@45
|
566 // don't use removeLayerFromView, as it issues a command
|
Chris@45
|
567 layer->setLayerDormant(*j, true);
|
Chris@45
|
568 (*j)->removeLayer(layer);
|
Chris@45
|
569 }
|
Chris@45
|
570
|
Chris@45
|
571 m_layerViewMap.erase(layer);
|
Chris@45
|
572
|
Chris@45
|
573 } else {
|
Chris@45
|
574 return;
|
Chris@45
|
575 }
|
Chris@45
|
576 }
|
Chris@45
|
577
|
Chris@45
|
578 if (m_layers.find(layer) == m_layers.end()) {
|
Chris@45
|
579 std::cerr << "Document::deleteLayer: Layer "
|
Chris@45
|
580 << layer << " does not exist, or has already been deleted "
|
Chris@45
|
581 << "(this may not be as serious as it sounds)" << std::endl;
|
Chris@45
|
582 return;
|
Chris@45
|
583 }
|
Chris@45
|
584
|
Chris@45
|
585 m_layers.erase(layer);
|
Chris@45
|
586
|
Chris@132
|
587 #ifdef DEBUG_DOCUMENT
|
Chris@52
|
588 std::cerr << "Document::deleteLayer: Removing, now have "
|
Chris@52
|
589 << m_layers.size() << " layers" << std::endl;
|
Chris@132
|
590 #endif
|
Chris@52
|
591
|
Chris@45
|
592 releaseModel(layer->getModel());
|
Chris@45
|
593 emit layerRemoved(layer);
|
Chris@45
|
594 emit layerAboutToBeDeleted(layer);
|
Chris@45
|
595 delete layer;
|
Chris@45
|
596 }
|
Chris@45
|
597
|
Chris@45
|
598 void
|
Chris@45
|
599 Document::setModel(Layer *layer, Model *model)
|
Chris@45
|
600 {
|
Chris@45
|
601 if (model &&
|
Chris@45
|
602 model != m_mainModel &&
|
Chris@45
|
603 m_models.find(model) == m_models.end()) {
|
Chris@45
|
604 std::cerr << "ERROR: Document::setModel: Layer " << layer
|
Chris@45
|
605 << " (\"" << layer->objectName().toStdString()
|
Chris@45
|
606 << "\") wants to use unregistered model " << model
|
Chris@45
|
607 << ": register the layer's model before setting it!"
|
Chris@45
|
608 << std::endl;
|
Chris@45
|
609 return;
|
Chris@45
|
610 }
|
Chris@45
|
611
|
Chris@45
|
612 Model *previousModel = layer->getModel();
|
Chris@45
|
613
|
Chris@45
|
614 if (previousModel == model) {
|
Chris@132
|
615 std::cerr << "NOTE: Document::setModel: Layer " << layer << " (\""
|
Chris@45
|
616 << layer->objectName().toStdString()
|
Chris@45
|
617 << "\") is already set to model "
|
Chris@45
|
618 << model << " (\""
|
Chris@45
|
619 << (model ? model->objectName().toStdString() : "(null)")
|
Chris@45
|
620 << "\")" << std::endl;
|
Chris@45
|
621 return;
|
Chris@45
|
622 }
|
Chris@45
|
623
|
Chris@45
|
624 if (model && model != m_mainModel) {
|
Chris@45
|
625 m_models[model].refcount ++;
|
Chris@45
|
626 }
|
Chris@45
|
627
|
Chris@45
|
628 if (model && previousModel) {
|
Chris@45
|
629 PlayParameterRepository::getInstance()->copyParameters
|
Chris@45
|
630 (previousModel, model);
|
Chris@45
|
631 }
|
Chris@45
|
632
|
Chris@45
|
633 LayerFactory::getInstance()->setModel(layer, model);
|
Chris@45
|
634
|
Chris@45
|
635 if (previousModel) {
|
Chris@45
|
636 releaseModel(previousModel);
|
Chris@45
|
637 }
|
Chris@45
|
638 }
|
Chris@45
|
639
|
Chris@45
|
640 void
|
Chris@45
|
641 Document::setChannel(Layer *layer, int channel)
|
Chris@45
|
642 {
|
Chris@45
|
643 LayerFactory::getInstance()->setChannel(layer, channel);
|
Chris@45
|
644 }
|
Chris@45
|
645
|
Chris@45
|
646 void
|
Chris@45
|
647 Document::addLayerToView(View *view, Layer *layer)
|
Chris@45
|
648 {
|
Chris@45
|
649 Model *model = layer->getModel();
|
Chris@45
|
650 if (!model) {
|
Chris@77
|
651 #ifdef DEBUG_DOCUMENT
|
Chris@77
|
652 std::cerr << "Document::addLayerToView: Layer (\""
|
Chris@77
|
653 << layer->objectName().toStdString()
|
Chris@77
|
654 << "\") with no model being added to view: "
|
Chris@77
|
655 << "normally you want to set the model first" << std::endl;
|
Chris@77
|
656 #endif
|
Chris@45
|
657 } else {
|
Chris@45
|
658 if (model != m_mainModel &&
|
Chris@45
|
659 m_models.find(model) == m_models.end()) {
|
Chris@45
|
660 std::cerr << "ERROR: Document::addLayerToView: Layer " << layer
|
Chris@45
|
661 << " has unregistered model " << model
|
Chris@45
|
662 << " -- register the layer's model before adding the layer!" << std::endl;
|
Chris@45
|
663 return;
|
Chris@45
|
664 }
|
Chris@45
|
665 }
|
Chris@45
|
666
|
Chris@45
|
667 CommandHistory::getInstance()->addCommand
|
Chris@45
|
668 (new Document::AddLayerCommand(this, view, layer));
|
Chris@45
|
669 }
|
Chris@45
|
670
|
Chris@45
|
671 void
|
Chris@45
|
672 Document::removeLayerFromView(View *view, Layer *layer)
|
Chris@45
|
673 {
|
Chris@45
|
674 CommandHistory::getInstance()->addCommand
|
Chris@45
|
675 (new Document::RemoveLayerCommand(this, view, layer));
|
Chris@45
|
676 }
|
Chris@45
|
677
|
Chris@45
|
678 void
|
Chris@45
|
679 Document::addToLayerViewMap(Layer *layer, View *view)
|
Chris@45
|
680 {
|
Chris@45
|
681 bool firstView = (m_layerViewMap.find(layer) == m_layerViewMap.end() ||
|
Chris@45
|
682 m_layerViewMap[layer].empty());
|
Chris@45
|
683
|
Chris@45
|
684 if (m_layerViewMap[layer].find(view) !=
|
Chris@45
|
685 m_layerViewMap[layer].end()) {
|
Chris@45
|
686 std::cerr << "WARNING: Document::addToLayerViewMap:"
|
Chris@45
|
687 << " Layer " << layer << " -> view " << view << " already in"
|
Chris@45
|
688 << " layer view map -- internal inconsistency" << std::endl;
|
Chris@45
|
689 }
|
Chris@45
|
690
|
Chris@45
|
691 m_layerViewMap[layer].insert(view);
|
Chris@45
|
692
|
Chris@45
|
693 if (firstView) emit layerInAView(layer, true);
|
Chris@45
|
694 }
|
Chris@45
|
695
|
Chris@45
|
696 void
|
Chris@45
|
697 Document::removeFromLayerViewMap(Layer *layer, View *view)
|
Chris@45
|
698 {
|
Chris@45
|
699 if (m_layerViewMap[layer].find(view) ==
|
Chris@45
|
700 m_layerViewMap[layer].end()) {
|
Chris@45
|
701 std::cerr << "WARNING: Document::removeFromLayerViewMap:"
|
Chris@45
|
702 << " Layer " << layer << " -> view " << view << " not in"
|
Chris@45
|
703 << " layer view map -- internal inconsistency" << std::endl;
|
Chris@45
|
704 }
|
Chris@45
|
705
|
Chris@45
|
706 m_layerViewMap[layer].erase(view);
|
Chris@45
|
707
|
Chris@45
|
708 if (m_layerViewMap[layer].empty()) {
|
Chris@45
|
709 m_layerViewMap.erase(layer);
|
Chris@45
|
710 emit layerInAView(layer, false);
|
Chris@45
|
711 }
|
Chris@45
|
712 }
|
Chris@45
|
713
|
Chris@45
|
714 QString
|
Chris@45
|
715 Document::getUniqueLayerName(QString candidate)
|
Chris@45
|
716 {
|
Chris@45
|
717 for (int count = 1; ; ++count) {
|
Chris@45
|
718
|
Chris@45
|
719 QString adjusted =
|
Chris@45
|
720 (count > 1 ? QString("%1 <%2>").arg(candidate).arg(count) :
|
Chris@45
|
721 candidate);
|
Chris@45
|
722
|
Chris@45
|
723 bool duplicate = false;
|
Chris@45
|
724
|
Chris@45
|
725 for (LayerSet::iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
|
Chris@45
|
726 if ((*i)->objectName() == adjusted) {
|
Chris@45
|
727 duplicate = true;
|
Chris@45
|
728 break;
|
Chris@45
|
729 }
|
Chris@45
|
730 }
|
Chris@45
|
731
|
Chris@45
|
732 if (!duplicate) return adjusted;
|
Chris@45
|
733 }
|
Chris@45
|
734 }
|
Chris@45
|
735
|
Chris@45
|
736 std::vector<Model *>
|
Chris@72
|
737 Document::getTransformInputModels()
|
Chris@45
|
738 {
|
Chris@45
|
739 std::vector<Model *> models;
|
Chris@45
|
740
|
Chris@45
|
741 if (!m_mainModel) return models;
|
Chris@45
|
742
|
Chris@45
|
743 models.push_back(m_mainModel);
|
Chris@45
|
744
|
Chris@45
|
745 //!!! This will pick up all models, including those that aren't visible...
|
Chris@45
|
746
|
Chris@45
|
747 for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) {
|
Chris@45
|
748
|
Chris@45
|
749 Model *model = i->first;
|
Chris@45
|
750 if (!model || model == m_mainModel) continue;
|
Chris@45
|
751 DenseTimeValueModel *dtvm = dynamic_cast<DenseTimeValueModel *>(model);
|
Chris@45
|
752
|
Chris@45
|
753 if (dtvm) {
|
Chris@45
|
754 models.push_back(dtvm);
|
Chris@45
|
755 }
|
Chris@45
|
756 }
|
Chris@45
|
757
|
Chris@45
|
758 return models;
|
Chris@45
|
759 }
|
Chris@45
|
760
|
Chris@50
|
761 bool
|
Chris@77
|
762 Document::isKnownModel(const Model *model) const
|
Chris@77
|
763 {
|
Chris@77
|
764 if (model == m_mainModel) return true;
|
Chris@77
|
765 return (m_models.find(const_cast<Model *>(model)) != m_models.end());
|
Chris@77
|
766 }
|
Chris@77
|
767
|
Chris@90
|
768 TransformId
|
Chris@90
|
769 Document::getAlignmentTransformName()
|
Chris@90
|
770 {
|
Chris@90
|
771 QSettings settings;
|
Chris@90
|
772 settings.beginGroup("Alignment");
|
Chris@90
|
773 TransformId id =
|
Chris@90
|
774 settings.value("transform-id",
|
Chris@90
|
775 "vamp:match-vamp-plugin:match:path").toString();
|
Chris@90
|
776 settings.endGroup();
|
Chris@90
|
777 return id;
|
Chris@90
|
778 }
|
Chris@90
|
779
|
Chris@77
|
780 bool
|
Chris@51
|
781 Document::canAlign()
|
Chris@50
|
782 {
|
Chris@90
|
783 TransformId id = getAlignmentTransformName();
|
Chris@54
|
784 TransformFactory *factory = TransformFactory::getInstance();
|
Chris@54
|
785 return factory->haveTransform(id);
|
Chris@50
|
786 }
|
Chris@50
|
787
|
Chris@45
|
788 void
|
Chris@45
|
789 Document::alignModel(Model *model)
|
Chris@45
|
790 {
|
Chris@86
|
791 if (!m_mainModel) return;
|
Chris@45
|
792
|
Chris@45
|
793 RangeSummarisableTimeValueModel *rm =
|
Chris@45
|
794 dynamic_cast<RangeSummarisableTimeValueModel *>(model);
|
Chris@45
|
795 if (!rm) return;
|
Chris@48
|
796
|
Chris@86
|
797 if (rm->getAlignmentReference() == m_mainModel) {
|
Chris@86
|
798 std::cerr << "Document::alignModel: model " << rm << " is already aligned to main model " << m_mainModel << std::endl;
|
Chris@86
|
799 return;
|
Chris@86
|
800 }
|
Chris@45
|
801
|
Chris@86
|
802 if (model == m_mainModel) {
|
Chris@86
|
803 // The reference has an empty alignment to itself. This makes
|
Chris@86
|
804 // it possible to distinguish between the reference and any
|
Chris@86
|
805 // unaligned model just by looking at the model itself,
|
Chris@86
|
806 // without also knowing what the main model is
|
Chris@86
|
807 std::cerr << "Document::alignModel(" << model << "): is main model, setting appropriately" << std::endl;
|
Chris@86
|
808 rm->setAlignment(new AlignmentModel(model, model, 0, 0));
|
Chris@86
|
809 return;
|
Chris@86
|
810 }
|
Chris@86
|
811
|
Chris@45
|
812 // This involves creating three new models:
|
Chris@45
|
813
|
Chris@45
|
814 // 1. an AggregateWaveModel to provide the mixdowns of the main
|
Chris@45
|
815 // model and the new model in its two channels, as input to the
|
Chris@45
|
816 // MATCH plugin
|
Chris@45
|
817
|
Chris@45
|
818 // 2. a SparseTimeValueModel, which is the model automatically
|
Chris@53
|
819 // created by FeatureExtractionPluginTransformer when running the
|
Chris@45
|
820 // MATCH plugin (thus containing the alignment path)
|
Chris@45
|
821
|
Chris@45
|
822 // 3. an AlignmentModel, which stores the path model and carries
|
Chris@45
|
823 // out alignment lookups on it.
|
Chris@45
|
824
|
Chris@45
|
825 // The first two of these are provided as arguments to the
|
Chris@45
|
826 // constructor for the third, which takes responsibility for
|
Chris@45
|
827 // deleting them. The AlignmentModel, meanwhile, is passed to the
|
Chris@45
|
828 // new model we are aligning, which also takes responsibility for
|
Chris@45
|
829 // it. We should not have to delete any of these new models here.
|
Chris@45
|
830
|
Chris@45
|
831 AggregateWaveModel::ChannelSpecList components;
|
Chris@45
|
832
|
Chris@45
|
833 components.push_back(AggregateWaveModel::ModelChannelSpec
|
Chris@45
|
834 (m_mainModel, -1));
|
Chris@45
|
835
|
Chris@45
|
836 components.push_back(AggregateWaveModel::ModelChannelSpec
|
Chris@45
|
837 (rm, -1));
|
Chris@45
|
838
|
Chris@112
|
839 Model *aggregateModel = new AggregateWaveModel(components);
|
Chris@112
|
840 ModelTransformer::Input aggregate(aggregateModel);
|
Chris@45
|
841
|
Chris@72
|
842 TransformId id = "vamp:match-vamp-plugin:match:path"; //!!! configure
|
Chris@45
|
843
|
Chris@72
|
844 TransformFactory *tf = TransformFactory::getInstance();
|
Chris@45
|
845
|
Chris@72
|
846 Transform transform = tf->getDefaultTransformFor
|
Chris@112
|
847 (id, aggregateModel->getSampleRate());
|
Chris@57
|
848
|
Chris@72
|
849 transform.setStepSize(transform.getBlockSize()/2);
|
Chris@72
|
850 transform.setParameter("serialise", 1);
|
Chris@64
|
851
|
Chris@81
|
852 std::cerr << "Document::alignModel: Alignment transform step size " << transform.getStepSize() << ", block size " << transform.getBlockSize() << std::endl;
|
Chris@81
|
853
|
Chris@72
|
854 ModelTransformerFactory *mtf = ModelTransformerFactory::getInstance();
|
Chris@72
|
855
|
Chris@78
|
856 QString message;
|
Chris@78
|
857 Model *transformOutput = mtf->transform(transform, aggregate, message);
|
Chris@64
|
858
|
Chris@64
|
859 if (!transformOutput) {
|
Chris@72
|
860 transform.setStepSize(0);
|
Chris@78
|
861 transformOutput = mtf->transform(transform, aggregate, message);
|
Chris@64
|
862 }
|
Chris@45
|
863
|
Chris@45
|
864 SparseTimeValueModel *path = dynamic_cast<SparseTimeValueModel *>
|
Chris@45
|
865 (transformOutput);
|
Chris@45
|
866
|
Chris@45
|
867 if (!path) {
|
Chris@45
|
868 std::cerr << "Document::alignModel: ERROR: Failed to create alignment path (no MATCH plugin?)" << std::endl;
|
Chris@78
|
869 emit alignmentFailed(id, message);
|
Chris@45
|
870 delete transformOutput;
|
Chris@112
|
871 delete aggregateModel;
|
Chris@45
|
872 return;
|
Chris@45
|
873 }
|
Chris@45
|
874
|
Chris@112
|
875 path->setCompletion(0);
|
Chris@112
|
876
|
Chris@45
|
877 AlignmentModel *alignmentModel = new AlignmentModel
|
Chris@112
|
878 (m_mainModel, model, aggregateModel, path);
|
Chris@45
|
879
|
Chris@45
|
880 rm->setAlignment(alignmentModel);
|
Chris@45
|
881 }
|
Chris@45
|
882
|
Chris@45
|
883 void
|
Chris@45
|
884 Document::alignModels()
|
Chris@45
|
885 {
|
Chris@45
|
886 for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) {
|
Chris@45
|
887 alignModel(i->first);
|
Chris@45
|
888 }
|
Chris@86
|
889 alignModel(m_mainModel);
|
Chris@45
|
890 }
|
Chris@45
|
891
|
Chris@45
|
892 Document::AddLayerCommand::AddLayerCommand(Document *d,
|
Chris@45
|
893 View *view,
|
Chris@45
|
894 Layer *layer) :
|
Chris@45
|
895 m_d(d),
|
Chris@45
|
896 m_view(view),
|
Chris@45
|
897 m_layer(layer),
|
Chris@45
|
898 m_name(qApp->translate("AddLayerCommand", "Add %1 Layer").arg(layer->objectName())),
|
Chris@45
|
899 m_added(false)
|
Chris@45
|
900 {
|
Chris@45
|
901 }
|
Chris@45
|
902
|
Chris@45
|
903 Document::AddLayerCommand::~AddLayerCommand()
|
Chris@45
|
904 {
|
Chris@77
|
905 #ifdef DEBUG_DOCUMENT
|
Chris@77
|
906 std::cerr << "Document::AddLayerCommand::~AddLayerCommand" << std::endl;
|
Chris@77
|
907 #endif
|
Chris@45
|
908 if (!m_added) {
|
Chris@45
|
909 m_d->deleteLayer(m_layer);
|
Chris@45
|
910 }
|
Chris@45
|
911 }
|
Chris@45
|
912
|
Chris@45
|
913 void
|
Chris@45
|
914 Document::AddLayerCommand::execute()
|
Chris@45
|
915 {
|
Chris@45
|
916 for (int i = 0; i < m_view->getLayerCount(); ++i) {
|
Chris@45
|
917 if (m_view->getLayer(i) == m_layer) {
|
Chris@45
|
918 // already there
|
Chris@45
|
919 m_layer->setLayerDormant(m_view, false);
|
Chris@45
|
920 m_added = true;
|
Chris@45
|
921 return;
|
Chris@45
|
922 }
|
Chris@45
|
923 }
|
Chris@45
|
924
|
Chris@45
|
925 m_view->addLayer(m_layer);
|
Chris@45
|
926 m_layer->setLayerDormant(m_view, false);
|
Chris@45
|
927
|
Chris@45
|
928 m_d->addToLayerViewMap(m_layer, m_view);
|
Chris@45
|
929 m_added = true;
|
Chris@45
|
930 }
|
Chris@45
|
931
|
Chris@45
|
932 void
|
Chris@45
|
933 Document::AddLayerCommand::unexecute()
|
Chris@45
|
934 {
|
Chris@45
|
935 m_view->removeLayer(m_layer);
|
Chris@45
|
936 m_layer->setLayerDormant(m_view, true);
|
Chris@45
|
937
|
Chris@45
|
938 m_d->removeFromLayerViewMap(m_layer, m_view);
|
Chris@45
|
939 m_added = false;
|
Chris@45
|
940 }
|
Chris@45
|
941
|
Chris@45
|
942 Document::RemoveLayerCommand::RemoveLayerCommand(Document *d,
|
Chris@45
|
943 View *view,
|
Chris@45
|
944 Layer *layer) :
|
Chris@45
|
945 m_d(d),
|
Chris@45
|
946 m_view(view),
|
Chris@45
|
947 m_layer(layer),
|
Chris@45
|
948 m_name(qApp->translate("RemoveLayerCommand", "Delete %1 Layer").arg(layer->objectName())),
|
Chris@45
|
949 m_added(true)
|
Chris@45
|
950 {
|
Chris@45
|
951 }
|
Chris@45
|
952
|
Chris@45
|
953 Document::RemoveLayerCommand::~RemoveLayerCommand()
|
Chris@45
|
954 {
|
Chris@77
|
955 #ifdef DEBUG_DOCUMENT
|
Chris@77
|
956 std::cerr << "Document::RemoveLayerCommand::~RemoveLayerCommand" << std::endl;
|
Chris@77
|
957 #endif
|
Chris@45
|
958 if (!m_added) {
|
Chris@45
|
959 m_d->deleteLayer(m_layer);
|
Chris@45
|
960 }
|
Chris@45
|
961 }
|
Chris@45
|
962
|
Chris@45
|
963 void
|
Chris@45
|
964 Document::RemoveLayerCommand::execute()
|
Chris@45
|
965 {
|
Chris@45
|
966 bool have = false;
|
Chris@45
|
967 for (int i = 0; i < m_view->getLayerCount(); ++i) {
|
Chris@45
|
968 if (m_view->getLayer(i) == m_layer) {
|
Chris@45
|
969 have = true;
|
Chris@45
|
970 break;
|
Chris@45
|
971 }
|
Chris@45
|
972 }
|
Chris@45
|
973
|
Chris@45
|
974 if (!have) { // not there!
|
Chris@45
|
975 m_layer->setLayerDormant(m_view, true);
|
Chris@45
|
976 m_added = false;
|
Chris@45
|
977 return;
|
Chris@45
|
978 }
|
Chris@45
|
979
|
Chris@45
|
980 m_view->removeLayer(m_layer);
|
Chris@45
|
981 m_layer->setLayerDormant(m_view, true);
|
Chris@45
|
982
|
Chris@45
|
983 m_d->removeFromLayerViewMap(m_layer, m_view);
|
Chris@45
|
984 m_added = false;
|
Chris@45
|
985 }
|
Chris@45
|
986
|
Chris@45
|
987 void
|
Chris@45
|
988 Document::RemoveLayerCommand::unexecute()
|
Chris@45
|
989 {
|
Chris@45
|
990 m_view->addLayer(m_layer);
|
Chris@45
|
991 m_layer->setLayerDormant(m_view, false);
|
Chris@45
|
992
|
Chris@45
|
993 m_d->addToLayerViewMap(m_layer, m_view);
|
Chris@45
|
994 m_added = true;
|
Chris@45
|
995 }
|
Chris@45
|
996
|
Chris@45
|
997 void
|
Chris@45
|
998 Document::toXml(QTextStream &out, QString indent, QString extraAttributes) const
|
Chris@45
|
999 {
|
Chris@45
|
1000 out << indent + QString("<data%1%2>\n")
|
Chris@45
|
1001 .arg(extraAttributes == "" ? "" : " ").arg(extraAttributes);
|
Chris@45
|
1002
|
Chris@45
|
1003 if (m_mainModel) {
|
Chris@108
|
1004
|
Chris@45
|
1005 m_mainModel->toXml(out, indent + " ", "mainModel=\"true\"");
|
Chris@108
|
1006
|
Chris@108
|
1007 PlayParameters *playParameters =
|
Chris@108
|
1008 PlayParameterRepository::getInstance()->getPlayParameters(m_mainModel);
|
Chris@108
|
1009 if (playParameters) {
|
Chris@108
|
1010 playParameters->toXml
|
Chris@108
|
1011 (out, indent + " ",
|
Chris@108
|
1012 QString("model=\"%1\"")
|
Chris@108
|
1013 .arg(XmlExportable::getObjectExportId(m_mainModel)));
|
Chris@108
|
1014 }
|
Chris@45
|
1015 }
|
Chris@45
|
1016
|
Chris@45
|
1017 // Models that are not used in a layer that is in a view should
|
Chris@45
|
1018 // not be written. Get our list of required models first.
|
Chris@45
|
1019
|
Chris@45
|
1020 std::set<const Model *> used;
|
Chris@45
|
1021
|
Chris@45
|
1022 for (LayerViewMap::const_iterator i = m_layerViewMap.begin();
|
Chris@45
|
1023 i != m_layerViewMap.end(); ++i) {
|
Chris@45
|
1024
|
Chris@45
|
1025 if (i->first && !i->second.empty() && i->first->getModel()) {
|
Chris@45
|
1026 used.insert(i->first->getModel());
|
Chris@45
|
1027 }
|
Chris@45
|
1028 }
|
Chris@45
|
1029
|
Chris@111
|
1030 std::set<Model *> written;
|
Chris@111
|
1031
|
Chris@45
|
1032 for (ModelMap::const_iterator i = m_models.begin();
|
Chris@45
|
1033 i != m_models.end(); ++i) {
|
Chris@45
|
1034
|
Chris@111
|
1035 Model *model = i->first;
|
Chris@45
|
1036 const ModelRecord &rec = i->second;
|
Chris@45
|
1037
|
Chris@45
|
1038 if (used.find(model) == used.end()) continue;
|
Chris@45
|
1039
|
Chris@45
|
1040 // We need an intelligent way to determine which models need
|
Chris@45
|
1041 // to be streamed (i.e. have been edited, or are small) and
|
Chris@45
|
1042 // which should not be (i.e. remain as generated by a
|
Chris@45
|
1043 // transform, and are large).
|
Chris@45
|
1044 //
|
Chris@45
|
1045 // At the moment we can get away with deciding not to stream
|
Chris@45
|
1046 // dense 3d models or writable wave file models, provided they
|
Chris@45
|
1047 // were generated from a transform, because at the moment there
|
Chris@45
|
1048 // is no way to edit those model types so it should be safe to
|
Chris@45
|
1049 // regenerate them. That won't always work in future though.
|
Chris@45
|
1050 // It would be particularly nice to be able to ask the user,
|
Chris@45
|
1051 // as well as making an intelligent guess.
|
Chris@45
|
1052
|
Chris@45
|
1053 bool writeModel = true;
|
Chris@45
|
1054 bool haveDerivation = false;
|
Chris@45
|
1055
|
Chris@72
|
1056 if (rec.source && rec.transform.getIdentifier() != "") {
|
Chris@45
|
1057 haveDerivation = true;
|
Chris@45
|
1058 }
|
Chris@45
|
1059
|
Chris@45
|
1060 if (haveDerivation) {
|
Chris@45
|
1061 if (dynamic_cast<const WritableWaveFileModel *>(model)) {
|
Chris@45
|
1062 writeModel = false;
|
Chris@45
|
1063 } else if (dynamic_cast<const DenseThreeDimensionalModel *>(model)) {
|
Chris@45
|
1064 writeModel = false;
|
Chris@45
|
1065 }
|
Chris@45
|
1066 }
|
Chris@45
|
1067
|
Chris@45
|
1068 if (writeModel) {
|
Chris@111
|
1069 model->toXml(out, indent + " ");
|
Chris@111
|
1070 written.insert(model);
|
Chris@45
|
1071 }
|
Chris@45
|
1072
|
Chris@45
|
1073 if (haveDerivation) {
|
Chris@72
|
1074 writeBackwardCompatibleDerivation(out, indent + " ",
|
Chris@111
|
1075 model, rec);
|
Chris@45
|
1076 }
|
Chris@45
|
1077
|
Chris@45
|
1078 //!!! We should probably own the PlayParameterRepository
|
Chris@45
|
1079 PlayParameters *playParameters =
|
Chris@111
|
1080 PlayParameterRepository::getInstance()->getPlayParameters(model);
|
Chris@45
|
1081 if (playParameters) {
|
Chris@45
|
1082 playParameters->toXml
|
Chris@45
|
1083 (out, indent + " ",
|
Chris@45
|
1084 QString("model=\"%1\"")
|
Chris@111
|
1085 .arg(XmlExportable::getObjectExportId(model)));
|
Chris@45
|
1086 }
|
Chris@45
|
1087 }
|
Chris@45
|
1088
|
Chris@111
|
1089 //!!!
|
Chris@111
|
1090
|
Chris@111
|
1091 // We should write out the alignment models here. AlignmentModel
|
Chris@111
|
1092 // needs a toXml that writes out the export IDs of its reference
|
Chris@111
|
1093 // and aligned models, and then streams its path model. Note that
|
Chris@111
|
1094 // this will only work when the alignment is complete, so we
|
Chris@111
|
1095 // should probably wait for it if it isn't already by this point.
|
Chris@111
|
1096
|
Chris@111
|
1097 for (std::set<Model *>::const_iterator i = written.begin();
|
Chris@111
|
1098 i != written.end(); ++i) {
|
Chris@111
|
1099
|
Chris@111
|
1100 const Model *model = *i;
|
Chris@111
|
1101 const AlignmentModel *alignment = model->getAlignment();
|
Chris@111
|
1102 if (!alignment) continue;
|
Chris@111
|
1103
|
Chris@111
|
1104 alignment->toXml(out, indent + " ");
|
Chris@111
|
1105 }
|
Chris@111
|
1106
|
Chris@45
|
1107 for (LayerSet::const_iterator i = m_layers.begin();
|
Chris@45
|
1108 i != m_layers.end(); ++i) {
|
Chris@45
|
1109
|
Chris@45
|
1110 (*i)->toXml(out, indent + " ");
|
Chris@45
|
1111 }
|
Chris@45
|
1112
|
Chris@45
|
1113 out << indent + "</data>\n";
|
Chris@45
|
1114 }
|
Chris@45
|
1115
|
Chris@72
|
1116 void
|
Chris@72
|
1117 Document::writeBackwardCompatibleDerivation(QTextStream &out, QString indent,
|
Chris@72
|
1118 Model *targetModel,
|
Chris@72
|
1119 const ModelRecord &rec) const
|
Chris@72
|
1120 {
|
Chris@72
|
1121 // There is a lot of redundancy in the XML we output here, because
|
Chris@72
|
1122 // we want it to work with older SV session file reading code as
|
Chris@72
|
1123 // well.
|
Chris@72
|
1124 //
|
Chris@72
|
1125 // Formerly, a transform was described using a derivation element
|
Chris@72
|
1126 // which set out the source and target models, execution context
|
Chris@72
|
1127 // (step size, input channel etc) and transform id, containing a
|
Chris@72
|
1128 // plugin element which set out the transform parameters and so
|
Chris@72
|
1129 // on. (The plugin element came from a "configurationXml" string
|
Chris@72
|
1130 // obtained from PluginXml.)
|
Chris@72
|
1131 //
|
Chris@72
|
1132 // This has been replaced by a derivation element setting out the
|
Chris@72
|
1133 // source and target models and input channel, containing a
|
Chris@72
|
1134 // transform element which sets out everything in the Transform.
|
Chris@72
|
1135 //
|
Chris@72
|
1136 // In order to retain compatibility with older SV code, however,
|
Chris@72
|
1137 // we have to write out the same stuff into the derivation as
|
Chris@72
|
1138 // before, and manufacture an appropriate plugin element as well
|
Chris@72
|
1139 // as the transform element. In order that newer code knows it's
|
Chris@72
|
1140 // dealing with a newer format, we will also write an attribute
|
Chris@72
|
1141 // 'type="transform"' in the derivation element.
|
Chris@45
|
1142
|
Chris@72
|
1143 const Transform &transform = rec.transform;
|
Chris@72
|
1144
|
Chris@72
|
1145 // Just for reference, this is what we would write if we didn't
|
Chris@72
|
1146 // have to be backward compatible:
|
Chris@72
|
1147 //
|
Chris@72
|
1148 // out << indent
|
Chris@72
|
1149 // << QString("<derivation type=\"transform\" source=\"%1\" "
|
Chris@72
|
1150 // "model=\"%2\" channel=\"%3\">\n")
|
Chris@72
|
1151 // .arg(XmlExportable::getObjectExportId(rec.source))
|
Chris@72
|
1152 // .arg(XmlExportable::getObjectExportId(targetModel))
|
Chris@72
|
1153 // .arg(rec.channel);
|
Chris@72
|
1154 //
|
Chris@72
|
1155 // transform.toXml(out, indent + " ");
|
Chris@72
|
1156 //
|
Chris@72
|
1157 // out << indent << "</derivation>\n";
|
Chris@72
|
1158 //
|
Chris@72
|
1159 // Unfortunately, we can't just do that. So we do this...
|
Chris@72
|
1160
|
Chris@72
|
1161 QString extentsAttributes;
|
Chris@72
|
1162 if (transform.getStartTime() != RealTime::zeroTime ||
|
Chris@72
|
1163 transform.getDuration() != RealTime::zeroTime) {
|
Chris@72
|
1164 extentsAttributes = QString("startFrame=\"%1\" duration=\"%2\" ")
|
Chris@72
|
1165 .arg(RealTime::realTime2Frame(transform.getStartTime(),
|
Chris@72
|
1166 targetModel->getSampleRate()))
|
Chris@72
|
1167 .arg(RealTime::realTime2Frame(transform.getDuration(),
|
Chris@72
|
1168 targetModel->getSampleRate()));
|
Chris@72
|
1169 }
|
Chris@72
|
1170
|
Chris@72
|
1171 out << indent;
|
Chris@72
|
1172 out << QString("<derivation type=\"transform\" source=\"%1\" "
|
Chris@72
|
1173 "model=\"%2\" channel=\"%3\" domain=\"%4\" "
|
Chris@72
|
1174 "stepSize=\"%5\" blockSize=\"%6\" %7windowType=\"%8\" "
|
Chris@72
|
1175 "transform=\"%9\">\n")
|
Chris@72
|
1176 .arg(XmlExportable::getObjectExportId(rec.source))
|
Chris@72
|
1177 .arg(XmlExportable::getObjectExportId(targetModel))
|
Chris@72
|
1178 .arg(rec.channel)
|
Chris@72
|
1179 .arg(TransformFactory::getInstance()->getTransformInputDomain
|
Chris@72
|
1180 (transform.getIdentifier()))
|
Chris@72
|
1181 .arg(transform.getStepSize())
|
Chris@72
|
1182 .arg(transform.getBlockSize())
|
Chris@72
|
1183 .arg(extentsAttributes)
|
Chris@72
|
1184 .arg(int(transform.getWindowType()))
|
Chris@72
|
1185 .arg(XmlExportable::encodeEntities(transform.getIdentifier()));
|
Chris@72
|
1186
|
Chris@72
|
1187 transform.toXml(out, indent + " ");
|
Chris@72
|
1188
|
Chris@72
|
1189 out << indent << " "
|
Chris@72
|
1190 << TransformFactory::getInstance()->getPluginConfigurationXml(transform);
|
Chris@72
|
1191
|
Chris@72
|
1192 out << indent << "</derivation>\n";
|
Chris@72
|
1193 }
|
Chris@72
|
1194
|