Bitcoin ABC  0.24.7
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 }
RecentRequestsTableModel::list
QList< RecentRequestEntry > list
Definition: recentrequeststablemodel.h:94
WalletModel::getOptionsModel
OptionsModel * getOptionsModel()
Definition: walletmodel.cpp:315
RecentRequestEntry::recipient
SendCoinsRecipient recipient
Definition: recentrequeststablemodel.h:25
BitcoinUnits::SeparatorStyle::NEVER
@ NEVER
RecentRequestEntryLessThan
Definition: recentrequeststablemodel.h:35
count
static int count
Definition: tests.c:41
RecentRequestEntry::date
QDateTime date
Definition: recentrequeststablemodel.h:24
WalletModel
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:47
RecentRequestsTableModel::Amount
@ Amount
Definition: recentrequeststablemodel.h:62
SendCoinsRecipient::amount
Amount amount
Definition: sendcoinsrecipient.h:41
streams.h
RecentRequestsTableModel::entry
const RecentRequestEntry & entry(int row) const
Definition: recentrequeststablemodel.h:83
walletmodel.h
SendCoinsRecipient::label
QString label
Definition: sendcoinsrecipient.h:40
RecentRequestsTableModel::columnCount
int columnCount(const QModelIndex &parent) const override
Definition: recentrequeststablemodel.cpp:43
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:114
OptionsModel::displayUnitChanged
void displayUnitChanged(int unit)
clientversion.h
RecentRequestsTableModel::walletModel
WalletModel * walletModel
Definition: recentrequeststablemodel.h:92
RecentRequestsTableModel::updateDisplayUnit
void updateDisplayUnit()
Definition: recentrequeststablemodel.cpp:218
RecentRequestsTableModel::columns
QStringList columns
Definition: recentrequeststablemodel.h:93
BitcoinUnits::format
static QString format(int unit, const Amount amount, bool plussign=false, SeparatorStyle separators=SeparatorStyle::STANDARD, bool justify=false)
Format as string.
Definition: bitcoinunits.cpp:108
Amount::zero
static constexpr Amount zero()
Definition: amount.h:42
RecentRequestsTableModel::removeRows
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
Definition: recentrequeststablemodel.cpp:138
SendCoinsRecipient
Definition: sendcoinsrecipient.h:23
WalletModel::saveReceiveRequest
bool saveReceiveRequest(const std::string &sAddress, const int64_t nId, const std::string &sRequest)
Definition: walletmodel.cpp:500
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:121
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:96
RecentRequestEntryLessThan::order
Qt::SortOrder order
Definition: recentrequeststablemodel.h:44
WalletModel::loadReceiveRequests
void loadReceiveRequests(std::vector< std::string > &vReceiveRequests)
Definition: walletmodel.cpp:494
guiutil.h
RecentRequestsTableModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const override
Definition: recentrequeststablemodel.cpp:160
Amount
Definition: amount.h:19
RecentRequestsTableModel::addNewRequest
void addNewRequest(const SendCoinsRecipient &recipient)
Definition: recentrequeststablemodel.cpp:165
RecentRequestsTableModel::nReceiveRequestsMaxId
int64_t nReceiveRequestsMaxId
Definition: recentrequeststablemodel.h:95
RecentRequestsTableModel::~RecentRequestsTableModel
~RecentRequestsTableModel()
Definition: recentrequeststablemodel.cpp:33
RecentRequestsTableModel::index
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Definition: recentrequeststablemodel.cpp:131
RecentRequestsTableModel::Date
@ Date
Definition: recentrequeststablemodel.h:59
CLIENT_VERSION
static constexpr int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
Definition: clientversion.h:44
RecentRequestEntry::id
int64_t id
Definition: recentrequeststablemodel.h:23
OptionsModel::getDisplayUnit
int getDisplayUnit() const
Definition: optionsmodel.h:97
SendCoinsRecipient::address
QString address
Definition: sendcoinsrecipient.h:39
RecentRequestsTableModel::Label
@ Label
Definition: recentrequeststablemodel.h:60
RecentRequestEntryLessThan::operator()
bool operator()(const RecentRequestEntry &left, const RecentRequestEntry &right) const
Definition: recentrequeststablemodel.cpp:222
SER_DISK
@ SER_DISK
Definition: serialize.h:166
bitcoinunits.h
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:197
RecentRequestsTableModel::sort
void sort(int column, Qt::SortOrder order=Qt::AscendingOrder) override
Definition: recentrequeststablemodel.cpp:210
RecentRequestsTableModel::data
QVariant data(const QModelIndex &index, int role) const override
Definition: recentrequeststablemodel.cpp:49
RecentRequestsTableModel::Message
@ Message
Definition: recentrequeststablemodel.h:61
GUIUtil::dateTimeStr
QString dateTimeStr(const QDateTime &date)
Definition: guiutil.cpp:67
BitcoinUnits::shortName
static QString shortName(int unit)
Short name.
Definition: bitcoinunits.cpp:72
optionsmodel.h
RecentRequestsTableModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
Definition: recentrequeststablemodel.cpp:101
CDataStream::str
std::string str() const
Definition: streams.h:271
RecentRequestEntryLessThan::column
int column
Definition: recentrequeststablemodel.h:43
RecentRequestEntry
Definition: recentrequeststablemodel.h:16
SendCoinsRecipient::message
QString message
Definition: sendcoinsrecipient.h:43