annotate framework/SVFileReader.cpp @ 87:1b217963e53a

* Update remaining editable layers to support proper realignment on copy/paste * Permit pasting when no suitable layer is current: create a new layer on paste * Add preference for showing the splash screen or not * Rename spectrogram smoothing prefs (partly following Craig's suggestions)
author Chris Cannam
date Wed, 06 Feb 2008 14:15:09 +0000
parents 1fa8d5e28d0f
children 3b616971bdc6
rev   line source
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 "SVFileReader.h"
Chris@45 17
Chris@45 18 #include "layer/Layer.h"
Chris@45 19 #include "view/View.h"
Chris@45 20 #include "base/PlayParameters.h"
Chris@45 21 #include "base/PlayParameterRepository.h"
Chris@45 22
Chris@45 23 #include "data/fileio/AudioFileReaderFactory.h"
Chris@45 24 #include "data/fileio/FileFinder.h"
Chris@45 25 #include "data/fileio/FileSource.h"
Chris@45 26
Chris@45 27 #include "data/model/WaveFileModel.h"
Chris@45 28 #include "data/model/EditableDenseThreeDimensionalModel.h"
Chris@45 29 #include "data/model/SparseOneDimensionalModel.h"
Chris@45 30 #include "data/model/SparseTimeValueModel.h"
Chris@45 31 #include "data/model/NoteModel.h"
Chris@45 32 #include "data/model/TextModel.h"
Chris@45 33 #include "data/model/ImageModel.h"
Chris@45 34
Chris@72 35 #include "plugin/transform/TransformFactory.h"
Chris@72 36
Chris@45 37 #include "view/Pane.h"
Chris@45 38
Chris@45 39 #include "Document.h"
Chris@45 40
Chris@45 41 #include <QString>
Chris@45 42 #include <QMessageBox>
Chris@45 43 #include <QFileDialog>
Chris@45 44
Chris@45 45 #include <iostream>
Chris@45 46
Chris@45 47 SVFileReader::SVFileReader(Document *document,
Chris@45 48 SVFileReaderPaneCallback &callback,
Chris@45 49 QString location) :
Chris@45 50 m_document(document),
Chris@45 51 m_paneCallback(callback),
Chris@45 52 m_location(location),
Chris@45 53 m_currentPane(0),
Chris@45 54 m_currentDataset(0),
Chris@45 55 m_currentDerivedModel(0),
Chris@45 56 m_currentDerivedModelId(-1),
Chris@45 57 m_currentPlayParameters(0),
Chris@72 58 m_currentTransformSource(0),
Chris@45 59 m_datasetSeparator(" "),
Chris@45 60 m_inRow(false),
Chris@45 61 m_inLayer(false),
Chris@45 62 m_inView(false),
Chris@45 63 m_rowNumber(0),
Chris@45 64 m_ok(false)
Chris@45 65 {
Chris@45 66 }
Chris@45 67
Chris@45 68 void
Chris@45 69 SVFileReader::parse(const QString &xmlData)
Chris@45 70 {
Chris@45 71 QXmlInputSource inputSource;
Chris@45 72 inputSource.setData(xmlData);
Chris@45 73 parse(inputSource);
Chris@45 74 }
Chris@45 75
Chris@45 76 void
Chris@45 77 SVFileReader::parse(QXmlInputSource &inputSource)
Chris@45 78 {
Chris@45 79 QXmlSimpleReader reader;
Chris@45 80 reader.setContentHandler(this);
Chris@45 81 reader.setErrorHandler(this);
Chris@45 82 m_ok = reader.parse(inputSource);
Chris@45 83 }
Chris@45 84
Chris@45 85 bool
Chris@45 86 SVFileReader::isOK()
Chris@45 87 {
Chris@45 88 return m_ok;
Chris@45 89 }
Chris@45 90
Chris@45 91 SVFileReader::~SVFileReader()
Chris@45 92 {
Chris@45 93 if (!m_awaitingDatasets.empty()) {
Chris@45 94 std::cerr << "WARNING: SV-XML: File ended with "
Chris@45 95 << m_awaitingDatasets.size() << " unfilled model dataset(s)"
Chris@45 96 << std::endl;
Chris@45 97 }
Chris@45 98
Chris@45 99 std::set<Model *> unaddedModels;
Chris@45 100
Chris@45 101 for (std::map<int, Model *>::iterator i = m_models.begin();
Chris@45 102 i != m_models.end(); ++i) {
Chris@45 103 if (m_addedModels.find(i->second) == m_addedModels.end()) {
Chris@45 104 unaddedModels.insert(i->second);
Chris@45 105 }
Chris@45 106 }
Chris@45 107
Chris@45 108 if (!unaddedModels.empty()) {
Chris@45 109 std::cerr << "WARNING: SV-XML: File contained "
Chris@45 110 << unaddedModels.size() << " unused models"
Chris@45 111 << std::endl;
Chris@45 112 while (!unaddedModels.empty()) {
Chris@45 113 delete *unaddedModels.begin();
Chris@45 114 unaddedModels.erase(unaddedModels.begin());
Chris@45 115 }
Chris@45 116 }
Chris@45 117 }
Chris@45 118
Chris@45 119 bool
Chris@45 120 SVFileReader::startElement(const QString &, const QString &,
Chris@45 121 const QString &qName,
Chris@45 122 const QXmlAttributes &attributes)
Chris@45 123 {
Chris@45 124 QString name = qName.toLower();
Chris@45 125
Chris@45 126 bool ok = false;
Chris@45 127
Chris@45 128 // Valid element names:
Chris@45 129 //
Chris@45 130 // sv
Chris@45 131 // data
Chris@45 132 // dataset
Chris@45 133 // display
Chris@45 134 // derivation
Chris@45 135 // playparameters
Chris@45 136 // layer
Chris@45 137 // model
Chris@45 138 // point
Chris@45 139 // row
Chris@45 140 // view
Chris@45 141 // window
Chris@72 142 // plugin
Chris@72 143 // transform
Chris@72 144 // selections
Chris@72 145 // selection
Chris@72 146 // measurement
Chris@45 147
Chris@45 148 if (name == "sv") {
Chris@45 149
Chris@45 150 // nothing needed
Chris@45 151 ok = true;
Chris@45 152
Chris@45 153 } else if (name == "data") {
Chris@45 154
Chris@45 155 // nothing needed
Chris@45 156 m_inData = true;
Chris@45 157 ok = true;
Chris@45 158
Chris@45 159 } else if (name == "display") {
Chris@45 160
Chris@45 161 // nothing needed
Chris@45 162 ok = true;
Chris@45 163
Chris@45 164 } else if (name == "window") {
Chris@45 165
Chris@45 166 ok = readWindow(attributes);
Chris@45 167
Chris@45 168 } else if (name == "model") {
Chris@45 169
Chris@45 170 ok = readModel(attributes);
Chris@45 171
Chris@45 172 } else if (name == "dataset") {
Chris@45 173
Chris@45 174 ok = readDatasetStart(attributes);
Chris@45 175
Chris@45 176 } else if (name == "bin") {
Chris@45 177
Chris@45 178 ok = addBinToDataset(attributes);
Chris@45 179
Chris@45 180 } else if (name == "point") {
Chris@45 181
Chris@45 182 ok = addPointToDataset(attributes);
Chris@45 183
Chris@45 184 } else if (name == "row") {
Chris@45 185
Chris@45 186 ok = addRowToDataset(attributes);
Chris@45 187
Chris@45 188 } else if (name == "layer") {
Chris@45 189
Chris@45 190 addUnaddedModels(); // all models must be specified before first layer
Chris@45 191 ok = readLayer(attributes);
Chris@45 192
Chris@45 193 } else if (name == "view") {
Chris@45 194
Chris@45 195 m_inView = true;
Chris@45 196 ok = readView(attributes);
Chris@45 197
Chris@45 198 } else if (name == "derivation") {
Chris@45 199
Chris@45 200 ok = readDerivation(attributes);
Chris@45 201
Chris@45 202 } else if (name == "playparameters") {
Chris@45 203
Chris@45 204 ok = readPlayParameters(attributes);
Chris@45 205
Chris@45 206 } else if (name == "plugin") {
Chris@45 207
Chris@45 208 ok = readPlugin(attributes);
Chris@45 209
Chris@45 210 } else if (name == "selections") {
Chris@45 211
Chris@45 212 m_inSelections = true;
Chris@45 213 ok = true;
Chris@45 214
Chris@45 215 } else if (name == "selection") {
Chris@45 216
Chris@45 217 ok = readSelection(attributes);
Chris@45 218
Chris@45 219 } else if (name == "measurement") {
Chris@45 220
Chris@45 221 ok = readMeasurement(attributes);
Chris@45 222
Chris@72 223 } else if (name == "transform") {
Chris@72 224
Chris@72 225 ok = readTransform(attributes);
Chris@72 226
Chris@72 227 } else if (name == "parameter") {
Chris@72 228
Chris@72 229 ok = readParameter(attributes);
Chris@72 230
Chris@45 231 } else {
Chris@45 232 std::cerr << "WARNING: SV-XML: Unexpected element \""
Chris@45 233 << name.toLocal8Bit().data() << "\"" << std::endl;
Chris@45 234 }
Chris@45 235
Chris@45 236 if (!ok) {
Chris@45 237 std::cerr << "WARNING: SV-XML: Failed to completely process element \""
Chris@45 238 << name.toLocal8Bit().data() << "\"" << std::endl;
Chris@45 239 }
Chris@45 240
Chris@45 241 return true;
Chris@45 242 }
Chris@45 243
Chris@45 244 bool
Chris@45 245 SVFileReader::characters(const QString &text)
Chris@45 246 {
Chris@45 247 bool ok = false;
Chris@45 248
Chris@45 249 if (m_inRow) {
Chris@45 250 ok = readRowData(text);
Chris@45 251 if (!ok) {
Chris@45 252 std::cerr << "WARNING: SV-XML: Failed to read row data content for row " << m_rowNumber << std::endl;
Chris@45 253 }
Chris@45 254 }
Chris@45 255
Chris@45 256 return true;
Chris@45 257 }
Chris@45 258
Chris@45 259 bool
Chris@45 260 SVFileReader::endElement(const QString &, const QString &,
Chris@45 261 const QString &qName)
Chris@45 262 {
Chris@45 263 QString name = qName.toLower();
Chris@45 264
Chris@45 265 if (name == "dataset") {
Chris@45 266
Chris@45 267 if (m_currentDataset) {
Chris@45 268
Chris@45 269 bool foundInAwaiting = false;
Chris@45 270
Chris@45 271 for (std::map<int, int>::iterator i = m_awaitingDatasets.begin();
Chris@45 272 i != m_awaitingDatasets.end(); ++i) {
Chris@45 273 if (haveModel(i->second) &&
Chris@45 274 m_models[i->second] == m_currentDataset) {
Chris@45 275 m_awaitingDatasets.erase(i);
Chris@45 276 foundInAwaiting = true;
Chris@45 277 break;
Chris@45 278 }
Chris@45 279 }
Chris@45 280
Chris@45 281 if (!foundInAwaiting) {
Chris@45 282 std::cerr << "WARNING: SV-XML: Dataset precedes model, or no model uses dataset" << std::endl;
Chris@45 283 }
Chris@45 284 }
Chris@45 285
Chris@45 286 m_currentDataset = 0;
Chris@45 287
Chris@45 288 } else if (name == "data") {
Chris@45 289
Chris@45 290 addUnaddedModels();
Chris@45 291 m_inData = false;
Chris@45 292
Chris@45 293 } else if (name == "derivation") {
Chris@45 294
Chris@45 295 if (!m_currentDerivedModel) {
Chris@45 296 if (m_currentDerivedModel < 0) {
Chris@45 297 std::cerr << "WARNING: SV-XML: Bad derivation output model id "
Chris@45 298 << m_currentDerivedModelId << std::endl;
Chris@45 299 } else if (haveModel(m_currentDerivedModelId)) {
Chris@45 300 std::cerr << "WARNING: SV-XML: Derivation has existing model "
Chris@45 301 << m_currentDerivedModelId
Chris@45 302 << " as target, not regenerating" << std::endl;
Chris@45 303 } else {
Chris@78 304 QString message;
Chris@45 305 m_currentDerivedModel = m_models[m_currentDerivedModelId] =
Chris@72 306 m_document->addDerivedModel
Chris@72 307 (m_currentTransform,
Chris@72 308 ModelTransformer::Input(m_currentTransformSource,
Chris@78 309 m_currentTransformChannel),
Chris@78 310 message);
Chris@79 311 if (!m_currentDerivedModel) {
Chris@79 312 emit modelRegenerationFailed(tr("(derived model in SV-XML)"),
Chris@79 313 m_currentTransform.getIdentifier(),
Chris@79 314 message);
Chris@79 315 } else if (message != "") {
Chris@79 316 emit modelRegenerationWarning(tr("(derived model in SV-XML)"),
Chris@79 317 m_currentTransform.getIdentifier(),
Chris@79 318 message);
Chris@79 319 }
Chris@45 320 }
Chris@45 321 } else {
Chris@72 322 m_document->addDerivedModel
Chris@72 323 (m_currentTransform,
Chris@72 324 ModelTransformer::Input(m_currentTransformSource,
Chris@72 325 m_currentTransformChannel),
Chris@72 326 m_currentDerivedModel);
Chris@45 327 }
Chris@45 328
Chris@45 329 m_addedModels.insert(m_currentDerivedModel);
Chris@45 330 m_currentDerivedModel = 0;
Chris@45 331 m_currentDerivedModelId = -1;
Chris@72 332 m_currentTransformSource = 0;
Chris@72 333 m_currentTransform = Transform();
Chris@72 334 m_currentTransformChannel = -1;
Chris@45 335
Chris@45 336 } else if (name == "row") {
Chris@45 337 m_inRow = false;
Chris@45 338 } else if (name == "layer") {
Chris@45 339 m_inLayer = false;
Chris@45 340 } else if (name == "view") {
Chris@45 341 m_inView = false;
Chris@45 342 } else if (name == "selections") {
Chris@45 343 m_inSelections = false;
Chris@45 344 } else if (name == "playparameters") {
Chris@45 345 m_currentPlayParameters = 0;
Chris@45 346 }
Chris@45 347
Chris@45 348 return true;
Chris@45 349 }
Chris@45 350
Chris@45 351 bool
Chris@45 352 SVFileReader::error(const QXmlParseException &exception)
Chris@45 353 {
Chris@45 354 m_errorString =
Chris@45 355 QString("ERROR: SV-XML: %1 at line %2, column %3")
Chris@45 356 .arg(exception.message())
Chris@45 357 .arg(exception.lineNumber())
Chris@45 358 .arg(exception.columnNumber());
Chris@45 359 std::cerr << m_errorString.toLocal8Bit().data() << std::endl;
Chris@45 360 return QXmlDefaultHandler::error(exception);
Chris@45 361 }
Chris@45 362
Chris@45 363 bool
Chris@45 364 SVFileReader::fatalError(const QXmlParseException &exception)
Chris@45 365 {
Chris@45 366 m_errorString =
Chris@45 367 QString("FATAL ERROR: SV-XML: %1 at line %2, column %3")
Chris@45 368 .arg(exception.message())
Chris@45 369 .arg(exception.lineNumber())
Chris@45 370 .arg(exception.columnNumber());
Chris@45 371 std::cerr << m_errorString.toLocal8Bit().data() << std::endl;
Chris@45 372 return QXmlDefaultHandler::fatalError(exception);
Chris@45 373 }
Chris@45 374
Chris@45 375
Chris@45 376 #define READ_MANDATORY(TYPE, NAME, CONVERSION) \
Chris@45 377 TYPE NAME = attributes.value(#NAME).trimmed().CONVERSION(&ok); \
Chris@45 378 if (!ok) { \
Chris@45 379 std::cerr << "WARNING: SV-XML: Missing or invalid mandatory " #TYPE " attribute \"" #NAME "\"" << std::endl; \
Chris@45 380 return false; \
Chris@45 381 }
Chris@45 382
Chris@45 383 bool
Chris@45 384 SVFileReader::readWindow(const QXmlAttributes &attributes)
Chris@45 385 {
Chris@45 386 bool ok = false;
Chris@45 387
Chris@45 388 READ_MANDATORY(int, width, toInt);
Chris@45 389 READ_MANDATORY(int, height, toInt);
Chris@45 390
Chris@45 391 m_paneCallback.setWindowSize(width, height);
Chris@45 392 return true;
Chris@45 393 }
Chris@45 394
Chris@45 395 void
Chris@45 396 SVFileReader::addUnaddedModels()
Chris@45 397 {
Chris@45 398 std::set<Model *> unaddedModels;
Chris@45 399
Chris@45 400 for (std::map<int, Model *>::iterator i = m_models.begin();
Chris@45 401 i != m_models.end(); ++i) {
Chris@45 402 if (m_addedModels.find(i->second) == m_addedModels.end()) {
Chris@45 403 unaddedModels.insert(i->second);
Chris@45 404 }
Chris@45 405 }
Chris@45 406
Chris@45 407 for (std::set<Model *>::iterator i = unaddedModels.begin();
Chris@45 408 i != unaddedModels.end(); ++i) {
Chris@45 409 m_document->addImportedModel(*i);
Chris@45 410 m_addedModels.insert(*i);
Chris@45 411 }
Chris@45 412 }
Chris@45 413
Chris@45 414 bool
Chris@45 415 SVFileReader::readModel(const QXmlAttributes &attributes)
Chris@45 416 {
Chris@45 417 bool ok = false;
Chris@45 418
Chris@45 419 READ_MANDATORY(int, id, toInt);
Chris@45 420
Chris@45 421 if (haveModel(id)) {
Chris@45 422 std::cerr << "WARNING: SV-XML: Ignoring duplicate model id " << id
Chris@45 423 << std::endl;
Chris@45 424 return false;
Chris@45 425 }
Chris@45 426
Chris@45 427 QString name = attributes.value("name");
Chris@45 428
Chris@45 429 std::cerr << "SVFileReader::readModel: model name \"" << name.toStdString() << "\"" << std::endl;
Chris@45 430
Chris@45 431 READ_MANDATORY(int, sampleRate, toInt);
Chris@45 432
Chris@45 433 QString type = attributes.value("type").trimmed();
Chris@45 434 bool mainModel = (attributes.value("mainModel").trimmed() == "true");
Chris@45 435
Chris@45 436 if (type == "wavefile") {
Chris@45 437
Chris@45 438 WaveFileModel *model = 0;
Chris@45 439 FileFinder *ff = FileFinder::getInstance();
Chris@45 440 QString originalPath = attributes.value("file");
Chris@45 441 QString path = ff->find(FileFinder::AudioFile,
Chris@45 442 originalPath, m_location);
Chris@45 443
Chris@76 444 FileSource file(path, FileSource::ProgressDialog);
Chris@45 445 file.waitForStatus();
Chris@45 446
Chris@45 447 if (!file.isOK()) {
Chris@45 448 std::cerr << "SVFileReader::readModel: Failed to retrieve file \"" << path.toStdString() << "\" for wave file model: " << file.getErrorString().toStdString() << std::endl;
Chris@45 449 } else if (!file.isAvailable()) {
Chris@45 450 std::cerr << "SVFileReader::readModel: Failed to retrieve file \"" << path.toStdString() << "\" for wave file model: Source unavailable" << std::endl;
Chris@45 451 } else {
Chris@45 452
Chris@45 453 file.waitForData();
Chris@45 454 model = new WaveFileModel(file);
Chris@45 455 if (!model->isOK()) {
Chris@45 456 delete model;
Chris@45 457 model = 0;
Chris@45 458 }
Chris@45 459 }
Chris@45 460
Chris@45 461 if (!model) return false;
Chris@45 462
Chris@45 463 model->setObjectName(name);
Chris@45 464 m_models[id] = model;
Chris@45 465 if (mainModel) {
Chris@45 466 m_document->setMainModel(model);
Chris@45 467 m_addedModels.insert(model);
Chris@45 468 }
Chris@45 469 // Derived models will be added when their derivation
Chris@45 470 // is found.
Chris@45 471
Chris@45 472 return true;
Chris@45 473
Chris@45 474 } else if (type == "dense") {
Chris@45 475
Chris@45 476 READ_MANDATORY(int, dimensions, toInt);
Chris@45 477
Chris@45 478 // Currently the only dense model we support here is the dense
Chris@45 479 // 3d model. Dense time-value models are always file-backed
Chris@45 480 // waveform data, at this point, and they come in as wavefile
Chris@45 481 // models.
Chris@45 482
Chris@45 483 if (dimensions == 3) {
Chris@45 484
Chris@45 485 READ_MANDATORY(int, windowSize, toInt);
Chris@45 486 READ_MANDATORY(int, yBinCount, toInt);
Chris@45 487
Chris@45 488 EditableDenseThreeDimensionalModel *model =
Chris@45 489 new EditableDenseThreeDimensionalModel
Chris@45 490 (sampleRate, windowSize, yBinCount);
Chris@45 491
Chris@45 492 float minimum = attributes.value("minimum").trimmed().toFloat(&ok);
Chris@45 493 if (ok) model->setMinimumLevel(minimum);
Chris@45 494
Chris@45 495 float maximum = attributes.value("maximum").trimmed().toFloat(&ok);
Chris@45 496 if (ok) model->setMaximumLevel(maximum);
Chris@45 497
Chris@45 498 int dataset = attributes.value("dataset").trimmed().toInt(&ok);
Chris@45 499 if (ok) m_awaitingDatasets[dataset] = id;
Chris@45 500
Chris@45 501 model->setObjectName(name);
Chris@45 502 m_models[id] = model;
Chris@45 503 return true;
Chris@45 504
Chris@45 505 } else {
Chris@45 506
Chris@45 507 std::cerr << "WARNING: SV-XML: Unexpected dense model dimension ("
Chris@45 508 << dimensions << ")" << std::endl;
Chris@45 509 }
Chris@45 510 } else if (type == "sparse") {
Chris@45 511
Chris@45 512 READ_MANDATORY(int, dimensions, toInt);
Chris@45 513
Chris@45 514 if (dimensions == 1) {
Chris@45 515
Chris@45 516 READ_MANDATORY(int, resolution, toInt);
Chris@45 517
Chris@45 518 if (attributes.value("subtype") == "image") {
Chris@45 519
Chris@45 520 bool notifyOnAdd = (attributes.value("notifyOnAdd") == "true");
Chris@45 521 ImageModel *model = new ImageModel(sampleRate, resolution,
Chris@45 522 notifyOnAdd);
Chris@45 523 model->setObjectName(name);
Chris@45 524 m_models[id] = model;
Chris@45 525
Chris@45 526 } else {
Chris@45 527
Chris@45 528 SparseOneDimensionalModel *model = new SparseOneDimensionalModel
Chris@45 529 (sampleRate, resolution);
Chris@45 530 model->setObjectName(name);
Chris@45 531 m_models[id] = model;
Chris@45 532 }
Chris@45 533
Chris@45 534 int dataset = attributes.value("dataset").trimmed().toInt(&ok);
Chris@45 535 if (ok) m_awaitingDatasets[dataset] = id;
Chris@45 536
Chris@45 537 return true;
Chris@45 538
Chris@45 539 } else if (dimensions == 2 || dimensions == 3) {
Chris@45 540
Chris@45 541 READ_MANDATORY(int, resolution, toInt);
Chris@45 542
Chris@45 543 bool haveMinMax = true;
Chris@45 544 float minimum = attributes.value("minimum").trimmed().toFloat(&ok);
Chris@45 545 if (!ok) haveMinMax = false;
Chris@45 546 float maximum = attributes.value("maximum").trimmed().toFloat(&ok);
Chris@45 547 if (!ok) haveMinMax = false;
Chris@45 548
Chris@45 549 float valueQuantization =
Chris@45 550 attributes.value("valueQuantization").trimmed().toFloat(&ok);
Chris@45 551
Chris@45 552 bool notifyOnAdd = (attributes.value("notifyOnAdd") == "true");
Chris@45 553
Chris@45 554 QString units = attributes.value("units");
Chris@45 555
Chris@45 556 if (dimensions == 2) {
Chris@45 557 if (attributes.value("subtype") == "text") {
Chris@45 558 TextModel *model = new TextModel
Chris@45 559 (sampleRate, resolution, notifyOnAdd);
Chris@45 560 model->setObjectName(name);
Chris@45 561 m_models[id] = model;
Chris@45 562 } else {
Chris@45 563 SparseTimeValueModel *model;
Chris@45 564 if (haveMinMax) {
Chris@45 565 model = new SparseTimeValueModel
Chris@45 566 (sampleRate, resolution, minimum, maximum, notifyOnAdd);
Chris@45 567 } else {
Chris@45 568 model = new SparseTimeValueModel
Chris@45 569 (sampleRate, resolution, notifyOnAdd);
Chris@45 570 }
Chris@45 571 model->setScaleUnits(units);
Chris@45 572 model->setObjectName(name);
Chris@45 573 m_models[id] = model;
Chris@45 574 }
Chris@45 575 } else {
Chris@45 576 NoteModel *model;
Chris@45 577 if (haveMinMax) {
Chris@45 578 model = new NoteModel
Chris@45 579 (sampleRate, resolution, minimum, maximum, notifyOnAdd);
Chris@45 580 } else {
Chris@45 581 model = new NoteModel
Chris@45 582 (sampleRate, resolution, notifyOnAdd);
Chris@45 583 }
Chris@45 584 model->setValueQuantization(valueQuantization);
Chris@45 585 model->setScaleUnits(units);
Chris@45 586 model->setObjectName(name);
Chris@45 587 m_models[id] = model;
Chris@45 588 }
Chris@45 589
Chris@45 590 int dataset = attributes.value("dataset").trimmed().toInt(&ok);
Chris@45 591 if (ok) m_awaitingDatasets[dataset] = id;
Chris@45 592
Chris@45 593 return true;
Chris@45 594
Chris@45 595 } else {
Chris@45 596
Chris@45 597 std::cerr << "WARNING: SV-XML: Unexpected sparse model dimension ("
Chris@45 598 << dimensions << ")" << std::endl;
Chris@45 599 }
Chris@45 600 } else {
Chris@45 601
Chris@45 602 std::cerr << "WARNING: SV-XML: Unexpected model type \""
Chris@45 603 << type.toLocal8Bit().data() << "\" for model id " << id << std::endl;
Chris@45 604 }
Chris@45 605
Chris@45 606 return false;
Chris@45 607 }
Chris@45 608
Chris@45 609 bool
Chris@45 610 SVFileReader::readView(const QXmlAttributes &attributes)
Chris@45 611 {
Chris@45 612 QString type = attributes.value("type");
Chris@45 613 m_currentPane = 0;
Chris@45 614
Chris@45 615 if (type != "pane") {
Chris@45 616 std::cerr << "WARNING: SV-XML: Unexpected view type \""
Chris@45 617 << type.toLocal8Bit().data() << "\"" << std::endl;
Chris@45 618 return false;
Chris@45 619 }
Chris@45 620
Chris@45 621 m_currentPane = m_paneCallback.addPane();
Chris@45 622
Chris@45 623 if (!m_currentPane) {
Chris@45 624 std::cerr << "WARNING: SV-XML: Internal error: Failed to add pane!"
Chris@45 625 << std::endl;
Chris@45 626 return false;
Chris@45 627 }
Chris@45 628
Chris@45 629 bool ok = false;
Chris@45 630
Chris@45 631 View *view = m_currentPane;
Chris@45 632
Chris@45 633 // The view properties first
Chris@45 634
Chris@45 635 READ_MANDATORY(size_t, centre, toUInt);
Chris@45 636 READ_MANDATORY(size_t, zoom, toUInt);
Chris@45 637 READ_MANDATORY(int, followPan, toInt);
Chris@45 638 READ_MANDATORY(int, followZoom, toInt);
Chris@45 639 QString tracking = attributes.value("tracking");
Chris@45 640
Chris@45 641 // Specify the follow modes before we set the actual values
Chris@45 642 view->setFollowGlobalPan(followPan);
Chris@45 643 view->setFollowGlobalZoom(followZoom);
Chris@45 644 view->setPlaybackFollow(tracking == "scroll" ? PlaybackScrollContinuous :
Chris@45 645 tracking == "page" ? PlaybackScrollPage
Chris@45 646 : PlaybackIgnore);
Chris@45 647
Chris@45 648 // Then set these values
Chris@45 649 view->setCentreFrame(centre);
Chris@45 650 view->setZoomLevel(zoom);
Chris@45 651
Chris@45 652 // And pane properties
Chris@45 653 READ_MANDATORY(int, centreLineVisible, toInt);
Chris@45 654 m_currentPane->setCentreLineVisible(centreLineVisible);
Chris@45 655
Chris@45 656 int height = attributes.value("height").toInt(&ok);
Chris@45 657 if (ok) {
Chris@45 658 m_currentPane->resize(m_currentPane->width(), height);
Chris@45 659 }
Chris@45 660
Chris@45 661 return true;
Chris@45 662 }
Chris@45 663
Chris@45 664 bool
Chris@45 665 SVFileReader::readLayer(const QXmlAttributes &attributes)
Chris@45 666 {
Chris@45 667 QString type = attributes.value("type");
Chris@45 668
Chris@45 669 int id;
Chris@45 670 bool ok = false;
Chris@45 671 id = attributes.value("id").trimmed().toInt(&ok);
Chris@45 672
Chris@45 673 if (!ok) {
Chris@45 674 std::cerr << "WARNING: SV-XML: No layer id for layer of type \""
Chris@45 675 << type.toLocal8Bit().data()
Chris@45 676 << "\"" << std::endl;
Chris@45 677 return false;
Chris@45 678 }
Chris@45 679
Chris@45 680 Layer *layer = 0;
Chris@45 681 bool isNewLayer = false;
Chris@45 682
Chris@45 683 // Layers are expected to be defined in layer elements in the data
Chris@45 684 // section, and referred to in layer elements in the view
Chris@45 685 // sections. So if we're in the data section, we expect this
Chris@45 686 // layer not to exist already; if we're in the view section, we
Chris@45 687 // expect it to exist.
Chris@45 688
Chris@45 689 if (m_inData) {
Chris@45 690
Chris@45 691 if (m_layers.find(id) != m_layers.end()) {
Chris@45 692 std::cerr << "WARNING: SV-XML: Ignoring duplicate layer id " << id
Chris@45 693 << " in data section" << std::endl;
Chris@45 694 return false;
Chris@45 695 }
Chris@45 696
Chris@45 697 layer = m_layers[id] = m_document->createLayer
Chris@45 698 (LayerFactory::getInstance()->getLayerTypeForName(type));
Chris@45 699
Chris@45 700 if (layer) {
Chris@45 701 m_layers[id] = layer;
Chris@45 702 isNewLayer = true;
Chris@45 703 }
Chris@45 704
Chris@45 705 } else {
Chris@45 706
Chris@45 707 if (!m_currentPane) {
Chris@45 708 std::cerr << "WARNING: SV-XML: No current pane for layer " << id
Chris@45 709 << " in view section" << std::endl;
Chris@45 710 return false;
Chris@45 711 }
Chris@45 712
Chris@45 713 if (m_layers.find(id) != m_layers.end()) {
Chris@45 714
Chris@45 715 layer = m_layers[id];
Chris@45 716
Chris@45 717 } else {
Chris@45 718 std::cerr << "WARNING: SV-XML: Layer id " << id
Chris@45 719 << " in view section has not been defined -- defining it here"
Chris@45 720 << std::endl;
Chris@45 721
Chris@45 722 layer = m_document->createLayer
Chris@45 723 (LayerFactory::getInstance()->getLayerTypeForName(type));
Chris@45 724
Chris@45 725 if (layer) {
Chris@45 726 m_layers[id] = layer;
Chris@45 727 isNewLayer = true;
Chris@45 728 }
Chris@45 729 }
Chris@45 730 }
Chris@45 731
Chris@45 732 if (!layer) {
Chris@45 733 std::cerr << "WARNING: SV-XML: Failed to add layer of type \""
Chris@45 734 << type.toLocal8Bit().data()
Chris@45 735 << "\"" << std::endl;
Chris@45 736 return false;
Chris@45 737 }
Chris@45 738
Chris@45 739 if (isNewLayer) {
Chris@45 740
Chris@45 741 QString name = attributes.value("name");
Chris@45 742 layer->setObjectName(name);
Chris@45 743
Chris@45 744 int modelId;
Chris@45 745 bool modelOk = false;
Chris@45 746 modelId = attributes.value("model").trimmed().toInt(&modelOk);
Chris@45 747
Chris@45 748 if (modelOk) {
Chris@45 749 if (haveModel(modelId)) {
Chris@45 750 Model *model = m_models[modelId];
Chris@45 751 m_document->setModel(layer, model);
Chris@45 752 } else {
Chris@45 753 std::cerr << "WARNING: SV-XML: Unknown model id " << modelId
Chris@45 754 << " in layer definition" << std::endl;
Chris@45 755 }
Chris@45 756 }
Chris@45 757
Chris@45 758 layer->setProperties(attributes);
Chris@45 759 }
Chris@45 760
Chris@45 761 if (!m_inData && m_currentPane) {
Chris@45 762
Chris@45 763 QString visible = attributes.value("visible");
Chris@45 764 bool dormant = (visible == "false");
Chris@45 765
Chris@45 766 // We need to do this both before and after adding the layer
Chris@45 767 // to the view -- we need it to be dormant if appropriate
Chris@45 768 // before it's actually added to the view so that any property
Chris@45 769 // box gets the right state when it's added, but the add layer
Chris@45 770 // command sets dormant to false because it assumes it may be
Chris@45 771 // restoring a previously dormant layer, so we need to set it
Chris@45 772 // again afterwards too. Hm
Chris@45 773 layer->setLayerDormant(m_currentPane, dormant);
Chris@45 774
Chris@45 775 m_document->addLayerToView(m_currentPane, layer);
Chris@45 776
Chris@45 777 layer->setLayerDormant(m_currentPane, dormant);
Chris@45 778 }
Chris@45 779
Chris@45 780 m_currentLayer = layer;
Chris@45 781 m_inLayer = true;
Chris@45 782
Chris@45 783 return true;
Chris@45 784 }
Chris@45 785
Chris@45 786 bool
Chris@45 787 SVFileReader::readDatasetStart(const QXmlAttributes &attributes)
Chris@45 788 {
Chris@45 789 bool ok = false;
Chris@45 790
Chris@45 791 READ_MANDATORY(int, id, toInt);
Chris@45 792 READ_MANDATORY(int, dimensions, toInt);
Chris@45 793
Chris@45 794 if (m_awaitingDatasets.find(id) == m_awaitingDatasets.end()) {
Chris@45 795 std::cerr << "WARNING: SV-XML: Unwanted dataset " << id << std::endl;
Chris@45 796 return false;
Chris@45 797 }
Chris@45 798
Chris@45 799 int modelId = m_awaitingDatasets[id];
Chris@45 800
Chris@45 801 Model *model = 0;
Chris@45 802 if (haveModel(modelId)) {
Chris@45 803 model = m_models[modelId];
Chris@45 804 } else {
Chris@45 805 std::cerr << "WARNING: SV-XML: Internal error: Unknown model " << modelId
Chris@45 806 << " expecting dataset " << id << std::endl;
Chris@45 807 return false;
Chris@45 808 }
Chris@45 809
Chris@45 810 bool good = false;
Chris@45 811
Chris@45 812 switch (dimensions) {
Chris@45 813 case 1:
Chris@45 814 if (dynamic_cast<SparseOneDimensionalModel *>(model)) good = true;
Chris@45 815 else if (dynamic_cast<ImageModel *>(model)) good = true;
Chris@45 816 break;
Chris@45 817
Chris@45 818 case 2:
Chris@45 819 if (dynamic_cast<SparseTimeValueModel *>(model)) good = true;
Chris@45 820 else if (dynamic_cast<TextModel *>(model)) good = true;
Chris@45 821 break;
Chris@45 822
Chris@45 823 case 3:
Chris@45 824 if (dynamic_cast<NoteModel *>(model)) good = true;
Chris@45 825 else if (dynamic_cast<EditableDenseThreeDimensionalModel *>(model)) {
Chris@45 826 m_datasetSeparator = attributes.value("separator");
Chris@45 827 good = true;
Chris@45 828 }
Chris@45 829 break;
Chris@45 830 }
Chris@45 831
Chris@45 832 if (!good) {
Chris@45 833 std::cerr << "WARNING: SV-XML: Model id " << modelId << " has wrong number of dimensions or inappropriate type for " << dimensions << "-D dataset " << id << std::endl;
Chris@45 834 m_currentDataset = 0;
Chris@45 835 return false;
Chris@45 836 }
Chris@45 837
Chris@45 838 m_currentDataset = model;
Chris@45 839 return true;
Chris@45 840 }
Chris@45 841
Chris@45 842 bool
Chris@45 843 SVFileReader::addPointToDataset(const QXmlAttributes &attributes)
Chris@45 844 {
Chris@45 845 bool ok = false;
Chris@45 846
Chris@45 847 READ_MANDATORY(int, frame, toInt);
Chris@45 848
Chris@45 849 // std::cerr << "SVFileReader::addPointToDataset: frame = " << frame << std::endl;
Chris@45 850
Chris@45 851 SparseOneDimensionalModel *sodm = dynamic_cast<SparseOneDimensionalModel *>
Chris@45 852 (m_currentDataset);
Chris@45 853
Chris@45 854 if (sodm) {
Chris@45 855 // std::cerr << "Current dataset is a sparse one dimensional model" << std::endl;
Chris@45 856 QString label = attributes.value("label");
Chris@45 857 sodm->addPoint(SparseOneDimensionalModel::Point(frame, label));
Chris@45 858 return true;
Chris@45 859 }
Chris@45 860
Chris@45 861 SparseTimeValueModel *stvm = dynamic_cast<SparseTimeValueModel *>
Chris@45 862 (m_currentDataset);
Chris@45 863
Chris@45 864 if (stvm) {
Chris@45 865 // std::cerr << "Current dataset is a sparse time-value model" << std::endl;
Chris@45 866 float value = 0.0;
Chris@45 867 value = attributes.value("value").trimmed().toFloat(&ok);
Chris@45 868 QString label = attributes.value("label");
Chris@45 869 stvm->addPoint(SparseTimeValueModel::Point(frame, value, label));
Chris@45 870 return ok;
Chris@45 871 }
Chris@45 872
Chris@45 873 NoteModel *nm = dynamic_cast<NoteModel *>(m_currentDataset);
Chris@45 874
Chris@45 875 if (nm) {
Chris@45 876 // std::cerr << "Current dataset is a note model" << std::endl;
Chris@45 877 float value = 0.0;
Chris@45 878 value = attributes.value("value").trimmed().toFloat(&ok);
Chris@45 879 size_t duration = 0;
Chris@45 880 duration = attributes.value("duration").trimmed().toUInt(&ok);
Chris@45 881 QString label = attributes.value("label");
Chris@61 882 float level = attributes.value("level").trimmed().toFloat(&ok);
Chris@61 883 if (!ok) { // level is optional
Chris@61 884 level = 1.f;
Chris@61 885 ok = true;
Chris@61 886 }
Chris@61 887 nm->addPoint(NoteModel::Point(frame, value, duration, level, label));
Chris@45 888 return ok;
Chris@45 889 }
Chris@45 890
Chris@45 891 TextModel *tm = dynamic_cast<TextModel *>(m_currentDataset);
Chris@45 892
Chris@45 893 if (tm) {
Chris@45 894 // std::cerr << "Current dataset is a text model" << std::endl;
Chris@45 895 float height = 0.0;
Chris@45 896 height = attributes.value("height").trimmed().toFloat(&ok);
Chris@45 897 QString label = attributes.value("label");
Chris@45 898 // std::cerr << "SVFileReader::addPointToDataset: TextModel: frame = " << frame << ", height = " << height << ", label = " << label.toStdString() << ", ok = " << ok << std::endl;
Chris@45 899 tm->addPoint(TextModel::Point(frame, height, label));
Chris@45 900 return ok;
Chris@45 901 }
Chris@45 902
Chris@45 903 ImageModel *im = dynamic_cast<ImageModel *>(m_currentDataset);
Chris@45 904
Chris@45 905 if (im) {
Chris@45 906 // std::cerr << "Current dataset is an image model" << std::endl;
Chris@45 907 QString image = attributes.value("image");
Chris@45 908 QString label = attributes.value("label");
Chris@45 909 // std::cerr << "SVFileReader::addPointToDataset: ImageModel: frame = " << frame << ", image = " << image.toStdString() << ", label = " << label.toStdString() << ", ok = " << ok << std::endl;
Chris@45 910 im->addPoint(ImageModel::Point(frame, image, label));
Chris@45 911 return ok;
Chris@45 912 }
Chris@45 913
Chris@45 914 std::cerr << "WARNING: SV-XML: Point element found in non-point dataset" << std::endl;
Chris@45 915
Chris@45 916 return false;
Chris@45 917 }
Chris@45 918
Chris@45 919 bool
Chris@45 920 SVFileReader::addBinToDataset(const QXmlAttributes &attributes)
Chris@45 921 {
Chris@45 922 EditableDenseThreeDimensionalModel *dtdm =
Chris@45 923 dynamic_cast<EditableDenseThreeDimensionalModel *>
Chris@45 924 (m_currentDataset);
Chris@45 925
Chris@45 926 if (dtdm) {
Chris@45 927
Chris@45 928 bool ok = false;
Chris@45 929 int n = attributes.value("number").trimmed().toInt(&ok);
Chris@45 930 if (!ok) {
Chris@45 931 std::cerr << "WARNING: SV-XML: Missing or invalid bin number"
Chris@45 932 << std::endl;
Chris@45 933 return false;
Chris@45 934 }
Chris@45 935
Chris@45 936 QString name = attributes.value("name");
Chris@45 937
Chris@45 938 dtdm->setBinName(n, name);
Chris@45 939 return true;
Chris@45 940 }
Chris@45 941
Chris@45 942 std::cerr << "WARNING: SV-XML: Bin definition found in incompatible dataset" << std::endl;
Chris@45 943
Chris@45 944 return false;
Chris@45 945 }
Chris@45 946
Chris@45 947
Chris@45 948 bool
Chris@45 949 SVFileReader::addRowToDataset(const QXmlAttributes &attributes)
Chris@45 950 {
Chris@45 951 m_inRow = false;
Chris@45 952
Chris@45 953 bool ok = false;
Chris@45 954 m_rowNumber = attributes.value("n").trimmed().toInt(&ok);
Chris@45 955 if (!ok) {
Chris@45 956 std::cerr << "WARNING: SV-XML: Missing or invalid row number"
Chris@45 957 << std::endl;
Chris@45 958 return false;
Chris@45 959 }
Chris@45 960
Chris@45 961 m_inRow = true;
Chris@45 962
Chris@45 963 // std::cerr << "SV-XML: In row " << m_rowNumber << std::endl;
Chris@45 964
Chris@45 965 return true;
Chris@45 966 }
Chris@45 967
Chris@45 968 bool
Chris@45 969 SVFileReader::readRowData(const QString &text)
Chris@45 970 {
Chris@45 971 EditableDenseThreeDimensionalModel *dtdm =
Chris@45 972 dynamic_cast<EditableDenseThreeDimensionalModel *>
Chris@45 973 (m_currentDataset);
Chris@45 974
Chris@45 975 bool warned = false;
Chris@45 976
Chris@45 977 if (dtdm) {
Chris@45 978 QStringList data = text.split(m_datasetSeparator);
Chris@45 979
Chris@45 980 DenseThreeDimensionalModel::Column values;
Chris@45 981
Chris@45 982 for (QStringList::iterator i = data.begin(); i != data.end(); ++i) {
Chris@45 983
Chris@45 984 if (values.size() == dtdm->getHeight()) {
Chris@45 985 if (!warned) {
Chris@45 986 std::cerr << "WARNING: SV-XML: Too many y-bins in 3-D dataset row "
Chris@45 987 << m_rowNumber << std::endl;
Chris@45 988 warned = true;
Chris@45 989 }
Chris@45 990 }
Chris@45 991
Chris@45 992 bool ok;
Chris@45 993 float value = i->toFloat(&ok);
Chris@45 994 if (!ok) {
Chris@45 995 std::cerr << "WARNING: SV-XML: Bad floating-point value "
Chris@45 996 << i->toLocal8Bit().data()
Chris@45 997 << " in row data" << std::endl;
Chris@45 998 } else {
Chris@45 999 values.push_back(value);
Chris@45 1000 }
Chris@45 1001 }
Chris@45 1002
Chris@45 1003 dtdm->setColumn(m_rowNumber, values);
Chris@45 1004 return true;
Chris@45 1005 }
Chris@45 1006
Chris@45 1007 std::cerr << "WARNING: SV-XML: Row data found in non-row dataset" << std::endl;
Chris@45 1008
Chris@45 1009 return false;
Chris@45 1010 }
Chris@45 1011
Chris@45 1012 bool
Chris@45 1013 SVFileReader::readDerivation(const QXmlAttributes &attributes)
Chris@45 1014 {
Chris@45 1015 int modelId = 0;
Chris@45 1016 bool modelOk = false;
Chris@45 1017 modelId = attributes.value("model").trimmed().toInt(&modelOk);
Chris@45 1018
Chris@45 1019 if (!modelOk) {
Chris@45 1020 std::cerr << "WARNING: SV-XML: No model id specified for derivation" << std::endl;
Chris@45 1021 return false;
Chris@45 1022 }
Chris@45 1023
Chris@45 1024 if (haveModel(modelId)) {
Chris@45 1025 m_currentDerivedModel = m_models[modelId];
Chris@45 1026 } else {
Chris@45 1027 // we'll regenerate the model when the derivation element ends
Chris@45 1028 m_currentDerivedModel = 0;
Chris@45 1029 }
Chris@45 1030
Chris@45 1031 m_currentDerivedModelId = modelId;
Chris@45 1032
Chris@45 1033 int sourceId = 0;
Chris@45 1034 bool sourceOk = false;
Chris@45 1035 sourceId = attributes.value("source").trimmed().toInt(&sourceOk);
Chris@45 1036
Chris@45 1037 if (sourceOk && haveModel(sourceId)) {
Chris@72 1038 m_currentTransformSource = m_models[sourceId];
Chris@45 1039 } else {
Chris@72 1040 m_currentTransformSource = m_document->getMainModel();
Chris@45 1041 }
Chris@45 1042
Chris@72 1043 m_currentTransform = Transform();
Chris@45 1044
Chris@45 1045 bool ok = false;
Chris@45 1046 int channel = attributes.value("channel").trimmed().toInt(&ok);
Chris@72 1047 if (ok) m_currentTransformChannel = channel;
Chris@72 1048 else m_currentTransformChannel = -1;
Chris@45 1049
Chris@72 1050 QString type = attributes.value("type");
Chris@72 1051
Chris@72 1052 if (type == "transform") {
Chris@72 1053 m_currentTransformIsNewStyle = true;
Chris@72 1054 return true;
Chris@72 1055 } else {
Chris@72 1056 m_currentTransformIsNewStyle = false;
Chris@72 1057 std::cerr << "NOTE: SV-XML: Reading old-style derivation element"
Chris@72 1058 << std::endl;
Chris@72 1059 }
Chris@72 1060
Chris@72 1061 QString transformId = attributes.value("transform");
Chris@72 1062
Chris@72 1063 m_currentTransform.setIdentifier(transformId);
Chris@45 1064
Chris@45 1065 int stepSize = attributes.value("stepSize").trimmed().toInt(&ok);
Chris@72 1066 if (ok) m_currentTransform.setStepSize(stepSize);
Chris@45 1067
Chris@45 1068 int blockSize = attributes.value("blockSize").trimmed().toInt(&ok);
Chris@72 1069 if (ok) m_currentTransform.setBlockSize(blockSize);
Chris@45 1070
Chris@45 1071 int windowType = attributes.value("windowType").trimmed().toInt(&ok);
Chris@72 1072 if (ok) m_currentTransform.setWindowType(WindowType(windowType));
Chris@72 1073
Chris@72 1074 if (!m_currentTransformSource) return true;
Chris@45 1075
Chris@45 1076 QString startFrameStr = attributes.value("startFrame");
Chris@45 1077 QString durationStr = attributes.value("duration");
Chris@45 1078
Chris@45 1079 size_t startFrame = 0;
Chris@45 1080 size_t duration = 0;
Chris@45 1081
Chris@45 1082 if (startFrameStr != "") {
Chris@45 1083 startFrame = startFrameStr.trimmed().toInt(&ok);
Chris@45 1084 if (!ok) startFrame = 0;
Chris@45 1085 }
Chris@45 1086 if (durationStr != "") {
Chris@45 1087 duration = durationStr.trimmed().toInt(&ok);
Chris@45 1088 if (!ok) duration = 0;
Chris@45 1089 }
Chris@45 1090
Chris@72 1091 m_currentTransform.setStartTime
Chris@72 1092 (RealTime::frame2RealTime
Chris@72 1093 (startFrame, m_currentTransformSource->getSampleRate()));
Chris@72 1094
Chris@72 1095 m_currentTransform.setDuration
Chris@72 1096 (RealTime::frame2RealTime
Chris@72 1097 (duration, m_currentTransformSource->getSampleRate()));
Chris@45 1098
Chris@45 1099 return true;
Chris@45 1100 }
Chris@45 1101
Chris@45 1102 bool
Chris@45 1103 SVFileReader::readPlayParameters(const QXmlAttributes &attributes)
Chris@45 1104 {
Chris@45 1105 m_currentPlayParameters = 0;
Chris@45 1106
Chris@45 1107 int modelId = 0;
Chris@45 1108 bool modelOk = false;
Chris@45 1109 modelId = attributes.value("model").trimmed().toInt(&modelOk);
Chris@45 1110
Chris@45 1111 if (!modelOk) {
Chris@45 1112 std::cerr << "WARNING: SV-XML: No model id specified for play parameters" << std::endl;
Chris@45 1113 return false;
Chris@45 1114 }
Chris@45 1115
Chris@45 1116 if (haveModel(modelId)) {
Chris@45 1117
Chris@45 1118 bool ok = false;
Chris@45 1119
Chris@45 1120 PlayParameters *parameters = PlayParameterRepository::getInstance()->
Chris@45 1121 getPlayParameters(m_models[modelId]);
Chris@45 1122
Chris@45 1123 if (!parameters) {
Chris@45 1124 std::cerr << "WARNING: SV-XML: Play parameters for model "
Chris@45 1125 << modelId
Chris@45 1126 << " not found - has model been added to document?"
Chris@45 1127 << std::endl;
Chris@45 1128 return false;
Chris@45 1129 }
Chris@45 1130
Chris@45 1131 bool muted = (attributes.value("mute").trimmed() == "true");
Chris@45 1132 parameters->setPlayMuted(muted);
Chris@45 1133
Chris@45 1134 float pan = attributes.value("pan").toFloat(&ok);
Chris@45 1135 if (ok) parameters->setPlayPan(pan);
Chris@45 1136
Chris@45 1137 float gain = attributes.value("gain").toFloat(&ok);
Chris@45 1138 if (ok) parameters->setPlayGain(gain);
Chris@45 1139
Chris@45 1140 QString pluginId = attributes.value("pluginId");
Chris@45 1141 if (pluginId != "") parameters->setPlayPluginId(pluginId);
Chris@45 1142
Chris@45 1143 m_currentPlayParameters = parameters;
Chris@45 1144
Chris@45 1145 // std::cerr << "Current play parameters for model: " << m_models[modelId] << ": " << m_currentPlayParameters << std::endl;
Chris@45 1146
Chris@45 1147 } else {
Chris@45 1148
Chris@45 1149 std::cerr << "WARNING: SV-XML: Unknown model " << modelId
Chris@45 1150 << " for play parameters" << std::endl;
Chris@45 1151 return false;
Chris@45 1152 }
Chris@45 1153
Chris@45 1154 return true;
Chris@45 1155 }
Chris@45 1156
Chris@45 1157 bool
Chris@45 1158 SVFileReader::readPlugin(const QXmlAttributes &attributes)
Chris@45 1159 {
Chris@45 1160 if (m_currentDerivedModelId < 0 && !m_currentPlayParameters) {
Chris@45 1161 std::cerr << "WARNING: SV-XML: Plugin found outside derivation or play parameters" << std::endl;
Chris@45 1162 return false;
Chris@45 1163 }
Chris@45 1164
Chris@72 1165 if (!m_currentPlayParameters && m_currentTransformIsNewStyle) {
Chris@72 1166 return true;
Chris@72 1167 }
Chris@72 1168
Chris@45 1169 QString configurationXml = "<plugin";
Chris@45 1170
Chris@45 1171 for (int i = 0; i < attributes.length(); ++i) {
Chris@45 1172 configurationXml += QString(" %1=\"%2\"")
Chris@45 1173 .arg(attributes.qName(i))
Chris@45 1174 .arg(XmlExportable::encodeEntities(attributes.value(i)));
Chris@45 1175 }
Chris@45 1176
Chris@45 1177 configurationXml += "/>";
Chris@45 1178
Chris@45 1179 if (m_currentPlayParameters) {
Chris@45 1180 m_currentPlayParameters->setPlayPluginConfiguration(configurationXml);
Chris@45 1181 } else {
Chris@72 1182 TransformFactory::getInstance()->
Chris@72 1183 setParametersFromPluginConfigurationXml(m_currentTransform,
Chris@72 1184 configurationXml);
Chris@45 1185 }
Chris@45 1186
Chris@45 1187 return true;
Chris@45 1188 }
Chris@45 1189
Chris@45 1190 bool
Chris@72 1191 SVFileReader::readTransform(const QXmlAttributes &attributes)
Chris@72 1192 {
Chris@72 1193 if (m_currentDerivedModelId < 0) {
Chris@72 1194 std::cerr << "WARNING: SV-XML: Transform found outside derivation" << std::endl;
Chris@72 1195 return false;
Chris@72 1196 }
Chris@72 1197
Chris@82 1198 m_currentTransform = Transform();
Chris@72 1199 m_currentTransform.setFromXmlAttributes(attributes);
Chris@72 1200 return true;
Chris@72 1201 }
Chris@72 1202
Chris@72 1203 bool
Chris@72 1204 SVFileReader::readParameter(const QXmlAttributes &attributes)
Chris@72 1205 {
Chris@72 1206 if (m_currentDerivedModelId < 0) {
Chris@72 1207 std::cerr << "WARNING: SV-XML: Parameter found outside derivation" << std::endl;
Chris@72 1208 return false;
Chris@72 1209 }
Chris@72 1210
Chris@72 1211 QString name = attributes.value("name");
Chris@72 1212 if (name == "") {
Chris@72 1213 std::cerr << "WARNING: SV-XML: Ignoring nameless transform parameter"
Chris@72 1214 << std::endl;
Chris@72 1215 return false;
Chris@72 1216 }
Chris@72 1217
Chris@72 1218 float value = attributes.value("value").trimmed().toFloat();
Chris@72 1219
Chris@72 1220 m_currentTransform.setParameter(name, value);
Chris@72 1221 return true;
Chris@72 1222 }
Chris@72 1223
Chris@72 1224 bool
Chris@45 1225 SVFileReader::readSelection(const QXmlAttributes &attributes)
Chris@45 1226 {
Chris@45 1227 bool ok;
Chris@45 1228
Chris@45 1229 READ_MANDATORY(int, start, toInt);
Chris@45 1230 READ_MANDATORY(int, end, toInt);
Chris@45 1231
Chris@45 1232 m_paneCallback.addSelection(start, end);
Chris@45 1233
Chris@45 1234 return true;
Chris@45 1235 }
Chris@45 1236
Chris@45 1237 bool
Chris@45 1238 SVFileReader::readMeasurement(const QXmlAttributes &attributes)
Chris@45 1239 {
Chris@45 1240 std::cerr << "SVFileReader::readMeasurement: inLayer "
Chris@45 1241 << m_inLayer << ", layer " << m_currentLayer << std::endl;
Chris@45 1242
Chris@45 1243 if (!m_inLayer) {
Chris@45 1244 std::cerr << "WARNING: SV-XML: Measurement found outside layer" << std::endl;
Chris@45 1245 return false;
Chris@45 1246 }
Chris@45 1247
Chris@45 1248 m_currentLayer->addMeasurementRect(attributes);
Chris@45 1249 return true;
Chris@45 1250 }
Chris@45 1251
Chris@45 1252 SVFileReaderPaneCallback::~SVFileReaderPaneCallback()
Chris@45 1253 {
Chris@45 1254 }
Chris@45 1255