Skip to content

Commit

Permalink
[Qt] add sorting for bantable
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip Kaufmann authored and jonasschnelli committed Sep 16, 2015
1 parent 51654de commit 65abe91
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/qt/bantablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@
#include <QDebug>
#include <QList>

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
Expand Down Expand Up @@ -43,6 +61,10 @@ class BanTablePriv
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
Expand Down
14 changes: 13 additions & 1 deletion src/qt/bantablemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -33,7 +45,7 @@ class BanTableModel : public QAbstractTableModel

enum ColumnIndex {
Address = 0,
Bantime = 1,
Bantime = 1
};

/** @name Methods overridden from QAbstractTableModel
Expand Down

0 comments on commit 65abe91

Please sign in to comment.