Bitcoin ABC  0.26.3
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 
198  }
199 
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 
223  const RecentRequestEntry &left, const RecentRequestEntry &right) const {
224  const RecentRequestEntry *pLeft = &left;
225  const RecentRequestEntry *pRight = &right;
226  if (order == Qt::DescendingOrder) {
227  std::swap(pLeft, pRight);
228  }
229 
230  switch (column) {
232  return pLeft->date.toTime_t() < pRight->date.toTime_t();
234  return pLeft->recipient.label < pRight->recipient.label;
236  return pLeft->recipient.message < pRight->recipient.message;
238  return pLeft->recipient.amount < pRight->recipient.amount;
239  default:
240  return pLeft->id < pRight->id;
241  }
242 }
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:197
std::string str() const
Definition: streams.h:271
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()
static constexpr int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
Definition: clientversion.h:38
QString dateTimeStr(const QDateTime &date)
Definition: guiutil.cpp:70
@ SER_DISK
Definition: serialize.h:167
Definition: amount.h:19
static constexpr Amount zero()
Definition: amount.h:42
static int count
Definition: tests.c:31