Bitcoin Core  21.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 <streams.h>
14 
15 #include <utility>
16 
18  QAbstractTableModel(parent), walletModel(parent)
19 {
20  // Load entries from wallet
21  std::vector<std::string> vReceiveRequests;
22  parent->loadReceiveRequests(vReceiveRequests);
23  for (const std::string& request : vReceiveRequests)
24  addNewRequest(request);
25 
26  /* These columns must match the indices in the ColumnIndex enumeration */
27  columns << tr("Date") << tr("Label") << tr("Message") << getAmountTitle();
28 
30 }
31 
33 {
34  /* Intentionally left empty */
35 }
36 
37 int RecentRequestsTableModel::rowCount(const QModelIndex &parent) const
38 {
39  if (parent.isValid()) {
40  return 0;
41  }
42  return list.length();
43 }
44 
45 int RecentRequestsTableModel::columnCount(const QModelIndex &parent) const
46 {
47  if (parent.isValid()) {
48  return 0;
49  }
50  return columns.length();
51 }
52 
53 QVariant RecentRequestsTableModel::data(const QModelIndex &index, int role) const
54 {
55  if(!index.isValid() || index.row() >= list.length())
56  return QVariant();
57 
58  if(role == Qt::DisplayRole || role == Qt::EditRole)
59  {
60  const RecentRequestEntry *rec = &list[index.row()];
61  switch(index.column())
62  {
63  case Date:
64  return GUIUtil::dateTimeStr(rec->date);
65  case Label:
66  if(rec->recipient.label.isEmpty() && role == Qt::DisplayRole)
67  {
68  return tr("(no label)");
69  }
70  else
71  {
72  return rec->recipient.label;
73  }
74  case Message:
75  if(rec->recipient.message.isEmpty() && role == Qt::DisplayRole)
76  {
77  return tr("(no message)");
78  }
79  else
80  {
81  return rec->recipient.message;
82  }
83  case Amount:
84  if (rec->recipient.amount == 0 && role == Qt::DisplayRole)
85  return tr("(no amount requested)");
86  else if (role == Qt::EditRole)
88  else
90  }
91  }
92  else if (role == Qt::TextAlignmentRole)
93  {
94  if (index.column() == Amount)
95  return (int)(Qt::AlignRight|Qt::AlignVCenter);
96  }
97  return QVariant();
98 }
99 
100 bool RecentRequestsTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
101 {
102  return true;
103 }
104 
105 QVariant RecentRequestsTableModel::headerData(int section, Qt::Orientation orientation, int role) const
106 {
107  if(orientation == Qt::Horizontal)
108  {
109  if(role == Qt::DisplayRole && section < columns.size())
110  {
111  return columns[section];
112  }
113  }
114  return QVariant();
115 }
116 
119 {
121  Q_EMIT headerDataChanged(Qt::Horizontal,Amount,Amount);
122 }
123 
126 {
127  return (this->walletModel->getOptionsModel() != nullptr) ? tr("Requested") + " ("+BitcoinUnits::shortName(this->walletModel->getOptionsModel()->getDisplayUnit()) + ")" : "";
128 }
129 
130 QModelIndex RecentRequestsTableModel::index(int row, int column, const QModelIndex &parent) const
131 {
132  Q_UNUSED(parent);
133 
134  return createIndex(row, column);
135 }
136 
137 bool RecentRequestsTableModel::removeRows(int row, int count, const QModelIndex &parent)
138 {
139  Q_UNUSED(parent);
140 
141  if(count > 0 && row >= 0 && (row+count) <= list.size())
142  {
143  for (int i = 0; i < count; ++i)
144  {
145  const RecentRequestEntry* rec = &list[row+i];
146  if (!walletModel->saveReceiveRequest(rec->recipient.address.toStdString(), rec->id, ""))
147  return false;
148  }
149 
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;
156  }
157 }
158 
159 Qt::ItemFlags RecentRequestsTableModel::flags(const QModelIndex &index) const
160 {
161  return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
162 }
163 
164 // called when adding a request from the GUI
166 {
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(), newEntry.id, ss.str()))
176  return;
177 
178  addNewRequest(newEntry);
179 }
180 
181 // called from ctor when loading from wallet
182 void RecentRequestsTableModel::addNewRequest(const std::string &recipient)
183 {
184  std::vector<char> data(recipient.begin(), recipient.end());
186 
188  ss >> entry;
189 
190  if (entry.id == 0) // should not happen
191  return;
192 
193  if (entry.id > nReceiveRequestsMaxId)
194  nReceiveRequestsMaxId = entry.id;
195 
196  addNewRequest(entry);
197 }
198 
199 // actually add to table in GUI
201 {
202  beginInsertRows(QModelIndex(), 0, 0);
203  list.prepend(recipient);
204  endInsertRows();
205 }
206 
207 void RecentRequestsTableModel::sort(int column, Qt::SortOrder order)
208 {
209  std::sort(list.begin(), list.end(), RecentRequestEntryLessThan(column, order));
210  Q_EMIT dataChanged(index(0, 0, QModelIndex()), index(list.size() - 1, NUMBER_OF_COLUMNS - 1, QModelIndex()));
211 }
212 
214 {
216 }
217 
219 {
220  const RecentRequestEntry* pLeft = &left;
221  const RecentRequestEntry* pRight = &right;
222  if (order == Qt::DescendingOrder)
223  std::swap(pLeft, pRight);
224 
225  switch(column)
226  {
228  return pLeft->date.toTime_t() < pRight->date.toTime_t();
230  return pLeft->recipient.label < pRight->recipient.label;
232  return pLeft->recipient.message < pRight->recipient.message;
234  return pLeft->recipient.amount < pRight->recipient.amount;
235  default:
236  return pLeft->id < pRight->id;
237  }
238 }
void loadReceiveRequests(std::vector< std::string > &vReceiveRequests)
void addNewRequest(const SendCoinsRecipient &recipient)
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:280
static QString format(int unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=SeparatorStyle::STANDARD, bool justify=false)
Format as string.
QString dateTimeStr(const QDateTime &date)
Definition: guiutil.cpp:69
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:202
int getDisplayUnit() const
Definition: optionsmodel.h:85
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)
int columnCount(const QModelIndex &parent) const override
int rowCount(const QModelIndex &parent) const override
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:51
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...
static const int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
Definition: clientversion.h:38
OptionsModel * getOptionsModel()