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