Anoncoin  0.9.4
P2P Digital Currency
recentrequeststablemodel.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2014 The Bitcoin developers
2 // Copyright (c) 2013-2014 The Anoncoin Core developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
7 
8 #include "anoncoinunits.h"
9 #include "clientversion.h"
10 #include "guiutil.h"
11 #include "optionsmodel.h"
12 
14  walletModel(parent)
15 {
16  Q_UNUSED(wallet);
18 
19  // Load entries from wallet
20  std::vector<std::string> vReceiveRequests;
21  parent->loadReceiveRequests(vReceiveRequests);
22  BOOST_FOREACH(const std::string& request, vReceiveRequests)
23  addNewRequest(request);
24 
25  /* These columns must match the indices in the ColumnIndex enumeration */
26  columns << tr("Date") << tr("Label") << tr("Message") << getAmountTitle();
27 
28  connect(walletModel->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
29 }
30 
32 {
33  /* Intentionally left empty */
34 }
35 
36 int RecentRequestsTableModel::rowCount(const QModelIndex &parent) const
37 {
38  Q_UNUSED(parent);
39 
40  return list.length();
41 }
42 
43 int RecentRequestsTableModel::columnCount(const QModelIndex &parent) const
44 {
45  Q_UNUSED(parent);
46 
47  return columns.length();
48 }
49 
50 QVariant RecentRequestsTableModel::data(const QModelIndex &index, int role) const
51 {
52  if(!index.isValid() || index.row() >= list.length())
53  return QVariant();
54 
55  const RecentRequestEntry *rec = &list[index.row()];
56 
57  if(role == Qt::DisplayRole || role == Qt::EditRole)
58  {
59  switch(index.column())
60  {
61  case Date:
62  return GUIUtil::dateTimeStr(rec->date);
63  case Label:
64  if(rec->recipient.label.isEmpty() && role == Qt::DisplayRole)
65  {
66  return tr("(no label)");
67  }
68  else
69  {
70  return rec->recipient.label;
71  }
72  case Message:
73  if(rec->recipient.message.isEmpty() && role == Qt::DisplayRole)
74  {
75  return tr("(no message)");
76  }
77  else
78  {
79  return rec->recipient.message;
80  }
81  case Amount:
82  if (rec->recipient.amount == 0 && role == Qt::DisplayRole)
83  return tr("(no amount)");
84  else
86  }
87  }
88  return QVariant();
89 }
90 
91 bool RecentRequestsTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
92 {
93  return true;
94 }
95 
96 QVariant RecentRequestsTableModel::headerData(int section, Qt::Orientation orientation, int role) const
97 {
98  if(orientation == Qt::Horizontal)
99  {
100  if(role == Qt::DisplayRole && section < columns.size())
101  {
102  return columns[section];
103  }
104  }
105  return QVariant();
106 }
107 
110 {
112  emit headerDataChanged(Qt::Horizontal,Amount,Amount);
113 }
114 
117 {
118  QString amountTitle = tr("Amount");
119  if (this->walletModel->getOptionsModel() != NULL)
120  {
121  amountTitle += " ("+AnoncoinUnits::name(this->walletModel->getOptionsModel()->getDisplayUnit()) + ")";
122  }
123  return amountTitle;
124 }
125 
126 QModelIndex RecentRequestsTableModel::index(int row, int column, const QModelIndex &parent) const
127 {
128  Q_UNUSED(parent);
129 
130  return createIndex(row, column);
131 }
132 
133 bool RecentRequestsTableModel::removeRows(int row, int count, const QModelIndex &parent)
134 {
135  Q_UNUSED(parent);
136 
137  if(count > 0 && row >= 0 && (row+count) <= list.size())
138  {
139  const RecentRequestEntry *rec;
140  for (int i = 0; i < count; ++i)
141  {
142  rec = &list[row+i];
143  if (!walletModel->saveReceiveRequest(rec->recipient.address.toStdString(), rec->id, ""))
144  return false;
145  }
146 
147  beginRemoveRows(parent, row, row + count - 1);
148  list.erase(list.begin() + row, list.begin() + row + count);
149  endRemoveRows();
150  return true;
151  } else {
152  return false;
153  }
154 }
155 
156 Qt::ItemFlags RecentRequestsTableModel::flags(const QModelIndex &index) const
157 {
158  return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
159 }
160 
161 // called when adding a request from the GUI
163 {
164  RecentRequestEntry newEntry;
165  newEntry.id = ++nReceiveRequestsMaxId;
166  newEntry.date = QDateTime::currentDateTime();
167  newEntry.recipient = recipient;
168 
169  CDataStream ss(SER_DISK, CLIENT_VERSION);
170  ss << newEntry;
171 
172  if (!walletModel->saveReceiveRequest(recipient.address.toStdString(), newEntry.id, ss.str()))
173  return;
174 
175  addNewRequest(newEntry);
176 }
177 
178 // called from ctor when loading from wallet
179 void RecentRequestsTableModel::addNewRequest(const std::string &recipient)
180 {
181  std::vector<char> data(recipient.begin(), recipient.end());
182  CDataStream ss(data, SER_DISK, CLIENT_VERSION);
183 
185  ss >> entry;
186 
187  if (entry.id == 0) // should not happen
188  return;
189 
190  if (entry.id > nReceiveRequestsMaxId)
191  nReceiveRequestsMaxId = entry.id;
192 
193  addNewRequest(entry);
194 }
195 
196 // actually add to table in GUI
198 {
199  beginInsertRows(QModelIndex(), 0, 0);
200  list.prepend(recipient);
201  endInsertRows();
202 }
203 
204 void RecentRequestsTableModel::sort(int column, Qt::SortOrder order)
205 {
206  qSort(list.begin(), list.end(), RecentRequestEntryLessThan(column, order));
207  emit dataChanged(index(0, 0, QModelIndex()), index(list.size() - 1, NUMBER_OF_COLUMNS - 1, QModelIndex()));
208 }
209 
211 {
213 }
214 
216 {
217  RecentRequestEntry *pLeft = &left;
218  RecentRequestEntry *pRight = &right;
219  if (order == Qt::DescendingOrder)
220  std::swap(pLeft, pRight);
221 
222  switch(column)
223  {
225  return pLeft->date.toTime_t() < pRight->date.toTime_t();
227  return pLeft->recipient.label < pRight->recipient.label;
229  return pLeft->recipient.message < pRight->recipient.message;
231  return pLeft->recipient.amount < pRight->recipient.amount;
232  default:
233  return pLeft->id < pRight->id;
234  }
235 }
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())
static QString name(int unit)
Short name.
QString dateTimeStr(const QDateTime &date)
Definition: guiutil.cpp:75
Double ended buffer combining vector and stream-like interfaces.
Definition: serialize.h:848
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: serialize.h:924
QVariant data(const QModelIndex &index, int role) const
static QString format(int unit, qint64 amount, bool plussign=false)
Format as string.
QList< RecentRequestEntry > list
int rowCount(const QModelIndex &parent) const
SendCoinsRecipient recipient
Qt::SortOrder order
int getDisplayUnit()
Definition: optionsmodel.h:87
bool operator()(RecentRequestEntry &left, RecentRequestEntry &right) const
Interface to Anoncoin wallet from Qt view code.
Definition: walletmodel.h:97
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:101
int columnCount(const QModelIndex &parent) const
int column
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()