annotate data/fileio/CSVFileWriter.cpp @ 1028:abe13fe10ed6

RealTime frame-conversion test
author Chris Cannam
date Wed, 14 Jan 2015 14:58:38 +0000
parents 59e7fe1b1003
children cc27f35aa75c
rev   line source
Chris@148 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@148 2
Chris@148 3 /*
Chris@148 4 Sonic Visualiser
Chris@148 5 An audio file viewer and annotation editor.
Chris@148 6 Centre for Digital Music, Queen Mary, University of London.
Chris@148 7 This file copyright 2006 Chris Cannam.
Chris@148 8
Chris@148 9 This program is free software; you can redistribute it and/or
Chris@148 10 modify it under the terms of the GNU General Public License as
Chris@148 11 published by the Free Software Foundation; either version 2 of the
Chris@148 12 License, or (at your option) any later version. See the file
Chris@148 13 COPYING included with this distribution for more information.
Chris@148 14 */
Chris@148 15
Chris@148 16 #include "CSVFileWriter.h"
Chris@148 17
Chris@150 18 #include "model/Model.h"
Chris@148 19 #include "model/SparseOneDimensionalModel.h"
Chris@148 20 #include "model/SparseTimeValueModel.h"
Chris@148 21 #include "model/NoteModel.h"
Chris@148 22 #include "model/TextModel.h"
Chris@148 23
Chris@675 24 #include "base/TempWriteFile.h"
Chris@675 25 #include "base/Exceptions.h"
Chris@839 26 #include "base/Selection.h"
Chris@675 27
Chris@148 28 #include <QFile>
Chris@148 29 #include <QTextStream>
Chris@148 30
Chris@148 31 CSVFileWriter::CSVFileWriter(QString path, Model *model, QString delimiter) :
Chris@148 32 m_path(path),
Chris@148 33 m_model(model),
Chris@148 34 m_error(""),
Chris@148 35 m_delimiter(delimiter)
Chris@148 36 {
Chris@148 37 }
Chris@148 38
Chris@148 39 CSVFileWriter::~CSVFileWriter()
Chris@148 40 {
Chris@148 41 }
Chris@148 42
Chris@148 43 bool
Chris@148 44 CSVFileWriter::isOK() const
Chris@148 45 {
Chris@148 46 return m_error == "";
Chris@148 47 }
Chris@148 48
Chris@148 49 QString
Chris@148 50 CSVFileWriter::getError() const
Chris@148 51 {
Chris@148 52 return m_error;
Chris@148 53 }
Chris@148 54
Chris@148 55 void
Chris@148 56 CSVFileWriter::write()
Chris@148 57 {
Chris@675 58 try {
Chris@675 59 TempWriteFile temp(m_path);
Chris@675 60
Chris@675 61 QFile file(temp.getTemporaryFilename());
Chris@675 62 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
Chris@675 63 m_error = tr("Failed to open file %1 for writing")
Chris@675 64 .arg(temp.getTemporaryFilename());
Chris@675 65 return;
Chris@675 66 }
Chris@675 67
Chris@675 68 QTextStream out(&file);
Chris@675 69 out << m_model->toDelimitedDataString(m_delimiter);
Chris@675 70
Chris@675 71 file.close();
Chris@675 72 temp.moveToTarget();
Chris@675 73
Chris@675 74 } catch (FileOperationFailed &f) {
Chris@675 75 m_error = f.what();
Chris@148 76 }
Chris@148 77 }
Chris@148 78
Chris@839 79 void
Chris@839 80 CSVFileWriter::writeSelection(MultiSelection *selection)
Chris@839 81 {
Chris@839 82 try {
Chris@839 83 TempWriteFile temp(m_path);
Chris@148 84
Chris@839 85 QFile file(temp.getTemporaryFilename());
Chris@839 86 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
Chris@839 87 m_error = tr("Failed to open file %1 for writing")
Chris@839 88 .arg(temp.getTemporaryFilename());
Chris@839 89 return;
Chris@839 90 }
Chris@839 91
Chris@839 92 QTextStream out(&file);
Chris@839 93
Chris@839 94 for (MultiSelection::SelectionList::iterator i =
Chris@839 95 selection->getSelections().begin();
Chris@839 96 i != selection->getSelections().end(); ++i) {
Chris@839 97
Chris@929 98 int f0(i->getStartFrame()), f1(i->getEndFrame());
Chris@929 99 out << m_model->toDelimitedDataStringSubset(m_delimiter, f0, f1);
Chris@839 100 }
Chris@839 101
Chris@839 102 file.close();
Chris@839 103 temp.moveToTarget();
Chris@839 104
Chris@839 105 } catch (FileOperationFailed &f) {
Chris@839 106 m_error = f.what();
Chris@839 107 }
Chris@839 108 }
Chris@839 109
Chris@839 110
Chris@839 111