svcore  1.9
CSVFileWriter.cpp
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7  This file copyright 2006 Chris Cannam.
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version. See the file
13  COPYING included with this distribution for more information.
14 */
15 
16 #include "CSVFileWriter.h"
17 
18 #include "model/Model.h"
21 #include "model/NoteModel.h"
22 #include "model/TextModel.h"
23 
24 #include "base/TempWriteFile.h"
25 #include "base/Exceptions.h"
26 #include "base/Selection.h"
27 
28 #include <QFile>
29 #include <QTextStream>
30 
31 CSVFileWriter::CSVFileWriter(QString path, Model *model, QString delimiter) :
32  m_path(path),
33  m_model(model),
34  m_error(""),
35  m_delimiter(delimiter)
36 {
37 }
38 
40 {
41 }
42 
43 bool
45 {
46  return m_error == "";
47 }
48 
49 QString
51 {
52  return m_error;
53 }
54 
55 void
57 {
58  try {
59  TempWriteFile temp(m_path);
60 
61  QFile file(temp.getTemporaryFilename());
62  if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
63  m_error = tr("Failed to open file %1 for writing")
64  .arg(temp.getTemporaryFilename());
65  return;
66  }
67 
68  QTextStream out(&file);
70 
71  file.close();
72  temp.moveToTarget();
73 
74  } catch (FileOperationFailed &f) {
75  m_error = f.what();
76  }
77 }
78 
79 void
81 {
82  try {
83  TempWriteFile temp(m_path);
84 
85  QFile file(temp.getTemporaryFilename());
86  if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
87  m_error = tr("Failed to open file %1 for writing")
88  .arg(temp.getTemporaryFilename());
89  return;
90  }
91 
92  QTextStream out(&file);
93 
94  for (MultiSelection::SelectionList::iterator i =
95  selection->getSelections().begin();
96  i != selection->getSelections().end(); ++i) {
97 
98  int f0(i->getStartFrame()), f1(i->getEndFrame());
100  }
101 
102  file.close();
103  temp.moveToTarget();
104 
105  } catch (FileOperationFailed &f) {
106  m_error = f.what();
107  }
108 }
109 
110 
111 
CSVFileWriter(QString path, Model *model, QString delimiter=",")
Model * m_model
Definition: CSVFileWriter.h:41
QString m_path
Definition: CSVFileWriter.h:40
virtual QString getError() const
QString m_delimiter
Definition: CSVFileWriter.h:43
virtual void writeSelection(MultiSelection *selection)
QString m_error
Definition: CSVFileWriter.h:42
virtual void write()
virtual ~CSVFileWriter()
virtual QString toDelimitedDataStringSubset(QString, int, int) const
Definition: Model.h:239
Model is the base class for all data models that represent any sort of data on a time scale based on ...
Definition: Model.h:35
A class that manages the creation of a temporary file with a given prefix and the renaming of that fi...
Definition: TempWriteFile.h:27
virtual const char * what() const
Definition: Exceptions.cpp:87
virtual QString toDelimitedDataString(QString delimiter) const
Definition: Model.h:236
void moveToTarget()
Rename the temporary file to the target filename.
const SelectionList & getSelections() const
Definition: Selection.cpp:111
QString getTemporaryFilename()
Return the name of the temporary file.
virtual bool isOK() const