Chris@148: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@148: Chris@148: /* Chris@148: Sonic Visualiser Chris@148: An audio file viewer and annotation editor. Chris@148: Centre for Digital Music, Queen Mary, University of London. Chris@148: This file copyright 2006 Chris Cannam. Chris@148: Chris@148: This program is free software; you can redistribute it and/or Chris@148: modify it under the terms of the GNU General Public License as Chris@148: published by the Free Software Foundation; either version 2 of the Chris@148: License, or (at your option) any later version. See the file Chris@148: COPYING included with this distribution for more information. Chris@148: */ Chris@148: Chris@148: #include "CSVFileWriter.h" Chris@148: Chris@150: #include "model/Model.h" Chris@148: #include "model/SparseOneDimensionalModel.h" Chris@148: #include "model/SparseTimeValueModel.h" Chris@148: #include "model/NoteModel.h" Chris@148: #include "model/TextModel.h" Chris@148: Chris@675: #include "base/TempWriteFile.h" Chris@675: #include "base/Exceptions.h" Chris@839: #include "base/Selection.h" Chris@675: Chris@148: #include Chris@148: #include Chris@148: Chris@1063: CSVFileWriter::CSVFileWriter(QString path, Chris@1063: Model *model, Chris@1063: QString delimiter, Chris@1063: DataExportOptions options) : Chris@148: m_path(path), Chris@148: m_model(model), Chris@148: m_error(""), Chris@1063: m_delimiter(delimiter), Chris@1063: m_options(options) Chris@148: { Chris@148: } Chris@148: Chris@148: CSVFileWriter::~CSVFileWriter() Chris@148: { Chris@148: } Chris@148: Chris@148: bool Chris@148: CSVFileWriter::isOK() const Chris@148: { Chris@148: return m_error == ""; Chris@148: } Chris@148: Chris@148: QString Chris@148: CSVFileWriter::getError() const Chris@148: { Chris@148: return m_error; Chris@148: } Chris@148: Chris@148: void Chris@148: CSVFileWriter::write() Chris@148: { Chris@675: try { Chris@675: TempWriteFile temp(m_path); Chris@675: Chris@675: QFile file(temp.getTemporaryFilename()); Chris@675: if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { Chris@675: m_error = tr("Failed to open file %1 for writing") Chris@675: .arg(temp.getTemporaryFilename()); Chris@675: return; Chris@675: } Chris@675: Chris@675: QTextStream out(&file); Chris@1063: out << m_model->toDelimitedDataStringWithOptions Chris@1063: (m_delimiter, m_options); Chris@675: Chris@675: file.close(); Chris@675: temp.moveToTarget(); Chris@675: Chris@675: } catch (FileOperationFailed &f) { Chris@675: m_error = f.what(); Chris@148: } Chris@148: } Chris@148: Chris@839: void Chris@839: CSVFileWriter::writeSelection(MultiSelection *selection) Chris@839: { Chris@839: try { Chris@839: TempWriteFile temp(m_path); Chris@148: Chris@839: QFile file(temp.getTemporaryFilename()); Chris@839: if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { Chris@839: m_error = tr("Failed to open file %1 for writing") Chris@839: .arg(temp.getTemporaryFilename()); Chris@839: return; Chris@839: } Chris@839: Chris@839: QTextStream out(&file); Chris@839: Chris@839: for (MultiSelection::SelectionList::iterator i = Chris@839: selection->getSelections().begin(); Chris@839: i != selection->getSelections().end(); ++i) { Chris@839: Chris@1038: sv_frame_t f0(i->getStartFrame()), f1(i->getEndFrame()); Chris@1063: out << m_model->toDelimitedDataStringSubsetWithOptions Chris@1063: (m_delimiter, m_options, f0, f1); Chris@839: } Chris@839: Chris@839: file.close(); Chris@839: temp.moveToTarget(); Chris@839: Chris@839: } catch (FileOperationFailed &f) { Chris@839: m_error = f.what(); Chris@839: } Chris@839: } Chris@839: Chris@839: Chris@839: