[tests] Add -blocknotify functional test
[bitcoinplatinum.git] / src / qt / csvmodelwriter.cpp
blobdf59927782d944383d9e7a06ee374f6698916370
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>
8 #include <QFile>
9 #include <QTextStream>
11 CSVModelWriter::CSVModelWriter(const QString &_filename, QObject *parent) :
12 QObject(parent),
13 filename(_filename), model(0)
17 void CSVModelWriter::setModel(const QAbstractItemModel *_model)
19 this->model = _model;
22 void CSVModelWriter::addColumn(const QString &title, int column, int role)
24 Column col;
25 col.title = title;
26 col.column = column;
27 col.role = role;
29 columns.append(col);
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)
41 f << ",";
44 static void writeNewline(QTextStream &f)
46 f << "\n";
49 bool CSVModelWriter::write()
51 QFile file(filename);
52 if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
53 return false;
54 QTextStream out(&file);
56 int numRows = 0;
57 if(model)
59 numRows = model->rowCount();
62 // Header row
63 for(int i=0; i<columns.size(); ++i)
65 if(i!=0)
67 writeSep(out);
69 writeValue(out, columns[i].title);
71 writeNewline(out);
73 // Data rows
74 for(int j=0; j<numRows; ++j)
76 for(int i=0; i<columns.size(); ++i)
78 if(i!=0)
80 writeSep(out);
82 QVariant data = model->index(j, columns[i].column).data(columns[i].role);
83 writeValue(out, data.toString());
85 writeNewline(out);
88 file.close();
90 return file.error() == QFile::NoError;