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