Skip to content

Commit fbaee7a

Browse files
committed
Export functionality for transaction list
1 parent d52a0f3 commit fbaee7a

13 files changed

+230
-13
lines changed

bitcoin-qt.pro

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ HEADERS += src/qt/bitcoingui.h \
7777
src/qt/transactionview.h \
7878
src/qt/walletmodel.h \
7979
src/bitcoinrpc.h \
80-
src/qt/overviewpage.h
80+
src/qt/overviewpage.h \
81+
src/qt/csvmodelwriter.h
8182
SOURCES += src/qt/bitcoin.cpp src/qt/bitcoingui.cpp \
8283
src/qt/transactiontablemodel.cpp \
8384
src/qt/addresstablemodel.cpp \
@@ -114,7 +115,8 @@ SOURCES += src/qt/bitcoin.cpp src/qt/bitcoingui.cpp \
114115
src/qt/transactionview.cpp \
115116
src/qt/walletmodel.cpp \
116117
src/bitcoinrpc.cpp \
117-
src/qt/overviewpage.cpp
118+
src/qt/overviewpage.cpp \
119+
src/qt/csvmodelwriter.cpp
118120

119121
RESOURCES += \
120122
src/qt/bitcoin.qrc

doc/assets-attribution.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ Designer: http://www.everaldo.com
3434
Icon Pack: Crystal SVG
3535
License: LGPL
3636

37-
Icon: src/qt/res/icons/receive.png, src/qt/res/icons/history.png
37+
Icon: src/qt/res/icons/receive.png, src/qt/res/icons/history.png,
38+
src/qt/res/icons/export.png
3839
Designer: Oxygen team
3940
Icon Pack: Oxygen
4041
License: Creative Common Attribution-ShareAlike 3.0 License or LGPL

src/qt/bitcoin.qrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<file alias="editdelete">res/icons/editdelete.png</file>
2929
<file alias="history">res/icons/history.png</file>
3030
<file alias="overview">res/icons/overview.png</file>
31+
<file alias="export">res/icons/export.png</file>
3132
</qresource>
3233
<qresource prefix="/images">
3334
<file alias="about">res/images/about.png</file>

src/qt/bitcoingui.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,12 @@ BitcoinGUI::BitcoinGUI(QWidget *parent):
7575
toolbar->addAction(receiveCoins);
7676
toolbar->addAction(addressbook);
7777

78-
overviewPage = new OverviewPage();
78+
QToolBar *toolbar2 = addToolBar("Transactions toolbar");
79+
toolbar2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
80+
toolbar2->addAction(exportAction);
7981

82+
// Overview page
83+
overviewPage = new OverviewPage();
8084
QVBoxLayout *vbox = new QVBoxLayout();
8185

8286
transactionView = new TransactionView(this);
@@ -146,8 +150,10 @@ void BitcoinGUI::createActions()
146150
receiveCoins->setToolTip(tr("Show the list of addresses for receiving payments"));
147151
options = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
148152
options->setToolTip(tr("Modify configuration options for bitcoin"));
149-
openBitcoin = new QAction(QIcon(":/icons/bitcoin"), "Open &Bitcoin", this);
153+
openBitcoin = new QAction(QIcon(":/icons/bitcoin"), tr("Open &Bitcoin"), this);
150154
openBitcoin->setToolTip(tr("Show the Bitcoin window"));
155+
exportAction = new QAction(QIcon(":/icons/export"), tr("&Export..."), this);
156+
exportAction->setToolTip(tr("Export data in current view to a file"));
151157

152158
connect(quit, SIGNAL(triggered()), qApp, SLOT(quit()));
153159
connect(sendCoins, SIGNAL(triggered()), this, SLOT(sendCoinsClicked()));
@@ -156,6 +162,7 @@ void BitcoinGUI::createActions()
156162
connect(options, SIGNAL(triggered()), this, SLOT(optionsClicked()));
157163
connect(about, SIGNAL(triggered()), this, SLOT(aboutClicked()));
158164
connect(openBitcoin, SIGNAL(triggered()), this, SLOT(show()));
165+
connect(exportAction, SIGNAL(triggered()), this, SLOT(exportClicked()));
159166
}
160167

161168
void BitcoinGUI::setClientModel(ClientModel *clientModel)
@@ -410,10 +417,20 @@ void BitcoinGUI::gotoOverviewTab()
410417
{
411418
overviewAction->setChecked(true);
412419
centralWidget->setCurrentWidget(overviewPage);
420+
exportAction->setEnabled(false);
413421
}
414422

415423
void BitcoinGUI::gotoHistoryTab()
416424
{
417425
historyAction->setChecked(true);
418426
centralWidget->setCurrentWidget(transactionsPage);
427+
exportAction->setEnabled(true);
428+
}
429+
430+
void BitcoinGUI::exportClicked()
431+
{
432+
// Redirect to the right view, as soon as export for other views
433+
// (such as address book) is implemented.
434+
transactionView->exportClicked();
419435
}
436+

src/qt/bitcoingui.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class BitcoinGUI : public QMainWindow
6363
QAction *receiveCoins;
6464
QAction *options;
6565
QAction *openBitcoin;
66+
QAction *exportAction;
6667

6768
QSystemTrayIcon *trayIcon;
6869
TransactionView *transactionView;
@@ -92,6 +93,7 @@ private slots:
9293
void trayIconActivated(QSystemTrayIcon::ActivationReason reason);
9394
void transactionDetails(const QModelIndex& idx);
9495
void incomingTransaction(const QModelIndex & parent, int start, int end);
96+
void exportClicked();
9597

9698
void gotoOverviewTab();
9799
void gotoHistoryTab();

src/qt/csvmodelwriter.cpp

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#include "csvmodelwriter.h"
2+
3+
#include <QAbstractItemModel>
4+
#include <QFile>
5+
#include <QTextStream>
6+
7+
CSVModelWriter::CSVModelWriter(const QString &filename, QObject *parent) :
8+
QObject(parent),
9+
filename(filename)
10+
{
11+
}
12+
13+
void CSVModelWriter::setModel(const QAbstractItemModel *model)
14+
{
15+
this->model = model;
16+
}
17+
18+
void CSVModelWriter::addColumn(const QString &title, int column, int role)
19+
{
20+
Column col;
21+
col.title = title;
22+
col.column = column;
23+
col.role = role;
24+
25+
columns.append(col);
26+
}
27+
28+
static void writeValue(QTextStream &f, const QString &value)
29+
{
30+
// TODO: quoting if " or \n in string
31+
f << "\"" << value << "\"";
32+
}
33+
34+
static void writeSep(QTextStream &f)
35+
{
36+
f << ",";
37+
}
38+
39+
static void writeNewline(QTextStream &f)
40+
{
41+
f << "\n";
42+
}
43+
44+
bool CSVModelWriter::write()
45+
{
46+
QFile file(filename);
47+
if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
48+
return false;
49+
QTextStream out(&file);
50+
51+
int numRows = model->rowCount();
52+
53+
// Header row
54+
for(int i=0; i<columns.size(); ++i)
55+
{
56+
if(i!=0)
57+
{
58+
writeSep(out);
59+
}
60+
writeValue(out, columns[i].title);
61+
}
62+
writeNewline(out);
63+
64+
// Data rows
65+
for(int j=0; j<numRows; ++j)
66+
{
67+
for(int i=0; i<columns.size(); ++i)
68+
{
69+
if(i!=0)
70+
{
71+
writeSep(out);
72+
}
73+
QVariant data = model->index(j, columns[i].column).data(columns[i].role);
74+
writeValue(out, data.toString());
75+
}
76+
writeNewline(out);
77+
}
78+
79+
file.close();
80+
81+
return file.error() == QFile::NoError;
82+
}
83+

src/qt/csvmodelwriter.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#ifndef CSVMODELWRITER_H
2+
#define CSVMODELWRITER_H
3+
4+
#include <QObject>
5+
#include <QList>
6+
7+
QT_BEGIN_NAMESPACE
8+
class QAbstractItemModel;
9+
QT_END_NAMESPACE
10+
11+
// Export TableModel to CSV file
12+
class CSVModelWriter : public QObject
13+
{
14+
Q_OBJECT
15+
public:
16+
explicit CSVModelWriter(const QString &filename, QObject *parent = 0);
17+
18+
void setModel(const QAbstractItemModel *model);
19+
void addColumn(const QString &title, int column, int role=Qt::EditRole);
20+
21+
// Perform write operation
22+
// Returns true on success, false otherwise
23+
bool write();
24+
25+
private:
26+
QString filename;
27+
const QAbstractItemModel *model;
28+
29+
struct Column
30+
{
31+
QString title;
32+
int column;
33+
int role;
34+
};
35+
QList<Column> columns;
36+
37+
signals:
38+
39+
public slots:
40+
41+
};
42+
43+
#endif // CSVMODELWRITER_H

src/qt/transactionrecord.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,9 @@ bool TransactionRecord::statusUpdateNeeded()
254254
{
255255
return status.cur_num_blocks != nBestHeight;
256256
}
257+
258+
std::string TransactionRecord::getTxID()
259+
{
260+
return hash.ToString() + strprintf("-%03d", idx);
261+
}
262+

src/qt/transactionrecord.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ class TransactionRecord
103103
/* Status: can change with block chain update */
104104
TransactionStatus status;
105105

106+
/* Return the unique identifier for this transaction (part) */
107+
std::string getTxID();
108+
106109
/* Update status from wallet tx.
107110
*/
108111
void updateStatus(const CWalletTx &wtx);

src/qt/transactiontablemodel.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,12 +394,15 @@ QVariant TransactionTableModel::formatTxToAddress(const TransactionRecord *wtx)
394394
return QVariant(description);
395395
}
396396

397-
QVariant TransactionTableModel::formatTxAmount(const TransactionRecord *wtx) const
397+
QVariant TransactionTableModel::formatTxAmount(const TransactionRecord *wtx, bool showUnconfirmed) const
398398
{
399399
QString str = QString::fromStdString(FormatMoney(wtx->credit + wtx->debit));
400-
if(!wtx->status.confirmed || wtx->status.maturity != TransactionStatus::Mature)
400+
if(showUnconfirmed)
401401
{
402-
str = QString("[") + str + QString("]");
402+
if(!wtx->status.confirmed || wtx->status.maturity != TransactionStatus::Mature)
403+
{
404+
str = QString("[") + str + QString("]");
405+
}
403406
}
404407
return QVariant(str);
405408
}
@@ -541,6 +544,18 @@ QVariant TransactionTableModel::data(const QModelIndex &index, int role) const
541544
{
542545
return llabs(rec->credit + rec->debit);
543546
}
547+
else if (role == TxIDRole)
548+
{
549+
return QString::fromStdString(rec->getTxID());
550+
}
551+
else if (role == ConfirmedRole)
552+
{
553+
return rec->status.status == TransactionStatus::HaveConfirmations;
554+
}
555+
else if (role == FormattedAmountRole)
556+
{
557+
return formatTxAmount(rec, false);
558+
}
544559
return QVariant();
545560
}
546561

0 commit comments

Comments
 (0)