Merge #11683: tests: Remove unused mininode functions {ser,deser}_int_vector(......
[bitcoinplatinum.git] / src / qt / recentrequeststablemodel.cpp
blob4d6e401d0d26bbc7bf1f813d140faada86e51177
1 // Copyright (c) 2011-2016 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #include <qt/recentrequeststablemodel.h>
7 #include <qt/bitcoinunits.h>
8 #include <qt/guiutil.h>
9 #include <qt/optionsmodel.h>
11 #include <clientversion.h>
12 #include <streams.h>
15 RecentRequestsTableModel::RecentRequestsTableModel(CWallet *wallet, WalletModel *parent) :
16 QAbstractTableModel(parent), walletModel(parent)
18 Q_UNUSED(wallet);
19 nReceiveRequestsMaxId = 0;
21 // Load entries from wallet
22 std::vector<std::string> vReceiveRequests;
23 parent->loadReceiveRequests(vReceiveRequests);
24 for (const std::string& request : vReceiveRequests)
25 addNewRequest(request);
27 /* These columns must match the indices in the ColumnIndex enumeration */
28 columns << tr("Date") << tr("Label") << tr("Message") << getAmountTitle();
30 connect(walletModel->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
33 RecentRequestsTableModel::~RecentRequestsTableModel()
35 /* Intentionally left empty */
38 int RecentRequestsTableModel::rowCount(const QModelIndex &parent) const
40 Q_UNUSED(parent);
42 return list.length();
45 int RecentRequestsTableModel::columnCount(const QModelIndex &parent) const
47 Q_UNUSED(parent);
49 return columns.length();
52 QVariant RecentRequestsTableModel::data(const QModelIndex &index, int role) const
54 if(!index.isValid() || index.row() >= list.length())
55 return QVariant();
57 if(role == Qt::DisplayRole || role == Qt::EditRole)
59 const RecentRequestEntry *rec = &list[index.row()];
60 switch(index.column())
62 case Date:
63 return GUIUtil::dateTimeStr(rec->date);
64 case Label:
65 if(rec->recipient.label.isEmpty() && role == Qt::DisplayRole)
67 return tr("(no label)");
69 else
71 return rec->recipient.label;
73 case Message:
74 if(rec->recipient.message.isEmpty() && role == Qt::DisplayRole)
76 return tr("(no message)");
78 else
80 return rec->recipient.message;
82 case Amount:
83 if (rec->recipient.amount == 0 && role == Qt::DisplayRole)
84 return tr("(no amount requested)");
85 else if (role == Qt::EditRole)
86 return BitcoinUnits::format(walletModel->getOptionsModel()->getDisplayUnit(), rec->recipient.amount, false, BitcoinUnits::separatorNever);
87 else
88 return BitcoinUnits::format(walletModel->getOptionsModel()->getDisplayUnit(), rec->recipient.amount);
91 else if (role == Qt::TextAlignmentRole)
93 if (index.column() == Amount)
94 return (int)(Qt::AlignRight|Qt::AlignVCenter);
96 return QVariant();
99 bool RecentRequestsTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
101 return true;
104 QVariant RecentRequestsTableModel::headerData(int section, Qt::Orientation orientation, int role) const
106 if(orientation == Qt::Horizontal)
108 if(role == Qt::DisplayRole && section < columns.size())
110 return columns[section];
113 return QVariant();
116 /** Updates the column title to "Amount (DisplayUnit)" and emits headerDataChanged() signal for table headers to react. */
117 void RecentRequestsTableModel::updateAmountColumnTitle()
119 columns[Amount] = getAmountTitle();
120 Q_EMIT headerDataChanged(Qt::Horizontal,Amount,Amount);
123 /** Gets title for amount column including current display unit if optionsModel reference available. */
124 QString RecentRequestsTableModel::getAmountTitle()
126 return (this->walletModel->getOptionsModel() != nullptr) ? tr("Requested") + " ("+BitcoinUnits::name(this->walletModel->getOptionsModel()->getDisplayUnit()) + ")" : "";
129 QModelIndex RecentRequestsTableModel::index(int row, int column, const QModelIndex &parent) const
131 Q_UNUSED(parent);
133 return createIndex(row, column);
136 bool RecentRequestsTableModel::removeRows(int row, int count, const QModelIndex &parent)
138 Q_UNUSED(parent);
140 if(count > 0 && row >= 0 && (row+count) <= list.size())
142 const RecentRequestEntry *rec;
143 for (int i = 0; i < count; ++i)
145 rec = &list[row+i];
146 if (!walletModel->saveReceiveRequest(rec->recipient.address.toStdString(), rec->id, ""))
147 return false;
150 beginRemoveRows(parent, row, row + count - 1);
151 list.erase(list.begin() + row, list.begin() + row + count);
152 endRemoveRows();
153 return true;
154 } else {
155 return false;
159 Qt::ItemFlags RecentRequestsTableModel::flags(const QModelIndex &index) const
161 return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
164 // called when adding a request from the GUI
165 void RecentRequestsTableModel::addNewRequest(const SendCoinsRecipient &recipient)
167 RecentRequestEntry newEntry;
168 newEntry.id = ++nReceiveRequestsMaxId;
169 newEntry.date = QDateTime::currentDateTime();
170 newEntry.recipient = recipient;
172 CDataStream ss(SER_DISK, CLIENT_VERSION);
173 ss << newEntry;
175 if (!walletModel->saveReceiveRequest(recipient.address.toStdString(), newEntry.id, ss.str()))
176 return;
178 addNewRequest(newEntry);
181 // called from ctor when loading from wallet
182 void RecentRequestsTableModel::addNewRequest(const std::string &recipient)
184 std::vector<char> data(recipient.begin(), recipient.end());
185 CDataStream ss(data, SER_DISK, CLIENT_VERSION);
187 RecentRequestEntry entry;
188 ss >> entry;
190 if (entry.id == 0) // should not happen
191 return;
193 if (entry.id > nReceiveRequestsMaxId)
194 nReceiveRequestsMaxId = entry.id;
196 addNewRequest(entry);
199 // actually add to table in GUI
200 void RecentRequestsTableModel::addNewRequest(RecentRequestEntry &recipient)
202 beginInsertRows(QModelIndex(), 0, 0);
203 list.prepend(recipient);
204 endInsertRows();
207 void RecentRequestsTableModel::sort(int column, Qt::SortOrder order)
209 qSort(list.begin(), list.end(), RecentRequestEntryLessThan(column, order));
210 Q_EMIT dataChanged(index(0, 0, QModelIndex()), index(list.size() - 1, NUMBER_OF_COLUMNS - 1, QModelIndex()));
213 void RecentRequestsTableModel::updateDisplayUnit()
215 updateAmountColumnTitle();
218 bool RecentRequestEntryLessThan::operator()(RecentRequestEntry &left, RecentRequestEntry &right) const
220 RecentRequestEntry *pLeft = &left;
221 RecentRequestEntry *pRight = &right;
222 if (order == Qt::DescendingOrder)
223 std::swap(pLeft, pRight);
225 switch(column)
227 case RecentRequestsTableModel::Date:
228 return pLeft->date.toTime_t() < pRight->date.toTime_t();
229 case RecentRequestsTableModel::Label:
230 return pLeft->recipient.label < pRight->recipient.label;
231 case RecentRequestsTableModel::Message:
232 return pLeft->recipient.message < pRight->recipient.message;
233 case RecentRequestsTableModel::Amount:
234 return pLeft->recipient.amount < pRight->recipient.amount;
235 default:
236 return pLeft->id < pRight->id;