]> Git Repo - VerusCoin.git/blob - src/qt/recentrequeststablemodel.cpp
5533adab8b996507e939a781111fa12344b8bbf1
[VerusCoin.git] / src / qt / recentrequeststablemodel.cpp
1 // Copyright (c) 2011-2014 The Bitcoin developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5 #include "recentrequeststablemodel.h"
6
7 #include "bitcoinunits.h"
8 #include "clientversion.h"
9 #include "guiutil.h"
10 #include "optionsmodel.h"
11 #include "streams.h"
12
13 #include <boost/foreach.hpp>
14
15 RecentRequestsTableModel::RecentRequestsTableModel(CWallet *wallet, WalletModel *parent) :
16     walletModel(parent)
17 {
18     Q_UNUSED(wallet);
19     nReceiveRequestsMaxId = 0;
20
21     // Load entries from wallet
22     std::vector<std::string> vReceiveRequests;
23     parent->loadReceiveRequests(vReceiveRequests);
24     BOOST_FOREACH(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
33 RecentRequestsTableModel::~RecentRequestsTableModel()
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     const RecentRequestEntry *rec = &list[index.row()];
58
59     if(role == Qt::DisplayRole || role == Qt::EditRole)
60     {
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)");
86             else if (role == Qt::EditRole)
87                 return BitcoinUnits::format(walletModel->getOptionsModel()->getDisplayUnit(), rec->recipient.amount, false, BitcoinUnits::separatorNever);
88             else
89                 return BitcoinUnits::format(walletModel->getOptionsModel()->getDisplayUnit(), rec->recipient.amount);
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
117 /** Updates the column title to "Amount (DisplayUnit)" and emits headerDataChanged() signal for table headers to react. */
118 void RecentRequestsTableModel::updateAmountColumnTitle()
119 {
120     columns[Amount] = getAmountTitle();
121     emit headerDataChanged(Qt::Horizontal,Amount,Amount);
122 }
123
124 /** Gets title for amount column including current display unit if optionsModel reference available. */
125 QString RecentRequestsTableModel::getAmountTitle()
126 {
127     QString amountTitle = tr("Amount");
128     if (this->walletModel->getOptionsModel() != NULL)
129     {
130         amountTitle += " ("+BitcoinUnits::name(this->walletModel->getOptionsModel()->getDisplayUnit()) + ")";
131     }
132     return amountTitle;
133 }
134
135 QModelIndex RecentRequestsTableModel::index(int row, int column, const QModelIndex &parent) const
136 {
137     Q_UNUSED(parent);
138
139     return createIndex(row, column);
140 }
141
142 bool RecentRequestsTableModel::removeRows(int row, int count, const QModelIndex &parent)
143 {
144     Q_UNUSED(parent);
145
146     if(count > 0 && row >= 0 && (row+count) <= list.size())
147     {
148         const RecentRequestEntry *rec;
149         for (int i = 0; i < count; ++i)
150         {
151             rec = &list[row+i];
152             if (!walletModel->saveReceiveRequest(rec->recipient.address.toStdString(), rec->id, ""))
153                 return false;
154         }
155
156         beginRemoveRows(parent, row, row + count - 1);
157         list.erase(list.begin() + row, list.begin() + row + count);
158         endRemoveRows();
159         return true;
160     } else {
161         return false;
162     }
163 }
164
165 Qt::ItemFlags RecentRequestsTableModel::flags(const QModelIndex &index) const
166 {
167     return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
168 }
169
170 // called when adding a request from the GUI
171 void RecentRequestsTableModel::addNewRequest(const SendCoinsRecipient &recipient)
172 {
173     RecentRequestEntry newEntry;
174     newEntry.id = ++nReceiveRequestsMaxId;
175     newEntry.date = QDateTime::currentDateTime();
176     newEntry.recipient = recipient;
177
178     CDataStream ss(SER_DISK, CLIENT_VERSION);
179     ss << newEntry;
180
181     if (!walletModel->saveReceiveRequest(recipient.address.toStdString(), newEntry.id, ss.str()))
182         return;
183
184     addNewRequest(newEntry);
185 }
186
187 // called from ctor when loading from wallet
188 void RecentRequestsTableModel::addNewRequest(const std::string &recipient)
189 {
190     std::vector<char> data(recipient.begin(), recipient.end());
191     CDataStream ss(data, SER_DISK, CLIENT_VERSION);
192
193     RecentRequestEntry entry;
194     ss >> entry;
195
196     if (entry.id == 0) // should not happen
197         return;
198
199     if (entry.id > nReceiveRequestsMaxId)
200         nReceiveRequestsMaxId = entry.id;
201
202     addNewRequest(entry);
203 }
204
205 // actually add to table in GUI
206 void RecentRequestsTableModel::addNewRequest(RecentRequestEntry &recipient)
207 {
208     beginInsertRows(QModelIndex(), 0, 0);
209     list.prepend(recipient);
210     endInsertRows();
211 }
212
213 void RecentRequestsTableModel::sort(int column, Qt::SortOrder order)
214 {
215     qSort(list.begin(), list.end(), RecentRequestEntryLessThan(column, order));
216     emit dataChanged(index(0, 0, QModelIndex()), index(list.size() - 1, NUMBER_OF_COLUMNS - 1, QModelIndex()));
217 }
218
219 void RecentRequestsTableModel::updateDisplayUnit()
220 {
221     updateAmountColumnTitle();
222 }
223
224 bool RecentRequestEntryLessThan::operator()(RecentRequestEntry &left, RecentRequestEntry &right) const
225 {
226     RecentRequestEntry *pLeft = &left;
227     RecentRequestEntry *pRight = &right;
228     if (order == Qt::DescendingOrder)
229         std::swap(pLeft, pRight);
230
231     switch(column)
232     {
233     case RecentRequestsTableModel::Date:
234         return pLeft->date.toTime_t() < pRight->date.toTime_t();
235     case RecentRequestsTableModel::Label:
236         return pLeft->recipient.label < pRight->recipient.label;
237     case RecentRequestsTableModel::Message:
238         return pLeft->recipient.message < pRight->recipient.message;
239     case RecentRequestsTableModel::Amount:
240         return pLeft->recipient.amount < pRight->recipient.amount;
241     default:
242         return pLeft->id < pRight->id;
243     }
244 }
This page took 0.02865 seconds and 2 git commands to generate.