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<uint8_t> data(recipient.begin(), recipient.end());
186 
188  ss >> entry;
189 
190  if (entry.id == 0) // should not happen
191  return;
192 
195 
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 }
SendCoinsRecipient::amount
CAmount amount
Definition: sendcoinsrecipient.h:33
RecentRequestsTableModel::list
QList< RecentRequestEntry > list
Definition: recentrequeststablemodel.h:90
WalletModel::getOptionsModel
OptionsModel * getOptionsModel()
Definition: walletmodel.cpp:284
RecentRequestEntry::recipient
SendCoinsRecipient recipient
Definition: recentrequeststablemodel.h:25
BitcoinUnits::SeparatorStyle::NEVER
@ NEVER
RecentRequestEntryLessThan
Definition: recentrequeststablemodel.h:35
count
static int count
Definition: tests.c:35
RecentRequestEntry::date
QDateTime date
Definition: recentrequeststablemodel.h:24
SER_DISK
@ SER_DISK
Definition: serialize.h:167
WalletModel
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:51
RecentRequestsTableModel::Amount
@ Amount
Definition: recentrequeststablemodel.h:62
streams.h
RecentRequestsTableModel::entry
const RecentRequestEntry & entry(int row) const
Definition: recentrequeststablemodel.h:79
walletmodel.h
SendCoinsRecipient::label
QString label
Definition: sendcoinsrecipient.h:32
RecentRequestsTableModel::columnCount
int columnCount(const QModelIndex &parent) const override
Definition: recentrequeststablemodel.cpp:45
RecentRequestsTableModel::rowCount
int rowCount(const QModelIndex &parent) const override
Definition: recentrequeststablemodel.cpp:37
RecentRequestsTableModel::updateAmountColumnTitle
void updateAmountColumnTitle()
Updates the column title to "Amount (DisplayUnit)" and emits headerDataChanged() signal for table hea...
Definition: recentrequeststablemodel.cpp:118
OptionsModel::displayUnitChanged
void displayUnitChanged(int unit)
clientversion.h
RecentRequestsTableModel::walletModel
WalletModel * walletModel
Definition: recentrequeststablemodel.h:88
RecentRequestsTableModel::updateDisplayUnit
void updateDisplayUnit()
Definition: recentrequeststablemodel.cpp:213
RecentRequestsTableModel::columns
QStringList columns
Definition: recentrequeststablemodel.h:89
RecentRequestsTableModel::removeRows
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
Definition: recentrequeststablemodel.cpp:137
SendCoinsRecipient
Definition: sendcoinsrecipient.h:19
WalletModel::saveReceiveRequest
bool saveReceiveRequest(const std::string &sAddress, const int64_t nId, const std::string &sRequest)
Definition: walletmodel.cpp:474
RecentRequestsTableModel::RecentRequestsTableModel
RecentRequestsTableModel(WalletModel *parent)
Definition: recentrequeststablemodel.cpp:17
RecentRequestsTableModel::getAmountTitle
QString getAmountTitle()
Gets title for amount column including current display unit if optionsModel reference available.
Definition: recentrequeststablemodel.cpp:125
recentrequeststablemodel.h
RecentRequestsTableModel::NUMBER_OF_COLUMNS
@ NUMBER_OF_COLUMNS
Definition: recentrequeststablemodel.h:63
RecentRequestsTableModel::setData
bool setData(const QModelIndex &index, const QVariant &value, int role) override
Definition: recentrequeststablemodel.cpp:100
RecentRequestEntryLessThan::order
Qt::SortOrder order
Definition: recentrequeststablemodel.h:44
WalletModel::loadReceiveRequests
void loadReceiveRequests(std::vector< std::string > &vReceiveRequests)
Definition: walletmodel.cpp:469
guiutil.h
RecentRequestsTableModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const override
Definition: recentrequeststablemodel.cpp:159
RecentRequestsTableModel::addNewRequest
void addNewRequest(const SendCoinsRecipient &recipient)
Definition: recentrequeststablemodel.cpp:165
RecentRequestsTableModel::nReceiveRequestsMaxId
int64_t nReceiveRequestsMaxId
Definition: recentrequeststablemodel.h:91
RecentRequestsTableModel::~RecentRequestsTableModel
~RecentRequestsTableModel()
Definition: recentrequeststablemodel.cpp:32
RecentRequestsTableModel::index
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Definition: recentrequeststablemodel.cpp:130
RecentRequestsTableModel::Date
@ Date
Definition: recentrequeststablemodel.h:59
RecentRequestEntry::id
int64_t id
Definition: recentrequeststablemodel.h:23
OptionsModel::getDisplayUnit
int getDisplayUnit() const
Definition: optionsmodel.h:86
SendCoinsRecipient::address
QString address
Definition: sendcoinsrecipient.h:31
RecentRequestsTableModel::Label
@ Label
Definition: recentrequeststablemodel.h:60
CLIENT_VERSION
static const int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
Definition: clientversion.h:33
RecentRequestEntryLessThan::operator()
bool operator()(const RecentRequestEntry &left, const RecentRequestEntry &right) const
Definition: recentrequeststablemodel.cpp:218
bitcoinunits.h
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:204
RecentRequestsTableModel::sort
void sort(int column, Qt::SortOrder order=Qt::AscendingOrder) override
Definition: recentrequeststablemodel.cpp:207
RecentRequestsTableModel::data
QVariant data(const QModelIndex &index, int role) const override
Definition: recentrequeststablemodel.cpp:53
RecentRequestsTableModel::Message
@ Message
Definition: recentrequeststablemodel.h:61
GUIUtil::dateTimeStr
QString dateTimeStr(const QDateTime &date)
Definition: guiutil.cpp:75
BitcoinUnits::shortName
static QString shortName(int unit)
Short name.
Definition: bitcoinunits.cpp:55
optionsmodel.h
RecentRequestsTableModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
Definition: recentrequeststablemodel.cpp:105
CDataStream::str
std::string str() const
Definition: streams.h:242
RecentRequestEntryLessThan::column
int column
Definition: recentrequeststablemodel.h:43
RecentRequestEntry
Definition: recentrequeststablemodel.h:16
BitcoinUnits::format
static QString format(int unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=SeparatorStyle::STANDARD, bool justify=false)
Format as string.
Definition: bitcoinunits.cpp:101
SendCoinsRecipient::message
QString message
Definition: sendcoinsrecipient.h:35