aboutsummaryrefslogtreecommitdiff
path: root/src/qt/csvmodelwriter.cpp
diff options
context:
space:
mode:
authorWladimir J. van der Laan <laanwj@gmail.com>2011-07-07 14:27:16 +0200
committerWladimir J. van der Laan <laanwj@gmail.com>2011-07-07 14:27:16 +0200
commitfbaee7a8533b23d846ee16837320f709c4e83d47 (patch)
treef867d31077177c79d27a2c475fb19b566c5010bf /src/qt/csvmodelwriter.cpp
parentd52a0f3bca2c8df8360308b062185d803e34f0d9 (diff)
Export functionality for transaction list
Diffstat (limited to 'src/qt/csvmodelwriter.cpp')
-rw-r--r--src/qt/csvmodelwriter.cpp83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/qt/csvmodelwriter.cpp b/src/qt/csvmodelwriter.cpp
new file mode 100644
index 0000000000..62c0b949aa
--- /dev/null
+++ b/src/qt/csvmodelwriter.cpp
@@ -0,0 +1,83 @@
+#include "csvmodelwriter.h"
+
+#include <QAbstractItemModel>
+#include <QFile>
+#include <QTextStream>
+
+CSVModelWriter::CSVModelWriter(const QString &filename, QObject *parent) :
+ QObject(parent),
+ filename(filename)
+{
+}
+
+void CSVModelWriter::setModel(const QAbstractItemModel *model)
+{
+ this->model = model;
+}
+
+void CSVModelWriter::addColumn(const QString &title, int column, int role)
+{
+ Column col;
+ col.title = title;
+ col.column = column;
+ col.role = role;
+
+ columns.append(col);
+}
+
+static void writeValue(QTextStream &f, const QString &value)
+{
+ // TODO: quoting if " or \n in string
+ f << "\"" << value << "\"";
+}
+
+static void writeSep(QTextStream &f)
+{
+ f << ",";
+}
+
+static void writeNewline(QTextStream &f)
+{
+ f << "\n";
+}
+
+bool CSVModelWriter::write()
+{
+ QFile file(filename);
+ if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
+ return false;
+ QTextStream out(&file);
+
+ int numRows = model->rowCount();
+
+ // Header row
+ for(int i=0; i<columns.size(); ++i)
+ {
+ if(i!=0)
+ {
+ writeSep(out);
+ }
+ writeValue(out, columns[i].title);
+ }
+ writeNewline(out);
+
+ // Data rows
+ for(int j=0; j<numRows; ++j)
+ {
+ for(int i=0; i<columns.size(); ++i)
+ {
+ if(i!=0)
+ {
+ writeSep(out);
+ }
+ QVariant data = model->index(j, columns[i].column).data(columns[i].role);
+ writeValue(out, data.toString());
+ }
+ writeNewline(out);
+ }
+
+ file.close();
+
+ return file.error() == QFile::NoError;
+}
+