Raven Core  3.0.0
P2P Digital Currency
recentrequeststablemodel.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2016 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Raven Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
7 
8 #include "ravenunits.h"
9 #include "guiutil.h"
10 #include "optionsmodel.h"
11 
12 #include "clientversion.h"
13 #include "streams.h"
14 
15 
17  QAbstractTableModel(parent), walletModel(parent)
18 {
19  Q_UNUSED(wallet);
21 
22  // Load entries from wallet
23  std::vector<std::string> vReceiveRequests;
24  parent->loadReceiveRequests(vReceiveRequests);
25  for (const std::string& request : vReceiveRequests)
26  addNewRequest(request);
27 
28  /* These columns must match the indices in the ColumnIndex enumeration */
29  columns << tr("Date") << tr("Label") << tr("Message") << getAmountTitle();
30 
31  connect(walletModel->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
32 }
33 
35 {
36  /* Intentionally left empty */
37 }
38 
39 int RecentRequestsTableModel::rowCount(const QModelIndex &parent) const
40 {
41  Q_UNUSED(parent);
42 
43  return list.length();
44 }
45 
46 int RecentRequestsTableModel::columnCount(const QModelIndex &parent) const
47 {
48  Q_UNUSED(parent);
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") + " ("+RavenUnits::name(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  const RecentRequestEntry *rec;
144  for (int i = 0; i < count; ++i)
145  {
146  rec = &list[row+i];
147  if (!walletModel->saveReceiveRequest(rec->recipient.address.toStdString(), rec->id, ""))
148  return false;
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 {
162  return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
163 }
164 
165 // called when adding a request from the GUI
167 {
168  RecentRequestEntry newEntry;
169  newEntry.id = ++nReceiveRequestsMaxId;
170  newEntry.date = QDateTime::currentDateTime();
171  newEntry.recipient = recipient;
172 
173  CDataStream ss(SER_DISK, CLIENT_VERSION);
174  ss << newEntry;
175 
176  if (!walletModel->saveReceiveRequest(recipient.address.toStdString(), newEntry.id, ss.str()))
177  return;
178 
179  addNewRequest(newEntry);
180 }
181 
182 // called from ctor when loading from wallet
183 void RecentRequestsTableModel::addNewRequest(const std::string &recipient)
184 {
185  std::vector<char> data(recipient.begin(), recipient.end());
186  CDataStream ss(data, SER_DISK, CLIENT_VERSION);
187 
189  ss >> entry;
190 
191  if (entry.id == 0) // should not happen
192  return;
193 
194  if (entry.id > nReceiveRequestsMaxId)
195  nReceiveRequestsMaxId = entry.id;
196 
197  addNewRequest(entry);
198 }
199 
200 // actually add to table in GUI
202 {
203  beginInsertRows(QModelIndex(), 0, 0);
204  list.prepend(recipient);
205  endInsertRows();
206 
207 
208 }
209 
210 void RecentRequestsTableModel::sort(int column, Qt::SortOrder order)
211 {
212  qSort(list.begin(), list.end(), RecentRequestEntryLessThan(column, order));
213  Q_EMIT dataChanged(index(0, 0, QModelIndex()), index(list.size() - 1, NUMBER_OF_COLUMNS - 1, QModelIndex()));
214 }
215 
217 {
219 }
220 
222 {
223  RecentRequestEntry *pLeft = &left;
224  RecentRequestEntry *pRight = &right;
225  if (order == Qt::DescendingOrder)
226  std::swap(pLeft, pRight);
227 
228  switch(column)
229  {
231  return pLeft->date.toTime_t() < pRight->date.toTime_t();
233  return pLeft->recipient.label < pRight->recipient.label;
235  return pLeft->recipient.message < pRight->recipient.message;
237  return pLeft->recipient.amount < pRight->recipient.amount;
238  default:
239  return pLeft->id < pRight->id;
240  }
241 }
void loadReceiveRequests(std::vector< std::string > &vReceiveRequests)
bool setData(const QModelIndex &index, const QVariant &value, int role)
void addNewRequest(const SendCoinsRecipient &recipient)
void sort(int column, Qt::SortOrder order=Qt::AscendingOrder)
static QString format(int unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=separatorStandard, const int nAssetUnit=MIN_ASSET_UNITS - 1)
Format as string.
Definition: ravenunits.cpp:101
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex())
std::string str() const
Definition: streams.h:225
QString dateTimeStr(const QDateTime &date)
Definition: guiutil.cpp:158
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:147
RecentRequestsTableModel(CWallet *wallet, WalletModel *parent)
int getDisplayUnit() const
Definition: optionsmodel.h:68
int64_t id
QDateTime date
QList< RecentRequestEntry > list
QVariant headerData(int section, Qt::Orientation orientation, int role) const
int rowCount(const QModelIndex &parent) const
QModelIndex index(int row, int column, const QModelIndex &parent) const
SendCoinsRecipient recipient
const RecentRequestEntry & entry(int row) const
bool operator()(RecentRequestEntry &left, RecentRequestEntry &right) const
QVariant data(const QModelIndex &index, int role) const
Interface to Raven wallet from Qt view code.
Definition: walletmodel.h:165
void updateAmountColumnTitle()
Updates the column title to "Amount (DisplayUnit)" and emits headerDataChanged() signal for table hea...
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances...
Definition: wallet.h:673
static QString name(int unit)
Short name.
Definition: ravenunits.cpp:40
int columnCount(const QModelIndex &parent) const
bool saveReceiveRequest(const std::string &sAddress, const int64_t nId, const std::string &sRequest)
QString getAmountTitle()
Gets title for amount column including current display unit if optionsModel reference available...
OptionsModel * getOptionsModel()
Qt::ItemFlags flags(const QModelIndex &index) const