Bitcoin ABC  0.22.13
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  /* Intentionally left empty */
35 }
36 
37 int RecentRequestsTableModel::rowCount(const QModelIndex &parent) const {
38  Q_UNUSED(parent);
39 
40  return list.length();
41 }
42 
43 int RecentRequestsTableModel::columnCount(const QModelIndex &parent) const {
44  Q_UNUSED(parent);
45 
46  return columns.length();
47 }
48 
49 QVariant RecentRequestsTableModel::data(const QModelIndex &index,
50  int role) const {
51  if (!index.isValid() || index.row() >= list.length()) {
52  return QVariant();
53  }
54 
55  if (role == Qt::DisplayRole || role == Qt::EditRole) {
56  const RecentRequestEntry *rec = &list[index.row()];
57  switch (index.column()) {
58  case Date:
59  return GUIUtil::dateTimeStr(rec->date);
60  case Label:
61  if (rec->recipient.label.isEmpty() && role == Qt::DisplayRole) {
62  return tr("(no label)");
63  } else {
64  return rec->recipient.label;
65  }
66  case Message:
67  if (rec->recipient.message.isEmpty() &&
68  role == Qt::DisplayRole) {
69  return tr("(no message)");
70  } else {
71  return rec->recipient.message;
72  }
73  case Amount:
74  if (rec->recipient.amount == ::Amount::zero() &&
75  role == Qt::DisplayRole) {
76  return tr("(no amount requested)");
77  } else if (role == Qt::EditRole) {
78  return BitcoinUnits::format(
80  rec->recipient.amount, false,
82  } else {
83  return BitcoinUnits::format(
85  rec->recipient.amount);
86  }
87  }
88  } else if (role == Qt::TextAlignmentRole) {
89  if (index.column() == Amount) {
90  return (int)(Qt::AlignRight | Qt::AlignVCenter);
91  }
92  }
93  return QVariant();
94 }
95 
96 bool RecentRequestsTableModel::setData(const QModelIndex &index,
97  const QVariant &value, int role) {
98  return true;
99 }
100 
102  Qt::Orientation orientation,
103  int role) const {
104  if (orientation == Qt::Horizontal) {
105  if (role == Qt::DisplayRole && section < columns.size()) {
106  return columns[section];
107  }
108  }
109  return QVariant();
110 }
111 
116  Q_EMIT headerDataChanged(Qt::Horizontal, Amount, Amount);
117 }
118 
122  return (this->walletModel->getOptionsModel() != nullptr)
123  ? tr("Requested") + " (" +
126  ->getDisplayUnit()) +
127  ")"
128  : "";
129 }
130 
131 QModelIndex RecentRequestsTableModel::index(int row, int column,
132  const QModelIndex &parent) const {
133  Q_UNUSED(parent);
134 
135  return createIndex(row, column);
136 }
137 
139  const QModelIndex &parent) {
140  Q_UNUSED(parent);
141 
142  if (count > 0 && row >= 0 && (row + count) <= list.size()) {
143  for (int i = 0; i < count; ++i) {
144  const RecentRequestEntry *rec = &list[row + i];
146  rec->recipient.address.toStdString(), rec->id, "")) {
147  return false;
148  }
149  }
150 
151  beginRemoveRows(parent, row, row + count - 1);
152  list.erase(list.begin() + row, list.begin() + row + count);
153  endRemoveRows();
154  return true;
155  } else {
156  return false;
157  }
158 }
159 
160 Qt::ItemFlags RecentRequestsTableModel::flags(const QModelIndex &index) const {
161  return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
162 }
163 
164 // called when adding a request from the GUI
166  const SendCoinsRecipient &recipient) {
167  RecentRequestEntry newEntry;
168  newEntry.id = ++nReceiveRequestsMaxId;
169  newEntry.date = QDateTime::currentDateTime();
170  newEntry.recipient = recipient;
171 
173  ss << newEntry;
174 
175  if (!walletModel->saveReceiveRequest(recipient.address.toStdString(),
176  newEntry.id, ss.str())) {
177  return;
178  }
179 
180  addNewRequest(newEntry);
181 }
182 
183 // called from ctor when loading from wallet
184 void RecentRequestsTableModel::addNewRequest(const std::string &recipient) {
185  std::vector<char> data(recipient.begin(), recipient.end());
187 
189  ss >> entry;
190 
191  // should not happen
192  if (entry.id == 0) {
193  return;
194  }
195 
196  if (entry.id > nReceiveRequestsMaxId) {
197  nReceiveRequestsMaxId = entry.id;
198  }
199 
200  addNewRequest(entry);
201 }
202 
203 // actually add to table in GUI
205  beginInsertRows(QModelIndex(), 0, 0);
206  list.prepend(recipient);
207  endInsertRows();
208 }
209 
210 void RecentRequestsTableModel::sort(int column, Qt::SortOrder order) {
211  std::sort(list.begin(), list.end(),
212  RecentRequestEntryLessThan(column, order));
213  Q_EMIT dataChanged(
214  index(0, 0, QModelIndex()),
215  index(list.size() - 1, NUMBER_OF_COLUMNS - 1, QModelIndex()));
216 }
217 
220 }
221 
224  const RecentRequestEntry &right) const {
225  const RecentRequestEntry *pLeft = &left;
226  const RecentRequestEntry *pRight = &right;
227  if (order == Qt::DescendingOrder) {
228  std::swap(pLeft, pRight);
229  }
230 
231  switch (column) {
233  return pLeft->date.toTime_t() < pRight->date.toTime_t();
235  return pLeft->recipient.label < pRight->recipient.label;
237  return pLeft->recipient.message < pRight->recipient.message;
239  return pLeft->recipient.amount < pRight->recipient.amount;
240  default:
241  return pLeft->id < pRight->id;
242  }
243 }
void loadReceiveRequests(std::vector< std::string > &vReceiveRequests)
void addNewRequest(const SendCoinsRecipient &recipient)
static constexpr Amount zero()
Definition: amount.h:35
bool setData(const QModelIndex &index, const QVariant &value, int role) override
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Qt::ItemFlags flags(const QModelIndex &index) const override
std::string str() const
Definition: streams.h:270
QString dateTimeStr(const QDateTime &date)
Definition: guiutil.cpp:66
Definition: amount.h:17
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:196
int getDisplayUnit() const
Definition: optionsmodel.h:96
void sort(int column, Qt::SortOrder order=Qt::AscendingOrder) override
QVariant data(const QModelIndex &index, int role) const override
int64_t id
QDateTime date
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
QList< RecentRequestEntry > list
static QString shortName(int unit)
Short name.
SendCoinsRecipient recipient
const RecentRequestEntry & entry(int row) const
void displayUnitChanged(int unit)
static QString format(int unit, const Amount amount, bool plussign=false, SeparatorStyle separators=separatorStandard)
Format as string.
int columnCount(const QModelIndex &parent) const override
int rowCount(const QModelIndex &parent) const override
static constexpr int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
Definition: clientversion.h:44
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:47
void updateAmountColumnTitle()
Updates the column title to "Amount (DisplayUnit)" and emits headerDataChanged() signal for table hea...
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
static int count
Definition: tests.c:35
bool operator()(const RecentRequestEntry &left, const RecentRequestEntry &right) const
bool saveReceiveRequest(const std::string &sAddress, const int64_t nId, const std::string &sRequest)
RecentRequestsTableModel(WalletModel *parent)
QString getAmountTitle()
Gets title for amount column including current display unit if optionsModel reference available...
OptionsModel * getOptionsModel()