You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

recentrequeststablemodel.cpp 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. #include "recentrequeststablemodel.h"
  5. #include "bitcoinunits.h"
  6. #include "guiutil.h"
  7. #include "optionsmodel.h"
  8. #include "clientversion.h"
  9. #include "streams.h"
  10. #include <boost/foreach.hpp>
  11. RecentRequestsTableModel::RecentRequestsTableModel(CWallet *wallet, WalletModel *parent) :
  12. QAbstractTableModel(parent), walletModel(parent)
  13. {
  14. Q_UNUSED(wallet);
  15. nReceiveRequestsMaxId = 0;
  16. // Load entries from wallet
  17. std::vector<std::string> vReceiveRequests;
  18. parent->loadReceiveRequests(vReceiveRequests);
  19. BOOST_FOREACH(const std::string& request, vReceiveRequests)
  20. addNewRequest(request);
  21. /* These columns must match the indices in the ColumnIndex enumeration */
  22. columns << tr("Date") << tr("Label") << tr("Message") << getAmountTitle();
  23. connect(walletModel->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
  24. }
  25. RecentRequestsTableModel::~RecentRequestsTableModel()
  26. {
  27. /* Intentionally left empty */
  28. }
  29. int RecentRequestsTableModel::rowCount(const QModelIndex &parent) const
  30. {
  31. Q_UNUSED(parent);
  32. return list.length();
  33. }
  34. int RecentRequestsTableModel::columnCount(const QModelIndex &parent) const
  35. {
  36. Q_UNUSED(parent);
  37. return columns.length();
  38. }
  39. QVariant RecentRequestsTableModel::data(const QModelIndex &index, int role) const
  40. {
  41. if(!index.isValid() || index.row() >= list.length())
  42. return QVariant();
  43. if(role == Qt::DisplayRole || role == Qt::EditRole)
  44. {
  45. const RecentRequestEntry *rec = &list[index.row()];
  46. switch(index.column())
  47. {
  48. case Date:
  49. return GUIUtil::dateTimeStr(rec->date);
  50. case Label:
  51. if(rec->recipient.label.isEmpty() && role == Qt::DisplayRole)
  52. {
  53. return tr("(no label)");
  54. }
  55. else
  56. {
  57. return rec->recipient.label;
  58. }
  59. case Message:
  60. if(rec->recipient.message.isEmpty() && role == Qt::DisplayRole)
  61. {
  62. return tr("(no message)");
  63. }
  64. else
  65. {
  66. return rec->recipient.message;
  67. }
  68. case Amount:
  69. if (rec->recipient.amount == 0 && role == Qt::DisplayRole)
  70. return tr("(no amount requested)");
  71. else if (role == Qt::EditRole)
  72. return BitcoinUnits::format(walletModel->getOptionsModel()->getDisplayUnit(), rec->recipient.amount, false, BitcoinUnits::separatorNever);
  73. else
  74. return BitcoinUnits::format(walletModel->getOptionsModel()->getDisplayUnit(), rec->recipient.amount);
  75. }
  76. }
  77. else if (role == Qt::TextAlignmentRole)
  78. {
  79. if (index.column() == Amount)
  80. return (int)(Qt::AlignRight|Qt::AlignVCenter);
  81. }
  82. return QVariant();
  83. }
  84. bool RecentRequestsTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
  85. {
  86. return true;
  87. }
  88. QVariant RecentRequestsTableModel::headerData(int section, Qt::Orientation orientation, int role) const
  89. {
  90. if(orientation == Qt::Horizontal)
  91. {
  92. if(role == Qt::DisplayRole && section < columns.size())
  93. {
  94. return columns[section];
  95. }
  96. }
  97. return QVariant();
  98. }
  99. /** Updates the column title to "Amount (DisplayUnit)" and emits headerDataChanged() signal for table headers to react. */
  100. void RecentRequestsTableModel::updateAmountColumnTitle()
  101. {
  102. columns[Amount] = getAmountTitle();
  103. Q_EMIT headerDataChanged(Qt::Horizontal,Amount,Amount);
  104. }
  105. /** Gets title for amount column including current display unit if optionsModel reference available. */
  106. QString RecentRequestsTableModel::getAmountTitle()
  107. {
  108. return (this->walletModel->getOptionsModel() != NULL) ? tr("Requested") + " ("+BitcoinUnits::name(this->walletModel->getOptionsModel()->getDisplayUnit()) + ")" : "";
  109. }
  110. QModelIndex RecentRequestsTableModel::index(int row, int column, const QModelIndex &parent) const
  111. {
  112. Q_UNUSED(parent);
  113. return createIndex(row, column);
  114. }
  115. bool RecentRequestsTableModel::removeRows(int row, int count, const QModelIndex &parent)
  116. {
  117. Q_UNUSED(parent);
  118. if(count > 0 && row >= 0 && (row+count) <= list.size())
  119. {
  120. const RecentRequestEntry *rec;
  121. for (int i = 0; i < count; ++i)
  122. {
  123. rec = &list[row+i];
  124. if (!walletModel->saveReceiveRequest(rec->recipient.address.toStdString(), rec->id, ""))
  125. return false;
  126. }
  127. beginRemoveRows(parent, row, row + count - 1);
  128. list.erase(list.begin() + row, list.begin() + row + count);
  129. endRemoveRows();
  130. return true;
  131. } else {
  132. return false;
  133. }
  134. }
  135. Qt::ItemFlags RecentRequestsTableModel::flags(const QModelIndex &index) const
  136. {
  137. return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
  138. }
  139. // called when adding a request from the GUI
  140. void RecentRequestsTableModel::addNewRequest(const SendCoinsRecipient &recipient)
  141. {
  142. RecentRequestEntry newEntry;
  143. newEntry.id = ++nReceiveRequestsMaxId;
  144. newEntry.date = QDateTime::currentDateTime();
  145. newEntry.recipient = recipient;
  146. CDataStream ss(SER_DISK, CLIENT_VERSION);
  147. ss << newEntry;
  148. if (!walletModel->saveReceiveRequest(recipient.address.toStdString(), newEntry.id, ss.str()))
  149. return;
  150. addNewRequest(newEntry);
  151. }
  152. // called from ctor when loading from wallet
  153. void RecentRequestsTableModel::addNewRequest(const std::string &recipient)
  154. {
  155. std::vector<char> data(recipient.begin(), recipient.end());
  156. CDataStream ss(data, SER_DISK, CLIENT_VERSION);
  157. RecentRequestEntry entry;
  158. ss >> entry;
  159. if (entry.id == 0) // should not happen
  160. return;
  161. if (entry.id > nReceiveRequestsMaxId)
  162. nReceiveRequestsMaxId = entry.id;
  163. addNewRequest(entry);
  164. }
  165. // actually add to table in GUI
  166. void RecentRequestsTableModel::addNewRequest(RecentRequestEntry &recipient)
  167. {
  168. beginInsertRows(QModelIndex(), 0, 0);
  169. list.prepend(recipient);
  170. endInsertRows();
  171. }
  172. void RecentRequestsTableModel::sort(int column, Qt::SortOrder order)
  173. {
  174. qSort(list.begin(), list.end(), RecentRequestEntryLessThan(column, order));
  175. Q_EMIT dataChanged(index(0, 0, QModelIndex()), index(list.size() - 1, NUMBER_OF_COLUMNS - 1, QModelIndex()));
  176. }
  177. void RecentRequestsTableModel::updateDisplayUnit()
  178. {
  179. updateAmountColumnTitle();
  180. }
  181. bool RecentRequestEntryLessThan::operator()(RecentRequestEntry &left, RecentRequestEntry &right) const
  182. {
  183. RecentRequestEntry *pLeft = &left;
  184. RecentRequestEntry *pRight = &right;
  185. if (order == Qt::DescendingOrder)
  186. std::swap(pLeft, pRight);
  187. switch(column)
  188. {
  189. case RecentRequestsTableModel::Date:
  190. return pLeft->date.toTime_t() < pRight->date.toTime_t();
  191. case RecentRequestsTableModel::Label:
  192. return pLeft->recipient.label < pRight->recipient.label;
  193. case RecentRequestsTableModel::Message:
  194. return pLeft->recipient.message < pRight->recipient.message;
  195. case RecentRequestsTableModel::Amount:
  196. return pLeft->recipient.amount < pRight->recipient.amount;
  197. default:
  198. return pLeft->id < pRight->id;
  199. }
  200. }