1 // Copyright (c) 2011-2016 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #include "csvmodelwriter.h"
7 #include <QAbstractItemModel>
11 CSVModelWriter::CSVModelWriter(const QString
&_filename
, QObject
*parent
) :
13 filename(_filename
), model(0)
17 void CSVModelWriter::setModel(const QAbstractItemModel
*_model
)
22 void CSVModelWriter::addColumn(const QString
&title
, int column
, int role
)
32 static void writeValue(QTextStream
&f
, const QString
&value
)
34 QString escaped
= value
;
35 escaped
.replace('"', "\"\"");
36 f
<< "\"" << escaped
<< "\"";
39 static void writeSep(QTextStream
&f
)
44 static void writeNewline(QTextStream
&f
)
49 bool CSVModelWriter::write()
52 if(!file
.open(QIODevice::WriteOnly
| QIODevice::Text
))
54 QTextStream
out(&file
);
59 numRows
= model
->rowCount();
63 for(int i
=0; i
<columns
.size(); ++i
)
69 writeValue(out
, columns
[i
].title
);
74 for(int j
=0; j
<numRows
; ++j
)
76 for(int i
=0; i
<columns
.size(); ++i
)
82 QVariant data
= model
->index(j
, columns
[i
].column
).data(columns
[i
].role
);
83 writeValue(out
, data
.toString());
90 return file
.error() == QFile::NoError
;