1 #include "transactiontablemodel.h"
3 #include "transactionrecord.h"
11 const QString TransactionTableModel::Sent = "s";
12 const QString TransactionTableModel::Received = "r";
13 const QString TransactionTableModel::Other = "o";
15 /* Internal implementation */
16 class TransactionTableImpl
19 QList<TransactionRecord> cachedWallet;
21 /* Update our model of the wallet */
24 QList<int> insertedIndices;
25 QList<int> removedIndices;
29 /* Query wallet from core, and compare with our own
32 CRITICAL_BLOCK(cs_mapWallet)
34 for(std::map<uint256, CWalletTx>::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
36 /* TODO: Make note of new and removed transactions */
39 cachedWallet.append(TransactionRecord::decomposeTransaction(it->second));
42 /* beginInsertRows(QModelIndex(), first, last) */
44 /* beginRemoveRows(QModelIndex(), first, last) */
50 return cachedWallet.size();
53 TransactionRecord *index(int idx)
55 if(idx >= 0 && idx < cachedWallet.size())
57 return &cachedWallet[idx];
64 /* Credit and Debit columns are right-aligned as they contain numbers */
65 static int column_alignments[] = {
66 Qt::AlignLeft|Qt::AlignVCenter,
67 Qt::AlignLeft|Qt::AlignVCenter,
68 Qt::AlignLeft|Qt::AlignVCenter,
69 Qt::AlignRight|Qt::AlignVCenter,
70 Qt::AlignRight|Qt::AlignVCenter,
71 Qt::AlignLeft|Qt::AlignVCenter
74 TransactionTableModel::TransactionTableModel(QObject *parent):
75 QAbstractTableModel(parent),
76 impl(new TransactionTableImpl())
78 columns << tr("Status") << tr("Date") << tr("Description") << tr("Debit") << tr("Credit");
83 TransactionTableModel::~TransactionTableModel()
88 void TransactionTableModel::updateWallet()
95 int TransactionTableModel::rowCount(const QModelIndex &parent) const
101 int TransactionTableModel::columnCount(const QModelIndex &parent) const
104 return columns.length();
107 QVariant TransactionTableModel::formatTxStatus(const TransactionRecord *wtx) const
111 switch(wtx->status.status)
113 case TransactionStatus::OpenUntilBlock:
114 status = tr("Open for %n block(s)","",wtx->status.open_for);
116 case TransactionStatus::OpenUntilDate:
117 status = tr("Open until ") + DateTimeStr(wtx->status.open_for);
119 case TransactionStatus::Offline:
120 status = tr("%1/offline").arg(wtx->status.depth);
122 case TransactionStatus::Unconfirmed:
123 status = tr("%1/unconfirmed").arg(wtx->status.depth);
125 case TransactionStatus::HaveConfirmations:
126 status = tr("%1 confirmations").arg(wtx->status.depth);
130 return QVariant(status);
133 QVariant TransactionTableModel::formatTxDate(const TransactionRecord *wtx) const
137 return QVariant(DateTimeStr(wtx->time));
143 /* Look up address in address book, if found return
144 address[0:12]... (label)
145 otherwise just return address
147 std::string lookupAddress(const std::string &address)
149 std::string description;
150 CRITICAL_BLOCK(cs_mapAddressBook)
152 std::map<std::string, std::string>::iterator mi = mapAddressBook.find(address);
153 if (mi != mapAddressBook.end() && !(*mi).second.empty())
155 std::string label = (*mi).second;
156 description += address.substr(0,12) + "... ";
157 description += "(" + label + ")";
160 description += address;
165 QVariant TransactionTableModel::formatTxDescription(const TransactionRecord *wtx) const
171 case TransactionRecord::RecvFromAddress:
172 description = tr("From: ") + QString::fromStdString(lookupAddress(wtx->address));
174 case TransactionRecord::RecvFromIP:
175 description = tr("From IP: ") + QString::fromStdString(wtx->address);
177 case TransactionRecord::SendToAddress:
178 description = tr("To: ") + QString::fromStdString(lookupAddress(wtx->address));
180 case TransactionRecord::SendToIP:
181 description = tr("To IP: ") + QString::fromStdString(wtx->address);
183 case TransactionRecord::SendToSelf:
184 description = tr("Payment to yourself");
186 case TransactionRecord::Generated:
187 /* TODO: more extensive description */
188 description = tr("Generated");
191 return QVariant(description);
194 QVariant TransactionTableModel::formatTxDebit(const TransactionRecord *wtx) const
198 QString str = QString::fromStdString(FormatMoney(wtx->debit));
199 if(!wtx->status.confirmed)
201 str = QString("[") + str + QString("]");
203 return QVariant(str);
209 QVariant TransactionTableModel::formatTxCredit(const TransactionRecord *wtx) const
213 QString str = QString::fromStdString(FormatMoney(wtx->credit));
214 if(!wtx->status.confirmed)
216 str = QString("[") + str + QString("]");
218 return QVariant(str);
224 QVariant TransactionTableModel::data(const QModelIndex &index, int role) const
228 TransactionRecord *rec = static_cast<TransactionRecord*>(index.internalPointer());
230 if(role == Qt::DisplayRole)
232 switch(index.column())
235 return formatTxStatus(rec);
237 return formatTxDate(rec);
239 return formatTxDescription(rec);
241 return formatTxDebit(rec);
243 return formatTxCredit(rec);
245 } else if (role == Qt::TextAlignmentRole)
247 return column_alignments[index.column()];
248 } else if (role == Qt::ForegroundRole)
250 if(rec->status.confirmed)
252 return QColor(0, 0, 0);
254 return QColor(128, 128, 128);
256 } else if (role == TypeRole)
260 case TransactionRecord::RecvFromAddress:
261 case TransactionRecord::RecvFromIP:
262 case TransactionRecord::Generated:
263 return TransactionTableModel::Received;
264 case TransactionRecord::SendToAddress:
265 case TransactionRecord::SendToIP:
266 case TransactionRecord::SendToSelf:
267 return TransactionTableModel::Sent;
269 return TransactionTableModel::Other;
275 QVariant TransactionTableModel::headerData(int section, Qt::Orientation orientation, int role) const
277 if(role == Qt::DisplayRole)
279 if(orientation == Qt::Horizontal)
281 return columns[section];
283 } else if (role == Qt::TextAlignmentRole)
285 return column_alignments[section];
290 Qt::ItemFlags TransactionTableModel::flags(const QModelIndex &index) const
292 return QAbstractTableModel::flags(index);
296 QModelIndex TransactionTableModel::index ( int row, int column, const QModelIndex & parent ) const
299 TransactionRecord *data = impl->index(row);
302 return createIndex(row, column, impl->index(row));
304 return QModelIndex();