Bitcoin Core 22.99.0
P2P Digital Currency
recentrequeststablemodel.cpp
Go to the documentation of this file.
1// Copyright (c) 2011-2020 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 <interfaces/wallet.h>
14#include <key_io.h>
15#include <streams.h>
16#include <util/string.h>
17
18#include <utility>
19
20#include <QLatin1Char>
21#include <QLatin1String>
22
24 QAbstractTableModel(parent), walletModel(parent)
25{
26 // Load entries from wallet
27 for (const std::string& request : parent->wallet().getAddressReceiveRequests()) {
28 addNewRequest(request);
29 }
30
31 /* These columns must match the indices in the ColumnIndex enumeration */
32 columns << tr("Date") << tr("Label") << tr("Message") << getAmountTitle();
33
35}
36
38{
39 /* Intentionally left empty */
40}
41
42int RecentRequestsTableModel::rowCount(const QModelIndex &parent) const
43{
44 if (parent.isValid()) {
45 return 0;
46 }
47 return list.length();
48}
49
50int RecentRequestsTableModel::columnCount(const QModelIndex &parent) const
51{
52 if (parent.isValid()) {
53 return 0;
54 }
55 return columns.length();
56}
57
58QVariant RecentRequestsTableModel::data(const QModelIndex &index, int role) const
59{
60 if(!index.isValid() || index.row() >= list.length())
61 return QVariant();
62
63 if(role == Qt::DisplayRole || role == Qt::EditRole)
64 {
65 const RecentRequestEntry *rec = &list[index.row()];
66 switch(index.column())
67 {
68 case Date:
69 return GUIUtil::dateTimeStr(rec->date);
70 case Label:
71 if(rec->recipient.label.isEmpty() && role == Qt::DisplayRole)
72 {
73 return tr("(no label)");
74 }
75 else
76 {
77 return rec->recipient.label;
78 }
79 case Message:
80 if(rec->recipient.message.isEmpty() && role == Qt::DisplayRole)
81 {
82 return tr("(no message)");
83 }
84 else
85 {
86 return rec->recipient.message;
87 }
88 case Amount:
89 if (rec->recipient.amount == 0 && role == Qt::DisplayRole)
90 return tr("(no amount requested)");
91 else if (role == Qt::EditRole)
93 else
95 }
96 }
97 else if (role == Qt::TextAlignmentRole)
98 {
99 if (index.column() == Amount)
100 return (int)(Qt::AlignRight|Qt::AlignVCenter);
101 }
102 return QVariant();
103}
104
105bool RecentRequestsTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
106{
107 return true;
108}
109
110QVariant RecentRequestsTableModel::headerData(int section, Qt::Orientation orientation, int role) const
111{
112 if(orientation == Qt::Horizontal)
113 {
114 if(role == Qt::DisplayRole && section < columns.size())
115 {
116 return columns[section];
117 }
118 }
119 return QVariant();
120}
121
124{
126 Q_EMIT headerDataChanged(Qt::Horizontal,Amount,Amount);
127}
128
131{
132 if (!walletModel->getOptionsModel()) return {};
133 return tr("Requested") +
134 QLatin1String(" (") +
136 QLatin1Char(')');
137}
138
139QModelIndex RecentRequestsTableModel::index(int row, int column, const QModelIndex &parent) const
140{
141 Q_UNUSED(parent);
142
143 return createIndex(row, column);
144}
145
146bool RecentRequestsTableModel::removeRows(int row, int count, const QModelIndex &parent)
147{
148 Q_UNUSED(parent);
149
150 if(count > 0 && row >= 0 && (row+count) <= list.size())
151 {
152 for (int i = 0; i < count; ++i)
153 {
154 const RecentRequestEntry* rec = &list[row+i];
156 return false;
157 }
158
159 beginRemoveRows(parent, row, row + count - 1);
160 list.erase(list.begin() + row, list.begin() + row + count);
161 endRemoveRows();
162 return true;
163 } else {
164 return false;
165 }
166}
167
168Qt::ItemFlags RecentRequestsTableModel::flags(const QModelIndex &index) const
169{
170 return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
171}
172
173// called when adding a request from the GUI
175{
176 RecentRequestEntry newEntry;
177 newEntry.id = ++nReceiveRequestsMaxId;
178 newEntry.date = QDateTime::currentDateTime();
179 newEntry.recipient = recipient;
180
182 ss << newEntry;
183
184 if (!walletModel->wallet().setAddressReceiveRequest(DecodeDestination(recipient.address.toStdString()), ToString(newEntry.id), ss.str()))
185 return;
186
187 addNewRequest(newEntry);
188}
189
190// called from ctor when loading from wallet
191void RecentRequestsTableModel::addNewRequest(const std::string &recipient)
192{
193 std::vector<uint8_t> data(recipient.begin(), recipient.end());
195
197 ss >> entry;
198
199 if (entry.id == 0) // should not happen
200 return;
201
204
206}
207
208// actually add to table in GUI
210{
211 beginInsertRows(QModelIndex(), 0, 0);
212 list.prepend(recipient);
213 endInsertRows();
214}
215
216void RecentRequestsTableModel::sort(int column, Qt::SortOrder order)
217{
218 std::sort(list.begin(), list.end(), RecentRequestEntryLessThan(column, order));
219 Q_EMIT dataChanged(index(0, 0, QModelIndex()), index(list.size() - 1, NUMBER_OF_COLUMNS - 1, QModelIndex()));
220}
221
223{
225}
226
228{
229 const RecentRequestEntry* pLeft = &left;
230 const RecentRequestEntry* pRight = &right;
231 if (order == Qt::DescendingOrder)
232 std::swap(pLeft, pRight);
233
234 switch(column)
235 {
237 return pLeft->date.toSecsSinceEpoch() < pRight->date.toSecsSinceEpoch();
239 return pLeft->recipient.label < pRight->recipient.label;
241 return pLeft->recipient.message < pRight->recipient.message;
243 return pLeft->recipient.amount < pRight->recipient.amount;
244 default:
245 return pLeft->id < pRight->id;
246 }
247}
static QString format(int unit, const CAmount &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:205
std::string str() const
Definition: streams.h:242
int getDisplayUnit() const
Definition: optionsmodel.h:88
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:52
OptionsModel * getOptionsModel()
interfaces::Wallet & wallet() const
Definition: walletmodel.h:144
virtual bool setAddressReceiveRequest(const CTxDestination &dest, const std::string &id, const std::string &value)=0
Save or remove receive request.
virtual std::vector< std::string > getAddressReceiveRequests()=0
Get receive requests.
static const int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
Definition: clientversion.h:33
CTxDestination DecodeDestination(const std::string &str, std::string &error_msg)
Definition: key_io.cpp:261
QString dateTimeStr(const QDateTime &date)
Definition: guiutil.cpp:78
@ SER_DISK
Definition: serialize.h:139
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:87
static int count
Definition: tests.c:41