Fixed sort order for datetime columns with empty values (closes #2988)

A small fix belonging to #2531.
During the sorting empty QDateTime values are shuffled around due to
unstable sort in QSortFilterProxyModel (see #2526 and #2158), causing
the transfer list items to constantly change order.

Fixed by using an already existing correct comparison (with a torrent
hash fallback).
This commit is contained in:
Vladimir Sinenko 2015-05-19 22:10:57 +06:00
parent 2c1d76c87f
commit 56ee6dac08
2 changed files with 14 additions and 18 deletions

View file

@ -107,14 +107,7 @@ bool TransferListSortModel::lessThan(const QModelIndex &left, const QModelIndex
return QSortFilterProxyModel::lessThan(left, right); return QSortFilterProxyModel::lessThan(left, right);
} }
else if (column == TorrentModelItem::TR_ADD_DATE || column == TorrentModelItem::TR_SEED_DATE || column == TorrentModelItem::TR_SEEN_COMPLETE_DATE) { else if (column == TorrentModelItem::TR_ADD_DATE || column == TorrentModelItem::TR_SEED_DATE || column == TorrentModelItem::TR_SEEN_COMPLETE_DATE) {
QDateTime vL = left.data().toDateTime(); return dateLessThan(column, left, right);
QDateTime vR = right.data().toDateTime();
//not valid dates should be sorted at the bottom.
if (!vL.isValid()) return false;
if (!vR.isValid()) return true;
return vL < vR;
} }
else if (column == TorrentModelItem::TR_PRIORITY) { else if (column == TorrentModelItem::TR_PRIORITY) {
return lowerPositionThan(left, right); return lowerPositionThan(left, right);
@ -191,14 +184,7 @@ bool TransferListSortModel::lessThan(const QModelIndex &left, const QModelIndex
if (invalidL && invalidR) { if (invalidL && invalidR) {
if (seedingL) { //Both seeding if (seedingL) { //Both seeding
QDateTime dateL = model->data(model->index(left.row(), TorrentModelItem::TR_SEED_DATE)).toDateTime(); return dateLessThan(TorrentModelItem::TR_SEED_DATE, left, right);
QDateTime dateR = model->data(model->index(right.row(), TorrentModelItem::TR_SEED_DATE)).toDateTime();
//not valid dates should be sorted at the bottom.
if (!dateL.isValid()) return false;
if (!dateR.isValid()) return true;
return dateL < dateR;
} }
else { else {
return prioL < prioR; return prioL < prioR;
@ -253,8 +239,17 @@ bool TransferListSortModel::lowerPositionThan(const QModelIndex &left, const QMo
} }
// Sort according to TR_SEED_DATE // Sort according to TR_SEED_DATE
const QDateTime dateL = model->data(model->index(left.row(), TorrentModelItem::TR_SEED_DATE)).toDateTime(); return dateLessThan(TorrentModelItem::TR_SEED_DATE, left, right);
const QDateTime dateR = model->data(model->index(right.row(), TorrentModelItem::TR_SEED_DATE)).toDateTime(); }
// Every time we compare QDateTimes we need a fallback comparison in case both
// values are empty. This is a workaround for unstable sort in QSortFilterProxyModel
// (detailed discussion in #2526 and #2158).
bool TransferListSortModel::dateLessThan(const int dateColumn, const QModelIndex &left, const QModelIndex &right) const
{
const TorrentModel *model = dynamic_cast<TorrentModel*>(sourceModel());
const QDateTime dateL = model->data(model->index(left.row(), dateColumn)).toDateTime();
const QDateTime dateR = model->data(model->index(right.row(), dateColumn)).toDateTime();
if (dateL.isValid() && dateR.isValid()) { if (dateL.isValid() && dateR.isValid()) {
if (dateL != dateR) if (dateL != dateR)
return dateL < dateR; return dateL < dateR;

View file

@ -51,6 +51,7 @@ public:
private: private:
bool lessThan(const QModelIndex &left, const QModelIndex &right) const; bool lessThan(const QModelIndex &left, const QModelIndex &right) const;
bool lowerPositionThan(const QModelIndex &left, const QModelIndex &right) const; bool lowerPositionThan(const QModelIndex &left, const QModelIndex &right) const;
bool dateLessThan(const int dateColumn, const QModelIndex &left, const QModelIndex &right) const;
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const; bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
bool matchStatusFilter(int sourceRow, const QModelIndex &sourceParent) const; bool matchStatusFilter(int sourceRow, const QModelIndex &sourceParent) const;