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@109: #include "widgets/FileFinder.h" Chris@109: Chris@45: #include "data/model/WaveFileModel.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@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@45: SVFileReaderPaneCallback &callback, Chris@45: QString location) : Chris@45: m_document(document), Chris@45: m_paneCallback(callback), Chris@45: m_location(location), Chris@45: m_currentPane(0), Chris@45: m_currentDataset(0), Chris@45: m_currentDerivedModel(0), Chris@45: m_currentDerivedModelId(-1), Chris@45: m_currentPlayParameters(0), Chris@72: m_currentTransformSource(0), Chris@45: m_datasetSeparator(" "), Chris@45: m_inRow(false), Chris@45: m_inLayer(false), Chris@45: m_inView(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@45: Chris@45: SVFileReader::~SVFileReader() Chris@45: { Chris@45: if (!m_awaitingDatasets.empty()) { Chris@45: std::cerr << "WARNING: SV-XML: File ended with " Chris@45: << m_awaitingDatasets.size() << " unfilled model dataset(s)" Chris@45: << std::endl; Chris@45: } Chris@45: 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: if (!unaddedModels.empty()) { Chris@45: std::cerr << "WARNING: SV-XML: File contained " Chris@45: << unaddedModels.size() << " unused models" Chris@45: << std::endl; Chris@45: while (!unaddedModels.empty()) { Chris@45: delete *unaddedModels.begin(); Chris@45: unaddedModels.erase(unaddedModels.begin()); Chris@45: } Chris@45: } Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::startElement(const QString &, const QString &, Chris@45: const QString &qName, Chris@45: 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@45: // nothing needed Chris@45: ok = true; Chris@45: Chris@45: } else if (name == "data") { Chris@45: Chris@45: // nothing needed Chris@45: m_inData = true; Chris@45: ok = true; Chris@45: Chris@45: } else if (name == "display") { Chris@45: Chris@45: // nothing needed Chris@45: ok = true; Chris@45: Chris@45: } else if (name == "window") { Chris@45: Chris@45: ok = readWindow(attributes); Chris@45: Chris@45: } else if (name == "model") { Chris@45: Chris@45: ok = readModel(attributes); Chris@45: Chris@45: } else if (name == "dataset") { Chris@45: Chris@45: ok = readDatasetStart(attributes); Chris@45: Chris@45: } else if (name == "bin") { Chris@45: Chris@45: ok = addBinToDataset(attributes); Chris@45: Chris@45: } else if (name == "point") { Chris@45: Chris@45: ok = addPointToDataset(attributes); Chris@45: Chris@45: } else if (name == "row") { Chris@45: Chris@45: 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@45: ok = readLayer(attributes); Chris@45: Chris@45: } else if (name == "view") { Chris@45: Chris@45: m_inView = true; Chris@45: ok = readView(attributes); Chris@45: Chris@45: } else if (name == "derivation") { Chris@45: Chris@45: 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@45: ok = readPlugin(attributes); Chris@45: Chris@45: } else if (name == "selections") { Chris@45: Chris@45: m_inSelections = true; Chris@45: ok = true; Chris@45: Chris@45: } else if (name == "selection") { Chris@45: Chris@45: 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@45: std::cerr << "WARNING: SV-XML: Unexpected element \"" Chris@45: << name.toLocal8Bit().data() << "\"" << std::endl; Chris@45: } Chris@45: Chris@45: if (!ok) { Chris@45: std::cerr << "WARNING: SV-XML: Failed to completely process element \"" Chris@45: << name.toLocal8Bit().data() << "\"" << std::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@45: ok = readRowData(text); Chris@45: if (!ok) { Chris@45: std::cerr << "WARNING: SV-XML: Failed to read row data content for row " << m_rowNumber << std::endl; Chris@45: } Chris@45: } Chris@45: Chris@45: return true; Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::endElement(const QString &, const QString &, Chris@45: const QString &qName) Chris@45: { Chris@45: QString name = qName.toLower(); Chris@45: Chris@45: if (name == "dataset") { Chris@45: Chris@45: if (m_currentDataset) { Chris@45: Chris@45: bool foundInAwaiting = false; Chris@45: Chris@45: for (std::map::iterator i = m_awaitingDatasets.begin(); Chris@45: i != m_awaitingDatasets.end(); ++i) { Chris@45: if (haveModel(i->second) && Chris@45: m_models[i->second] == m_currentDataset) { Chris@45: m_awaitingDatasets.erase(i); Chris@45: foundInAwaiting = true; Chris@45: break; Chris@45: } Chris@45: } Chris@45: Chris@45: if (!foundInAwaiting) { Chris@45: std::cerr << "WARNING: SV-XML: Dataset precedes model, or no model uses dataset" << std::endl; Chris@45: } Chris@45: } Chris@45: Chris@45: m_currentDataset = 0; Chris@45: Chris@45: } else if (name == "data") { Chris@45: Chris@45: addUnaddedModels(); Chris@45: m_inData = false; Chris@45: Chris@45: } else if (name == "derivation") { Chris@45: Chris@45: if (!m_currentDerivedModel) { Chris@45: if (m_currentDerivedModel < 0) { Chris@45: std::cerr << "WARNING: SV-XML: Bad derivation output model id " Chris@45: << m_currentDerivedModelId << std::endl; Chris@45: } else if (haveModel(m_currentDerivedModelId)) { Chris@45: std::cerr << "WARNING: SV-XML: Derivation has existing model " Chris@45: << m_currentDerivedModelId Chris@45: << " as target, not regenerating" << std::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@72: m_document->addDerivedModel 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@45: m_currentDerivedModel = 0; Chris@45: m_currentDerivedModelId = -1; Chris@72: m_currentTransformSource = 0; Chris@72: m_currentTransform = Transform(); Chris@72: m_currentTransformChannel = -1; Chris@45: Chris@45: } else if (name == "row") { Chris@45: m_inRow = false; Chris@45: } else if (name == "layer") { Chris@45: m_inLayer = false; Chris@45: } else if (name == "view") { Chris@45: m_inView = false; Chris@45: } else if (name == "selections") { Chris@45: m_inSelections = false; Chris@45: } else if (name == "playparameters") { Chris@45: m_currentPlayParameters = 0; 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@45: QString("ERROR: SV-XML: %1 at line %2, column %3") Chris@45: .arg(exception.message()) Chris@45: .arg(exception.lineNumber()) Chris@45: .arg(exception.columnNumber()); Chris@45: std::cerr << m_errorString.toLocal8Bit().data() << std::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@45: QString("FATAL ERROR: SV-XML: %1 at line %2, column %3") Chris@45: .arg(exception.message()) Chris@45: .arg(exception.lineNumber()) Chris@45: .arg(exception.columnNumber()); Chris@45: std::cerr << m_errorString.toLocal8Bit().data() << std::endl; Chris@45: return QXmlDefaultHandler::fatalError(exception); Chris@45: } Chris@45: Chris@45: Chris@45: #define READ_MANDATORY(TYPE, NAME, CONVERSION) \ Chris@45: TYPE NAME = attributes.value(#NAME).trimmed().CONVERSION(&ok); \ Chris@45: if (!ok) { \ Chris@45: std::cerr << "WARNING: SV-XML: Missing or invalid mandatory " #TYPE " attribute \"" #NAME "\"" << std::endl; \ Chris@45: return false; \ Chris@45: } Chris@45: Chris@45: bool Chris@45: SVFileReader::readWindow(const QXmlAttributes &attributes) Chris@45: { Chris@45: bool ok = false; Chris@45: Chris@45: READ_MANDATORY(int, width, toInt); Chris@45: READ_MANDATORY(int, height, toInt); Chris@45: Chris@45: m_paneCallback.setWindowSize(width, height); Chris@45: return true; Chris@45: } Chris@45: Chris@45: void Chris@45: SVFileReader::addUnaddedModels() Chris@45: { 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@45: m_document->addImportedModel(*i); Chris@45: m_addedModels.insert(*i); 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@45: std::cerr << "WARNING: SV-XML: Ignoring duplicate model id " << id Chris@45: << std::endl; Chris@45: return false; Chris@45: } Chris@45: Chris@45: QString name = attributes.value("name"); Chris@45: Chris@45: std::cerr << "SVFileReader::readModel: model name \"" << name.toStdString() << "\"" << std::endl; Chris@45: Chris@45: READ_MANDATORY(int, sampleRate, toInt); Chris@45: Chris@45: QString type = attributes.value("type").trimmed(); Chris@45: bool mainModel = (attributes.value("mainModel").trimmed() == "true"); Chris@45: Chris@45: if (type == "wavefile") { Chris@45: Chris@45: WaveFileModel *model = 0; 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@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@45: std::cerr << "SVFileReader::readModel: Failed to retrieve file \"" << path.toStdString() << "\" for wave file model: " << file.getErrorString().toStdString() << std::endl; Chris@45: } else if (!file.isAvailable()) { Chris@45: std::cerr << "SVFileReader::readModel: Failed to retrieve file \"" << path.toStdString() << "\" for wave file model: Source unavailable" << std::endl; Chris@45: } else { Chris@45: Chris@45: file.waitForData(); Chris@116: Chris@116: size_t rate = 0; Chris@116: Chris@116: if (!mainModel && Chris@116: Preferences::getInstance()->getResampleOnLoad()) { Chris@116: WaveFileModel *mm = m_document->getMainModel(); Chris@116: if (mm) rate = mm->getSampleRate(); Chris@116: } Chris@116: Chris@116: model = new WaveFileModel(file, rate); Chris@45: if (!model->isOK()) { Chris@45: delete model; Chris@45: model = 0; Chris@45: } Chris@45: } Chris@45: Chris@45: if (!model) return false; Chris@45: Chris@45: model->setObjectName(name); Chris@45: m_models[id] = model; Chris@45: if (mainModel) { Chris@45: m_document->setMainModel(model); Chris@45: m_addedModels.insert(model); Chris@45: } Chris@45: // Derived models will be added when their derivation Chris@45: // is found. Chris@45: Chris@45: return true; Chris@45: Chris@45: } else if (type == "dense") { Chris@45: Chris@45: READ_MANDATORY(int, dimensions, toInt); Chris@45: Chris@45: // Currently the only dense model we support here is the dense Chris@45: // 3d model. Dense time-value models are always file-backed Chris@45: // waveform data, at this point, and they come in as wavefile Chris@45: // models. Chris@45: Chris@45: if (dimensions == 3) { Chris@45: Chris@45: READ_MANDATORY(int, windowSize, toInt); Chris@45: READ_MANDATORY(int, yBinCount, toInt); Chris@45: Chris@45: EditableDenseThreeDimensionalModel *model = Chris@45: new EditableDenseThreeDimensionalModel Chris@45: (sampleRate, windowSize, yBinCount); Chris@45: Chris@45: float minimum = attributes.value("minimum").trimmed().toFloat(&ok); Chris@45: if (ok) model->setMinimumLevel(minimum); Chris@45: Chris@45: float maximum = attributes.value("maximum").trimmed().toFloat(&ok); Chris@45: if (ok) model->setMaximumLevel(maximum); Chris@45: Chris@45: int dataset = attributes.value("dataset").trimmed().toInt(&ok); Chris@45: if (ok) m_awaitingDatasets[dataset] = id; Chris@45: Chris@45: model->setObjectName(name); Chris@45: m_models[id] = model; Chris@45: return true; Chris@45: Chris@45: } else { Chris@45: Chris@45: std::cerr << "WARNING: SV-XML: Unexpected dense model dimension (" Chris@45: << dimensions << ")" << std::endl; Chris@45: } Chris@45: } else if (type == "sparse") { Chris@45: Chris@45: READ_MANDATORY(int, dimensions, toInt); Chris@45: Chris@45: if (dimensions == 1) { Chris@45: Chris@45: 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@45: int dataset = attributes.value("dataset").trimmed().toInt(&ok); Chris@45: if (ok) m_awaitingDatasets[dataset] = id; Chris@45: Chris@45: return true; Chris@45: Chris@45: } else if (dimensions == 2 || dimensions == 3) { Chris@45: Chris@45: READ_MANDATORY(int, resolution, toInt); Chris@45: Chris@45: bool haveMinMax = true; Chris@45: float minimum = attributes.value("minimum").trimmed().toFloat(&ok); Chris@45: if (!ok) haveMinMax = false; Chris@45: float maximum = attributes.value("maximum").trimmed().toFloat(&ok); Chris@45: if (!ok) haveMinMax = false; Chris@45: Chris@45: float valueQuantization = Chris@45: attributes.value("valueQuantization").trimmed().toFloat(&ok); Chris@45: Chris@45: bool notifyOnAdd = (attributes.value("notifyOnAdd") == "true"); Chris@45: Chris@45: QString units = attributes.value("units"); Chris@45: Chris@45: if (dimensions == 2) { Chris@45: if (attributes.value("subtype") == "text") { Chris@45: TextModel *model = new TextModel Chris@45: (sampleRate, resolution, notifyOnAdd); Chris@45: model->setObjectName(name); Chris@45: 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@45: } else { Chris@45: 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@45: m_models[id] = model; Chris@45: } Chris@45: } 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@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@45: int dataset = attributes.value("dataset").trimmed().toInt(&ok); Chris@45: if (ok) m_awaitingDatasets[dataset] = id; Chris@45: Chris@45: return true; Chris@45: Chris@45: } else { Chris@45: Chris@45: std::cerr << "WARNING: SV-XML: Unexpected sparse model dimension (" Chris@45: << dimensions << ")" << std::endl; Chris@45: } 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@111: Model *refModel = 0, *alignedModel = 0, *pathModel = 0; Chris@111: Chris@111: if (m_models.find(reference) != m_models.end()) { Chris@111: refModel = m_models[reference]; Chris@111: } else { Chris@111: std::cerr << "WARNING: SV-XML: Unknown reference model id " Chris@111: << reference << " in alignment model id " << id Chris@111: << std::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@111: std::cerr << "WARNING: SV-XML: Unknown aligned model id " Chris@111: << aligned << " in alignment model id " << id Chris@111: << std::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@111: std::cerr << "WARNING: SV-XML: Unknown path model id " Chris@111: << path << " in alignment model id " << id Chris@111: << std::endl; Chris@111: } Chris@111: Chris@111: if (refModel && alignedModel && pathModel) { Chris@111: AlignmentModel *model = new AlignmentModel Chris@111: (refModel, alignedModel, 0, 0); Chris@111: PathModel *pm = dynamic_cast(pathModel); Chris@111: if (!pm) { Chris@111: std::cerr << "WARNING: SV-XML: Model id " << path Chris@111: << " referenced as path for alignment " << id Chris@111: << " is not a path model" << std::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@45: std::cerr << "WARNING: SV-XML: Unexpected model type \"" Chris@45: << type.toLocal8Bit().data() << "\" for model id " << id << std::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@45: m_currentPane = 0; Chris@45: Chris@45: if (type != "pane") { Chris@45: std::cerr << "WARNING: SV-XML: Unexpected view type \"" Chris@45: << type.toLocal8Bit().data() << "\"" << std::endl; Chris@45: return false; Chris@45: } Chris@45: Chris@45: m_currentPane = m_paneCallback.addPane(); Chris@45: Chris@45: if (!m_currentPane) { Chris@45: std::cerr << "WARNING: SV-XML: Internal error: Failed to add pane!" Chris@45: << std::endl; Chris@45: 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@45: READ_MANDATORY(size_t, centre, toUInt); Chris@45: READ_MANDATORY(size_t, zoom, toUInt); Chris@45: READ_MANDATORY(int, followPan, toInt); Chris@45: READ_MANDATORY(int, followZoom, toInt); Chris@45: QString tracking = attributes.value("tracking"); Chris@45: 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@45: tracking == "page" ? PlaybackScrollPage Chris@45: : PlaybackIgnore); Chris@45: Chris@45: // Then set these values Chris@45: view->setCentreFrame(centre); Chris@45: view->setZoomLevel(zoom); 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@45: 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@45: std::cerr << "WARNING: SV-XML: No layer id for layer of type \"" Chris@45: << type.toLocal8Bit().data() Chris@45: << "\"" << std::endl; Chris@45: return false; Chris@45: } Chris@45: Chris@45: Layer *layer = 0; 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@45: if (m_layers.find(id) != m_layers.end()) { Chris@45: std::cerr << "WARNING: SV-XML: Ignoring duplicate layer id " << id Chris@45: << " in data section" << std::endl; Chris@45: return false; Chris@45: } Chris@45: Chris@45: layer = m_layers[id] = m_document->createLayer Chris@45: (LayerFactory::getInstance()->getLayerTypeForName(type)); Chris@45: Chris@45: if (layer) { Chris@45: m_layers[id] = layer; Chris@45: isNewLayer = true; Chris@45: } Chris@45: Chris@45: } else { Chris@45: Chris@45: if (!m_currentPane) { Chris@45: std::cerr << "WARNING: SV-XML: No current pane for layer " << id Chris@45: << " in view section" << std::endl; Chris@45: return false; Chris@45: } Chris@45: Chris@45: if (m_layers.find(id) != m_layers.end()) { Chris@45: Chris@45: layer = m_layers[id]; Chris@45: Chris@45: } else { Chris@45: std::cerr << "WARNING: SV-XML: Layer id " << id Chris@45: << " in view section has not been defined -- defining it here" Chris@45: << std::endl; Chris@45: Chris@45: layer = m_document->createLayer Chris@45: (LayerFactory::getInstance()->getLayerTypeForName(type)); Chris@45: Chris@45: if (layer) { Chris@45: m_layers[id] = layer; Chris@45: isNewLayer = true; Chris@45: } Chris@45: } Chris@45: } Chris@45: Chris@45: if (!layer) { Chris@45: std::cerr << "WARNING: SV-XML: Failed to add layer of type \"" Chris@45: << type.toLocal8Bit().data() Chris@45: << "\"" << std::endl; Chris@45: return false; Chris@45: } Chris@45: Chris@45: if (isNewLayer) { Chris@45: Chris@45: QString name = attributes.value("name"); Chris@45: layer->setObjectName(name); Chris@45: Chris@89: QString presentationName = attributes.value("presentationName"); Chris@89: layer->setPresentationName(presentationName); Chris@89: Chris@45: int modelId; Chris@45: bool modelOk = false; Chris@45: modelId = attributes.value("model").trimmed().toInt(&modelOk); Chris@45: Chris@45: if (modelOk) { Chris@45: if (haveModel(modelId)) { Chris@45: Model *model = m_models[modelId]; Chris@45: m_document->setModel(layer, model); Chris@45: } else { Chris@45: std::cerr << "WARNING: SV-XML: Unknown model id " << modelId Chris@45: << " in layer definition" << std::endl; Chris@45: } Chris@45: } Chris@45: Chris@45: layer->setProperties(attributes); Chris@45: } Chris@45: Chris@45: if (!m_inData && m_currentPane) { 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@45: 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@45: m_inLayer = true; 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@45: std::cerr << "WARNING: SV-XML: Unwanted dataset " << id << std::endl; Chris@45: return false; Chris@45: } Chris@45: Chris@45: int modelId = m_awaitingDatasets[id]; Chris@45: Chris@45: Model *model = 0; Chris@45: if (haveModel(modelId)) { Chris@45: model = m_models[modelId]; Chris@45: } else { Chris@45: std::cerr << "WARNING: SV-XML: Internal error: Unknown model " << modelId Chris@45: << " expecting dataset " << id << std::endl; Chris@45: return false; Chris@45: } Chris@45: Chris@45: bool good = false; Chris@45: Chris@45: switch (dimensions) { Chris@45: case 1: Chris@45: if (dynamic_cast(model)) good = true; Chris@45: else if (dynamic_cast(model)) good = true; Chris@45: break; Chris@45: Chris@45: case 2: Chris@45: if (dynamic_cast(model)) good = true; Chris@45: else if (dynamic_cast(model)) good = true; Chris@111: else if (dynamic_cast(model)) good = true; Chris@45: break; Chris@45: Chris@45: case 3: Chris@45: if (dynamic_cast(model)) good = true; Chris@137: else if (dynamic_cast(model)) good = true; Chris@45: else if (dynamic_cast(model)) { Chris@45: m_datasetSeparator = attributes.value("separator"); Chris@45: good = true; Chris@45: } Chris@45: break; Chris@45: } Chris@45: Chris@45: if (!good) { Chris@45: 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: m_currentDataset = 0; Chris@45: 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@45: // std::cerr << "SVFileReader::addPointToDataset: frame = " << frame << std::endl; Chris@45: Chris@45: SparseOneDimensionalModel *sodm = dynamic_cast Chris@45: (m_currentDataset); Chris@45: Chris@45: if (sodm) { Chris@45: // std::cerr << "Current dataset is a sparse one dimensional model" << std::endl; Chris@45: QString label = attributes.value("label"); Chris@45: sodm->addPoint(SparseOneDimensionalModel::Point(frame, label)); Chris@45: return true; Chris@45: } Chris@45: Chris@45: SparseTimeValueModel *stvm = dynamic_cast Chris@45: (m_currentDataset); Chris@45: Chris@45: if (stvm) { Chris@45: // std::cerr << "Current dataset is a sparse time-value model" << std::endl; Chris@45: float value = 0.0; Chris@45: value = attributes.value("value").trimmed().toFloat(&ok); Chris@45: QString label = attributes.value("label"); Chris@45: stvm->addPoint(SparseTimeValueModel::Point(frame, value, label)); Chris@45: return ok; Chris@45: } Chris@45: Chris@45: NoteModel *nm = dynamic_cast(m_currentDataset); Chris@45: Chris@45: if (nm) { Chris@45: // std::cerr << "Current dataset is a note model" << std::endl; Chris@45: float value = 0.0; Chris@45: value = attributes.value("value").trimmed().toFloat(&ok); Chris@45: size_t duration = 0; Chris@45: duration = attributes.value("duration").trimmed().toUInt(&ok); Chris@45: 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@61: nm->addPoint(NoteModel::Point(frame, value, duration, level, label)); Chris@45: return ok; Chris@45: } Chris@45: Chris@137: RegionModel *rm = dynamic_cast(m_currentDataset); Chris@137: Chris@137: if (rm) { Chris@137: // std::cerr << "Current dataset is a note model" << std::endl; Chris@137: float value = 0.0; Chris@137: value = attributes.value("value").trimmed().toFloat(&ok); Chris@137: size_t duration = 0; Chris@137: duration = attributes.value("duration").trimmed().toUInt(&ok); Chris@137: QString label = attributes.value("label"); Chris@137: rm->addPoint(RegionModel::Point(frame, value, duration, label)); Chris@137: return ok; Chris@137: } Chris@137: Chris@45: TextModel *tm = dynamic_cast(m_currentDataset); Chris@45: Chris@45: if (tm) { Chris@45: // std::cerr << "Current dataset is a text model" << std::endl; Chris@45: float height = 0.0; Chris@45: height = attributes.value("height").trimmed().toFloat(&ok); Chris@45: QString label = attributes.value("label"); Chris@45: // std::cerr << "SVFileReader::addPointToDataset: TextModel: frame = " << frame << ", height = " << height << ", label = " << label.toStdString() << ", ok = " << ok << std::endl; Chris@45: tm->addPoint(TextModel::Point(frame, height, label)); Chris@45: return ok; Chris@45: } Chris@45: Chris@111: PathModel *pm = dynamic_cast(m_currentDataset); Chris@111: Chris@111: if (pm) { Chris@111: // std::cerr << "Current dataset is a path model" << std::endl; Chris@111: int mapframe = attributes.value("mapframe").trimmed().toInt(&ok); Chris@111: // std::cerr << "SVFileReader::addPointToDataset: PathModel: frame = " << frame << ", mapframe = " << mapframe << ", ok = " << ok << std::endl; Chris@111: pm->addPoint(PathModel::Point(frame, mapframe)); Chris@111: return ok; Chris@111: } Chris@111: Chris@45: ImageModel *im = dynamic_cast(m_currentDataset); Chris@45: Chris@45: if (im) { Chris@45: // std::cerr << "Current dataset is an image model" << std::endl; Chris@45: QString image = attributes.value("image"); Chris@45: QString label = attributes.value("label"); Chris@45: // std::cerr << "SVFileReader::addPointToDataset: ImageModel: frame = " << frame << ", image = " << image.toStdString() << ", label = " << label.toStdString() << ", ok = " << ok << std::endl; Chris@45: im->addPoint(ImageModel::Point(frame, image, label)); Chris@45: return ok; Chris@45: } Chris@45: Chris@45: std::cerr << "WARNING: SV-XML: Point element found in non-point dataset" << std::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@45: (m_currentDataset); Chris@45: Chris@45: if (dtdm) { Chris@45: Chris@45: bool ok = false; Chris@45: int n = attributes.value("number").trimmed().toInt(&ok); Chris@45: if (!ok) { Chris@45: std::cerr << "WARNING: SV-XML: Missing or invalid bin number" Chris@45: << std::endl; Chris@45: return false; Chris@45: } Chris@45: Chris@45: QString name = attributes.value("name"); Chris@45: Chris@45: dtdm->setBinName(n, name); Chris@45: return true; Chris@45: } Chris@45: Chris@45: std::cerr << "WARNING: SV-XML: Bin definition found in incompatible dataset" << std::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@45: std::cerr << "WARNING: SV-XML: Missing or invalid row number" Chris@45: << std::endl; Chris@45: return false; Chris@45: } Chris@45: Chris@45: m_inRow = true; Chris@45: Chris@45: // std::cerr << "SV-XML: In row " << m_rowNumber << std::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@45: (m_currentDataset); Chris@45: Chris@45: bool warned = false; Chris@45: Chris@45: if (dtdm) { Chris@45: QStringList data = text.split(m_datasetSeparator); Chris@45: Chris@45: DenseThreeDimensionalModel::Column values; Chris@45: Chris@45: for (QStringList::iterator i = data.begin(); i != data.end(); ++i) { Chris@45: Chris@45: if (values.size() == dtdm->getHeight()) { Chris@45: if (!warned) { Chris@45: std::cerr << "WARNING: SV-XML: Too many y-bins in 3-D dataset row " Chris@45: << m_rowNumber << std::endl; Chris@45: warned = true; Chris@45: } Chris@45: } Chris@45: Chris@45: bool ok; Chris@45: float value = i->toFloat(&ok); Chris@45: if (!ok) { Chris@45: std::cerr << "WARNING: SV-XML: Bad floating-point value " Chris@45: << i->toLocal8Bit().data() Chris@45: << " in row data" << std::endl; Chris@45: } else { Chris@45: values.push_back(value); Chris@45: } Chris@45: } Chris@45: Chris@45: dtdm->setColumn(m_rowNumber, values); Chris@45: return true; Chris@45: } Chris@45: Chris@45: std::cerr << "WARNING: SV-XML: Row data found in non-row dataset" << std::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@45: std::cerr << "WARNING: SV-XML: No model id specified for derivation" << std::endl; Chris@45: 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@45: m_currentDerivedModel = 0; 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@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@72: std::cerr << "NOTE: SV-XML: Reading old-style derivation element" Chris@72: << std::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@45: size_t startFrame = 0; Chris@45: size_t 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@45: m_currentPlayParameters = 0; 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@45: std::cerr << "WARNING: SV-XML: No model id specified for play parameters" << std::endl; Chris@45: 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@45: std::cerr << "WARNING: SV-XML: Play parameters for model " Chris@45: << modelId Chris@45: << " not found - has model been added to document?" Chris@45: << std::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@45: QString pluginId = attributes.value("pluginId"); Chris@45: if (pluginId != "") parameters->setPlayPluginId(pluginId); Chris@45: Chris@45: m_currentPlayParameters = parameters; Chris@45: Chris@45: // std::cerr << "Current play parameters for model: " << m_models[modelId] << ": " << m_currentPlayParameters << std::endl; Chris@45: Chris@45: } else { Chris@45: Chris@45: std::cerr << "WARNING: SV-XML: Unknown model " << modelId Chris@45: << " for play parameters" << std::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@45: if (m_currentDerivedModelId < 0 && !m_currentPlayParameters) { Chris@45: std::cerr << "WARNING: SV-XML: Plugin found outside derivation or play parameters" << std::endl; Chris@45: return false; Chris@45: } Chris@45: Chris@72: if (!m_currentPlayParameters && m_currentTransformIsNewStyle) { Chris@72: return true; Chris@72: } Chris@72: Chris@45: QString configurationXml = "setPlayPluginConfiguration(configurationXml); Chris@45: } else { Chris@72: TransformFactory::getInstance()-> Chris@72: setParametersFromPluginConfigurationXml(m_currentTransform, Chris@72: configurationXml); 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@72: std::cerr << "WARNING: SV-XML: Transform found outside derivation" << std::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@72: std::cerr << "WARNING: SV-XML: Parameter found outside derivation" << std::endl; Chris@72: return false; Chris@72: } Chris@72: Chris@72: QString name = attributes.value("name"); Chris@72: if (name == "") { Chris@72: std::cerr << "WARNING: SV-XML: Ignoring nameless transform parameter" Chris@72: << std::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@45: std::cerr << "SVFileReader::readMeasurement: inLayer " Chris@45: << m_inLayer << ", layer " << m_currentLayer << std::endl; Chris@45: Chris@45: if (!m_inLayer) { Chris@45: std::cerr << "WARNING: SV-XML: Measurement found outside layer" << std::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@140: virtual ~SVFileIdentifier() { } 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@140: virtual bool startElement(const QString &, Chris@140: const QString &, Chris@140: const QString &qName, Chris@140: const QXmlAttributes& atts) 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@140: virtual bool endElement(const QString &, Chris@140: const QString &, Chris@140: const QString &qName) 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: