From 65abe91ce4c3c9b26afa5d1af38bd1bfe81546c4 Mon Sep 17 00:00:00 2001 From: Philip Kaufmann Date: Fri, 26 Jun 2015 14:58:15 +0200 Subject: [PATCH] [Qt] add sorting for bantable --- src/qt/bantablemodel.cpp | 22 ++++++++++++++++++++++ src/qt/bantablemodel.h | 14 +++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/qt/bantablemodel.cpp b/src/qt/bantablemodel.cpp index 6074cd2f07..04840b0526 100644 --- a/src/qt/bantablemodel.cpp +++ b/src/qt/bantablemodel.cpp @@ -14,6 +14,24 @@ #include #include +bool BannedNodeLessThan::operator()(const CCombinedBan& left, const CCombinedBan& right) const +{ + const CCombinedBan* pLeft = &left; + const CCombinedBan* pRight = &right; + + if (order == Qt::DescendingOrder) + std::swap(pLeft, pRight); + + switch(column) + { + case BanTableModel::Address: + return pLeft->subnet.ToString().compare(pRight->subnet.ToString()) < 0; + case BanTableModel::Bantime: + return pLeft->bantil < pRight->bantil; + } + + return false; +} // private implementation class BanTablePriv @@ -43,6 +61,10 @@ public: banEntry.bantil = banentry.second; cachedBanlist.append(banEntry); } + + if (sortColumn >= 0) + // sort cachedBanlist (use stable sort to prevent rows jumping around unneceesarily) + qStableSort(cachedBanlist.begin(), cachedBanlist.end(), BannedNodeLessThan(sortColumn, sortOrder)); } int size() const diff --git a/src/qt/bantablemodel.h b/src/qt/bantablemodel.h index c0f0813f28..ec93ed1014 100644 --- a/src/qt/bantablemodel.h +++ b/src/qt/bantablemodel.h @@ -18,6 +18,18 @@ struct CCombinedBan { int64_t bantil; }; +class BannedNodeLessThan +{ +public: + BannedNodeLessThan(int nColumn, Qt::SortOrder fOrder) : + column(nColumn), order(fOrder) {} + bool operator()(const CCombinedBan& left, const CCombinedBan& right) const; + +private: + int column; + Qt::SortOrder order; +}; + /** Qt model providing information about connected peers, similar to the "getpeerinfo" RPC call. Used by the rpc console UI. @@ -33,7 +45,7 @@ public: enum ColumnIndex { Address = 0, - Bantime = 1, + Bantime = 1 }; /** @name Methods overridden from QAbstractTableModel