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.
5 #include <qt/recentrequeststablemodel.h>
7 #include <qt/bitcoinunits.h>
8 #include <qt/guiutil.h>
9 #include <qt/optionsmodel.h>
11 #include <clientversion.h>
15 RecentRequestsTableModel::RecentRequestsTableModel(CWallet
*wallet
, WalletModel
*parent
) :
16 QAbstractTableModel(parent
), walletModel(parent
)
19 nReceiveRequestsMaxId
= 0;
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
);
27 /* These columns must match the indices in the ColumnIndex enumeration */
28 columns
<< tr("Date") << tr("Label") << tr("Message") << getAmountTitle();
30 connect(walletModel
->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
33 RecentRequestsTableModel::~RecentRequestsTableModel()
35 /* Intentionally left empty */
38 int RecentRequestsTableModel::rowCount(const QModelIndex
&parent
) const
45 int RecentRequestsTableModel::columnCount(const QModelIndex
&parent
) const
49 return columns
.length();
52 QVariant
RecentRequestsTableModel::data(const QModelIndex
&index
, int role
) const
54 if(!index
.isValid() || index
.row() >= list
.length())
57 if(role
== Qt::DisplayRole
|| role
== Qt::EditRole
)
59 const RecentRequestEntry
*rec
= &list
[index
.row()];
60 switch(index
.column())
63 return GUIUtil::dateTimeStr(rec
->date
);
65 if(rec
->recipient
.label
.isEmpty() && role
== Qt::DisplayRole
)
67 return tr("(no label)");
71 return rec
->recipient
.label
;
74 if(rec
->recipient
.message
.isEmpty() && role
== Qt::DisplayRole
)
76 return tr("(no message)");
80 return rec
->recipient
.message
;
83 if (rec
->recipient
.amount
== 0 && role
== Qt::DisplayRole
)
84 return tr("(no amount requested)");
85 else if (role
== Qt::EditRole
)
86 return BitcoinUnits::format(walletModel
->getOptionsModel()->getDisplayUnit(), rec
->recipient
.amount
, false, BitcoinUnits::separatorNever
);
88 return BitcoinUnits::format(walletModel
->getOptionsModel()->getDisplayUnit(), rec
->recipient
.amount
);
91 else if (role
== Qt::TextAlignmentRole
)
93 if (index
.column() == Amount
)
94 return (int)(Qt::AlignRight
|Qt::AlignVCenter
);
99 bool RecentRequestsTableModel::setData(const QModelIndex
&index
, const QVariant
&value
, int role
)
104 QVariant
RecentRequestsTableModel::headerData(int section
, Qt::Orientation orientation
, int role
) const
106 if(orientation
== Qt::Horizontal
)
108 if(role
== Qt::DisplayRole
&& section
< columns
.size())
110 return columns
[section
];
116 /** Updates the column title to "Amount (DisplayUnit)" and emits headerDataChanged() signal for table headers to react. */
117 void RecentRequestsTableModel::updateAmountColumnTitle()
119 columns
[Amount
] = getAmountTitle();
120 Q_EMIT
headerDataChanged(Qt::Horizontal
,Amount
,Amount
);
123 /** Gets title for amount column including current display unit if optionsModel reference available. */
124 QString
RecentRequestsTableModel::getAmountTitle()
126 return (this->walletModel
->getOptionsModel() != nullptr) ? tr("Requested") + " ("+BitcoinUnits::name(this->walletModel
->getOptionsModel()->getDisplayUnit()) + ")" : "";
129 QModelIndex
RecentRequestsTableModel::index(int row
, int column
, const QModelIndex
&parent
) const
133 return createIndex(row
, column
);
136 bool RecentRequestsTableModel::removeRows(int row
, int count
, const QModelIndex
&parent
)
140 if(count
> 0 && row
>= 0 && (row
+count
) <= list
.size())
142 const RecentRequestEntry
*rec
;
143 for (int i
= 0; i
< count
; ++i
)
146 if (!walletModel
->saveReceiveRequest(rec
->recipient
.address
.toStdString(), rec
->id
, ""))
150 beginRemoveRows(parent
, row
, row
+ count
- 1);
151 list
.erase(list
.begin() + row
, list
.begin() + row
+ count
);
159 Qt::ItemFlags
RecentRequestsTableModel::flags(const QModelIndex
&index
) const
161 return Qt::ItemIsSelectable
| Qt::ItemIsEnabled
;
164 // called when adding a request from the GUI
165 void RecentRequestsTableModel::addNewRequest(const SendCoinsRecipient
&recipient
)
167 RecentRequestEntry newEntry
;
168 newEntry
.id
= ++nReceiveRequestsMaxId
;
169 newEntry
.date
= QDateTime::currentDateTime();
170 newEntry
.recipient
= recipient
;
172 CDataStream
ss(SER_DISK
, CLIENT_VERSION
);
175 if (!walletModel
->saveReceiveRequest(recipient
.address
.toStdString(), newEntry
.id
, ss
.str()))
178 addNewRequest(newEntry
);
181 // called from ctor when loading from wallet
182 void RecentRequestsTableModel::addNewRequest(const std::string
&recipient
)
184 std::vector
<char> data(recipient
.begin(), recipient
.end());
185 CDataStream
ss(data
, SER_DISK
, CLIENT_VERSION
);
187 RecentRequestEntry entry
;
190 if (entry
.id
== 0) // should not happen
193 if (entry
.id
> nReceiveRequestsMaxId
)
194 nReceiveRequestsMaxId
= entry
.id
;
196 addNewRequest(entry
);
199 // actually add to table in GUI
200 void RecentRequestsTableModel::addNewRequest(RecentRequestEntry
&recipient
)
202 beginInsertRows(QModelIndex(), 0, 0);
203 list
.prepend(recipient
);
207 void RecentRequestsTableModel::sort(int column
, Qt::SortOrder order
)
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()));
213 void RecentRequestsTableModel::updateDisplayUnit()
215 updateAmountColumnTitle();
218 bool RecentRequestEntryLessThan::operator()(RecentRequestEntry
&left
, RecentRequestEntry
&right
) const
220 RecentRequestEntry
*pLeft
= &left
;
221 RecentRequestEntry
*pRight
= &right
;
222 if (order
== Qt::DescendingOrder
)
223 std::swap(pLeft
, pRight
);
227 case RecentRequestsTableModel::Date
:
228 return pLeft
->date
.toTime_t() < pRight
->date
.toTime_t();
229 case RecentRequestsTableModel::Label
:
230 return pLeft
->recipient
.label
< pRight
->recipient
.label
;
231 case RecentRequestsTableModel::Message
:
232 return pLeft
->recipient
.message
< pRight
->recipient
.message
;
233 case RecentRequestsTableModel::Amount
:
234 return pLeft
->recipient
.amount
< pRight
->recipient
.amount
;
236 return pLeft
->id
< pRight
->id
;