Skip to content

Commit 4d90102

Browse files
committed
[Qt] Add sorting feature to the requested payments table
1 parent 8476d5d commit 4d90102

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

src/qt/forms/receivecoinsdialog.ui

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,11 @@
207207
</widget>
208208
</item>
209209
<item>
210-
<widget class="QTableView" name="recentRequestsView"/>
210+
<widget class="QTableView" name="recentRequestsView">
211+
<property name="sortingEnabled">
212+
<bool>true</bool>
213+
</property>
214+
</widget>
211215
</item>
212216
<item>
213217
<layout class="QHBoxLayout" name="horizontalLayout_2">

src/qt/receivecoinsdialog.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ void ReceiveCoinsDialog::setModel(WalletModel *model)
5555
ui->recentRequestsView->horizontalHeader()->setSectionResizeMode(RecentRequestsTableModel::Message, QHeaderView::Stretch);
5656
#endif
5757
ui->recentRequestsView->horizontalHeader()->resizeSection(RecentRequestsTableModel::Amount, 100);
58+
59+
model->getRecentRequestsTableModel()->sort(RecentRequestsTableModel::Date, Qt::DescendingOrder);
5860
}
5961
}
6062

src/qt/recentrequeststablemodel.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,31 @@ void RecentRequestsTableModel::addNewRequest(RecentRequestEntry &recipient)
175175
list.prepend(recipient);
176176
endInsertRows();
177177
}
178+
179+
void RecentRequestsTableModel::sort(int column, Qt::SortOrder order)
180+
{
181+
qSort(list.begin(), list.end(), RecentRequestEntryLessThan(column, order));
182+
emit dataChanged(index(0, 0, QModelIndex()), index(list.size() - 1, NUMBER_OF_COLUMNS - 1, QModelIndex()));
183+
}
184+
185+
bool RecentRequestEntryLessThan::operator()(RecentRequestEntry &left, RecentRequestEntry &right) const
186+
{
187+
RecentRequestEntry *pLeft = &left;
188+
RecentRequestEntry *pRight = &right;
189+
if (order == Qt::DescendingOrder)
190+
std::swap(pLeft, pRight);
191+
192+
switch(column)
193+
{
194+
case RecentRequestsTableModel::Date:
195+
return pLeft->date.toTime_t() < pRight->date.toTime_t();
196+
case RecentRequestsTableModel::Label:
197+
return pLeft->recipient.label < pRight->recipient.label;
198+
case RecentRequestsTableModel::Message:
199+
return pLeft->recipient.message < pRight->recipient.message;
200+
case RecentRequestsTableModel::Amount:
201+
return pLeft->recipient.amount < pRight->recipient.amount;
202+
default:
203+
return pLeft->id < pRight->id;
204+
}
205+
}

src/qt/recentrequeststablemodel.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ class RecentRequestEntry
4141
)
4242
};
4343

44+
class RecentRequestEntryLessThan
45+
{
46+
public:
47+
RecentRequestEntryLessThan(int nColumn, Qt::SortOrder fOrder):
48+
column(nColumn), order(fOrder) {}
49+
bool operator()(RecentRequestEntry &left, RecentRequestEntry &right ) const;
50+
51+
private:
52+
int column;
53+
Qt::SortOrder order;
54+
};
55+
4456
/** Model for list of recently generated payment requests / bitcoin URIs.
4557
* Part of wallet model.
4658
*/
@@ -56,7 +68,8 @@ class RecentRequestsTableModel: public QAbstractTableModel
5668
Date = 0,
5769
Label = 1,
5870
Message = 2,
59-
Amount = 3
71+
Amount = 3,
72+
NUMBER_OF_COLUMNS
6073
};
6174

6275
/** @name Methods overridden from QAbstractTableModel
@@ -76,6 +89,9 @@ class RecentRequestsTableModel: public QAbstractTableModel
7689
void addNewRequest(const std::string &recipient);
7790
void addNewRequest(RecentRequestEntry &recipient);
7891

92+
public slots:
93+
void sort(int column, Qt::SortOrder order = Qt::AscendingOrder);
94+
7995
private:
8096
WalletModel *walletModel;
8197
QStringList columns;

0 commit comments

Comments
 (0)