svcore  1.9
CSVFileWriter.cpp
Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Sonic Visualiser
00005     An audio file viewer and annotation editor.
00006     Centre for Digital Music, Queen Mary, University of London.
00007     This file copyright 2006 Chris Cannam.
00008     
00009     This program is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU General Public License as
00011     published by the Free Software Foundation; either version 2 of the
00012     License, or (at your option) any later version.  See the file
00013     COPYING included with this distribution for more information.
00014 */
00015 
00016 #include "CSVFileWriter.h"
00017 
00018 #include "model/Model.h"
00019 #include "model/SparseOneDimensionalModel.h"
00020 #include "model/SparseTimeValueModel.h"
00021 #include "model/NoteModel.h"
00022 #include "model/TextModel.h"
00023 
00024 #include "base/TempWriteFile.h"
00025 #include "base/Exceptions.h"
00026 #include "base/Selection.h"
00027 
00028 #include <QFile>
00029 #include <QTextStream>
00030 
00031 CSVFileWriter::CSVFileWriter(QString path, Model *model, QString delimiter) :
00032     m_path(path),
00033     m_model(model),
00034     m_error(""),
00035     m_delimiter(delimiter)
00036 {
00037 }
00038 
00039 CSVFileWriter::~CSVFileWriter()
00040 {
00041 }
00042 
00043 bool
00044 CSVFileWriter::isOK() const
00045 {
00046     return m_error == "";
00047 }
00048 
00049 QString
00050 CSVFileWriter::getError() const
00051 {
00052     return m_error;
00053 }
00054 
00055 void
00056 CSVFileWriter::write()
00057 {
00058     try {
00059         TempWriteFile temp(m_path);
00060 
00061         QFile file(temp.getTemporaryFilename());
00062         if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
00063             m_error = tr("Failed to open file %1 for writing")
00064                 .arg(temp.getTemporaryFilename());
00065             return;
00066         }
00067     
00068         QTextStream out(&file);
00069         out << m_model->toDelimitedDataString(m_delimiter);
00070 
00071         file.close();
00072         temp.moveToTarget();
00073 
00074     } catch (FileOperationFailed &f) {
00075         m_error = f.what();
00076     }
00077 }
00078 
00079 void
00080 CSVFileWriter::writeSelection(MultiSelection *selection)
00081 {
00082     try {
00083         TempWriteFile temp(m_path);
00084 
00085         QFile file(temp.getTemporaryFilename());
00086         if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
00087             m_error = tr("Failed to open file %1 for writing")
00088                 .arg(temp.getTemporaryFilename());
00089             return;
00090         }
00091     
00092         QTextStream out(&file);
00093 
00094         for (MultiSelection::SelectionList::iterator i =
00095                  selection->getSelections().begin();
00096              i != selection->getSelections().end(); ++i) {
00097         
00098             int f0(i->getStartFrame()), f1(i->getEndFrame());
00099             out << m_model->toDelimitedDataStringSubset(m_delimiter, f0, f1);
00100         }
00101 
00102         file.close();
00103         temp.moveToTarget();
00104 
00105     } catch (FileOperationFailed &f) {
00106         m_error = f.what();
00107     }
00108 }
00109 
00110 
00111