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.1KB

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