Fabcoin Core  0.16.2
P2P Digital Currency
recentrequeststablemodel.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2017 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 <fabcoinunits.h>
8 #include <guiutil.h>
9 #include <optionsmodel.h>
10 
11 #include <clientversion.h>
12 #include <streams.h>
13 
14 
16  QAbstractTableModel(parent), walletModel(parent)
17 {
18  Q_UNUSED(wallet);
20 
21  // Load entries from wallet
22  std::vector<std::string> vReceiveRequests;
23  parent->loadReceiveRequests(vReceiveRequests);
24  for (const std::string& request : vReceiveRequests)
25  addNewRequest(request);
26 
27  /* These columns must match the indices in the ColumnIndex enumeration */
28  columns << tr("Date") << tr("Label") << tr("Message") << getAmountTitle();
29 
30  connect(walletModel->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
31 }
32 
34 {
35  /* Intentionally left empty */
36 }
37 
38 int RecentRequestsTableModel::rowCount(const QModelIndex &parent) const
39 {
40  Q_UNUSED(parent);
41 
42  return list.length();
43 }
44 
45 int RecentRequestsTableModel::columnCount(const QModelIndex &parent) const
46 {
47  Q_UNUSED(parent);
48 
49  return columns.length();
50 }
51 
52 QVariant RecentRequestsTableModel::data(const QModelIndex &index, int role) const
53 {
54  if(!index.isValid() || index.row() >= list.length())
55  return QVariant();
56 
57  if(role == Qt::DisplayRole || role == Qt::EditRole)
58  {
59  const RecentRequestEntry *rec = &list[index.row()];
60  switch(index.column())
61  {
62  case Date:
63  return GUIUtil::dateTimeStr(rec->date);
64  case Label:
65  if(rec->recipient.label.isEmpty() && role == Qt::DisplayRole)
66  {
67  return tr("(no label)");
68  }
69  else
70  {
71  return rec->recipient.label;
72  }
73  case Message:
74  if(rec->recipient.message.isEmpty() && role == Qt::DisplayRole)
75  {
76  return tr("(no message)");
77  }
78  else
79  {
80  return rec->recipient.message;
81  }
82  case Amount:
83  if (rec->recipient.amount == 0 && role == Qt::DisplayRole)
84  return tr("(no amount requested)");
85  else if (role == Qt::EditRole)
87  else
89  }
90  }
91  else if (role == Qt::TextAlignmentRole)
92  {
93  if (index.column() == Amount)
94  return (int)(Qt::AlignRight|Qt::AlignVCenter);
95  }
96  return QVariant();
97 }
98 
99 bool RecentRequestsTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
100 {
101  return true;
102 }
103 
104 QVariant RecentRequestsTableModel::headerData(int section, Qt::Orientation orientation, int role) const
105 {
106  if(orientation == Qt::Horizontal)
107  {
108  if(role == Qt::DisplayRole && section < columns.size())
109  {
110  return columns[section];
111  }
112  }
113  return QVariant();
114 }
115 
118 {
120  Q_EMIT headerDataChanged(Qt::Horizontal,Amount,Amount);
121 }
122 
125 {
126  return (this->walletModel->getOptionsModel() != nullptr) ? tr("Requested") + " ("+FabcoinUnits::name(this->walletModel->getOptionsModel()->getDisplayUnit()) + ")" : "";
127 }
128 
129 QModelIndex RecentRequestsTableModel::index(int row, int column, const QModelIndex &parent) const
130 {
131  Q_UNUSED(parent);
132 
133  return createIndex(row, column);
134 }
135 
136 bool RecentRequestsTableModel::removeRows(int row, int count, const QModelIndex &parent)
137 {
138  Q_UNUSED(parent);
139 
140  if(count > 0 && row >= 0 && (row+count) <= list.size())
141  {
142  const RecentRequestEntry *rec;
143  for (int i = 0; i < count; ++i)
144  {
145  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 
172  CDataStream ss(SER_DISK, CLIENT_VERSION);
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());
185  CDataStream ss(data, SER_DISK, CLIENT_VERSION);
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  qSort(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  RecentRequestEntry *pLeft = &left;
221  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)
bool setData(const QModelIndex &index, const QVariant &value, int role)
void addNewRequest(const SendCoinsRecipient &recipient)
void sort(int column, Qt::SortOrder order=Qt::AscendingOrder)
QModelIndex index(int row, int column, const QModelIndex &parent) const
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex())
void swap(dev::eth::Watch &_a, dev::eth::Watch &_b)
Definition: Interface.h:284
size_t count
Definition: ExecStats.cpp:37
QString dateTimeStr(const QDateTime &date)
Definition: guiutil.cpp:80
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:146
RecentRequestsTableModel(CWallet *wallet, WalletModel *parent)
const RecentRequestEntry & entry(int row) const
Qt::ItemFlags flags(const QModelIndex &index) const
int64_t id
QDateTime date
std::string str() const
Definition: streams.h:224
QVariant data(const QModelIndex &index, int role) const
QList< RecentRequestEntry > list
int rowCount(const QModelIndex &parent) const
static QString format(int unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=separatorStandard)
Format as string.
SendCoinsRecipient recipient
int getDisplayUnit()
Definition: optionsmodel.h:70
bool operator()(RecentRequestEntry &left, RecentRequestEntry &right) const
static QString name(int unit)
Short name.
Interface to Fabcoin wallet from Qt view code.
Definition: walletmodel.h:103
void updateAmountColumnTitle()
Updates the column title to "Amount (DisplayUnit)" and emits headerDataChanged() signal for table hea...
QVariant headerData(int section, Qt::Orientation orientation, int role) const
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances...
Definition: wallet.h:672
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()