Bitcoin ABC 0.32.4
P2P Digital Currency
recentrequeststablemodel.cpp
Go to the documentation of this file.
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.
4
6
7#include <qt/bitcoinunits.h>
8#include <qt/guiutil.h>
9#include <qt/optionsmodel.h>
10#include <qt/walletmodel.h>
11
12#include <clientversion.h>
13#include <streams.h>
14
15#include <utility>
16
18 : QAbstractTableModel(parent), walletModel(parent) {
19 // Load entries from wallet
20 std::vector<std::string> vReceiveRequests;
21 parent->loadReceiveRequests(vReceiveRequests);
22 for (const std::string &request : vReceiveRequests) {
23 addNewRequest(request);
24 }
25
26 /* These columns must match the indices in the ColumnIndex enumeration */
27 columns << tr("Date") << tr("Label") << tr("Message") << getAmountTitle();
28
31}
32
34
35int RecentRequestsTableModel::rowCount(const QModelIndex &parent) const {
36 Q_UNUSED(parent);
37
38 return list.length();
39}
40
41int RecentRequestsTableModel::columnCount(const QModelIndex &parent) const {
42 Q_UNUSED(parent);
43
44 return columns.length();
45}
46
47QVariant RecentRequestsTableModel::data(const QModelIndex &index,
48 int role) const {
49 if (!index.isValid() || index.row() >= list.length()) {
50 return QVariant();
51 }
52
53 if (role == Qt::DisplayRole || role == Qt::EditRole) {
54 const RecentRequestEntry *rec = &list[index.row()];
55 switch (index.column()) {
56 case Date:
57 return GUIUtil::dateTimeStr(rec->date);
58 case Label:
59 if (rec->recipient.label.isEmpty() && role == Qt::DisplayRole) {
60 return tr("(no label)");
61 } else {
62 return rec->recipient.label;
63 }
64 case Message:
65 if (rec->recipient.message.isEmpty() &&
66 role == Qt::DisplayRole) {
67 return tr("(no message)");
68 } else {
69 return rec->recipient.message;
70 }
71 case Amount:
72 if (rec->recipient.amount == ::Amount::zero() &&
73 role == Qt::DisplayRole) {
74 return tr("(no amount requested)");
75 } else if (role == Qt::EditRole) {
78 rec->recipient.amount, false,
80 } else {
83 rec->recipient.amount);
84 }
85 }
86 } else if (role == Qt::TextAlignmentRole) {
87 if (index.column() == Amount) {
88 return (int)(Qt::AlignRight | Qt::AlignVCenter);
89 }
90 }
91 return QVariant();
92}
93
94bool RecentRequestsTableModel::setData(const QModelIndex &index,
95 const QVariant &value, int role) {
96 return true;
97}
98
100 Qt::Orientation orientation,
101 int role) const {
102 if (orientation == Qt::Horizontal) {
103 if (role == Qt::DisplayRole && section < columns.size()) {
104 return columns[section];
105 }
106 }
107 return QVariant();
108}
109
114 Q_EMIT headerDataChanged(Qt::Horizontal, Amount, Amount);
115}
116
120 return (this->walletModel->getOptionsModel() != nullptr)
121 ? tr("Requested") + " (" +
124 ->getDisplayUnit()) +
125 ")"
126 : "";
127}
128
129QModelIndex RecentRequestsTableModel::index(int row, int column,
130 const QModelIndex &parent) const {
131 Q_UNUSED(parent);
132
133 return createIndex(row, column);
134}
135
137 const QModelIndex &parent) {
138 Q_UNUSED(parent);
139
140 if (count > 0 && row >= 0 && (row + count) <= list.size()) {
141 for (int i = 0; i < count; ++i) {
142 const RecentRequestEntry *rec = &list[row + i];
144 rec->recipient.address.toStdString(), rec->id, "")) {
145 return false;
146 }
147 }
148
149 beginRemoveRows(parent, row, row + count - 1);
150 list.erase(list.begin() + row, list.begin() + row + count);
151 endRemoveRows();
152 return true;
153 } else {
154 return false;
155 }
156}
157
158Qt::ItemFlags RecentRequestsTableModel::flags(const QModelIndex &index) const {
159 return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
160}
161
162// called when adding a request from the GUI
164 const SendCoinsRecipient &recipient) {
165 RecentRequestEntry newEntry;
166 newEntry.id = ++nReceiveRequestsMaxId;
167 newEntry.date = QDateTime::currentDateTime();
168 newEntry.recipient = recipient;
169
170 DataStream ss{};
171 ss << newEntry;
172
173 if (!walletModel->saveReceiveRequest(recipient.address.toStdString(),
174 newEntry.id, ss.str())) {
175 return;
176 }
177
178 addNewRequest(newEntry);
179}
180
181// called from ctor when loading from wallet
182void RecentRequestsTableModel::addNewRequest(const std::string &recipient) {
183 std::vector<uint8_t> data(recipient.begin(), recipient.end());
184 DataStream ss{data};
185
187 ss >> entry;
188
189 // should not happen
190 if (entry.id == 0) {
191 return;
192 }
193
196 }
197
199}
200
201// actually add to table in GUI
203 beginInsertRows(QModelIndex(), 0, 0);
204 list.prepend(recipient);
205 endInsertRows();
206}
207
208void RecentRequestsTableModel::sort(int column, Qt::SortOrder order) {
209 std::sort(list.begin(), list.end(),
210 RecentRequestEntryLessThan(column, order));
211 Q_EMIT dataChanged(
212 index(0, 0, QModelIndex()),
213 index(list.size() - 1, NUMBER_OF_COLUMNS - 1, QModelIndex()));
214}
215
218}
219
221 const RecentRequestEntry &left, const RecentRequestEntry &right) const {
222 const RecentRequestEntry *pLeft = &left;
223 const RecentRequestEntry *pRight = &right;
224 if (order == Qt::DescendingOrder) {
225 std::swap(pLeft, pRight);
226 }
227
228 switch (column) {
230 return pLeft->date.toSecsSinceEpoch() <
231 pRight->date.toSecsSinceEpoch();
233 return pLeft->recipient.label < pRight->recipient.label;
235 return pLeft->recipient.message < pRight->recipient.message;
237 return pLeft->recipient.amount < pRight->recipient.amount;
238 default:
239 return pLeft->id < pRight->id;
240 }
241}
static QString format(int unit, const Amount amount, bool plussign=false, SeparatorStyle separators=SeparatorStyle::STANDARD, bool justify=false)
Format as string.
static QString shortName(int unit)
Short name.
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:173
int getDisplayUnit() const
Definition: optionsmodel.h:97
void displayUnitChanged(int unit)
int64_t id
SendCoinsRecipient recipient
QDateTime date
Qt::SortOrder order
bool operator()(const RecentRequestEntry &left, const RecentRequestEntry &right) const
int column
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
bool setData(const QModelIndex &index, const QVariant &value, int role) override
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
QVariant data(const QModelIndex &index, int role) const override
const RecentRequestEntry & entry(int row) const
void sort(int column, Qt::SortOrder order=Qt::AscendingOrder) override
QList< RecentRequestEntry > list
int rowCount(const QModelIndex &parent) const override
void updateAmountColumnTitle()
Updates the column title to "Amount (DisplayUnit)" and emits headerDataChanged() signal for table hea...
QString getAmountTitle()
Gets title for amount column including current display unit if optionsModel reference available.
Qt::ItemFlags flags(const QModelIndex &index) const override
void addNewRequest(const SendCoinsRecipient &recipient)
RecentRequestsTableModel(WalletModel *parent)
int columnCount(const QModelIndex &parent) const override
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:47
void loadReceiveRequests(std::vector< std::string > &vReceiveRequests)
bool saveReceiveRequest(const std::string &sAddress, const int64_t nId, const std::string &sRequest)
OptionsModel * getOptionsModel()
QString dateTimeStr(const QDateTime &date)
Definition: guiutil.cpp:83
Definition: amount.h:19
static constexpr Amount zero() noexcept
Definition: amount.h:32
static int count
Definition: tests.c:31