Chris@45: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@45: Chris@45: /* Chris@45: Sonic Visualiser Chris@45: An audio file viewer and annotation editor. Chris@45: Centre for Digital Music, Queen Mary, University of London. Chris@45: This file copyright 2006 Chris Cannam and QMUL. Chris@45: Chris@45: This program is free software; you can redistribute it and/or Chris@45: modify it under the terms of the GNU General Public License as Chris@45: published by the Free Software Foundation; either version 2 of the Chris@45: License, or (at your option) any later version. See the file Chris@45: COPYING included with this distribution for more information. Chris@45: */ Chris@45: Chris@45: #include "SVFileReader.h" Chris@45: Chris@45: #include "layer/Layer.h" Chris@45: #include "view/View.h" Chris@45: #include "base/PlayParameters.h" Chris@45: #include "base/PlayParameterRepository.h" Chris@116: #include "base/Preferences.h" Chris@45: Chris@45: #include "data/fileio/AudioFileReaderFactory.h" Chris@45: #include "data/fileio/FileSource.h" Chris@45: Chris@170: #include "data/fileio/FileFinder.h" Chris@109: Chris@479: #include "data/model/ReadOnlyWaveFileModel.h" Chris@45: #include "data/model/EditableDenseThreeDimensionalModel.h" Chris@45: #include "data/model/SparseOneDimensionalModel.h" Chris@45: #include "data/model/SparseTimeValueModel.h" Chris@45: #include "data/model/NoteModel.h" Chris@137: #include "data/model/RegionModel.h" Chris@45: #include "data/model/TextModel.h" Chris@45: #include "data/model/ImageModel.h" Chris@111: #include "data/model/AlignmentModel.h" Chris@629: #include "data/model/AggregateWaveModel.h" Chris@45: Chris@106: #include "transform/TransformFactory.h" Chris@72: Chris@45: #include "view/Pane.h" Chris@45: Chris@109: #include "widgets/ProgressDialog.h" Chris@109: Chris@45: #include "Document.h" Chris@45: Chris@45: #include Chris@45: #include Chris@45: #include Chris@45: Chris@45: #include Chris@45: Chris@45: SVFileReader::SVFileReader(Document *document, Chris@595: SVFileReaderPaneCallback &callback, Chris@45: QString location) : Chris@45: m_document(document), Chris@45: m_paneCallback(callback), Chris@45: m_location(location), Chris@636: m_currentPane(nullptr), Chris@636: m_currentLayer(nullptr), Chris@636: m_currentDataset(nullptr), Chris@636: m_currentDerivedModel(nullptr), Chris@45: m_currentDerivedModelId(-1), Chris@636: m_currentPlayParameters(nullptr), Chris@636: m_currentTransformSource(nullptr), Chris@410: m_currentTransformChannel(0), Chris@410: m_currentTransformIsNewStyle(true), Chris@45: m_datasetSeparator(" "), Chris@45: m_inRow(false), Chris@45: m_inLayer(false), Chris@45: m_inView(false), Chris@410: m_inData(false), Chris@410: m_inSelections(false), Chris@45: m_rowNumber(0), Chris@45: m_ok(false) Chris@45: { Chris@45: } Chris@45: Chris@45: void Chris@45: SVFileReader::parse(const QString &xmlData) Chris@45: { Chris@45: QXmlInputSource inputSource; Chris@45: inputSource.setData(xmlData); Chris@45: parse(inputSource); Chris@45: } Chris@45: Chris@45: void Chris@45: SVFileReader::parse(QXmlInputSource &inputSource) Chris@45: { Chris@45: QXmlSimpleReader reader; Chris@45: reader.setContentHandler(this); Chris@45: reader.setErrorHandler(this); Chris@45: m_ok = reader.parse(inputSource); Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::isOK() Chris@45: { Chris@45: return m_ok; Chris@45: } Chris@595: Chris@45: SVFileReader::~SVFileReader() Chris@45: { Chris@45: if (!m_awaitingDatasets.empty()) { Chris@601: SVCERR << "WARNING: SV-XML: File ended with " Chris@595: << m_awaitingDatasets.size() << " unfilled model dataset(s)" Chris@595: << endl; Chris@45: } Chris@45: Chris@45: std::set unaddedModels; Chris@45: Chris@45: for (std::map::iterator i = m_models.begin(); Chris@595: i != m_models.end(); ++i) { Chris@595: if (m_addedModels.find(i->second) == m_addedModels.end()) { Chris@595: unaddedModels.insert(i->second); Chris@595: } Chris@45: } Chris@45: Chris@45: if (!unaddedModels.empty()) { Chris@601: SVCERR << "WARNING: SV-XML: File contained " Chris@595: << unaddedModels.size() << " unused models" Chris@595: << endl; Chris@595: while (!unaddedModels.empty()) { Chris@595: delete *unaddedModels.begin(); Chris@595: unaddedModels.erase(unaddedModels.begin()); Chris@595: } Chris@595: } Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::startElement(const QString &, const QString &, Chris@595: const QString &qName, Chris@595: const QXmlAttributes &attributes) Chris@45: { Chris@45: QString name = qName.toLower(); Chris@45: Chris@45: bool ok = false; Chris@45: Chris@45: // Valid element names: Chris@45: // Chris@45: // sv Chris@45: // data Chris@45: // dataset Chris@45: // display Chris@45: // derivation Chris@45: // playparameters Chris@45: // layer Chris@45: // model Chris@45: // point Chris@45: // row Chris@45: // view Chris@45: // window Chris@72: // plugin Chris@72: // transform Chris@72: // selections Chris@72: // selection Chris@72: // measurement Chris@45: Chris@45: if (name == "sv") { Chris@45: Chris@595: // nothing needed Chris@595: ok = true; Chris@45: Chris@45: } else if (name == "data") { Chris@45: Chris@595: // nothing needed Chris@595: m_inData = true; Chris@595: ok = true; Chris@45: Chris@45: } else if (name == "display") { Chris@45: Chris@595: // nothing needed Chris@595: ok = true; Chris@45: Chris@45: } else if (name == "window") { Chris@45: Chris@595: ok = readWindow(attributes); Chris@45: Chris@45: } else if (name == "model") { Chris@45: Chris@595: ok = readModel(attributes); Chris@45: Chris@45: } else if (name == "dataset") { Chris@595: Chris@595: ok = readDatasetStart(attributes); Chris@45: Chris@45: } else if (name == "bin") { Chris@595: Chris@595: ok = addBinToDataset(attributes); Chris@45: Chris@45: } else if (name == "point") { Chris@595: Chris@595: ok = addPointToDataset(attributes); Chris@45: Chris@45: } else if (name == "row") { Chris@45: Chris@595: ok = addRowToDataset(attributes); Chris@45: Chris@45: } else if (name == "layer") { Chris@45: Chris@45: addUnaddedModels(); // all models must be specified before first layer Chris@595: ok = readLayer(attributes); Chris@45: Chris@45: } else if (name == "view") { Chris@45: Chris@595: m_inView = true; Chris@595: ok = readView(attributes); Chris@45: Chris@45: } else if (name == "derivation") { Chris@45: Chris@629: makeAggregateModels(); // must be done before derivations that use them Chris@595: ok = readDerivation(attributes); Chris@45: Chris@45: } else if (name == "playparameters") { Chris@45: Chris@45: ok = readPlayParameters(attributes); Chris@45: Chris@45: } else if (name == "plugin") { Chris@45: Chris@595: ok = readPlugin(attributes); Chris@45: Chris@45: } else if (name == "selections") { Chris@45: Chris@595: m_inSelections = true; Chris@595: ok = true; Chris@45: Chris@45: } else if (name == "selection") { Chris@45: Chris@595: ok = readSelection(attributes); Chris@45: Chris@45: } else if (name == "measurement") { Chris@45: Chris@45: ok = readMeasurement(attributes); Chris@45: Chris@72: } else if (name == "transform") { Chris@72: Chris@72: ok = readTransform(attributes); Chris@72: Chris@72: } else if (name == "parameter") { Chris@72: Chris@72: ok = readParameter(attributes); Chris@72: Chris@45: } else { Chris@601: SVCERR << "WARNING: SV-XML: Unexpected element \"" Chris@294: << name << "\"" << endl; Chris@45: } Chris@45: Chris@45: if (!ok) { Chris@601: SVCERR << "WARNING: SV-XML: Failed to completely process element \"" Chris@595: << name << "\"" << endl; Chris@45: } Chris@45: Chris@45: return true; Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::characters(const QString &text) Chris@45: { Chris@45: bool ok = false; Chris@45: Chris@45: if (m_inRow) { Chris@595: ok = readRowData(text); Chris@595: if (!ok) { Chris@601: SVCERR << "WARNING: SV-XML: Failed to read row data content for row " << m_rowNumber << endl; Chris@595: } Chris@45: } Chris@45: Chris@45: return true; Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::endElement(const QString &, const QString &, Chris@595: const QString &qName) Chris@45: { Chris@45: QString name = qName.toLower(); Chris@45: Chris@45: if (name == "dataset") { Chris@45: Chris@595: if (m_currentDataset) { Chris@595: Chris@595: bool foundInAwaiting = false; Chris@45: Chris@595: for (std::map::iterator i = m_awaitingDatasets.begin(); Chris@595: i != m_awaitingDatasets.end(); ++i) { Chris@595: if (haveModel(i->second) && Chris@45: m_models[i->second] == m_currentDataset) { Chris@595: m_awaitingDatasets.erase(i); Chris@595: foundInAwaiting = true; Chris@595: break; Chris@595: } Chris@595: } Chris@45: Chris@595: if (!foundInAwaiting) { Chris@601: SVCERR << "WARNING: SV-XML: Dataset precedes model, or no model uses dataset" << endl; Chris@595: } Chris@595: } Chris@45: Chris@636: m_currentDataset = nullptr; Chris@45: Chris@45: } else if (name == "data") { Chris@45: Chris@45: addUnaddedModels(); Chris@595: m_inData = false; Chris@45: Chris@45: } else if (name == "derivation") { Chris@45: Chris@45: if (!m_currentDerivedModel) { Chris@358: if (m_currentDerivedModelId < 0) { Chris@601: SVCERR << "WARNING: SV-XML: Bad derivation output model id " Chris@293: << m_currentDerivedModelId << endl; Chris@45: } else if (haveModel(m_currentDerivedModelId)) { Chris@601: SVCERR << "WARNING: SV-XML: Derivation has existing model " Chris@45: << m_currentDerivedModelId Chris@293: << " as target, not regenerating" << endl; Chris@45: } else { Chris@78: QString message; Chris@45: m_currentDerivedModel = m_models[m_currentDerivedModelId] = Chris@72: m_document->addDerivedModel Chris@72: (m_currentTransform, Chris@72: ModelTransformer::Input(m_currentTransformSource, Chris@78: m_currentTransformChannel), Chris@78: message); Chris@79: if (!m_currentDerivedModel) { Chris@79: emit modelRegenerationFailed(tr("(derived model in SV-XML)"), Chris@79: m_currentTransform.getIdentifier(), Chris@79: message); Chris@79: } else if (message != "") { Chris@79: emit modelRegenerationWarning(tr("(derived model in SV-XML)"), Chris@79: m_currentTransform.getIdentifier(), Chris@79: message); Chris@79: } Chris@45: } Chris@45: } else { Chris@329: m_document->addAlreadyDerivedModel Chris@72: (m_currentTransform, Chris@72: ModelTransformer::Input(m_currentTransformSource, Chris@72: m_currentTransformChannel), Chris@72: m_currentDerivedModel); Chris@45: } Chris@45: Chris@45: m_addedModels.insert(m_currentDerivedModel); Chris@636: m_currentDerivedModel = nullptr; Chris@45: m_currentDerivedModelId = -1; Chris@636: m_currentTransformSource = nullptr; Chris@72: m_currentTransform = Transform(); Chris@72: m_currentTransformChannel = -1; Chris@45: Chris@45: } else if (name == "row") { Chris@595: m_inRow = false; Chris@45: } else if (name == "layer") { Chris@45: m_inLayer = false; Chris@45: } else if (name == "view") { Chris@595: m_inView = false; Chris@45: } else if (name == "selections") { Chris@595: m_inSelections = false; Chris@45: } else if (name == "playparameters") { Chris@636: m_currentPlayParameters = nullptr; Chris@45: } Chris@45: Chris@45: return true; Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::error(const QXmlParseException &exception) Chris@45: { Chris@45: m_errorString = Chris@595: QString("ERROR: SV-XML: %1 at line %2, column %3") Chris@595: .arg(exception.message()) Chris@595: .arg(exception.lineNumber()) Chris@595: .arg(exception.columnNumber()); Chris@601: SVCERR << m_errorString << endl; Chris@45: return QXmlDefaultHandler::error(exception); Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::fatalError(const QXmlParseException &exception) Chris@45: { Chris@45: m_errorString = Chris@595: QString("FATAL ERROR: SV-XML: %1 at line %2, column %3") Chris@595: .arg(exception.message()) Chris@595: .arg(exception.lineNumber()) Chris@595: .arg(exception.columnNumber()); Chris@601: SVCERR << m_errorString << endl; Chris@45: return QXmlDefaultHandler::fatalError(exception); Chris@45: } Chris@45: Chris@45: Chris@595: #define READ_MANDATORY(TYPE, NAME, CONVERSION) \ Chris@45: TYPE NAME = attributes.value(#NAME).trimmed().CONVERSION(&ok); \ Chris@45: if (!ok) { \ Chris@601: SVCERR << "WARNING: SV-XML: Missing or invalid mandatory " #TYPE " attribute \"" #NAME "\"" << endl; \ Chris@595: return false; \ Chris@45: } Chris@45: Chris@45: bool Chris@576: SVFileReader::readWindow(const QXmlAttributes &) Chris@45: { Chris@576: // The window element contains window dimensions, which we used to Chris@576: // read and size the window accordingly. This was a Bad Idea [tm] Chris@576: // and we now do nothing instead. See #1769 Loading window Chris@576: // dimensions from session file is a really bad idea Chris@45: return true; Chris@45: } Chris@45: Chris@45: void Chris@629: SVFileReader::makeAggregateModels() Chris@629: { Chris@629: std::map stillPending; Chris@629: Chris@629: for (auto p: m_pendingAggregates) { Chris@629: Chris@629: int id = p.first; Chris@629: const PendingAggregateRec &rec = p.second; Chris@629: bool skip = false; Chris@629: Chris@629: AggregateWaveModel::ChannelSpecList specs; Chris@629: for (int componentId: rec.components) { Chris@629: bool found = false; Chris@629: if (m_models.find(componentId) != m_models.end()) { Chris@629: RangeSummarisableTimeValueModel *rs = Chris@629: dynamic_cast Chris@629: (m_models[componentId]); Chris@629: if (rs) { Chris@629: specs.push_back(AggregateWaveModel::ModelChannelSpec Chris@629: (rs, -1)); Chris@629: found = true; Chris@629: } Chris@629: } Chris@629: if (!found) { Chris@629: SVDEBUG << "SVFileReader::makeAggregateModels:" Chris@629: << "Unknown component model id " Chris@629: << componentId << " in aggregate model id " << id Chris@629: << ", hoping we won't be needing it just yet" Chris@629: << endl; Chris@629: skip = true; Chris@629: } Chris@629: } Chris@629: Chris@629: if (skip) { Chris@629: stillPending[id] = rec; Chris@629: } else { Chris@629: AggregateWaveModel *model = new AggregateWaveModel(specs); Chris@629: model->setObjectName(rec.name); Chris@629: Chris@629: SVDEBUG << "SVFileReader::makeAggregateModels: created aggregate model id " Chris@629: << id << " with " << specs.size() << " components" << endl; Chris@629: Chris@629: m_models[id] = model; Chris@629: } Chris@629: } Chris@629: Chris@629: m_pendingAggregates = stillPending; Chris@629: } Chris@629: Chris@629: void Chris@45: SVFileReader::addUnaddedModels() Chris@45: { Chris@629: makeAggregateModels(); Chris@629: Chris@45: std::set unaddedModels; Chris@45: Chris@45: for (std::map::iterator i = m_models.begin(); Chris@45: i != m_models.end(); ++i) { Chris@45: if (m_addedModels.find(i->second) == m_addedModels.end()) { Chris@45: unaddedModels.insert(i->second); Chris@45: } Chris@45: } Chris@45: Chris@45: for (std::set::iterator i = unaddedModels.begin(); Chris@45: i != unaddedModels.end(); ++i) { Chris@212: Model *model = *i; Chris@212: // don't want to add these models, because their lifespans Chris@212: // are entirely dictated by the models that "own" them even Chris@212: // though they were read independently from the .sv file. Chris@212: // (pity we don't have a nicer way) Chris@212: if (!dynamic_cast(model) && Chris@212: !dynamic_cast(model)) { Chris@212: m_document->addImportedModel(model); Chris@212: } Chris@212: // but we add all models here, so they don't get deleted Chris@212: // when the file loader is destroyed Chris@212: m_addedModels.insert(model); Chris@45: } Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::readModel(const QXmlAttributes &attributes) Chris@45: { Chris@45: bool ok = false; Chris@45: Chris@45: READ_MANDATORY(int, id, toInt); Chris@45: Chris@45: if (haveModel(id)) { Chris@601: SVCERR << "WARNING: SV-XML: Ignoring duplicate model id " << id Chris@595: << endl; Chris@595: return false; Chris@45: } Chris@45: Chris@45: QString name = attributes.value("name"); Chris@45: Chris@233: SVDEBUG << "SVFileReader::readModel: model name \"" << name << "\"" << endl; Chris@45: Chris@436: READ_MANDATORY(double, sampleRate, toDouble); Chris@45: Chris@45: QString type = attributes.value("type").trimmed(); Chris@362: bool isMainModel = (attributes.value("mainModel").trimmed() == "true"); Chris@45: Chris@45: if (type == "wavefile") { Chris@595: Chris@636: WaveFileModel *model = nullptr; Chris@45: FileFinder *ff = FileFinder::getInstance(); Chris@45: QString originalPath = attributes.value("file"); Chris@45: QString path = ff->find(FileFinder::AudioFile, Chris@45: originalPath, m_location); Chris@45: Chris@248: SVDEBUG << "Wave file originalPath = " << originalPath << ", path = " Chris@248: << path << endl; Chris@248: Chris@109: ProgressDialog dialog(tr("Opening file or URL..."), true, 2000); Chris@109: FileSource file(path, &dialog); Chris@45: file.waitForStatus(); Chris@45: Chris@45: if (!file.isOK()) { Chris@601: SVCERR << "SVFileReader::readModel: Failed to retrieve file \"" << path << "\" for wave file model: " << file.getErrorString() << endl; Chris@45: } else if (!file.isAvailable()) { Chris@601: SVCERR << "SVFileReader::readModel: Failed to retrieve file \"" << path << "\" for wave file model: Source unavailable" << endl; Chris@45: } else { Chris@45: Chris@45: file.waitForData(); Chris@116: Chris@436: sv_samplerate_t rate = sampleRate; Chris@116: Chris@360: if (Preferences::getInstance()->getFixedSampleRate() != 0) { Chris@360: rate = Preferences::getInstance()->getFixedSampleRate(); Chris@362: } else if (rate == 0 && Chris@362: !isMainModel && Chris@360: Preferences::getInstance()->getResampleOnLoad()) { Chris@116: WaveFileModel *mm = m_document->getMainModel(); Chris@116: if (mm) rate = mm->getSampleRate(); Chris@116: } Chris@116: Chris@479: model = new ReadOnlyWaveFileModel(file, rate); Chris@45: if (!model->isOK()) { Chris@45: delete model; Chris@636: model = nullptr; Chris@45: } Chris@45: } Chris@45: Chris@601: if (!model) { Chris@601: m_document->setIncomplete(true); Chris@601: return false; Chris@601: } Chris@45: Chris@45: model->setObjectName(name); Chris@595: m_models[id] = model; Chris@595: if (isMainModel) { Chris@595: m_document->setMainModel(model); Chris@595: m_addedModels.insert(model); Chris@595: } Chris@595: // Derived models will be added when their derivation Chris@595: // is found. Chris@45: Chris@595: return true; Chris@45: Chris@629: } else if (type == "aggregatewave") { Chris@629: Chris@629: QString components = attributes.value("components"); Chris@629: QStringList componentIdStrings = components.split(","); Chris@629: std::vector componentIds; Chris@629: for (auto cid: componentIdStrings) { Chris@629: bool ok = false; Chris@629: int id = cid.toInt(&ok); Chris@629: if (!ok) { Chris@629: SVCERR << "SVFileReader::readModel: Failed to convert component model id from part \"" << cid << "\" in \"" << components << "\"" << endl; Chris@629: } else { Chris@629: componentIds.push_back(id); Chris@629: } Chris@629: } Chris@629: PendingAggregateRec rec { name, sampleRate, componentIds }; Chris@629: m_pendingAggregates[id] = rec; Chris@629: Chris@629: // The aggregate model will be constructed from its pending Chris@629: // record in makeAggregateModels; it can't happen here because Chris@629: // the component models might not all have been observed yet Chris@629: // (an unfortunate accident of the way the file is written) Chris@629: Chris@629: return true; Chris@629: Chris@45: } else if (type == "dense") { Chris@595: Chris@595: READ_MANDATORY(int, dimensions, toInt); Chris@595: Chris@595: // Currently the only dense model we support here is the dense Chris@595: // 3d model. Dense time-value models are always file-backed Chris@595: // waveform data, at this point, and they come in as wavefile Chris@595: // models. Chris@595: Chris@595: if (dimensions == 3) { Chris@595: Chris@595: READ_MANDATORY(int, windowSize, toInt); Chris@595: READ_MANDATORY(int, yBinCount, toInt); Chris@595: Chris@45: EditableDenseThreeDimensionalModel *model = Chris@595: new EditableDenseThreeDimensionalModel Chris@153: (sampleRate, windowSize, yBinCount, Chris@153: EditableDenseThreeDimensionalModel::NoCompression); Chris@595: Chris@595: float minimum = attributes.value("minimum").trimmed().toFloat(&ok); Chris@595: if (ok) model->setMinimumLevel(minimum); Chris@595: Chris@595: float maximum = attributes.value("maximum").trimmed().toFloat(&ok); Chris@595: if (ok) model->setMaximumLevel(maximum); Chris@45: Chris@595: int dataset = attributes.value("dataset").trimmed().toInt(&ok); Chris@595: if (ok) m_awaitingDatasets[dataset] = id; Chris@45: Chris@181: int startFrame = attributes.value("startFrame").trimmed().toInt(&ok); Chris@181: if (ok) model->setStartFrame(startFrame); Chris@181: Chris@45: model->setObjectName(name); Chris@595: m_models[id] = model; Chris@595: return true; Chris@45: Chris@595: } else { Chris@45: Chris@601: SVCERR << "WARNING: SV-XML: Unexpected dense model dimension (" Chris@595: << dimensions << ")" << endl; Chris@595: } Chris@45: } else if (type == "sparse") { Chris@45: Chris@595: READ_MANDATORY(int, dimensions, toInt); Chris@595: Chris@595: if (dimensions == 1) { Chris@595: Chris@595: READ_MANDATORY(int, resolution, toInt); Chris@45: Chris@45: if (attributes.value("subtype") == "image") { Chris@45: Chris@45: bool notifyOnAdd = (attributes.value("notifyOnAdd") == "true"); Chris@45: ImageModel *model = new ImageModel(sampleRate, resolution, Chris@45: notifyOnAdd); Chris@45: model->setObjectName(name); Chris@45: m_models[id] = model; Chris@45: Chris@45: } else { Chris@45: Chris@45: SparseOneDimensionalModel *model = new SparseOneDimensionalModel Chris@45: (sampleRate, resolution); Chris@45: model->setObjectName(name); Chris@45: m_models[id] = model; Chris@45: } Chris@45: Chris@595: int dataset = attributes.value("dataset").trimmed().toInt(&ok); Chris@595: if (ok) m_awaitingDatasets[dataset] = id; Chris@45: Chris@595: return true; Chris@45: Chris@595: } else if (dimensions == 2 || dimensions == 3) { Chris@595: Chris@595: READ_MANDATORY(int, resolution, toInt); Chris@45: Chris@45: bool haveMinMax = true; Chris@595: float minimum = attributes.value("minimum").trimmed().toFloat(&ok); Chris@45: if (!ok) haveMinMax = false; Chris@595: float maximum = attributes.value("maximum").trimmed().toFloat(&ok); Chris@45: if (!ok) haveMinMax = false; Chris@45: Chris@595: float valueQuantization = Chris@595: attributes.value("valueQuantization").trimmed().toFloat(&ok); Chris@45: Chris@595: bool notifyOnAdd = (attributes.value("notifyOnAdd") == "true"); Chris@45: Chris@45: QString units = attributes.value("units"); Chris@45: Chris@595: if (dimensions == 2) { Chris@595: if (attributes.value("subtype") == "text") { Chris@595: TextModel *model = new TextModel Chris@595: (sampleRate, resolution, notifyOnAdd); Chris@45: model->setObjectName(name); Chris@595: m_models[id] = model; Chris@111: } else if (attributes.value("subtype") == "path") { Chris@111: PathModel *model = new PathModel Chris@111: (sampleRate, resolution, notifyOnAdd); Chris@111: model->setObjectName(name); Chris@111: m_models[id] = model; Chris@595: } else { Chris@595: SparseTimeValueModel *model; Chris@45: if (haveMinMax) { Chris@45: model = new SparseTimeValueModel Chris@45: (sampleRate, resolution, minimum, maximum, notifyOnAdd); Chris@45: } else { Chris@45: model = new SparseTimeValueModel Chris@45: (sampleRate, resolution, notifyOnAdd); Chris@45: } Chris@45: model->setScaleUnits(units); Chris@45: model->setObjectName(name); Chris@595: m_models[id] = model; Chris@595: } Chris@595: } else { Chris@137: if (attributes.value("subtype") == "region") { Chris@137: RegionModel *model; Chris@137: if (haveMinMax) { Chris@137: model = new RegionModel Chris@137: (sampleRate, resolution, minimum, maximum, notifyOnAdd); Chris@137: } else { Chris@137: model = new RegionModel Chris@137: (sampleRate, resolution, notifyOnAdd); Chris@137: } Chris@137: model->setValueQuantization(valueQuantization); Chris@137: model->setScaleUnits(units); Chris@137: model->setObjectName(name); Chris@137: m_models[id] = model; Chris@343: } else if (attributes.value("subtype") == "flexinote") { Chris@646: NoteModel *model; Chris@343: if (haveMinMax) { Chris@646: model = new NoteModel Chris@646: (sampleRate, resolution, minimum, maximum, Chris@646: notifyOnAdd, Chris@646: NoteModel::FLEXI_NOTE); Chris@343: } else { Chris@646: model = new NoteModel Chris@646: (sampleRate, resolution, notifyOnAdd, Chris@646: NoteModel::FLEXI_NOTE); Chris@343: } Chris@343: model->setValueQuantization(valueQuantization); Chris@343: model->setScaleUnits(units); Chris@343: model->setObjectName(name); Chris@343: m_models[id] = model; Chris@45: } else { Chris@137: // note models written out by SV 1.3 and earlier Chris@137: // have no subtype, so we can't test that Chris@137: NoteModel *model; Chris@137: if (haveMinMax) { Chris@137: model = new NoteModel Chris@137: (sampleRate, resolution, minimum, maximum, notifyOnAdd); Chris@137: } else { Chris@137: model = new NoteModel Chris@137: (sampleRate, resolution, notifyOnAdd); Chris@137: } Chris@137: model->setValueQuantization(valueQuantization); Chris@137: model->setScaleUnits(units); Chris@137: model->setObjectName(name); Chris@137: m_models[id] = model; Chris@45: } Chris@137: } Chris@45: Chris@595: int dataset = attributes.value("dataset").trimmed().toInt(&ok); Chris@595: if (ok) m_awaitingDatasets[dataset] = id; Chris@45: Chris@595: return true; Chris@45: Chris@595: } else { Chris@45: Chris@601: SVCERR << "WARNING: SV-XML: Unexpected sparse model dimension (" Chris@595: << dimensions << ")" << endl; Chris@595: } Chris@111: Chris@111: } else if (type == "alignment") { Chris@111: Chris@111: READ_MANDATORY(int, reference, toInt); Chris@111: READ_MANDATORY(int, aligned, toInt); Chris@111: READ_MANDATORY(int, path, toInt); Chris@111: Chris@636: Model *refModel = nullptr, *alignedModel = nullptr, *pathModel = nullptr; Chris@111: Chris@111: if (m_models.find(reference) != m_models.end()) { Chris@111: refModel = m_models[reference]; Chris@111: } else { Chris@601: SVCERR << "WARNING: SV-XML: Unknown reference model id " Chris@111: << reference << " in alignment model id " << id Chris@293: << endl; Chris@111: } Chris@111: Chris@111: if (m_models.find(aligned) != m_models.end()) { Chris@111: alignedModel = m_models[aligned]; Chris@111: } else { Chris@601: SVCERR << "WARNING: SV-XML: Unknown aligned model id " Chris@111: << aligned << " in alignment model id " << id Chris@293: << endl; Chris@111: } Chris@111: Chris@111: if (m_models.find(path) != m_models.end()) { Chris@111: pathModel = m_models[path]; Chris@111: } else { Chris@601: SVCERR << "WARNING: SV-XML: Unknown path model id " Chris@111: << path << " in alignment model id " << id Chris@293: << endl; Chris@111: } Chris@111: Chris@111: if (refModel && alignedModel && pathModel) { Chris@111: AlignmentModel *model = new AlignmentModel Chris@636: (refModel, alignedModel, nullptr, nullptr); Chris@111: PathModel *pm = dynamic_cast(pathModel); Chris@111: if (!pm) { Chris@601: SVCERR << "WARNING: SV-XML: Model id " << path Chris@111: << " referenced as path for alignment " << id Chris@293: << " is not a path model" << endl; Chris@111: } else { Chris@111: model->setPath(pm); Chris@111: pm->setCompletion(100); Chris@111: } Chris@111: model->setObjectName(name); Chris@111: m_models[id] = model; Chris@111: alignedModel->setAlignment(model); Chris@111: return true; Chris@111: } Chris@111: Chris@45: } else { Chris@45: Chris@590: SVCERR << "WARNING: SV-XML: Unexpected model type \"" Chris@590: << type << "\" for model id " << id << endl; Chris@45: } Chris@45: Chris@45: return false; Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::readView(const QXmlAttributes &attributes) Chris@45: { Chris@45: QString type = attributes.value("type"); Chris@636: m_currentPane = nullptr; Chris@45: Chris@45: if (type != "pane") { Chris@601: SVCERR << "WARNING: SV-XML: Unexpected view type \"" Chris@595: << type << "\"" << endl; Chris@595: return false; Chris@45: } Chris@45: Chris@45: m_currentPane = m_paneCallback.addPane(); Chris@45: Chris@630: SVDEBUG << "SVFileReader::addPane: pane is " << m_currentPane << endl; Chris@342: Chris@45: if (!m_currentPane) { Chris@601: SVCERR << "WARNING: SV-XML: Internal error: Failed to add pane!" Chris@595: << endl; Chris@595: return false; Chris@45: } Chris@45: Chris@45: bool ok = false; Chris@45: Chris@45: View *view = m_currentPane; Chris@45: Chris@45: // The view properties first Chris@45: Chris@366: READ_MANDATORY(int, centre, toInt); Chris@366: READ_MANDATORY(int, zoom, toInt); Chris@45: READ_MANDATORY(int, followPan, toInt); Chris@45: READ_MANDATORY(int, followZoom, toInt); Chris@45: QString tracking = attributes.value("tracking"); Chris@45: Chris@624: ZoomLevel zoomLevel; Chris@624: int deepZoom = attributes.value("deepZoom").trimmed().toInt(&ok); Chris@624: if (ok && zoom == 1 && deepZoom > 1) { Chris@624: zoomLevel = { ZoomLevel::PixelsPerFrame, deepZoom }; Chris@624: } else { Chris@624: zoomLevel = { ZoomLevel::FramesPerPixel, zoom }; Chris@624: } Chris@624: Chris@45: // Specify the follow modes before we set the actual values Chris@45: view->setFollowGlobalPan(followPan); Chris@45: view->setFollowGlobalZoom(followZoom); Chris@45: view->setPlaybackFollow(tracking == "scroll" ? PlaybackScrollContinuous : Chris@595: tracking == "page" ? PlaybackScrollPageWithCentre : Chris@595: tracking == "daw" ? PlaybackScrollPage Chris@595: : PlaybackIgnore); Chris@45: Chris@45: // Then set these values Chris@45: view->setCentreFrame(centre); Chris@624: view->setZoomLevel(zoomLevel); Chris@45: Chris@45: // And pane properties Chris@45: READ_MANDATORY(int, centreLineVisible, toInt); Chris@45: m_currentPane->setCentreLineVisible(centreLineVisible); Chris@45: Chris@45: int height = attributes.value("height").toInt(&ok); Chris@45: if (ok) { Chris@595: m_currentPane->resize(m_currentPane->width(), height); Chris@45: } Chris@45: Chris@45: return true; Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::readLayer(const QXmlAttributes &attributes) Chris@45: { Chris@45: QString type = attributes.value("type"); Chris@45: Chris@45: int id; Chris@45: bool ok = false; Chris@45: id = attributes.value("id").trimmed().toInt(&ok); Chris@45: Chris@45: if (!ok) { Chris@601: SVCERR << "WARNING: SV-XML: No layer id for layer of type \"" Chris@595: << type Chris@595: << "\"" << endl; Chris@595: return false; Chris@45: } Chris@45: Chris@636: Layer *layer = nullptr; Chris@45: bool isNewLayer = false; Chris@45: Chris@45: // Layers are expected to be defined in layer elements in the data Chris@45: // section, and referred to in layer elements in the view Chris@45: // sections. So if we're in the data section, we expect this Chris@45: // layer not to exist already; if we're in the view section, we Chris@45: // expect it to exist. Chris@45: Chris@45: if (m_inData) { Chris@45: Chris@595: if (m_layers.find(id) != m_layers.end()) { Chris@601: SVCERR << "WARNING: SV-XML: Ignoring duplicate layer id " << id Chris@595: << " in data section" << endl; Chris@595: return false; Chris@595: } Chris@45: Chris@595: layer = m_layers[id] = m_document->createLayer Chris@595: (LayerFactory::getInstance()->getLayerTypeForName(type)); Chris@45: Chris@595: if (layer) { Chris@595: m_layers[id] = layer; Chris@595: isNewLayer = true; Chris@595: } Chris@45: Chris@45: } else { Chris@45: Chris@595: if (!m_currentPane) { Chris@601: SVCERR << "WARNING: SV-XML: No current pane for layer " << id Chris@595: << " in view section" << endl; Chris@595: return false; Chris@595: } Chris@45: Chris@595: if (m_layers.find(id) != m_layers.end()) { Chris@595: Chris@595: layer = m_layers[id]; Chris@595: Chris@595: } else { Chris@601: SVCERR << "WARNING: SV-XML: Layer id " << id Chris@595: << " in view section has not been defined -- defining it here" Chris@595: << endl; Chris@45: Chris@595: layer = m_document->createLayer Chris@595: (LayerFactory::getInstance()->getLayerTypeForName(type)); Chris@45: Chris@595: if (layer) { Chris@595: m_layers[id] = layer; Chris@595: isNewLayer = true; Chris@595: } Chris@595: } Chris@45: } Chris@595: Chris@45: if (!layer) { Chris@601: SVCERR << "WARNING: SV-XML: Failed to add layer of type \"" Chris@595: << type Chris@595: << "\"" << endl; Chris@595: return false; Chris@45: } Chris@45: Chris@45: if (isNewLayer) { Chris@45: Chris@595: QString name = attributes.value("name"); Chris@595: layer->setObjectName(name); Chris@45: Chris@89: QString presentationName = attributes.value("presentationName"); Chris@89: layer->setPresentationName(presentationName); Chris@89: Chris@595: int modelId; Chris@595: bool modelOk = false; Chris@595: modelId = attributes.value("model").trimmed().toInt(&modelOk); Chris@45: Chris@595: if (modelOk) { Chris@595: if (haveModel(modelId)) { Chris@595: Model *model = m_models[modelId]; Chris@595: m_document->setModel(layer, model); Chris@595: } else { Chris@601: SVCERR << "WARNING: SV-XML: Unknown model id " << modelId Chris@595: << " in layer definition" << endl; Chris@451: if (!layer->canExistWithoutModel()) { Chris@451: // Don't add a layer with an unknown model id Chris@451: // unless it explicitly supports this state Chris@451: m_document->deleteLayer(layer); Chris@636: m_layers[id] = layer = nullptr; Chris@451: return false; Chris@451: } Chris@451: } Chris@595: } Chris@45: Chris@447: if (layer) layer->setProperties(attributes); Chris@45: } Chris@45: Chris@447: if (!m_inData && m_currentPane && layer) { Chris@45: Chris@45: QString visible = attributes.value("visible"); Chris@45: bool dormant = (visible == "false"); Chris@45: Chris@45: // We need to do this both before and after adding the layer Chris@45: // to the view -- we need it to be dormant if appropriate Chris@45: // before it's actually added to the view so that any property Chris@45: // box gets the right state when it's added, but the add layer Chris@45: // command sets dormant to false because it assumes it may be Chris@45: // restoring a previously dormant layer, so we need to set it Chris@45: // again afterwards too. Hm Chris@45: layer->setLayerDormant(m_currentPane, dormant); Chris@45: Chris@595: m_document->addLayerToView(m_currentPane, layer); Chris@45: Chris@45: layer->setLayerDormant(m_currentPane, dormant); Chris@45: } Chris@45: Chris@45: m_currentLayer = layer; Chris@636: m_inLayer = (layer != nullptr); Chris@45: Chris@45: return true; Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::readDatasetStart(const QXmlAttributes &attributes) Chris@45: { Chris@45: bool ok = false; Chris@45: Chris@45: READ_MANDATORY(int, id, toInt); Chris@45: READ_MANDATORY(int, dimensions, toInt); Chris@45: Chris@45: if (m_awaitingDatasets.find(id) == m_awaitingDatasets.end()) { Chris@601: SVCERR << "WARNING: SV-XML: Unwanted dataset " << id << endl; Chris@595: return false; Chris@45: } Chris@45: Chris@45: int modelId = m_awaitingDatasets[id]; Chris@45: Chris@636: Model *model = nullptr; Chris@45: if (haveModel(modelId)) { Chris@595: model = m_models[modelId]; Chris@45: } else { Chris@601: SVCERR << "WARNING: SV-XML: Internal error: Unknown model " << modelId Chris@595: << " expecting dataset " << id << endl; Chris@595: return false; Chris@45: } Chris@45: Chris@45: bool good = false; Chris@45: Chris@45: switch (dimensions) { Chris@45: case 1: Chris@595: if (dynamic_cast(model)) good = true; Chris@45: else if (dynamic_cast(model)) good = true; Chris@595: break; Chris@45: Chris@45: case 2: Chris@595: if (dynamic_cast(model)) good = true; Chris@595: else if (dynamic_cast(model)) good = true; Chris@595: else if (dynamic_cast(model)) good = true; Chris@595: break; Chris@45: Chris@45: case 3: Chris@595: if (dynamic_cast(model)) good = true; Chris@595: else if (dynamic_cast(model)) good = true; Chris@595: else if (dynamic_cast(model)) { Chris@595: m_datasetSeparator = attributes.value("separator"); Chris@595: good = true; Chris@595: } Chris@595: break; Chris@45: } Chris@45: Chris@45: if (!good) { Chris@601: SVCERR << "WARNING: SV-XML: Model id " << modelId << " has wrong number of dimensions or inappropriate type for " << dimensions << "-D dataset " << id << endl; Chris@636: m_currentDataset = nullptr; Chris@595: return false; Chris@45: } Chris@45: Chris@45: m_currentDataset = model; Chris@45: return true; Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::addPointToDataset(const QXmlAttributes &attributes) Chris@45: { Chris@45: bool ok = false; Chris@45: Chris@45: READ_MANDATORY(int, frame, toInt); Chris@45: Chris@233: // SVDEBUG << "SVFileReader::addPointToDataset: frame = " << frame << endl; Chris@45: Chris@45: SparseOneDimensionalModel *sodm = dynamic_cast Chris@595: (m_currentDataset); Chris@45: Chris@45: if (sodm) { Chris@601: // SVCERR << "Current dataset is a sparse one dimensional model" << endl; Chris@595: QString label = attributes.value("label"); Chris@595: sodm->addPoint(SparseOneDimensionalModel::Point(frame, label)); Chris@595: return true; Chris@45: } Chris@45: Chris@45: SparseTimeValueModel *stvm = dynamic_cast Chris@595: (m_currentDataset); Chris@45: Chris@45: if (stvm) { Chris@601: // SVCERR << "Current dataset is a sparse time-value model" << endl; Chris@595: float value = 0.0; Chris@595: value = attributes.value("value").trimmed().toFloat(&ok); Chris@595: QString label = attributes.value("label"); Chris@595: stvm->addPoint(SparseTimeValueModel::Point(frame, value, label)); Chris@595: return ok; Chris@45: } Chris@595: Chris@45: NoteModel *nm = dynamic_cast(m_currentDataset); Chris@45: Chris@45: if (nm) { Chris@601: // SVCERR << "Current dataset is a note model" << endl; Chris@595: float value = 0.0; Chris@595: value = attributes.value("value").trimmed().toFloat(&ok); Chris@595: int duration = 0; Chris@595: duration = attributes.value("duration").trimmed().toInt(&ok); Chris@595: QString label = attributes.value("label"); Chris@61: float level = attributes.value("level").trimmed().toFloat(&ok); Chris@61: if (!ok) { // level is optional Chris@61: level = 1.f; Chris@61: ok = true; Chris@61: } Chris@646: nm->add(Event(frame, value, duration, level, label)); Chris@595: return ok; Chris@343: } Chris@343: Chris@137: RegionModel *rm = dynamic_cast(m_currentDataset); Chris@137: Chris@137: if (rm) { Chris@601: // SVCERR << "Current dataset is a region model" << endl; Chris@595: float value = 0.0; Chris@595: value = attributes.value("value").trimmed().toFloat(&ok); Chris@595: int duration = 0; Chris@595: duration = attributes.value("duration").trimmed().toInt(&ok); Chris@595: QString label = attributes.value("label"); Chris@648: rm->add(Event(frame, value, duration, label)); Chris@595: return ok; Chris@137: } Chris@137: Chris@45: TextModel *tm = dynamic_cast(m_currentDataset); Chris@45: Chris@45: if (tm) { Chris@601: // SVCERR << "Current dataset is a text model" << endl; Chris@595: float height = 0.0; Chris@595: height = attributes.value("height").trimmed().toFloat(&ok); Chris@595: QString label = attributes.value("label"); Chris@233: // SVDEBUG << "SVFileReader::addPointToDataset: TextModel: frame = " << frame << ", height = " << height << ", label = " << label << ", ok = " << ok << endl; Chris@595: tm->addPoint(TextModel::Point(frame, height, label)); Chris@595: return ok; Chris@45: } Chris@45: Chris@111: PathModel *pm = dynamic_cast(m_currentDataset); Chris@111: Chris@111: if (pm) { Chris@601: // SVCERR << "Current dataset is a path model" << endl; Chris@111: int mapframe = attributes.value("mapframe").trimmed().toInt(&ok); Chris@233: // SVDEBUG << "SVFileReader::addPointToDataset: PathModel: frame = " << frame << ", mapframe = " << mapframe << ", ok = " << ok << endl; Chris@595: pm->addPoint(PathModel::Point(frame, mapframe)); Chris@595: return ok; Chris@111: } Chris@111: Chris@45: ImageModel *im = dynamic_cast(m_currentDataset); Chris@45: Chris@45: if (im) { Chris@601: // SVCERR << "Current dataset is an image model" << endl; Chris@595: QString image = attributes.value("image"); Chris@595: QString label = attributes.value("label"); Chris@233: // SVDEBUG << "SVFileReader::addPointToDataset: ImageModel: frame = " << frame << ", image = " << image << ", label = " << label << ", ok = " << ok << endl; Chris@595: im->addPoint(ImageModel::Point(frame, image, label)); Chris@595: return ok; Chris@45: } Chris@45: Chris@601: SVCERR << "WARNING: SV-XML: Point element found in non-point dataset" << endl; Chris@45: Chris@45: return false; Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::addBinToDataset(const QXmlAttributes &attributes) Chris@45: { Chris@45: EditableDenseThreeDimensionalModel *dtdm = Chris@45: dynamic_cast Chris@595: (m_currentDataset); Chris@45: Chris@45: if (dtdm) { Chris@45: Chris@595: bool ok = false; Chris@595: int n = attributes.value("number").trimmed().toInt(&ok); Chris@595: if (!ok) { Chris@601: SVCERR << "WARNING: SV-XML: Missing or invalid bin number" Chris@595: << endl; Chris@595: return false; Chris@595: } Chris@45: Chris@595: QString name = attributes.value("name"); Chris@45: Chris@595: dtdm->setBinName(n, name); Chris@595: return true; Chris@45: } Chris@45: Chris@601: SVCERR << "WARNING: SV-XML: Bin definition found in incompatible dataset" << endl; Chris@45: Chris@45: return false; Chris@45: } Chris@45: Chris@45: Chris@45: bool Chris@45: SVFileReader::addRowToDataset(const QXmlAttributes &attributes) Chris@45: { Chris@45: m_inRow = false; Chris@45: Chris@45: bool ok = false; Chris@45: m_rowNumber = attributes.value("n").trimmed().toInt(&ok); Chris@45: if (!ok) { Chris@601: SVCERR << "WARNING: SV-XML: Missing or invalid row number" Chris@595: << endl; Chris@595: return false; Chris@45: } Chris@45: Chris@45: m_inRow = true; Chris@45: Chris@601: // SVCERR << "SV-XML: In row " << m_rowNumber << endl; Chris@45: Chris@45: return true; Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::readRowData(const QString &text) Chris@45: { Chris@45: EditableDenseThreeDimensionalModel *dtdm = Chris@45: dynamic_cast Chris@595: (m_currentDataset); Chris@45: Chris@45: bool warned = false; Chris@45: Chris@45: if (dtdm) { Chris@595: QStringList data = text.split(m_datasetSeparator); Chris@45: Chris@595: DenseThreeDimensionalModel::Column values; Chris@45: Chris@595: for (QStringList::iterator i = data.begin(); i != data.end(); ++i) { Chris@45: Chris@595: if (int(values.size()) == dtdm->getHeight()) { Chris@595: if (!warned) { Chris@601: SVCERR << "WARNING: SV-XML: Too many y-bins in 3-D dataset row " Chris@595: << m_rowNumber << endl; Chris@595: warned = true; Chris@595: } Chris@595: } Chris@45: Chris@595: bool ok; Chris@595: float value = i->toFloat(&ok); Chris@595: if (!ok) { Chris@601: SVCERR << "WARNING: SV-XML: Bad floating-point value " Chris@595: << i->toLocal8Bit().data() Chris@595: << " in row data" << endl; Chris@595: } else { Chris@595: values.push_back(value); Chris@595: } Chris@595: } Chris@45: Chris@595: dtdm->setColumn(m_rowNumber, values); Chris@595: return true; Chris@45: } Chris@45: Chris@601: SVCERR << "WARNING: SV-XML: Row data found in non-row dataset" << endl; Chris@45: Chris@45: return false; Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::readDerivation(const QXmlAttributes &attributes) Chris@45: { Chris@45: int modelId = 0; Chris@45: bool modelOk = false; Chris@45: modelId = attributes.value("model").trimmed().toInt(&modelOk); Chris@45: Chris@45: if (!modelOk) { Chris@601: SVCERR << "WARNING: SV-XML: No model id specified for derivation" << endl; Chris@595: return false; Chris@45: } Chris@45: Chris@45: if (haveModel(modelId)) { Chris@45: m_currentDerivedModel = m_models[modelId]; Chris@45: } else { Chris@45: // we'll regenerate the model when the derivation element ends Chris@636: m_currentDerivedModel = nullptr; Chris@45: } Chris@45: Chris@45: m_currentDerivedModelId = modelId; Chris@45: Chris@45: int sourceId = 0; Chris@45: bool sourceOk = false; Chris@45: sourceId = attributes.value("source").trimmed().toInt(&sourceOk); Chris@45: Chris@45: if (sourceOk && haveModel(sourceId)) { Chris@72: m_currentTransformSource = m_models[sourceId]; Chris@45: } else { Chris@585: SVDEBUG << "NOTE: SV-XML: Can't find a model with id " << sourceId Chris@585: << " for derivation source, falling back to main model" << endl; Chris@72: m_currentTransformSource = m_document->getMainModel(); Chris@45: } Chris@45: Chris@72: m_currentTransform = Transform(); Chris@45: Chris@45: bool ok = false; Chris@45: int channel = attributes.value("channel").trimmed().toInt(&ok); Chris@72: if (ok) m_currentTransformChannel = channel; Chris@72: else m_currentTransformChannel = -1; Chris@45: Chris@72: QString type = attributes.value("type"); Chris@72: Chris@72: if (type == "transform") { Chris@72: m_currentTransformIsNewStyle = true; Chris@72: return true; Chris@72: } else { Chris@72: m_currentTransformIsNewStyle = false; Chris@233: SVDEBUG << "NOTE: SV-XML: Reading old-style derivation element" Chris@229: << endl; Chris@72: } Chris@72: Chris@72: QString transformId = attributes.value("transform"); Chris@72: Chris@72: m_currentTransform.setIdentifier(transformId); Chris@45: Chris@45: int stepSize = attributes.value("stepSize").trimmed().toInt(&ok); Chris@72: if (ok) m_currentTransform.setStepSize(stepSize); Chris@45: Chris@45: int blockSize = attributes.value("blockSize").trimmed().toInt(&ok); Chris@72: if (ok) m_currentTransform.setBlockSize(blockSize); Chris@45: Chris@45: int windowType = attributes.value("windowType").trimmed().toInt(&ok); Chris@72: if (ok) m_currentTransform.setWindowType(WindowType(windowType)); Chris@72: Chris@72: if (!m_currentTransformSource) return true; Chris@45: Chris@45: QString startFrameStr = attributes.value("startFrame"); Chris@45: QString durationStr = attributes.value("duration"); Chris@45: Chris@366: int startFrame = 0; Chris@366: int duration = 0; Chris@45: Chris@45: if (startFrameStr != "") { Chris@45: startFrame = startFrameStr.trimmed().toInt(&ok); Chris@45: if (!ok) startFrame = 0; Chris@45: } Chris@45: if (durationStr != "") { Chris@45: duration = durationStr.trimmed().toInt(&ok); Chris@45: if (!ok) duration = 0; Chris@45: } Chris@45: Chris@72: m_currentTransform.setStartTime Chris@72: (RealTime::frame2RealTime Chris@72: (startFrame, m_currentTransformSource->getSampleRate())); Chris@72: Chris@72: m_currentTransform.setDuration Chris@72: (RealTime::frame2RealTime Chris@72: (duration, m_currentTransformSource->getSampleRate())); Chris@45: Chris@45: return true; Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::readPlayParameters(const QXmlAttributes &attributes) Chris@45: { Chris@636: m_currentPlayParameters = nullptr; Chris@45: Chris@45: int modelId = 0; Chris@45: bool modelOk = false; Chris@45: modelId = attributes.value("model").trimmed().toInt(&modelOk); Chris@45: Chris@45: if (!modelOk) { Chris@601: SVCERR << "WARNING: SV-XML: No model id specified for play parameters" << endl; Chris@595: return false; Chris@45: } Chris@45: Chris@45: if (haveModel(modelId)) { Chris@45: Chris@45: bool ok = false; Chris@45: Chris@45: PlayParameters *parameters = PlayParameterRepository::getInstance()-> Chris@45: getPlayParameters(m_models[modelId]); Chris@45: Chris@45: if (!parameters) { Chris@601: SVCERR << "WARNING: SV-XML: Play parameters for model " Chris@45: << modelId Chris@45: << " not found - has model been added to document?" Chris@293: << endl; Chris@45: return false; Chris@45: } Chris@45: Chris@45: bool muted = (attributes.value("mute").trimmed() == "true"); Chris@45: parameters->setPlayMuted(muted); Chris@45: Chris@45: float pan = attributes.value("pan").toFloat(&ok); Chris@45: if (ok) parameters->setPlayPan(pan); Chris@45: Chris@45: float gain = attributes.value("gain").toFloat(&ok); Chris@45: if (ok) parameters->setPlayGain(gain); Chris@45: Chris@309: QString clipId = attributes.value("clipId"); Chris@309: if (clipId != "") parameters->setPlayClipId(clipId); Chris@45: Chris@45: m_currentPlayParameters = parameters; Chris@45: Chris@601: // SVCERR << "Current play parameters for model: " << m_models[modelId] << ": " << m_currentPlayParameters << endl; Chris@45: Chris@45: } else { Chris@45: Chris@601: SVCERR << "WARNING: SV-XML: Unknown model " << modelId Chris@595: << " for play parameters" << endl; Chris@45: return false; Chris@45: } Chris@45: Chris@45: return true; Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::readPlugin(const QXmlAttributes &attributes) Chris@45: { Chris@308: if (m_currentDerivedModelId >= 0) { Chris@308: return readPluginForTransform(attributes); Chris@308: } else if (m_currentPlayParameters) { Chris@308: return readPluginForPlayback(attributes); Chris@308: } else { Chris@601: SVCERR << "WARNING: SV-XML: Plugin found outside derivation or play parameters" << endl; Chris@45: return false; Chris@45: } Chris@308: } Chris@45: Chris@308: bool Chris@308: SVFileReader::readPluginForTransform(const QXmlAttributes &attributes) Chris@308: { Chris@308: if (m_currentTransformIsNewStyle) { Chris@308: // Not needed, we have the transform element instead Chris@72: return true; Chris@72: } Chris@72: Chris@45: QString configurationXml = " Chris@308: setParametersFromPluginConfigurationXml(m_currentTransform, Chris@308: configurationXml); Chris@308: return true; Chris@308: } Chris@308: Chris@308: bool Chris@308: SVFileReader::readPluginForPlayback(const QXmlAttributes &attributes) Chris@308: { Chris@308: // Obsolete but supported for compatibility Chris@308: Chris@308: QString ident = attributes.value("identifier"); Chris@308: if (ident == "sample_player") { Chris@309: QString clipId = attributes.value("program"); Chris@309: if (clipId != "") m_currentPlayParameters->setPlayClipId(clipId); Chris@45: } Chris@45: Chris@45: return true; Chris@45: } Chris@45: Chris@45: bool Chris@72: SVFileReader::readTransform(const QXmlAttributes &attributes) Chris@72: { Chris@72: if (m_currentDerivedModelId < 0) { Chris@601: SVCERR << "WARNING: SV-XML: Transform found outside derivation" << endl; Chris@72: return false; Chris@72: } Chris@72: Chris@82: m_currentTransform = Transform(); Chris@72: m_currentTransform.setFromXmlAttributes(attributes); Chris@72: return true; Chris@72: } Chris@72: Chris@72: bool Chris@72: SVFileReader::readParameter(const QXmlAttributes &attributes) Chris@72: { Chris@72: if (m_currentDerivedModelId < 0) { Chris@601: SVCERR << "WARNING: SV-XML: Parameter found outside derivation" << endl; Chris@72: return false; Chris@72: } Chris@72: Chris@72: QString name = attributes.value("name"); Chris@72: if (name == "") { Chris@601: SVCERR << "WARNING: SV-XML: Ignoring nameless transform parameter" Chris@293: << endl; Chris@72: return false; Chris@72: } Chris@72: Chris@72: float value = attributes.value("value").trimmed().toFloat(); Chris@72: Chris@72: m_currentTransform.setParameter(name, value); Chris@72: return true; Chris@72: } Chris@72: Chris@72: bool Chris@45: SVFileReader::readSelection(const QXmlAttributes &attributes) Chris@45: { Chris@45: bool ok; Chris@45: Chris@45: READ_MANDATORY(int, start, toInt); Chris@45: READ_MANDATORY(int, end, toInt); Chris@45: Chris@45: m_paneCallback.addSelection(start, end); Chris@45: Chris@45: return true; Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::readMeasurement(const QXmlAttributes &attributes) Chris@45: { Chris@233: SVDEBUG << "SVFileReader::readMeasurement: inLayer " Chris@229: << m_inLayer << ", layer " << m_currentLayer << endl; Chris@45: Chris@45: if (!m_inLayer) { Chris@601: SVCERR << "WARNING: SV-XML: Measurement found outside layer" << endl; Chris@45: return false; Chris@45: } Chris@45: Chris@45: m_currentLayer->addMeasurementRect(attributes); Chris@45: return true; Chris@45: } Chris@45: Chris@45: SVFileReaderPaneCallback::~SVFileReaderPaneCallback() Chris@45: { Chris@45: } Chris@45: Chris@140: Chris@140: class SVFileIdentifier : public QXmlDefaultHandler Chris@140: { Chris@140: public: Chris@140: SVFileIdentifier() : Chris@140: m_inSv(false), Chris@140: m_inData(false), Chris@140: m_type(SVFileReader::UnknownFileType) Chris@140: { } Chris@633: ~SVFileIdentifier() override { } Chris@140: Chris@140: void parse(QXmlInputSource &source) { Chris@140: QXmlSimpleReader reader; Chris@140: reader.setContentHandler(this); Chris@140: reader.setErrorHandler(this); Chris@140: reader.parse(source); Chris@140: } Chris@140: Chris@140: SVFileReader::FileType getType() const { return m_type; } Chris@140: Chris@633: bool startElement(const QString &, Chris@595: const QString &, Chris@595: const QString &qName, Chris@633: const QXmlAttributes& atts) override Chris@140: { Chris@140: QString name = qName.toLower(); Chris@140: Chris@140: // SV session files have an sv element containing a data Chris@140: // element containing a model element with mainModel="true". Chris@140: Chris@140: // If the sv element is present but the rest does not satisfy, Chris@140: // then it's (probably) an SV layer file. Chris@140: Chris@140: // Otherwise, it's of unknown type. Chris@140: Chris@140: if (name == "sv") { Chris@140: m_inSv = true; Chris@140: if (m_type == SVFileReader::UnknownFileType) { Chris@140: m_type = SVFileReader::SVLayerFile; Chris@140: } Chris@140: return true; Chris@140: } else if (name == "data") { Chris@140: if (!m_inSv) return true; Chris@140: m_inData = true; Chris@140: } else if (name == "model") { Chris@140: if (!m_inData) return true; Chris@140: if (atts.value("mainModel").trimmed() == "true") { Chris@140: if (m_type == SVFileReader::SVLayerFile) { Chris@140: m_type = SVFileReader::SVSessionFile; Chris@140: return false; // done Chris@140: } Chris@140: } Chris@140: } Chris@140: return true; Chris@140: } Chris@140: Chris@633: bool endElement(const QString &, Chris@595: const QString &, Chris@633: const QString &qName) override Chris@140: { Chris@140: QString name = qName.toLower(); Chris@140: Chris@140: if (name == "sv") { Chris@140: if (m_inSv) { Chris@140: m_inSv = false; Chris@140: return false; // done Chris@140: } Chris@140: } else if (name == "data") { Chris@140: if (m_inData) { Chris@140: m_inData = false; Chris@140: return false; // also done, nothing after the first Chris@140: // data element is of use here Chris@140: } Chris@140: } Chris@140: return true; Chris@140: } Chris@140: Chris@140: private: Chris@140: bool m_inSv; Chris@140: bool m_inData; Chris@140: SVFileReader::FileType m_type; Chris@140: }; Chris@140: Chris@140: Chris@140: SVFileReader::FileType Chris@140: SVFileReader::identifyXmlFile(QString path) Chris@140: { Chris@140: QFile file(path); Chris@140: SVFileIdentifier identifier; Chris@140: QXmlInputSource source(&file); Chris@140: identifier.parse(source); Chris@140: return identifier.getType(); Chris@140: } Chris@140: Chris@140: Chris@140: