]> Git Repo - VerusCoin.git/blame - src/qt/recentrequeststablemodel.cpp
Merge pull request #6020
[VerusCoin.git] / src / qt / recentrequeststablemodel.cpp
CommitLineData
f914f1a7 1// Copyright (c) 2011-2014 The Bitcoin Core developers
78253fcb 2// Distributed under the MIT software license, see the accompanying
666893b1
WL
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include "recentrequeststablemodel.h"
7df07b3f 6
666893b1 7#include "bitcoinunits.h"
71697f97 8#include "clientversion.h"
7df07b3f 9#include "guiutil.h"
666893b1 10#include "optionsmodel.h"
fa736190 11#include "streams.h"
666893b1 12
53efb09e 13#include <boost/foreach.hpp>
14
7df07b3f 15RecentRequestsTableModel::RecentRequestsTableModel(CWallet *wallet, WalletModel *parent) :
666893b1
WL
16 walletModel(parent)
17{
7df07b3f 18 Q_UNUSED(wallet);
8476d5d4
CL
19 nReceiveRequestsMaxId = 0;
20
21 // Load entries from wallet
22 std::vector<std::string> vReceiveRequests;
23 parent->loadReceiveRequests(vReceiveRequests);
24 BOOST_FOREACH(const std::string& request, vReceiveRequests)
25 addNewRequest(request);
7df07b3f 26
666893b1 27 /* These columns must match the indices in the ColumnIndex enumeration */
8969828d 28 columns << tr("Date") << tr("Label") << tr("Message") << getAmountTitle();
29
30 connect(walletModel->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
666893b1
WL
31}
32
33RecentRequestsTableModel::~RecentRequestsTableModel()
34{
35 /* Intentionally left empty */
36}
37
38int RecentRequestsTableModel::rowCount(const QModelIndex &parent) const
39{
40 Q_UNUSED(parent);
7df07b3f 41
666893b1
WL
42 return list.length();
43}
44
45int RecentRequestsTableModel::columnCount(const QModelIndex &parent) const
46{
47 Q_UNUSED(parent);
7df07b3f 48
666893b1
WL
49 return columns.length();
50}
51
52QVariant RecentRequestsTableModel::data(const QModelIndex &index, int role) const
53{
54 if(!index.isValid() || index.row() >= list.length())
55 return QVariant();
56
57 const RecentRequestEntry *rec = &list[index.row()];
58
59 if(role == Qt::DisplayRole || role == Qt::EditRole)
60 {
61 switch(index.column())
62 {
63 case Date:
64 return GUIUtil::dateTimeStr(rec->date);
65 case Label:
66 if(rec->recipient.label.isEmpty() && role == Qt::DisplayRole)
67 {
68 return tr("(no label)");
69 }
70 else
71 {
72 return rec->recipient.label;
73 }
74 case Message:
75 if(rec->recipient.message.isEmpty() && role == Qt::DisplayRole)
76 {
77 return tr("(no message)");
78 }
79 else
80 {
81 return rec->recipient.message;
82 }
83 case Amount:
79fb0557
CL
84 if (rec->recipient.amount == 0 && role == Qt::DisplayRole)
85 return tr("(no amount)");
027dcdc7
CL
86 else if (role == Qt::EditRole)
87 return BitcoinUnits::format(walletModel->getOptionsModel()->getDisplayUnit(), rec->recipient.amount, false, BitcoinUnits::separatorNever);
79fb0557
CL
88 else
89 return BitcoinUnits::format(walletModel->getOptionsModel()->getDisplayUnit(), rec->recipient.amount);
666893b1
WL
90 }
91 }
027dcdc7
CL
92 else if (role == Qt::TextAlignmentRole)
93 {
94 if (index.column() == Amount)
95 return (int)(Qt::AlignRight|Qt::AlignVCenter);
96 }
666893b1
WL
97 return QVariant();
98}
99
100bool RecentRequestsTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
101{
102 return true;
103}
104
105QVariant RecentRequestsTableModel::headerData(int section, Qt::Orientation orientation, int role) const
106{
107 if(orientation == Qt::Horizontal)
108 {
109 if(role == Qt::DisplayRole && section < columns.size())
110 {
111 return columns[section];
112 }
113 }
114 return QVariant();
115}
116
8969828d 117/** Updates the column title to "Amount (DisplayUnit)" and emits headerDataChanged() signal for table headers to react. */
118void RecentRequestsTableModel::updateAmountColumnTitle()
119{
120 columns[Amount] = getAmountTitle();
121 emit headerDataChanged(Qt::Horizontal,Amount,Amount);
122}
123
124/** Gets title for amount column including current display unit if optionsModel reference available. */
125QString RecentRequestsTableModel::getAmountTitle()
126{
127 QString amountTitle = tr("Amount");
128 if (this->walletModel->getOptionsModel() != NULL)
129 {
130 amountTitle += " ("+BitcoinUnits::name(this->walletModel->getOptionsModel()->getDisplayUnit()) + ")";
131 }
132 return amountTitle;
133}
134
666893b1
WL
135QModelIndex RecentRequestsTableModel::index(int row, int column, const QModelIndex &parent) const
136{
7df07b3f
PK
137 Q_UNUSED(parent);
138
139 return createIndex(row, column);
666893b1
WL
140}
141
142bool RecentRequestsTableModel::removeRows(int row, int count, const QModelIndex &parent)
143{
144 Q_UNUSED(parent);
7df07b3f 145
666893b1
WL
146 if(count > 0 && row >= 0 && (row+count) <= list.size())
147 {
8476d5d4
CL
148 const RecentRequestEntry *rec;
149 for (int i = 0; i < count; ++i)
150 {
151 rec = &list[row+i];
152 if (!walletModel->saveReceiveRequest(rec->recipient.address.toStdString(), rec->id, ""))
153 return false;
154 }
155
666893b1
WL
156 beginRemoveRows(parent, row, row + count - 1);
157 list.erase(list.begin() + row, list.begin() + row + count);
158 endRemoveRows();
159 return true;
160 } else {
161 return false;
162 }
163}
164
165Qt::ItemFlags RecentRequestsTableModel::flags(const QModelIndex &index) const
166{
167 return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
168}
169
8476d5d4 170// called when adding a request from the GUI
666893b1
WL
171void RecentRequestsTableModel::addNewRequest(const SendCoinsRecipient &recipient)
172{
173 RecentRequestEntry newEntry;
8476d5d4 174 newEntry.id = ++nReceiveRequestsMaxId;
666893b1
WL
175 newEntry.date = QDateTime::currentDateTime();
176 newEntry.recipient = recipient;
8476d5d4
CL
177
178 CDataStream ss(SER_DISK, CLIENT_VERSION);
179 ss << newEntry;
180
181 if (!walletModel->saveReceiveRequest(recipient.address.toStdString(), newEntry.id, ss.str()))
182 return;
183
184 addNewRequest(newEntry);
185}
186
187// called from ctor when loading from wallet
188void RecentRequestsTableModel::addNewRequest(const std::string &recipient)
189{
190 std::vector<char> data(recipient.begin(), recipient.end());
191 CDataStream ss(data, SER_DISK, CLIENT_VERSION);
192
193 RecentRequestEntry entry;
194 ss >> entry;
195
196 if (entry.id == 0) // should not happen
197 return;
198
199 if (entry.id > nReceiveRequestsMaxId)
200 nReceiveRequestsMaxId = entry.id;
201
202 addNewRequest(entry);
203}
204
205// actually add to table in GUI
206void RecentRequestsTableModel::addNewRequest(RecentRequestEntry &recipient)
207{
666893b1 208 beginInsertRows(QModelIndex(), 0, 0);
8476d5d4 209 list.prepend(recipient);
666893b1
WL
210 endInsertRows();
211}
4d901023
CL
212
213void RecentRequestsTableModel::sort(int column, Qt::SortOrder order)
214{
215 qSort(list.begin(), list.end(), RecentRequestEntryLessThan(column, order));
216 emit dataChanged(index(0, 0, QModelIndex()), index(list.size() - 1, NUMBER_OF_COLUMNS - 1, QModelIndex()));
217}
218
8969828d 219void RecentRequestsTableModel::updateDisplayUnit()
220{
221 updateAmountColumnTitle();
222}
223
4d901023
CL
224bool RecentRequestEntryLessThan::operator()(RecentRequestEntry &left, RecentRequestEntry &right) const
225{
226 RecentRequestEntry *pLeft = &left;
227 RecentRequestEntry *pRight = &right;
228 if (order == Qt::DescendingOrder)
229 std::swap(pLeft, pRight);
230
231 switch(column)
232 {
233 case RecentRequestsTableModel::Date:
234 return pLeft->date.toTime_t() < pRight->date.toTime_t();
235 case RecentRequestsTableModel::Label:
236 return pLeft->recipient.label < pRight->recipient.label;
237 case RecentRequestsTableModel::Message:
238 return pLeft->recipient.message < pRight->recipient.message;
239 case RecentRequestsTableModel::Amount:
240 return pLeft->recipient.amount < pRight->recipient.amount;
241 default:
242 return pLeft->id < pRight->id;
243 }
244}
This page took 0.150421 seconds and 4 git commands to generate.